This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
396 lines (350 loc) · 9.75 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
package gosocks5
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
"io"
"log"
"net"
"strconv"
)
type Server struct {
Host string
Port int
}
type UDPAssociatePacket struct {
AddrType AddrType
DstIP net.IP
DstPort uint16
Data []byte
}
func NewServer(host string, port int) *Server {
if port == 0 {
port = defaultPort
}
return &Server{
Host: host,
Port: port,
}
}
// ListenAndServe listens on the configured tcp address and
// calls Serve to handle the incoming connections
func (s *Server) ListenAndServe() error {
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.Host, s.Port))
if err != nil {
return errors.Wrap(err, "failed to listen")
}
return s.serve(listener)
}
// Serve accepts new connections and creates a new goroutine
// for handling each
func (s *Server) serve(listener net.Listener) error {
for {
conn, err := listener.Accept()
if err != nil {
return errors.Wrap(err, "failed to accept")
}
go s.Handle(conn)
}
}
func (s *Server) Handle(conn net.Conn) {
defer conn.Close()
if err := s.handleAuth(conn); err != nil {
log.Printf("failed to authenticate the request, err: %v", err)
return
}
processRequest(conn)
}
// handleAuth reads auth request and calls an appropriate
// authenticator if found and send auth failure response
// otherwise
func (s *Server) handleAuth(conn net.Conn) error {
headerBuf := make([]byte, 2)
_, err := conn.Read(headerBuf)
if err != nil {
return err
}
if int(headerBuf[0]) != socks5Version {
return fmt.Errorf("the requested proxy version %d is not supported", int(headerBuf[0]))
}
nMethods := int(headerBuf[1])
authMethodsBuf := make([]byte, nMethods)
_, err = conn.Read(authMethodsBuf)
if err != nil {
return err
}
// authenticate with the first supported method
for i := 0; i < nMethods; i += 1 {
authenticator := getAuthenticator(int(authMethodsBuf[i]))
if authenticator != nil {
if err = authenticator.Authenticate(conn); err != nil {
return errors.Wrap(err, "failed to authenticate")
}
return nil
}
}
_, err = conn.Write([]byte{subnegotiationVersion, noAcceptableMethod})
if err != nil {
return err
}
return fmt.Errorf("no valid method found to authenticate")
}
func processRequest(conn net.Conn) {
header := make([]byte, 3)
_, err := conn.Read(header)
if err != nil {
log.Printf("failed to read from conn, err: %v", err)
return
}
if int(header[0]) != socks5Version {
log.Printf("the requested proxy version %d is not supported", int(header[0]))
return
}
dstAddr, err := readAddrPort(conn)
if err != nil {
if err == ErrInvalidAddrType {
sendReply(conn, replyAddressTypeNotSupported, "")
}
log.Printf("failed to read address & port, err: %v", err)
return
}
var dstIP net.IP
switch dstAddr.addrType {
case addrTypeIPv4, addrTypeIPv6:
dstIP = dstAddr.ip
case addrTypeFQDN:
dstIP, err = resolveAddr(dstAddr.name)
if err != nil {
log.Printf("failed to resolve addr, err: %v", err)
return
}
}
switch int(header[1]) {
case cmdConnect:
handleConnect(conn, dstIP, dstAddr.port)
case cmdBind:
handleBind(conn, dstIP, dstAddr.port)
case cmdUDPAssociate:
handleUDPAssociate(conn, dstIP, dstAddr.port)
default:
sendReply(conn, replyCommandNotSupported, "")
return
}
}
func handleConnect(conn net.Conn, dstIP net.IP, dstPort uint16) {
remoteConn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", dstIP.String(), dstPort))
if err != nil {
log.Printf("failed to dial remote server, err: %v", err)
sendReply(conn, replyHostUnreachable, "")
return
}
localAddr := remoteConn.LocalAddr().(*net.TCPAddr)
sendReply(conn, replySucceeded, localAddr.String())
pipeConnections(conn, remoteConn)
}
func pipeConnections(src, dst net.Conn) {
g := errgroup.Group{}
g.Go(func() error {
_, err := io.Copy(src, dst)
return err
})
g.Go(func() error {
_, err := io.Copy(dst, src)
return err
})
if err := g.Wait(); err != nil {
log.Printf("connection pipe failed, err: %v", err)
}
}
func handleBind(conn net.Conn, dstIP net.IP, dstPort uint16) {
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", dstIP.String(), dstPort))
if err != nil {
log.Printf("failed to listen to the requested addr, err: %v", err)
sendReply(conn, replyGeneralSOCKSServerFailure, "")
return
}
//localAddr := conn.LocalAddr().(*net.TCPAddr)
sendReply(conn, replySucceeded, ln.Addr().String())
server, err := ln.Accept()
if err != nil {
log.Printf("failed to accept new connection, err: %v", err)
sendReply(conn, replyGeneralSOCKSServerFailure, "")
return
}
serverAddr := server.LocalAddr().(*net.TCPAddr)
sendReply(conn, replySucceeded, serverAddr.String())
fmt.Println(4)
pipeConnections(conn, server)
}
// handleUDPAssociate handles the UDPAssociate cmd.
// Note: As per the RFC-1928 we don't notify the clients
// whether we relay or drop packets
func handleUDPAssociate(conn net.Conn, dstIP net.IP, dstPort uint16) {
udpConn, err := net.ListenPacket("udp", net.JoinHostPort(dstIP.String(), strconv.Itoa(int(dstPort))))
if err != nil {
log.Printf("failed to create udp listener, err: %v", err)
return
}
defer udpConn.Close()
bindIP := conn.LocalAddr().(*net.TCPAddr).IP
bindPort := udpConn.LocalAddr().(*net.UDPAddr).Port
sendReply(conn, replySucceeded, net.JoinHostPort(bindIP.String(), strconv.Itoa(bindPort)))
go monitorConn(conn, udpConn)
var (
buf = make([]byte, maxUDPPacketSize)
clientAddr *net.UDPAddr
serverAddr *net.UDPAddr
)
for {
n, addr, err := udpConn.ReadFrom(buf[:])
if err != nil {
log.Printf("failed to read from udp connection, err: %v", err)
return
}
if clientAddr == nil {
clientAddr = addr.(*net.UDPAddr)
}
switch {
case clientAddr != nil && addr.String() == clientAddr.String():
// UDP packets from the client have UDP-Associate header
if err := validateUDPAssociatePacket(buf[:n]); err != nil {
log.Printf("invalid packet, err: %v", err)
return
}
uaPacket, err := parseUDPAssociatePacket(buf[:n])
if err != nil {
log.Printf("failed to parse udp associate packet, err: %v", err)
return
}
serverNetAddr, err := getNetAddr("udp", uaPacket.DstIP, int(uaPacket.DstPort))
if err != nil {
log.Printf("failed to get server net addr, err: %v", err)
return
}
serverAddr = serverNetAddr.(*net.UDPAddr)
if err != nil {
log.Printf("failed to parse UDP Associate Packet, err: %v", err)
return
}
_, err = udpConn.WriteTo(uaPacket.Data, serverAddr)
if err != nil {
log.Printf("failed to write to serverAddr, err: %v", err)
break
}
case serverAddr != nil && addr.String() == serverAddr.String():
// UDP packets sent to the client must have UDP-Associate header
uaPacket, err := packUDPAssociatePacket(buf[:n], serverAddr)
if err != nil {
log.Printf("failed to packed udp packet, err: %v", err)
break
}
_, err = udpConn.WriteTo(uaPacket, clientAddr)
if err != nil {
log.Printf("failed to write to serverAddr, err: %v", err)
break
}
default:
log.Printf("invalid DstIP: %s", addr.String())
continue
}
}
}
func packUDPAssociatePacket(data []byte, addr *net.UDPAddr) ([]byte, error) {
packedWriter := bytes.NewBuffer(make([]byte, 0))
// RSV and FRAG
packedWriter.Write([]byte{0, 0, 0})
if err := writeUDPAssociateAddr(packedWriter, addr); err != nil {
return nil, err
}
packedWriter.Write(data)
return packedWriter.Bytes(), nil
}
func writeUDPAssociateAddr(writer io.Writer, addr *net.UDPAddr) error {
var (
addrType AddrType
ip []byte
)
if addr.IP.To4() != nil {
addrType = addrTypeIPv4
ip = addr.IP.To4()
} else {
addrType = addrTypeIPv6
ip = addr.IP.To16()
}
_, err := writer.Write([]byte{uint8(addrType)})
if err != nil {
return err
}
_, err = writer.Write(ip)
if err != nil {
return err
}
portBytes := make([]byte, 2)
binary.BigEndian.PutUint16(portBytes, uint16(addr.Port))
_, err = writer.Write(portBytes)
return err
}
func validateUDPAssociatePacket(data []byte) error {
if len(data) < minUDPAssociatePacketSize {
return fmt.Errorf("packet length %d is too small", len(data))
}
if int(data[2]) != udpAssociateNoFrag {
return fmt.Errorf("fragmentation is requested but not supported")
}
return nil
}
func parseUDPAssociatePacket(data []byte) (*UDPAssociatePacket, error) {
uaPacket := &UDPAssociatePacket{}
uaPacket.AddrType = AddrType(data[3])
addrLen, err := getAddrLen(uaPacket.AddrType)
if err != nil {
log.Printf("failed to get addr len, err: %v", err)
return nil, err
}
var endOfAddr = 4 + addrLen
dstAddr := data[4:endOfAddr]
if uaPacket.AddrType == addrTypeFQDN {
addrLen = data[4]
endOfAddr = 5 + addrLen
dstAddr = data[5:endOfAddr]
ipAddr, err := net.ResolveIPAddr("ip", string(dstAddr))
if err != nil {
return nil, fmt.Errorf("failed to read address, %w", err)
}
dstAddr = ipAddr.IP
}
uaPacket.DstIP = dstAddr
uaPacket.DstPort = binary.BigEndian.Uint16(data[endOfAddr : endOfAddr+2])
uaPacket.Data = data[endOfAddr+2:]
return uaPacket, nil
}
// monitorConn monitors the tcp connection and closes the udp connection
// as soon as the tcp connection encounters an error.
// Based on RFC-1928:
// A UDP association terminates when the TCP connection that the
// UDP ASSOCIATE request arrived on terminates.
func monitorConn(conn net.Conn, packetConn net.PacketConn) {
for {
buf := make([]byte, 1)
if _, err := conn.Read(buf[:]); err != nil {
packetConn.Close()
return
}
}
}
func sendReply(conn net.Conn, rep uint8, addr string) {
if _, err := conn.Write([]byte{socks5Version, rep, 0}); err != nil {
log.Printf("failed to write reply prefix, err: %v", err)
}
var err error
if rep == replySucceeded {
err = writeAddrPort(conn, addr, false)
} else {
_, err = conn.Write([]byte{0, 1, 0, 0, 0})
}
if err != nil {
log.Printf("failed to write reply, err: %v", err)
}
}