forked from hidu/proxy-manager
/
client.go
340 lines (297 loc) · 7.45 KB
/
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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package internal
import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"sync"
"time"
)
type requestLog struct {
startTime time.Time
req *http.Request
data []string
logData []string
logID int64
mux sync.Mutex
}
func newRequestLog(req *http.Request) *requestLog {
rlog := &requestLog{req: req}
rlog.reset()
return rlog
}
func (rlog *requestLog) print() {
rlog.mux.Lock()
defer rlog.mux.Unlock()
if len(rlog.logData) == 0 {
return
}
used := time.Since(rlog.startTime)
log.Println("logID:", rlog.logID,
rlog.req.Method, rlog.req.URL.String(),
strings.Join(rlog.data, " "),
strings.Join(rlog.logData, " "),
"used:", used.String())
rlog.reset()
}
func (rlog *requestLog) setLog(arg ...any) {
rlog.mux.Lock()
defer rlog.mux.Unlock()
rlog.data = append(rlog.logData, fmt.Sprint(arg))
}
func (rlog *requestLog) addLog(arg ...any) {
rlog.mux.Lock()
defer rlog.mux.Unlock()
rlog.logData = append(rlog.logData, fmt.Sprint(arg))
}
func (rlog *requestLog) reset() {
rlog.startTime = time.Now()
rlog.logData = []string{}
}
type httpClient struct {
ProxyManager *ProxyManager
}
func newHTTPClient(manager *ProxyManager) *httpClient {
log.Println("loading http client...")
proxy := new(httpClient)
proxy.ProxyManager = manager
return proxy
}
func _reqFix(req *http.Request) {
if req.Method == "CONNECT" && req.URL.Scheme == "" {
req.URL.Scheme = "https"
}
if req.URL.Scheme == "" {
req.URL.Scheme = "http"
}
}
func (hc *httpClient) ServeHTTP(w http.ResponseWriter, req *http.Request) {
_reqFix(req)
rlog := newRequestLog(req)
rlog.logID = hc.ProxyManager.reqNum + time.Now().Unix()
defer rlog.print()
user := getProxyAuthorInfo(req)
rlog.setLog("uname", user.Name)
if ProxyDebug {
dump, _ := httputil.DumpRequest(req, true)
log.Println("request dump:\n", string(dump))
}
if !hc.ProxyManager.checkHTTPAuth(user) {
rlog.addLog("auth", "failed")
w.Header().Set("Proxy-Authenticate", "Basic realm=auth need")
w.WriteHeader(407)
w.Write([]byte("proxy auth failed"))
return
}
req.RequestURI = ""
req.Header.Del("Connection")
req.Header.Del("Proxy-Connection")
req.Header.Del("Proxy-Authorization")
_statusOk := strings.Split(req.Header.Get("X-Man-Status-Ok"), ",")
statusOk := make(map[int]int)
for _, v := range _statusOk {
_code, _ := strconv.Atoi(v)
if _code > 0 {
statusOk[_code] = 1
}
}
_clientReTry := -1
xManRetry := req.Header.Get("X-Man-ReTry")
if xManRetry != "" {
_clientReTry, _ = strconv.Atoi(xManRetry)
}
for k := range req.Header {
k = strings.ToLower(k)
if strings.HasPrefix(k, "x-man") || strings.HasPrefix(k, "proxy-") {
req.Header.Del(k)
}
}
if req.Body != nil {
defer req.Body.Close()
}
var resp *http.Response
var err error
maxReTry := hc.ProxyManager.config.getReTry() + 1
if _clientReTry >= 0 && _clientReTry <= hc.ProxyManager.config.ReTryMax {
maxReTry = _clientReTry + 1
}
if req.Method == http.MethodConnect {
hc.handlerHTTPS(w, req, user, rlog)
return
}
no := 1
for ; no <= maxReTry; no++ {
rlog.addLog("try", fmt.Sprintf("%d/%d", no, maxReTry))
proxy, err := hc.ProxyManager.proxyPool.getOneProxy(user.Name)
if err != nil {
rlog.addLog("get_proxy_failed", err)
rlog.print()
break
}
rlog.addLog("proxy", proxy.proxy)
rlog.addLog("proxyUsed", proxy.Used)
client, err := newClient(proxy.URL, hc.ProxyManager.config.getTimeout())
if err != nil {
rlog.addLog("get http client failed", err)
continue
}
resp, err = client.Do(req)
if err == nil {
if ProxyDebug {
dump, _ := httputil.DumpResponse(resp, true)
log.Println("response dump:\n", string(dump))
}
if hc.ProxyManager.config.IsWrongCode(resp.StatusCode) {
rlog.addLog("statusCode wrong def in conf", resp.StatusCode)
goto failed
}
if len(statusOk) != 0 {
if _, has := statusOk[resp.StatusCode]; !has {
rlog.addLog("statusCode wrong", resp.StatusCode)
goto failed
}
}
hc.ProxyManager.proxyPool.markProxyStatus(proxy, proxyUsedSuc)
break
} else {
rlog.addLog("response error", err.Error())
}
failed:
{
hc.ProxyManager.proxyPool.markProxyStatus(proxy, proxyUsedFailed)
rlog.addLog("failed")
if no == maxReTry {
rlog.addLog("all failed")
}
rlog.print()
}
}
w.Header().Set("x-man-try", fmt.Sprintf("%d/%d", no, maxReTry))
w.Header().Set("x-man-id", strconv.FormatInt(rlog.logID, 10))
if err != nil || resp == nil {
w.WriteHeader(550)
w.Write([]byte("all failed," + fmt.Sprintf("try:%d", no)))
return
}
// log.Println("resp.Header:", resp.Header)
resp.Header.Del("Content-Length")
resp.Header.Del("Connection")
w.WriteHeader(resp.StatusCode)
rlog.addLog("status:", resp.StatusCode)
copyHeaders(w.Header(), resp.Header)
n, err := io.Copy(w, resp.Body)
rlog.addLog("res_len:", n)
if err != nil {
// client may be not read the body
rlog.addLog("io.copy_err:", err)
}
if err := resp.Body.Close(); err != nil {
rlog.addLog("close response body err:", err)
}
rlog.addLog("OK")
}
func (hc *httpClient) handlerHTTPS(w http.ResponseWriter, req *http.Request, user *User, rlog *requestLog) {
proxy, err := hc.ProxyManager.proxyPool.getOneProxy(user.Name)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("no proxy"))
rlog.addLog("no proxy")
return
}
host := proxy.URL.Host
if !strings.Contains(host, ":") {
host += ":443"
}
h, p, err := net.SplitHostPort(host)
if err != nil {
rlog.addLog("invalid host:", host)
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("parser proxy host failed, host=" + proxy.URL.Host))
return
}
address := net.JoinHostPort(h, p)
sConn, err := net.DialTimeout("tcp", address, hc.ProxyManager.config.getTimeout())
if err != nil {
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("connect to proxy failed:" + err.Error()))
rlog.addLog("connect to proxy failed:", err)
return
}
defer sConn.Close()
deadLine := time.Now().Add(hc.ProxyManager.config.getTimeout())
sConn.SetDeadline(deadLine)
hj, ok := w.(http.Hijacker)
if !ok {
rlog.addLog("can not hijack")
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("can not hijack"))
return
}
conn, _, err := hj.Hijack()
if err != nil {
rlog.addLog("hijack failed")
w.WriteHeader(http.StatusBadGateway)
w.Write([]byte("can not hijack"))
return
}
defer conn.Close()
conn.SetDeadline(deadLine)
sConn.Write([]byte("CONNECT " + req.URL.Host + " HTTP/1.1\r\n"))
sConn.Write([]byte("Connection: close\r\n"))
sConn.Write([]byte("Host: " + req.URL.Host + "\r\n\r\n"))
if err = connCopyLimit(conn, sConn); err != nil {
rlog.addLog("copy CONNECT message header failed:", err)
return
}
var closeOnce sync.Once
doClose := func() {
closeOnce.Do(func() {
sConn.Close()
conn.Close()
})
}
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
n, e := io.Copy(conn, sConn)
rlog.addLog("copy.toClient", n, e)
doClose()
}()
go func() {
defer wg.Done()
n, e := io.Copy(sConn, conn)
rlog.addLog("copy.toServer", n, e)
doClose()
}()
go func() {
defer wg.Done()
<-req.Context().Done()
doClose()
}()
wg.Wait()
rlog.addLog("https handler finished")
}
func connCopyLimit(dst net.Conn, src net.Conn) error {
bf := make([]byte, 12)
n, err := io.ReadFull(src, bf)
if err != nil {
return err
}
_, err = dst.Write(bf[:n])
return err
}
func copyHeaders(dst, src http.Header) {
for k, vs := range src {
if len(k) > 5 && k[:6] == "Proxy-" {
continue
}
for _, v := range vs {
dst.Add(k, v)
}
}
}