shadowsocks

package
v0.0.0-...-49a3824 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AddrMask is used to mask the AddrType
	AddrMask byte = 0xf
)

Variables

View Source
var (
	// Logger used to out put the log, zap logger is fast and efficiency
	Logger *zap.Logger
	// Level can be set into Debug Info Error, and Error level is used by default
	Level string
)
View Source
var (
	ErrPacketTooSmall       = errors.New("[udp]read error: cannot decrypt, received packet is smaller than ivLen")
	ErrBufferTooSmall       = errors.New("[udp]read error: given buffer is too small to hold data")
	ErrInvalidHostname      = errors.New("error invalid hostname")
	ErrInvalidPacket        = errors.New("invalid message received")
	ErrInvalidServerAddress = errors.New("invalid server ip address, can not be parsed")
	ErrNilPasswd            = errors.New("password should NOT be nil")
	ErrParesConfigfile      = errors.New("can not parse the config fire")
	ErrNilCipher            = errors.New("cipher should NOT be nil")
	ErrInvalidCipher        = errors.New("cipher method invalid or not supported")
	ErrUnexpectedIO         = errors.New("error in IO, expect more data than we get")
	ErrInvalidConfig        = errors.New("error in config check, config fields invalid")
)
View Source
var (
	BufferSize = 0x1FFFF // BufferSize define pool size for buffer. By default, 32K will give for each buffer

)
View Source
var UDPBufferPool = sync.Pool{
	New: func() interface{} {
		return make([]byte, UDPMaxSize)
	},
}
View Source
var (
	//reqList            = newReqList()
	UDPMaxSize = 65536
)

Functions

func ForwardUDPConn

func ForwardUDPConn(serverIn net.PacketConn, src net.Addr, payload []byte) error

ForwardUDPConn forwards the payload (should with header) to the dst with UDP. meanwhile, the request header is cached and the connection is else cached for further use.

func GetRequest

func GetRequest(ss net.Conn) (host string, err error)

GetRequest can handler the ss request header and decryption for ss protocol

func GetUDPRequest

func GetUDPRequest(req []byte) (dst string, length int, err error)

GetUDPRequest can handler the ss request header and decryption for ss protocol

func HmacSha1

func HmacSha1(key []byte, data []byte) []byte

HmacSha1 implements HmacSha1

func IsFileExists

func IsFileExists(path string) (bool, error)

IsFileExists returns true if the file exists

func NewSecureConn

func NewSecureConn(c net.Conn, cipher encrypt.Cipher, timeout int) net.Conn

NewSecureConn creates a SecureConn with given cipher and timeout by warp the net.Conn

func NewSecurePacketConn

func NewSecurePacketConn(c net.PacketConn, cipher encrypt.Cipher, timeout int) net.PacketConn

NewSecurePacketConn creates a secured PacketConn

func PipeThenClose

func PipeThenClose(src, dst NetConnection, done func())

PipeThenClose copies data from src to dst, close dst when done.

func PipeThenCloseFromUDP

func PipeThenCloseFromUDP(src net.PacketConn, dst net.Conn, timeout int)

PipeThenCloseFromUDP will copy data from UDP connection to tcp connection

func PipeUDPThenClose

func PipeUDPThenClose(src net.Conn, dst net.PacketConn, dstaddr string, timeout int)

PipeUDPThenClose will copy data to UDP connection

func PrintVersion

func PrintVersion()

PrintVersion prints the current version of shadowsocks-go

func SecureListen

func SecureListen(network, laddr string, cipher encrypt.Cipher, timeout int) (net.Listener, error)

Listen announces on the TCP address laddr and returns a TCP listener. Net must be "tcp", "tcp4", or "tcp6". If laddr has a port of 0, ListenTCP will choose an available port. The caller can use the Addr method of TCPListener to retrieve the chosen address.

func SecureListenPacket

func SecureListenPacket(network, laddr string, cipher encrypt.Cipher, timeout int) (net.PacketConn, error)

ListenPacket is like net.ListenPacket() but returns an secured connection

func SetLogger

func SetLogger()

SetLogger will generate a zap logger with given level for log output

func UDPGetRequest

func UDPGetRequest(buf []byte) (host string, headerLen int, err error)

UDPGetRequest parse the request header from buffer the Header is the SS address header TODO need a unit test

Types

type BackwardInfo

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

BackwardInfo is defined for the backword packet to the src address

type ConfOption

type ConfOption func(c *Config)

ConfOption define the config options

func WithDNSServer

func WithDNSServer(server string) ConfOption

WithDNSServer set the DNS server address

func WithEncryptMethod

func WithEncryptMethod(method string) ConfOption

WithEncryptMethod set the encrypt method

func WithLocalAddr

func WithLocalAddr(addr string) ConfOption

WithLocalAddr set the local socks5 address

func WithLocalPort

func WithLocalPort(port string) ConfOption

WithLocalPort set the local socks5 port

func WithMultiServerMode

func WithMultiServerMode(mode string) ConfOption

WithMultiServerMode choose the mode about multiserver

func WithPassword

func WithPassword(pwd string) ConfOption

WithPassword set the password for server

func WithRemoteServer

func WithRemoteServer(server, method, passwd string) ConfOption

WithRemoteServer add a remote server entry into serverlist

func WithServer

func WithServer(server string) ConfOption

WithServer set the server address

func WithServerPort

func WithServerPort(port string) ConfOption

WithServerPort set the server port for server

func WithTimeOut

func WithTimeOut(t int) ConfOption

WithTimeOut set the timeout for ss connection

type Config

type Config struct {
	Server           string        `json:"server_addr"`        // shadowsocks remote Server address
	ServerPort       string        `json:"server_port"`        // shadowsocks remote Server port
	Local            string        `json:"local_addr"`         // shadowsocks local socks5 Server address
	LocalPort        string        `json:"local_port"`         // shadowsocks local socks5 Server port
	Password         string        `json:"password"`           // shadowsocks encrypt password
	Method           string        `json:"method"`             // shadowsocks encryption method
	TunnelPort       string        `json:"tunnel_port"`        // shadowsocks tunnel port local, this will enable tunnel mode only tunnel remote port setted
	TunnelRemotePort string        `json:"tunnel_remote_port"` // shadowsocks tunnel remote port for tunnel access
	TunnelDest       string        `json:"tunnel_destination"` // shadowsocks tunnel remote address for ss-remote to access
	MultiServerMode  string        `json:"multi_server_mode"`  // shadowsocks client multi-server access mode: fastest,round-robin,dissable
	DNSServer        string        `json:"dns_server"`         // shadowsocks remote Server DNS server option, the system DNS will be uesd for domain lookup by defalut
	Timeout          int           `json:"timeout"`            // shadowsocks connection timeout
	ServerList       []ServerEntry `json:"server_list"`        // shadowsocks server list keep a list of remote ss-server information

}

Config implement the ss config

func NewConfig

func NewConfig(opts ...ConfOption) (*Config, error)

NewConfig use the option to generate the ss config

func ParseConfig

func ParseConfig(path string) (conf *Config, err error)

ParseConfig parses a config file

func (*Config) Check

func (c *Config) Check() error

func (*Config) Detect

func (c *Config) Detect()

Detect used only when multi tcp based ss server is setted for the client Detect will try to dial each server to caculate a delay and sort server

func (*Config) GetServer

func (c *Config) GetServer() ServerEntry

func (*Config) GetServerRoundRobin

func (c *Config) GetServerRoundRobin() ServerEntry

func (*Config) String

func (c *Config) String() string

String return the ss config content in string

type Dialer

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

Dialer provides client side connection support and also implements the Dialer interface described in golang.org/x/net/proxy

func NewDialer

func NewDialer(server string, cipher encrypt.Cipher, timeout int) (dialer *Dialer, err error)

NewDialer initializes a new Dialer

func (*Dialer) Dial

func (d *Dialer) Dial(network, addr string) (c net.Conn, err error)

Dial is intended for the Dialer interface described in golang.org/x/net/proxy

func (*Dialer) DialUDP

func (d *Dialer) DialUDP(network, laddr, raddr string) (c net.PacketConn, err error)

DialUDP is used to open an UDP connection on client side to and remote dst

func (*Dialer) DialWithRawAddr

func (d *Dialer) DialWithRawAddr(rawaddr []byte) (conn net.Conn, err error)

DialWithRawAddr is intended for use by users implementing a local socks proxy. rawaddr shoud contain part of the data in socks request, starting from the ATYP field. (Refer to rfc1928 for more information.)

func (*Dialer) ListenPacket

func (d *Dialer) ListenPacket(network, laddr string) (c net.PacketConn, err error)

ListenPacket is used to open an UDP connection on client side

type NatPacketUnit

type NatPacketUnit struct {
	net.PacketConn
	// contains filtered or unexported fields
}

type NatTable

type NatTable struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

NatTable used to map the incomming packet to the outgoing packet listener

func NewNatTable

func NewNatTable() *NatTable

NewNatTable returns an empty NatTable

func (*NatTable) Delete

func (table *NatTable) Delete(src string)

Delete deletes an item from the table can be called parallel

func (*NatTable) Get

func (table *NatTable) Get(src net.Addr) (*NatPacketUnit, bool)

func (*NatTable) Put

func (table *NatTable) Put(src net.Addr, packetln net.PacketConn)

type NetConnection

type NetConnection interface {
	net.Conn
	CloseWrite() error
	CloseRead() error
}

NetConnection inmlements the net.Conn & net.TcpConn with Shutdown liked function

type SecureConn

type SecureConn struct {
	net.Conn
	encrypt.Cipher
	// contains filtered or unexported fields
}

SecureConn is a secured connection with shadowsocks protocol also implements net.Conn interface

func (*SecureConn) Close

func (c *SecureConn) Close() error

Close closes the connection and free the buffer

func (*SecureConn) CloseRead

func (c *SecureConn) CloseRead() error

CloseRead closes the connection on read half

func (*SecureConn) CloseWrite

func (c *SecureConn) CloseWrite() error

CloseWrite closes the connection on write half

func (*SecureConn) Read

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

Read read the data from connection and decrypted with given cipher. the data may be cached and return with ErrAgain, that means more data is wantted for decryption

SecureConn Read will take best affort to read the data and decrypt no matter what cipher it is. The aead cipher data stream was encrypted data block which with the definitely length. So the cipher has a cache inside for tcp stream data caching, and then return the data bolck read from stream if the length is enough.

There get a second data cache here which caching the decrypted data in case the len of buffer is less than the data we decrypted. The remain data will append in the front of buffer for return when next read comes.

func (*SecureConn) Write

func (c *SecureConn) Write(b []byte) (n int, err error)

type SecurePacketConn

type SecurePacketConn struct {
	net.PacketConn
	encrypt.Cipher
	// contains filtered or unexported fields
}

SecurePacketConn is the implementation of net.PacketConn interfaces for shadowsocks UDP network connections.

func (*SecurePacketConn) ReadFrom

func (c *SecurePacketConn) ReadFrom(b []byte) (n int, src net.Addr, err error)

ReadFrom reads a packet from the connection.

func (*SecurePacketConn) WriteTo

func (c *SecurePacketConn) WriteTo(b []byte, dst net.Addr) (n int, err error)

WriteTo writes a packet with payload b to addr.

type ServerEntry

type ServerEntry struct {
	Address  string `json:"address"`
	Method   string `json:"method"`
	Password string `json:"password"`
}

ServerEntry give out basic elements a server needs

func (*ServerEntry) Check

func (se *ServerEntry) Check() error

Check the server entry if invalid

func (*ServerEntry) String

func (se *ServerEntry) String() string

Jump to

Keyboard shortcuts

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