采集参数【部分】
核心代码
定义结构体存放基本信息
type Params struct {
Url string //目标url
HttpType string //请求方式,默认get
Timeout int // 超时时间 秒
Transport *http.Transport // 请求配置
Headers *http.Header // 头部信息
}
dUrl := &CurlParams{}
设置代理(-x)
u, _ := url.Parse("http://" + ip + `:` + strconv.Itoa(int(port)))
dUrl.Transport.Proxy = http.ProxyURL(u)
设置头部
例如:
dUrl.Headers.Set("User-Agent", "curl/7.29.0")
dUrl.Headers.Set("Host", host)
设置–referer 跳转查询
dUrl.Headers.Set("Referer", url)
设置SSL跳过检测
dUrl.Transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
参数计算
初始化
client := &http.Client{}
http链路跟踪
var start,dns, connect time.Time
var connectTime,dNSDoneTime, firstRespByteTime int64
// 创建客户端请求跟踪
trace := &httptrace.ClientTrace{
DNSStart: func(dsi httptrace.DNSStartInfo) {
dns = time.Now()
},
DNSDone: func(ddi httptrace.DNSDoneInfo) {
dNSDoneTime = time.Since(dns).Milliseconds()
},
ConnectStart: func(network, addr string) { connect = time.Now() },
ConnectDone: func(network, addr string, err error) {
connectTime = time.Since(connect).Milliseconds()
}
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
采集计算
statusCode := resp.StatusCode // 响应状态码
dnsTime := dNSDoneTime //dns
fileSize := resp.ContentLength // 获取响应内容大小
connTime:= connectTime