socks

package module
v0.0.0-...-cb6bca4 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Go Networking

Go Reference

This repository holds supplementary Go networking packages.

Report Issues / Send Patches

This repository uses Gerrit for code changes. To learn how to submit changes to this repository, see https://go.dev/doc/contribute.

The git repository is https://go.googlesource.com/net.

The main issue tracker for the net repository is located at https://go.dev/issues. Prefix your issue with "x/net:" in the subject line, so it is easy to find.

Documentation

Overview

Package proxy provides support for a variety of protocols to proxy network data.

Package socks provides a SOCKS version 5 client implementation.

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

Package sockstest provides utilities for SOCKS testing.

Index

Constants

View Source
const (
	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

	StatusSucceeded Reply = 0x00
)

Wire protocol constants.

Variables

View Source
var Direct = direct{}

Direct implements Dialer by making network connections directly using net.Dial or net.DialContext.

Functions

func Dial

func Dial(ctx context.Context, network, address string) (net.Conn, error)

Dial works like DialContext on net.Dialer but using a dialer returned by FromEnvironment.

The passed ctx is only used for returning the Conn, not the lifetime of the Conn.

Custom dialers (registered via RegisterDialerType) that do not implement ContextDialer can leak a goroutine for as long as it takes the underlying Dialer implementation to timeout.

A Conn returned from a successful Dial after the context has been cancelled will be immediately closed.

func MarshalAuthReply

func MarshalAuthReply(ver int, m AuthMethod) ([]byte, error)

MarshalAuthReply returns an authentication reply in wire format.

func MarshalCmdReply

func MarshalCmdReply(ver int, reply Reply, a *Addr) ([]byte, error)

MarshalCmdReply returns a command reply in wire format.

func NoAuthRequired

func NoAuthRequired(rw io.ReadWriter, b []byte) error

NoAuthRequired handles a no-authentication-required signaling.

func NoProxyRequired

func NoProxyRequired(rw io.ReadWriter, b []byte) error

NoProxyRequired handles a command signaling without constructing a proxy connection to the final destination.

func RegisterDialerType

func RegisterDialerType(scheme string, f func(*url.URL, Dialer) (Dialer, error))

RegisterDialerType takes a URL scheme and a function to generate Dialers from a URL with that scheme and a forwarding Dialer. Registered schemes are used by FromURL.

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

func (*Addr) String

func (a *Addr) String() string

type Auth

type Auth struct {
	User, Password string
}

Auth contains authentication parameters that specific Dialers may require.

type AuthMethod

type AuthMethod int

An AuthMethod represents a SOCKS authentication method.

type AuthRequest

type AuthRequest struct {
	Version int
	Methods []AuthMethod
}

An AuthRequest represents an authentication request.

func ParseAuthRequest

func ParseAuthRequest(b []byte) (*AuthRequest, error)

ParseAuthRequest parses an authentication request.

type CmdRequest

type CmdRequest struct {
	Version int
	Cmd     Command
	Addr    Addr
}

A CmdRequest represents a command request.

func ParseCmdRequest

func ParseCmdRequest(b []byte) (*CmdRequest, error)

ParseCmdRequest parses a command request.

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 ContextDialer

type ContextDialer interface {
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

A ContextDialer dials using a context.

type Dialer

type Dialer interface {
	// Dial connects to the given address via the proxy.
	Dial(network, addr string) (c net.Conn, err error)
}

A Dialer is a means to establish a connection. Custom dialers should also implement ContextDialer.

func FromEnvironment

func FromEnvironment() Dialer

FromEnvironment returns the dialer specified by the proxy-related variables in the environment and makes underlying connections directly.

func FromEnvironmentUsing

func FromEnvironmentUsing(forward Dialer) Dialer

FromEnvironmentUsing returns the dialer specify by the proxy-related variables in the environment and makes underlying connections using the provided forwarding Dialer (for instance, a *net.Dialer with desired configuration).

type PerHost

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

A PerHost directs connections to a default Dialer unless the host name requested matches one of a number of exceptions.

func NewPerHost

func NewPerHost(defaultDialer, bypass Dialer) *PerHost

NewPerHost returns a PerHost Dialer that directs connections to either defaultDialer or bypass, depending on whether the connection matches one of the configured rules.

func (*PerHost) AddFromString

func (p *PerHost) AddFromString(s string)

AddFromString parses a string that contains comma-separated values specifying hosts that should use the bypass proxy. Each value is either an IP address, a CIDR range, a zone (*.example.com) or a host name (localhost). A best effort is made to parse the string and errors are ignored.

func (*PerHost) AddHost

func (p *PerHost) AddHost(host string)

AddHost specifies a host name that will use the bypass proxy.

func (*PerHost) AddIP

func (p *PerHost) AddIP(ip net.IP)

AddIP specifies an IP address that will use the bypass proxy. Note that this will only take effect if a literal IP address is dialed. A connection to a named host will never match an IP.

func (*PerHost) AddNetwork

func (p *PerHost) AddNetwork(net *net.IPNet)

AddNetwork specifies an IP range that will use the bypass proxy. Note that this will only take effect if a literal IP address is dialed. A connection to a named host will never match.

func (*PerHost) AddZone

func (p *PerHost) AddZone(zone string)

AddZone specifies a DNS suffix that will use the bypass proxy. A zone of "example.com" matches "example.com" and all of its subdomains.

func (*PerHost) Dial

func (p *PerHost) Dial(network, addr string) (c net.Conn, err error)

Dial connects to the address addr on the given network through either defaultDialer or bypass.

func (*PerHost) DialContext

func (p *PerHost) DialContext(ctx context.Context, network, addr string) (c net.Conn, err error)

DialContext connects to the address addr on the given network through either defaultDialer or bypass.

type Reply

type Reply int

A Reply represents a SOCKS command reply code.

func (Reply) String

func (code Reply) String() string

type Server

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

A Server represents a server for handshake testing.

func NewServer

func NewServer(authFunc, cmdFunc func(io.ReadWriter, []byte) error) (*Server, error)

NewServer returns a new server.

The provided authFunc and cmdFunc must parse requests and return appropriate replies to clients.

func (*Server) Addr

func (s *Server) Addr() net.Addr

Addr returns a server address.

func (*Server) Close

func (s *Server) Close() error

Close closes the server.

func (*Server) TargetAddr

func (s *Server) TargetAddr() net.Addr

TargetAddr returns a fake final destination address.

The returned address is only valid for testing with Server.

type SocksDialer

type SocksDialer struct {
	ProxyNetwork string // network between a proxy server and a client
	ProxyAddress string // proxy server address

	// 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.
	// 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.
	Authenticate func(context.Context, io.ReadWriter, AuthMethod) error
	// contains filtered or unexported fields
}

A SocksDialer holds SOCKS-specific options.

func FromURL

func FromURL(u *url.URL, forward Dialer) (*SocksDialer, error)

FromURL returns a Dialer given a URL specification and an underlying Dialer for it to make network requests.

func NewSocksDialer

func NewSocksDialer(network, address string) *SocksDialer

NewSocksDialer returns a new Dialer that dials through the provided proxy server's network and address.

func SOCKS5

func SOCKS5(network, address string, auth *Auth, forward Dialer) (*SocksDialer, error)

SOCKS5 returns a Dialer that makes SOCKSv5 connections to the given address with an optional username and password. See RFC 1928 and RFC 1929.

func (*SocksDialer) Dial deprecated

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

Dial connects to the provided address on the provided network.

Unlike DialContext, it returns a raw transport connection instead of a forward proxy connection.

Deprecated: Use DialContext or DialWithConn instead.

func (*SocksDialer) DialContext

func (d *SocksDialer) 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 (*SocksDialer) DialWithConn

func (d *SocksDialer) 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 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