Documentation ¶
Index ¶
- Constants
- Variables
- func AddrFromReader(r io.Reader) ([]byte, error)
- func AddrPortFromSlice(b []byte) (netip.AddrPort, int, error)
- func AppendAddrFromAddrPort(b []byte, addrPort netip.AddrPort) []byte
- func AppendAddrFromConnAddr(b []byte, addr conn.Addr) []byte
- func AppendFromReader(b []byte, r io.Reader) ([]byte, error)
- func ClientConnect(rw io.ReadWriter, targetAddr conn.Addr) error
- func ClientConnectUsernamePassword(rw io.ReadWriter, authMsg []byte, targetAddr conn.Addr) error
- func ClientRequest(rw io.ReadWriter, command byte, targetAddr conn.Addr) (addr conn.Addr, err error)
- func ClientRequestUsernamePassword(rw io.ReadWriter, authMsg []byte, command byte, targetAddr conn.Addr) (addr conn.Addr, err error)
- func ClientUDPAssociate(rw io.ReadWriter, targetAddr conn.Addr) (conn.Addr, error)
- func ClientUDPAssociateUsernamePassword(rw io.ReadWriter, authMsg []byte, targetAddr conn.Addr) (conn.Addr, error)
- func ConnAddrFromReader(r io.Reader) (conn.Addr, error)
- func ConnAddrFromSlice(b []byte) (conn.Addr, int, error)
- func ConnAddrFromSliceWithDomainCache(b []byte, cachedDomain string) (conn.Addr, int, string, error)
- func LengthOfAddrFromAddrPort(addrPort netip.AddrPort) int
- func LengthOfAddrFromConnAddr(addr conn.Addr) int
- func ServerAccept(rw io.ReadWriter, enableTCP, enableUDP bool) (addr conn.Addr, err error)
- func ServerAcceptUsernamePassword(rw io.ReadWriter, userInfoByUsername map[string]UserInfo, ...) (addr conn.Addr, username string, err error)
- func ValidatePacketHeader(b []byte) error
- func WriteAddrFromAddrPort(b []byte, addrPort netip.AddrPort) (n int)
- func WriteAddrFromConnAddr(b []byte, addr conn.Addr) int
- func WritePacketHeader(b []byte)
- type ReplyError
- type UnsupportedAuthMethodError
- type UnsupportedCommandError
- type UnsupportedUsernamePasswordAuthVersionError
- type UnsupportedVersionError
- type UserInfo
Constants ¶
const ( AtypIPv4 = 1 AtypDomainName = 3 AtypIPv6 = 4 )
SOCKS address types as defined in RFC 1928 section 5.
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 )
const ( MethodNoAuthenticationRequired = 0 MethodGSSAPI = 1 MethodUsernamePassword = 2 MethodNoAcceptable = 0xFF )
SOCKS5 authentication methods as defined in RFC 1928 section 3.
const ( CmdConnect = 1 CmdBind = 2 CmdUDPAssociate = 3 )
SOCKS5 request commands as defined in RFC 1928 section 4.
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.
const UsernamePasswordAuthVersion = 1
UsernamePasswordAuthVersion is the version of the username/password authentication method, as defined in RFC 1929 section 2.
const Version = 5
SOCKS version 5.
Variables ¶
var ( // IPv4UnspecifiedAddr represents 0.0.0.0:0. IPv4UnspecifiedAddr = [IPv4AddrLen]byte{AtypIPv4} // IPv6UnspecifiedAddr represents [::]:0. IPv6UnspecifiedAddr = [IPv6AddrLen]byte{AtypIPv6} )
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") )
var ErrFragmentationNotSupported = errors.New("packet fragmentation is not supported")
Functions ¶
func AddrFromReader ¶
AddrFromReader allocates and reads a SOCKS address from an io.Reader.
To avoid allocations, call AppendFromReader directly.
func AddrPortFromSlice ¶ added in v1.1.0
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
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
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 ¶
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
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 ¶
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
ConnAddrFromReader reads a SOCKS address from r and returns the converted conn.Addr.
func ConnAddrFromSlice ¶ added in v1.1.0
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
LengthOfAddrFromAddrPort returns the length of a SOCKS address converted from the netip.AddrPort.
func LengthOfAddrFromConnAddr ¶ added in v1.1.0
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 ¶
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 ¶
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
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
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 (m UnsupportedAuthMethodError) Error() string
func (UnsupportedAuthMethodError) Is ¶ added in v1.12.0
func (UnsupportedAuthMethodError) Is(target error) bool
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 (v UnsupportedUsernamePasswordAuthVersionError) Error() string
func (UnsupportedUsernamePasswordAuthVersionError) Is ¶ added in v1.12.0
func (UnsupportedUsernamePasswordAuthVersionError) Is(target error) bool
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
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
AuthMsgLength returns the length of the authentication message for the username/password pair.