socks5

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: Apache-2.0 Imports: 14 Imported by: 7

README

socks5

中文文档
A lightweight SOCKS proxy server that supports socks4, socks4a, and socks5 protocols. The code is simple and easy to read, just like the original SOCKS protocol.

Feature

Usage

Download the latest program for your operating system and architecture from the Release page. Extracting,then execute the binary file directly (Linux platform requires execution permission)

./ss5

or

./ss5 -c ./ss5.json

Docker installation

Configuration

The ss5.json file in the extracted directory is the configuration file
Simple configuration instructions:

 ListenPort The listening port for TCP and UDP proxies, default is 1080
 UserName,Password Fill in if username/password authentication is required, default is empty
 LogLevel Log level (debug, info, warn, error)

Advanced configuration

Package Usage

go get github.com/0990/socks5  

Here is a simple example:

s := socks5.NewServer(socks5.ServerCfg{
	    ListenPort: 1080,
	    UserName:   "",
	    Password:   "",
	    UDPTimout:  60,
	    TCPTimeout: 60,
	    LogLevel:"error"
})
err := s.Run()
if err != nil {
	log.Fatalln(err)
}

TODO

  • Support BIND command

Thanks

txthinking/socks5

Documentation

Index

Constants

View Source
const (
	VerSocks4 = 0x04
	VerSocks5 = 0x05
)
View Source
const (
	RepSocks4Granted     = 0x5a
	RepSocks4Rejected    = 0x5b
	RepSocks4NoIdentd    = 0x5c
	RepSocks4InvalidUser = 0x5d
)
View Source
const (
	MethodNone         = 0x00
	MethodUserPass     = 0x02
	MethodNoAcceptable = 0xff

	VerAuthUserPass   = 0x01
	AuthStatusSuccess = 0x00
	AuthStatusFailure = 0x01

	CmdConnect     = 0x01
	CmdBind        = 0x02
	CmdUDP         = 0x03
	ATypIPV4       = 0x01
	ATypDomainname = 0x03
	ATypIPV6       = 0x04

	RepSuccess              = 0x00
	RepServerFailure        = 0x01
	RepRuleFailure          = 0x02
	RepNetworkUnreachable   = 0x03
	RepHostUnreachable      = 0x04
	RepConnectionRefused    = 0x05
	RepTTLExpired           = 0x06
	RepCmdNotSupported      = 0x07
	RepAddrTypeNotSupported = 0x08
)
View Source
const DefaultListenPort = 1080
View Source
const DefaultLogLevel = "error"
View Source
const DefaultTcpTimeout = 60
View Source
const DefaultUdpTimeout = 60
View Source
const MaxAddrLen = 1 + 1 + 255 + 2
View Source
const MaxSegmentSize = 65535
View Source
const PortLen = 2
View Source
const SocketBufSize = 20480

buf cannot larger than 64k,because of the socket buffer size header is 16bit

Variables

View Source
var (
	ErrMethodNoAcceptable = errors.New("no acceptable method")
	ErrAuthFailed         = errors.New("User authentication failed")
	NoSupportedAuth       = errors.New("no supported auth")
	ErrAuthUserPassVer    = errors.New("auth user pass version")
	ErrCmdNotSupport      = errors.New("cmd not support")

	ErrAddrType     = fmt.Errorf("Unrecognized address type")
	ErrSocksVersion = fmt.Errorf("not socks version 5")
	ErrMethod       = fmt.Errorf("Unsupport method")
	ErrBadRequest   = fmt.Errorf("bad request")
	ErrUDPFrag      = fmt.Errorf("Frag !=0 not supported")
)
View Source
var DefaultServerConfig = ServerCfg{
	ListenPort:      DefaultListenPort,
	UserName:        "",
	Password:        "",
	UDPTimout:       DefaultTcpTimeout,
	TCPTimeout:      DefaultUdpTimeout,
	UDPAdvertisedIP: "",
	LogLevel:        DefaultLogLevel,
}

Functions

func CheckServerCfgDefault

func CheckServerCfgDefault(cfg *ServerCfg)

func CreateClientCfg

func CreateClientCfg(path string) error

func CreateServerCfg

func CreateServerCfg(path string) error

func NewSocks4Client

func NewSocks4Client(cfg ClientCfg) *socks4Client

func NewSocks5Client

func NewSocks5Client(cfg ClientCfg) *socks5client

func Pipe added in v1.0.6

func Pipe(left Stream, right Stream, timeout time.Duration) error

Types

type AddrByte

type AddrByte []byte

func NewAddrByteFrom

func NewAddrByteFrom(r io.Reader) (AddrByte, error)

func NewAddrByteFromByte

func NewAddrByteFromByte(b []byte) (AddrByte, error)

func NewAddrByteFromString

func NewAddrByteFromString(s string) (AddrByte, error)

func (AddrByte) Split

func (a AddrByte) Split() (aType byte, addr []byte, port []byte)

func (AddrByte) String

func (a AddrByte) String() string

type ClientCfg

type ClientCfg struct {
	ServerAddr string
	UserName   string
	Password   string
	UDPTimout  int
	TCPTimeout int
}

type Conn

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

func NewConn added in v1.0.3

func NewConn(conn Stream, cfg ConnCfg) *Conn

func (*Conn) Handle

func (p *Conn) Handle() error

func (*Conn) SetCustomDialTarget added in v1.0.3

func (p *Conn) SetCustomDialTarget(f func(addr string) (Stream, byte, string, error))

type ConnCfg added in v1.0.3

type ConnCfg struct {
	UserName   string
	Password   string
	TCPTimeout int32

	UDPAdvertisedIP   string
	UDPAdvertisedPort int
}

type MethodSelectReply

type MethodSelectReply struct {
	Ver    byte
	Method byte
}

func NewMethodSelectReply

func NewMethodSelectReply(method byte) *MethodSelectReply

func NewMethodSelectReplyFrom

func NewMethodSelectReplyFrom(r io.Reader) (*MethodSelectReply, error)

func (*MethodSelectReply) ToBytes

func (p *MethodSelectReply) ToBytes() []byte

type MethodSelectReq

type MethodSelectReq struct {
	Ver      byte
	NMethods byte
	Methods  []byte
}

func NewMethodSelectReq

func NewMethodSelectReq(methods []byte) *MethodSelectReq

func NewMethodSelectReqFrom

func NewMethodSelectReqFrom(r io.Reader) (*MethodSelectReq, error)

func (*MethodSelectReq) ToBytes

func (p *MethodSelectReq) ToBytes() []byte

type Reply

type Reply struct {
	Ver     byte
	Rep     byte
	Rsv     byte
	Atyp    byte
	BndAddr []byte
	BndPort []byte //2 bytes
}

func NewReply

func NewReply(rep byte, addrByte AddrByte) *Reply

func NewReplyFrom

func NewReplyFrom(r io.Reader) (*Reply, error)

func (*Reply) Address

func (p *Reply) Address() string

func (*Reply) ToBytes

func (p *Reply) ToBytes() []byte

type ReplySocks4

type ReplySocks4 struct {
	VN      byte
	CD      byte
	DstPort []byte
	DstIP   []byte
}

func NewReplySocks4

func NewReplySocks4(cd byte, portIp []byte) *ReplySocks4

func NewReplySocks4From

func NewReplySocks4From(r io.Reader) (*ReplySocks4, error)

func (*ReplySocks4) ToBytes

func (p *ReplySocks4) ToBytes() []byte

type ReqSocks4

type ReqSocks4 struct {
	Ver      byte
	CD       byte   //1 connect 2  bind
	DstPort  []byte //2 bytes
	DstIP    []byte
	UserId   []byte
	HostName []byte
}

Ver|CD|DstPort|DstIP|USERID|0|HostName|0

func NewReqSocks4

func NewReqSocks4(cmd byte, addr string) (*ReqSocks4, error)

func NewReqSocks4From

func NewReqSocks4From(r io.Reader) (*ReqSocks4, error)

func (*ReqSocks4) Address

func (p *ReqSocks4) Address() string

func (*ReqSocks4) PortIPBytes

func (p *ReqSocks4) PortIPBytes() []byte

func (*ReqSocks4) ToBytes

func (p *ReqSocks4) ToBytes() []byte

type Request

type Request struct {
	Ver     byte
	Cmd     byte
	Rsv     byte //0x00
	Atyp    byte
	DstAddr []byte
	DstPort []byte //2 bytes
}

func NewRequest

func NewRequest(cmd byte, addrByte AddrByte) *Request

func NewRequestFrom

func NewRequestFrom(r io.Reader) (*Request, error)

func (*Request) Address

func (p *Request) Address() string

func (*Request) ToBytes

func (p *Request) ToBytes() []byte

type SenderMap

type SenderMap struct {
	sync.Map
}

func (*SenderMap) Add

func (p *SenderMap) Add(key string, conn net.PacketConn)

func (*SenderMap) Del

func (p *SenderMap) Del(key string) net.PacketConn

func (*SenderMap) Get

func (p *SenderMap) Get(key string) (net.PacketConn, bool)

type Server

type Server interface {
	Run() error
	SetCustomTcpConnHandler(handler func(conn *net.TCPConn))
}

func NewServer

func NewServer(cfg ServerCfg) (Server, error)

type ServerCfg

type ServerCfg struct {
	ListenPort      int    //tcp,udp监听端口,仅当TCPListen或UDPListen无值时有效,监听地址为 0.0.0.0:ListenPort
	TCPListen       string //tcp监听地址
	UDPListen       string //udp监听地址
	UDPAdvertisedIP string //udp的广告IP地址,告诉客户端将UDP数据发往这个ip,默认值为udp监听的本地ip地址

	UserName   string
	Password   string
	UDPTimout  int
	TCPTimeout int
	LogLevel   string
}

func ReadClientCfg

func ReadClientCfg(path string) (*ServerCfg, error)

func ReadOrCreateServerCfg

func ReadOrCreateServerCfg(path string) (*ServerCfg, error)

func ReadServerCfg

func ReadServerCfg(path string) (*ServerCfg, error)

type Socks4Conn

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

func (*Socks4Conn) Handle

func (p *Socks4Conn) Handle() error

type Socks5Conn

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

func (*Socks5Conn) Handle

func (p *Socks5Conn) Handle() error

func (*Socks5Conn) SetCustomDialTarget added in v1.0.3

func (p *Socks5Conn) SetCustomDialTarget(f func(addr string) (Stream, byte, string, error))

type SocksUDPConn

type SocksUDPConn struct {
	*net.UDPConn
	// contains filtered or unexported fields
}

func (*SocksUDPConn) Read

func (p *SocksUDPConn) Read(b []byte) (int, error)

func (*SocksUDPConn) SetReadDeadline added in v1.0.4

func (p *SocksUDPConn) SetReadDeadline(t time.Time) error

func (*SocksUDPConn) Write

func (p *SocksUDPConn) Write(b []byte) (int, error)

type Stream added in v1.0.3

type Stream interface {
	RemoteAddr() net.Addr
	LocalAddr() net.Addr
	io.ReadWriteCloser
	SetReadDeadline(t time.Time) error
}

type UDPDatagram

type UDPDatagram struct {
	Rsv     []byte //0x00,0x00
	Frag    byte
	AType   byte
	DstAddr []byte
	DstPort []byte
	Data    []byte
}

func NewUDPDatagram

func NewUDPDatagram(addrByte AddrByte, data []byte) *UDPDatagram

func NewUDPDatagramFromBytes

func NewUDPDatagramFromBytes(b []byte) (*UDPDatagram, error)

func (*UDPDatagram) Address

func (p *UDPDatagram) Address() string

func (*UDPDatagram) ToBytes

func (p *UDPDatagram) ToBytes() []byte

type UserPassAuthReply

type UserPassAuthReply struct {
	Ver    byte
	Status byte
}

func NewUserPassAuthReply

func NewUserPassAuthReply(status byte) *UserPassAuthReply

func NewUserPassAuthReplyFrom

func NewUserPassAuthReplyFrom(r io.Reader) (*UserPassAuthReply, error)

func (*UserPassAuthReply) ToBytes

func (p *UserPassAuthReply) ToBytes() []byte

type UserPassAuthReq

type UserPassAuthReq struct {
	Ver      byte
	ULen     byte
	UserName []byte
	PLen     byte
	Password []byte
}

func NewUserPassAuthReq

func NewUserPassAuthReq(username []byte, password []byte) *UserPassAuthReq

func NewUserPassAuthReqFrom

func NewUserPassAuthReqFrom(r io.Reader) (*UserPassAuthReq, error)

func (*UserPassAuthReq) ToBytes

func (p *UserPassAuthReq) ToBytes() []byte

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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