forked from yinghuocho/gotun2socks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
844 lines (750 loc) · 19 KB
/
tcp.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
package gotun2socks
import (
"fmt"
"log"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/yinghuocho/gosocks"
"github.com/yinghuocho/gotun2socks/internal/packet"
)
type tcpPacket struct {
ip *packet.IPv4
tcp *packet.TCP
mtuBuf []byte
wire []byte
}
type tcpState byte
const (
// simplified server-side tcp states
CLOSED tcpState = 0x0
SYN_RCVD tcpState = 0x1
ESTABLISHED tcpState = 0x2
FIN_WAIT_1 tcpState = 0x3
FIN_WAIT_2 tcpState = 0x4
CLOSING tcpState = 0x5
LAST_ACK tcpState = 0x6
TIME_WAIT tcpState = 0x7
MAX_RECV_WINDOW int = 65535
MAX_SEND_WINDOW int = 65535
)
type tcpConnTrack struct {
t2s *Tun2Socks
id string
input chan *tcpPacket
toTunCh chan<- interface{}
fromSocksCh chan []byte
toSocksCh chan *tcpPacket
socksCloseCh chan bool
quitBySelf chan bool
quitByOther chan bool
localSocksAddr string
socksConn *gosocks.SocksConn
// tcp context
state tcpState
// sequence I should use to send next segment
// also as ack I expect in next received segment
nxtSeq uint32
// sequence I want in next received segment
rcvNxtSeq uint32
// what I have acked
lastAck uint32
// flow control
recvWindow int32
sendWindow int32
sendWndCond *sync.Cond
// recvWndCond *sync.Cond
localIP net.IP
remoteIP net.IP
localPort uint16
remotePort uint16
}
var (
tcpPacketPool *sync.Pool = &sync.Pool{
New: func() interface{} {
return &tcpPacket{}
},
}
)
func tcpflagsString(tcp *packet.TCP) string {
s := []string{}
if tcp.SYN {
s = append(s, "SYN")
}
if tcp.RST {
s = append(s, "RST")
}
if tcp.FIN {
s = append(s, "FIN")
}
if tcp.ACK {
s = append(s, "ACK")
}
if tcp.PSH {
s = append(s, "PSH")
}
if tcp.URG {
s = append(s, "URG")
}
if tcp.ECE {
s = append(s, "ECE")
}
if tcp.CWR {
s = append(s, "CWR")
}
return strings.Join(s, ",")
}
func tcpstateString(state tcpState) string {
switch state {
case CLOSED:
return "CLOSED"
case SYN_RCVD:
return "SYN_RCVD"
case ESTABLISHED:
return "ESTABLISHED"
case FIN_WAIT_1:
return "FIN_WAIT_1"
case FIN_WAIT_2:
return "FIN_WAIT_2"
case CLOSING:
return "CLOSING"
case LAST_ACK:
return "LAST_ACK"
case TIME_WAIT:
return "TIME_WAIT"
}
return ""
}
func newTCPPacket() *tcpPacket {
return tcpPacketPool.Get().(*tcpPacket)
}
func releaseTCPPacket(pkt *tcpPacket) {
packet.ReleaseIPv4(pkt.ip)
packet.ReleaseTCP(pkt.tcp)
if pkt.mtuBuf != nil {
releaseBuffer(pkt.mtuBuf)
}
pkt.mtuBuf = nil
pkt.wire = nil
tcpPacketPool.Put(pkt)
}
func copyTCPPacket(raw []byte, ip *packet.IPv4, tcp *packet.TCP) *tcpPacket {
iphdr := packet.NewIPv4()
tcphdr := packet.NewTCP()
pkt := newTCPPacket()
// make a deep copy
var buf []byte
if len(raw) <= MTU {
buf = newBuffer()
pkt.mtuBuf = buf
} else {
buf = make([]byte, len(raw))
}
n := copy(buf, raw)
pkt.wire = buf[:n]
packet.ParseIPv4(pkt.wire, iphdr)
packet.ParseTCP(iphdr.Payload, tcphdr)
pkt.ip = iphdr
pkt.tcp = tcphdr
return pkt
}
func tcpConnID(ip *packet.IPv4, tcp *packet.TCP) string {
return strings.Join([]string{
ip.SrcIP.String(),
fmt.Sprintf("%d", tcp.SrcPort),
ip.DstIP.String(),
fmt.Sprintf("%d", tcp.DstPort),
}, "|")
}
func packTCP(ip *packet.IPv4, tcp *packet.TCP) *tcpPacket {
pkt := newTCPPacket()
pkt.ip = ip
pkt.tcp = tcp
buf := newBuffer()
pkt.mtuBuf = buf
payloadL := len(tcp.Payload)
payloadStart := MTU - payloadL
if payloadL != 0 {
copy(pkt.mtuBuf[payloadStart:], tcp.Payload)
}
tcpHL := tcp.HeaderLength()
tcpStart := payloadStart - tcpHL
pseduoStart := tcpStart - packet.IPv4_PSEUDO_LENGTH
ip.PseudoHeader(pkt.mtuBuf[pseduoStart:tcpStart], packet.IPProtocolTCP, tcpHL+payloadL)
tcp.Serialize(pkt.mtuBuf[tcpStart:payloadStart], pkt.mtuBuf[pseduoStart:])
ipHL := ip.HeaderLength()
ipStart := tcpStart - ipHL
ip.Serialize(pkt.mtuBuf[ipStart:tcpStart], tcpHL+payloadL)
pkt.wire = pkt.mtuBuf[ipStart:]
return pkt
}
func rst(srcIP net.IP, dstIP net.IP, srcPort uint16, dstPort uint16, seq uint32, ack uint32, payloadLen uint32) *tcpPacket {
iphdr := packet.NewIPv4()
tcphdr := packet.NewTCP()
iphdr.Version = 4
iphdr.Id = packet.IPID()
iphdr.DstIP = srcIP
iphdr.SrcIP = dstIP
iphdr.TTL = 64
iphdr.Protocol = packet.IPProtocolTCP
tcphdr.DstPort = srcPort
tcphdr.SrcPort = dstPort
tcphdr.Window = uint16(MAX_RECV_WINDOW)
tcphdr.RST = true
tcphdr.ACK = true
tcphdr.Seq = 0
// RFC 793:
// "If the incoming segment has an ACK field, the reset takes its sequence
// number from the ACK field of the segment, otherwise the reset has
// sequence number zero and the ACK field is set to the sum of the sequence
// number and segment length of the incoming segment. The connection remains
// in the CLOSED state."
tcphdr.Ack = seq + payloadLen
if tcphdr.Ack == seq {
tcphdr.Ack += 1
}
if ack != 0 {
tcphdr.Seq = ack
}
return packTCP(iphdr, tcphdr)
}
func rstByPacket(pkt *tcpPacket) *tcpPacket {
return rst(pkt.ip.SrcIP, pkt.ip.DstIP, pkt.tcp.SrcPort, pkt.tcp.DstPort, pkt.tcp.Seq, pkt.tcp.Ack, uint32(len(pkt.tcp.Payload)))
}
func (tt *tcpConnTrack) changeState(nxt tcpState) {
// log.Printf("### [%s -> %s]", tcpstateString(tt.state), tcpstateString(nxt))
tt.state = nxt
}
func (tt *tcpConnTrack) validAck(pkt *tcpPacket) bool {
ret := (pkt.tcp.Ack == tt.nxtSeq)
if !ret {
// log.Printf("WARNING: invalid ack: recvd: %d, expecting: %d", pkt.tcp.Ack, tt.nxtSeq)
}
return ret
}
func (tt *tcpConnTrack) validSeq(pkt *tcpPacket) bool {
ret := (pkt.tcp.Seq == tt.rcvNxtSeq)
if !ret {
// log.Printf("WARNING: invalid seq: recvd: %d, expecting: %d", pkt.tcp.Seq, tt.rcvNxtSeq)
// if (tt.rcvNxtSeq - pkt.tcp.Seq) == 1 && tt.state == ESTABLISHED {
// log.Printf("(probably a keep-alive message)")
// }
}
return ret
}
func (tt *tcpConnTrack) relayPayload(pkt *tcpPacket) bool {
payloadLen := uint32(len(pkt.tcp.Payload))
select {
case tt.toSocksCh <- pkt:
tt.rcvNxtSeq += payloadLen
// reduce window when recved
wnd := atomic.LoadInt32(&tt.recvWindow)
wnd -= int32(payloadLen)
if wnd < 0 {
wnd = 0
}
atomic.StoreInt32(&tt.recvWindow, wnd)
return true
case <-tt.socksCloseCh:
return false
}
}
func (tt *tcpConnTrack) send(pkt *tcpPacket) {
// log.Printf("<-- [TCP][%s][%s][seq:%d][ack:%d][payload:%d]", tt.id, tcpflagsString(pkt.tcp), pkt.tcp.Seq, pkt.tcp.Ack, len(pkt.tcp.Payload))
if pkt.tcp.ACK {
tt.lastAck = pkt.tcp.Ack
}
tt.toTunCh <- pkt
}
func (tt *tcpConnTrack) synAck(syn *tcpPacket) {
iphdr := packet.NewIPv4()
tcphdr := packet.NewTCP()
iphdr.Version = 4
iphdr.Id = packet.IPID()
iphdr.SrcIP = tt.remoteIP
iphdr.DstIP = tt.localIP
iphdr.TTL = 64
iphdr.Protocol = packet.IPProtocolTCP
tcphdr.SrcPort = tt.remotePort
tcphdr.DstPort = tt.localPort
tcphdr.Window = uint16(atomic.LoadInt32(&tt.recvWindow))
tcphdr.SYN = true
tcphdr.ACK = true
tcphdr.Seq = tt.nxtSeq
tcphdr.Ack = tt.rcvNxtSeq
tcphdr.Options = []packet.TCPOption{{2, 4, []byte{0x5, 0xb4}}}
synAck := packTCP(iphdr, tcphdr)
tt.send(synAck)
// SYN counts 1 seq
tt.nxtSeq += 1
}
func (tt *tcpConnTrack) finAck() {
iphdr := packet.NewIPv4()
tcphdr := packet.NewTCP()
iphdr.Version = 4
iphdr.Id = packet.IPID()
iphdr.SrcIP = tt.remoteIP
iphdr.DstIP = tt.localIP
iphdr.TTL = 64
iphdr.Protocol = packet.IPProtocolTCP
tcphdr.SrcPort = tt.remotePort
tcphdr.DstPort = tt.localPort
tcphdr.Window = uint16(atomic.LoadInt32(&tt.recvWindow))
tcphdr.FIN = true
tcphdr.ACK = true
tcphdr.Seq = tt.nxtSeq
tcphdr.Ack = tt.rcvNxtSeq
finAck := packTCP(iphdr, tcphdr)
tt.send(finAck)
// FIN counts 1 seq
tt.nxtSeq += 1
}
func (tt *tcpConnTrack) ack() {
iphdr := packet.NewIPv4()
tcphdr := packet.NewTCP()
iphdr.Version = 4
iphdr.Id = packet.IPID()
iphdr.SrcIP = tt.remoteIP
iphdr.DstIP = tt.localIP
iphdr.TTL = 64
iphdr.Protocol = packet.IPProtocolTCP
tcphdr.SrcPort = tt.remotePort
tcphdr.DstPort = tt.localPort
tcphdr.Window = uint16(atomic.LoadInt32(&tt.recvWindow))
tcphdr.ACK = true
tcphdr.Seq = tt.nxtSeq
tcphdr.Ack = tt.rcvNxtSeq
ack := packTCP(iphdr, tcphdr)
tt.send(ack)
}
func (tt *tcpConnTrack) payload(data []byte) {
iphdr := packet.NewIPv4()
tcphdr := packet.NewTCP()
iphdr.Version = 4
iphdr.Id = packet.IPID()
iphdr.SrcIP = tt.remoteIP
iphdr.DstIP = tt.localIP
iphdr.TTL = 64
iphdr.Protocol = packet.IPProtocolTCP
tcphdr.SrcPort = tt.remotePort
tcphdr.DstPort = tt.localPort
tcphdr.Window = uint16(atomic.LoadInt32(&tt.recvWindow))
tcphdr.ACK = true
tcphdr.PSH = true
tcphdr.Seq = tt.nxtSeq
tcphdr.Ack = tt.rcvNxtSeq
tcphdr.Payload = data
pkt := packTCP(iphdr, tcphdr)
tt.send(pkt)
// adjust seq
tt.nxtSeq = tt.nxtSeq + uint32(len(data))
}
// stateClosed receives a SYN packet, tries to connect the socks proxy, gives a
// SYN/ACK if success, otherwise RST
func (tt *tcpConnTrack) stateClosed(syn *tcpPacket) (continu bool, release bool) {
var e error
for i := 0; i < 2; i++ {
tt.socksConn, e = dialLocalSocks(tt.localSocksAddr)
if e != nil {
log.Printf("fail to connect SOCKS proxy: %s", e)
} else {
// no timeout
tt.socksConn.SetDeadline(time.Time{})
break
}
}
if tt.socksConn == nil {
resp := rstByPacket(syn)
tt.toTunCh <- resp.wire
// log.Printf("<-- [TCP][%s][RST]", tt.id)
return false, true
}
// context variables
tt.rcvNxtSeq = syn.tcp.Seq + 1
tt.nxtSeq = 1
tt.synAck(syn)
tt.changeState(SYN_RCVD)
return true, true
}
func (tt *tcpConnTrack) tcpSocks2Tun(dstIP net.IP, dstPort uint16, conn net.Conn, readCh chan<- []byte, writeCh <-chan *tcpPacket, closeCh chan bool) {
_, e := gosocks.WriteSocksRequest(conn, &gosocks.SocksRequest{
Cmd: gosocks.SocksCmdConnect,
HostType: gosocks.SocksIPv4Host,
DstHost: dstIP.String(),
DstPort: dstPort,
})
if e != nil {
log.Printf("error to send socks request: %s", e)
conn.Close()
close(closeCh)
return
}
reply, e := gosocks.ReadSocksReply(conn)
if e != nil {
log.Printf("error to read socks reply: %s", e)
conn.Close()
close(closeCh)
return
}
if reply.Rep != gosocks.SocksSucceeded {
log.Printf("socks connect request fail, retcode: %d", reply.Rep)
conn.Close()
close(closeCh)
return
}
// writer
go func() {
loop:
for {
select {
case <-closeCh:
break loop
case pkt := <-writeCh:
conn.Write(pkt.tcp.Payload)
// increase window when processed
wnd := atomic.LoadInt32(&tt.recvWindow)
wnd += int32(len(pkt.tcp.Payload))
if wnd > int32(MAX_RECV_WINDOW) {
wnd = int32(MAX_RECV_WINDOW)
}
atomic.StoreInt32(&tt.recvWindow, wnd)
releaseTCPPacket(pkt)
}
}
}()
// reader
for {
var buf [MTU - 40]byte
// tt.sendWndCond.L.Lock()
var wnd int32
var cur int32
wnd = atomic.LoadInt32(&tt.sendWindow)
if wnd <= 0 {
for wnd <= 0 {
tt.sendWndCond.L.Lock()
tt.sendWndCond.Wait()
wnd = atomic.LoadInt32(&tt.sendWindow)
}
tt.sendWndCond.L.Unlock()
}
cur = wnd
if cur > MTU-40 {
cur = MTU - 40
}
// tt.sendWndCond.L.Unlock()
n, e := conn.Read(buf[:cur])
if e != nil {
log.Printf("error to read from socks: %s", e)
conn.Close()
break
} else {
b := make([]byte, n)
copy(b, buf[:n])
readCh <- b
// tt.sendWndCond.L.Lock()
nxt := wnd - int32(n)
if nxt < 0 {
nxt = 0
}
// if sendWindow does not equal to wnd, it is already updated by a
// received pkt from TUN
atomic.CompareAndSwapInt32(&tt.sendWindow, wnd, nxt)
// tt.sendWndCond.L.Unlock()
}
}
close(closeCh)
}
// stateSynRcvd expects a ACK with matching ack number,
func (tt *tcpConnTrack) stateSynRcvd(pkt *tcpPacket) (continu bool, release bool) {
// rst to packet with invalid sequence/ack, state unchanged
if !(tt.validSeq(pkt) && tt.validAck(pkt)) {
if !pkt.tcp.RST {
resp := rstByPacket(pkt)
tt.toTunCh <- resp
// log.Printf("<-- [TCP][%s][RST] continue", tt.id)
}
return true, true
}
// connection ends by valid RST
if pkt.tcp.RST {
return false, true
}
// ignore non-ACK packets
if !pkt.tcp.ACK {
return true, true
}
continu = true
release = true
tt.changeState(ESTABLISHED)
go tt.tcpSocks2Tun(tt.remoteIP, uint16(tt.remotePort), tt.socksConn, tt.fromSocksCh, tt.toSocksCh, tt.socksCloseCh)
if len(pkt.tcp.Payload) != 0 {
if tt.relayPayload(pkt) {
// pkt hands to socks writer
release = false
}
}
return
}
func (tt *tcpConnTrack) stateEstablished(pkt *tcpPacket) (continu bool, release bool) {
// ack if sequence is not expected
if !tt.validSeq(pkt) {
tt.ack()
return true, true
}
// connection ends by valid RST
if pkt.tcp.RST {
return false, true
}
// ignore non-ACK packets
if !pkt.tcp.ACK {
return true, true
}
continu = true
release = true
if len(pkt.tcp.Payload) != 0 {
if tt.relayPayload(pkt) {
// pkt hands to socks writer
release = false
}
}
if pkt.tcp.FIN {
tt.rcvNxtSeq += 1
tt.finAck()
tt.changeState(LAST_ACK)
tt.socksConn.Close()
}
return
}
func (tt *tcpConnTrack) stateFinWait1(pkt *tcpPacket) (continu bool, release bool) {
// ignore packet with invalid sequence, state unchanged
if !tt.validSeq(pkt) {
return true, true
}
// connection ends by valid RST
if pkt.tcp.RST {
return false, true
}
// ignore non-ACK packets
if !pkt.tcp.ACK {
return true, true
}
if pkt.tcp.FIN {
tt.rcvNxtSeq += 1
tt.ack()
if pkt.tcp.ACK && tt.validAck(pkt) {
tt.changeState(TIME_WAIT)
return false, true
} else {
tt.changeState(CLOSING)
return true, true
}
} else {
tt.changeState(FIN_WAIT_2)
return true, true
}
}
func (tt *tcpConnTrack) stateFinWait2(pkt *tcpPacket) (continu bool, release bool) {
// ignore packet with invalid sequence/ack, state unchanged
if !(tt.validSeq(pkt) && tt.validAck(pkt)) {
return true, true
}
// connection ends by valid RST
if pkt.tcp.RST {
return false, true
}
// ignore non-FIN non-ACK packets
if !pkt.tcp.ACK || !pkt.tcp.FIN {
return true, true
}
tt.rcvNxtSeq += 1
tt.ack()
tt.changeState(TIME_WAIT)
return false, true
}
func (tt *tcpConnTrack) stateClosing(pkt *tcpPacket) (continu bool, release bool) {
// ignore packet with invalid sequence/ack, state unchanged
if !(tt.validSeq(pkt) && tt.validAck(pkt)) {
return true, true
}
// connection ends by valid RST
if pkt.tcp.RST {
return false, true
}
// ignore non-ACK packets
if !pkt.tcp.ACK {
return true, true
}
tt.changeState(TIME_WAIT)
return false, true
}
func (tt *tcpConnTrack) stateLastAck(pkt *tcpPacket) (continu bool, release bool) {
// ignore packet with invalid sequence/ack, state unchanged
if !(tt.validSeq(pkt) && tt.validAck(pkt)) {
return true, true
}
// ignore non-ACK packets
if !pkt.tcp.ACK {
return true, true
}
// connection ends
tt.changeState(CLOSED)
return false, true
}
func (tt *tcpConnTrack) newPacket(pkt *tcpPacket) {
select {
case <-tt.quitByOther:
case <-tt.quitBySelf:
case tt.input <- pkt:
}
}
func (tt *tcpConnTrack) updateSendWindow(pkt *tcpPacket) {
// tt.sendWndCond.L.Lock()
atomic.StoreInt32(&tt.sendWindow, int32(pkt.tcp.Window))
tt.sendWndCond.Signal()
// tt.sendWndCond.L.Unlock()
}
func (tt *tcpConnTrack) run() {
for {
var ackTimer *time.Timer
var timeout *time.Timer = time.NewTimer(5 * time.Minute)
var ackTimeout <-chan time.Time
var socksCloseCh chan bool
var fromSocksCh chan []byte
// enable some channels only when the state is ESTABLISHED
if tt.state == ESTABLISHED {
socksCloseCh = tt.socksCloseCh
fromSocksCh = tt.fromSocksCh
ackTimer = time.NewTimer(10 * time.Millisecond)
ackTimeout = ackTimer.C
}
select {
case pkt := <-tt.input:
// log.Printf("--> [TCP][%s][%s][%s][seq:%d][ack:%d][payload:%d]", tt.id, tcpstateString(tt.state), tcpflagsString(pkt.tcp), pkt.tcp.Seq, pkt.tcp.Ack, len(pkt.tcp.Payload))
var continu, release bool
tt.updateSendWindow(pkt)
switch tt.state {
case CLOSED:
continu, release = tt.stateClosed(pkt)
case SYN_RCVD:
continu, release = tt.stateSynRcvd(pkt)
case ESTABLISHED:
continu, release = tt.stateEstablished(pkt)
case FIN_WAIT_1:
continu, release = tt.stateFinWait1(pkt)
case FIN_WAIT_2:
continu, release = tt.stateFinWait2(pkt)
case CLOSING:
continu, release = tt.stateClosing(pkt)
case LAST_ACK:
continu, release = tt.stateLastAck(pkt)
}
if release {
releaseTCPPacket(pkt)
}
if !continu {
if tt.socksConn != nil {
tt.socksConn.Close()
}
close(tt.quitBySelf)
tt.t2s.clearTCPConnTrack(tt.id)
return
}
case <-ackTimeout:
if tt.lastAck < tt.rcvNxtSeq {
// have something to ack
tt.ack()
}
case data := <-fromSocksCh:
tt.payload(data)
case <-socksCloseCh:
tt.finAck()
tt.changeState(FIN_WAIT_1)
case <-timeout.C:
if tt.socksConn != nil {
tt.socksConn.Close()
}
close(tt.quitBySelf)
tt.t2s.clearTCPConnTrack(tt.id)
return
case <-tt.quitByOther:
// who closes this channel should be responsible to clear track map
log.Printf("tcpConnTrack quitByOther")
if tt.socksConn != nil {
tt.socksConn.Close()
}
return
}
timeout.Stop()
if ackTimer != nil {
ackTimer.Stop()
}
}
}
func (t2s *Tun2Socks) createTCPConnTrack(id string, ip *packet.IPv4, tcp *packet.TCP) *tcpConnTrack {
t2s.tcpConnTrackLock.Lock()
defer t2s.tcpConnTrackLock.Unlock()
track := &tcpConnTrack{
t2s: t2s,
id: id,
toTunCh: t2s.writeCh,
input: make(chan *tcpPacket, 10000),
fromSocksCh: make(chan []byte, 100),
toSocksCh: make(chan *tcpPacket, 100),
socksCloseCh: make(chan bool),
quitBySelf: make(chan bool),
quitByOther: make(chan bool),
sendWindow: int32(MAX_SEND_WINDOW),
recvWindow: int32(MAX_RECV_WINDOW),
sendWndCond: &sync.Cond{L: &sync.Mutex{}},
localPort: tcp.SrcPort,
remotePort: tcp.DstPort,
localSocksAddr: t2s.localSocksAddr,
state: CLOSED,
}
track.localIP = make(net.IP, len(ip.SrcIP))
copy(track.localIP, ip.SrcIP)
track.remoteIP = make(net.IP, len(ip.DstIP))
copy(track.remoteIP, ip.DstIP)
t2s.tcpConnTrackMap[id] = track
go track.run()
log.Printf("tracking %d TCP connections", len(t2s.tcpConnTrackMap))
return track
}
func (t2s *Tun2Socks) getTCPConnTrack(id string) *tcpConnTrack {
t2s.tcpConnTrackLock.Lock()
defer t2s.tcpConnTrackLock.Unlock()
return t2s.tcpConnTrackMap[id]
}
func (t2s *Tun2Socks) clearTCPConnTrack(id string) {
t2s.tcpConnTrackLock.Lock()
defer t2s.tcpConnTrackLock.Unlock()
delete(t2s.tcpConnTrackMap, id)
log.Printf("tracking %d TCP connections", len(t2s.tcpConnTrackMap))
}
func (t2s *Tun2Socks) tcp(raw []byte, ip *packet.IPv4, tcp *packet.TCP) {
connID := tcpConnID(ip, tcp)
track := t2s.getTCPConnTrack(connID)
if track != nil {
pkt := copyTCPPacket(raw, ip, tcp)
track.newPacket(pkt)
} else {
// ignore RST, if there is no track of this connection
if tcp.RST {
// log.Printf("--> [TCP][%s][%s]", connID, tcpflagsString(tcp))
return
}
// return a RST to non-SYN packet
if !tcp.SYN {
// log.Printf("--> [TCP][%s][%s]", connID, tcpflagsString(tcp))
resp := rst(ip.SrcIP, ip.DstIP, tcp.SrcPort, tcp.DstPort, tcp.Seq, tcp.Ack, uint32(len(tcp.Payload)))
t2s.writeCh <- resp
// log.Printf("<-- [TCP][%s][RST]", connID)
return
}
pkt := copyTCPPacket(raw, ip, tcp)
track := t2s.createTCPConnTrack(connID, ip, tcp)
track.newPacket(pkt)
}
}