forked from riobard/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.go
166 lines (137 loc) · 3.65 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
package main
import (
"io"
"net"
"github.com/riobard/go-shadowsocks2/core"
"github.com/riobard/go-shadowsocks2/socks"
)
// Create a SOCKS server listening on addr and proxy to server.
func socksLocal(addr, server string, ciph core.StreamConnCipher) {
logf("SOCKS proxy %s <-> %s", addr, server)
tcpLocal(addr, server, ciph, func(c net.Conn) (socks.Addr, error) { return socks.Handshake(c) })
}
// Create a TCP tunnel from addr to target via server.
func tcpTun(addr, server, target string, ciph core.StreamConnCipher) {
tgt := socks.ParseAddr(target)
if tgt == nil {
logf("invalid target address %q", target)
return
}
logf("TCP tunnel %s <-> %s <-> %s", addr, server, target)
tcpLocal(addr, server, ciph, func(net.Conn) (socks.Addr, error) { return tgt, nil })
}
// Listen on addr and proxy to server to reach target from getAddr.
func tcpLocal(addr, server string, ciph core.StreamConnCipher, getAddr func(net.Conn) (socks.Addr, error)) {
ln, err := net.Listen("tcp", addr)
if err != nil {
logf("failed to listen on %s: %v", addr, err)
return
}
for {
conn, err := ln.Accept()
if err != nil {
logf("failed to accept: %s", err)
continue
}
tgt, err := getAddr(conn)
if err != nil {
logf("failed to get target address: %v", err)
continue
}
go tcpLocalHandle(conn, server, tgt, ciph)
}
}
func tcpLocalHandle(c net.Conn, server string, target socks.Addr, ciph core.StreamConnCipher) {
logf("proxy %s <-> %s <-> %s", c.RemoteAddr(), server, target)
defer c.Close()
sc, err := core.Dial("tcp", server, ciph)
if err != nil {
logf("failed to connect to server %v: %v", server, err)
return
}
defer sc.Close()
if _, err = sc.Write(target); err != nil {
logf("failed to send target address: %v", err)
return
}
_, _, err = relay(sc, c)
if err != nil {
logf("relay error: %v", err)
return
}
}
// Listen on addr for incoming connections.
func tcpRemote(addr string, ciph core.StreamConnCipher) {
ln, err := core.Listen("tcp", addr, ciph)
if err != nil {
logf("failed to listen on %s: %v", addr, err)
return
}
logf("listening TCP on %s", addr)
for {
conn, err := ln.Accept()
if err != nil {
logf("failed to accept: %s", err)
continue
}
go tcpRemoteHandle(conn)
}
}
func tcpRemoteHandle(c net.Conn) {
defer c.Close()
addr, err := socks.ReadAddr(c)
if err != nil {
logf("failed to read address: %v", err)
return
}
logf("proxy %s <-> %s", c.RemoteAddr(), addr)
conn, err := net.Dial("tcp", addr.String())
if err != nil {
logf("failed to connect to target: %s", err)
return
}
defer conn.Close()
_, _, err = relay(c, conn)
if err != nil {
logf("relay error: %v", err)
return
}
}
// relay copies between left and right bidirectionally. Returns number of
// bytes copied from right to left, from left to right, and any error occurred.
func relay(left, right io.ReadWriter) (int64, int64, error) {
type res struct {
N int64
Err error
}
ch := make(chan res)
go func() {
n, err := copyHalfClose(right, left)
ch <- res{n, err}
}()
n, err := copyHalfClose(left, right)
rs := <-ch
if err == nil {
err = rs.Err
}
return n, rs.N, err
}
type closeWriter interface {
CloseWrite() error
}
type closeReader interface {
CloseRead() error
}
// copyHalfClose copies to dst from src and optionally closes dst for writing and src for reading.
func copyHalfClose(dst io.Writer, src io.Reader) (int64, error) {
defer func() {
// half-close to wake up other goroutines blocking on dst and src
if c, ok := dst.(closeWriter); ok {
c.CloseWrite()
}
if c, ok := src.(closeReader); ok {
c.CloseRead()
}
}()
return io.Copy(dst, src) // will use io.ReaderFrom or io.WriterTo shortcut if possible
}