socks5

package
v3.8.4 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: GPL-3.0, BSD-2-Clause, MIT Imports: 25 Imported by: 0

Documentation

Overview

Package socks5 is imported from https://github.com/armon/go-socks5 and https://github.com/h12w/socks

Index

Constants

This section is empty.

Variables

View Source
var (
	HTTPMetricGroupName = "HTTP proxy"

	HTTPRequests     = metrics.RegisterMetric(HTTPMetricGroupName, "Requests", metrics.COUNTER)
	HTTPConnErrors   = metrics.RegisterMetric(HTTPMetricGroupName, "ConnErrors", metrics.COUNTER)
	HTTPSchemeErrors = metrics.RegisterMetric(HTTPMetricGroupName, "SchemeErrors", metrics.COUNTER)
)
View Source
var (
	HandshakeErrors          = metrics.RegisterMetric("socks5", "HandshakeErrors", metrics.COUNTER)
	DNSResolveErrors         = metrics.RegisterMetric("socks5", "DNSResolveErrors", metrics.COUNTER)
	UnsupportedCommandErrors = metrics.RegisterMetric("socks5", "UnsupportedCommandErrors", metrics.COUNTER)
	NetworkUnreachableErrors = metrics.RegisterMetric("socks5", "NetworkUnreachableErrors", metrics.COUNTER)
	HostUnreachableErrors    = metrics.RegisterMetric("socks5", "HostUnreachableErrors", metrics.COUNTER)
	ConnectionRefusedErrors  = metrics.RegisterMetric("socks5", "ConnectionRefusedErrors", metrics.COUNTER)
	UDPAssociateErrors       = metrics.RegisterMetric("socks5", "UDPAssociateErrors", metrics.COUNTER)

	UDPAssociateUploadBytes     = metrics.RegisterMetric("socks5 UDP associate", "UploadBytes", metrics.COUNTER)
	UDPAssociateDownloadBytes   = metrics.RegisterMetric("socks5 UDP associate", "DownloadBytes", metrics.COUNTER)
	UDPAssociateUploadPackets   = metrics.RegisterMetric("socks5 UDP associate", "UploadPackets", metrics.COUNTER)
	UDPAssociateDownloadPackets = metrics.RegisterMetric("socks5 UDP associate", "DownloadPackets", metrics.COUNTER)
)

Functions

func BidiCopyUDP

func BidiCopyUDP(udpConn *net.UDPConn, tunnelConn *UDPAssociateTunnelConn) error

BidiCopyUDP does bi-directional data copy between a proxy client UDP endpoint and the proxy tunnel.

func Dial

func Dial(proxyURI string, cmdType byte) func(string, string) (net.Conn, error)

Dial returns the dial function to be used in http.Transport object. Argument proxyURI should be in the format: "socks5://user:password@127.0.0.1:1080?timeout=5s". Only socks5 protocol is supported.

func DialSocks5Proxy

func DialSocks5Proxy(c *Client) func(string, string) (net.Conn, *net.UDPConn, *net.UDPAddr, error)

DialSocks5Proxy returns two connections that can be used to send TCP and UDP traffic.

func HTTPTransportProxyFunc

func HTTPTransportProxyFunc(proxy string) func(*http.Request) (*url.URL, error)

HTTPTransportProxyFunc returns the Proxy function used by http.Transport.

func NewHTTPProxyServer

func NewHTTPProxyServer(listenAddr string, proxy *HTTPProxy) *http.Server

NewHTTPProxyServer returns a new HTTP proxy server.

func TransceiveUDPPacket

func TransceiveUDPPacket(conn *net.UDPConn, proxyAddr, dstAddr *net.UDPAddr, payload []byte) ([]byte, error)

TransceiveUDPPacket sends a single UDP associate message and returns the response.

Types

type Auth

type Auth struct {
	// Do socks5 authentication at proxy client side.
	ClientSideAuthentication bool

	// Credentials to authenticate incoming requests.
	// If empty, username password authentication is not supported.
	IngressCredentials []Credential

	// Credential to dial an outgoing socks5 connection.
	// If nil, username password authentication is not used.
	EgressCredential *Credential
}

Auth provide authentication settings to socks5 server.

type Client

type Client struct {
	Host       string
	Credential *Credential
	Timeout    time.Duration
	CmdType    byte
}

Client contains socks5 client configuration.

type Config

type Config struct {
	// Mieru proxy multiplexer.
	ProxyMux *protocol.Mux

	// Egress controller.
	EgressController egress.Controller

	// Resolver can be provided to do custom name resolution.
	Resolver apicommon.DNSResolver

	// BindIP is used for bind or udp associate
	BindIP net.IP

	// Authentication options.
	AuthOpts Auth

	// Handshake timeout to establish socks5 connection.
	// Use 0 or negative value to disable the timeout.
	HandshakeTimeout time.Duration

	// Use mieru proxy to carry socks5 traffic.
	UseProxy bool

	// Allow using socks5 to access resources served in localhost.
	AllowLocalDestination bool
}

Config is used to setup and configure a socks5 server.

type Credential

type Credential struct {
	// User to dial an outgoing socks5 connection.
	User string

	// Password to dial an outgoing socks5 connection.
	Password string
}

Credential stores socks5 credential for user password authentication.

type HTTPProxy

type HTTPProxy struct {
	ProxyURI string
	// contains filtered or unexported fields
}

func (*HTTPProxy) ServeHTTP

func (p *HTTPProxy) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler interface with a socks5 backend.

type Request

type Request struct {
	// Protocol version.
	Version uint8
	// Requested command.
	Command uint8
	// AddrSpec of the desired destination.
	DstAddr *model.AddrSpec
	// Raw request bytes.
	Raw []byte
}

A Request represents request received by a server.

type Server

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

Server is responsible for accepting connections and handling the details of the SOCKS5 protocol

func New

func New(conf *Config) (*Server, error)

New creates a new Server and potentially returns an error.

func (*Server) Close

func (s *Server) Close() error

Close closes the network listener used by the server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe(network, addr string) error

ListenAndServe is used to create a listener and serve on it.

func (*Server) Serve

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

Serve is used to serve connections from a listener.

func (*Server) ServeConn

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

ServeConn is used to serve a single connection.

type UDPAssociateTunnelConn

type UDPAssociateTunnelConn struct {
	io.ReadWriteCloser
}

UDPAssociateTunnelConn keeps the boundary of UDP packets when transmitted inside the proxy tunnel, which is typically a streaming pipe.

Each original UDP packet will be wrapped like this

0x00 + 2 bytes of original length + original content + 0xff

the length is encoded with big endian.

func WrapUDPAssociateTunnel

func WrapUDPAssociateTunnel(conn io.ReadWriteCloser) *UDPAssociateTunnelConn

WrapUDPAssociateTunnel wraps an existing connection with UDPAssociateTunnelConn.

func (*UDPAssociateTunnelConn) Close

func (c *UDPAssociateTunnelConn) Close() error

func (*UDPAssociateTunnelConn) Read

func (c *UDPAssociateTunnelConn) Read(b []byte) (n int, err error)

func (*UDPAssociateTunnelConn) Write

func (c *UDPAssociateTunnelConn) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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