shadowsocks

package
v0.0.0-...-1b6a249 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2019 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const DEFAULT_BUF_SIZE = 3072
View Source
const HKDF_INFO = "ss-subkey"
View Source
const HTTP_CONTENT_CHUNKED = -2
View Source
const HTTP_CONTENT_EOF = -1
View Source
const LEN_SIZE = 2
View Source
const MAX_BUF_SIZE = 32768
View Source
const MAX_READ_SIZE = 2048
View Source
const MAX_WRITE_CHUNK_SIZE = 2048
View Source
const SO_ORIGINAL_DST = 80

Variables

View Source
var Ciphers = map[string]*CipherInfo{}
View Source
var ERR_AUTH_FAIL = NewAuthError("Authentication failure")
View Source
var ERR_BUF_SIZE_EXCEED = NewError("Maximum buffer size exceeded")
View Source
var ERR_DUP_SALT = NewAuthError("Duplicated salt (maybe replay attack)")
View Source
var ERR_HTTP_HOST_TOO_LONG = NewError("HTTP host too long")
View Source
var ERR_HTTP_INVALID_HEADER = NewError("Invalid HTTP header")
View Source
var ERR_HTTP_MANAGER_DEAD = NewError("HTTP manager is dead")
View Source
var ERR_INVALID_ADDR = NewError("Invalid address")
View Source
var ERR_INVALID_ADDR_TYPE = NewError("Invalid address type")
View Source
var ERR_INVALID_CHUNK_SIZE = NewError("Invalid chunk size")
View Source
var ERR_MAX_CHUNK_SIZE_EXCEED = NewError("Maximum chunk size exceeded")
View Source
var ERR_SERVER_NOT_EXIST = NewError("Server does not exist")
View Source
var ERR_SOCKS4_COMMAND_NOT_SUPPORTED = NewError("Unsupported socks4 command")
View Source
var ERR_SOCKS4_INVALID_PROTOCOL = NewError("Invalid socks4 protocol")
View Source
var ERR_SOCKS5_COMMAND_NOT_SUPPORTED = NewError("Unsupported socks5 command")
View Source
var ERR_SOCKS5_INVALID_PROTOCOL = NewError("Invalid socks5 protocol")
View Source
var ERR_SOCKS5_NO_VALID_AUTH = NewError("Socks5 request requires auth")
View Source
var ERR_UNIMPLEMENTED = NewError("Unimplemented")

Functions

func DPipe

func DPipe(conn1, conn2 SSConn, buf12, buf21 *SSBuffer, res chan error)

DPipe is a utility to pipe bi-directionally.

func DeriveKey

func DeriveKey(key []byte, pass []byte)

DeriveKey derives the key of given size from the password.

func DetectHTTP

func DetectHTTP(buf *SSBuffer) bool

DetectHTTP detects whether the buffer contains valid HTTP proxy request.

Protocol definition: RFC 7230 5.3.2, RFC 7231 4.3.6
https://www.ietf.org/rfc/rfc7230.txt
https://www.ietf.org/rfc/rfc7231.txt

func DetectRedir

func DetectRedir(tconn SSConn) bool

func DetectSocks4

func DetectSocks4(buf *SSBuffer) bool

DetectSocks4 detects whether the buffer contains a valid socks4(a) request.

Protocol definition:
https://www.openssh.com/txt/socks4.protocol
https://www.openssh.com/txt/socks4a.protocol

func DetectSocks5

func DetectSocks5(buf *SSBuffer) bool

DetectSocks5 detects whether the buffer contains valid socks5 request.

Protocol definition: RFC 1928
https://www.ietf.org/rfc/rfc1928.txt

func FDAttain

func FDAttain()

func FDGetCur

func FDGetCur() int

func FDRelease

func FDRelease()

func FDSetMax

func FDSetMax(max int)

func HTTPPipeBody

func HTTPPipeBody(in SSConn, buf *SSBuffer, out SSConn, header HTTPHeader, res chan error)

func HTTPReadLine

func HTTPReadLine(tconn SSConn, buf *SSBuffer, i_offset int) (line string, offset int, err error)

func HTTPWrite400

func HTTPWrite400(conn SSConn) error

func HTTPWrite502

func HTTPWrite502(conn SSConn) error

func HTTPWriteHeader

func HTTPWriteHeader(conn SSConn, header HTTPHeader) (err error)

func IsAuthError

func IsAuthError(err error) bool

func IsIPv6

func IsIPv6(host string) bool

IsIPv6 checks whether an address is IPv6. It does not guarantee that the address is valid, so you should only check it on an IP address or a hostname.

func NewAuthError

func NewAuthError(message string) error

func NewError

func NewError(message string) error

func NewKeyDeriver

func NewKeyDeriver(pass []byte) io.Reader

NewKeyDeriver creates a new KeyDeriver.

func ParseAddress

func ParseAddress(buf []byte) (addr string, n int, err error)

ParseAddress parses an address buffer into string. It returns resulting address, length of bytes required, and error if exists. You must check that len(buf) >= n, otherwise addr is not a valid address.

func Pipe

func Pipe(reader, writer SSConn, buf *SSBuffer, res chan error)

Pipe is a utility to pipe from the reader to the writer, and writes the error to res (may be nil).

func UnwrapAddr

func UnwrapAddr(addr string) (host string, port uint16, err error)

func WrapAddr

func WrapAddr(host string, port uint16) string

Types

type AEADCipherFactory

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

AEADCipherFactory implements CipherFactory with AEAD ciphers.

Specification:
https://shadowsocks.org/en/spec/AEAD-Ciphers.html

func (*AEADCipherFactory) Wrap

func (a *AEADCipherFactory) Wrap(c PlainConn) SSConn

type AEADConn

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

AEADCipherConn implements SSConn with given AEAD cipher.

func (*AEADConn) Alive

func (c *AEADConn) Alive() bool

func (*AEADConn) Close

func (c *AEADConn) Close() (err error)

func (*AEADConn) RemoteAddr

func (c *AEADConn) RemoteAddr() string

func (*AEADConn) SSRead

func (c *AEADConn) SSRead(b *SSBuffer) (err error)

func (*AEADConn) SSWrite

func (c *AEADConn) SSWrite(b *SSBuffer) (err error)

type CipherFactory

type CipherFactory interface {
	Wrap(PlainConn) SSConn
}

CipherFactory is a factory that wraps PlainConn into encrypted connections.

func NewAEADCipherFactory

func NewAEADCipherFactory(newCipher NewAEADCipherFunc, keySize, saltSize int, key []byte) CipherFactory

type CipherInfo

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

type ClientContext

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

ClientContext represents an instance of client. It listens on a local port, and is configured to connect certain server address using specified encryption. It accepts several protocols, e.g. HTTP proxy, socks4(a), socks5. The combinition should be able to be configured in the future.

func NewClientContext

func NewClientContext(config Config) (ctx ClientContext, err error)

NewClientContext creates a new client context.

func (*ClientContext) DialServer

func (ctx *ClientContext) DialServer() (conn SSConn, err error)

func (*ClientContext) HandleConnection

func (ctx *ClientContext) HandleConnection(conn net.Conn)

HandleConnection handles client connections, checking input buffer, and dispatch connections to different protocol handler.

func (*ClientContext) HandleHTTP

func (ctx *ClientContext) HandleHTTP(tconn SSConn, buf *SSBuffer) (err error)

HandleHTTP handles a HTTP/1.0 or HTTP/1.1 proxy connection.

func (*ClientContext) HandleRedir

func (ctx *ClientContext) HandleRedir(tconn SSConn, buf *SSBuffer) (err error)

func (*ClientContext) HandleSocks4

func (ctx *ClientContext) HandleSocks4(tconn SSConn, buf *SSBuffer) (err error)

HandleSocks4 handles a socks4(a) connection.

func (*ClientContext) HandleSocks5

func (ctx *ClientContext) HandleSocks5(tconn SSConn, buf *SSBuffer) (err error)

HandleSocks5 handles a socks5 connection.

func (*ClientContext) Run

func (ctx *ClientContext) Run()

Run runs a client. Usually this should be run in a goroutine.

func (*ClientContext) Stop

func (ctx *ClientContext) Stop()

Stop stops the client running goroutine.

func (*ClientContext) Wait

func (ctx *ClientContext) Wait() (err error)

Wait waits the client to stop and return its error

type Config

type Config struct {
	// Server listening address
	ServerHost string
	// Server listening port
	ServerPort uint16
	// Local listening address (Client only)
	LocalHost string
	// Local listening port (Client only)
	LocalPort uint16
	// Encryption method
	Method string
	// Key generator
	KeyDeriver io.Reader
	// TCP keepalive timeout
	Timeout time.Duration
	// Connect IPv4 address only (Server only)
	ConnectV4Only bool
	// New connection timeout (Server only)
	ConnectTimeout time.Duration
}

func DefaultConfig

func DefaultConfig() Config

type DelayInitConn

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

func NewDelayInitConn

func NewDelayInitConn(conn SSConn, initBuf []byte) *DelayInitConn

func (*DelayInitConn) Alive

func (c *DelayInitConn) Alive() bool

func (*DelayInitConn) Close

func (c *DelayInitConn) Close() error

func (*DelayInitConn) RemoteAddr

func (c *DelayInitConn) RemoteAddr() string

func (*DelayInitConn) SSRead

func (c *DelayInitConn) SSRead(buf *SSBuffer) error

func (*DelayInitConn) SSWrite

func (c *DelayInitConn) SSWrite(buf *SSBuffer) error

type HTTPConnCtx

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

type HTTPConnectionManager

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

func NewHTTPConnectionManager

func NewHTTPConnectionManager(ctx *ClientContext) (m *HTTPConnectionManager)

func (*HTTPConnectionManager) Delete

func (m *HTTPConnectionManager) Delete()

func (*HTTPConnectionManager) Get

func (m *HTTPConnectionManager) Get(addr string) (hctx *HTTPConnCtx, err error)

func (*HTTPConnectionManager) Release

func (m *HTTPConnectionManager) Release(hctx *HTTPConnCtx)

type HTTPHeader

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

func HTTPParseHeader

func HTTPParseHeader(tconn SSConn, buf *SSBuffer, req bool) (header HTTPHeader, err error)

func (*HTTPHeader) ContentLength

func (header *HTTPHeader) ContentLength() (int64, error)

func (*HTTPHeader) KeepAlive

func (header *HTTPHeader) KeepAlive() bool

func (*HTTPHeader) Method

func (header *HTTPHeader) Method() (string, error)

func (*HTTPHeader) Status

func (header *HTTPHeader) Status() (int, error)

func (*HTTPHeader) URL

func (header *HTTPHeader) URL() (string, error)

func (*HTTPHeader) URLHost

func (header *HTTPHeader) URLHost() (string, error)

func (*HTTPHeader) URLRel

func (header *HTTPHeader) URLRel() (string, error)

func (*HTTPHeader) Version

func (header *HTTPHeader) Version() int

type HTTPHeaderField

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

type KeyDeriver

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

KeyDeriver is a context of key deriving.

func (*KeyDeriver) Read

func (r *KeyDeriver) Read(p []byte) (n int, err error)

Read implements io.Reader. It writes derived key into the buffer and returns bytes written. err is guaranteed to be nil.

type NewAEADCipherFunc

type NewAEADCipherFunc func([]byte) (cipher.AEAD, error)

type NewCipherFactoryFunc

type NewCipherFactoryFunc func([]byte) CipherFactory

type Nonce

type Nonce []byte

Nonce is a nonce.

func NewNonce

func NewNonce(n int) Nonce

NewNonce creates a nonce with given size.

func (Nonce) Inc

func (non Nonce) Inc()

Inc increases the nonce.

type PlainConn

type PlainConn struct {
	TCPConn *net.TCPConn
}

PlainConn is a SSConn wrapped on TCPConn.

func (PlainConn) Alive

func (c PlainConn) Alive() bool

func (PlainConn) Close

func (c PlainConn) Close() error

func (PlainConn) RemoteAddr

func (c PlainConn) RemoteAddr() string

func (PlainConn) SSRead

func (c PlainConn) SSRead(b *SSBuffer) (err error)

func (PlainConn) SSReadTimeout

func (c PlainConn) SSReadTimeout(b *SSBuffer, millis int64) error

func (PlainConn) SSWrite

func (c PlainConn) SSWrite(b *SSBuffer) error

type SBFSaltFilter

type SBFSaltFilter struct{}

func (*SBFSaltFilter) Add

func (f *SBFSaltFilter) Add(salt []byte)

func (*SBFSaltFilter) Clean

func (f *SBFSaltFilter) Clean()

func (*SBFSaltFilter) Contains

func (f *SBFSaltFilter) Contains(salt []byte) bool

type SSBuffer

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

SSBuffer contains a buffer, currently a simple []byte. The content is to be sent. The capacity of the slice will be reused when reading.

func NewBuffer

func NewBuffer() *SSBuffer

NewBuffer creates a buffer with default size.

func (*SSBuffer) Expand

func (b *SSBuffer) Expand(n int) error

Expand expands a buffer to either twice of its original size or the inputed size, unless the size exceeds the maximum buffer size when it generates a BUF_SIZE_EXCEED error.

type SSConn

type SSConn interface {
	// SSRead reads some data into buffer. The size is uncertain.
	// Data is appended to the buffer.
	SSRead(*SSBuffer) error
	// SSWrite writes the data to the connection. It should write
	// the whole buffer and reset the buffer, otherwise it will
	// report an error.
	SSWrite(*SSBuffer) error
	// Close closes the connection.
	Close() error
	// Alive checks whether the connection is alive.
	Alive() bool
	// RemoteAddr returns the address of remote endpoint
	// note this should not used to Dial. It is currently
	// used for debugging.
	RemoteAddr() string
}

SSConn represents a Shadowsocks accepted connection (maybe).

type SSError

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

func (*SSError) Error

func (e *SSError) Error() string

type SaltFilter

type SaltFilter interface {
	Contains(salt []byte) bool
	Add(salt []byte)
	Clean()
}

type ServerContext

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

ServerContext represents an instance of Shadowsocks server which listens on a single port and accept a single kind of encryption.

func NewServerContext

func NewServerContext(config Config) (ctx ServerContext, err error)

NewServerContext creates a new instance of ServerContext with specified arguments.

func (*ServerContext) HandleConnection

func (ctx *ServerContext) HandleConnection(conn net.Conn)

HandleConnection handles a newly accepted connection with configured ciphers.

func (*ServerContext) Run

func (ctx *ServerContext) Run()

Run runs the server, normally running in a new goroutine.

func (*ServerContext) Stop

func (ctx *ServerContext) Stop()

Stop stops the running server.

func (*ServerContext) Wait

func (ctx *ServerContext) Wait() (err error)

Wait waits the server to stop and return its error.

type ServerManager

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

func NewServerManager

func NewServerManager() ServerManager

func (*ServerManager) Add

func (m *ServerManager) Add(config Config) (err error)

func (*ServerManager) Listen

func (m *ServerManager) Listen(addr string) (err error)

func (*ServerManager) Remove

func (m *ServerManager) Remove(host string, port uint16) (err error)

type SyncSaltFilter

type SyncSaltFilter struct {
	SaltFilter
	// contains filtered or unexported fields
}

func WrapSyncSaltFilter

func WrapSyncSaltFilter(f SaltFilter) (s *SyncSaltFilter)

func (*SyncSaltFilter) Add

func (f *SyncSaltFilter) Add(salt []byte)

func (*SyncSaltFilter) Clean

func (f *SyncSaltFilter) Clean()

func (*SyncSaltFilter) Contains

func (f *SyncSaltFilter) Contains(salt []byte) bool

Jump to

Keyboard shortcuts

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