-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnection.go
198 lines (176 loc) · 4.7 KB
/
connection.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
package go_socks5
import (
"encoding/binary"
"errors"
"fmt"
"log"
"net"
"strconv"
)
type Connection struct {
client *Client
conn net.Conn
}
func (c Connection) writePacket(buf []byte) error {
rc, err := c.conn.Write(buf)
if err != nil {
return err
}
if rc != len(buf) {
return errors.New(fmt.Sprintf("proxy: couldn't write all data: %d/%d", rc, len(buf)))
}
if c.client.Debug {
log.Println("W", buf)
}
return nil
}
func (c Connection) readPacket(buf []byte) (int, error) {
rc, err := c.conn.Read(buf)
if err != nil {
return rc, err
}
if c.client.Debug {
log.Println("R", buf[:rc])
}
return rc, nil
}
func (c Connection) authenticate() error {
//TODO support other method than username/password
buf := make([]byte, MaxProtoSize)
buf[0] = 5 // Socks 5
buf[1] = 1 // 1 Method
buf[2] = 2 // username/password
if err := c.writePacket(buf[0:3]); err != nil {
return err
}
if rc, err := c.readPacket(buf); err != nil {
return err
} else if rc != 2 {
return errors.New(fmt.Sprintf("proxy: unexpected response packet size: %d", rc))
} else if buf[1] == 0xff {
return errors.New("proxy: no acceptable authentication method")
}
username_len := len(c.client.Username)
password_len := len(c.client.Password)
buf[0] = 1
buf[1] = byte(username_len)
copy(buf[2:], c.client.Username)
buf[2+username_len] = byte(password_len)
copy(buf[3+username_len:], c.client.Password)
if err := c.writePacket(buf[0 : 3+username_len+password_len]); err != nil {
return err
}
if rc, err := c.readPacket(buf); err != nil {
return err
} else if rc != 2 {
return errors.New(fmt.Sprintf("proxy: unexpected response packet size: %d", rc))
} else if buf[1] != 0 {
return errors.New(fmt.Sprintf("proxy: authentication failed: status=%x", buf[1]))
}
return nil
}
func (c Connection) bind(laddr *net.TCPAddr) (*net.TCPAddr, error) {
buf := make([]byte, MaxProtoSize)
buf[0] = 5 // Ver
buf[1] = 2 // bind
buf[2] = 0 // Reserved
buf[3] = 1 // IPv4
copy(buf[4:8], laddr.IP.To4())
binary.BigEndian.PutUint16(buf[8:10], uint16(laddr.Port))
//TODO support ipv6, domain
if err := c.writePacket(buf[0:10]); err != nil {
return nil, err
}
if rc, err := c.readPacket(buf); err != nil {
return nil, err
} else if rc != 10 {
return nil, errors.New(fmt.Sprintf("proxy: unexpected response packet size: %d", rc))
} else if buf[1] != 0 {
return nil, errors.New(fmt.Sprintf("proxy: udp associate failed: status=%x", buf[1]))
}
return &net.TCPAddr{
IP: buf[4:8],
Port: int(binary.BigEndian.Uint16(buf[8:10])),
}, nil
}
func (c Connection) udpAssociate(laddr *net.UDPAddr) (*net.UDPAddr, error) {
buf := make([]byte, MaxProtoSize)
buf[0] = 5 // Ver
buf[1] = 3 // UDP Associate
buf[2] = 0 // Reserved
buf[3] = 1 // IPv4
//All blank if local network
for k := 4; k < 10; k++ {
buf[k] = 0
}
//TODO support ipv6, domain
if err := c.writePacket(buf[0:10]); err != nil {
return nil, err
}
if rc, err := c.readPacket(buf); err != nil {
return nil, err
} else if rc != 10 {
return nil, errors.New(fmt.Sprintf("proxy: unexpected response packet size: %d", rc))
} else if buf[1] != 0 {
return nil, errors.New(fmt.Sprintf("proxy: udp associate failed: status=%x", buf[1]))
}
return &net.UDPAddr{
IP: buf[4:8],
Port: int(binary.BigEndian.Uint16(buf[8:10])),
}, nil
}
func (c Connection) connect(address string) (*net.TCPAddr, error) {
host, portStr, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portStr)
if err != nil {
return nil, errors.New("proxy: failed to parse port number: " + portStr)
}
if port < 1 || port > 0xffff {
return nil, errors.New("proxy: port number out of range: " + portStr)
}
buf := make([]byte, MaxProtoSize)
buf[0] = 5 // Ver
buf[1] = 1 // Connect
buf[2] = 0 // Reserved
size := 4
if ip := net.ParseIP(host); ip != nil {
if ip4 := ip.To4(); ip4 != nil {
buf[3] = 1 // Ipv4
size += copy(buf[size:], ip4)
} else {
buf[3] = 4 // Ipv6
size += copy(buf[size:], ip)
}
} else {
host_len := len(host)
if host_len > 255 {
return nil, errors.New("proxy: hostname too long: " + host)
}
buf[3] = 3 // domain
buf[size] = byte(host_len)
size++
size += copy(buf[size:], host)
}
binary.BigEndian.PutUint16(buf[size:], uint16(port))
size += 2
if err := c.writePacket(buf[:size]); err != nil {
return nil, err
}
if rc, err := c.readPacket(buf); err != nil {
return nil, err
} else if rc < 10 {
return nil, errors.New("unexpected response")
} else if buf[1] != 0 {
return nil, errors.New(fmt.Sprintf("connect failed: %d", buf[1]))
}
return &net.TCPAddr{
IP: buf[4:8],
Port: int(binary.LittleEndian.Uint16(buf[8:10])),
}, nil
}
func (c *Connection) close() error {
return c.conn.Close()
}