-
Notifications
You must be signed in to change notification settings - Fork 75
/
handle.go
358 lines (329 loc) · 10.1 KB
/
handle.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
package socks5
import (
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"github.com/things-go/go-socks5/statute"
)
// AddressRewriter is used to rewrite a destination transparently
type AddressRewriter interface {
Rewrite(ctx context.Context, request *Request) (context.Context, *statute.AddrSpec)
}
// A Request represents request received by a server
type Request struct {
statute.Request
// AuthContext provided during negotiation
AuthContext *AuthContext
// LocalAddr of the network server listen
LocalAddr net.Addr
// RemoteAddr of the network that sent the request
RemoteAddr net.Addr
// DestAddr of the actual destination (might be affected by rewrite)
DestAddr *statute.AddrSpec
// Reader connect of request
Reader io.Reader
// RawDestAddr of the desired destination
RawDestAddr *statute.AddrSpec
}
// ParseRequest creates a new Request from the tcp connection
func ParseRequest(bufConn io.Reader) (*Request, error) {
hd, err := statute.ParseRequest(bufConn)
if err != nil {
return nil, err
}
return &Request{
Request: hd,
RawDestAddr: &hd.DstAddr,
Reader: bufConn,
}, nil
}
// handleRequest is used for request processing after authentication
func (sf *Server) handleRequest(write io.Writer, req *Request) error {
var err error
ctx := context.Background()
// Resolve the address if we have a FQDN
dest := req.RawDestAddr
if dest.FQDN != "" {
ctx, dest.IP, err = sf.resolver.Resolve(ctx, dest.FQDN)
if err != nil {
if err := SendReply(write, statute.RepHostUnreachable, nil); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
return fmt.Errorf("failed to resolve destination[%v], %v", dest.FQDN, err)
}
}
// Apply any address rewrites
req.DestAddr = req.RawDestAddr
if sf.rewriter != nil {
ctx, req.DestAddr = sf.rewriter.Rewrite(ctx, req)
}
// Check if this is allowed
var ok bool
ctx, ok = sf.rules.Allow(ctx, req)
if !ok {
if err := SendReply(write, statute.RepRuleFailure, nil); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
return fmt.Errorf("bind to %v blocked by rules", req.RawDestAddr)
}
var last Handler
// Switch on the command
switch req.Command {
case statute.CommandConnect:
last = sf.handleConnect
if sf.userConnectHandle != nil {
last = sf.userConnectHandle
}
if len(sf.userConnectMiddlewares) != 0 {
return sf.userConnectMiddlewares.Execute(ctx, write, req, last)
}
case statute.CommandBind:
last = sf.handleBind
if sf.userBindHandle != nil {
last = sf.userBindHandle
}
if len(sf.userBindMiddlewares) != 0 {
return sf.userBindMiddlewares.Execute(ctx, write, req, last)
}
case statute.CommandAssociate:
last = sf.handleAssociate
if sf.userAssociateHandle != nil {
last = sf.userAssociateHandle
}
if len(sf.userAssociateMiddlewares) != 0 {
return sf.userAssociateMiddlewares.Execute(ctx, write, req, last)
}
default:
if err := SendReply(write, statute.RepCommandNotSupported, nil); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
return fmt.Errorf("unsupported command[%v]", req.Command)
}
return last(ctx, write, req)
}
// handleConnect is used to handle a connect command
func (sf *Server) handleConnect(ctx context.Context, writer io.Writer, request *Request) error {
// Attempt to connect
var target net.Conn
var err error
if sf.dialWithRequest != nil {
target, err = sf.dialWithRequest(ctx, "tcp", request.DestAddr.String(), request)
} else {
dial := sf.dial
if dial == nil {
dial = func(ctx context.Context, net_, addr string) (net.Conn, error) {
return net.Dial(net_, addr)
}
}
target, err = dial(ctx, "tcp", request.DestAddr.String())
}
if err != nil {
msg := err.Error()
resp := statute.RepHostUnreachable
if strings.Contains(msg, "refused") {
resp = statute.RepConnectionRefused
} else if strings.Contains(msg, "network is unreachable") {
resp = statute.RepNetworkUnreachable
}
if err := SendReply(writer, resp, nil); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
return fmt.Errorf("connect to %v failed, %v", request.RawDestAddr, err)
}
defer target.Close()
// Send success
if err := SendReply(writer, statute.RepSuccess, target.LocalAddr()); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
// Start proxying
errCh := make(chan error, 2)
sf.goFunc(func() { errCh <- sf.Proxy(target, request.Reader) })
sf.goFunc(func() { errCh <- sf.Proxy(writer, target) })
// Wait
for i := 0; i < 2; i++ {
e := <-errCh
if e != nil {
// return from this function closes target (and conn).
return e
}
}
return nil
}
// handleBind is used to handle a connect command
func (sf *Server) handleBind(_ context.Context, writer io.Writer, _ *Request) error {
// TODO: Support bind
if err := SendReply(writer, statute.RepCommandNotSupported, nil); err != nil {
return fmt.Errorf("failed to send reply: %v", err)
}
return nil
}
// handleAssociate is used to handle a connect command
func (sf *Server) handleAssociate(ctx context.Context, writer io.Writer, request *Request) error {
// Attempt to connect
dial := sf.dial
if dial == nil {
dial = func(_ context.Context, net_, addr string) (net.Conn, error) {
return net.Dial(net_, addr)
}
}
bindLn, err := net.ListenUDP("udp", nil)
if err != nil {
if err := SendReply(writer, statute.RepServerFailure, nil); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
return fmt.Errorf("listen udp failed, %v", err)
}
sf.logger.Errorf("client want to used addr %v, listen addr: %s", request.DestAddr, bindLn.LocalAddr())
// send BND.ADDR and BND.PORT, client used
if err = SendReply(writer, statute.RepSuccess, bindLn.LocalAddr()); err != nil {
return fmt.Errorf("failed to send reply, %v", err)
}
sf.goFunc(func() {
// read from client and write to remote server
conns := sync.Map{}
bufPool := sf.bufferPool.Get()
defer func() {
sf.bufferPool.Put(bufPool)
bindLn.Close()
conns.Range(func(key, value any) bool {
if connTarget, ok := value.(net.Conn); !ok {
sf.logger.Errorf("conns has illegal item %v:%v", key, value)
} else {
connTarget.Close()
}
return true
})
}()
for {
n, srcAddr, err := bindLn.ReadFromUDP(bufPool[:cap(bufPool)])
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
return
}
continue
}
pk, err := statute.ParseDatagram(bufPool[:n])
if err != nil {
continue
}
// check src addr whether equal requst.DestAddr
srcEqual := ((request.DestAddr.IP.IsUnspecified()) || request.DestAddr.IP.Equal(srcAddr.IP)) && (request.DestAddr.Port == 0 || request.DestAddr.Port == srcAddr.Port) //nolint:lll
if !srcEqual {
continue
}
connKey := srcAddr.String() + "--" + pk.DstAddr.String()
if target, ok := conns.Load(connKey); !ok {
// if the 'connection' doesn't exist, create one and store it
targetNew, err := dial(ctx, "udp", pk.DstAddr.String())
if err != nil {
sf.logger.Errorf("connect to %v failed, %v", pk.DstAddr, err)
// TODO:continue or return Error?
continue
}
conns.Store(connKey, targetNew)
// read from remote server and write to original client
sf.goFunc(func() {
bufPool := sf.bufferPool.Get()
defer func() {
targetNew.Close()
conns.Delete(connKey)
sf.bufferPool.Put(bufPool)
}()
for {
buf := bufPool[:cap(bufPool)]
n, err := targetNew.Read(buf)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
return
}
sf.logger.Errorf("read data from remote %s failed, %v", targetNew.RemoteAddr().String(), err)
return
}
tmpBufPool := sf.bufferPool.Get()
proBuf := tmpBufPool
proBuf = append(proBuf, pk.Header()...)
proBuf = append(proBuf, buf[:n]...)
if _, err := bindLn.WriteTo(proBuf, srcAddr); err != nil {
sf.bufferPool.Put(tmpBufPool)
sf.logger.Errorf("write data to client %s failed, %v", srcAddr, err)
return
}
sf.bufferPool.Put(tmpBufPool)
}
})
if _, err := targetNew.Write(pk.Data); err != nil {
sf.logger.Errorf("write data to remote server %s failed, %v", targetNew.RemoteAddr().String(), err)
return
}
} else {
if _, err := target.(net.Conn).Write(pk.Data); err != nil {
sf.logger.Errorf("write data to remote server %s failed, %v", target.(net.Conn).RemoteAddr().String(), err)
return
}
}
}
})
buf := sf.bufferPool.Get()
defer sf.bufferPool.Put(buf)
for {
_, err := request.Reader.Read(buf[:cap(buf)])
// sf.logger.Errorf("read data from client %s, %d bytesm, err is %+v", request.RemoteAddr.String(), num, err)
if err != nil {
bindLn.Close()
if errors.Is(err, io.EOF) || errors.Is(err, net.ErrClosed) {
return nil
}
return err
}
}
}
// SendReply is used to send a reply message
// rep: reply status see statute's statute file
func SendReply(w io.Writer, rep uint8, bindAddr net.Addr) error {
rsp := statute.Reply{
Version: statute.VersionSocks5,
Response: rep,
BndAddr: statute.AddrSpec{
AddrType: statute.ATYPIPv4,
IP: net.IPv4zero,
Port: 0,
},
}
if rsp.Response == statute.RepSuccess {
if tcpAddr, ok := bindAddr.(*net.TCPAddr); ok && tcpAddr != nil {
rsp.BndAddr.IP = tcpAddr.IP
rsp.BndAddr.Port = tcpAddr.Port
} else if udpAddr, ok := bindAddr.(*net.UDPAddr); ok && udpAddr != nil {
rsp.BndAddr.IP = udpAddr.IP
rsp.BndAddr.Port = udpAddr.Port
} else {
rsp.Response = statute.RepAddrTypeNotSupported
}
if rsp.BndAddr.IP.To4() != nil {
rsp.BndAddr.AddrType = statute.ATYPIPv4
} else if rsp.BndAddr.IP.To16() != nil {
rsp.BndAddr.AddrType = statute.ATYPIPv6
}
}
// Send the message
_, err := w.Write(rsp.Bytes())
return err
}
type closeWriter interface {
CloseWrite() error
}
// Proxy is used to suffle data from src to destination, and sends errors
// down a dedicated channel
func (sf *Server) Proxy(dst io.Writer, src io.Reader) error {
buf := sf.bufferPool.Get()
defer sf.bufferPool.Put(buf)
_, err := io.CopyBuffer(dst, src, buf[:cap(buf)])
if tcpConn, ok := dst.(closeWriter); ok {
tcpConn.CloseWrite() //nolint: errcheck
}
return err
}