-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclient.go
292 lines (256 loc) · 7.38 KB
/
client.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
package main
import (
"fmt"
"os"
"net"
"time"
kcp "github.com/xtaci/kcp-go"
"github.com/xtaci/smux"
)
// wrapper for StartClientTCP(), StartClientKCP(), and StartClientUDP()
func StartClient(conn net.Conn, conf Config) {
switch conf.getMode() {
case "tcp":
StartClientTCP(conn, conf.(*TCPConfig))
case "udp":
if conf.(*UDPConfig).Proto == "kcp" {
StartClientKCP(conn.(net.PacketConn), conf.(*UDPConfig))
} else {
StartClientUDP(conn.(net.PacketConn), conf.(*UDPConfig))
}
}
}
func StartClientTCP(conn net.Conn, conf *TCPConfig) {
// encrypt socket
if conf.Key != "" {
switch conn.(type) {
case *net.TCPConn:
conn = NewEConn(conn, conf.Enc, conf.Key)
}
}
// Setup client side of smux
var interval int = g_timeout/3
interval = bound(interval, 1, 10)
smuxConfig := smux.DefaultConfig()
smuxConfig.Version = 1
smuxConfig.MaxReceiveBuffer = 4194304
smuxConfig.MaxStreamBuffer = 2097152
smuxConfig.KeepAliveInterval = time.Duration(interval) * time.Second
smuxConfig.KeepAliveTimeout = time.Duration(g_timeout) * time.Second
if err := smux.VerifyConfig(smuxConfig); err != nil {
perror("smux.VerifyConfig() failed.", err)
os.Exit(1)
}
sess, err := smux.Client(conn, smuxConfig)
if err != nil {
perror("smux.Client() failed.", err)
os.Exit(1)
}
fmt.Printf("tunnel created: [local]%v <--> [remote]%v\n", sess.LocalAddr(), sess.RemoteAddr())
// listen for connections from forward address
lis, err := net.ListenTCP("tcp", conf.FwdAddr.(*net.TCPAddr))
if err != nil {
perror("net.Listen() failed.", err)
os.Exit(1)
}
defer lis.Close()
fmt.Printf("Waiting for new connections from %s ...\n", conf.FwdAddr.String())
// periodic check if smux session is still alive
go func() {
for {
time.Sleep(2*time.Second)
if sess.IsClosed() {
lis.Close()
fmt.Printf("tunnel is closed\n")
break
}
}
}()
for {
fwd_conn, err := lis.Accept()
if err != nil {
perror("lis.Accept() failed.", err)
break
}
stream, err := sess.OpenStream()
if err != nil {
perror("sess.OpenStream() failed.", err)
fwd_conn.Close()
break
}
PrintDbgf("stream open(%d): %v --> tunnel\n", stream.ID(), fwd_conn.RemoteAddr())
go conn2stream(fwd_conn, stream)
}
// clean up
fmt.Printf("...\n")
fmt.Printf("tunnel collapsed: [local]%v <--> [remote]%v\n", sess.LocalAddr(), sess.RemoteAddr())
sess.Close()
conn.Close()
time.Sleep(time.Second)
}
func StartClientKCP(conn net.PacketConn, conf *UDPConfig) {
// encrypt socket
if conf.Key != "" {
switch conn.(type) {
case *net.UDPConn:
conn = NewEPacketConn(conn, conf.Enc, conf.Key)
}
}
// setup kcp
kconf := getKCPConfig(conf.KConf)
PrintDbgf("%T: %v\n", kconf, kconf)
block := getKCPBlockCipher(kconf)
kconn, err := kcp.NewConn2(conf.RAddr, block, kconf.DataShard, kconf.ParityShard, conn)
if err != nil {
perror("kcp.NewConn2() failed.", err)
os.Exit(1)
}
kconn.SetStreamMode(true)
kconn.SetWriteDelay(false)
kconn.SetNoDelay(kconf.NoDelay, kconf.Interval, kconf.Resend, kconf.NoCongestion)
kconn.SetMtu(kconf.MTU)
kconn.SetWindowSize(kconf.SndWnd, kconf.RcvWnd)
kconn.SetACKNoDelay(kconf.AckNodelay)
if err := SetDSCP(conn.(net.Conn), kconf.DSCP); err != nil {
perror("SetDSCP() failed.", err)
}
if err := kconn.SetReadBuffer(kconf.SockBuf); err != nil {
perror("kconn.SetReadBuffer() failed.", err)
}
if err := kconn.SetWriteBuffer(kconf.SockBuf); err != nil {
perror("kconn.SetWriteBuffer() failed.", err)
}
kconn.Write([]byte{1,3,0,0,0,0,0,0}) // smux cmdNOP, let remote know we are connected
// Setup client side of smux
var interval int = g_timeout/3
interval = bound(interval, 1, 10)
smuxConfig := smux.DefaultConfig()
smuxConfig.Version = 1
smuxConfig.MaxReceiveBuffer = 4194304
smuxConfig.MaxStreamBuffer = 2097152
smuxConfig.KeepAliveInterval = time.Duration(interval) * time.Second
smuxConfig.KeepAliveTimeout = time.Duration(g_timeout) * time.Second
if err := smux.VerifyConfig(smuxConfig); err != nil {
perror("smux.VerifyConfig() failed.", err)
os.Exit(1)
}
sess, err := smux.Client(kconn, smuxConfig)
if err != nil {
perror("smux.Client() failed.", err)
os.Exit(1)
}
fmt.Printf("tunnel created: [local]%v <--> [remote]%v\n", sess.LocalAddr(), sess.RemoteAddr())
// listen from forward
lis, err := net.ListenTCP("tcp", conf.FwdAddr.(*net.TCPAddr))
if err != nil {
perror("net.Listen() failed.", err)
os.Exit(1)
}
defer lis.Close()
fmt.Printf("Waiting for new connections from %s ...\n", conf.FwdAddr.String())
// periodic check if smux session is still alive
go func() {
for {
time.Sleep(2*time.Second)
if sess.IsClosed() {
lis.Close()
fmt.Printf("tunnel is closed\n")
break
}
}
}()
for {
fwd_conn, err := lis.Accept()
if err != nil {
perror("lis.Accept() failed.", err)
break
}
stream, err := sess.OpenStream()
if err != nil {
perror("sess.OpenStream() failed.", err)
fwd_conn.Close()
break
}
PrintDbgf("stream open(%d): %v --> tunnel\n", stream.ID(), fwd_conn.RemoteAddr())
go conn2stream(fwd_conn, stream)
} // AcceptTCP()
// clean up
fmt.Printf("...\n")
fmt.Printf("tunnel collapsed: [local]%v <--> [remote]%v\n", sess.LocalAddr(), sess.RemoteAddr())
sess.Close()
kconn.Close()
conn.Close()
time.Sleep(time.Second)
}
func StartClientUDP(conn net.PacketConn, conf *UDPConfig) {
// encrypt socket
if conf.Key != "" {
switch conn.(type) {
case *net.UDPConn:
conn = NewEPacketConn(conn, conf.Enc, conf.Key)
}
}
fmt.Printf("tunnel created: [local]%v <--> [remote]%v\n", conf.LocalAddr(), conf.RemoteAddr())
fmt.Printf("Listen on forward address: %s\n", conf.FwdAddr)
fwd_conn, err := net.ListenUDP("udp", conf.FwdAddr.(*net.UDPAddr))
if err != nil {
perror("net.ListenUDP() failed.", err)
os.Exit(1)
}
var client_addr *net.UDPAddr // address originates from client app
var sent chan struct{} = make(chan struct{}, 1)
// TODO: Below code for udp forwarding is just a prototype and should never
// be used in a production system as it only allows one connection.
// Needs udp muxing.
// conn --> fwd_conn
go func() {
defer conn.Close()
defer fwd_conn.Close()
buf := make([]byte, 4096)
for {
conn.SetDeadline(time.Now().Add(time.Duration(g_timeout) * time.Second))
n, _, err := conn.ReadFrom(buf)
if err != nil {
fmt.Println("conn.ReadFromUDP() failed.", err)
return
}
<-sent // wait until client first send something
_, err = fwd_conn.WriteToUDP(buf[:n], client_addr)
if err != nil {
fmt.Println("fwd_conn.WriteToUDP() failed.", err)
return
}
}
}()
// fwd_conn --> conn
func() {
defer conn.Close()
defer fwd_conn.Close()
buf := make([]byte, 4096)
for {
n, c_addr, err := fwd_conn.ReadFromUDP(buf)
if err != nil {
fmt.Println("fwd_conn.ReadFromUDP() failed.", err)
return
}
// use the most recent client address
if client_addr==nil {
client_addr = c_addr
PrintDbgf("connection from: %s\n", client_addr)
close(sent)
} else if !UDPAddrEqual(c_addr, client_addr) {
client_addr = c_addr
PrintDbgf("new connection from: %s\n", client_addr)
}
_, err = conn.WriteTo(buf[:n], conf.RAddr)
if err != nil {
fmt.Println("conn.Write() failed.", err)
return
}
conn.SetDeadline(time.Now().Add(time.Duration(g_timeout) * time.Second))
}
}()
fmt.Println("...")
fmt.Printf("tunnel collapsed: [local]%v <--> [remote]%v\n", conf.LocalAddr(), conf.RemoteAddr())
time.Sleep(time.Second)
}