-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
89 lines (77 loc) · 2.34 KB
/
udp.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
package protocol
import (
"errors"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/transport"
)
var (
ErrorUnknownAddressType = errors.New("Unknown Address Type.")
)
type Socks5UDPRequest struct {
Fragment byte
Address v2net.Address
Port v2net.Port
Data *alloc.Buffer
}
func (request *Socks5UDPRequest) Destination() v2net.Destination {
return v2net.UDPDestination(request.Address, request.Port)
}
func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
buffer.AppendBytes(0, 0, request.Fragment)
switch {
case request.Address.IsIPv4():
buffer.AppendBytes(AddrTypeIPv4).Append(request.Address.IP())
case request.Address.IsIPv6():
buffer.AppendBytes(AddrTypeIPv6).Append(request.Address.IP())
case request.Address.IsDomain():
buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
}
buffer.AppendUint16(request.Port.Value())
buffer.Append(request.Data.Value)
}
func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
if len(packet) < 5 {
return nil, transport.ErrCorruptedPacket
}
request := new(Socks5UDPRequest)
// packet[0] and packet[1] are reserved
request.Fragment = packet[2]
addrType := packet[3]
var dataBegin int
switch addrType {
case AddrTypeIPv4:
if len(packet) < 10 {
return nil, transport.ErrCorruptedPacket
}
ip := packet[4:8]
request.Port = v2net.PortFromBytes(packet[8:10])
request.Address = v2net.IPAddress(ip)
dataBegin = 10
case AddrTypeIPv6:
if len(packet) < 22 {
return nil, transport.ErrCorruptedPacket
}
ip := packet[4:20]
request.Port = v2net.PortFromBytes(packet[20:22])
request.Address = v2net.IPAddress(ip)
dataBegin = 22
case AddrTypeDomain:
domainLength := int(packet[4])
if len(packet) < 5+domainLength+2 {
return nil, transport.ErrCorruptedPacket
}
domain := string(packet[5 : 5+domainLength])
request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])
request.Address = v2net.ParseAddress(domain)
dataBegin = 5 + domainLength + 2
default:
log.Warning("Unknown address type ", addrType)
return nil, ErrorUnknownAddressType
}
if len(packet) > dataBegin {
request.Data = alloc.NewBuffer().Clear().Append(packet[dataBegin:])
}
return request, nil
}