proxy

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package proxy connects through SOCKS5, SOCKS4 and HTTP CONNECT proxies, bounded by the caller's context, and carries UDP datagrams through SOCKS5 UDP associations (Association).

Index

Constants

View Source
const (
	MethodNone = 0
	MethodUser = 2
)

SOCKS5 (RFC 1928, RFC 1929) values.

Variables

View Source
var ErrAnswerTooLarge = errors.New("the proxy's answer header is longer than 64 KiB")

ErrAnswerTooLarge is returned for a CONNECT answer whose header is longer than 64 KiB.

View Source
var ErrAssociationEnded = errors.New("the SOCKS5 proxy ended the UDP association: its control connection closed")

ErrAssociationEnded is the error of an Association's reads and writes once its control connection has closed or failed: the proxy has dropped the association (RFC 1928, section 7).

Functions

func SOCKS4 added in v0.5.0

func SOCKS4(ctx context.Context, conn net.Conn, req []byte) error

SOCKS4 sends req, a SOCKS4Request, to a SOCKS4 proxy over conn and reads its answer: nil once the proxy grants the connection.

func SOCKS4Request added in v0.5.0

func SOCKS4Request(target, userID string, hostnameIP [4]byte) ([]byte, error)

SOCKS4Request is the CONNECT request for target ("host:port") with userID in USERID: VN, CD, DSTPORT, DSTIP, USERID and NUL. An IPv4 address goes out as DSTIP; a host name as SOCKS4A, DSTIP hostnameIP (0.0.0.x, the browser's) and the name and NUL after USERID, for the proxy to resolve. SOCKS4 carries no IPv6 address, and NUL ends its fields: either is an error, found before anything is dialled.

func SOCKS5

func SOCKS5(ctx context.Context, conn net.Conn, target string, methods []byte, user *url.Userinfo) error

SOCKS5 asks a SOCKS5 proxy over conn for a connection to target, offering methods in order; host names are resolved by the proxy. user answers username/password authentication.

func UDPAssociate

func UDPAssociate(ctx context.Context, conn net.Conn, methods []byte, user *url.Userinfo) (string, error)

UDPAssociate asks a SOCKS5 proxy over conn, which becomes the association's control connection, for a UDP association (RFC 1928, section 7), offering methods and authenticating as SOCKS5 does. It returns the relay's address as the proxy gives it ("host:port"); an unspecified IP there is the proxy's own address, which the caller substitutes. A refusal is a *RefusedError.

Types

type Answer

type Answer struct {
	StatusCode int
	// Status is the status line's code and text ("407 Proxy
	// Authentication Required").
	Status string
	Header http.Header
	// Close reports that the proxy closes the connection after it.
	Close bool
}

Answer is a proxy's response to CONNECT, its body read.

func Connect

func Connect(ctx context.Context, conn net.Conn, target string, header []string) (Answer, net.Conn, error)

Connect asks an HTTP proxy over conn for a tunnel to target ("host:port") with header lines sent as given. On 2xx the returned conn is the tunnel; otherwise conn stays usable unless the answer closes it.

type Association

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

Association is a SOCKS5 UDP association carrying one connection's datagrams to one target. It is a net.PacketConn: a write goes to the target whatever address it names, behind the SOCKS5 UDP header, and a read is a datagram from the relay without it, reported from the peer address given to NewAssociation. Its socket is connected to the relay, so nothing it sends can go anywhere else. Fragments (FRAG other than 0) and malformed datagrams are dropped. It ends with its control connection: once that closes or fails, reads and writes fail with ErrAssociationEnded. Close closes both.

func NewAssociation

func NewAssociation(ctrl, udp net.Conn, host string, port uint16, peer netip.AddrPort) (*Association, error)

NewAssociation starts an association over udp, a socket connected to the relay, for the target host at port: an IP address goes as one, a host name as one for the proxy to resolve. ctrl is the control connection UDPAssociate set it up on; peer is the source address reads report.

func (*Association) Close

func (a *Association) Close() error

Close ends the association: its control connection and its socket.

func (*Association) LocalAddr

func (a *Association) LocalAddr() net.Addr

LocalAddr is the socket's local address.

func (*Association) ReadFrom

func (a *Association) ReadFrom(p []byte) (int, net.Addr, error)

ReadFrom reads the target's next datagram into p.

func (*Association) SetDeadline

func (a *Association) SetDeadline(t time.Time) error

SetDeadline sets the socket's read and write deadlines.

func (*Association) SetReadDeadline

func (a *Association) SetReadDeadline(t time.Time) error

SetReadDeadline sets the socket's read deadline.

func (*Association) SetWriteDeadline

func (a *Association) SetWriteDeadline(t time.Time) error

SetWriteDeadline sets the socket's write deadline.

func (*Association) WriteTo

func (a *Association) WriteTo(b []byte, _ net.Addr) (int, error)

WriteTo sends b to the association's target; addr is ignored.

type RefusedError

type RefusedError struct {
	// UDP: the refused request was UDP ASSOCIATE.
	UDP bool
	// Reply is the reply code (RFC 1928, section 6).
	Reply byte
}

RefusedError is a SOCKS5 proxy's refusal of a request: a reply other than "succeeded" to CONNECT or UDP ASSOCIATE.

func (*RefusedError) Error

func (e *RefusedError) Error() string

Jump to

Keyboard shortcuts

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