-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
defaulthandler.go
117 lines (100 loc) · 2.94 KB
/
defaulthandler.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
package socks
import (
"context"
"errors"
"fmt"
"io"
"net"
"time"
)
// DefaultHandler is the default socks5 implementation
type DefaultHandler struct {
// Timeout defines the connect timeout to the destination
Timeout time.Duration
Log Logger
}
// Init is the default socks5 implementation
func (s DefaultHandler) Init(ctx context.Context, request Request) (context.Context, io.ReadWriteCloser, *Error) {
target := request.GetDestinationString()
if s.Log != nil {
s.Log.Infof("Connecting to target %s", target)
}
remote, err := net.DialTimeout("tcp", target, s.Timeout)
if err != nil {
return ctx, nil, NewError(RequestReplyHostUnreachable, fmt.Errorf("error on connecting to server: %w", err))
}
return ctx, remote, nil
}
const bufferSize = 10240
type readDeadline interface {
SetReadDeadline(time.Time) error
}
type writeDeadline interface {
SetWriteDeadline(time.Time) error
}
// ReadFromClient is the default socks5 implementation
func (s DefaultHandler) ReadFromClient(ctx context.Context, client io.ReadCloser, remote io.WriteCloser) error {
timeOut := time.Now().Add(s.Timeout)
ctx, cancel := context.WithDeadline(ctx, timeOut)
defer cancel()
if c, ok := remote.(writeDeadline); ok {
if err := c.SetWriteDeadline(timeOut); err != nil {
return fmt.Errorf("could not set write deadline on remote: %v", err)
}
}
if c, ok := client.(readDeadline); ok {
if err := c.SetReadDeadline(timeOut); err != nil {
return fmt.Errorf("could not set read deadline on client: %v", err)
}
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
i, err := io.CopyN(remote, client, bufferSize)
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return fmt.Errorf("ReadFromClient: %w", err)
}
s.Log.Debugf("[socks] wrote %d bytes to remote", i)
}
}
}
// ReadFromRemote is the default socks5 implementation
func (s DefaultHandler) ReadFromRemote(ctx context.Context, remote io.ReadCloser, client io.WriteCloser) error {
timeOut := time.Now().Add(s.Timeout)
ctx, cancel := context.WithDeadline(ctx, timeOut)
defer cancel()
if c, ok := client.(writeDeadline); ok {
if err := c.SetWriteDeadline(timeOut); err != nil {
return fmt.Errorf("could not set write deadline on client: %v", err)
}
}
if c, ok := remote.(readDeadline); ok {
if err := c.SetReadDeadline(timeOut); err != nil {
return fmt.Errorf("could not set read deadline on remote: %v", err)
}
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
i, err := io.CopyN(client, remote, bufferSize)
if errors.Is(err, io.EOF) {
return nil
} else if err != nil {
return fmt.Errorf("ReadFromRemote: %w", err)
}
s.Log.Debugf("[socks] wrote %d bytes to client", i)
}
}
}
// Close is the default socks5 implementation
func (s DefaultHandler) Close(ctx context.Context) error {
return nil
}
// Refresh is the default socks5 implementation
func (s DefaultHandler) Refresh(ctx context.Context) {}