core

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 NewUDPClientPacketAddr

func NewUDPClientPacketAddr(targetAddr socks.Addr, sourceAddr net.Addr) net.Addr

func NewUDPServerPacketAddr

func NewUDPServerPacketAddr(targetAddr, clientAddr net.Addr, session UDPSession) net.Addr

func UsersToEIHHash

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

func XORBytes

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

Types

type CacheItem

type CacheItem interface {
	LastUsed() time.Time
}

CacheItem is the interface that cache values must implement to be checked for expiration.

type ClientConfig

type ClientConfig struct {
	Cipher     ShadowCipher
	ServerAddr netip.AddrPort
	UDPTimeout time.Duration
}

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
	UDPTimeout time.Duration
}

type SessionCache

type SessionCache[K comparable, V CacheItem] struct {
	// contains filtered or unexported fields
}

SessionCache provides a thread-safe generic cache with background TTL eviction.

func NewSessionCache

func NewSessionCache[K comparable, V CacheItem](timeout time.Duration, onEvict func(K, V)) *SessionCache[K, V]

NewSessionCache creates a new generic SessionCache.

func (*SessionCache[K, V]) Close

func (c *SessionCache[K, V]) Close() error

Close stops the cleanup loop and clears the cache.

func (*SessionCache[K, V]) Delete

func (c *SessionCache[K, V]) Delete(key K)

Delete removes an item by key.

func (*SessionCache[K, V]) Get

func (c *SessionCache[K, V]) Get(key K) (V, bool)

Get retrieves an item by key.

func (*SessionCache[K, V]) Put

func (c *SessionCache[K, V]) Put(key K, value V)

Put adds or updates an item.

type SessionHash

type SessionHash string

func SessionHashFromAddrPort

func SessionHashFromAddrPort(addr netip.AddrPort) SessionHash

func SessionHashFromSessionID

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) (net.Conn, error)

func (*TCPClient) WrapConn

func (c *TCPClient) WrapConn(conn net.Conn, target socks.Addr) (net.Conn, error)

type TCPConn

type TCPConn interface {
	net.Conn

	InitServer() error                  // for server side
	InitClient(target socks.Addr) error // for client side
	Target() socks.Addr

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

type TCPConnCipher

type TCPConnCipher interface {
	TCPConn(net.Conn, []UserConfig, 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) (net.Conn, error)

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) UDPClient

func (*UDPClient) Init

func (c *UDPClient) Init() error

func (*UDPClient) WrapConn

func (c *UDPClient) WrapConn(pc net.PacketConn) net.PacketConn

type UDPConnCipher

type UDPConnCipher interface {
	NewUDPSessionManager(timeout time.Duration, users []UserConfig, 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) UDPServer

func (*UDPServer) Init

func (s *UDPServer) Init() error

func (*UDPServer) WrapConn

func (s *UDPServer) WrapConn(pc net.PacketConn) net.PacketConn

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

	// Get associated PacketConn (if any)
	Conn() net.PacketConn
	// Set associated PacketConn
	SetConn(net.PacketConn)
}

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, socks.Addr, []byte, error)

	// This function should return data from target, so it's maybe a block function.
	ServerHandleOutbound(plaintext []byte, target socks.Addr, 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