-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtcp_protocol.go
240 lines (218 loc) · 5.76 KB
/
tcp_protocol.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
package main
import (
"bytes"
"encoding/binary"
"errors"
"net"
"strconv"
"time"
)
type AuthHandle func(username, password string) bool
type TCPProtocol struct {
cmd byte
atyp byte
ip net.IP //when viaUDP is false, this is remote IP otherwise UDP client IP
port int //when viaUDP is false, this is remote port otherwise UDP client port
domain string
viaUDP bool
authHandle AuthHandle
}
func (p *TCPProtocol) handshake(conn *net.TCPConn) error {
//version
data, err := p.checkVersion(conn)
p.writeBuf(conn, data)
if err != nil {
return err
}
//auth
data, err = p.checkAuth(conn)
p.writeBuf(conn, data)
if err != nil {
return err
}
//addr
atyp, cmd, addrBytes, port, data, err := p.getAddr(conn)
if err != nil {
p.writeBuf(conn, data)
return err
} else {
p.cmd = cmd
p.atyp = atyp
}
switch p.atyp {
case ATYPIPv4:
p.ip = net.IPv4(addrBytes[0], addrBytes[1], addrBytes[2], addrBytes[3])
case ATYPIPv6:
p.ip = net.ParseIP(string(addrBytes))
case ATYPDomain:
p.domain = string(addrBytes)
if addr, er := net.ResolveIPAddr("ip", p.domain); er != nil {
p.writeBuf(conn, []byte{Version, 0x04, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
return err
} else {
p.ip = addr.IP
}
}
p.port = port
//check remote addr
switch p.cmd {
case CmdConnect:
if !p.ip.IsGlobalUnicast() || p.port <= 0 {
p.writeBuf(conn, []byte{Version, 0x02, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
return errors.New("remote address error")
}
case CmdUdpAssociate:
p.viaUDP = true
}
return nil
}
func (p *TCPProtocol) checkVersion(conn *net.TCPConn) ([]byte, error) {
var version byte
var methodLen int
if buf, err := p.readBuf(conn, 2); err != nil {
return []byte{Version, MethodNone}, err
} else {
version = buf[0]
methodLen = int(buf[1])
}
if version != Version || methodLen <= 0 {
return []byte{Version, MethodNone}, errors.New("unsupported socks version")
}
if _, err := p.readBuf(conn, methodLen); err != nil {
return []byte{Version, MethodNone}, err
}
if p.authHandle != nil {
return []byte{Version, MethodAuth}, nil
} else {
return []byte{Version, MethodNoAuth}, nil
}
}
func (p *TCPProtocol) checkAuth(conn *net.TCPConn) ([]byte, error) {
if p.authHandle == nil {
return nil, nil
}
var ver byte
var userLen, passLen int
var username, password string
if buf, err := p.readBuf(conn, 2); err != nil {
return []byte{0x01, 0x01}, err
} else {
ver = buf[0]
userLen = int(buf[1])
}
if ver != 0x01 || userLen <= 0 {
return []byte{0x01, 0x01}, errors.New("unsupported auth version or username is empty")
}
if buf, err := p.readBuf(conn, userLen); err != nil {
return []byte{0x01, 0x01}, err
} else {
username = string(buf)
}
if buf, err := p.readBuf(conn, 1); err != nil {
return []byte{0x01, 0x01}, err
} else {
passLen = int(buf[0])
}
if passLen <= 0 {
return []byte{0x01, 0x01}, errors.New("password is empty")
}
if buf, err := p.readBuf(conn, passLen); err != nil {
return []byte{0x01, 0x01}, err
} else {
password = string(buf)
}
if !p.authHandle(username, password) {
return []byte{0x01, 0x01}, errors.New("username or password invalid")
} else {
return []byte{0x01, 0x00}, nil
}
}
func (p *TCPProtocol) getAddr(conn *net.TCPConn) (atyp, cmd byte, addrBytes []byte, port int, data []byte, err error) {
var ver byte
if buf, er := p.readBuf(conn, 4); er != nil {
err = er
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
} else {
ver = buf[0]
cmd = buf[1]
atyp = buf[3]
}
if ver != Version {
err = errors.New("unsupported socks version")
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
}
if bytes.IndexByte([]byte{CmdConnect, CmdUdpAssociate}, cmd) == -1 {
err = errors.New("unsupported CMD")
data = []byte{Version, 0x07, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
}
switch atyp {
case ATYPIPv4:
addrBytes, err = p.readBuf(conn, 4)
if err != nil {
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
}
case ATYPDomain:
var domainLen int
if buf, er := p.readBuf(conn, 1); er != nil {
err = er
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
} else {
domainLen = int(buf[0])
}
if domainLen <= 0 {
err = errors.New("length of domain is zero")
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
}
addrBytes, err = p.readBuf(conn, domainLen)
if err != nil {
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
}
case ATYPIPv6:
addrBytes, err = p.readBuf(conn, 16)
if err != nil {
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
}
default:
err = errors.New("unsupported ATYP")
data = []byte{Version, 0x08, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
}
if buf, er := p.readBuf(conn, 2); er != nil {
err = er
data = []byte{Version, 0x05, 0x00, ATYPIPv4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
return
} else {
port = int(binary.BigEndian.Uint16(buf))
}
return
}
func (p *TCPProtocol) networkString() string {
return p.ip.String() + ":" + strconv.Itoa(p.port)
}
func (p *TCPProtocol) readBuf(conn *net.TCPConn, ln int) ([]byte, error) {
buf := make([]byte, ln)
curReadLen := 0
for curReadLen < ln {
_ = conn.SetReadDeadline(time.Now().Add(time.Second * 10))
l, err := conn.Read(buf[curReadLen:])
if err != nil {
return nil, err
}
curReadLen += l
}
return buf, nil
}
func (p *TCPProtocol) writeBuf(conn *net.TCPConn, data []byte) {
if data != nil && len(data) > 0 {
_ = conn.SetWriteDeadline(time.Now().Add(time.Second * 10))
_, _ = conn.Write(data)
}
}