message

package
v0.0.0-...-89ec37b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2022 License: 0BSD Imports: 13 Imported by: 0

Documentation

Overview

message contains SOCKS 6 wireformat parser and serializer

Index

Constants

View Source
const (
	// lv1
	StackOptionIPTOS          = int(StackOptionLevelIP)*256 + int(StackOptionCodeTOS)
	StackOptionIPHappyEyeball = int(StackOptionLevelIP)*256 + int(StackOptionCodeHappyEyeball)
	StackOptionIPTTL          = int(StackOptionLevelIP)*256 + int(StackOptionCodeTTL)
	StackOptionIPNoFragment   = int(StackOptionLevelIP)*256 + int(StackOptionCodeNoFragment)
	// lv4
	StackOptionTCPTFO       = int(StackOptionLevelTCP)*256 + int(StackOptionCodeTFO)
	StackOptionTCPMultipath = int(StackOptionLevelTCP)*256 + int(StackOptionCodeMultipath)
	StackOptionTCPBacklog   = int(StackOptionLevelTCP)*256 + int(StackOptionCodeBacklog)
	// lv5
	StackOptionUDPUDPError   = int(StackOptionLevelUDP)*256 + int(StackOptionCodeUDPError)
	StackOptionUDPPortParity = int(StackOptionLevelUDP)*256 + int(StackOptionCodePortParity)
)
View Source
const (
	StackPortParityOptionParityNo   = 0
	StackPortParityOptionParityEven = 1
	StackPortParityOptionParityOdd  = 2
)
View Source
const MaxOptionSize = 20 * 1024
View Source
const Socks5Version = 5

Variables

View Source
var ErrAddressTypeNotSupport = common.LeveledError{
	Message: "unsupported address type",
	Base:    ErrMessageProcess,
	Level:   lg.LvError,
}
View Source
var ErrBufferSize = common.LeveledError{
	Message: "invalid buffer size",
	Base:    ErrMessageProcess,
	Level:   lg.LvWarning,
}
View Source
var ErrEnumValue = common.LeveledError{
	Message: "invalid enum value",
	Base:    ErrMessageProcess,
	Level:   lg.LvError,
}
View Source
var ErrFormat = common.LeveledError{
	Message: "format error",
	Base:    ErrMessageProcess,
	Level:   lg.LvError,
}
View Source
var ErrMessageProcess = errors.New("message error")
View Source
var ErrOptionTooLong = common.LeveledError{
	Message: "option too long",
	Base:    ErrMessageProcess,
	Level:   lg.LvWarning,
}
View Source
var ErrStackOptionNoLeg = common.LeveledError{
	Message: "stack option should have at least one leg",
	Base:    ErrMessageProcess,
	Level:   lg.LvWarning,
}

Functions

func GetAssociationID

func GetAssociationID(b []byte) uint64

func NewErrVersionMismatch

func NewErrVersionMismatch(v int, b []byte) error

func SetOptionDataParser

func SetOptionDataParser(kind OptionKind, fn func([]byte) (OptionData, error))

SetOptionDataParser set the option data parse function for given kind to fn set fn to nil to clear parser

func SetStackOptionDataParser

func SetStackOptionDataParser(lv StackOptionLevel, code StackOptionCode, fn func([]byte) (StackOptionData, error))

SetStackOptionDataParser set the stack option data parse function for given level and code to fn set fn to nil to clear parser

func SplitStackOptionID

func SplitStackOptionID(id int) (StackOptionLevel, StackOptionCode)

func StackOptionID

func StackOptionID(level StackOptionLevel, code StackOptionCode) int

Types

type AddressType

type AddressType byte
const (
	AddressTypeIPv4       AddressType = 1
	AddressTypeDomainName AddressType = 3
	AddressTypeIPv6       AddressType = 4
)

type AuthenticationDataOptionData

type AuthenticationDataOptionData struct {
	Method byte
	Data   []byte
}

func (AuthenticationDataOptionData) Len

func (AuthenticationDataOptionData) Marshal

func (s AuthenticationDataOptionData) Marshal() []byte

type AuthenticationMethodAdvertisementOptionData

type AuthenticationMethodAdvertisementOptionData struct {
	InitialDataLength uint16
	Methods           []byte
}

func (AuthenticationMethodAdvertisementOptionData) Len

func (AuthenticationMethodAdvertisementOptionData) Marshal

type AuthenticationMethodSelectionOptionData

type AuthenticationMethodSelectionOptionData struct {
	Method byte
}

func (AuthenticationMethodSelectionOptionData) Marshal

type AuthenticationReply

type AuthenticationReply struct {
	Type    AuthenticationReplyType
	Options *OptionSet
}

func NewAuthenticationReply

func NewAuthenticationReply() *AuthenticationReply

func NewAuthenticationReplyWithType

func NewAuthenticationReplyWithType(typ AuthenticationReplyType) *AuthenticationReply

func ParseAuthenticationReplyFrom

func ParseAuthenticationReplyFrom(b io.Reader) (*AuthenticationReply, error)

func (*AuthenticationReply) Marshal

func (a *AuthenticationReply) Marshal() []byte

type AuthenticationReplyType

type AuthenticationReplyType byte
const (
	AuthenticationReplySuccess AuthenticationReplyType = 0
	AuthenticationReplyFail    AuthenticationReplyType = 1
)

type BacklogOptionData

type BacklogOptionData struct {
	Backlog uint16
}

func (BacklogOptionData) GetData

func (t BacklogOptionData) GetData() interface{}

func (BacklogOptionData) Len

func (t BacklogOptionData) Len() uint16

func (BacklogOptionData) Marshal

func (t BacklogOptionData) Marshal() []byte

func (*BacklogOptionData) SetData

func (t *BacklogOptionData) SetData(d interface{})

func (*BacklogOptionData) SetUint16

func (t *BacklogOptionData) SetUint16(b uint16)

type BaseStackOptionData

type BaseStackOptionData struct {
	ClientLeg bool
	RemoteLeg bool
	Level     StackOptionLevel
	Code      StackOptionCode
	Data      StackOptionData
}

func (BaseStackOptionData) Len

func (s BaseStackOptionData) Len() uint16

func (BaseStackOptionData) Marshal

func (s BaseStackOptionData) Marshal() []byte

type CommandCode

type CommandCode byte

CommandCode is SOCKS 6 request command code

const (
	CommandNoop CommandCode = iota
	CommandConnect
	CommandBind
	CommandUdpAssociate
)

type ErrVersionMismatch

type ErrVersionMismatch struct {
	Version       int
	ConsumedBytes []byte
}

func (ErrVersionMismatch) Error

func (e ErrVersionMismatch) Error() string

func (ErrVersionMismatch) Is

func (e ErrVersionMismatch) Is(e2 error) bool

func (ErrVersionMismatch) Unwrap

func (e ErrVersionMismatch) Unwrap() error

type Handshake

type Handshake struct {
	Methods []byte
}

func ParseHandshake5From

func ParseHandshake5From(b io.Reader) (*Handshake, error)

func (*Handshake) Marshal5

func (h *Handshake) Marshal5() []byte

type HappyEyeballOptionData

type HappyEyeballOptionData struct {
	Availability bool
}

func (HappyEyeballOptionData) GetData

func (t HappyEyeballOptionData) GetData() interface{}

func (HappyEyeballOptionData) Len

func (HappyEyeballOptionData) Marshal

func (t HappyEyeballOptionData) Marshal() []byte

func (*HappyEyeballOptionData) SetBool

func (t *HappyEyeballOptionData) SetBool(b bool)

func (*HappyEyeballOptionData) SetData

func (t *HappyEyeballOptionData) SetData(d interface{})

type IdempotenceAcceptedOptionData

type IdempotenceAcceptedOptionData struct{}

func (IdempotenceAcceptedOptionData) Marshal

func (s IdempotenceAcceptedOptionData) Marshal() []byte

type IdempotenceExpenditureOptionData

type IdempotenceExpenditureOptionData struct {
	Token uint32
}

func (IdempotenceExpenditureOptionData) Marshal

func (s IdempotenceExpenditureOptionData) Marshal() []byte

type IdempotenceRejectedOptionData

type IdempotenceRejectedOptionData struct{}

func (IdempotenceRejectedOptionData) Marshal

func (s IdempotenceRejectedOptionData) Marshal() []byte

type IdempotenceWindowOptionData

type IdempotenceWindowOptionData struct {
	WindowBase uint32
	WindowSize uint32
}

func (IdempotenceWindowOptionData) Marshal

func (s IdempotenceWindowOptionData) Marshal() []byte

type MethodSelection

type MethodSelection struct {
	Method byte
}

func ParseMethodSelection5From

func ParseMethodSelection5From(b io.Reader) (*MethodSelection, error)

func (*MethodSelection) Marshal5

func (h *MethodSelection) Marshal5() []byte

type MultipathOptionData

type MultipathOptionData struct {
	Availability bool
}

func (MultipathOptionData) GetData

func (t MultipathOptionData) GetData() interface{}

func (MultipathOptionData) Len

func (t MultipathOptionData) Len() uint16

func (MultipathOptionData) Marshal

func (t MultipathOptionData) Marshal() []byte

func (*MultipathOptionData) SetBool

func (t *MultipathOptionData) SetBool(b bool)

func (*MultipathOptionData) SetData

func (t *MultipathOptionData) SetData(d interface{})

type NoFragmentationOptionData

type NoFragmentationOptionData struct {
	Availability bool
}

func (NoFragmentationOptionData) GetData

func (t NoFragmentationOptionData) GetData() interface{}

func (NoFragmentationOptionData) Len

func (NoFragmentationOptionData) Marshal

func (t NoFragmentationOptionData) Marshal() []byte

func (*NoFragmentationOptionData) SetBool

func (t *NoFragmentationOptionData) SetBool(b bool)

func (*NoFragmentationOptionData) SetData

func (t *NoFragmentationOptionData) SetData(d interface{})

type OperationReply

type OperationReply struct {
	ReplyCode ReplyCode
	Endpoint  *SocksAddr
	Options   *OptionSet
}

func NewOperationReply

func NewOperationReply() *OperationReply

func NewOperationReplyWithCode

func NewOperationReplyWithCode(code ReplyCode) *OperationReply

func ParseOperationReply5From

func ParseOperationReply5From(b io.Reader) (*OperationReply, error)

func ParseOperationReplyFrom

func ParseOperationReplyFrom(b io.Reader) (*OperationReply, error)

func (*OperationReply) Marshal

func (o *OperationReply) Marshal() []byte

func (*OperationReply) Marshal5

func (o *OperationReply) Marshal5() []byte

type Option

type Option struct {
	Kind   OptionKind
	Length uint16
	Data   OptionData
}

Option represent a SOCKS6 option

func GetCombinedStackOptions

func GetCombinedStackOptions(client StackOptionInfo, remote StackOptionInfo) []Option

func ParseOptionFrom

func ParseOptionFrom(b io.Reader) (Option, error)

ParseOptionFrom parses b as a SOCKS6 option.

func (*Option) Marshal

func (o *Option) Marshal() []byte

Marshal return option's binary encoding When encoding, it will always use OptionData provided length, option's Length field is ignored and updated by actual length.

type OptionData

type OptionData interface {
	Marshal() []byte
}

type OptionKind

type OptionKind uint16
const (
	OptionKindStack OptionKind
	OptionKindAuthenticationMethodAdvertisement
	OptionKindAuthenticationMethodSelection
	OptionKindAuthenticationData
	OptionKindSessionRequest
	OptionKindSessionID

	OptionKindSessionOK
	OptionKindSessionInvalid
	OptionKindSessionTeardown
	OptionKindTokenRequest
	OptionKindIdempotenceWindow
	OptionKindIdempotenceExpenditure
	OptionKindIdempotenceAccepted
	OptionKindIdempotenceRejected
)
const OptionKindStreamID OptionKind = 0xfd10

type OptionSet

type OptionSet struct {
	// contains filtered or unexported fields
}

func NewOptionSet

func NewOptionSet() *OptionSet

func ParseOptionSetFrom

func ParseOptionSetFrom(b io.Reader, limit int) (*OptionSet, error)

func (*OptionSet) Add

func (s *OptionSet) Add(o Option)

func (*OptionSet) AddMany

func (s *OptionSet) AddMany(o []Option)

func (*OptionSet) GetData

func (s *OptionSet) GetData(kind OptionKind) (OptionData, bool)

func (*OptionSet) GetDataF

func (s *OptionSet) GetDataF(kind OptionKind, fn func(Option) bool) (OptionData, bool)

func (*OptionSet) GetKind

func (s *OptionSet) GetKind(kind OptionKind) []Option

func (*OptionSet) GetKindF

func (s *OptionSet) GetKindF(kind OptionKind, fn func(Option) bool) []Option

func (*OptionSet) Len

func (s *OptionSet) Len() int

func (*OptionSet) Marshal

func (s *OptionSet) Marshal() []byte

func (OptionSet) String

func (s OptionSet) String() string

type PortParityOptionData

type PortParityOptionData struct {
	Parity  byte
	Reserve bool
}

func (PortParityOptionData) GetData

func (t PortParityOptionData) GetData() interface{}

func (PortParityOptionData) Len

func (t PortParityOptionData) Len() uint16

func (PortParityOptionData) Marshal

func (t PortParityOptionData) Marshal() []byte

func (*PortParityOptionData) SetData

func (t *PortParityOptionData) SetData(d interface{})

type RawOptionData

type RawOptionData struct {
	Data []byte
}

func (RawOptionData) GetData

func (r RawOptionData) GetData() interface{}

func (RawOptionData) Len

func (r RawOptionData) Len() uint16

func (*RawOptionData) Marshal

func (r *RawOptionData) Marshal() []byte

func (*RawOptionData) SetData

func (r *RawOptionData) SetData(d interface{})

type ReplyCode

type ReplyCode byte
const (
	OperationReplySuccess ReplyCode = iota
	OperationReplyServerFailure
	OperationReplyNotAllowedByRule
	OperationReplyNetworkUnreachable
	OperationReplyHostUnreachable
	OperationReplyConnectionRefused
	OperationReplyTTLExpired
	OperationReplyCommandNotSupported
	OperationReplyAddressNotSupported
	OperationReplyTimeout
)

type Request

type Request struct {
	CommandCode CommandCode
	Endpoint    *SocksAddr
	Options     *OptionSet
}

func NewRequest

func NewRequest() *Request

func ParseRequest5From

func ParseRequest5From(b io.Reader) (*Request, error)

func ParseRequestFrom

func ParseRequestFrom(b io.Reader) (*Request, error)

func (*Request) Marshal

func (r *Request) Marshal() (buf []byte)

func (*Request) Marshal5

func (r *Request) Marshal5() (buf []byte)

type SessionIDOptionData

type SessionIDOptionData struct {
	ID []byte
}

func (SessionIDOptionData) Marshal

func (s SessionIDOptionData) Marshal() []byte

type SessionInvalidOptionData

type SessionInvalidOptionData struct{}

func (SessionInvalidOptionData) Marshal

func (s SessionInvalidOptionData) Marshal() []byte

type SessionOKOptionData

type SessionOKOptionData struct{}

func (SessionOKOptionData) Marshal

func (s SessionOKOptionData) Marshal() []byte

type SessionRequestOptionData

type SessionRequestOptionData struct{}

func (SessionRequestOptionData) Marshal

func (s SessionRequestOptionData) Marshal() []byte

type SessionTeardownOptionData

type SessionTeardownOptionData struct{}

func (SessionTeardownOptionData) Marshal

func (s SessionTeardownOptionData) Marshal() []byte

type SocksAddr

type SocksAddr struct {
	// address' type
	AddressType AddressType
	// actual address,
	// if AddressType is IPv4/IPv6, contains IP address byte.
	// If AddressType is DomainName, contains domain name in punycode encoded format without leading length byte and padding.
	Address []byte
	// port used by transport layer protocol
	Port uint16
}

SocksAddr is address and port used in SOCKS6 protocol

var AddrIPv4Zero *SocksAddr = &SocksAddr{
	AddressType: AddressTypeIPv4,
	Address:     []byte{0, 0, 0, 0},
	Port:        0,
}

AddrIPv4Zero is 0.0.0.0:0 in SocksAddr format

var AddrIPv6Zero *SocksAddr = &SocksAddr{
	AddressType: AddressTypeIPv6,
	Address: []byte{
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0},
	Port: 0,
}

AddrIPv6Zero is [::]:0 in SocksAddr format

var DefaultAddr *SocksAddr = AddrIPv4Zero

DefaultAddr is 0.0.0.0:0 in SocksAddr format

func ConvertAddr

func ConvertAddr(addr net.Addr) *SocksAddr

ConvertAddr try to convert net.Addr to SocksAddr

func NewAddr

func NewAddr(address string) (*SocksAddr, error)

NewAddr parse address string to SocksAddr

func ParseAddr

func ParseAddr(addr string) *SocksAddr

ParseAddr parse address string to SocksAddr panic when error

func ParseSocksAddr5From

func ParseSocksAddr5From(b io.Reader) (*SocksAddr, error)

func ParseSocksAddr6From

func ParseSocksAddr6From(b io.Reader) (addr *SocksAddr, pad byte, nConsume int, err error)

ParseSocksAddr6FromWithLimit parse socks 6 address with border set to 260 byte

func ParseSocksAddr6FromWithLimit

func ParseSocksAddr6FromWithLimit(b io.Reader, limit int) (addr *SocksAddr, pad byte, nConsume int, err error)

ParseSocksAddr6FromWithLimit parse socks 6 address with border check

func (*SocksAddr) Marshal5

func (a *SocksAddr) Marshal5() []byte

func (*SocksAddr) Marshal6

func (a *SocksAddr) Marshal6(pad byte) []byte

Marshal6 serialize to socks 6 wireformat

func (*SocksAddr) Network

func (a *SocksAddr) Network() string

Network implements net.Addr, always return "socks"

func (*SocksAddr) String

func (a *SocksAddr) String() string

String implements net.Addr

type StackOptionCode

type StackOptionCode byte
const (
	// lv1
	StackOptionCodeTOS          StackOptionCode = 1
	StackOptionCodeHappyEyeball StackOptionCode = 2
	StackOptionCodeTTL          StackOptionCode = 3
	StackOptionCodeNoFragment   StackOptionCode = 4
	// lv4
	StackOptionCodeTFO       StackOptionCode = 1
	StackOptionCodeMultipath StackOptionCode = 2
	StackOptionCodeBacklog   StackOptionCode = 3
	//lv5
	StackOptionCodeUDPError   StackOptionCode = 1
	StackOptionCodePortParity StackOptionCode = 2
)

type StackOptionData

type StackOptionData interface {
	OptionData
	Len() uint16
	GetData() interface{}
	SetData(interface{})
}

type StackOptionInfo

type StackOptionInfo map[int]interface{}

func GetStackOptionInfo

func GetStackOptionInfo(ops *OptionSet, clientLeg bool) StackOptionInfo

func (*StackOptionInfo) Add

func (*StackOptionInfo) AddMany

func (s *StackOptionInfo) AddMany(d []Option)

func (*StackOptionInfo) Combine

func (s *StackOptionInfo) Combine(s2 StackOptionInfo)

func (StackOptionInfo) Filter

func (StackOptionInfo) GetOptions

func (s StackOptionInfo) GetOptions(clientLeg bool, remoteLeg bool) []Option

type StackOptionLevel

type StackOptionLevel byte
const (
	StackOptionLevelIP StackOptionLevel
	StackOptionLevelIPv4
	StackOptionLevelIPv6
	StackOptionLevelTCP
	StackOptionLevelUDP
)

type StreamIDOptionData

type StreamIDOptionData struct {
	ID uint32
}

func (StreamIDOptionData) Marshal

func (s StreamIDOptionData) Marshal() []byte

type TFOOptionData

type TFOOptionData struct {
	PayloadSize uint16
}

func (TFOOptionData) GetData

func (t TFOOptionData) GetData() interface{}

func (TFOOptionData) Len

func (t TFOOptionData) Len() uint16

func (TFOOptionData) Marshal

func (t TFOOptionData) Marshal() []byte

func (*TFOOptionData) SetData

func (t *TFOOptionData) SetData(d interface{})

func (*TFOOptionData) SetUint16

func (t *TFOOptionData) SetUint16(b uint16)

type TOSOptionData

type TOSOptionData struct {
	TOS byte
}

func (TOSOptionData) GetData

func (t TOSOptionData) GetData() interface{}

func (TOSOptionData) Len

func (t TOSOptionData) Len() uint16

func (TOSOptionData) Marshal

func (t TOSOptionData) Marshal() []byte

func (*TOSOptionData) SetData

func (t *TOSOptionData) SetData(d interface{})

func (*TOSOptionData) SetUint8

func (t *TOSOptionData) SetUint8(b byte)

type TTLOptionData

type TTLOptionData struct {
	TTL byte
}

func (TTLOptionData) GetData

func (t TTLOptionData) GetData() interface{}

func (TTLOptionData) Len

func (t TTLOptionData) Len() uint16

func (TTLOptionData) Marshal

func (t TTLOptionData) Marshal() []byte

func (*TTLOptionData) SetData

func (t *TTLOptionData) SetData(d interface{})

func (*TTLOptionData) SetUint8

func (t *TTLOptionData) SetUint8(b byte)

type TokenRequestOptionData

type TokenRequestOptionData struct {
	WindowSize uint32
}

func (TokenRequestOptionData) Marshal

func (s TokenRequestOptionData) Marshal() []byte

type UDPErrorOptionData

type UDPErrorOptionData struct {
	Availability bool
}

func (UDPErrorOptionData) GetData

func (t UDPErrorOptionData) GetData() interface{}

func (UDPErrorOptionData) Len

func (t UDPErrorOptionData) Len() uint16

func (UDPErrorOptionData) Marshal

func (t UDPErrorOptionData) Marshal() []byte

func (*UDPErrorOptionData) SetBool

func (t *UDPErrorOptionData) SetBool(b bool)

func (*UDPErrorOptionData) SetData

func (t *UDPErrorOptionData) SetData(d interface{})

type UDPErrorType

type UDPErrorType byte
const (
	UDPErrorNetworkUnreachable UDPErrorType
	UDPErrorHostUnreachable
	UDPErrorTTLExpired
	UDPErrorDatagramTooBig
)

type UDPHeaderType

type UDPHeaderType byte
const (
	UDPMessageAssociationInit UDPHeaderType
	UDPMessageAssociationAck
	UDPMessageDatagram
	UDPMessageError
)

type UDPMessage

type UDPMessage struct {
	Type          UDPHeaderType
	AssociationID uint64
	// dgram & icmp
	Endpoint *SocksAddr
	// icmp
	ErrorEndpoint *SocksAddr
	ErrorCode     UDPErrorType
	// dgram
	Data []byte
}

func ParseUDPMessage5From

func ParseUDPMessage5From(b io.Reader) (*UDPMessage, error)

func ParseUDPMessageFrom

func ParseUDPMessageFrom(b io.Reader) (*UDPMessage, error)

func (*UDPMessage) Marshal

func (u *UDPMessage) Marshal() []byte

func (*UDPMessage) Marshal5

func (u *UDPMessage) Marshal5() []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL