Help us shape the future of Go! Take the 2026 Go Developer Survey

socks

package
v3.61.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package socks provides SOCKS version 4/4a and 5 client implementations.

SOCKS protocol version 4 is described in:

https://www.openssh.org/txt/socks4.protocol

SOCKS protocol version 4a (domain name extension) is described in:

https://www.openssh.org/txt/socks4a.protocol

SOCKS protocol version 5 is defined in RFC 1928. Username/Password authentication for SOCKS version 5 is defined in RFC 1929.

Index

Constants

View Source
const (
	Version4 = 0x04
	Version5 = 0x05

	AddrTypeIPv4 = 0x01
	AddrTypeFQDN = 0x03
	AddrTypeIPv6 = 0x04

	CmdConnect Command = 0x01 // establishes an active-open forward proxy connection

	AuthMethodNotRequired         AuthMethod = 0x00 // no authentication required
	AuthMethodUsernamePassword    AuthMethod = 0x02 // use username/password
	AuthMethodNoAcceptableMethods AuthMethod = 0xff // no acceptable authentication methods

	// SOCKS5 reply codes
	StatusSucceeded Reply = 0x00

	// SOCKS4 reply codes
	Status4Granted        Reply = 90
	Status4Rejected       Reply = 91
	Status4IdentdFailed   Reply = 92
	Status4IdentdMismatch Reply = 93
)

Wire protocol constants.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addr

type Addr struct {
	Name string // fully-qualified domain name
	IP   net.IP
	Port int
}

An Addr represents a SOCKS-specific address. Either Name or IP is used exclusively.

func (*Addr) Network

func (a *Addr) Network() string

Network return "socks"

func (*Addr) String

func (a *Addr) String() string

type AuthMethod

type AuthMethod int

An AuthMethod represents a SOCKS authentication method.

type Command

type Command int

A Command represents a SOCKS command.

func (Command) String

func (cmd Command) String() string

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

A Conn represents a forward proxy connection.

func (*Conn) BoundAddr

func (c *Conn) BoundAddr() net.Addr

BoundAddr returns the address assigned by the proxy server for connecting to the command target address from the proxy server.

type Dialer

type Dialer struct {

	// Version specifies the SOCKS protocol version.
	// Supported values are Version4 and Version5.
	// If zero, Version5 is used.
	Version int

	// Socks4A enables the SOCKS4a extension so domain names are
	// resolved by the proxy instead of the client.
	// Only used when Version is Version4.
	Socks4A bool

	// UserID is the SOCKS4 user identity string sent to the proxy.
	// Only used when Version is Version4. Empty UserID is allowed.
	UserID string

	// ProxyDial specifies the optional dial function for
	// establishing the transport connection.
	ProxyDial func(context.Context, string, string) (net.Conn, error)

	// AuthMethods specifies the list of request authentication
	// methods. Only used when Version is Version5.
	// If empty, SOCKS client requests only AuthMethodNotRequired.
	AuthMethods []AuthMethod

	// Authenticate specifies the optional authentication
	// function. It must be non-nil when AuthMethods is not empty.
	// It must return an error when the authentication is failed.
	// Only used when Version is Version5.
	Authenticate func(context.Context, io.ReadWriter, AuthMethod) error
	// contains filtered or unexported fields
}

A Dialer holds SOCKS-specific options.

func NewDialer

func NewDialer(network, address string) *Dialer

NewDialer returns a new Dialer that dials through the provided proxy server's network and address using SOCKS5 by default.

func (*Dialer) DialContext

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

DialContext connects to the provided address on the provided network.

The returned error value may be a net.OpError. When the Op field of net.OpError contains "socks", the Source field contains a proxy server address and the Addr field contains a command target address.

See func Dial of the net package of standard library for a description of the network and address parameters.

func (*Dialer) DialWithConn

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

DialWithConn initiates a connection from SOCKS server to the target network and address using the connection c that is already connected to the SOCKS server.

It returns the connection's local address assigned by the SOCKS server.

type Reply

type Reply int

A Reply represents a SOCKS command reply code.

func (Reply) String

func (code Reply) String() string

type UsernamePassword

type UsernamePassword struct {
	Username string
	Password string
}

UsernamePassword are the credentials for the username/password authentication method.

func (*UsernamePassword) Authenticate

func (up *UsernamePassword) Authenticate(ctx context.Context, rw io.ReadWriter, auth AuthMethod) error

Authenticate authenticates a pair of username and password with the proxy server.

Jump to

Keyboard shortcuts

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