socks5

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

This package provide a basic socks5 server

Index

Constants

View Source
const (
	CmdDialTCP    = 0x01
	CmdBindTCP    = 0x02
	CmdForwardUDP = 0x03
)
View Source
const DefaultSocksPort = ":1080"
View Source
const (
	Socks5Version = 0x05
)

Variables

View Source
var (
	ErrNoAcceptableAuth = errors.New("No acceptable auth were offered")
	ErrUnsupportVersion = errors.New("Unsupport packet format verion")
	ErrUnauthorized     = errors.New("Unauthorized")
	ErrVersionChanged   = errors.New("Version changed after auth")
	ErrTargetBlocked    = errors.New("Target address is blocked")
	ErrUnsupportCommand = errors.New("Unsupported command")
)

Functions

func ListenAndServe

func ListenAndServe(addr string, handler Handler) (err error)

func Serve

func Serve(listener net.Listener, handler Handler) (err error)

Types

type AddrPort

type AddrPort struct {
	Type AddrType
	Data []byte
	Port uint16
}

AddrPort represents a TCP host:port pair

func AddrPortFromNetAddr

func AddrPortFromNetAddr(na net.Addr) (a *AddrPort, err error)

func (*AddrPort) GetDomain

func (a *AddrPort) GetDomain() (domain string, ok bool)

func (*AddrPort) GetIPv4

func (a *AddrPort) GetIPv4() (ip net.IP, ok bool)

func (*AddrPort) GetIPv6

func (a *AddrPort) GetIPv6() (ip net.IP, ok bool)

func (*AddrPort) MarshalBinary

func (a *AddrPort) MarshalBinary() (buf []byte, err error)

func (*AddrPort) ReadFrom

func (a *AddrPort) ReadFrom(r io.Reader) (n int64, err error)

func (*AddrPort) SetDomain

func (a *AddrPort) SetDomain(domain string) *AddrPort

func (*AddrPort) SetIPv4

func (a *AddrPort) SetIPv4(ip net.IP) *AddrPort

func (*AddrPort) SetIPv6

func (a *AddrPort) SetIPv6(ip net.IP) *AddrPort

func (*AddrPort) String

func (a *AddrPort) String() (s string)

func (*AddrPort) WriteTo

func (a *AddrPort) WriteTo(w io.Writer) (n int64, err error)

type AddrType

type AddrType byte
const (
	IPv4Addr   AddrType = 0x01
	DomainAddr AddrType = 0x03
	IPv6Addr   AddrType = 0x04
)

type AuthType

type AuthType = byte
const (
	NoAuth AuthType = 0x00
	// [GSSAPI](https://en.wikipedia.org/wiki/GSSAPI) ([RFC 1961](https://datatracker.ietf.org/doc/html/rfc1961))
	GSSAPIAuth AuthType = 0x01
	// Username/password ([RFC 1929](https://datatracker.ietf.org/doc/html/rfc1929))
	UserAuth AuthType = 0x02
)

type Handler

type Handler interface {
	DialTCP(ctx context.Context, addr *AddrPort) (net.Conn, error)
	BindTCP(ctx context.Context, addr *AddrPort) (net.Listener, error)
	BindUDP(ctx context.Context, addr *AddrPort) (net.PacketConn, error)
}

if Handler returned ErrUnsupportCommand, the server will reply with StatusCommandUnsupported

type MemoryUserGuarder

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

func NewMemoryUserGuarder

func NewMemoryUserGuarder() *MemoryUserGuarder

func NewMemoryUserGuarderFromMap

func NewMemoryUserGuarderFromMap(m map[string]string) *MemoryUserGuarder

func (*MemoryUserGuarder) Auth

func (g *MemoryUserGuarder) Auth(username string, password string) (ok bool)

func (*MemoryUserGuarder) DelUser

func (g *MemoryUserGuarder) DelUser(username string)

func (*MemoryUserGuarder) ListUsers

func (g *MemoryUserGuarder) ListUsers() (users []string)

func (*MemoryUserGuarder) SetUser

func (g *MemoryUserGuarder) SetUser(username string, password string)

type Server

type Server struct {
	Addr    string
	Handler Handler

	// If UserGuarder is set, the Username/password auth mode will be enabled
	UserGuarder UserGuarder
	DialTimeout time.Duration
	// contains filtered or unexported fields
}

func (*Server) Handle

func (s *Server) Handle(netconn net.Conn) (err error)

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() (err error)

func (*Server) Serve

func (s *Server) Serve(listener net.Listener) (err error)

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) (err error)

type StatusCode

type StatusCode byte
const (
	StatusGranted             StatusCode = 0x00 // request granted
	StatusFailed              StatusCode = 0x01 // general failure
	StatusBlocked             StatusCode = 0x02 // connection not allowed by ruleset
	StatusNetworkUnreachable  StatusCode = 0x03 // network unreachable
	StatusHostUnreachable     StatusCode = 0x04 // host unreachable
	StatusRefused             StatusCode = 0x05 // connection refused by destination host
	StatusTLLExpired          StatusCode = 0x06 // TTL expired
	StatusCommandUnsupported  StatusCode = 0x07 // command not supported / protocol error
	StatusAddrTypeUnsupported StatusCode = 0x08 // address type not supported
)

type UnsupportVersionError

type UnsupportVersionError struct {
	Version byte
}

func (*UnsupportVersionError) Error

func (e *UnsupportVersionError) Error() string

type UserGuarder

type UserGuarder interface {
	Auth(username string, password string) (ok bool)
}

Jump to

Keyboard shortcuts

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