-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhttpProxy.go
More file actions
307 lines (266 loc) · 7.15 KB
/
Copy pathhttpProxy.go
File metadata and controls
307 lines (266 loc) · 7.15 KB
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
package vproxy
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
utls "github.com/refraction-networking/utls"
"golang.org/x/net/http2"
)
type h2clientConn struct {
*http2.ClientConn
used atomic.Bool
}
type proxyHTTP struct {
*http.Transport
proxy *Proxy
mu sync.RWMutex // Use RWMutex for better concurrency
h2Conns map[string][]*h2clientConn // H2 连接缓存 (per host)\
}
type contextKey string
const (
ctxKeyProxyConn contextKey = "proxyConn"
ctxKeySkipVerify contextKey = "skipVerify"
ctxKeyBrowserVer contextKey = "browserVer"
ctxKeyRequest contextKey = "httpRequest"
)
type connHolder struct {
mu sync.Mutex
conn net.Conn
}
func (h *connHolder) Take() net.Conn {
h.mu.Lock()
defer h.mu.Unlock()
c := h.conn
h.conn = nil
return c
}
func newProxyHTTP(p *Proxy) *proxyHTTP {
tr := &http.Transport{
MaxIdleConns: 100, // Improved for performance
MaxIdleConnsPerHost: 10, // Better reuse
IdleConnTimeout: 90 * time.Second,
}
phttp := &proxyHTTP{
Transport: tr,
proxy: p,
h2Conns: make(map[string][]*h2clientConn),
}
// http
tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if holder, ok := ctx.Value(ctxKeyProxyConn).(*connHolder); ok {
if c := holder.Take(); c != nil {
return c, nil
}
}
return p.dial(ctx, network, addr)
}
// ssl
tr.DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if holder, ok := ctx.Value(ctxKeyProxyConn).(*connHolder); ok {
if c := holder.Take(); c != nil {
return c, nil
}
}
return phttp.dialUTLS(ctx, network, addr)
}
return phttp
}
func cleanProxyHeaders(req *http.Request) {
for k := range req.Header {
if strings.HasPrefix(strings.ToLower(k), "proxy-") {
delete(req.Header, k)
}
}
req.Header.Set("referer", req.URL.String()) // 设置 referer 为当前请求的 URL
req.Header.Set("origin", req.URL.Scheme+"://"+req.URL.Host) // 设置 origin 为当前请求的协议和主机名
}
func (T *proxyHTTP) htt2Client(targetAddr string, req *http.Request) (*http.Response, bool) {
T.mu.Lock()
var h2cc *h2clientConn
for _, cc := range T.h2Conns[targetAddr] {
if cc.CanTakeNewRequest() && !cc.used.Swap(true) {
h2cc = cc
break
}
}
T.mu.Unlock()
if h2cc != nil {
if resp, err := h2cc.RoundTrip(req); err == nil {
h2cc.used.Store(false)
return resp, true
}
// Cleanup stale
T.mu.Lock()
h2cc.Close()
h2ccs := T.h2Conns[targetAddr]
if len(h2ccs) == 1 && h2ccs[0] == h2cc {
delete(T.h2Conns, targetAddr)
} else {
for i, cc := range h2ccs {
if cc == h2cc {
T.h2Conns[targetAddr] = append(h2ccs[:i], h2ccs[i+1:]...)
break
}
}
}
T.mu.Unlock()
}
return nil, false
}
func (T *proxyHTTP) roundTrip(req *http.Request) (*http.Response, error) {
ctx := context.WithValue(req.Context(), ctxKeyRequest, req)
targetAddr := host2addr(req.URL.Host, req.URL.Scheme)
if req.URL.Scheme == "https" {
// 尝试复用现有 H2 连接
if resp, ok := T.htt2Client(targetAddr, req); ok {
return resp, nil
}
uConn, err := T.dialUTLS(ctx, "tcp", targetAddr)
if err != nil {
return nil, err
}
if uConn.ConnectionState().NegotiatedProtocol == "h2" {
h2Conn, err := T.http2ClientConn(uConn)
if err != nil {
uConn.Close()
return nil, fmt.Errorf("h2 client conn: %w", err)
}
resp, err := h2Conn.RoundTrip(req)
if err != nil {
h2Conn.Close()
return nil, err
}
// 请求成功,将连接加入池
T.mu.Lock()
T.h2Conns[targetAddr] = append(T.h2Conns[targetAddr], &h2clientConn{ClientConn: h2Conn})
T.mu.Unlock()
return resp, nil
}
// HTTP/1.1 over TLS
holder := &connHolder{conn: uConn}
ctx = context.WithValue(ctx, ctxKeyProxyConn, holder)
req = req.WithContext(ctx)
resp, err := T.Transport.RoundTrip(req)
if unusedConn := holder.Take(); unusedConn != nil {
unusedConn.Close()
}
return resp, err
}
// HTTP (non-TLS)
req = req.WithContext(ctx)
return T.Transport.RoundTrip(req)
}
func (T *proxyHTTP) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()
query := req.URL.Query()
if query.Get("@skipVerify") == "true" {
ctx = context.WithValue(ctx, ctxKeySkipVerify, true)
query.Del("@skipVerify")
req.URL.RawQuery = query.Encode()
}
if browserVer := query.Get("@browserVer"); browserVer != "" {
ctx = context.WithValue(ctx, ctxKeyBrowserVer, browserVer)
query.Del("@browserVer")
req.URL.RawQuery = query.Encode()
}
cReq := req.WithContext(ctx)
cReq.RequestURI = ""
if _, ok := cReq.Header["Accept-Encoding"]; !ok {
cReq.Header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
}
if cReq.URL.Host != "" {
cReq.Host = cReq.URL.Host
} else if cReq.Host != "" {
cReq.URL.Host = cReq.Host
}
if cReq.URL.Scheme == "" {
cReq.URL.Scheme = "https"
}
cleanProxyHeaders(cReq)
resp, err := T.roundTrip(cReq)
if err != nil {
T.proxy.resErr(rw, err.Error())
return
}
defer resp.Body.Close()
T.proxy.logf(Response, "响应:\r\n%v", resp)
wh := rw.Header()
copyHeaders(wh, resp.Header)
if len(resp.Trailer) > 0 {
var trailerKeys []string
for k := range resp.Trailer {
trailerKeys = append(trailerKeys, k)
}
rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
}
rw.WriteHeader(resp.StatusCode)
if fl, ok := rw.(http.Flusher); ok && len(resp.Trailer) > 0 {
fl.Flush()
}
// 优化 copyDate 调用,直接传递 Body
copyData(rw, resp.Body, T.proxy.DataBufioSize)
}
func copyHeaders(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func (T *proxyHTTP) CloseIdleConnections() {
T.mu.Lock()
defer T.mu.Unlock()
for k, ccs := range T.h2Conns {
for _, cc := range ccs {
cc.Close()
}
delete(T.h2Conns, k)
}
T.Transport.CloseIdleConnections()
}
func (T *proxyHTTP) http2ClientConn(uConn net.Conn) (*http2.ClientConn, error) {
t2 := &http2.Transport{
StrictMaxConcurrentStreams: true,
AllowHTTP: false,
}
return t2.NewClientConn(uConn)
}
func (T *proxyHTTP) uConn(ctx context.Context, conn net.Conn, targetAddr string) (*utls.UConn, error) {
host, _, err := net.SplitHostPort(targetAddr)
if err != nil {
host = targetAddr
}
uconfig := &utls.Config{
ServerName: host,
NextProtos: []string{"h2", "http/1.1"},
InsecureSkipVerify: false, // Keep secure by default
}
if skip, ok := ctx.Value(ctxKeySkipVerify).(bool); ok {
uconfig.InsecureSkipVerify = skip
}
chromeVer := utls.HelloChrome_Auto
if ver, ok := ctx.Value(ctxKeyBrowserVer).(string); ok {
chromeVer = utls.ClientHelloID{Client: "Chrome", Version: ver, Seed: nil, Weights: nil}
}
tlsConn := utls.UClient(conn, uconfig, chromeVer)
tlsConn.SetDeadline(time.Now().Add(15 * time.Second))
if err := tlsConn.Handshake(); err != nil {
conn.Close()
return nil, fmt.Errorf("utls handshake failed: %w", err)
}
// 握手完成,清除超时
tlsConn.SetDeadline(time.Time{})
return tlsConn, nil
}
func (T *proxyHTTP) dialUTLS(ctx context.Context, network, addr string) (*utls.UConn, error) {
conn, err := T.proxy.dial(ctx, network, addr)
if err != nil {
return nil, err
}
return T.uConn(ctx, conn, addr)
}