-
Notifications
You must be signed in to change notification settings - Fork 190
/
local_server.go
516 lines (477 loc) · 13.9 KB
/
local_server.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
package local
import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/yinqiwen/gsnova/common/channel"
"github.com/yinqiwen/gsnova/common/dns"
"github.com/yinqiwen/gsnova/common/dump"
"github.com/yinqiwen/gsnova/common/helper"
"github.com/yinqiwen/gsnova/common/hosts"
"github.com/yinqiwen/gsnova/common/logger"
"github.com/yinqiwen/gsnova/common/mux"
"github.com/yinqiwen/gsnova/common/socks"
"github.com/yinqiwen/pmux"
)
var proxyServerRunning = true
var runningProxyStreamCount int64
var activeStreams sync.Map
var upBytesPool, downBytesPool *sync.Pool
func init() {
upBytesPool = &sync.Pool{
New: func() interface{} {
blen := GConf.Mux.UpBufferSize
if 0 == blen {
blen = 16 * 1024
}
return make([]byte, blen)
},
}
downBytesPool = &sync.Pool{
New: func() interface{} {
blen := GConf.Mux.DownBufferSize
if 0 == blen {
blen = 64 * 1024
}
return make([]byte, blen)
},
}
}
func isTimeoutErr(err error) bool {
if err == pmux.ErrTimeout {
return true
}
if err, ok := err.(net.Error); ok && err.Timeout() {
return true
}
return false
}
type proxyStreamContext struct {
stream mux.MuxStream
c io.ReadWriteCloser
}
var ssidSeed = uint32(0)
func serveProxyConn(conn net.Conn, remoteHost, remotePort string, proxy *ProxyConfig) {
var proxyChannelName string
protocol := "tcp"
localConn := conn
atomic.AddInt64(&runningProxyStreamCount, 1)
defer localConn.Close()
defer atomic.AddInt64(&runningProxyStreamCount, -1)
if len(proxy.Forward) > 0 {
logger.Notice("Server:%s forward connection to %s, ", proxy.Local, proxy.Forward)
remoteHost, remotePort, _ = net.SplitHostPort(proxy.Forward)
}
isSocksProxy := false
isHttpsProxy := false
isHttp11Proto := false
mitmEnabled := false
isTransparentProxy := len(remoteHost) > 0
var initialHTTPReq *http.Request
var bufconn *helper.BufConn
buildMITMConn := func() error {
tlsCfg, err := helper.TLSConfig(remoteHost)
if nil != err {
logger.Error("Failed to get MITM TLS config for %s with reason:%v", remoteHost, err)
return err
}
tlsConn := tls.Server(bufconn, tlsCfg)
localConn = tlsConn
bufconn = helper.NewBufConn(localConn, nil)
mitmEnabled = true
return nil
}
//logger.Info("###Enter with %v %v", isTransparentProxy, remoteHost)
if !isTransparentProxy {
socksConn, sbufconn, err := socks.NewSocksConn(conn)
if nil == err {
isSocksProxy = true
logger.Debug("Local proxy recv %s proxy conn to %s", socksConn.Version(), socksConn.Req.Target)
socksConn.Grant(&net.TCPAddr{
IP: net.ParseIP("0.0.0.0"), Port: 0})
localConn = socksConn
if socksConn.Req.Target == GConf.UDPGW.Addr {
logger.Debug("Handle udpgw conn for %v", socksConn.Req.Target)
handleUDPGatewayConn(localConn, proxy)
return
}
remoteHost, remotePort, err = net.SplitHostPort(socksConn.Req.Target)
if nil != err {
logger.Error("Invalid socks target addresss:%s with reason %v", socksConn.Req.Target, err)
return
}
} else { //not socks proxy
if nil == sbufconn {
localConn.Close()
return
}
if sbufconn.Buffered() > 0 {
bufconn = helper.NewBufConn(conn, sbufconn)
}
}
}
if nil == bufconn {
//bufconn = bufio.NewReader(localConn)
bufconn = helper.NewBufConn(conn, nil)
}
trySniffDomain := false
if len(remoteHost) == 0 || (net.ParseIP(remoteHost) != nil && !helper.IsPrivateIP(remoteHost)) {
//this is a ip from local dns query
trySniffDomain = true
}
//1. sniff SNI first
var sniffedSNI string
if (isSocksProxy || isTransparentProxy) && trySniffDomain {
if remotePort == "80" {
conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
} else {
conn.SetReadDeadline(time.Now().Add(200 * time.Millisecond))
}
if nil != dns.CNIPSet {
remoteIP := net.ParseIP(remoteHost)
logger.Debug("Recv proxy request to IP:%v CNIP:%v", remoteIP, dns.CNIPSet.IsInCountry(remoteIP, "CN"))
}
sni, err := helper.PeekTLSServerName(bufconn)
if nil != err {
//logger.Debug("##Failed to sniff SNI with error:%v", err)
} else {
if redirect, ok := GConf.SNI.redirect(sni); ok {
sni = redirect
}
logger.Debug("Sniffed SNI:%s:%s for IP:%s:%s", sni, remotePort, remoteHost, remotePort)
remoteHost = sni
trySniffDomain = false
sniffedSNI = sni
}
}
if proxy.MITM {
if len(sniffedSNI) > 0 || (isSocksProxy && !trySniffDomain && remotePort == "443") {
err := buildMITMConn()
if nil != err {
return
}
}
}
//2. sniff domain via http
if trySniffDomain {
localConn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
//ss := time.Now()
headChunk, err := bufconn.Peek(3)
if len(headChunk) != 3 {
if err != io.EOF {
logger.Error("Peek:%s %d %v to %s:%s", string(headChunk), len(headChunk), err, remoteHost, remotePort)
} else {
return
}
//logger.Error("Peek %d %v %v %v", len(headChunk), err, time.Now().Sub(ss), string(headChunk))
goto START
}
method := string(headChunk)
// if tmp := strings.Fields(method); len(tmp) > 0 {
// method = tmp[0]
// }
// method = strings.ToUpper(method)
switch method {
case "GET":
fallthrough
case "POS":
fallthrough
case "HEA":
fallthrough
case "PUT":
fallthrough
case "DEL":
fallthrough
case "CON":
fallthrough
case "OPT":
fallthrough
case "TRAC":
fallthrough
case "PAT":
isHttp11Proto = true
default:
logger.Error("Method:%s", method)
isHttp11Proto = false
}
//log.Printf("[%d]]Method:%s", sid, method)
if isHttp11Proto {
initialHTTPReq, err = http.ReadRequest(bufconn.BR)
if nil != err {
logger.Error("Read first request failed from proxy connection for reason:%v", err)
return
}
//log.Printf("Host:%s %v", initialHTTPReq.Host, initialHTTPReq.URL)
if strings.Contains(initialHTTPReq.Host, ":") {
remoteHost, remotePort, _ = net.SplitHostPort(initialHTTPReq.Host)
} else {
remoteHost = initialHTTPReq.Host
remotePort = "80"
if strings.EqualFold(initialHTTPReq.Method, "CONNECT") {
remotePort = "443"
}
}
if strings.EqualFold(initialHTTPReq.Method, "CONNECT") {
protocol = "https"
if !isSocksProxy {
localConn.Write([]byte("HTTP/1.0 200 Connection established\r\n\r\n"))
isHttpsProxy = true
initialHTTPReq = nil
if proxy.MITM {
err := buildMITMConn()
if nil != err {
return
}
}
}
} else {
protocol = "http"
}
} else {
if !isHttp11Proto && !isSocksProxy && !isTransparentProxy {
logger.Error("[ERROR]Can NOT handle non HTTP1.1 proto in non socks proxy mode %v:%v:%v %v to %v", isHttp11Proto, isSocksProxy, isTransparentProxy, conn.LocalAddr(), conn.RemoteAddr())
return
}
}
}
START:
if len(remoteHost) == 0 || len(remotePort) == 0 {
logger.Error("Can NOT resolve remote host or port %s:%s %v", remoteHost, remotePort, initialHTTPReq)
return
}
proxyChannelName = proxy.getProxyChannelByHost(protocol, remoteHost)
if len(proxyChannelName) == 0 {
logger.Error("[ERROR]No proxy found for %s:%s", protocol, remoteHost)
return
}
stream, conf, err := channel.GetMuxStreamByChannel(proxyChannelName)
if nil != err || nil == stream {
logger.Error("Failed to open stream for reason:%v by proxy:%s", err, proxyChannelName)
return
}
defer stream.Close()
var maxIdleTime time.Duration
if GConf.Mux.StreamIdleTimeout < 0 {
maxIdleTime = 24 * 3600 * time.Second
} else {
maxIdleTime = time.Duration(GConf.Mux.StreamIdleTimeout) * time.Second
if maxIdleTime == 0 {
maxIdleTime = 10 * time.Second
}
}
ssid := stream.StreamID()
if 0 == ssid {
ssid = atomic.AddUint32(&ssidSeed, uint32(1))
}
opt := mux.StreamOptions{
DialTimeout: conf.RemoteDialMSTimeout,
Hops: conf.Hops,
ReadTimeout: int(maxIdleTime.Seconds()) * 1000,
}
if remotePort == "443" && nil == net.ParseIP(remoteHost) {
remoteSNI := conf.GetRemoteSNI(remoteHost)
if len(remoteSNI) > 0 {
sniHost := hosts.GetHost(remoteSNI)
logger.Notice("Proxy stream[%d] select remote SNI host %s for proxy to %s:%s", ssid, sniHost, remoteHost, remotePort)
remoteHost = sniHost
}
}
logger.Notice("Proxy stream[%d] select %s for proxy to %s:%s", ssid, proxyChannelName, remoteHost, remotePort)
err = stream.Connect("tcp", net.JoinHostPort(remoteHost, remotePort), opt)
if nil != err {
logger.Error("Connect failed from proxy connection for reason:%v", err)
return
}
//clear read timeout
var zero time.Time
localConn.SetReadDeadline(zero)
var streamReader io.Reader
var streamWriter io.Writer
if mitmEnabled {
streamConn := &mux.MuxStreamConn{
MuxStream: stream,
}
tlcClientCfg := &tls.Config{
InsecureSkipVerify: true,
}
tlsClient := tls.Client(streamConn, tlcClientCfg)
streamReader, streamWriter = mux.GetCompressStreamReaderWriter(tlsClient, conf.Compressor)
} else {
streamReader, streamWriter = mux.GetCompressStreamReaderWriter(stream, conf.Compressor)
}
if proxy.HTTPDump.MatchDomain(remoteHost) {
_, isTLSConn := localConn.(*tls.Conn)
options := &dump.HttpDumpOptions{
IsTLS: isTLSConn,
Destination: proxy.HTTPDump.Dump,
ExcludeBody: proxy.HTTPDump.ExcludeBody,
IncludeBody: proxy.HTTPDump.IncludeBody,
}
dumpReadWriter := dump.NewHttpDumpReadWriter(streamReader, streamWriter, options)
streamReader = dumpReadWriter
streamWriter = dumpReadWriter
defer dumpReadWriter.Close()
}
streamCtx := &proxyStreamContext{}
streamCtx.stream = stream
streamCtx.c = localConn
activeStreams.Store(streamCtx, true)
start := time.Now()
closeCh := make(chan int, 1)
go func() {
//buf := make([]byte, 128*1024)
buf := downBytesPool.Get().([]byte)
_, cerr := io.CopyBuffer(localConn, streamReader, buf)
logger.Notice("Proxy stream[%d] cost %v to copy from %s:%v %v", ssid, time.Now().Sub(start), remoteHost, remotePort, cerr)
localConn.Close()
bufconn.Close()
downBytesPool.Put(buf)
closeCh <- 1
}()
//start task to check stream timeout(if the stream has no read&write action more than 10s)
if (isSocksProxy || isHttpsProxy || isTransparentProxy) && nil == initialHTTPReq {
//buf := make([]byte, 128*1024)
buf := upBytesPool.Get().([]byte)
for {
localConn.SetReadDeadline(time.Now().Add(maxIdleTime))
_, cerr := io.CopyBuffer(streamWriter, bufconn, buf)
//logger.Notice("Proxy stream[%d] cost %v to copy from local to %s:%v %v", ssid, time.Now().Sub(start), remoteHost, remotePort, cerr)
if isTimeoutErr(cerr) && time.Now().Sub(stream.LatestIOTime()) < maxIdleTime {
continue
}
//logger.Error("###%s %v after %v", remoteHost, cerr, time.Now().Sub(stream.LatestIOTime()))
break
}
upBytesPool.Put(buf)
if close, ok := streamWriter.(io.Closer); ok {
close.Close()
} else {
stream.Close()
}
//localConn.Close()
logger.Notice("Proxy stream[%d] cost %v to copy from local to %s:%v", ssid, time.Now().Sub(start), remoteHost, remotePort)
} else {
proxyReq := initialHTTPReq
initialHTTPReq = nil
for {
if nil != proxyReq {
proxyReq.Header.Del("Proxy-Connection")
proxyReq.Header.Del("Proxy-Authorization")
err = proxyReq.Write(streamWriter)
if nil != err {
logger.Error("Failed to write http request for reason:%v", err)
return
}
}
prevReq := proxyReq
for {
localConn.SetReadDeadline(time.Now().Add(maxIdleTime))
proxyReq, err = http.ReadRequest(bufconn.BR)
if isTimeoutErr(err) && time.Now().Sub(stream.LatestIOTime()) < maxIdleTime {
continue
}
break
}
if nil != err {
if err, ok := err.(net.Error); ok && err.Timeout() {
}
if err != io.EOF && !strings.Contains(err.Error(), "use of closed network connection") {
logger.Notice("Failed to read proxy http request to %s:%s for reason:%v", remoteHost, remotePort, err)
}
return
}
if nil != prevReq && prevReq.Host != proxyReq.Host {
logger.Debug("Switch to next stream since target host change from %s to %s", prevReq.Host, proxyReq.Host)
stream.Close()
goto START
}
}
}
<-closeCh
activeStreams.Delete(streamCtx)
}
func startLocalProxyServer(proxyIdx int) (*net.TCPListener, error) {
proxyConf := &GConf.Proxy[proxyIdx]
if supportTransparentProxy() {
go startTransparentUDProxy(proxyConf.Local, proxyConf)
}
tcpaddr, err := net.ResolveTCPAddr("tcp", proxyConf.Local)
if nil != err {
logger.Fatal("[ERROR]Local server address:%s error:%v", proxyConf.Local, err)
return nil, err
}
var lp *net.TCPListener
lp, err = net.ListenTCP("tcp", tcpaddr)
if nil != err {
logger.Fatal("Can NOT listen on address:%s", proxyConf.Local)
return nil, err
}
logger.Info("Listen on address %s", proxyConf.Local)
runningServers[proxyIdx] = lp
go func() {
for proxyServerRunning {
var conn net.Conn
conn, err = lp.AcceptTCP()
if nil != err {
continue
}
isTransparent := false
if proxyConf.Transparent && supportTransparentProxy() {
tcpAddr, ok := conn.RemoteAddr().(*net.TCPAddr)
if ok {
_, exist := helper.GetLocalIPSet()[tcpAddr.IP.String()]
if !exist {
isTransparent = true
}
}
}
var originalHost, originalPort string
if isTransparent {
newConn, remoteIP, remotePort, err := getOrinalTCPRemoteAddr(conn)
if nil != err {
logger.Error("Failed to get original address for transparent proxy:%v", err)
continue
}
conn = newConn
originalHost = remoteIP.String()
originalPort = fmt.Sprintf("%d", remotePort)
}
go serveProxyConn(conn, originalHost, originalPort, &GConf.Proxy[proxyIdx])
}
lp.Close()
}()
return lp, nil
}
var runningServers []*net.TCPListener
func startLocalServers() error {
proxyServerRunning = true
runningServers = make([]*net.TCPListener, len(GConf.Proxy))
for i := range GConf.Proxy {
startLocalProxyServer(i)
}
return nil
}
func stopLocalServers() {
proxyServerRunning = false
for _, l := range runningServers {
if nil != l {
l.Close()
}
}
//closeAllProxySession()
closeAllUDPSession()
activeStreams.Range(func(key, value interface{}) bool {
ctx := key.(*proxyStreamContext)
if nil != ctx.c {
ctx.c.Close()
}
activeStreams.Delete(key)
return true
})
}