forked from riobard/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
146 lines (125 loc) · 3.08 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
package main
import (
"io"
"net"
"sync"
"time"
"github.com/riobard/go-shadowsocks2/socks"
)
func tcpKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
tcp.SetKeepAlive(true)
tcp.SetKeepAlivePeriod(3 * time.Minute)
}
}
// Create a SOCKS server listening on addr and proxy to server.
func socksLocal(addr string, d Dialer) {
logf("SOCKS proxy %s", addr)
tcpLocal(addr, d, func(c net.Conn) (socks.Addr, error) { return socks.Handshake(c) })
}
// Create a TCP tunnel from addr to target via server.
func tcpTun(addr, target string, d Dialer) {
tgt := socks.ParseAddr(target)
if tgt == nil {
logf("invalid target address %q", target)
return
}
logf("TCP tunnel %s <-> %s", addr, target)
tcpLocal(addr, d, func(net.Conn) (socks.Addr, error) { return tgt, nil })
}
// Listen on addr and proxy to server to reach target from getAddr.
func tcpLocal(addr string, d Dialer, getAddr func(net.Conn) (socks.Addr, error)) {
l, err := net.Listen("tcp", addr)
if err != nil {
logf("failed to listen on %s: %v", addr, err)
return
}
for {
c, err := l.Accept()
if err != nil {
logf("failed to accept: %s", err)
continue
}
go func() {
defer c.Close()
tcpKeepAlive(c)
tgt, err := getAddr(c)
if err != nil {
logf("failed to get target address: %v", err)
return
}
rc, err := d.Dial("tcp", tgt.String())
if err != nil {
logf("failed to connect: %v", err)
return
}
defer rc.Close()
tcpKeepAlive(rc)
logf("proxy %s <--[%s]--> %s", c.RemoteAddr(), rc.RemoteAddr(), tgt)
if err = relay(rc, c); err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
return // ignore i/o timeout
}
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()
tcpKeepAlive(c)
c = shadow(c)
tgt, err := socks.ReadAddr(c)
if err != nil {
logf("failed to get target address: %v", err)
return
}
rc, err := net.Dial("tcp", tgt.String())
if err != nil {
logf("failed to connect to target: %v", err)
return
}
defer rc.Close()
tcpKeepAlive(rc)
logf("proxy %s <-> %s", c.RemoteAddr(), tgt)
if err = relay(c, rc); err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
return // ignore i/o timeout
}
logf("relay error: %v", err)
}
}()
}
}
// relay copies between left and right bidirectionally. Returns any error occurred.
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 {
err = err1
}
return err
}