/
proxy.go
272 lines (235 loc) · 6.56 KB
/
proxy.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package dispather
import (
"encoding/binary"
"errors"
"log"
"net"
"strconv"
"time"
"github.com/Dliv3/Venom/global"
"github.com/Dliv3/Venom/node"
"github.com/Dliv3/Venom/protocol"
"github.com/Dliv3/Venom/utils"
)
var (
Commands = []string{"CONNECT", "BIND", "UDP ASSOCIATE"}
AddrType = []string{"", "IPv4", "", "Domain", "IPv6"}
Conns = make([]net.Conn, 0)
Verbose = false
errAddrType = errors.New("socks addr type not supported")
errVer = errors.New("socks version not supported")
errMethod = errors.New("socks only support noauth method")
errAuthExtraData = errors.New("socks authentication get extra data")
errReqExtraData = errors.New("socks request get extra data")
errCmd = errors.New("socks only support connect command")
)
const (
socksVer5 = 0x05
socksCmdConnect = 0x01
)
func AgentHandShake(peerNode *node.Node, currentSessionID uint16) (err error) {
const (
idVer = 0
idNmethod = 1
)
var n int
buf, err := peerNode.DataBuffers[protocol.SOCKSDATA].GetDataBuffer(currentSessionID).ReadBytes()
if err != nil {
return err
}
n = len(buf)
if buf[idVer] != socksVer5 {
return errVer
}
nmethod := int(buf[idNmethod]) // client support auth mode
msgLen := nmethod + 2 // auth msg length
if n == msgLen { // handshake done, common case
// do nothing, jump directly to send confirmation
} else if n < msgLen { // has more methods to read, rare case
buf, err = peerNode.DataBuffers[protocol.SOCKSDATA].GetDataBuffer(currentSessionID).ReadBytes()
if err != nil {
return err
}
} else { // error, should not get extra data
return errAuthExtraData
}
/*
X'00' NO AUTHENTICATION REQUIRED
X'01' GSSAPI
X'02' USERNAME/PASSWORD
X'03' to X'7F' IANA ASSIGNED
X'80' to X'FE' RESERVED FOR PRIVATE METHODS
X'FF' NO ACCEPTABLE METHODS
*/
// send confirmation: version 5, no authentication required
// _, err = conn.Write([]byte{socksVer5, 0})
buf = []byte{socksVer5, 0}
socks5Data := protocol.NetDataPacket{
SessionID: currentSessionID,
DataLen: uint32(len(buf)),
Data: buf,
}
size, _ := utils.PacketSize(socks5Data)
packetHeader := protocol.PacketHeader{
Separator: global.PROTOCOL_SEPARATOR,
CmdType: protocol.SOCKSDATA,
SrcHashID: utils.UUIDToArray32(node.CurrentNode.HashID),
DstHashID: utils.UUIDToArray32(peerNode.HashID),
DataLen: size,
}
peerNode.WritePacket(packetHeader, socks5Data)
return
}
func AgentParseTarget(peerNode *node.Node, currentSessionID uint16) (host string, err error) {
const (
idVer = 0
idCmd = 1
idType = 3 // address type index
idIP0 = 4 // ip addres start index
idDmLen = 4 // domain address length index
idDm0 = 5 // domain address start index
typeIPv4 = 1 // type is ipv4 address
typeDm = 3 // type is domain address
typeIPv6 = 4 // type is ipv6 address
lenIPv4 = 3 + 1 + net.IPv4len + 2 // 3(ver+cmd+rsv) + 1addrType + ipv4 + 2port
lenIPv6 = 3 + 1 + net.IPv6len + 2 // 3(ver+cmd+rsv) + 1addrType + ipv6 + 2port
lenDmBase = 3 + 1 + 1 + 2 // 3 + 1addrType + 1addrLen + 2port, plus addrLen
)
// refer to getRequest in server.go for why set buffer size to 263
// buf := make([]byte, 263)
var n int
// read till we get possible domain length field
// if n, err = io.ReadAtLeast(conn, buf, idDmLen+1); err != nil {
// return
// }
buf, err := peerNode.DataBuffers[protocol.SOCKSDATA].GetDataBuffer(currentSessionID).ReadBytes()
if err != nil {
return
}
n = len(buf)
// check version and cmd
if buf[idVer] != socksVer5 {
err = errVer
return
}
/*
CONNECT X'01'
BIND X'02'
UDP ASSOCIATE X'03'
*/
if buf[idCmd] > 0x03 || buf[idCmd] == 0x00 {
log.Println("Unknown Command", buf[idCmd])
}
if Verbose {
log.Println("Command:", Commands[buf[idCmd]-1])
}
if buf[idCmd] != socksCmdConnect { // only support CONNECT mode
err = errCmd
return
}
// read target address
reqLen := -1
switch buf[idType] {
case typeIPv4:
reqLen = lenIPv4
case typeIPv6:
reqLen = lenIPv6
case typeDm: // domain name
reqLen = int(buf[idDmLen]) + lenDmBase
default:
err = errAddrType
return
}
if n == reqLen {
// common case, do nothing
} else if n < reqLen { // rare case
// if _, err = io.ReadFull(conn, buf[n:reqLen]); err != nil {
// return
// }
var tmp []byte
tmp, err = peerNode.DataBuffers[protocol.SOCKSDATA].GetDataBuffer(currentSessionID).ReadBytes()
if err != nil {
return
}
buf = append(buf, tmp...)
} else {
err = errReqExtraData
return
}
switch buf[idType] {
case typeIPv4:
host = net.IP(buf[idIP0 : idIP0+net.IPv4len]).String()
case typeIPv6:
host = net.IP(buf[idIP0 : idIP0+net.IPv6len]).String()
case typeDm:
host = string(buf[idDm0 : idDm0+buf[idDmLen]])
}
port := binary.BigEndian.Uint16(buf[reqLen-2 : reqLen])
host = net.JoinHostPort(host, strconv.Itoa(int(port)))
return
}
func PipeWhenClose(peerNode *node.Node, currentSessionID uint16, target string) {
remoteConn, err := net.DialTimeout("tcp", target, time.Duration(time.Second*2))
if err != nil {
log.Println("Connect remote :", err)
return
}
tcpAddr := remoteConn.LocalAddr().(*net.TCPAddr)
if tcpAddr.Zone == "" {
if tcpAddr.IP.Equal(tcpAddr.IP.To4()) {
tcpAddr.Zone = "ip4"
} else {
tcpAddr.Zone = "ip6"
}
}
// if Verbose {
log.Println("[+]Connect to remote address:", target)
// }
rep := make([]byte, 256)
rep[0] = 0x05
rep[1] = 0x00 // success
rep[2] = 0x00 //RSV
//IP
if tcpAddr.Zone == "ip6" {
rep[3] = 0x04 //IPv6
} else {
rep[3] = 0x01 //IPv4
}
var ip net.IP
if "ip6" == tcpAddr.Zone {
ip = tcpAddr.IP.To16()
} else {
ip = tcpAddr.IP.To4()
}
pindex := 4
for _, b := range ip {
rep[pindex] = b
pindex++
}
rep[pindex] = byte((tcpAddr.Port >> 8) & 0xff)
rep[pindex+1] = byte(tcpAddr.Port & 0xff)
// conn.Write(rep[0 : pindex+2])
socks5Data := protocol.NetDataPacket{
SessionID: currentSessionID,
DataLen: uint32(pindex + 2),
Data: rep[0 : pindex+2],
}
size, _ := utils.PacketSize(socks5Data)
packetHeader := protocol.PacketHeader{
Separator: global.PROTOCOL_SEPARATOR,
CmdType: protocol.SOCKSDATA,
SrcHashID: utils.UUIDToArray32(node.CurrentNode.HashID),
DstHashID: utils.UUIDToArray32(peerNode.HashID),
DataLen: size,
}
peerNode.WritePacket(packetHeader, socks5Data)
// 退出处理
defer remoteConn.Close()
// Transfer data
c := make(chan bool, 2)
// Copy local to remote
go node.CopyNode2Net(peerNode, remoteConn, currentSessionID, protocol.SOCKSDATA, c)
// Copy remote to local
go node.CopyNet2Node(remoteConn, peerNode, currentSessionID, protocol.SOCKSDATA, c)
<-c
}