-
Notifications
You must be signed in to change notification settings - Fork 5
/
http_client.go
119 lines (105 loc) · 2.78 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
package rod_helper
import (
"crypto/tls"
"github.com/go-resty/resty/v2"
"net/http"
"net/url"
"time"
)
// NewHttpClient 新建一个 resty 的对象
func NewHttpClient(opt *HttpClientOptions) (*resty.Client, error) {
var UserAgent string
// ------------------------------------------------
// 随机的 Browser
UserAgent = RandomUserAgent()
// ------------------------------------------------
httpClient := resty.New().SetTransport(&http.Transport{
DisableKeepAlives: true,
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 1000,
})
httpClient.SetTimeout(opt.htmlTimeOut)
httpClient.SetRetryCount(1)
// ------------------------------------------------
// 设置 Referer
if len(opt.Referer()) > 0 {
httpClient.SetHeader("Referer", opt.Referer())
}
// ------------------------------------------------
// 设置 Header
httpClient.SetHeaders(map[string]string{
"Content-Type": "application/json",
"User-Agent": UserAgent,
})
// ------------------------------------------------
// 不要求安全链接
httpClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
// ------------------------------------------------
proxyType, proxyUrl := opt.ProxyUrl()
switch proxyType {
case Http:
httpClient.SetProxy(proxyUrl)
case Socks5:
proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(proxyUrl)
}
httpTransport := &http.Transport{
Proxy: proxy,
DisableKeepAlives: true,
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 1000,
}
httpClient.SetTransport(httpTransport)
default:
httpClient.RemoveProxy()
}
return httpClient, nil
}
type HttpClientOptions struct {
htmlTimeOut time.Duration
proxyType ProxyType
httpProxyUrl string
socks5ProxyUrl string
referer string
}
func NewHttpClientOptions(HTMLTimeOut time.Duration) *HttpClientOptions {
return &HttpClientOptions{
htmlTimeOut: HTMLTimeOut,
}
}
func (h *HttpClientOptions) HtmlTimeOut() time.Duration {
return h.htmlTimeOut
}
// SetHttpProxy 输入这样的代理连接:http://127.0.0.1:9150
func (h *HttpClientOptions) SetHttpProxy(SetHttpProxy string) {
h.proxyType = Http
h.httpProxyUrl = SetHttpProxy
}
// SetSocks5Proxy 输入这样的代理连接:socks5://127.0.0.1:9150
func (h *HttpClientOptions) SetSocks5Proxy(SetSock5Proxy string) {
h.proxyType = Socks5
h.socks5ProxyUrl = SetSock5Proxy
}
func (h *HttpClientOptions) ProxyUrl() (ProxyType, string) {
switch h.proxyType {
case None:
return None, ""
case Http:
return Http, h.httpProxyUrl
case Socks5:
return Socks5, h.socks5ProxyUrl
}
return None, ""
}
func (h *HttpClientOptions) SetReferer(referer string) {
h.referer = referer
}
func (h *HttpClientOptions) Referer() string {
return h.referer
}
type ProxyType int
const (
None ProxyType = iota + 1
Http
Socks5
)