socks

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2023 License: MIT Imports: 14 Imported by: 3

Documentation

Overview

Package socks provides SOCKS server framework.

Features: * SOCKS4, SOCS4a, SOCKS5 protocols. * Username/password authentication. * CONNECT command (BIND and UDP ASSOCIATE is not supported). * Graceful stop (thanks to github.com/cybozu-go/well package).

Index

Constants

View Source
const (
	SOCKS4 = version(0x04)
	SOCKS5 = version(0x05)
)

SOCKS versions.

View Source
const (
	CmdConnect = commandType(0x01)
	CmdBind    = commandType(0x02)
	CmdUDP     = commandType(0x03)
)

SOCKS commands.

View Source
const (
	AddrIPv4   = addressType(0x01)
	AddrDomain = addressType(0x03)
	AddrIPv6   = addressType(0x04)
)

SOCKS address types.

View Source
const (
	AuthNo     = authType(0x00)
	AuthGSSAPI = authType(0x01)
	AuthBasic  = authType(0x02)
)

SOCKS authentication types.

View Source
const (
	Status4Granted     = socks4ResponseStatus(0x5a)
	Status4Rejected    = socks4ResponseStatus(0x5b)
	Status4NoIdentd    = socks4ResponseStatus(0x5c)
	Status4InvalidUser = socks4ResponseStatus(0x5d)
)

SOCKS4 response status codes.

View Source
const (
	Status5Granted             = socks5ResponseStatus(0x00)
	Status5Failure             = socks5ResponseStatus(0x01)
	Status5DeniedByRuleset     = socks5ResponseStatus(0x02)
	Status5NetworkUnreachable  = socks5ResponseStatus(0x03)
	Status5HostUnreachable     = socks5ResponseStatus(0x04)
	Status5ConnectionRefused   = socks5ResponseStatus(0x05)
	Status5TTLExpired          = socks5ResponseStatus(0x06)
	Status5CommandNotSupported = socks5ResponseStatus(0x07)
	Status5AddressNotSupported = socks5ResponseStatus(0x08)
)

SOCKS5 response status codes.

View Source
const UNKNOWN = "unknown"

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	Authenticate(r *Request) bool
}

Authenticator is the interface for user authentication. It should look Username and Password field in the request and returns true if authentication succeeds.

Note that both Username and Password may be empty.

type Dialer

type Dialer interface {
	Dial(r *Request) (net.Conn, error)
}

Dialer is the interface to establish connection to the destination.

type Request

type Request struct {
	// Version is either SOCKS4 or SOCKS5
	Version version

	// Hostname is the destination DNS hostname.
	// If this is empty, IP is set to the destination address.
	Hostname string

	// Command is the requested command.
	Command commandType

	// IP is the destination IP address.
	// This may not be set if Hostname is not empty.
	IP net.IP

	// Port is the destination port number.
	Port int

	// Username is user name string for authentication.
	// Username may be empty when no authencation is requested.
	Username string

	// Password is password string for authentication.
	Password string

	// Conn is the connection from the client.
	Conn net.Conn
	// contains filtered or unexported fields
}

Request is a struct to represent a request from SOCKS client.

Authenticator, RuleSet, and Dialer can use Context and SetContext to associate any value with the request, and to cancel lengthy operations.

func (*Request) Context

func (r *Request) Context() context.Context

Context returns the request context.

func (*Request) SetContext

func (r *Request) SetContext(ctx context.Context)

SetContext sets the request context.

type RuleSet

type RuleSet interface {
	Match(r *Request) bool
}

RuleSet is the interface for access control. It should look the request properties and returns true if the request matches rules.

type Server

type Server struct {
	// Auth can be used to authenticate a request.
	// If nil, all requests are allowed.
	Auth Authenticator

	// Rules can be used to test a request if it matches rules.
	// If nil, all requests passes.
	Rules RuleSet

	// Dialer is used to make connections to destination servers.
	// If nil, net.DialContext is used.
	Dialer Dialer

	// Logger can be used to provide a custom logger.
	// If nil, the default logger is used.
	Logger *log.Logger

	// ShutdownTimeout is the maximum duration the server waits for
	// all connections to be closed before shutdown.
	//
	// Zero duration disables timeout.
	ShutdownTimeout time.Duration

	// Env is the environment where this server runs.
	//
	// The global environment is used if Env is nil.
	Env *well.Environment

	// SilenceLogs changes Info-level logs to Debug-level ones.
	SilenceLogs bool
	// contains filtered or unexported fields
}

Server implement SOCKS protocol.

func (*Server) Serve

func (s *Server) Serve(l net.Listener)

Serve starts a goroutine to accept connections. This returns immediately. l will be closed when s.Env is canceled. See https://godoc.org/github.com/cybozu-go/well#Server.Serve

Jump to

Keyboard shortcuts

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