-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
258 lines (225 loc) · 5.13 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
package socks
import (
"encoding/binary"
"errors"
"fmt"
"io"
"net"
"net/netip"
"strconv"
"strings"
)
type cmd byte
const (
Connect cmd = iota + 1
Bind
Udp
)
type atyp byte
const (
IPv4 atyp = iota + 1
_
Domain
IPv6
)
type rep byte
const (
Succeeded rep = iota
GeneralSocks5ServerFailure
ConnectionNotAllowedByRuleset
NetworkUnreachable
HostUnreachable
ConnectionRefused
TTLExpired
CommandNotSupported
AddressTypeNotSupported
)
func genAddrByAtyp(a atyp, addr string) ([]byte, error) {
byts := make([]byte, 0)
switch a {
case IPv4:
addr, err := netip.ParseAddrPort(addr)
if err != nil {
return nil, err
}
as4 := addr.Addr().As4()
for i := 0; i < len(as4); i++ {
byts = append(byts, as4[i])
}
port := make([]byte, 2)
binary.BigEndian.PutUint16(port, addr.Port())
byts = append(byts, port...)
case IPv6:
addr, err := netip.ParseAddrPort(addr)
if err != nil {
return Request{}, err
}
as6 := addr.Addr().As16()
for i := 0; i < len(as6); i++ {
byts = append(byts, as6[i])
}
port := make([]byte, 2)
binary.BigEndian.PutUint16(port, addr.Port())
byts = append(byts, port...)
case Domain:
arr := strings.Split(addr, ":")
if len(arr) != 2 {
return nil, errors.New("domain format error")
}
byts = append(byts, byte(len(arr[0])))
byts = append(byts, []byte(arr[0])...)
uintp, err := strconv.ParseUint(arr[1], 10, 16)
if err != nil {
return nil, err
}
port := make([]byte, 2)
binary.BigEndian.PutUint16(port, uint16(uintp))
byts = append(byts, port...)
default:
return nil, errors.New("unexpected atyp")
}
return byts, nil
}
func readAddrByAtyp(r io.Reader, a atyp) ([]byte, error) {
var byts []byte
var addr []byte
port := make([]byte, 2)
switch a {
case IPv4:
addr = make([]byte, 4)
case IPv6:
addr = make([]byte, 16)
case Domain:
num := make([]byte, 1)
_, err := r.Read(num)
if err != nil {
return nil, err
}
byts = append(byts, num...)
addr = make([]byte, num[0])
}
_, err := r.Read(addr)
if err != nil {
return nil, err
}
_, err = r.Read(port)
if err != nil {
return nil, err
}
byts = append(byts, addr...)
byts = append(byts, port...)
return byts, nil
}
func ReadRequest(r io.Reader) (Request, error) {
req := make(Request, 4)
_, err := r.Read(req)
if err != nil {
return nil, err
}
byts, err := readAddrByAtyp(r, atyp(req[3]))
if err != nil {
return nil, err
}
req = append(req, byts...)
return req, nil
}
func NewRequest(c cmd, a atyp, addr string) (Request, error) {
var byts Request
byts = append(byts, 5, byte(c), 0, byte(a))
b, err := genAddrByAtyp(a, addr)
byts = append(byts, b...)
return byts, err
}
type Request []byte
func (r Request) AddrType() atyp { return atyp(r[3]) }
func (r Request) Addr() string {
switch r.AddrType() {
case IPv4:
addr := netip.AddrFrom4([4]byte{r[4], r[5], r[6], r[7]})
addrport := netip.AddrPortFrom(addr, binary.BigEndian.Uint16(r[8:]))
return addrport.String()
case IPv6:
addr := netip.AddrFrom16([16]byte{r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15], r[16], r[17], r[18], r[19]})
addrport := netip.AddrPortFrom(addr, binary.BigEndian.Uint16(r[20:]))
return addrport.String()
case Domain:
return fmt.Sprintf("%s:%d", r[5:5+r[4]], binary.BigEndian.Uint16(r[5+r[4]:]))
default:
return ""
}
}
func ReadRespone(r io.Reader) (Respone, error) {
resp := make(Respone, 4)
_, err := r.Read(resp)
if err != nil {
return nil, err
}
byts, err := readAddrByAtyp(r, atyp(resp[3]))
if err != nil {
return nil, err
}
resp = append(resp, byts...)
return resp, nil
}
func NewRespone(r rep, a atyp, addr string) (Respone, error) {
var byts Respone
byts = append(byts, 5, byte(r), 0, byte(a))
b, err := genAddrByAtyp(a, addr)
byts = append(byts, b...)
return byts, err
}
type Respone []byte
func (r Respone) AddrType() atyp { return atyp(r[3]) }
func (r Respone) Addr() string {
switch r.AddrType() {
case IPv4:
addr := netip.AddrFrom4([4]byte{r[4], r[5], r[6], r[7]})
addrport := netip.AddrPortFrom(addr, binary.BigEndian.Uint16(r[8:]))
return addrport.String()
case IPv6:
addr := netip.AddrFrom16([16]byte{r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15], r[16], r[17], r[18], r[19]})
addrport := netip.AddrPortFrom(addr, binary.BigEndian.Uint16(r[20:]))
return addrport.String()
case Domain:
return fmt.Sprintf("%s:%d", r[5:5+r[4]], binary.BigEndian.Uint16(r[5+r[4]:]))
default:
return ""
}
}
func Client(conn net.Conn, dstAddr string) (net.Conn, error) {
_, err := conn.Write([]byte{5, 1, 0})
if err != nil {
return nil, err
}
msg := make([]byte, 2)
conn.Read(msg)
if err != nil {
return nil, err
}
if msg[0] != 5 {
return nil, errors.New("unsupported version")
}
switch msg[1] {
case 0:
req, err := NewRequest(Connect, IPv4, dstAddr)
if err != nil {
return nil, err
}
_, err = conn.Write(req)
if err != nil {
return nil, err
}
resp, err := ReadRespone(conn)
if err != nil {
return nil, err
}
switch resp[1] {
case byte(Succeeded):
return conn, nil
default:
return nil, errors.New("unknown rep")
}
default:
return nil, errors.New("unsupported mthod")
}
}