v2ray-core/proxy/socks/protocol/udp.go

92 lines
2.5 KiB
Go
Raw Normal View History

package protocol
2015-09-16 19:13:13 +00:00
import (
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
2016-12-06 10:03:42 +00:00
"v2ray.com/core/common/serial"
2015-09-16 19:13:13 +00:00
)
2015-09-21 18:56:07 +00:00
var (
ErrorUnknownAddressType = errors.New("Unknown Address Type.")
)
2015-09-16 19:13:13 +00:00
type Socks5UDPRequest struct {
Fragment byte
Address v2net.Address
2015-12-16 22:53:38 +00:00
Port v2net.Port
2016-12-09 10:35:27 +00:00
Data *buf.Buffer
2015-09-16 19:13:13 +00:00
}
2015-09-21 18:56:07 +00:00
func (request *Socks5UDPRequest) Destination() v2net.Destination {
2015-12-16 22:53:38 +00:00
return v2net.UDPDestination(request.Address, request.Port)
2015-09-21 18:56:07 +00:00
}
2016-12-09 10:35:27 +00:00
func (request *Socks5UDPRequest) Write(buffer *buf.Buffer) {
2015-10-08 21:28:51 +00:00
buffer.AppendBytes(0, 0, request.Fragment)
2016-08-14 16:14:12 +00:00
switch request.Address.Family() {
case v2net.AddressFamilyIPv4:
2016-12-06 10:03:42 +00:00
buffer.AppendBytes(AddrTypeIPv4)
buffer.Append(request.Address.IP())
2016-08-14 16:14:12 +00:00
case v2net.AddressFamilyIPv6:
2016-12-06 10:03:42 +00:00
buffer.AppendBytes(AddrTypeIPv6)
buffer.Append(request.Address.IP())
2016-08-14 16:14:12 +00:00
case v2net.AddressFamilyDomain:
2016-12-06 10:03:42 +00:00
buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain())))
buffer.Append([]byte(request.Address.Domain()))
2015-10-05 14:58:14 +00:00
}
2016-12-09 11:08:25 +00:00
buffer.AppendSupplier(serial.WriteUint16(request.Port.Value()))
2016-12-06 10:03:42 +00:00
buffer.Append(request.Data.Bytes())
2015-10-05 14:57:50 +00:00
}
func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
if len(packet) < 5 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
request := new(Socks5UDPRequest)
2015-09-21 18:56:07 +00:00
// packet[0] and packet[1] are reserved
request.Fragment = packet[2]
2015-09-21 18:56:07 +00:00
addrType := packet[3]
var dataBegin int
switch addrType {
case AddrTypeIPv4:
if len(packet) < 10 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
2015-09-21 18:56:07 +00:00
ip := packet[4:8]
2015-12-16 22:53:38 +00:00
request.Port = v2net.PortFromBytes(packet[8:10])
request.Address = v2net.IPAddress(ip)
2015-09-21 18:56:07 +00:00
dataBegin = 10
case AddrTypeIPv6:
if len(packet) < 22 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
2015-09-21 18:56:07 +00:00
ip := packet[4:20]
2015-12-16 22:53:38 +00:00
request.Port = v2net.PortFromBytes(packet[20:22])
request.Address = v2net.IPAddress(ip)
2015-09-21 18:56:07 +00:00
dataBegin = 22
case AddrTypeDomain:
domainLength := int(packet[4])
if len(packet) < 5+domainLength+2 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
2015-09-21 18:56:07 +00:00
domain := string(packet[5 : 5+domainLength])
2015-12-16 22:53:38 +00:00
request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])
2016-02-05 10:23:16 +00:00
request.Address = v2net.ParseAddress(domain)
2015-09-21 18:56:07 +00:00
dataBegin = 5 + domainLength + 2
default:
2016-12-15 10:05:32 +00:00
return nil, errors.Format("Socks|UDP: Unknown address type %d", addrType)
2015-09-21 18:56:07 +00:00
}
if len(packet) > dataBegin {
2016-12-09 11:08:25 +00:00
b := buf.NewSmall()
2016-12-06 10:03:42 +00:00
b.Append(packet[dataBegin:])
request.Data = b
}
2015-09-16 19:13:13 +00:00
return request, nil
2015-09-16 19:13:13 +00:00
}