-
Notifications
You must be signed in to change notification settings - Fork 1
/
request_setting.go
68 lines (61 loc) · 1.82 KB
/
request_setting.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
package requests
import (
"net/http"
"net/url"
)
type RequestSetting func(client *http.Client, request *http.Request) error
const DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"
// DefaultRequestSetting 每个请求默认添加的设置
func DefaultRequestSetting() RequestSetting {
return func(client *http.Client, httpRequest *http.Request) error {
settings := []RequestSetting{
RequestSettingUserAgent(),
RequestSettingSkipTlsVerify(),
}
for _, setting := range settings {
err := setting(client, httpRequest)
if err != nil {
return err
}
}
return nil
}
}
// RequestSettingUserAgent 设置User-Agent,如果不传递的则使用默认的User-Agent
func RequestSettingUserAgent(userAgent ...string) RequestSetting {
if len(userAgent) == 0 {
userAgent = append(userAgent, DefaultUserAgent)
}
return func(client *http.Client, httpRequest *http.Request) error {
httpRequest.Header.Set("User-Agent", userAgent[0])
return nil
}
}
// RequestSettingProxy 配置代理IP
func RequestSettingProxy(proxy string) RequestSetting {
parse, err := url.Parse(proxy)
return func(client *http.Client, httpRequest *http.Request) error {
if err != nil {
return err
}
transport, ok := client.Transport.(*http.Transport)
if !ok {
transport = &http.Transport{}
}
transport.Proxy = http.ProxyURL(parse)
client.Transport = transport
return nil
}
}
// RequestSettingSkipTlsVerify 跳过https证书验证
func RequestSettingSkipTlsVerify() RequestSetting {
return func(client *http.Client, httpRequest *http.Request) error {
transport, ok := client.Transport.(*http.Transport)
if !ok {
transport = &http.Transport{}
}
transport.TLSClientConfig.InsecureSkipVerify = true
client.Transport = transport
return nil
}
}