-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.go
398 lines (303 loc) · 10.8 KB
/
server.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
package main
// Defines functions to run the input servers (SOCKS5 and HTTP CONNECT) and to handle incomming client connections.
import (
"bufio"
"context"
"io"
"net"
"net/http"
"sync"
)
const (
atypIPV4 byte = 1 // SOCKS5 IP V4 address address type (see RFC 1928)
atypDomain byte = 3 // SOCKS5 DOMAINNAME address type (see RFC 1928)
atypIPV6 byte = 4 // SOCKS5 IP V6 address address type (see RFC 1928)
cmdConnect byte = 1 // SOCKS5 request CONNECT command (see RFC 1928)
cmdBind byte = 2 // SOCKS5 request BIND command (see RFC 1928)
cmdUDPAssociate byte = 3 // SOCKS5 request UDP ASSOCIATE command (see RFC 1928)
)
type ServerType byte
const (
HTTPCONNECT ServerType = iota // HTTP CONNECT input server type
SOCKS5 // SOCKS5 input server type
)
// run runs an input server of type serverType listening on address
func run(address string, serverType ServerType) {
// Creates a TCP socket and listen on address for incomming client connections
l, err := net.Listen("tcp", address)
if err != nil {
gMetaLogger.Panic(err)
}
gMetaLogger.Infof("Listener started on %v", address)
// For each client connection received on the listening socket, create a context and start a goroutine handling the connection
for {
var c net.Conn
c, err = l.Accept()
if err != nil {
gMetaLogger.Error(err)
continue
}
gMetaLogger.Debugf("new connection (%v) accepted", c)
ctx, cancel := context.WithCancel(context.Background())
switch serverType {
case HTTPCONNECT:
go handleHttpCONNECTConnection(c, ctx, cancel)
case SOCKS5:
go handleSOCKSConnection(c, ctx, cancel)
}
}
}
// handleSOCKSConnection handles the connection of a client on the input SOCKS5 listener.
// It parses the SOCKS command, establishes a connection to the requested host through the right chain,
// transfers data between the established connecion socket and the clien socket, and finally closes evetything on errors or at the end.
func handleSOCKSConnection(client net.Conn, ctx context.Context, cancel context.CancelFunc) {
gMetaLogger.Debugf("Entering handleConnection for connection %v", &client)
defer func() { gMetaLogger.Debugf("Leavings handleConnection for connection %v", &client) }()
defer client.Close()
// ***** BEGIN SOCKS5 input parsing *****
// Parse SOCKS5 input to retrieve command, target host and target port (see RFC 1928)
reader := bufio.NewReader(client)
// Read version and number of methods
buff := make([]byte, 2)
_, err := io.ReadFull(reader, buff)
if err != nil {
gMetaLogger.Errorf("could not read on client socket: %v", err)
return
}
if buff[0] != 5 {
gMetaLogger.Error("only SOCKS5 is supported")
return
}
gMetaLogger.Debugf("received SOCKS %v connection with %v proposed methods", buff[0], buff[1])
// Read methods
buff = make([]byte, buff[1])
_, err = io.ReadFull(reader, buff)
if err != nil {
gMetaLogger.Errorf("could not read on client socket: %v", err)
return
}
gMetaLogger.Debugf("Following methods are proposed: %v", buff)
method := byte(255)
for _, m := range buff {
if m == 0 {
method = 0
}
}
if method == 255 {
gMetaLogger.Error("no accepted methods proposed by the client")
return
}
// Send selected method
_, err = client.Write([]byte{5, method})
if err != nil {
gMetaLogger.Error(err)
return
}
gMetaLogger.Debugf("sending SOCKS answer, accepting method %v", method)
// Read version, cmd, rsv and atyp
buff = make([]byte, 4)
_, err = io.ReadFull(reader, buff)
if err != nil {
gMetaLogger.Error(err)
return
}
cmd := buff[1]
atyp := buff[3]
// Only connect command is supported
if cmd != cmdConnect {
gMetaLogger.Errorf("only CONNECT (0x01) SOCKS command is supported, not 0x0%v", cmd)
client.Write([]byte{5, 7})
return
}
addr, err := addrToString(reader, atyp)
if err != nil {
gMetaLogger.Error(err)
client.Write([]byte{5, 1})
return
}
gMetaLogger.Debugf("received SOCKS CMD packet : cmd=%v - atype=%v - addr=%s\n", cmd, atyp, addr)
// ***** END SOCKS5 input parsing *****
// ***** BEGIN Routing decision *****
// Decide which chain to use based on the target address
var chainStr string
if gArgPACPath != "" {
// -pac flag defined, use PAC to find the chain
chainStr, err = getRouteWithPAC(addr)
if err != nil {
gMetaLogger.Errorf("error getting route PAC: %v", err)
client.Write([]byte{5, 1})
return
}
} else {
// use JSON config to find the chain
gRoutingConf.mu.RLock()
chainStr, err = gRoutingConf.routing.getRoute(addr)
gRoutingConf.mu.RUnlock()
if err != nil {
gMetaLogger.Errorf("error getting route with JSON conf: %v", err)
client.Write([]byte{5, 1})
return
}
}
gMetaLogger.Debugf("chain to use for %v: %v\n", addr, chainStr)
if chainStr == "drop" {
gMetaLogger.Debugf("dropping connection to %v", addr)
gMetaLogger.Auditf("| DROPPED\t| %v\t| %v\t| %v\n", &client, chainStr, addr)
client.Write([]byte{5, 2})
return
}
gChainsConf.mu.RLock()
chain, ok := gChainsConf.proxychains[chainStr]
gChainsConf.mu.RUnlock()
if !ok {
gMetaLogger.Errorf("chain '%v' is not declared in configuration", chainStr)
client.Write([]byte{5, 1})
return
}
// ***** END Routing decision *****
// ***** BEGIN Connection to target host *****
//Connect to chain
target, chainRepresentation, err := chain.connect(addr)
if err != nil {
gMetaLogger.Error(err)
gMetaLogger.Auditf("| ERROR\t| %v\t| %v\t| %v\t| %v\n", &client, chainStr, addr, chainRepresentation)
client.Write([]byte{5, 1})
return
}
defer target.Close()
gMetaLogger.Debugf("Client %v connected to host %v through chain %v", client, addr, chainStr)
// Create auditing trace for connection opening and defering closing trace
gMetaLogger.Auditf("| OPEN\t| %v\t| %v\t| %v\t| %v\n", &client, chainStr, addr, chainRepresentation)
defer gMetaLogger.Auditf("| CLOSE\t| %v\t| %v\t| %v\t| %v\n", &client, chainStr, addr, chainRepresentation)
//Terminate SOCKS5 handshake with client
_, err = client.Write([]byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0})
if err != nil {
gMetaLogger.Error(err)
return
}
gMetaLogger.Debugf("sent SOCKS success response")
// ***** END Connection to target host *****
relay(client, target)
}
// handleHttpCONNECTConnection handles the connection of a client on the input HTTP CONNECT listener.
// It parses the CONNECT request, establishes a connection to the requested host through the right chain,
// transfers data between the established connecion socket and the clien socket, and finally closes evetything on errors or at the end.
func handleHttpCONNECTConnection(client net.Conn, ctx context.Context, cancel context.CancelFunc) {
//debug
gMetaLogger.Debugf("Entering handleHttpCONNNECTConnection for connection %v", &client)
defer func() { gMetaLogger.Debugf("Leaving handleHttpCONNNECTConnection for connection %v", &client) }()
defer client.Close()
// ***** BEGIN HTTP CONNECT input parsing *****
// Parse CONNECT request to retrieve target host and target port
reader := bufio.NewReader(client)
request, err := http.ReadRequest(reader)
if err != nil {
gMetaLogger.Error(err)
return
}
gMetaLogger.Debug(request)
gMetaLogger.Debugf("METHOD: %v\nURL: %v", request.Method, request.URL.Host)
if request.Method != "CONNECT" {
gMetaLogger.Errorf("only HTTP CONNECT method is supported")
(&http.Response{StatusCode: 405, ProtoMajor: 1}).Write(client)
return
}
if request.Host != request.URL.Host {
gMetaLogger.Error("host and URL do not match")
(&http.Response{StatusCode: 400, ProtoMajor: 1}).Write(client)
return
}
addr := request.Host
// ***** END HTTP CONNECT input parsing *****
// ***** BEGIN Routing decision *****
var chainStr string
if gArgPACPath != "" {
// -pac flag defined, use PAC to find the chain
chainStr, err = getRouteWithPAC(addr)
if err != nil {
gMetaLogger.Errorf("error getting route PAC: %v", err)
(&http.Response{StatusCode: 400, ProtoMajor: 1}).Write(client)
return
}
} else {
// use JSON config to find the chain
gRoutingConf.mu.RLock()
chainStr, err = gRoutingConf.routing.getRoute(addr)
gRoutingConf.mu.RUnlock()
if err != nil {
gMetaLogger.Errorf("error getting route with JSON conf: %v", err)
(&http.Response{StatusCode: 400, ProtoMajor: 1}).Write(client)
return
}
}
gMetaLogger.Debugf("chain to use for %v: %v\n", addr, chainStr)
if chainStr == "drop" {
gMetaLogger.Debugf("dropping connection to %v", addr)
gMetaLogger.Auditf("| DROPPED\t| %v\t| %v\t| %v\n", &client, chainStr, addr)
(&http.Response{StatusCode: 403, ProtoMajor: 1}).Write(client)
return
}
gChainsConf.mu.RLock()
chain, ok := gChainsConf.proxychains[chainStr]
gChainsConf.mu.RUnlock()
if !ok {
gMetaLogger.Errorf("chain '%v' returned by PAC script is not declared in configuration", chainStr)
(&http.Response{StatusCode: 500, ProtoMajor: 1}).Write(client)
return
}
// ***** END Routing decision *****
// ***** BEGIN Connection to target host *****
//Connect to chain
target, chainRepresentation, err := chain.connect(addr)
if err != nil {
gMetaLogger.Error(err)
gMetaLogger.Auditf("| ERROR\t| %v\t| %v\t| %v\t| %v\n", &client, chainStr, addr, chainRepresentation)
(&http.Response{StatusCode: 502, ProtoMajor: 1}).Write(client)
return
}
defer target.Close()
gMetaLogger.Debugf("Client %v connected to host %v through chain %v", client, addr, chainStr)
// Create auditing trace for connection opening and defering closing trace
gMetaLogger.Auditf("| OPEN\t| %v\t| %v\t| %v\t| %v\n", &client, chainStr, addr, chainRepresentation)
defer gMetaLogger.Auditf("| CLOSE\t| %v\t| %v\t| %v\t| %v\n", &client, chainStr, addr, chainRepresentation)
// Send HTTP Success
err = (&http.Response{StatusCode: 200, ProtoMajor: 1}).Write(client)
if err != nil {
gMetaLogger.Error(err)
return
}
gMetaLogger.Debugf("sent HTTP success response")
// ***** END Connection to target host *****
relay(client, target)
}
// relay takes two net.Conn target and client (representing TCP sockets) and transfers data between them.
func relay(client net.Conn, target net.Conn) {
var wg sync.WaitGroup
wg.Add(1)
// Transfer from target to client
go func() {
defer wg.Done()
defer client.Close()
defer target.Close()
written, err := io.Copy(client, target)
gMetaLogger.Debugf("%v bytes sent from target %v to client %v", written, target, client)
if err != nil {
gMetaLogger.Debugf("copy from target to client returned an error: %v", err)
}
}()
wg.Add(1)
// Transfer from client to target
go func() {
defer wg.Done()
defer client.Close()
defer target.Close()
written, err := io.Copy(target, client)
gMetaLogger.Debugf("%v bytes sent from client %v to target %v", written, client, target)
if err != nil {
gMetaLogger.Debugf("copy from client to target returned an error: %v", err)
}
}()
gMetaLogger.Debug("Waiting for both goroutines to complete")
wg.Wait()
gMetaLogger.Debug("Goroutines ended")
}