-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks5_helper.go
49 lines (40 loc) · 1.28 KB
/
socks5_helper.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
package scenarios
import (
v2net "github.com/v2ray/v2ray-core/common/net"
)
const (
socks5Version = byte(0x05)
)
func socks5AuthMethodRequest(methods ...byte) []byte {
request := []byte{socks5Version, byte(len(methods))}
request = append(request, methods...)
return request
}
func appendAddress(request []byte, address v2net.Address) []byte {
switch {
case address.IsIPv4():
request = append(request, byte(0x01))
request = append(request, address.IP()...)
case address.IsIPv6():
request = append(request, byte(0x04))
request = append(request, address.IP()...)
case address.IsDomain():
request = append(request, byte(0x03), byte(len(address.Domain())))
request = append(request, []byte(address.Domain())...)
}
return request
}
func socks5Request(command byte, address v2net.Destination) []byte {
request := []byte{socks5Version, command, 0}
request = appendAddress(request, address.Address())
request = append(request, address.Port().Bytes()...)
return request
}
func socks5UDPRequest(address v2net.Destination, payload []byte) []byte {
request := make([]byte, 0, 1024)
request = append(request, 0, 0, 0)
request = appendAddress(request, address.Address())
request = append(request, address.Port().Bytes()...)
request = append(request, payload...)
return request
}