-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
78 lines (65 loc) · 1.5 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
package proxifier
import (
"context"
"net"
)
// Detail about the network to handshake
type Context struct {
Resolver interface{}
Port int
}
// Core proxy client
type Client struct {
*net.TCPConn // underyling tcp connection
target *Context // target context
proxy *Context // proxy context
worker chan error // synchronization primitive
}
// SOCKS5 client
type Socks4Client struct {
Client // core client
UID []byte // userid, defaults to null
}
// SOCKS5 client
type Socks5Client struct {
Client // core client
Auth // authentication context
}
// Authentication credentials
type Auth struct {
Username string // Username
Password string // Password
}
// Proxy client which implements SOCKS4/SOCKS5
type SocksClient interface {
*Socks4Client | *Socks5Client
setup() chan error
init(target, proxy *Context) error
}
// TCP/IP stream
type Command = byte
const (
CMD Command = 0x01 // stream connection
NULL byte = 0x00 // null byte
)
// Creates new [SocksClient].
//
// ``target`` and ``proxy`` comfort [Context]
//
// On failure, returns error.
func New[T SocksClient](client T, target, proxy Context) (T, error) {
return client, client.init(&target, &proxy)
}
// Tunnels through proxy to target. On failure returns error.
//
// ``ctx`` is a [context.Context] used for timeout/cancellation signal.
func Connect[T SocksClient](client T, ctx context.Context) error {
worker := client.setup()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-worker:
close(worker)
return err
}
}