forked from onrik/SOCKSProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_socks5.go
197 lines (182 loc) · 4.48 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
package proxy
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"syscall"
)
type socks5Conn struct {
localConn net.Conn
conf *SOCKSConf
}
func (c *socks5Conn) Serve() (err error) {
err = c.handshake()
if err == errAuthMethodNotSupported {
c.localConn.Close()
return
}
if err != nil {
return
}
request, err := readSocks5Request(c.localConn)
if err == errAddressTypeNotSupported {
c.sendReply(request, socks5StatusAddressTypeNotSupported)
return
}
if err != nil {
return
}
if request.version != socks5version {
return errVersionError
}
switch request.command {
case commandConnect:
err = c.handleConnect(request)
case commandUDPAssociate:
err = c.handleUDPAssociate(request)
default:
c.sendReply(request, socks5StatusCommandNotSupported)
return errCommandNotSupported
}
return
}
func (c *socks5Conn) handleConnect(request *socks5Request) (err error) {
c.sendReply(request, socks5StatusSucceeded)
remoteConn, err := c.conf.Dial("tcp", request.Address())
if c.sendReplyWithError(request, err) {
return
}
go io.Copy(c.localConn, remoteConn)
go io.Copy(remoteConn, c.localConn)
return
}
func (c *socks5Conn) handleUDPAssociate(request *socks5Request) (err error) {
fmt.Printf("handleUDPAssociate %s\n", request.Address())
c.sendUDPReply(request)
remoteConn, err := c.conf.Dial("udp", request.Address())
if c.sendReplyWithError(request, err) {
fmt.Printf("handleUDPAssociate error %s\n", err.Error())
return err
}
// go io.Copy(c.localConn, remoteConn)
// go io.Copy(remoteConn, c.localConn)
go copy(c.localConn, remoteConn)
go copy(remoteConn, c.localConn)
return
}
func (c *socks5Conn) handshake() (err error) {
reader := bufio.NewReader(c.localConn)
method, err := reader.ReadByte()
if err != nil {
return
}
methods := make([]byte, method)
if _, err = reader.Read(methods); err != nil {
return
}
if c.conf.Auth == nil {
c.sendAuthReply(socks5AuthMethodNoRequired)
return
}
if err = c.authBasedPassword(methods); err != nil {
return
}
return
}
func (c *socks5Conn) authBasedPassword(methods []byte) (err error) {
method := socks5AuthMethodPassword
if c.isTLS() {
method = socks5AuthMethodTLSPassword
}
if !bytes.Contains(methods, []byte{method}) {
c.sendAuthReply(socks5AuthMethodNoAcceptable)
return errAuthMethodNotSupported
}
c.sendAuthReply(method)
reader := bufio.NewReader(c.localConn)
version, err := reader.ReadByte()
if version != 0x01 {
return errAuthMethodNotSupported
}
usernameLength, err := reader.ReadByte()
if err != nil {
return
}
username := make([]byte, usernameLength)
if _, err = reader.Read(username); err != nil {
return
}
passwordLength, err := reader.ReadByte()
if err != nil {
return
}
password := make([]byte, passwordLength)
if _, err = reader.Read(password); err != nil {
return
}
if c.conf.Auth(string(username), string(password)) {
c.localConn.Write([]byte{0x01, 0x01})
} else {
c.localConn.Write([]byte{0x01, 0x00})
}
return
}
func (c *socks5Conn) sendReply(request *socks5Request, status byte) {
reply := []byte{socks5version, status, 0x00}
hostName, hostPort, _ := splitHostPort(c.localConn.LocalAddr().String())
ip := net.ParseIP(string(hostName))
addrType := socks5AddressTypeIPv4
if ip.To16() != nil {
addrType = socks5AddressTypeIPv6
}
reply = append(reply, addrType)
reply = append(reply, ip...)
reply = append(reply, hostPort...)
c.localConn.Write(reply)
if status != socks5StatusSucceeded {
c.localConn.Close()
}
}
func (c *socks5Conn) sendReplyWithError(request *socks5Request, err error) bool {
if err == nil {
return false
}
switch err := err.(type) {
case net.Error:
if err.Timeout() {
c.sendReply(request, socks5StatusHostUnreachable)
}
case *net.OpError:
switch err.Op {
case "dial":
c.sendReply(request, socks5StatusHostUnreachable)
case "read":
c.sendReply(request, socks5StatusConnectionRefused)
}
case syscall.Errno:
switch err {
case syscall.ECONNREFUSED:
c.sendReply(request, socks5StatusConnectionRefused)
}
default:
c.sendReply(request, socks5StatusGeneral)
}
return true
}
func (c *socks5Conn) sendAuthReply(status byte) {
c.localConn.Write([]byte{socks5version, status})
}
func (c *socks5Conn) sendUDPReply(request *socks5Request) {
reply := []byte{0, 0}
fragment := byte(0)
reply = append(reply, fragment)
reply = append(reply, request.addrType)
reply = append(reply, request.addr...)
reply = append(reply, request.port...)
c.localConn.Write(reply)
}
func (c *socks5Conn) isTLS() bool {
return c.conf.TLSConfig != nil
}