core

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2025 License: Apache-2.0 Imports: 10 Imported by: 4

Documentation

Index

Constants

View Source
const (
	ROLE_UNKNOWN int = 0
	ROLE_CLIENT  int = 1
	ROLE_SERVER  int = 2
)

Variables

This section is empty.

Functions

func Base64Decode

func Base64Decode(password string) ([]byte, error)

func GCMWithAES

func GCMWithAES(key []byte) (cipher.AEAD, error)

func Increment

func Increment(b []byte)

increment little-endian encoded unsigned integer b. Wrap around on overflow.

func UsersToEIHHash

func UsersToEIHHash(users []UserConfig) map[EIHHash]string

func XORBytes

func XORBytes(a, b []byte) ([]byte, error)

Types

type ClientConfig

type ClientConfig struct {
	Cipher ShadowCipher
}

type EIHHash

type EIHHash [16]byte // Extensible Identity Headers hash

type KeySizeError

type KeySizeError int

func (KeySizeError) Error

func (e KeySizeError) Error() string

type MetaCipher

type MetaCipher struct {
	Name      string
	KeySize   int
	SaltSize  int
	NonceSize int
	TagSize   int
}

type ServerConfig

type ServerConfig struct {
	Cipher ShadowCipher
	Users  []UserConfig
}

type SessionHash

type SessionHash string

func SessionHashFromAddrPort added in v0.1.1

func SessionHashFromAddrPort(addr netip.AddrPort) SessionHash

func SessionHashFromSessionID added in v0.1.1

func SessionHashFromSessionID(id uint64) SessionHash

type ShadowCipher

type ShadowCipher interface {
	TCPConnCipher
	UDPConnCipher
	SaltSize() int
	KeySize() int
	NonceSize() int
	TagSize() int
	Encrypter(key, salt []byte) (cipher.AEAD, error)
	Decrypter(key, salt []byte) (cipher.AEAD, error)
	// Start from SIP023, the cipher can hold multiple keys for clients
	// For server, user configuration should bind to implementation, ShadowCipher just save the main key
	Keys() [][]byte
	Key() []byte      // return last key
	FirstKey() []byte // return first key
}

type TCPClient

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

func NewTCPClient

func NewTCPClient(config ClientConfig) TCPClient

func (*TCPClient) Dial

func (c *TCPClient) Dial(target socks.Addr, server netip.AddrPort, padding, initialPayload []byte) (TCPConn, error)

func (*TCPClient) WrapConn added in v0.1.1

func (c *TCPClient) WrapConn(conn net.Conn, target socks.Addr, padding, initialPayload []byte) (TCPConn, error)

type TCPConfig

type TCPConfig struct {
	Users []UserConfig
}

configurartions for shadowsocks TCP connections

type TCPConn

type TCPConn interface {
	net.Conn

	InitServer() error                                                  // for server side
	InitClient(target socks.Addr, padding, initialPayload []byte) error // for client side
	Target() socks.Addr

	ClientFirstWrite() error // send headers, for fast opening
}

type TCPConnCipher

type TCPConnCipher interface {
	TCPConn(net.Conn, TCPConfig, int) TCPConn
}

type TCPServer

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

func NewTCPServer

func NewTCPServer(config ServerConfig) TCPServer

func (*TCPServer) Init

func (s *TCPServer) Init() error

func (*TCPServer) WrapConn

func (s *TCPServer) WrapConn(conn net.Conn) (TCPConn, error)

This is a block function When some errors lead to unexpected closing of conn, the caller MUST act in a way that does not exhibit the amount of bytes consumed by the server. This defends against probes that send one byte at a time to detect how many bytes the server consumes before closing the connection.

type UDPClient

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

Clients create UDP relay sessions based on source address and port. When a client receives a packet from a new source address and port, it opens a new relay session, and subsequent packets from that source are sent over the same session.

func NewUDPClient

func NewUDPClient(config ClientConfig, timeout int) UDPClient

func (*UDPClient) Inbound

func (c *UDPClient) Inbound(payload []byte, clientAddr netip.AddrPort, target socks.Addr) (UDPSession, []byte, error)

func (*UDPClient) Init

func (c *UDPClient) Init() error

func (*UDPClient) Outbound

func (c *UDPClient) Outbound(encryted []byte, session UDPSession) ([]byte, error)

type UDPConfig

type UDPConfig struct {
	Users []UserConfig
}

type UDPConnCipher

type UDPConnCipher interface {
	NewUDPSessionManager(timeout time.Duration, config UDPConfig, windowSize, role int) UDPSessionManager
}

type UDPServer

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

Servers manage UDP relay sessions by session ID. Each client session corresponds to one outgoing UDP socket on the server.

func NewUDPServer

func NewUDPServer(config ServerConfig, timeout time.Duration) UDPServer

func (*UDPServer) Inbound

func (s *UDPServer) Inbound(encrypted []byte, clientAddr netip.AddrPort) (UDPSession, []byte, error)

func (*UDPServer) Init

func (s *UDPServer) Init() error

func (*UDPServer) Outbound

func (s *UDPServer) Outbound(plaintext []byte, session UDPSession) ([]byte, error)

type UDPSession

type UDPSession interface {
	// Target returns the destination address for this session
	Target() socks.Addr

	ClientAddr() netip.AddrPort

	// LastUsed returns when this session was last used
	LastUsed() time.Time

	// update LastUsed
	Touch()

	SessionID() uint64

	// unique id for identifying session
	Hash() SessionHash
}

UDPSession represents a single UDP relay session. This is used by protocol-specific managers internally.

type UDPSessionManager

type UDPSessionManager interface {
	// This function should complete following things:
	// 1. Decrypt data and send to target
	// 2. Validate session
	ServerHandleInbound(encrypted []byte, clientAddr netip.AddrPort) (UDPSession, []byte, error)

	// This function should return data from target, so it's maybe a block function.
	ServerHandleOutbound(plaintext []byte, session UDPSession) ([]byte, error)

	ClientHandleInbound(payload []byte, target socks.Addr, clientAddr netip.AddrPort) (UDPSession, []byte, error)

	// This function should return data from target, so it's maybe a block function.
	ClientHandleOutbound(encrypted []byte, session UDPSession) ([]byte, error)
}

UDPSessionManager is the interface that abstracts session management for different Shadowsocks protocols (AEAD vs SIP022). For server, the dataflow is: client encrypted data -> ServerHandleReceive -> target and plaintext -> target server target server -> plaintext -> ServerHandleOutbound -> encrypted data -> client For Client, the dataflow is: app -> plaintext -> ServerHandleReceive -> encrypted -> ss server ss server -> encrypted data -> ServerHandleOutbound -> plaintext -> app

type UserConfig

type UserConfig struct {
	Name     string
	Password string
}

func NewUserConfig

func NewUserConfig(name, password string) UserConfig

Jump to

Keyboard shortcuts

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