-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
581 lines (500 loc) · 18.3 KB
/
api.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package socksauth
import (
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"sync/atomic"
"syscall"
)
const (
_SOCKS_VERSION = 0x05
_NO_ACCEPTABLE_METHODS = 0xff
_NO_AUTHENTICATION = 0x00
_USERNAME_PASSWORD_AUTH = 0x02
_CONNECT = 0x01
_STATUS_OK = 0x00
_GENERAL_SOCKS_FAILURE = 0x01
_CONN_NOT_ALLOWED_BY_RULESET = 0x02
_NETWORK_UNREACHABLE = 0x03
_HOST_UNREACHABLE = 0x04
_CONN_REFUSED = 0x05
_TTL_EXPIRED = 0x06
_COMMAND_NOT_SUPPORTED = 0x07
_ADDRESS_TYPE_NOT_SUPPORTED = 0x08
_IP_V4 = 0x01
_DOMAIN_NAME = 0x03
_IP_V6 = 0x04
)
var (
ErrEstablishClientConn = newError("ERR_ESTABLISH_CLIENT_CONN", "error establishing the client connection")
ErrEstablishProxyConn = newError("ERR_ESTABLISH_PROXY_CONN", "error establishing the proxy connection")
ErrAuthentication = newError("ERR_AUTHENTICATION", "error authenticating with the proxy server")
ErrDataTransfer = newError("ERR_DATA_TRANSFER", "error transferring data between client and proxy server")
ErrSocksFailure = newError("ERR_SOCKS_FAILURE", "general SOCKS failure")
ErrConnectionNotAllowed = newError("ERR_CONN_NOT_ALLOWED", "connection not allowed by ruleset")
ErrNetworkUnreachable = newError("ERR_NETWORK_UNREACHABLE", "network unreachable")
ErrHostUnreachable = newError("ERR_HOST_UNREACHABLE", "destination host is unreachable")
ErrConnectionRefused = newError("ERR_CONN_REFUSED", "destination host refused the connection")
ErrTTLExpired = newError("ERR_TTL_EXPIRED", "TTL expired")
ErrCommandNotSupported = newError("ERR_COMMAND_NOT_SUPPORTED", "command not supported")
ErrAddressTypeNotSupported = newError("ERR_ADDRESS_TYPE_NOT_SUPPORTED", "address type not supported")
)
type Server struct {
Addr string
RemoteUser string
RemotePass string
ConnCount atomic.Int64
OpenConnCount atomic.Int32
openConnLimit uint32
onConnect func(id int64, conn net.Conn)
onDisconnect func(id int64, conn net.Conn)
onError func(id int64, conn net.Conn, err SocksError)
serverFinder func(context.Context) (string, error)
}
type ServerOption func(*Server)
// WithOpenConnLimit sets the maximum number of open connections the server will accept
// Default is 0, which means no limit
func WithOpenConnLimit(limit uint32) ServerOption {
return func(s *Server) { s.openConnLimit = limit }
}
// WithOnConnect sets the onConnect callback which is called when a new connection is accepted
// To not block the server the callback is called in a new goroutine
func WithOnConnect(fn func(id int64, conn net.Conn)) ServerOption {
return func(s *Server) { s.onConnect = fn }
}
// WithOnDisconnect sets the onDisconnect callback which is called when a connection is closed
// To not block the server the callback is called in a new goroutine
func WithOnDisconnect(fn func(id int64, conn net.Conn)) ServerOption {
return func(s *Server) { s.onDisconnect = fn }
}
// WithOnError sets the onError callback which is called when an error occurs on a connection.
// If the error occurs while accepting a connection the id is 0 and the conn argument will be nil
// To not block the server the callback is called in a new goroutine
func WithOnError(fn func(id int64, conn net.Conn, err SocksError)) ServerOption {
return func(s *Server) { s.onError = fn }
}
// WithServerFinder sets the function to find a SOCKS5 server if no remoteHost is provided
// Default will find a NordVPN server and try to authenticate with it
func WithServerFinder(fn func(context.Context) (string, error)) ServerOption {
return func(s *Server) { s.serverFinder = fn }
}
// WithAddr sets the address the server will listen on
// Default is ":1080"
func WithAddr(addr string) ServerOption {
return func(s *Server) { s.Addr = addr }
}
// NewServer creates a new SOCKS5 server
// if the remoteHost is empty the server will try to find a server using the serverFinder function specified in the WithServerFinder option (default is FindNordVpnServer)
// if the remoteUser and remotePass are empty the server will not authenticate with the remote server, so it is just a simple SOCKS5 proxy, no auth.
func NewServer(remoteHost, remoteUser, remotePass string, opts ...ServerOption) *Server {
s := &Server{
Addr: ":1080",
RemoteUser: remoteUser,
RemotePass: remotePass,
ConnCount: atomic.Int64{},
OpenConnCount: atomic.Int32{},
}
for _, opt := range opts {
opt(s)
}
if remoteHost == "" && s.serverFinder == nil {
s.serverFinder = FindNordVpnServer
}
if s.serverFinder == nil {
s.serverFinder = func(ctx context.Context) (string, error) { return remoteHost, nil }
} else {
// we call the serverFinder here to give the injection the chance to cache (the default FindNordVpnServer does)
s.serverFinder(context.Background())
}
return s
}
func (s *Server) Start(ctx context.Context) error {
l, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
}
s.Addr = "socks5://" + l.Addr().String()
var semaphore chan struct{}
if s.openConnLimit > 0 {
semaphore = make(chan struct{}, s.openConnLimit)
}
for {
select {
case <-ctx.Done():
return l.Close()
default:
}
conn, err := l.Accept()
if err != nil {
if s.onError != nil {
err = fmt.Errorf("error accepting connection: %w", err)
go s.onError(0, conn, ErrEstablishClientConn.withError(err))
}
continue
}
// aquire semaphore
if semaphore != nil {
semaphore <- struct{}{}
}
go func() {
s.handleConnection(ctx, conn)
// release semaphore
if semaphore != nil {
semaphore <- struct{}{}
}
}()
}
}
func (s *Server) handleConnection(ctx context.Context, clientConn net.Conn) {
conn := &socksConnection{connId: s.ConnCount.Add(1), clientConn: clientConn}
s.OpenConnCount.Add(1)
if s.onConnect != nil {
go s.onConnect(conn.connId, conn.clientConn)
}
ctx, cancel := context.WithCancel(ctx)
defer func() {
if s.onDisconnect != nil {
go s.onDisconnect(conn.connId, conn.clientConn)
}
conn.clientConn.Close()
cancel()
s.OpenConnCount.Add(-1)
}()
// Greet the client
if err := conn.greetClient(); err != nil {
if s.onError != nil {
go s.onError(conn.connId, clientConn, err)
}
return
}
// Connect to the remote SOCKS5 server
err := conn.getProxyConn(ctx, s.serverFinder)
if err != nil {
if s.onError != nil {
go s.onError(conn.connId, clientConn, err)
}
return
}
defer conn.proxyConn.Close()
// Authenticate with the remote SOCKS5 server
// TODO: implement unauthenticated connection
err = conn.authenticateRemoteSocks(s.RemoteUser, s.RemotePass)
if err != nil {
if s.onError != nil {
go s.onError(conn.connId, clientConn, err)
}
return
}
// Forward the client's request to the remote SOCKS5 server
err = conn.sendRemoteRequest()
if err != nil {
if s.onError != nil {
go s.onError(conn.connId, clientConn, err)
}
return
}
// Relay data between the client and the remote SOCKS5 server
err = conn.syncConns()
if err != nil {
if s.onError != nil {
go s.onError(conn.connId, clientConn, err)
}
return
}
}
type socksConnection struct {
connId int64
clientConn, proxyConn net.Conn
destination string
proxyName, proxyHost string
}
func (c *socksConnection) greetClient() SocksError {
// https://datatracker.ietf.org/doc/html/rfc1928#section-3
header := make([]byte, 2)
if _, err := io.ReadFull(c.clientConn, header); err != nil {
err = fmt.Errorf("error reading header: %w", err)
return ErrEstablishClientConn.fromConnection(*c).withError(err)
}
socksVersion := header[0]
if socksVersion != _SOCKS_VERSION {
err := fmt.Errorf("unsupported SOCKS version: %d", socksVersion)
return ErrEstablishClientConn.fromConnection(*c).withError(err)
}
numMethods := int(header[1])
methods := make([]byte, numMethods)
if _, err := io.ReadFull(c.clientConn, methods); err != nil {
err := fmt.Errorf("error reading methods: %w", err)
return ErrEstablishClientConn.fromConnection(*c).withError(err)
}
if !contains(methods, _NO_AUTHENTICATION) {
c.clientConn.Write([]byte{_SOCKS_VERSION, _NO_ACCEPTABLE_METHODS})
err := fmt.Errorf("no supported authentication methods")
return ErrEstablishClientConn.fromConnection(*c).withError(err)
}
c.clientConn.Write([]byte{_SOCKS_VERSION, _NO_AUTHENTICATION})
return nil
}
func (c *socksConnection) getProxyConn(ctx context.Context, hostFinder func(context.Context) (string, error)) SocksError {
var err error
c.proxyName, err = hostFinder(ctx)
if err != nil {
err = fmt.Errorf("error finding proxy server: %w", err)
return ErrEstablishProxyConn.fromConnection(*c).withError(err)
}
c.proxyName = strings.TrimPrefix(c.proxyName, "socks5://")
if !strings.Contains(c.proxyName, ":") {
c.proxyName += ":1080"
}
c.proxyConn, err = net.Dial("tcp", c.proxyName)
if err != nil {
return ErrEstablishProxyConn.fromConnection(*c).withError(err)
}
c.proxyHost = c.proxyConn.RemoteAddr().String()
return nil
}
func (c *socksConnection) authenticateRemoteSocks(username, password string) SocksError {
// Send the authentication methods supported by the client https://datatracker.ietf.org/doc/html/rfc1928#section-3
_, err := c.proxyConn.Write([]byte{
_SOCKS_VERSION,
0x01, // number of auth methods
_USERNAME_PASSWORD_AUTH,
})
if err != nil {
err = fmt.Errorf("error sending authentication methods: %w", err)
return ErrAuthentication.fromConnection(*c).withError(err)
}
// Read the server's choice of authentication method
response := make([]byte, 2)
if _, err := io.ReadFull(c.proxyConn, response); err != nil {
err = fmt.Errorf("error reading authentication method selection: %w", err)
return ErrAuthentication.fromConnection(*c).withError(err)
}
// Check if the server selected username/password authentication
if response[1] != _USERNAME_PASSWORD_AUTH {
err = fmt.Errorf("server did not select username/password authentication, selected method: %d", response[1])
return ErrAuthentication.fromConnection(*c).withError(err)
}
// Then, send the username and password
// https://datatracker.ietf.org/doc/html/rfc1929#section-2
authRequest := make([]byte, 3+len(username)+len(password)) // 1 byte to specify subnegotiation version, 1 byte for username length, 1 byte for password length
authRequest[0] = 0x01 // version 1 of the subnegotiation
authRequest[1] = byte(len(username))
copy(authRequest[2:], username)
authRequest[2+len(username)] = byte(len(password))
copy(authRequest[3+len(username):], password)
_, err = c.proxyConn.Write(authRequest)
if err != nil {
err = fmt.Errorf("error sending username/password: %w", err)
return ErrAuthentication.fromConnection(*c).withError(err)
}
// Read the server's response
response = make([]byte, 2)
response[1] = 0xff // Set the response to an invalid value to check if the server changes it (00 is success)
if _, err := io.ReadFull(c.proxyConn, response); err != nil {
err = fmt.Errorf("error reading authentication response: %w", err)
return ErrAuthentication.fromConnection(*c).withError(err)
}
// Check the server's response
if response[1] != _STATUS_OK {
err = fmt.Errorf("authentication failed")
return ErrAuthentication.fromConnection(*c).withError(err)
}
return nil
}
func (c *socksConnection) syncConns() SocksError {
done := make(chan error, 1)
// Relay data from client to remote
go func() {
_, err := io.Copy(c.proxyConn, c.clientConn)
if errors.Is(err, syscall.ECONNRESET) {
fmt.Printf("[connection %d] proxy -> client: disconnected\n", c.connId)
done <- nil // this happens when the client disconnects abruptly, rude but not an error
}
if err != nil {
done <- fmt.Errorf("error copying data from client to remote: %w", err)
return
}
done <- nil
}()
// Relay data from remote to client
go func() {
_, err := io.Copy(c.clientConn, c.proxyConn)
if errors.Is(err, syscall.ECONNRESET) {
fmt.Printf("[connection %d] client -> proxy: disconnected\n", c.connId)
done <- nil // this happens when the client disconnects abruptly, rude but not an error
}
if err != nil {
done <- fmt.Errorf("error copying data from remote to client: %w", err)
return
}
done <- nil
}()
// Wait for either goroutine to finish
err := <-done
if err != nil {
return ErrDataTransfer.fromConnection(*c).withError(err)
}
return nil
}
func (c *socksConnection) sendRemoteRequest() SocksError {
request, destination, err := readSocks5Request(c.clientConn)
if err != nil {
err = fmt.Errorf("error reading request from client: %w", err)
return ErrEstablishClientConn.fromConnection(*c).withError(err)
}
c.destination = destination
_, err = c.proxyConn.Write(request)
if err != nil {
err = fmt.Errorf("error forwarding request to proxy server: %w", err)
return ErrEstablishProxyConn.fromConnection(*c).withError(err)
}
response, err := readSocks5Response(c.proxyConn)
if err != nil {
err = fmt.Errorf("error reading response from proxy server: %w", err)
return ErrEstablishProxyConn.fromConnection(*c).withError(err)
}
_, err = c.clientConn.Write(response)
if err != nil {
err = fmt.Errorf("error forwarding response to client: %w", err)
return ErrEstablishClientConn.fromConnection(*c).withError(err)
}
return nil
}
func readSocks5Request(conn net.Conn) (request []byte, destination string, err error) {
// Read the SOCKS request from the client https://datatracker.ietf.org/doc/html/rfc1928#section-4
// Read the first 4 Bytes of the request, the fourth byte determines the length of the rest of the request
requestHeader := make([]byte, 4)
if _, err := io.ReadFull(conn, requestHeader); err != nil {
return nil, "", fmt.Errorf("error reading request header: %w", err)
}
version := requestHeader[0]
if version != _SOCKS_VERSION {
conn.Write([]byte{_SOCKS_VERSION, _GENERAL_SOCKS_FAILURE})
return nil, "", fmt.Errorf("unsupported SOCKS version: %d", version)
}
cmd := requestHeader[1]
if cmd != _CONNECT {
conn.Write([]byte{_SOCKS_VERSION, _COMMAND_NOT_SUPPORTED})
return nil, "", ErrCommandNotSupported.withMessage(fmt.Sprintf("unsupported command: %d", cmd))
}
// Determine the length of the remaining part of the request based on the address type
addrLen := 0
switch requestHeader[3] { // ATYP, the address type
case _IP_V4:
addrLen = net.IPv4len
case _DOMAIN_NAME:
lengthByte := make([]byte, 1)
if _, err := io.ReadFull(conn, lengthByte); err != nil {
return nil, "", fmt.Errorf("error reading domain name length: %w", err)
}
requestHeader = append(requestHeader, lengthByte...)
addrLen = int(lengthByte[0])
case _IP_V6:
addrLen = net.IPv6len
default:
conn.Write([]byte{_SOCKS_VERSION, _ADDRESS_TYPE_NOT_SUPPORTED})
return nil, "", ErrAddressTypeNotSupported.withMessage(fmt.Sprintf("unknown address type: %d", requestHeader[3]))
}
// Read the rest of the request
requestRest := make([]byte, addrLen+2) // +2 for port number
if _, err := io.ReadFull(conn, requestRest); err != nil {
conn.Write([]byte{_SOCKS_VERSION, _GENERAL_SOCKS_FAILURE})
return nil, "", fmt.Errorf("error reading the rest of the request: %w", err)
}
// Combine the header and the rest of the request
fullRequest := append(requestHeader, requestRest...)
// we are done here, but for logging purpose we will extract the destination
var destinationAddr string
var destinationPort int
switch fullRequest[3] {
case _IP_V4:
destinationAddr = net.IP(fullRequest[4 : 4+net.IPv4len]).String()
destinationPort = int(fullRequest[len(fullRequest)-2])<<8 | int(fullRequest[len(fullRequest)-1])
case _DOMAIN_NAME:
destinationAddr = string(fullRequest[5 : 5+fullRequest[4]])
destinationPort = int(fullRequest[len(fullRequest)-2])<<8 | int(fullRequest[len(fullRequest)-1])
case _IP_V6:
destinationAddr = net.IP(fullRequest[4 : 4+net.IPv6len]).String()
destinationPort = int(fullRequest[len(fullRequest)-2])<<8 | int(fullRequest[len(fullRequest)-1])
}
return fullRequest, fmt.Sprintf("%s:%d", destinationAddr, destinationPort), nil
}
func readSocks5Response(conn net.Conn) ([]byte, error) {
// Read the SOCKS response from the remote server
// https://datatracker.ietf.org/doc/html/rfc1928#section-6
header := make([]byte, 4)
if _, err := io.ReadFull(conn, header); err != nil {
conn.Write([]byte{_SOCKS_VERSION, _GENERAL_SOCKS_FAILURE})
return nil, fmt.Errorf("error reading response header: %w", err)
}
version := header[0]
if version != _SOCKS_VERSION {
conn.Write([]byte{_SOCKS_VERSION, _GENERAL_SOCKS_FAILURE})
return nil, fmt.Errorf("unsupported SOCKS version: %d", version)
}
reply := header[1]
switch reply {
case _STATUS_OK:
case _GENERAL_SOCKS_FAILURE:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrSocksFailure
case _CONN_NOT_ALLOWED_BY_RULESET:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrConnectionNotAllowed
case _NETWORK_UNREACHABLE:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrNetworkUnreachable
case _HOST_UNREACHABLE:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrHostUnreachable
case _CONN_REFUSED:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrConnectionRefused
case _TTL_EXPIRED:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrTTLExpired
case _COMMAND_NOT_SUPPORTED:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrCommandNotSupported
case _ADDRESS_TYPE_NOT_SUPPORTED:
conn.Write([]byte{_SOCKS_VERSION, header[1]})
return nil, ErrAddressTypeNotSupported
default:
conn.Write([]byte{_SOCKS_VERSION, _GENERAL_SOCKS_FAILURE})
return nil, fmt.Errorf("unknown reply: %d", reply)
}
addrLen := 0
switch header[3] { // Address type
case _IP_V4:
addrLen = net.IPv4len
case _DOMAIN_NAME:
lengthByte := make([]byte, 1)
if _, err := io.ReadFull(conn, lengthByte); err != nil {
return nil, fmt.Errorf("error reading domain name length: %w", err)
}
addrLen = int(lengthByte[0])
case _IP_V6:
addrLen = net.IPv6len
default:
conn.Write([]byte{_SOCKS_VERSION, _ADDRESS_TYPE_NOT_SUPPORTED})
return nil, fmt.Errorf("unknown address type: %d", header[3])
}
requestRest := make([]byte, addrLen+2) // +2 for port
if _, err := io.ReadFull(conn, requestRest); err != nil {
conn.Write([]byte{_SOCKS_VERSION, _GENERAL_SOCKS_FAILURE})
return nil, fmt.Errorf("error reading the rest of the response: %w", err)
}
fullResponse := append(header, requestRest...)
return fullResponse, nil
}
func contains[T comparable](slice []T, item T) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}