-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_http_forward.go
539 lines (477 loc) · 15.3 KB
/
handler_http_forward.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package main
import (
"cmp"
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"net/url"
"runtime"
"slices"
"strings"
"text/template"
"time"
"github.com/jszwec/csvutil"
"github.com/mileusna/useragent"
"github.com/phuslu/log"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/publicsuffix"
)
type HTTPForwardHandler struct {
Config HTTPConfig
ForwardLogger log.Logger
LocalDialer *LocalDialer
LocalTransport *http.Transport
Dialers map[string]Dialer
Functions template.FuncMap
policy *template.Template
dialer *template.Template
transports map[string]*http.Transport
csvloader *FileLoader[[]ForwardAuthInfo]
}
func (h *HTTPForwardHandler) Load() error {
var err error
if s := h.Config.Forward.Policy; s != "" {
if h.policy, err = template.New(s).Funcs(h.Functions).Parse(s); err != nil {
return err
}
}
if s := h.Config.Forward.Dialer; s != "" {
if h.dialer, err = template.New(s).Funcs(h.Functions).Parse(s); err != nil {
return err
}
}
if len(h.Dialers) != 0 {
h.transports = make(map[string]*http.Transport)
for name, dailer := range h.Dialers {
h.transports[name] = &http.Transport{
DialContext: dailer.DialContext,
TLSClientConfig: h.LocalTransport.TLSClientConfig,
TLSHandshakeTimeout: h.LocalTransport.TLSHandshakeTimeout,
IdleConnTimeout: h.LocalTransport.IdleConnTimeout,
DisableCompression: h.LocalTransport.DisableCompression,
MaxIdleConns: 32,
}
}
}
if strings.HasSuffix(h.Config.Forward.AuthTable, ".csv") {
h.csvloader = &FileLoader[[]ForwardAuthInfo]{
Filename: h.Config.Forward.AuthTable,
Unmarshal: csvutil.Unmarshal,
PollDuration: 15 * time.Second,
ErrorLogger: log.DefaultLogger.Std("", 0),
}
records := h.csvloader.Load()
if records == nil {
log.Fatal().Strs("server_name", h.Config.ServerName).Str("auth_table", h.Config.Forward.AuthTable).Msg("load auth_table failed")
}
log.Info().Strs("server_name", h.Config.ServerName).Str("auth_table", h.Config.Forward.AuthTable).Int("auth_table_size", len(*records)).Msg("load auth_table ok")
}
if h.Config.Forward.BindInterface != "" {
if runtime.GOOS != "linux" {
log.Fatal().Strs("server_name", h.Config.ServerName).Msg("option bind_device is only available on linux")
}
if h.Config.Forward.Dialer != "" {
log.Fatal().Strs("server_name", h.Config.ServerName).Msg("option bind_device is confilict with option dialer")
}
dialer := new(LocalDialer)
*dialer = *h.LocalDialer
dialer.BindInterface = h.Config.Forward.BindInterface
dialer.PreferIPv6 = h.Config.Forward.PreferIpv6
h.LocalDialer = dialer
h.LocalTransport = &http.Transport{
DialContext: dialer.DialContext,
TLSClientConfig: h.LocalTransport.TLSClientConfig,
TLSHandshakeTimeout: h.LocalTransport.TLSHandshakeTimeout,
IdleConnTimeout: h.LocalTransport.IdleConnTimeout,
MaxIdleConns: h.LocalTransport.MaxIdleConns,
DisableCompression: h.LocalTransport.DisableCompression,
}
}
return nil
}
func (h *HTTPForwardHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
ri := req.Context().Value(RequestInfoContextKey).(*RequestInfo)
websocket := h.Config.Forward.Websocket != "" && req.URL.Path == h.Config.Forward.Websocket && ((req.Method == http.MethodGet && req.ProtoMajor == 1) || (req.Method == http.MethodConnect && req.ProtoAtLeast(2, 0)))
if websocket {
host, port := req.URL.Query().Get("host"), req.URL.Query().Get("port")
if host == "" && port == "" {
host, port = req.URL.Query().Get("h"), req.URL.Query().Get("p")
}
req.Host = net.JoinHostPort(host, port)
req.URL = &url.URL{Host: req.Host}
req.Method = http.MethodConnect
}
var err error
var host = req.Host
if h, _, err := net.SplitHostPort(req.Host); err == nil {
host = h
}
var domain = host
if net.ParseIP(domain) == nil {
if s, err := publicsuffix.EffectiveTLDPlusOne(host); err == nil {
domain = s
}
}
if h.Config.Forward.Policy == "" {
http.NotFound(rw, req)
return
}
var bypassAuth bool
var sb strings.Builder
if h.policy != nil {
sb.Reset()
err = h.policy.Execute(&sb, struct {
Request *http.Request
ClientHelloInfo *tls.ClientHelloInfo
UserAgent *useragent.UserAgent
ServerAddr string
}{req, ri.ClientHelloInfo, &ri.UserAgent, ri.ServerAddr})
if err != nil {
log.Error().Err(err).Context(ri.LogContext).Str("forward_policy", h.Config.Forward.Policy).Interface("client_hello_info", ri.ClientHelloInfo).Interface("tls_connection_state", req.TLS).Msg("execute forward_policy error")
http.NotFound(rw, req)
return
}
output := strings.TrimSpace(sb.String())
log.Debug().Context(ri.LogContext).Interface("client_hello_info", ri.ClientHelloInfo).Interface("tls_connection_state", req.TLS).Str("forward_policy_output", output).Msg("execute forward_policy ok")
switch output {
case "", "proxy_pass":
http.NotFound(rw, req)
return
case "reject", "deny":
RejectRequest(rw, req)
return
case "reset", "close":
if hijacker, ok := rw.(http.Hijacker); ok {
if conn, _, err := hijacker.Hijack(); err == nil {
conn.Close()
}
}
return
case "require_auth", "require_proxy_auth", "require_www_auth":
var authCode int
var authHeader, authText string
switch output {
case "require_www_auth":
authCode = http.StatusUnauthorized
authHeader = "www-authenticate"
authText = "Authentication Required"
default:
authCode = http.StatusProxyAuthRequired
authHeader = "proxy-authenticate"
authText = "Authentication Required"
}
resp := &http.Response{
StatusCode: authCode,
Header: http.Header{
"content-type": []string{"text/plain; charset=UTF-8"},
authHeader: []string{fmt.Sprintf("Basic realm=\"%s\"", authText)},
},
Request: req,
ContentLength: int64(len(authText)),
Body: io.NopCloser(strings.NewReader(authText)),
}
for key, values := range resp.Header {
for _, value := range values {
rw.Header().Add(key, value)
}
}
rw.WriteHeader(resp.StatusCode)
io.Copy(rw, resp.Body)
return
case "bypass_auth":
bypassAuth = true
}
}
var ai ForwardAuthInfo
if h.Config.Forward.AuthTable != "" && !bypassAuth {
ai, err = h.GetAuthInfo(ri, req)
if err != nil {
log.Warn().Err(err).Context(ri.LogContext).Str("username", ai.Username).Str("proxy_authorization", req.Header.Get("proxy-authorization")).Msg("auth error")
RejectRequest(rw, req)
return
}
}
if ai.VIP == 0 {
if ai.SpeedLimit == 0 && h.Config.Forward.SpeedLimit > 0 {
ai.SpeedLimit = h.Config.Forward.SpeedLimit
}
}
var dialerName = ""
if h.dialer != nil {
sb.Reset()
err := h.dialer.Execute(&sb, struct {
Request *http.Request
ClientHelloInfo *tls.ClientHelloInfo
UserAgent *useragent.UserAgent
ServerAddr string
User ForwardAuthInfo
}{req, ri.ClientHelloInfo, &ri.UserAgent, ri.ServerAddr, ai})
if err != nil {
log.Error().Err(err).Context(ri.LogContext).Str("forward_dialer_name", h.Config.Forward.Dialer).Msg("execute forward_dialer error")
http.NotFound(rw, req)
return
}
dialerName = strings.TrimSpace(sb.String())
}
log.Info().Context(ri.LogContext).Str("username", ai.Username).Str("dialer_name", dialerName).Str("http_domain", domain).Msg("forward request")
var transmitBytes int64
switch req.Method {
case http.MethodConnect:
if req.URL.Host == ri.ServerName {
// FIXME: handle self-connect clients
}
var dialer Dialer
if dialerName != "" {
if d, ok := h.Dialers[dialerName]; !ok {
log.Error().Context(ri.LogContext).Str("dialer", dialerName).Msg("no dialer exists")
http.NotFound(rw, req)
return
} else {
dialer = d
}
} else {
dialer = h.LocalDialer
}
conn, err := dialer.DialContext(req.Context(), "tcp", req.Host)
if err != nil {
log.Error().Err(err).Context(ri.LogContext).Msg("dial host error")
http.Error(rw, err.Error(), http.StatusBadGateway)
return
}
var w io.Writer
var r io.Reader
if req.ProtoAtLeast(2, 0) {
flusher, ok := rw.(http.Flusher)
if !ok {
http.Error(rw, fmt.Sprintf("%#v is not http.Flusher", rw), http.StatusBadGateway)
return
}
if websocket {
key := sha1.Sum([]byte(req.Header.Get("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))
rw.Header().Set("sec-websocket-accept", string(key[:]))
rw.Header().Set("upgrade", "websocket")
rw.Header().Set("connection", "Upgrade")
rw.WriteHeader(http.StatusSwitchingProtocols)
} else {
rw.WriteHeader(http.StatusOK)
}
flusher.Flush()
w = FlushWriter{rw}
r = req.Body
} else {
hijacker, ok := rw.(http.Hijacker)
if !ok {
http.Error(rw, fmt.Sprintf("%#v is not http.Hijacker", rw), http.StatusBadGateway)
return
}
lconn, _, err := hijacker.Hijack()
if err != nil {
http.Error(rw, err.Error(), http.StatusBadGateway)
return
}
defer lconn.Close()
w = lconn
r = lconn
if websocket {
key := sha1.Sum([]byte(req.Header.Get("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"))
fmt.Fprintf(lconn, "HTTP/1.1 101 Switching Protocols\r\nSec-WebSocket-Accept: %s\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n", key[:])
} else {
io.WriteString(lconn, "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n")
}
}
defer conn.Close()
go io.Copy(conn, r)
if h.Config.Forward.Log {
w = &ForwardLogWriter{
Writer: w,
Logger: h.ForwardLogger,
Context: log.NewContext(nil).
Xid("trace_id", ri.TraceID).
Str("server_name", ri.ServerName).
Str("server_addr", ri.ServerAddr).
Str("tls_version", ri.TLSVersion.String()).
Str("username", ai.Username).
Str("remote_ip", ri.RemoteIP).
Str("remote_country", ri.GeoipInfo.Country).
Str("remote_region", ri.GeoipInfo.Region).
Str("remote_city", ri.GeoipInfo.City).
Str("http_method", req.Method).
Str("http_host", host).
Str("http_domain", domain).
Str("http_proto", req.Proto).
Str("user_agent", req.UserAgent()).
Str("user_agent_os", ri.UserAgent.OS).
Str("user_agent_os_version", ri.UserAgent.OSVersion).
Str("user_agent_name", ri.UserAgent.Name).
Str("user_agent_version", ri.UserAgent.Version).
Value(),
FieldName: "transmit_bytes",
Interval: cmp.Or(h.Config.Forward.LogInterval, 1),
}
}
transmitBytes, err = io.CopyBuffer(w, NewRateLimitReader(conn, ai.SpeedLimit), make([]byte, 1024*1024)) // buffer size should align to http2.MaxReadFrameSize
log.Debug().Context(ri.LogContext).Str("username", ai.Username).Str("http_domain", domain).Int64("transmit_bytes", transmitBytes).Err(err).Msg("forward log")
default:
if req.Host == "" {
http.NotFound(rw, req)
return
}
if req.URL.Host == "" {
req.URL.Host = req.Host
}
if req.ContentLength == 0 {
io.Copy(io.Discard, req.Body)
req.Body.Close()
req.Body = nil
}
if req.URL.Scheme == "" {
req.URL.Scheme = "http"
}
if req.URL.Host == "" {
req.URL.Host = req.Host
}
h2 := req.ProtoAtLeast(2, 0)
if h2 {
req.ProtoMajor = 1
req.ProtoMinor = 1
req.Proto = "HTTP/1.1"
}
var tr *http.Transport
if dialerName != "" {
if t, ok := h.transports[dialerName]; !ok {
log.Error().Context(ri.LogContext).Str("dialer", dialerName).Msg("no dialer transport exists")
http.NotFound(rw, req)
return
} else {
tr = t
}
} else {
tr = h.LocalTransport
}
resp, err := tr.RoundTrip(req)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadGateway)
return
}
if h2 {
resp.Header.Del("connection")
resp.Header.Del("keep-alive")
}
for k, vv := range resp.Header {
for _, v := range vv {
rw.Header().Add(k, v)
}
}
rw.Header().Set("connection", "close")
rw.WriteHeader(resp.StatusCode)
defer resp.Body.Close()
var w io.Writer = rw
if h.Config.Forward.Log {
w = &ForwardLogWriter{
Writer: w,
Logger: h.ForwardLogger,
Context: log.NewContext(nil).
Xid("trace_id", ri.TraceID).
Str("server_name", ri.ServerName).
Str("server_addr", ri.ServerAddr).
Str("tls_version", ri.TLSVersion.String()).
Str("username", ai.Username).
Str("remote_ip", ri.RemoteIP).
Str("remote_country", ri.GeoipInfo.Country).
Str("remote_region", ri.GeoipInfo.Region).
Str("remote_city", ri.GeoipInfo.City).
Str("http_method", req.Method).
Str("http_host", host).
Str("http_domain", domain).
Str("http_proto", req.Proto).
Str("user_agent", req.UserAgent()).
Str("user_agent_os", ri.UserAgent.OS).
Str("user_agent_os_version", ri.UserAgent.OSVersion).
Str("user_agent_name", ri.UserAgent.Name).
Str("user_agent_version", ri.UserAgent.Version).
Value(),
FieldName: "transmit_bytes",
Interval: cmp.Or(h.Config.Forward.LogInterval, 1),
}
}
transmitBytes, err = io.CopyBuffer(w, NewRateLimitReader(resp.Body, ai.SpeedLimit), make([]byte, 1024*1024)) // buffer size should align to http2.MaxReadFrameSize
log.Debug().Context(ri.LogContext).Str("username", ai.Username).Str("http_domain", domain).Int64("transmit_bytes", transmitBytes).Err(err).Msg("forward log")
}
}
type ForwardAuthInfo struct {
Username string `csv:"username"`
Password string `csv:"password"`
SpeedLimit int64 `csv:"speedlimit"`
VIP int `csv:"vip"`
}
func (h *HTTPForwardHandler) GetAuthInfo(ri *RequestInfo, req *http.Request) (ForwardAuthInfo, error) {
authorization := req.Header.Get("proxy-authorization")
parts := strings.SplitN(authorization, " ", 2)
if len(parts) == 1 {
return ForwardAuthInfo{}, fmt.Errorf("invaild auth header: %s", authorization)
}
if parts[0] != "Basic" {
return ForwardAuthInfo{}, fmt.Errorf("unsupported auth header: %s", authorization)
}
data, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return ForwardAuthInfo{}, err
}
parts = strings.SplitN(string(data), ":", 2)
if len(parts) == 1 {
return ForwardAuthInfo{}, fmt.Errorf("invaild auth header: %s", authorization)
}
username, password := parts[0], parts[1]
var ai ForwardAuthInfo
records := h.csvloader.Load()
if records == nil {
return ai, fmt.Errorf("empty records in csvloader %s", h.csvloader.Filename)
}
if i := slices.IndexFunc(*records, func(r ForwardAuthInfo) bool {
if r.Username != username {
return false
}
switch {
case strings.HasPrefix(r.Password, "$2a$"):
return bcrypt.CompareHashAndPassword([]byte(r.Password), []byte(password)) == nil
default:
return r.Password == password
}
}); i >= 0 {
ai = (*records)[i]
}
if ai.Username == "" {
return ai, fmt.Errorf("wrong username='%s' or password='%s'", username, password)
}
return ai, nil
}
func RejectRequest(rw http.ResponseWriter, req *http.Request) {
time.Sleep(time.Duration(1+fastrandn(3)) * time.Second)
// http.Error(rw, "403 Forbidden", http.StatusForbidden)
http.Error(rw, "400 Bad Request", http.StatusBadRequest)
}
type ForwardLogWriter struct {
io.Writer
Logger log.Logger
Context log.Context
FieldName string
Interval int64
timestamp int64
transmits int64
}
func (w *ForwardLogWriter) Write(buf []byte) (n int, err error) {
n, err = w.Writer.Write(buf)
now := time.Now().Unix()
if w.transmits != 0 && (w.timestamp == 0 || now-w.timestamp >= w.Interval || err != nil) {
w.Logger.Log().Context(w.Context).Int64(w.FieldName, w.transmits).Msg("forward log")
w.timestamp = now
w.transmits = 0
} else {
w.transmits += int64(n)
}
return
}