forked from riobard/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
103 lines (93 loc) · 2.07 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
package main
import (
"errors"
"io"
"net"
"os"
"sync"
"time"
"github.com/riobard/go-shadowsocks2/socks"
)
func tcpLocal(l net.Listener, d Dialer) {
for {
c, err := l.Accept()
if err != nil {
logf("failed to accept: %v", err)
continue
}
go func() {
defer c.Close()
laddr := c.LocalAddr()
if laddr == nil {
logf("failed to determine target address")
return
}
rc, err := d.Dial(laddr.Network(), laddr.String())
if err != nil {
logf("failed to connect: %v", err)
return
}
defer rc.Close()
logf("proxy %s <--[%s]--> %s", c.RemoteAddr(), rc.RemoteAddr(), laddr)
if err = relay(rc, c); err != nil {
logf("relay error: %v", err)
}
}()
}
}
// Listen on addr for incoming connections.
func tcpRemote(addr string, shadow func(net.Conn) net.Conn) {
l, err := net.Listen("tcp", addr)
if err != nil {
logf("failed to listen on %s: %v", addr, err)
return
}
logf("listening TCP on %s", addr)
for {
c, err := l.Accept()
if err != nil {
logf("failed to accept: %v", err)
continue
}
go func() {
defer c.Close()
c = shadow(c)
tgt, err := socks.ReadAddr(c)
if err != nil {
logf("failed to get target address from %v: %v", c.RemoteAddr(), err)
return
}
rc, err := net.Dial("tcp", tgt.String())
if err != nil {
logf("failed to connect to target: %v", err)
return
}
defer rc.Close()
logf("proxy %s <-> %s", c.RemoteAddr(), tgt)
if err = relay(c, rc); err != nil {
logf("relay error: %v", err)
}
}()
}
}
// relay copies between left and right bidirectionally
func relay(left, right net.Conn) error {
var err, err1 error
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_, err1 = io.Copy(right, left)
right.SetReadDeadline(time.Now()) // unblock read on right
}()
_, err = io.Copy(left, right)
left.SetReadDeadline(time.Now()) // unblock read on left
wg.Wait()
if err1 != nil && !errors.Is(err1, os.ErrDeadlineExceeded) { // requires Go 1.15+
return err1
}
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
return err
}
return nil
}