forked from sipt/shuttle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocks5.go
More file actions
158 lines (148 loc) · 4.07 KB
/
Copy pathsocks5.go
File metadata and controls
158 lines (148 loc) · 4.07 KB
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
package shuttle
import (
"net"
"errors"
"github.com/sipt/shuttle/pool"
"encoding/binary"
"time"
)
const (
socksVer5 = 0x05
verIndex = 0
nMethodIndex = 1
methodIndex = 2
cmdIndex = 1
rsvIndex = cmdIndex + 1
atypIndex = rsvIndex + 1
addrIndex = atypIndex + 1
domianLenIndex = atypIndex + 1
ipv4PortIndex = addrIndex + 4
ipv6PortIndex = addrIndex + 16
)
func SocksHandle(co net.Conn) {
Logger.Debug("start shuttle.IConn wrap net.Con")
conn, err := NewDefaultConn(co, TCP)
if err != nil {
Logger.Errorf("shuttle.IConn wrap net.Conn failed: %v", err)
return
}
Logger.Debugf("shuttle.IConn wrap net.Con success [ID:%d]", conn.GetID())
Logger.Debugf("[ID:%d] start handShake", conn.GetID())
err = handShake(conn)
if err != nil {
Logger.Errorf("[%d] handShake failed: %v", conn.GetID(), err)
return
}
req, err := parseRequest(conn)
if err != nil {
Logger.Error("parseRequest failed: ", err)
return
}
req.Protocol = ProtocolSocks
req.Target = req.Host()
_, err = conn.Write([]byte{socksVer5, 0x00, 0x00, AddrTypeIPv4, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
if err != nil {
Logger.Error("send connection confirmation:", err)
return
}
//filter by Rules and DNS
rule, s, err := FilterByReq(req)
if err != nil {
Logger.Error("ConnectToServer failed [", req.Host(), "] err: ", err)
}
//connnet to server
sc, err := s.Conn(req)
if err != nil {
if err == ErrorReject {
Logger.Debugf("Reject [%s]", req.Target)
} else {
Logger.Error("ConnectToServer failed [", req.Host(), "] err: ", err)
}
return
}
recordChan <- &Record{
ID: sc.GetID(),
Protocol: req.Protocol,
Created: time.Now(),
Proxy: s,
Status: RecordStatusActive,
URL: req.Target,
Rule: rule,
}
direct := &DirectChannel{}
direct.Transport(conn, sc)
}
//socks 握手
func handShake(conn net.Conn) error {
buf := pool.GetBuf()
_, err := conn.Read(buf)
if err != nil {
return err
}
if buf[verIndex] != socksVer5 {
return errors.New("socks version not supported")
}
//todo get methods
//return supported methods
conn.Write([]byte{0x05, 0x00})
pool.PutBuf(buf)
return nil
}
//获取协议
func parseRequest(conn IConn) (*Request, error) {
//+----+-----+-------+------+----------+----------+
//|VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
//+----+-----+-------+------+----------+----------+
//| 1 | 1 | 1 | 1 | Variable | 2 |
//+----+-----+-------+------+----------+----------+
//CMD 字段:command 的缩写,shadowsocks 只用到了:
// 0x01:建立 TCP 连接
// 0x03:关联 UDP 请求
//RSV 字段:保留字段,值为 0x00;
//ATYP 字段:address type 的缩写,取值为:
// 0x01:IPv4
// 0x03:域名
// 0x04:IPv6
//DST.ADDR 字段:destination address 的缩写,取值随 ATYP 变化:
// ATYP == 0x01:4 个字节的 IPv4 地址
// ATYP == 0x03:1 个字节表示域名长度,紧随其后的是对应的域名
// ATYP == 0x04:16 个字节的 IPv6 地址
//DST.PORT 字段:目的服务器的端口。
buf := pool.GetBuf()
_, err := conn.Read(buf)
if err != nil {
return nil, err
}
request := &Request{
Ver: uint8(buf[verIndex]),
Cmd: uint8(buf[cmdIndex]),
Rsv: uint8(buf[rsvIndex]),
Atyp: uint8(buf[atypIndex]),
ConnID: conn.GetID(),
}
switch request.Atyp {
case AddrTypeIPv4:
request.IP = buf[atypIndex+1 : ipv4PortIndex]
request.Port = binary.BigEndian.Uint16(buf[ipv4PortIndex : ipv4PortIndex+2])
if request.Cmd == CmdUDP {
request.Data = buf[ipv4PortIndex+2:]
}
case AddrTypeDomain:
end := buf[domianLenIndex] + 1 + domianLenIndex
request.Addr = string(buf[domianLenIndex+1 : end])
request.Port = binary.BigEndian.Uint16(buf[end : end+2])
if request.Cmd == CmdUDP {
request.Data = buf[end+2:]
}
case AddrTypeIPv6:
request.IP = buf[atypIndex+1 : ipv6PortIndex]
request.Port = binary.BigEndian.Uint16(buf[ipv6PortIndex : ipv6PortIndex+2])
if request.Cmd == CmdUDP {
request.Data = buf[ipv6PortIndex+2:]
}
}
if request.Cmd != CmdUDP {
pool.PutBuf(buf) // 回收
}
return request, nil
}