-
Notifications
You must be signed in to change notification settings - Fork 43
/
http_client.go
120 lines (105 loc) · 2.61 KB
/
http_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package requester
import (
"crypto/tls"
"net/http"
"net/http/cookiejar"
"time"
)
// HTTPClient http client
type HTTPClient struct {
http.Client
transport *http.Transport
https bool
UserAgent string
}
// NewHTTPClient 返回 HTTPClient 的指针,
// 预设了一些配置
func NewHTTPClient() *HTTPClient {
h := &HTTPClient{
Client: http.Client{
Timeout: 30 * time.Second,
},
UserAgent: UserAgent,
}
h.Client.Jar, _ = cookiejar.New(nil)
return h
}
func (h *HTTPClient) lazyInit() {
if h.transport == nil {
h.transport = &http.Transport{
Proxy: proxyFunc,
DialContext: dialContext,
Dial: dial,
// DialTLS: h.dialTLSFunc(),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !h.https,
},
TLSHandshakeTimeout: 10 * time.Second,
DisableKeepAlives: false,
DisableCompression: false, // gzip
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
}
h.Client.Transport = h.transport
}
}
// SetUserAgent 设置 UserAgent 浏览器标识
func (h *HTTPClient) SetUserAgent(ua string) {
h.UserAgent = ua
}
// SetProxy 设置代理
func (h *HTTPClient) SetProxy(proxyAddr string) {
h.lazyInit()
u, err := checkProxyAddr(proxyAddr)
if err != nil {
h.transport.Proxy = http.ProxyFromEnvironment
return
}
h.transport.Proxy = http.ProxyURL(u)
}
// SetCookiejar 设置 cookie
func (h *HTTPClient) SetCookiejar(jar http.CookieJar) {
h.Client.Jar = jar
}
// ResetCookiejar 清空 cookie
func (h *HTTPClient) ResetCookiejar() {
h.Jar, _ = cookiejar.New(nil)
}
// SetHTTPSecure 是否启用 https 安全检查, 默认不检查
func (h *HTTPClient) SetHTTPSecure(b bool) {
h.https = b
h.lazyInit()
if b {
h.transport.TLSClientConfig = nil
} else {
h.transport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: !b,
}
}
}
// SetKeepAlive 设置 Keep-Alive
func (h *HTTPClient) SetKeepAlive(b bool) {
h.lazyInit()
h.transport.DisableKeepAlives = !b
}
// SetGzip 是否启用Gzip
func (h *HTTPClient) SetGzip(b bool) {
h.lazyInit()
h.transport.DisableCompression = !b
}
// SetResponseHeaderTimeout 设置目标服务器响应超时时间
func (h *HTTPClient) SetResponseHeaderTimeout(t time.Duration) {
h.lazyInit()
h.transport.ResponseHeaderTimeout = t
}
// SetTLSHandshakeTimeout 设置tls握手超时时间
func (h *HTTPClient) SetTLSHandshakeTimeout(t time.Duration) {
h.lazyInit()
h.transport.TLSHandshakeTimeout = t
}
// SetTimeout 设置 http 请求超时时间, 默认30s
func (h *HTTPClient) SetTimeout(t time.Duration) {
h.Client.Timeout = t
}