forked from qjfoidnh/BaiduPCS-Go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dial.go
177 lines (149 loc) · 3.67 KB
/
dial.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package requester
import (
"context"
"crypto/tls"
"errors"
"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/expires"
"github.com/qjfoidnh/BaiduPCS-Go/baidupcs/expires/cachemap"
mathrand "math/rand"
"net"
"net/http"
"net/url"
"strconv"
"time"
)
const (
// MaxDuration 最大的Duration
MaxDuration = 1<<63 - 1
)
var (
localTCPAddrList = []*net.TCPAddr{}
// ProxyAddr 代理地址
ProxyAddr string
// ErrProxyAddrEmpty 代理地址为空
ErrProxyAddrEmpty = errors.New("proxy addr is empty")
tcpCache = cachemap.GlobalCacheOpMap.LazyInitCachePoolOp("requester/tcp")
)
// SetLocalTCPAddrList 设置网卡地址
func SetLocalTCPAddrList(ips ...string) {
list := make([]*net.TCPAddr, 0, len(ips))
for k := range ips {
p := net.ParseIP(ips[k])
if p == nil {
continue
}
list = append(list, &net.TCPAddr{
IP: p,
})
}
localTCPAddrList = list
}
func proxyFunc(req *http.Request) (*url.URL, error) {
u, err := checkProxyAddr(ProxyAddr)
if err != nil {
return http.ProxyFromEnvironment(req)
}
return u, err
}
func getLocalTCPAddr() *net.TCPAddr {
if len(localTCPAddrList) == 0 {
return nil
}
i := mathrand.Intn(len(localTCPAddrList))
return localTCPAddrList[i]
}
func getDialer() *net.Dialer {
return &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
LocalAddr: getLocalTCPAddr(),
DualStack: true,
}
}
func checkProxyAddr(proxyAddr string) (u *url.URL, err error) {
if proxyAddr == "" {
return nil, ErrProxyAddrEmpty
}
host, port, err := net.SplitHostPort(proxyAddr)
if err == nil {
u = &url.URL{
Host: net.JoinHostPort(host, port),
}
return
}
u, err = url.Parse(proxyAddr)
if err == nil {
return
}
return
}
// SetGlobalProxy 设置代理
func SetGlobalProxy(proxyAddr string) {
ProxyAddr = proxyAddr
}
// SetTCPHostBind 设置host绑定ip
func SetTCPHostBind(host, ip string) {
tcpCache.Store(host, expires.NewDataExpires(net.ParseIP(ip), MaxDuration))
return
}
func getServerName(address string) string {
host, _, err := net.SplitHostPort(address)
if err != nil {
return address
}
return host
}
// resolveTCPHost
// 解析的tcpaddr没有port!!!
func resolveTCPHost(ctx context.Context, host string) (ip net.IP, err error) {
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, host)
if err != nil {
return
}
return addrs[0].IP, nil
}
func dialContext(ctx context.Context, network, address string) (conn net.Conn, err error) {
switch network {
case "tcp", "tcp4", "tcp6":
host, portStr, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
data, err := cachemap.GlobalCacheOpMap.CacheOperationWithError("requester/tcp", host, func() (expires.DataExpires, error) {
ip, err := resolveTCPHost(ctx, host)
if err != nil {
return nil, err
}
return expires.NewDataExpires(ip, 10*time.Minute), nil // 传值
})
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, err
}
return net.DialTCP(network, getLocalTCPAddr(), &net.TCPAddr{
IP: data.Data().(net.IP),
Port: port, // 设置端口
})
}
// 非 tcp 请求
conn, err = getDialer().DialContext(ctx, network, address)
return
}
func dial(network, address string) (conn net.Conn, err error) {
return dialContext(context.Background(), network, address)
}
func (h *HTTPClient) dialTLSFunc() func(network, address string) (tlsConn net.Conn, err error) {
return func(network, address string) (tlsConn net.Conn, err error) {
conn, err := dialContext(context.Background(), network, address)
if err != nil {
return nil, err
}
return tls.Client(conn, &tls.Config{
ServerName: getServerName(address),
InsecureSkipVerify: !h.https,
}), nil
}
}