-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
http_client_util.go
91 lines (81 loc) · 2.32 KB
/
http_client_util.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
package util
import (
"context"
"crypto/tls"
"net"
"net/http"
"time"
)
var dialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
var defaultTransport = &http.Transport{
// from http.DefaultTransport
Proxy: http.ProxyFromEnvironment,
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.DialContext(ctx, network, address)
},
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// CreateHTTPClient Create Default HTTP Client
func CreateHTTPClient() *http.Client {
return &http.Client{
Timeout: 30 * time.Second,
Transport: defaultTransport,
}
}
var noProxyTcp4Transport = &http.Transport{
// no proxy
// DisableKeepAlives
DisableKeepAlives: true,
// tcp4
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.DialContext(ctx, "tcp4", address)
},
// from http.DefaultTransport
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
var noProxyTcp6Transport = &http.Transport{
// no proxy
// DisableKeepAlives
DisableKeepAlives: true,
// tcp6
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.DialContext(ctx, "tcp6", address)
},
// from http.DefaultTransport
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// CreateNoProxyHTTPClient Create NoProxy HTTP Client
func CreateNoProxyHTTPClient(network string) *http.Client {
if network == "tcp6" {
return &http.Client{
Timeout: 30 * time.Second,
Transport: noProxyTcp6Transport,
}
}
return &http.Client{
Timeout: 30 * time.Second,
Transport: noProxyTcp4Transport,
}
}
// SetInsecureSkipVerify 将所有 http.Transport 的 InsecureSkipVerify 设置为 true
func SetInsecureSkipVerify() {
transports := []*http.Transport{defaultTransport, noProxyTcp4Transport, noProxyTcp6Transport}
for _, transport := range transports {
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
}