-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer_socks5.go
178 lines (153 loc) · 5.12 KB
/
dialer_socks5.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
package main
import (
"context"
"errors"
"io"
"net"
"net/netip"
"strconv"
"time"
)
var _ Dialer = (*Socks5Dialer)(nil)
type Socks5Dialer struct {
Username string
Password string
Host string
Port string
Socks5H bool
Resolver *Resolver
Dialer Dialer
}
func (d *Socks5Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
conn, err := d.Dialer.DialContext(ctx, network, net.JoinHostPort(d.Host, d.Port))
if err != nil {
return nil, err
}
closeConn := &conn
defer func() {
if closeConn != nil {
(*closeConn).Close()
}
}()
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, errors.New("proxy: failed to parse port number: " + portStr)
}
if port < 1 || port > 0xffff {
return nil, errors.New("proxy: port number out of range: " + portStr)
}
if !d.Socks5H && d.Resolver != nil {
ips, err := d.Resolver.LookupNetIP(ctx, "ip", host)
if err == nil && len(ips) > 0 {
host = ips[0].String()
}
}
// the size here is just an estimate
buf := make([]byte, 0, 6+len(host))
buf = append(buf, VersionSocks5)
if len(d.Username) > 0 && len(d.Username) < 256 && len(d.Password) < 256 {
buf = append(buf, 2 /* num auth methods */, Socks5AuthMethodNone, Socks5AuthMethodPassword)
} else {
buf = append(buf, 1 /* num auth methods */, Socks5AuthMethodNone)
}
if _, err := conn.Write(buf); err != nil {
return nil, errors.New("proxy: failed to write greeting to SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
return nil, errors.New("proxy: failed to read greeting from SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
if buf[0] != 5 {
return nil, errors.New("proxy: SOCKS5 proxy at " + d.Host + " has unexpected version " + strconv.Itoa(int(buf[0])))
}
if buf[1] == 0xff {
return nil, errors.New("proxy: SOCKS5 proxy at " + d.Host + " requires authentication")
}
if buf[1] == byte(Socks5AuthMethodPassword) {
buf = buf[:0]
buf = append(buf, 1 /* password protocol version */)
buf = append(buf, uint8(len(d.Username)))
buf = append(buf, d.Username...)
buf = append(buf, uint8(len(d.Password)))
buf = append(buf, d.Password...)
if _, err := conn.Write(buf); err != nil {
return nil, errors.New("proxy: failed to write authentication request to SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
return nil, errors.New("proxy: failed to read authentication reply from SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
if buf[1] != 0 {
return nil, errors.New("proxy: SOCKS5 proxy at " + d.Host + " rejected username/password")
}
}
buf = buf[:0]
switch network {
case "tcp", "tcp6", "tcp4":
buf = append(buf, VersionSocks5, SocksCommandConnectTCP, 0 /* reserved */)
case "udp", "udp6", "udp4":
buf = append(buf, VersionSocks5, SocksCommandConnectUDP, 0 /* reserved */)
default:
return nil, errors.New("proxy: no support for SOCKS5 proxy connections of type " + network)
}
if ip, err := netip.ParseAddr(host); err == nil {
if ip.Is4() {
buf = append(buf, Socks5IPv4Address)
} else {
buf = append(buf, Socks5IPv6Address)
}
buf = append(buf, ip.AsSlice()...)
} else {
if len(host) > 255 {
return nil, errors.New("proxy: destination hostname too long: " + host)
}
buf = append(buf, Socks5DomainName)
buf = append(buf, byte(len(host)))
buf = append(buf, host...)
}
buf = append(buf, byte(port>>8), byte(port))
if deadline, ok := ctx.Deadline(); ok {
conn.SetDeadline(deadline)
defer conn.SetDeadline(time.Time{})
}
if _, err := conn.Write(buf); err != nil {
return nil, errors.New("proxy: failed to write connect request to SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
if _, err := io.ReadFull(conn, buf[:4]); err != nil {
return nil, errors.New("proxy: failed to read connect reply from SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
if status := Socks5Status(buf[1]); status > 0 {
return nil, errors.New("proxy: SOCKS5 proxy at " + d.Host + " failed to connect: " + status.String())
}
bytesToDiscard := 0
switch buf[3] {
case Socks5IPv4Address:
bytesToDiscard = net.IPv4len
case Socks5IPv6Address:
bytesToDiscard = net.IPv6len
case Socks5DomainName:
_, err := io.ReadFull(conn, buf[:1])
if err != nil {
return nil, errors.New("proxy: failed to read domain length from SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
bytesToDiscard = int(buf[0])
default:
return nil, errors.New("proxy: got unknown address type " + strconv.Itoa(int(buf[3])) + " from SOCKS5 proxy at " + d.Host)
}
if cap(buf) < bytesToDiscard {
buf = make([]byte, bytesToDiscard)
} else {
buf = buf[:bytesToDiscard]
}
if _, err := io.ReadFull(conn, buf); err != nil {
return nil, errors.New("proxy: failed to read address from SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
// Also need to discard the port number
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
return nil, errors.New("proxy: failed to read port from SOCKS5 proxy at " + d.Host + ": " + err.Error())
}
closeConn = nil
return conn, nil
}