mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
362 lines
8.0 KiB
362 lines
8.0 KiB
9 years ago
|
package protocol
|
||
9 years ago
|
|
||
|
import (
|
||
9 years ago
|
"encoding/binary"
|
||
9 years ago
|
"errors"
|
||
9 years ago
|
"fmt"
|
||
|
"io"
|
||
9 years ago
|
|
||
9 years ago
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||
9 years ago
|
"github.com/v2ray/v2ray-core/log"
|
||
9 years ago
|
)
|
||
|
|
||
|
const (
|
||
9 years ago
|
socksVersion = byte(0x05)
|
||
|
socks4Version = byte(0x04)
|
||
9 years ago
|
|
||
9 years ago
|
AuthNotRequired = byte(0x00)
|
||
|
AuthGssApi = byte(0x01)
|
||
|
AuthUserPass = byte(0x02)
|
||
|
AuthNoMatchingMethod = byte(0xFF)
|
||
9 years ago
|
|
||
|
Socks4RequestGranted = byte(90)
|
||
|
Socks4RequestRejected = byte(91)
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrorSocksVersion4 = errors.New("Using SOCKS version 4.")
|
||
9 years ago
|
)
|
||
|
|
||
|
// Authentication request header of Socks5 protocol
|
||
|
type Socks5AuthenticationRequest struct {
|
||
9 years ago
|
version byte
|
||
9 years ago
|
nMethods byte
|
||
9 years ago
|
authMethods [256]byte
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
type Socks4AuthenticationRequest struct {
|
||
|
Version byte
|
||
|
Command byte
|
||
|
Port uint16
|
||
|
IP [4]byte
|
||
|
}
|
||
|
|
||
9 years ago
|
func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
|
||
9 years ago
|
for i := 0; i < int(request.nMethods); i++ {
|
||
9 years ago
|
if request.authMethods[i] == method {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
9 years ago
|
func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, auth4 Socks4AuthenticationRequest, err error) {
|
||
9 years ago
|
buffer := make([]byte, 256)
|
||
9 years ago
|
nBytes, err := reader.Read(buffer)
|
||
|
if err != nil {
|
||
9 years ago
|
log.Error("Failed to read socks authentication: %v", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
if nBytes < 2 {
|
||
|
err = fmt.Errorf("Expected 2 bytes read, but actaully %d bytes read", nBytes)
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if buffer[0] == socks4Version {
|
||
|
auth4.Version = buffer[0]
|
||
|
auth4.Command = buffer[1]
|
||
|
auth4.Port = binary.BigEndian.Uint16(buffer[2:4])
|
||
|
copy(auth4.IP[:], buffer[4:8])
|
||
|
err = ErrorSocksVersion4
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
auth.version = buffer[0]
|
||
|
if auth.version != socksVersion {
|
||
|
err = fmt.Errorf("Unknown SOCKS version %d", auth.version)
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
auth.nMethods = buffer[1]
|
||
|
if auth.nMethods <= 0 {
|
||
9 years ago
|
err = fmt.Errorf("Zero length of authentication methods")
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if nBytes-2 != int(auth.nMethods) {
|
||
9 years ago
|
err = fmt.Errorf("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
|
||
|
return
|
||
|
}
|
||
9 years ago
|
copy(auth.authMethods[:], buffer[2:nBytes])
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|
||
|
|
||
|
type Socks5AuthenticationResponse struct {
|
||
9 years ago
|
version byte
|
||
|
authMethod byte
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
type Socks4AuthenticationResponse struct {
|
||
|
result byte
|
||
|
port uint16
|
||
|
ip []byte
|
||
|
}
|
||
|
|
||
9 years ago
|
func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
|
||
9 years ago
|
return &Socks5AuthenticationResponse{
|
||
|
version: socksVersion,
|
||
|
authMethod: authMethod,
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func NewSocks4AuthenticationResponse(result byte, port uint16, ip []byte) *Socks4AuthenticationResponse {
|
||
|
return &Socks4AuthenticationResponse{
|
||
|
result: result,
|
||
|
port: port,
|
||
|
ip: ip,
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func WriteAuthentication(writer io.Writer, r *Socks5AuthenticationResponse) error {
|
||
|
_, err := writer.Write([]byte{r.version, r.authMethod})
|
||
9 years ago
|
return err
|
||
9 years ago
|
}
|
||
|
|
||
|
func WriteSocks4AuthenticationResponse(writer io.Writer, r *Socks4AuthenticationResponse) error {
|
||
|
buffer := make([]byte, 8)
|
||
|
// buffer[0] is always 0
|
||
|
buffer[1] = r.result
|
||
|
binary.BigEndian.PutUint16(buffer[2:4], r.port)
|
||
|
copy(buffer[4:], r.ip)
|
||
|
_, err := writer.Write(buffer)
|
||
|
return err
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
type Socks5UserPassRequest struct {
|
||
9 years ago
|
version byte
|
||
|
username string
|
||
|
password string
|
||
9 years ago
|
}
|
||
|
|
||
|
func (request Socks5UserPassRequest) IsValid(username string, password string) bool {
|
||
9 years ago
|
return request.username == username && request.password == password
|
||
9 years ago
|
}
|
||
|
|
||
|
func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) {
|
||
9 years ago
|
buffer := make([]byte, 256)
|
||
|
_, err = reader.Read(buffer[0:2])
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
request.version = buffer[0]
|
||
|
nUsername := buffer[1]
|
||
|
nBytes, err := reader.Read(buffer[:nUsername])
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
request.username = string(buffer[:nBytes])
|
||
|
|
||
|
_, err = reader.Read(buffer[0:1])
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
nPassword := buffer[0]
|
||
|
nBytes, err = reader.Read(buffer[:nPassword])
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
request.password = string(buffer[:nBytes])
|
||
|
return
|
||
9 years ago
|
}
|
||
|
|
||
|
type Socks5UserPassResponse struct {
|
||
9 years ago
|
version byte
|
||
|
status byte
|
||
9 years ago
|
}
|
||
|
|
||
|
func NewSocks5UserPassResponse(status byte) Socks5UserPassResponse {
|
||
9 years ago
|
return Socks5UserPassResponse{
|
||
|
version: socksVersion,
|
||
|
status: status,
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
func WriteUserPassResponse(writer io.Writer, response Socks5UserPassResponse) error {
|
||
9 years ago
|
_, err := writer.Write([]byte{response.version, response.status})
|
||
|
return err
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
const (
|
||
|
AddrTypeIPv4 = byte(0x01)
|
||
|
AddrTypeIPv6 = byte(0x04)
|
||
|
AddrTypeDomain = byte(0x03)
|
||
9 years ago
|
|
||
|
CmdConnect = byte(0x01)
|
||
|
CmdBind = byte(0x02)
|
||
|
CmdUdpAssociate = byte(0x03)
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
type Socks5Request struct {
|
||
9 years ago
|
Version byte
|
||
|
Command byte
|
||
|
AddrType byte
|
||
|
IPv4 [4]byte
|
||
|
Domain string
|
||
|
IPv6 [16]byte
|
||
|
Port uint16
|
||
9 years ago
|
}
|
||
|
|
||
|
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||
9 years ago
|
|
||
9 years ago
|
buffer := make([]byte, 4)
|
||
|
nBytes, err := reader.Read(buffer)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if nBytes < len(buffer) {
|
||
|
err = fmt.Errorf("Unable to read request.")
|
||
|
return
|
||
|
}
|
||
9 years ago
|
request = &Socks5Request{
|
||
|
Version: buffer[0],
|
||
|
Command: buffer[1],
|
||
|
// buffer[2] is a reserved field
|
||
|
AddrType: buffer[3],
|
||
|
}
|
||
9 years ago
|
switch request.AddrType {
|
||
9 years ago
|
case AddrTypeIPv4:
|
||
9 years ago
|
nBytes, err = reader.Read(request.IPv4[:])
|
||
9 years ago
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if nBytes != 4 {
|
||
|
err = fmt.Errorf("Unable to read IPv4 address.")
|
||
|
return
|
||
|
}
|
||
9 years ago
|
case AddrTypeDomain:
|
||
9 years ago
|
buffer = make([]byte, 256)
|
||
9 years ago
|
nBytes, err = reader.Read(buffer[0:1])
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
domainLength := buffer[0]
|
||
9 years ago
|
nBytes, err = reader.Read(buffer[:domainLength])
|
||
9 years ago
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
if nBytes != int(domainLength) {
|
||
|
err = fmt.Errorf("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
request.Domain = string(buffer[:domainLength])
|
||
9 years ago
|
case AddrTypeIPv6:
|
||
9 years ago
|
nBytes, err = reader.Read(request.IPv6[:])
|
||
9 years ago
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if nBytes != 16 {
|
||
|
err = fmt.Errorf("Unable to read IPv4 address.")
|
||
|
return
|
||
|
}
|
||
|
default:
|
||
9 years ago
|
err = fmt.Errorf("Unexpected address type %d", request.AddrType)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
buffer = make([]byte, 2)
|
||
|
nBytes, err = reader.Read(buffer)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if nBytes != 2 {
|
||
|
err = fmt.Errorf("Unable to read port.")
|
||
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
request.Port = binary.BigEndian.Uint16(buffer)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
func (request *Socks5Request) Destination() v2net.Address {
|
||
9 years ago
|
switch request.AddrType {
|
||
|
case AddrTypeIPv4:
|
||
|
return v2net.IPAddress(request.IPv4[:], request.Port)
|
||
|
case AddrTypeIPv6:
|
||
|
return v2net.IPAddress(request.IPv6[:], request.Port)
|
||
|
case AddrTypeDomain:
|
||
|
return v2net.DomainAddress(request.Domain, request.Port)
|
||
|
default:
|
||
|
panic("Unknown address type")
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
const (
|
||
|
ErrorSuccess = byte(0x00)
|
||
|
ErrorGeneralFailure = byte(0x01)
|
||
|
ErrorConnectionNotAllowed = byte(0x02)
|
||
|
ErrorNetworkUnreachable = byte(0x03)
|
||
|
ErrorHostUnUnreachable = byte(0x04)
|
||
|
ErrorConnectionRefused = byte(0x05)
|
||
|
ErrorTTLExpired = byte(0x06)
|
||
|
ErrorCommandNotSupported = byte(0x07)
|
||
|
ErrorAddressTypeNotSupported = byte(0x08)
|
||
|
)
|
||
|
|
||
|
type Socks5Response struct {
|
||
|
Version byte
|
||
|
Error byte
|
||
|
AddrType byte
|
||
|
IPv4 [4]byte
|
||
|
Domain string
|
||
|
IPv6 [16]byte
|
||
|
Port uint16
|
||
|
}
|
||
|
|
||
9 years ago
|
func NewSocks5Response() *Socks5Response {
|
||
9 years ago
|
return &Socks5Response{
|
||
|
Version: socksVersion,
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
|
func (r *Socks5Response) SetIPv4(ipv4 []byte) {
|
||
|
r.AddrType = AddrTypeIPv4
|
||
|
copy(r.IPv4[:], ipv4)
|
||
|
}
|
||
|
|
||
|
func (r *Socks5Response) SetIPv6(ipv6 []byte) {
|
||
|
r.AddrType = AddrTypeIPv6
|
||
|
copy(r.IPv6[:], ipv6)
|
||
|
}
|
||
|
|
||
|
func (r *Socks5Response) SetDomain(domain string) {
|
||
|
r.AddrType = AddrTypeDomain
|
||
|
r.Domain = domain
|
||
|
}
|
||
|
|
||
|
func (r *Socks5Response) toBytes() []byte {
|
||
9 years ago
|
buffer := make([]byte, 0, 300)
|
||
|
buffer = append(buffer, r.Version)
|
||
|
buffer = append(buffer, r.Error)
|
||
|
buffer = append(buffer, 0x00) // reserved
|
||
|
buffer = append(buffer, r.AddrType)
|
||
|
switch r.AddrType {
|
||
|
case 0x01:
|
||
|
buffer = append(buffer, r.IPv4[:]...)
|
||
|
case 0x03:
|
||
|
buffer = append(buffer, byte(len(r.Domain)))
|
||
|
buffer = append(buffer, []byte(r.Domain)...)
|
||
|
case 0x04:
|
||
|
buffer = append(buffer, r.IPv6[:]...)
|
||
|
}
|
||
|
portBuffer := make([]byte, 2)
|
||
|
binary.BigEndian.PutUint16(portBuffer, r.Port)
|
||
|
buffer = append(buffer, portBuffer...)
|
||
|
return buffer
|
||
|
}
|
||
|
|
||
9 years ago
|
func WriteResponse(writer io.Writer, response *Socks5Response) error {
|
||
9 years ago
|
_, err := writer.Write(response.toBytes())
|
||
|
return err
|
||
|
}
|