-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
socks.go
202 lines (175 loc) · 5.13 KB
/
socks.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
package socks
// https://tools.ietf.org/html/rfc1928
import (
"context"
"fmt"
"io"
"net"
"sync"
)
func (p *Proxy) handle(ctx context.Context, conn net.Conn) {
defer conn.Close()
defer func() {
p.Log.Debug("client connection closed")
}()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
p.Log.Debugf("got connection from %s", conn.RemoteAddr().String())
if err := p.socks(ctx, conn); err != nil {
// send error reply
p.Log.Errorf("socks error: %v", err.Err)
if err := p.socksErrorReply(ctx, conn, err.Reason); err != nil {
p.Log.Error(err)
return
}
}
}
func (p *Proxy) socks(ctx context.Context, conn net.Conn) *Error {
if err := p.handleConnect(ctx, conn); err != nil {
return err
}
request, err := p.handleRequest(ctx, conn)
if err != nil {
return err
}
p.Log.Infof("Connecting to %s", request.GetDestinationString())
// Should we assume connection succeed here?
ctx, remote, err := p.Proxyhandler.Init(ctx, *request)
if err != nil {
return err
}
defer func() {
if err := p.Proxyhandler.Close(ctx); err != nil {
p.Log.Errorf("error on close: %v", err)
}
}()
defer remote.Close()
p.Log.Infof("Connection established %s - %s", conn.RemoteAddr().String(), request.GetDestinationString())
err = p.handleRequestReply(ctx, conn, request)
if err != nil {
return err
}
p.Log.Debug("beginning of data copy")
wg := &sync.WaitGroup{}
errChannel1 := make(chan error, 1)
errChannel2 := make(chan error, 1)
ctx2, cancel := context.WithCancel(ctx)
defer cancel()
wg.Add(2)
go p.copyClientToRemote(ctx2, conn, remote, wg, errChannel1)
go p.copyRemoteToClient(ctx2, remote, conn, wg, errChannel2)
go p.Proxyhandler.Refresh(ctx2)
p.Log.Debug("waiting for copy to finish")
wg.Wait()
// stop refreshing the connection
cancel()
if err := <-errChannel1; err != nil {
return NewError(RequestReplyHostUnreachable, err)
}
if err := <-errChannel2; err != nil {
return NewError(RequestReplyHostUnreachable, err)
}
p.Log.Debug("end of connection handling")
return nil
}
func (p *Proxy) copyClientToRemote(ctx context.Context, client io.ReadCloser, remote io.WriteCloser, wg *sync.WaitGroup, errChannel chan<- error) {
defer wg.Done()
defer close(errChannel)
select {
case <-p.Done:
errChannel <- nil
return
default:
if err := p.Proxyhandler.ReadFromClient(ctx, client, remote); err != nil {
errChannel <- fmt.Errorf("error on copy from Client to Remote: %v", err)
return
}
errChannel <- nil
return
}
}
func (p *Proxy) copyRemoteToClient(ctx context.Context, remote io.ReadCloser, client io.WriteCloser, wg *sync.WaitGroup, errChannel chan<- error) {
defer wg.Done()
defer close(errChannel)
select {
case <-p.Done:
errChannel <- nil
return
default:
if err := p.Proxyhandler.ReadFromRemote(ctx, remote, client); err != nil {
errChannel <- fmt.Errorf("error on copy from Remote to Client: %v", err)
return
}
errChannel <- nil
return
}
}
func (p *Proxy) socksErrorReply(ctx context.Context, conn io.ReadWriteCloser, reason RequestReplyReason) error {
// send error reply
repl, err := requestReply(nil, reason)
if err != nil {
return err
}
err = connectionWrite(ctx, conn, repl, p.Timeout)
if err != nil {
return err
}
return nil
}
func (p *Proxy) handleConnect(ctx context.Context, conn io.ReadWriteCloser) *Error {
buf, err := connectionRead(ctx, conn, p.Timeout)
if err != nil {
return NewError(RequestReplyConnectionRefused, err)
}
header, err := parseHeader(buf)
if err != nil {
return NewError(RequestReplyConnectionRefused, err)
}
switch header.Version {
case Version4:
return NewError(RequestReplyCommandNotSupported, fmt.Errorf("socks4 not yet implemented"))
case Version5:
default:
return NewError(RequestReplyCommandNotSupported, fmt.Errorf("version %#x not yet implemented", byte(header.Version)))
}
methodSupported := false
for _, x := range header.Methods {
if x == MethodNoAuthRequired {
methodSupported = true
break
}
}
if !methodSupported {
return NewError(RequestReplyMethodNotSupported, fmt.Errorf("we currently only support no authentication"))
}
reply := make([]byte, 2)
reply[0] = byte(Version5)
reply[1] = byte(MethodNoAuthRequired)
err = connectionWrite(ctx, conn, reply, p.Timeout)
if err != nil {
return NewError(RequestReplyGeneralFailure, fmt.Errorf("could not send connect reply: %w", err))
}
return nil
}
func (p *Proxy) handleRequest(ctx context.Context, conn io.ReadWriteCloser) (*Request, *Error) {
buf, err := connectionRead(ctx, conn, p.Timeout)
if err != nil {
return nil, NewError(RequestReplyGeneralFailure, fmt.Errorf("error on ConnectionRead: %w", err))
}
request, err2 := parseRequest(buf)
if err2 != nil {
return nil, err2
}
return request, nil
}
func (p *Proxy) handleRequestReply(ctx context.Context, conn io.ReadWriteCloser, request *Request) *Error {
repl, err := requestReply(request, RequestReplySucceeded)
if err != nil {
return NewError(RequestReplyGeneralFailure, fmt.Errorf("error on requestReply: %w", err))
}
err = connectionWrite(ctx, conn, repl, p.Timeout)
if err != nil {
return NewError(RequestReplyGeneralFailure, fmt.Errorf("error on RequestResponse: %w", err))
}
return nil
}