2015-11-06 07:08:20 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
2015-11-06 07:08:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
socks5Version = byte(0x05)
|
|
|
|
)
|
|
|
|
|
|
|
|
func socks5AuthMethodRequest(methods ...byte) []byte {
|
|
|
|
request := []byte{socks5Version, byte(len(methods))}
|
|
|
|
request = append(request, methods...)
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:45:41 -05:00
|
|
|
func appendAddress(request []byte, address v2net.Address) []byte {
|
2016-08-14 12:14:12 -04:00
|
|
|
switch address.Family() {
|
|
|
|
case v2net.AddressFamilyIPv4:
|
2015-11-06 07:08:20 -05:00
|
|
|
request = append(request, byte(0x01))
|
|
|
|
request = append(request, address.IP()...)
|
|
|
|
|
2016-08-14 12:14:12 -04:00
|
|
|
case v2net.AddressFamilyIPv6:
|
2015-11-06 07:08:20 -05:00
|
|
|
request = append(request, byte(0x04))
|
|
|
|
request = append(request, address.IP()...)
|
|
|
|
|
2016-08-14 12:14:12 -04:00
|
|
|
case v2net.AddressFamilyDomain:
|
2015-11-06 07:08:20 -05:00
|
|
|
request = append(request, byte(0x03), byte(len(address.Domain())))
|
|
|
|
request = append(request, []byte(address.Domain())...)
|
|
|
|
|
|
|
|
}
|
|
|
|
return request
|
|
|
|
}
|
2015-11-06 07:45:41 -05:00
|
|
|
|
2015-12-16 17:53:38 -05:00
|
|
|
func socks5Request(command byte, address v2net.Destination) []byte {
|
2015-11-06 07:45:41 -05:00
|
|
|
request := []byte{socks5Version, command, 0}
|
2016-09-20 05:53:05 -04:00
|
|
|
request = appendAddress(request, address.Address)
|
|
|
|
request = address.Port.Bytes(request)
|
2015-11-06 07:45:41 -05:00
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:53:38 -05:00
|
|
|
func socks5UDPRequest(address v2net.Destination, payload []byte) []byte {
|
2015-11-06 07:45:41 -05:00
|
|
|
request := make([]byte, 0, 1024)
|
|
|
|
request = append(request, 0, 0, 0)
|
2016-09-20 05:53:05 -04:00
|
|
|
request = appendAddress(request, address.Address)
|
|
|
|
request = address.Port.Bytes(request)
|
2015-11-06 07:45:41 -05:00
|
|
|
request = append(request, payload...)
|
|
|
|
return request
|
|
|
|
}
|