Documentation
¶
Index ¶
- Constants
- func Base64Decode(password string) ([]byte, error)
- func GCMWithAES(key []byte) (cipher.AEAD, error)
- func Increment(b []byte)
- func NewUDPClientPacketAddr(targetAddr socks.Addr, sourceAddr net.Addr) net.Addr
- func NewUDPServerPacketAddr(targetAddr, clientAddr net.Addr, session UDPSession) net.Addr
- func UsersToEIHHash(users []UserConfig) map[EIHHash]UserConfig
- func XORBytes(a, b []byte) ([]byte, error)
- type CacheItem
- type ClientConfig
- type EIHHash
- type KeySizeError
- type MetaCipher
- type ServerConfig
- type SessionCache
- type SessionHash
- type ShadowCipher
- type TCPClient
- type TCPConn
- type TCPConnCipher
- type TCPServer
- type UDPClient
- type UDPConnCipher
- type UDPServer
- type UDPSession
- type UDPSessionManager
- type UserConfig
Constants ¶
const ( ROLE_UNKNOWN int = 0 ROLE_CLIENT int = 1 ROLE_SERVER int = 2 )
Variables ¶
This section is empty.
Functions ¶
func Base64Decode ¶
func Increment ¶
func Increment(b []byte)
increment little-endian encoded unsigned integer b. Wrap around on overflow.
func NewUDPClientPacketAddr ¶
func NewUDPServerPacketAddr ¶
func NewUDPServerPacketAddr(targetAddr, clientAddr net.Addr, session UDPSession) net.Addr
func UsersToEIHHash ¶
func UsersToEIHHash(users []UserConfig) map[EIHHash]UserConfig
Types ¶
type CacheItem ¶
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 KeySizeError ¶
type KeySizeError int
func (KeySizeError) Error ¶
func (e KeySizeError) Error() string
type MetaCipher ¶
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
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
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) 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) 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 ¶
func NewUserConfig ¶
func NewUserConfig(name, password string) UserConfig