socks5

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AtypIPv4       = 1
	AtypDomainName = 3
	AtypIPv6       = 4
)

SOCKS address types as defined in RFC 1928 section 5.

View Source
const (
	// IPv4AddrLen is the size of an IPv4 SOCKS address in bytes.
	IPv4AddrLen = 1 + 4 + 2

	// IPv6AddrLen is the size of an IPv6 SOCKS address in bytes.
	IPv6AddrLen = 1 + 16 + 2

	// MaxAddrLen is the maximum size of a SOCKS address in bytes.
	MaxAddrLen = 1 + 1 + 255 + 2
)
View Source
const (
	MethodNoAuthenticationRequired = 0
	MethodGSSAPI                   = 1
	MethodUsernamePassword         = 2
	MethodNoAcceptable             = 0xFF
)

SOCKS5 authentication methods as defined in RFC 1928 section 3.

View Source
const (
	CmdConnect      = 1
	CmdBind         = 2
	CmdUDPAssociate = 3
)

SOCKS5 request commands as defined in RFC 1928 section 4.

View Source
const (
	ReplySucceeded                     = 0
	ReplyGeneralSocksServerFailure     = 1
	ReplyConnectionNotAllowedByRuleset = 2
	ReplyNetworkUnreachable            = 3
	ReplyHostUnreachable               = 4
	ReplyConnectionRefused             = 5
	ReplyTTLExpired                    = 6
	ReplyCommandNotSupported           = 7
	ReplyAddressTypeNotSupported       = 8
)

SOCKS5 reply field values as defined in RFC 1928 section 6.

View Source
const UsernamePasswordAuthVersion = 1

UsernamePasswordAuthVersion is the version of the username/password authentication method, as defined in RFC 1929 section 2.

View Source
const Version = 5

SOCKS version 5.

Variables

View Source
var (
	// IPv4UnspecifiedAddr represents 0.0.0.0:0.
	IPv4UnspecifiedAddr = [IPv4AddrLen]byte{AtypIPv4}

	// IPv6UnspecifiedAddr represents [::]:0.
	IPv6UnspecifiedAddr = [IPv6AddrLen]byte{AtypIPv6}
)
View Source
var (
	ErrUsernameLengthOutOfRange  = errors.New("username length out of range [1, 255]")
	ErrPasswordLengthOutOfRange  = errors.New("password length out of range [1, 255]")
	ErrNoAcceptableAuthMethod    = errors.New("no acceptable authentication method")
	ErrIncorrectUsernamePassword = errors.New("incorrect username or password")
	ErrUDPAssociateDone          = errors.New("UDP ASSOCIATE done")
)
View Source
var ErrFragmentationNotSupported = errors.New("packet fragmentation is not supported")

Functions

func AddrFromReader

func AddrFromReader(r io.Reader) ([]byte, error)

AddrFromReader allocates and reads a SOCKS address from an io.Reader.

To avoid allocations, call AppendFromReader directly.

func AddrPortFromSlice added in v1.1.0

func AddrPortFromSlice(b []byte) (netip.AddrPort, int, error)

AddrPortFromSlice slices a SOCKS address from the beginning of b and returns the converted netip.AddrPort and the length of the SOCKS address.

func AppendAddrFromAddrPort added in v1.1.0

func AppendAddrFromAddrPort(b []byte, addrPort netip.AddrPort) []byte

AppendAddrFromAddrPort appends the netip.AddrPort to the buffer in the SOCKS address format.

If the address is an IPv4-mapped IPv6 address, it is converted to an IPv4 address.

func AppendAddrFromConnAddr added in v1.1.0

func AppendAddrFromConnAddr(b []byte, addr conn.Addr) []byte

AppendAddrFromConnAddr appends the address to the buffer in the SOCKS address format.

- Zero value address is treated as 0.0.0.0:0. - IPv4-mapped IPv6 address is converted to the equivalent IPv4 address.

func AppendFromReader

func AppendFromReader(b []byte, r io.Reader) ([]byte, error)

AppendFromReader reads just enough bytes from r to get a valid Addr and appends it to the buffer.

func ClientConnect

func ClientConnect(rw io.ReadWriter, targetAddr conn.Addr) error

ClientConnect writes a CONNECT request to targetAddr.

func ClientConnectUsernamePassword added in v1.12.0

func ClientConnectUsernamePassword(rw io.ReadWriter, authMsg []byte, targetAddr conn.Addr) error

ClientConnectUsernamePassword is like ClientConnect, but uses username/password authentication.

func ClientRequest

func ClientRequest(rw io.ReadWriter, command byte, targetAddr conn.Addr) (addr conn.Addr, err error)

ClientRequest completes the handshake and writes a request with the given command and target address to rw. It returns the bound address in the reply.

func ClientRequestUsernamePassword added in v1.12.0

func ClientRequestUsernamePassword(rw io.ReadWriter, authMsg []byte, command byte, targetAddr conn.Addr) (addr conn.Addr, err error)

ClientRequestUsernamePassword is like ClientRequest, but uses username/password authentication.

func ClientUDPAssociate

func ClientUDPAssociate(rw io.ReadWriter, targetAddr conn.Addr) (conn.Addr, error)

ClientUDPAssociate writes a UDP ASSOCIATE request to targetAddr.

func ClientUDPAssociateUsernamePassword added in v1.12.0

func ClientUDPAssociateUsernamePassword(rw io.ReadWriter, authMsg []byte, targetAddr conn.Addr) (conn.Addr, error)

ClientUDPAssociateUsernamePassword is like ClientUDPAssociate, but uses username/password authentication.

func ConnAddrFromReader added in v1.1.0

func ConnAddrFromReader(r io.Reader) (conn.Addr, error)

ConnAddrFromReader reads a SOCKS address from r and returns the converted conn.Addr.

func ConnAddrFromSlice added in v1.1.0

func ConnAddrFromSlice(b []byte) (conn.Addr, int, error)

ConnAddrFromSlice slices a SOCKS address from the beginning of b and returns the converted conn.Addr and the length of the SOCKS address.

func ConnAddrFromSliceWithDomainCache added in v1.1.0

func ConnAddrFromSliceWithDomainCache(b []byte, cachedDomain string) (conn.Addr, int, string, error)

ConnAddrFromSliceWithDomainCache is like ConnAddrFromSlice but uses a domain cache to minimize string allocations. The returned string is the updated domain cache.

func LengthOfAddrFromAddrPort added in v1.1.0

func LengthOfAddrFromAddrPort(addrPort netip.AddrPort) int

LengthOfAddrFromAddrPort returns the length of a SOCKS address converted from the netip.AddrPort.

func LengthOfAddrFromConnAddr added in v1.1.0

func LengthOfAddrFromConnAddr(addr conn.Addr) int

LengthOfAddrFromConnAddr returns the length of a SOCKS address converted from the conn.Addr.

- Zero value address is treated as 0.0.0.0:0. - IPv4-mapped IPv6 address is treated as the equivalent IPv4 address.

func ServerAccept

func ServerAccept(rw io.ReadWriter, enableTCP, enableUDP bool) (addr conn.Addr, err error)

ServerAccept processes an incoming request from rw.

enableTCP enables the CONNECT command. enableUDP enables the UDP ASSOCIATE command.

When UDP is enabled, rw must be a *net.TCPConn.

func ServerAcceptUsernamePassword added in v1.12.0

func ServerAcceptUsernamePassword(rw io.ReadWriter, userInfoByUsername map[string]UserInfo, enableTCP, enableUDP bool) (addr conn.Addr, username string, err error)

ServerAcceptUsernamePassword is like ServerAccept, but uses username/password authentication.

func ValidatePacketHeader

func ValidatePacketHeader(b []byte) error

ValidatePacketHeader validates RSV and FRAG at the beginning of b. The length of b must be at least 3 bytes.

func WriteAddrFromAddrPort added in v1.1.0

func WriteAddrFromAddrPort(b []byte, addrPort netip.AddrPort) (n int)

WriteAddrFromAddrPort writes the netip.AddrPort to the buffer in the SOCKS address format and returns the number of bytes written.

If the address is an IPv4-mapped IPv6 address, it is converted to an IPv4 address.

This function does not check whether b has sufficient space for the address. The caller may call LengthOfAddrFromAddrPort to get the required length.

func WriteAddrFromConnAddr added in v1.1.0

func WriteAddrFromConnAddr(b []byte, addr conn.Addr) int

WriteAddrFromConnAddr writes the address to the buffer in the SOCKS address format and returns the number of bytes written.

- Zero value address is treated as 0.0.0.0:0. - IPv4-mapped IPv6 address is converted to the equivalent IPv4 address.

This function does not check whether b has sufficient space for the address. The caller may call LengthOfAddrFromConnAddr to get the required length.

func WritePacketHeader

func WritePacketHeader(b []byte)

WritePacketHeader writes RSV and FRAG to the beginning of b. The length of b must be at least 3 bytes.

Types

type ReplyError added in v1.12.0

type ReplyError byte

ReplyError is an error type for SOCKS5 reply errors.

func (ReplyError) Error added in v1.12.0

func (r ReplyError) Error() string

type UnsupportedAuthMethodError added in v1.12.0

type UnsupportedAuthMethodError byte

UnsupportedAuthMethodError is an error type for unsupported SOCKS5 authentication methods.

func (UnsupportedAuthMethodError) Error added in v1.12.0

func (UnsupportedAuthMethodError) Is added in v1.12.0

type UnsupportedCommandError added in v1.12.0

type UnsupportedCommandError byte

UnsupportedCommandError is an error type for unsupported SOCKS5 request commands.

func (UnsupportedCommandError) Error added in v1.12.0

func (c UnsupportedCommandError) Error() string

func (UnsupportedCommandError) Is added in v1.12.0

func (UnsupportedCommandError) Is(target error) bool

type UnsupportedUsernamePasswordAuthVersionError added in v1.12.0

type UnsupportedUsernamePasswordAuthVersionError byte

UnsupportedUsernamePasswordAuthVersionError is an error type for unsupported username/password authentication versions.

func (UnsupportedUsernamePasswordAuthVersionError) Error added in v1.12.0

func (UnsupportedUsernamePasswordAuthVersionError) Is added in v1.12.0

type UnsupportedVersionError added in v1.12.0

type UnsupportedVersionError byte

UnsupportedVersionError is an error type for unsupported SOCKS versions.

func (UnsupportedVersionError) Error added in v1.12.0

func (v UnsupportedVersionError) Error() string

func (UnsupportedVersionError) Is added in v1.12.0

func (UnsupportedVersionError) Is(target error) bool

type UserInfo added in v1.12.0

type UserInfo struct {
	// Username is the username.
	// It must be non-empty and at most 255 bytes long.
	Username string `json:"username"`

	// Password is the password.
	// It must be non-empty and at most 255 bytes long.
	Password string `json:"password"`
}

UserInfo is a username/password pair.

func (UserInfo) AppendAuthMsg added in v1.12.0

func (u UserInfo) AppendAuthMsg(b []byte) []byte

AppendAuthMsg appends the username/password pair as an authentication message to b.

Call Validate first to ensure the username and password are valid.

func (UserInfo) AuthMsgLength added in v1.12.0

func (u UserInfo) AuthMsgLength() int

AuthMsgLength returns the length of the authentication message for the username/password pair.

func (UserInfo) Validate added in v1.12.0

func (u UserInfo) Validate() error

Validate checks if the username and password are valid.

Jump to

Keyboard shortcuts

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