-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathconn_socks5.go
234 lines (193 loc) · 4.97 KB
/
conn_socks5.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
package socks5
import (
"fmt"
"io"
"net"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
)
type Stream interface {
RemoteAddr() net.Addr
LocalAddr() net.Addr
io.ReadWriteCloser
SetReadDeadline(t time.Time) error
}
type Socks5Conn struct {
conn Stream
cfg ConnCfg
customDialTarget func(addr string) (Stream, byte, string, error)
}
func (p *Socks5Conn) SetCustomDialTarget(f func(addr string) (Stream, byte, string, error)) {
p.customDialTarget = f
}
func (p *Socks5Conn) Handle() error {
method, err := p.selectAuthMethod()
if err != nil {
return fmt.Errorf("selectAuthMethod:%w", err)
}
err = p.checkAuthMethod(method)
if err != nil {
return fmt.Errorf("checkAuthMethod:%w", err)
}
req, err := p.readRequest()
if err != nil {
return fmt.Errorf("failed to readRequest:%w", err)
}
return p.handleRequest(req)
}
func (p *Socks5Conn) selectAuthMethod() (byte, error) {
req, err := NewMethodSelectReqFrom(p.conn)
if err != nil {
return 0, fmt.Errorf("NewMethodSelectReqFrom:%w", err)
}
if req.Ver != VerSocks5 {
return 0, ErrSocksVersion
}
var method byte = MethodNone
if p.cfg.UserName != "" && p.cfg.Password != "" {
method = MethodUserPass
}
var exist bool
for _, v := range req.Methods {
if method == v {
exist = true
break
}
}
if !exist {
method = MethodNoAcceptable
}
_, err = p.conn.Write(NewMethodSelectReply(method).ToBytes())
if err != nil {
return 0, fmt.Errorf("reply:%w", err)
}
if method == MethodNoAcceptable {
return 0, ErrMethodNoAcceptable
}
return method, nil
}
func (p *Socks5Conn) checkAuthMethod(method byte) error {
switch method {
case MethodNone:
return nil
case MethodUserPass:
req, err := NewUserPassAuthReqFrom(p.conn)
if err != nil {
return fmt.Errorf("NewUserPassAuthReqFrom:%w", err)
}
if req.Ver != VerAuthUserPass {
return ErrAuthUserPassVer
}
var status byte = AuthStatusFailure
if string(req.UserName) == p.cfg.UserName && string(req.Password) == p.cfg.Password {
status = AuthStatusSuccess
}
_, err = p.conn.Write(NewUserPassAuthReply(status).ToBytes())
if err != nil {
return fmt.Errorf("reply:%w", err)
}
if status != AuthStatusSuccess {
return ErrAuthFailed
}
return nil
default:
return ErrMethod
}
}
// TODO suport bind
func (p *Socks5Conn) handleRequest(req *Request) error {
switch req.Cmd {
case CmdConnect:
return p.handleConnect(req)
case CmdUDP:
return p.handleUDP(req)
default:
p.conn.Write(NewReply(RepCmdNotSupported, nil).ToBytes())
return ErrCmdNotSupport
}
}
func (p *Socks5Conn) handleUDP(req *Request) error {
addrAdv := p.getUDPAdvAddr()
bAddr, err := NewAddrByteFromString(addrAdv)
if err != nil {
p.conn.Write(NewReply(RepServerFailure, nil).ToBytes())
return err
}
_, err = p.conn.Write(NewReply(RepSuccess, bAddr).ToBytes())
if err != nil {
return err
}
buf := make([]byte, 32)
for {
//p.conn.SetDeadline(time.Time{})
_, err := p.conn.Read(buf)
if err != nil {
return err
}
}
return nil
}
func (p *Socks5Conn) getUDPAdvAddr() string {
port := p.cfg.UDPAdvertisedPort
//docker等环境中获取不了本机正确ip,这时需要从事先设置的配置或环境变量中获取
if len(p.cfg.UDPAdvertisedIP) > 0 {
return net.JoinHostPort(p.cfg.UDPAdvertisedIP, strconv.FormatInt(int64(port), 10))
}
localAddr := p.conn.LocalAddr().(*net.TCPAddr)
addr := net.UDPAddr{
IP: localAddr.IP,
Zone: localAddr.Zone,
Port: port,
}
return addr.String()
}
func (p *Socks5Conn) handleConnect(req *Request) error {
addr := req.Address()
logrus.Debug("tcp req:", addr)
s, rep, bindAddr, err := p.dialTarget(addr)
if err != nil {
p.conn.Write(NewReply(rep, nil).ToBytes())
return fmt.Errorf("failed to dialTarget(%s):%w", addr, err)
}
defer s.Close()
bAddr, err := NewAddrByteFromString(bindAddr)
if err != nil {
p.conn.Write(NewReply(RepServerFailure, nil).ToBytes())
return fmt.Errorf("NewAddrByteFromString:%w", err)
}
_, err = p.conn.Write(NewReply(RepSuccess, bAddr).ToBytes())
if err != nil {
return fmt.Errorf("faied to write reply:%w", err)
}
timeout := time.Duration(p.cfg.TCPTimeout) * time.Second
return Pipe(p.conn, s, timeout)
}
func (p *Socks5Conn) dialTarget(addr string) (Stream, byte, string, error) {
if p.customDialTarget != nil {
return p.customDialTarget(addr)
}
return p.defaultDialTarget(addr)
}
func (p *Socks5Conn) defaultDialTarget(addr string) (Stream, byte, string, error) {
s, err := net.DialTimeout("tcp", addr, time.Second*3)
if err != nil {
msg := err.Error()
var rep byte = RepHostUnreachable
if strings.Contains(msg, "refused") {
rep = RepConnectionRefused
} else if strings.Contains(msg, "network is unreachable") {
rep = RepNetworkUnreachable
}
return nil, rep, "", err
}
return s, RepSuccess, s.LocalAddr().(*net.TCPAddr).String(), nil
}
func (p *Socks5Conn) readRequest() (*Request, error) {
req, err := NewRequestFrom(p.conn)
if err != nil {
return nil, fmt.Errorf("failed to NewRequestFrom:%w", err)
}
return req, nil
}