1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/testing/scenarios/socks5_helper.go

49 lines
1.2 KiB
Go
Raw Normal View History

2015-11-06 07:08:20 -05:00
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
}
2015-11-06 07:45:41 -05:00
func appendAddress(request []byte, address v2net.Address) []byte {
2015-11-06 07:08:20 -05:00
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())...)
}
2015-12-02 06:47:54 -05:00
request = append(request, address.Port().Bytes()...)
2015-11-06 07:08:20 -05:00
return request
}
2015-11-06 07:45:41 -05:00
func socks5Request(command byte, address v2net.Address) []byte {
request := []byte{socks5Version, command, 0}
request = appendAddress(request, address)
return request
}
func socks5UDPRequest(address v2net.Address, payload []byte) []byte {
request := make([]byte, 0, 1024)
request = append(request, 0, 0, 0)
request = appendAddress(request, address)
request = append(request, payload...)
return request
}