/
socks5.go
200 lines (183 loc) · 4.57 KB
/
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
package dialer
import (
"fmt"
"io"
"net"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/Centny/gwf/log"
"github.com/Centny/gwf/util"
)
type CodeError struct {
Inner error
ByteCode byte
}
func (c *CodeError) Code() byte {
return c.ByteCode
}
func (c *CodeError) Error() string {
return c.Inner.Error()
}
//SocksProxyAddressPooler is an interface to handler proxy server address get/set
type SocksProxyAddressPooler interface {
//Get will return the proxy server address
Get(uri string) (address string, err error)
//Done will mark one address is free
Done(address, uri string, err error)
}
//StringAddressPooler is an implementation of the SocksProxyAddressPooler interface for one string address.
type StringAddressPooler string
//Get will return the proxy server address
func (s StringAddressPooler) Get(uri string) (address string, err error) {
address = string(s)
return
}
//Done will mark one address is fress
func (s StringAddressPooler) Done(address, uri string, err error) {
return
}
//SocksProxyDialer is an implementation of the Dialer interface for dial by socks proxy.
type SocksProxyDialer struct {
ID string
Pooler SocksProxyAddressPooler
matcher *regexp.Regexp
conf util.Map
}
//NewSocksProxyDialer will return new SocksProxyDialer
func NewSocksProxyDialer() *SocksProxyDialer {
return &SocksProxyDialer{
matcher: regexp.MustCompile("^.*:[0-9]+$"),
conf: util.Map{},
}
}
//Name will return dialer name
func (s *SocksProxyDialer) Name() string {
return s.ID
}
//Bootstrap the dialer.
func (s *SocksProxyDialer) Bootstrap(options util.Map) (err error) {
s.ID = options.StrVal("id")
if len(s.ID) < 1 {
return fmt.Errorf("the dialer id is required")
}
if options != nil {
s.Pooler = StringAddressPooler(options.StrVal("address"))
}
s.conf = options
matcher := options.StrVal("matcher")
if len(matcher) > 0 {
s.matcher, err = regexp.Compile(matcher)
}
return nil
}
func (s *SocksProxyDialer) Options() util.Map {
return s.conf
}
//Matched will return whether the uri is invalid tcp uri.
func (s *SocksProxyDialer) Matched(uri string) bool {
remote, err := url.Parse(uri)
return err == nil && s.matcher.MatchString(remote.Host)
}
//Dial one connection by uri
func (s *SocksProxyDialer) Dial(sid uint64, uri string, pipe io.ReadWriteCloser) (raw Conn, err error) {
remote, err := url.Parse(uri)
if err != nil {
return
}
parts := strings.SplitN(remote.Host, ":", 2)
if len(parts) < 2 {
err = fmt.Errorf("not supported address:%v", remote.Host)
return
}
host := parts[0]
port, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
err = fmt.Errorf("parse address:%v error:%v", remote.Host, err)
return
}
address, err := s.Pooler.Get(uri)
if err != nil {
return
}
var doneErr error
defer s.Pooler.Done(address, uri, doneErr)
log.D("SocksProxyDialer dial to %v", address)
conn, err := net.Dial("tcp", address)
if err != nil {
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
return
}
_, err = conn.Write([]byte{0x05, 0x01, 0x00})
if err != nil {
conn.Close()
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
return
}
buf := make([]byte, 1024*64)
err = fullBuf(conn, buf, 2, nil)
if err != nil {
conn.Close()
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
return
}
if buf[0] != 0x05 || buf[1] != 0x00 {
err = fmt.Errorf("unsupported %x", buf)
conn.Close()
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
return
}
blen := len(host) + 7
buf[0], buf[1], buf[2] = 0x05, 0x01, 0x00
buf[3], buf[4] = 0x03, byte(len(host))
copy(buf[5:], []byte(host))
buf[blen-2] = byte(port / 256)
buf[blen-1] = byte(port % 256)
_, err = conn.Write(buf[:blen])
if err != nil {
conn.Close()
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
return
}
err = fullBuf(conn, buf, 5, nil)
if err != nil {
conn.Close()
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
return
}
switch buf[3] {
case 0x01:
err = fullBuf(conn, buf[5:], 5, nil)
case 0x03:
err = fullBuf(conn, buf[5:], uint32(buf[4])+2, nil)
case 0x04:
err = fullBuf(conn, buf[5:], 17, nil)
default:
err = fmt.Errorf("reply address type is not supported:%v", buf[3])
}
if err != nil {
conn.Close()
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
return
}
if buf[1] != 0x00 {
conn.Close()
err = fmt.Errorf("response code(%x)", buf[1])
if buf[1] >= 0x10 {
doneErr = &CodeError{Inner: err, ByteCode: 0x10}
}
return
}
raw = NewCopyPipable(conn)
if pipe != nil {
err = raw.Pipe(pipe)
}
if err != nil {
conn.Close()
}
return
}
func (s *SocksProxyDialer) String() string {
return fmt.Sprintf("SocksProxyDialer-%v", s.ID)
}