socks5

package
v1.15.1 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2023 License: GPL-3.0, MIT Imports: 16 Imported by: 2

README

go-socks5 Build Status

Provides the socks5 package that implements a SOCKS5 server. SOCKS (Secure Sockets) is used to route traffic between a client and server through an intermediate proxy layer. This can be used to bypass firewalls or NATs.

Feature

The package has the following features:

  • "No Auth" mode
  • User/Password authentication
  • Support for the CONNECT command
  • Rules to do granular filtering of commands
  • Custom DNS resolution
  • Unit tests

Example

Below is a simple example of usage

// Create a SOCKS5 server
conf := &socks5.Config{}
server, err := socks5.New(conf)
if err != nil {
  panic(err)
}

// Create SOCKS5 proxy on localhost port 8000
if err := server.ListenAndServe("tcp", "127.0.0.1:8000"); err != nil {
  panic(err)
}

Documentation

Overview

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

Index

Constants

View Source
const (
	ConnectCommand   = uint8(1)
	BindCommand      = uint8(2)
	AssociateCommand = uint8(3)
)
View Source
const (
	// socks5 version number.
	Socks5Version byte = 5

	// No authentication required.
	NoAuth byte = 0
)

Variables

View Source
var (
	HandshakeErrors          = metrics.RegisterMetric("socks5", "HandshakeErrors")
	DNSResolveErrors         = metrics.RegisterMetric("socks5", "DNSResolveErrors")
	UnsupportedCommandErrors = metrics.RegisterMetric("socks5", "UnsupportedCommandErrors")
	NetworkUnreachableErrors = metrics.RegisterMetric("socks5", "NetworkUnreachableErrors")
	HostUnreachableErrors    = metrics.RegisterMetric("socks5", "HostUnreachableErrors")
	ConnectionRefusedErrors  = metrics.RegisterMetric("socks5", "ConnectionRefusedErrors")
	UDPAssociateErrors       = metrics.RegisterMetric("socks5", "UDPAssociateErrors")

	// Incoming UDP association bytes.
	UDPAssociateInBytes = metrics.RegisterMetric("socks5 UDP associate", "InBytes")

	// Outgoing UDP association bytes.
	UDPAssociateOutBytes = metrics.RegisterMetric("socks5 UDP associate", "OutBytes")

	// Incoming UDP association packets.
	UDPAssociateInPkts = metrics.RegisterMetric("socks5 UDP associate", "InPkts")

	// Outgoing UDP association packets.
	UDPAssociateOutPkts = metrics.RegisterMetric("socks5 UDP associate", "OutPkts")
)

Functions

func BidiCopyUDP added in v1.7.0

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

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

Types

type AddrSpec

type AddrSpec struct {
	FQDN string
	IP   net.IP
	Port int
}

AddrSpec is used to return the target AddrSpec which may be specified as IPv4, IPv6, or a FQDN.

func (AddrSpec) Address

func (a AddrSpec) Address() string

Address returns a string suitable to dial; prefer returning IP-based address, fallback to FQDN

func (*AddrSpec) String

func (a *AddrSpec) String() string

type Config

type Config struct {
	// Resolver can be provided to do custom name resolution.
	Resolver *util.DNSResolver

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

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

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

	// Mieru proxy configuration.
	ProxyConf []ProxyConfig

	// Mieru proxy multiplexer.
	ProxyMux *protocolv2.Mux
}

Config is used to setup and configure a socks5 server.

type ProxyConfig added in v1.3.0

type ProxyConfig struct {
	// NetworkType ("tcp", "udp", etc.) used when dial to the proxy.
	NetworkType string

	// Address is proxy server listening address, in host:port format.
	Address string

	// Password is used to derive the cipher block used for encryption.
	Password []byte

	// Dial is the function to dial to the proxy server.
	Dial func(ctx context.Context, proxyNetwork, localAddr, proxyAddr string, block cipher.BlockCipher) (net.Conn, error)
}

ProxyConfig is used to configure mieru proxy options.

type Request

type Request struct {
	// Protocol version.
	Version uint8
	// Requested command.
	Command uint8
	// AddrSpec of the the network that sent the request.
	RemoteAddr *AddrSpec
	// AddrSpec of the desired destination.
	DestAddr *AddrSpec
}

A Request represents request received by a server.

func NewRequest

func NewRequest(conn io.Reader) (*Request, error)

NewRequest creates a new Request from the tcp connection.

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.

func (*Server) Take added in v1.15.1

func (s *Server) Take(conn net.Conn) (closed bool, err error)

Take implements util.ConnHandler interface.

type ServerGroup added in v1.3.0

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

ServerGroup is a collection of socks5 servers that share the same lifecycle.

func NewGroup added in v1.3.0

func NewGroup() *ServerGroup

NewGroup creates a new ServerGroup.

func (*ServerGroup) Add added in v1.3.0

func (g *ServerGroup) Add(underlayProtocol string, port int, s *Server) error

Add adds a socks5 server into the ServerGroup.

func (*ServerGroup) CloseAndRemoveAll added in v1.3.0

func (g *ServerGroup) CloseAndRemoveAll() error

CloseAndRemoveAll closes all the socks5 servers and clear the group.

func (*ServerGroup) IsEmpty added in v1.3.0

func (g *ServerGroup) IsEmpty() bool

IsEmpty returns true if the group has no socks5 server.

type UDPAssociateTunnelConn added in v1.7.0

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 little endian.

func WrapUDPAssociateTunnel added in v1.7.0

func WrapUDPAssociateTunnel(conn io.ReadWriteCloser) *UDPAssociateTunnelConn

WrapUDPAssociateTunnel wraps an existing connection with UDPAssociateTunnelConn.

func (*UDPAssociateTunnelConn) Close added in v1.7.0

func (c *UDPAssociateTunnelConn) Close() error

func (*UDPAssociateTunnelConn) Read added in v1.7.0

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

func (*UDPAssociateTunnelConn) Write added in v1.7.0

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