socks5

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CmdConnect      = 1
	CmdBind         = 2
	CmdUDPAssociate = 3
	CmdResolve      = 0xF0
	CmdResolvePTR   = 0xF1
)

Command codes (CMD) for client requests.

View Source
const (
	AddrTypeIPv4   = 1
	AddrTypeDomain = 3
	AddrTypeIPv6   = 4
)

Address types (ATYP) used in requests and responses.

View Source
const (
	RepSuccess              = 0
	RepGeneralFailure       = 1
	RepConnectionNotAllowed = 2
	RepNetworkUnreachable   = 3
	RepHostUnreachable      = 4
	RepConnectionRefused    = 5
	RepTTLExpired           = 6
	RepCommandNotSupported  = 7
	RepAddrTypeNotSupported = 8
)

Reply codes (REP) for server responses.

View Source
const (
	MethodNoAuth       = 0x00
	MethodGSSAPI       = 0x01
	MethodUserPass     = 0x02
	MethodNoAcceptable = 0xFF
)

Authentication methods (METHOD) for initial greeting.

View Source
const (
	GSSAPITypeInit  = 0x01
	GSSAPITypeReply = 0x02
	GSSAPITypeAbort = 0xFF
)

GSS-API message types (MTYP)

View Source
const (
	UserPassStatusSuccess = 0x00
	UserPassStatusFailure = 0x01
)

Username/password authentication status codes.

View Source
const (
	GSSAPIStatusSuccess = GSSAPITypeReply // 0x02
	GSSAPIStatusFailure = GSSAPITypeAbort // 0xFF
)

GSSAPI authentication status (using MsgType values).

View Source
const (
	AuthVersionUserPass = 1
)

Authentication sub-negotiation versions.

View Source
const (
	GSSAPIVersion = 1
)

GSS-API protocol version. (VER)

View Source
const (
	SocksVersion = 5
)

Protocol version.

Variables

View Source
var (
	ErrInvalidGSSAPIReplyVersion = errors.New("invalid GSSAPI reply version (must be 1)")
	ErrInvalidGSSAPIMsgType      = errors.New("invalid GSSAPI message type")
	ErrGSSAPIReplyTooLong        = errors.New("GSSAPI reply token too long (max 65535)")
)

Errors for GSSAPI authentication replies.

View Source
var (
	ErrInvalidGSSAPIVersion = errors.New("invalid GSSAPI version (must be 1)")
	ErrGSSAPITokenTooLong   = errors.New("GSSAPI token too long (max 65535)")
)

Errors for GSSAPI authentication requests.

View Source
var (
	ErrInvalidHandshakeVersion = errors.New("invalid SOCKS version (must be 5)")
	ErrTooManyMethods          = errors.New("too many authentication methods")
	ErrNoMethodsProvided       = errors.New("no authentication methods provided")
)

Errors for SOCKS5 handshake requests.

View Source
var (
	ErrInvalidReplyVersion = errors.New("invalid SOCKS version in reply (must be 5)")
	ErrInvalidReplyRSV     = errors.New("invalid reserved byte in reply (must be 0x00)")
	ErrInvalidReplyAddr    = errors.New("invalid address or address type in reply")
	ErrInvalidReplyDomain  = errors.New("invalid domain in reply (empty or too long)")
)

Common validation errors for replies.

View Source
var (
	ErrInvalidVersion = errors.New("invalid SOCKS version (must be 5)")
	ErrInvalidCommand = errors.New("invalid command (must be 1=CONNECT, 2=BIND, 3=UDP ASSOCIATE, F0=RESOLVE, or F1=RESOLVE_PTR)")
	ErrInvalidAddr    = errors.New("invalid address or address type")
	ErrInvalidDomain  = errors.New("invalid domain (empty or too long)")
	ErrInvalidRSV     = errors.New("invalid reserved byte (must be 0x00)")
)

Common validation errors.

View Source
var (
	ErrInvalidUDPReserved = errors.New("invalid UDP reserved bytes (must be 0x0000)")
	ErrUnsupportedFrag    = errors.New("unsupported UDP fragmentation (FRAG must be 0x00)")
	ErrInvalidUDPAddrType = errors.New("invalid UDP address type")
	ErrInvalidUDPDomain   = errors.New("invalid UDP domain (empty or too long)")
	ErrMissingUDPData     = errors.New("missing UDP payload data")
)

Common validation errors for UDP packets.

View Source
var (
	ErrInvalidUserPassVersion = errors.New("invalid user/password auth version (must be 1)")
	ErrEmptyUserPassUsername  = errors.New("username cannot be empty")
	ErrEmptyUserPassPassword  = errors.New("password cannot be empty")
	ErrUserPassTooLong        = errors.New("username or password too long (max 255)")
)

Errors for username/password authentication requests.

View Source
var (
	ErrInvalidHandshakeReplyVersion = errors.New("invalid SOCKS version in handshake reply (must be 5)")
)

Errors for SOCKS5 handshake replies.

View Source
var (
	ErrInvalidUserPassReplyVersion = errors.New("invalid user/password reply version (must be 1)")
)

Errors for username/password authentication replies.

Functions

func BaseOnBind

func BaseOnBind(ctx context.Context, conn net.Conn, req *Request, acceptTimeout, connTimeout time.Duration, bufferSize int) error

BaseOnBind provides BIND implementation

func BaseOnConnect

func BaseOnConnect(ctx context.Context, conn net.Conn, req *Request, dialer socksnet.Dialer, connTimeout time.Duration, bufferSize int) error

BaseOnConnect provides CONNECT implementation

func BaseOnHandshake

func BaseOnHandshake(ctx context.Context, conn net.Conn, req *HandshakeRequest, supportedMethods []byte) (byte, error)

BaseOnHandshake provides a default handshake implementation that selects the first matching authentication method.

func BaseOnRequest

func BaseOnRequest(ctx context.Context, handler ServerHandler, conn net.Conn, req *Request) error

BaseOnRequest provides request handling logic for CONNECT, BIND, UDP ASSOCIATE, and RESOLVE commands.

func BaseOnResolve

func BaseOnResolve(
	ctx context.Context,
	conn net.Conn,
	req *Request,
	dialer socksnet.Dialer, resolver *net.Resolver, preferIPv4 bool,
	connTimeout time.Duration,
	bufferSize int,
) error

BaseOnResolve provides RESOLVE implementation

func BaseOnUDPAssociate

func BaseOnUDPAssociate(
	ctx context.Context,
	conn net.Conn,
	req *Request,
	timeout time.Duration,
	bufferSize int,
	laddr *net.UDPAddr,
) error

BaseOnUDPAssociate provides UDP ASSOCIATE implementation

func ListenAndServe

func ListenAndServe(ctx context.Context, network, address string, handler ServerHandler) error

ListenAndServe listens on the network address and serves SOCKS5 requests.

func ResolveSelectBestIP

func ResolveSelectBestIP(ips []net.IP, preferIPv4 bool) net.IP

ResolveSelectBestIP selects the most appropriate IP address from a list based on preferences

func Serve

func Serve(ctx context.Context, listener net.Listener, handler ServerHandler) error

Serve accepts incoming connections on the listener and serves SOCKS5 requests.

func ServeConn

func ServeConn(ctx context.Context, handler ServerHandler, conn net.Conn) error

ServeConn handles a single client connection, including handshake, authentication, and request processing.

func WriteHandshake

func WriteHandshake(conn net.Conn, code byte) error

WriteHandshake sends a SOCKS5 handshake reply with the given code.

func WriteRejectReply

func WriteRejectReply(conn net.Conn, code byte)

WriteRejectReply sends a SOCKS5 reply with the given rejection code.

func WriteSuccessReply

func WriteSuccessReply(conn net.Conn, addr net.Addr) error

WriteSuccessReply writes a SOCKS5 success reply with the given network address.

Types

type Auth

type Auth struct {
	Username string
	Password string
}

Auth holds username/password credentials for SOCKS5 authentication.

type BaseServerHandler

type BaseServerHandler struct {
	Dialer socksnet.Dialer

	RequestTimeout         time.Duration
	BindAcceptTimeout      time.Duration
	BindConnTimeout        time.Duration
	ConnectConnTimeout     time.Duration
	UDPAssociateTimeout    time.Duration
	ConnectBufferSize      int
	UDPAssociateBufferSize int
	AllowConnect           bool
	AllowBind              bool
	AllowUDPAssociate      bool
	AllowResolve           bool
	ResolveResolver        *net.Resolver
	ResolvePreferIPv4      bool // When true, prefer IPv4 addresses over IPv6 for DNS resolution

	SupportedMethods []byte

	UserPassAuthenticator func(ctx context.Context, username, password string) error
	GSSAPIAuthenticator   func(ctx context.Context, token []byte) (resp []byte, done bool, err error)
	UDPAssociateLocalAddr func(ctx context.Context, conn net.Conn, req *Request) (*net.UDPAddr, error)
}

BaseServerHandler provides a basic implementation of ServerHandler with configurable options.

func (*BaseServerHandler) GetSupportedMethods

func (d *BaseServerHandler) GetSupportedMethods() []byte

GetSupportedMethods returns the supported authentication methods.

func (*BaseServerHandler) OnAccept

func (d *BaseServerHandler) OnAccept(ctx context.Context, conn net.Conn) error

func (*BaseServerHandler) OnAuthGSSAPI

func (d *BaseServerHandler) OnAuthGSSAPI(ctx context.Context, conn net.Conn, token []byte) ([]byte, bool, error)

func (*BaseServerHandler) OnAuthUserPass

func (d *BaseServerHandler) OnAuthUserPass(ctx context.Context, conn net.Conn, username, password string) error

func (*BaseServerHandler) OnBind

func (d *BaseServerHandler) OnBind(ctx context.Context, conn net.Conn, req *Request) error

func (*BaseServerHandler) OnConnect

func (d *BaseServerHandler) OnConnect(ctx context.Context, conn net.Conn, req *Request) error

func (*BaseServerHandler) OnError

func (d *BaseServerHandler) OnError(ctx context.Context, conn net.Conn, err error)

func (*BaseServerHandler) OnHandshake

func (d *BaseServerHandler) OnHandshake(ctx context.Context, conn net.Conn, req *HandshakeRequest) (byte, error)

func (*BaseServerHandler) OnPanic

func (d *BaseServerHandler) OnPanic(ctx context.Context, conn net.Conn, r any)

func (*BaseServerHandler) OnRequest

func (d *BaseServerHandler) OnRequest(ctx context.Context, conn net.Conn, req *Request) error

func (*BaseServerHandler) OnResolve

func (d *BaseServerHandler) OnResolve(ctx context.Context, conn net.Conn, req *Request) error

func (*BaseServerHandler) OnUDPAssociate

func (d *BaseServerHandler) OnUDPAssociate(ctx context.Context, conn net.Conn, req *Request) error

type Dialer

type Dialer struct {
	ProxyAddr  string
	Auth       *Auth
	GSSAPIAuth *GSSAPIAuth
	Dialer     socksnet.Dialer
}

Dialer implements a SOCKS5 proxy dialer.

func NewDialer

func NewDialer(proxyAddr string, auth *Auth, dialer socksnet.Dialer) *Dialer

NewDialer creates a new SOCKS5 dialer instance.

func NewDialerWithGSSAPI

func NewDialerWithGSSAPI(proxyAddr string, auth *Auth, gssapiAuth *GSSAPIAuth, dialer socksnet.Dialer) *Dialer

NewDialerWithGSSAPI creates a new SOCKS5 dialer instance with GSSAPI support.

func (*Dialer) Bind

func (d *Dialer) Bind(network, address string) (net.Conn, *net.TCPAddr, <-chan error, error)

Bind establishes a passive BIND connection using background context.

func (*Dialer) BindContext

func (d *Dialer) BindContext(
	ctx context.Context,
	network, address string,
) (net.Conn, *net.TCPAddr, <-chan error, error)

BindContext establishes a passive BIND connection via SOCKS5 proxy.

func (*Dialer) Dial

func (d *Dialer) Dial(network, address string) (net.Conn, error)

Dial establishes a connection via SOCKS5 proxy using background context.

func (*Dialer) DialConn

func (d *Dialer) DialConn(conn net.Conn, network, address string) (net.Conn, error)

DialConn upgrades an existing connection using background context.

func (*Dialer) DialConnContext

func (d *Dialer) DialConnContext(ctx context.Context, conn net.Conn, network, address string) (net.Conn, error)

DialConnContext upgrades an existing connection via SOCKS5 proxy (CONNECT command).

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext establishes a connection via SOCKS5 proxy (CONNECT command).

func (*Dialer) ListenPacket

func (d *Dialer) ListenPacket(ctx context.Context, network string, laddr *net.UDPAddr) (net.PacketConn, error)

ListenPacket establishes a UDP association and returns a PacketConn for sending/receiving UDP packets via the SOCKS5 proxy.

func (*Dialer) ProxyAddress

func (d *Dialer) ProxyAddress() string

ProxyAddress returns the configured SOCKS5 proxy address.

func (*Dialer) ResolveContext

func (d *Dialer) ResolveContext(ctx context.Context, network, host string) (net.IP, error)

ResolveContext resolves a hostname via SOCKS5 proxy (Tor-style extension).

func (*Dialer) UDPAssociate

func (d *Dialer) UDPAssociate(network string, clientAddr *net.UDPAddr) (net.Conn, *net.UDPAddr, error)

UDPAssociate establishes a UDP association using background context.

func (*Dialer) UDPAssociateContext

func (d *Dialer) UDPAssociateContext(
	ctx context.Context,
	network string,
	clientAddr *net.UDPAddr,
) (net.Conn, *net.UDPAddr, error)

UDPAssociateContext establishes a UDP association via SOCKS5 proxy.

type GSSAPIAuth

type GSSAPIAuth struct {
	Context GSSAPIContext
}

GSSAPIAuth holds GSSAPI authentication context.

type GSSAPIContext

type GSSAPIContext interface {
	// InitSecContext generates initial GSSAPI token
	InitSecContext() ([]byte, error)
	// AcceptSecContext processes server tokens and generates response
	// Returns: (responseToken, authComplete, error)
	AcceptSecContext(serverToken []byte) ([]byte, bool, error)
	// IsComplete returns true when authentication is finished
	IsComplete() bool
}

GSSAPIContext interface for GSSAPI authentication operations.

type GSSAPIReply

type GSSAPIReply struct {
	Version byte   // VER (should always be 0x01)
	MsgType byte   // MTYP (0x02 = reply token, 0xFF = failure)
	Token   []byte // TOKEN (optional; may be empty for final success)
}

GSSAPIReply represents a GSSAPI authentication reply message (RFC 1961 §3.7).

func (*GSSAPIReply) Init

func (r *GSSAPIReply) Init(version, msgType byte, token []byte)

Init initializes the GSSAPI reply.

func (*GSSAPIReply) ReadFrom

func (r *GSSAPIReply) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a GSSAPI reply from a reader.

func (*GSSAPIReply) String

func (r *GSSAPIReply) String() string

String returns a human-readable representation.

func (*GSSAPIReply) Validate

func (r *GSSAPIReply) Validate() error

Validate checks for protocol correctness.

func (*GSSAPIReply) WriteTo

func (r *GSSAPIReply) WriteTo(dst io.Writer) (int64, error)

WriteTo writes the GSSAPI reply to a writer.

type GSSAPIRequest

type GSSAPIRequest struct {
	Version byte   // VER (should always be 0x01)
	MsgType byte   // MTYP (0x01 = initial token)
	Token   []byte // TOKEN (opaque GSSAPI token)
}

GSSAPIRequest represents a GSSAPI authentication request (RFC 1961 §3.4).

func (*GSSAPIRequest) Init

func (r *GSSAPIRequest) Init(version, msgType byte, token []byte)

Init initializes a GSSAPI authentication request.

func (*GSSAPIRequest) ReadFrom

func (r *GSSAPIRequest) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a GSSAPI authentication request from a reader.

func (*GSSAPIRequest) String

func (r *GSSAPIRequest) String() string

String returns a human-readable representation.

func (*GSSAPIRequest) Validate

func (r *GSSAPIRequest) Validate() error

Validate checks for protocol correctness.

func (*GSSAPIRequest) WriteTo

func (r *GSSAPIRequest) WriteTo(dst io.Writer) (int64, error)

WriteTo writes the GSSAPI authentication request to a writer.

type HandshakeReply

type HandshakeReply struct {
	Version byte // VER (should always be 0x05)
	Method  byte // METHOD; selected authentication method
}

HandshakeReply represents the server’s response to a SOCKS5 handshake request.

func (*HandshakeReply) Init

func (h *HandshakeReply) Init(version byte, method byte)

Init initializes a handshake reply with the given method.

func (*HandshakeReply) ReadFrom

func (h *HandshakeReply) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a SOCKS5 handshake reply from an io.Reader. Implements io.ReaderFrom.

func (*HandshakeReply) String

func (h *HandshakeReply) String() string

String returns a human-readable representation of the handshake reply.

func (*HandshakeReply) Validate

func (h *HandshakeReply) Validate() error

Validate ensures the handshake reply is valid.

func (*HandshakeReply) WriteTo

func (h *HandshakeReply) WriteTo(dst io.Writer) (int64, error)

WriteTo writes the handshake reply to an io.Writer. Implements io.WriterTo.

type HandshakeRequest

type HandshakeRequest struct {
	Version  byte   // VER (should always be 0x05)
	NMethods byte   // NMETHODS; number of methods
	Methods  []byte // METHODS; list of supported methods
}

HandshakeRequest represents the initial SOCKS5 client handshake (method negotiation).

func (*HandshakeRequest) Init

func (h *HandshakeRequest) Init(version byte, methods ...byte)

Init initializes a handshake request with the given methods.

func (*HandshakeRequest) ReadFrom

func (h *HandshakeRequest) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a SOCKS5 handshake request from an io.Reader. Implements io.ReaderFrom.

func (*HandshakeRequest) String

func (h *HandshakeRequest) String() string

String returns a human-readable representation of the handshake request.

func (*HandshakeRequest) Validate

func (h *HandshakeRequest) Validate() error

Validate ensures the handshake request is structurally valid.

func (*HandshakeRequest) WriteTo

func (h *HandshakeRequest) WriteTo(dst io.Writer) (int64, error)

WriteTo writes the handshake request to an io.Writer. Implements io.WriterTo.

type Reply

type Reply struct {
	Version  byte   // VER; SOCKS protocol version (always 5)
	Reply    byte   // REP; reply code
	Reserved byte   // RSV; must be 0x00
	AddrType byte   // ATYP; address type (IPv4, DOMAIN, IPv6)
	IP       net.IP // BND.ADDR; Bound IP (if IPv4/IPv6)
	Domain   string // BND.ADDR; Bound domain (if ATYP=DOMAIN)
	Port     uint16 // BND.PORT; Bound port
}

Reply represents a SOCKS5 server reply.

func (*Reply) Addr

func (r *Reply) Addr() string

Addr returns a combined "host:port" string.

func (*Reply) GetHost

func (r *Reply) GetHost() string

GetHost returns the bound host (domain or IP string).

func (*Reply) Init

func (r *Reply) Init(version, rep, reserved, addrType byte, ip net.IP, domain string, port uint16)

Init initializes a SOCKS5 reply.

func (*Reply) ReadFrom

func (r *Reply) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a SOCKS5 reply from a Reader. Implements io.ReaderFrom.

func (*Reply) String

func (r *Reply) String() string

String returns a human-readable representation of the reply.

func (*Reply) Validate

func (r *Reply) Validate() error

Validate validates the full reply.

func (*Reply) ValidateHeader

func (r *Reply) ValidateHeader() error

ValidateHeader validates the reply header fields.

func (*Reply) WriteTo

func (r *Reply) WriteTo(dst io.Writer) (int64, error)

WriteTo writes a SOCKS5 reply to a Writer. Implements io.WriterTo.

type Request

type Request struct {
	Version  byte   // VER; SOCKS protocol version (always 5)
	Command  byte   // CMD; CONNECT, BIND, UDP ASSOCIATE, RESOLVE, etc.
	Reserved byte   // RSV; reserved byte (must be 0x00)
	AddrType byte   // ATYP; address type (IPv4, DOMAIN, IPv6)
	IP       net.IP // BND.ADDR; Destination IP (IPv4 or IPv6)
	Domain   string // BND.ADDR; Destination domain (if ATYP=DOMAIN)
	Port     uint16 // BND.PORT; Destination port (big-endian)
}

Request represents a SOCKS5 CONNECT/BIND/UDP ASSOCIATE/RESOLVE request.

func (*Request) Addr

func (r *Request) Addr() string

Addr returns the full "host:port" string form.

func (*Request) GetHost

func (r *Request) GetHost() string

GetHost returns the destination hostname or IP string.

func (*Request) Init

func (r *Request) Init(
	version byte,
	command byte,
	reserved byte,
	addrType byte,
	ip net.IP,
	domain string,
	port uint16,
)

Init initializes a SOCKS5 request.

func (*Request) ReadFrom

func (r *Request) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a SOCKS5 request from a Reader. Implements the io.ReaderFrom interface.

func (*Request) String

func (r *Request) String() string

String returns a string representation of the SOCKS5 Request.

func (*Request) Validate

func (r *Request) Validate() error

Validate validates the full SOCKS5 request.

func (*Request) ValidateHeader

func (r *Request) ValidateHeader() error

ValidateHeader validates the SOCKS5 request header.

func (*Request) WriteTo

func (r *Request) WriteTo(dst io.Writer) (int64, error)

WriteTo writes a SOCKS5 request to a Writer. Implements the io.WriterTo interface.

type ServerHandler

type ServerHandler interface {
	// OnAccept is called for each accepted connection.
	OnAccept(ctx context.Context, conn net.Conn) error

	// OnHandshake is called during method negotiation phase.
	OnHandshake(ctx context.Context, conn net.Conn, req *HandshakeRequest) (selectedMethod byte, err error)

	// OnAuthUserPass is called for username/password authentication.
	OnAuthUserPass(ctx context.Context, conn net.Conn, username, password string) error

	// OnAuthGSSAPI is called for GSSAPI authentication.
	OnAuthGSSAPI(ctx context.Context, conn net.Conn, token []byte) (resp []byte, done bool, err error)

	// OnRequest is called for each SOCKS5 request after successful handshake/auth.
	OnRequest(ctx context.Context, conn net.Conn, req *Request) error

	// OnConnect is called for each CONNECT request.
	OnConnect(ctx context.Context, conn net.Conn, req *Request) error

	// OnBind is called for each BIND request.
	OnBind(ctx context.Context, conn net.Conn, req *Request) error

	// OnUDPAssociate is called for each UDP ASSOCIATE request.
	OnUDPAssociate(ctx context.Context, conn net.Conn, req *Request) error

	// OnResolve is called for each RESOLVE request.
	OnResolve(ctx context.Context, conn net.Conn, req *Request) error

	// OnError is called for each connection error.
	OnError(ctx context.Context, conn net.Conn, err error)

	// OnPanic is called when a panic occurs in any handler goroutine.
	OnPanic(ctx context.Context, conn net.Conn, r any)
}

ServerHandler handles SOCKS5 server events.

var DefaultServerHandler ServerHandler = &BaseServerHandler{
	RequestTimeout:         10 * time.Second,
	BindAcceptTimeout:      10 * time.Second,
	BindConnTimeout:        60 * time.Second,
	ConnectConnTimeout:     60 * time.Second,
	UDPAssociateTimeout:    300 * time.Second,
	ConnectBufferSize:      1024 * 32,
	UDPAssociateBufferSize: 1024 * 64,
	AllowConnect:           true,
	AllowBind:              false,
	AllowUDPAssociate:      false,
	SupportedMethods:       []byte{MethodNoAuth},
	UserPassAuthenticator:  nil,
	GSSAPIAuthenticator:    nil,
}

DefaultServerHandler is a default implementation used when no custom ServerHandler is provided to Serve or ListenAndServe.

type UDPConn

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

UDPConn is a net.PacketConn that wraps the UDP socket used for SOCKS5 UDP ASSOCIATE, allowing it to be used with standard Go APIs.

func NewUDPConn

func NewUDPConn(tcpConn net.Conn, udpConn *net.UDPConn, relayAddr *net.UDPAddr) *UDPConn

NewUDPConn creates a new UDPConn for the given TCP control connection, UDP socket, and proxy relay address.

func (*UDPConn) Close

func (c *UDPConn) Close() error

Close implements net.PacketConn.

func (*UDPConn) LocalAddr

func (c *UDPConn) LocalAddr() net.Addr

LocalAddr implements net.PacketConn.

func (*UDPConn) ReadFrom

func (c *UDPConn) ReadFrom(p []byte) (int, net.Addr, error)

ReadFrom implements net.PacketConn.

func (*UDPConn) SetDeadline

func (c *UDPConn) SetDeadline(t time.Time) error

SetDeadline implements net.PacketConn.

func (*UDPConn) SetReadDeadline

func (c *UDPConn) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.PacketConn.

func (*UDPConn) SetWriteDeadline

func (c *UDPConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.PacketConn.

func (*UDPConn) WriteTo

func (c *UDPConn) WriteTo(p []byte, addr net.Addr) (int, error)

WriteTo implements net.PacketConn.

type UDPPacket

type UDPPacket struct {
	Reserved [2]byte // RSV; must be 0x0000
	Frag     byte    // FRAG; must be 0x00 (no fragmentation)
	AddrType byte    // ATYP; IPv4, DOMAIN, or IPv6
	IP       net.IP  // Destination IP (if ATYP=IPv4 or IPv6)
	Domain   string  // Destination domain (if ATYP=DOMAIN)
	Port     uint16  // Destination port
	Data     []byte  // UDP payload data
}

UDPPacket represents a SOCKS5 UDP ASSOCIATE packet.

func (*UDPPacket) Init

func (p *UDPPacket) Init(
	reserved [2]byte,
	frag byte,
	addrType byte,
	ip net.IP,
	domain string,
	port uint16,
	data []byte,
)

Init initializes a UDPPacket with given values.

func (*UDPPacket) MarshalTo

func (p *UDPPacket) MarshalTo(b []byte) (int, error)

MarshalTo writes the packet into b and returns bytes written.

func (*UDPPacket) Size

func (p *UDPPacket) Size() int

func (*UDPPacket) String

func (p *UDPPacket) String() string

String returns a human-readable representation.

func (*UDPPacket) UnmarshalFrom

func (p *UDPPacket) UnmarshalFrom(b []byte) (int, error)

UnmarshalFrom parses a SOCKS5 UDP packet from raw bytes.

func (*UDPPacket) Validate

func (p *UDPPacket) Validate() error

Validate checks for protocol correctness.

func (*UDPPacket) ValidateHeader

func (p *UDPPacket) ValidateHeader() error

ValidateHeader checks RSV/FRAG/ATYP fields before full read.

type UserPassReply

type UserPassReply struct {
	Version byte // VER (should be AuthVersionUserPass = 0x01)
	Status  byte // STATUS (0x00 = success, otherwise failure)
}

UserPassReply represents a username/password authentication reply.

func (*UserPassReply) Init

func (r *UserPassReply) Init(version, status byte)

Init initializes a user/password authentication reply with the given version and status.

func (*UserPassReply) ReadFrom

func (r *UserPassReply) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a username/password authentication reply from an io.Reader. Implements io.ReaderFrom.

func (*UserPassReply) String

func (r *UserPassReply) String() string

String returns a human-readable representation.

func (*UserPassReply) Success

func (r *UserPassReply) Success() bool

Success returns true if STATUS == 0x00.

func (*UserPassReply) Validate

func (r *UserPassReply) Validate() error

Validate ensures the reply is structurally valid.

func (*UserPassReply) WriteTo

func (r *UserPassReply) WriteTo(dst io.Writer) (int64, error)

WriteTo writes the authentication reply to an io.Writer. Implements io.WriterTo. Note: assumes the struct is already valid.

type UserPassRequest

type UserPassRequest struct {
	Version  byte   // VER (should always be AuthVersionUserPass = 0x01)
	Username string // UNAME (1–255 bytes)
	Password string // PASSWD (1–255 bytes)
}

UserPassRequest represents a username/password authentication request.

func (*UserPassRequest) Init

func (r *UserPassRequest) Init(version byte, username, password string)

Init initializes the authentication request with username and password.

func (*UserPassRequest) ReadFrom

func (r *UserPassRequest) ReadFrom(src io.Reader) (int64, error)

ReadFrom reads a username/password authentication request from a reader. Implements io.ReaderFrom.

func (*UserPassRequest) String

func (r *UserPassRequest) String() string

String returns a human-readable representation.

func (*UserPassRequest) Validate

func (r *UserPassRequest) Validate() error

Validate checks for protocol correctness.

func (*UserPassRequest) WriteTo

func (r *UserPassRequest) WriteTo(dst io.Writer) (int64, error)

WriteTo writes the username/password request to a writer. Implements io.WriterTo.

Jump to

Keyboard shortcuts

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