socks

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

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

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

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

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

	StatusSucceeded          byte = 0x00
	StatusFailed             byte = 0x01
	StatusNetworkUnreachable byte = 0x03
)

Variables

View Source
var ErrAuthFailed error = fmt.Errorf("auth failed")
View Source
var ErrAuthMethodNotSupported error = fmt.Errorf("auth method not supported")
View Source
var ErrBadRequest error = fmt.Errorf("bad request")
View Source
var ErrCmdNotSupported error = fmt.Errorf("cmd not supported")
View Source
var ErrWrongFormat error = fmt.Errorf("wrong format")
View Source
var ErrWrongProtocol error = fmt.Errorf("wrong protocol")

Functions

func SOCKS5Client added in v0.2.0

func SOCKS5Client(ctx context.Context, conn net.Conn, network, addr string, user *url.Userinfo) (net.Conn, error)

Types

type Client added in v0.2.0

type Client struct {
	ProxyUrl *url.URL

	// Dialer specifies an optional dial function with context for
	// creating connections for requests.
	//
	// If Dialer is nil, &net.Dialer{} is used.
	Dialer koNet.ContextDialer
}

func (*Client) DialContext added in v0.2.0

func (s *Client) DialContext(ctx context.Context, network, addr string) (conn net.Conn, err error)

type Request added in v0.2.4

type Request struct {
	Version int
	Address string
}

type Server

type Server struct {
	// Dialer specifies an optional ContextDialer.
	// If non-nil, it will be used on outbound.
	Dialer koNet.ContextDialer

	// Logger specifies an optional logger for errors.
	// If nil, log nothing.
	Logger *slog.Logger

	// AuthContext handles user authentication for a proxy.
	//
	// It returns a boolean indicating whether the provided username and password are
	// valid. If true, a non-nil Context must also be returned, which will be used
	// for dialing. The original context is passed in via the 'ctx' parameter.
	//
	// Authentication is optional. SOCKS4(a) connections are supported only when
	// both AuthContext and AuthHandler are nil. If AuthContext is set, it takes
	// precedence over AuthHandler, and SOCKS4(a) connections are not supported.
	AuthContext func(ctx context.Context, username, password string) (bool, context.Context)

	// AuthHandler authenticates a user based on the provided username and password,
	// returning true on success. This handler is only used if AuthContext is nil.
	AuthHandler func(username, password string) bool

	// ConnContext optionally specifies a function that modifies
	// the context used for a new connection c. The provided ctx
	// is derived from the base context and has a ServerContextKey
	// value.
	ConnContext func(ctx context.Context, c net.Conn, req *Request) context.Context

	// Context is used to shut down the server.
	// If not nil, the server will be shut down when the context is done.
	Context context.Context
}

A Server defines parameters for running an SOCKS4/SOCKS5 server. The zero value for Server is a valid configuration.

func (*Server) Serve

func (srv *Server) Serve(l koNet.Listener) error

Serve accepts incoming connections on the listener l and serves requests for SOCKS4/SOCKS5 connections. Returns when the listener is closed or an error occurs.

func (*Server) ServeConn added in v0.2.4

func (srv *Server) ServeConn(conn net.Conn) error

ServeConn serves a single connection. Returns when the connection is closed or an error occurs.

Jump to

Keyboard shortcuts

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