ss2022

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2022 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderTypeClientStream = 0
	HeaderTypeServerStream = 1

	HeaderTypeClientPacket = 0
	HeaderTypeServerPacket = 1

	MinPaddingLength = 0
	MaxPaddingLength = 900

	IdentityHeaderLength = 16

	// type + unix epoch timestamp + u16be length
	TCPRequestFixedLengthHeaderLength = 1 + 8 + 2

	// SOCKS address + padding length + padding
	TCPRequestVariableLengthHeaderNoPayloadMaxLength = socks5.MaxAddrLen + 2 + MaxPaddingLength

	// type + unix epoch timestamp + request salt + u16be length
	TCPResponseHeaderMaxLength = 1 + 8 + 32 + 2

	// session ID + packet ID
	UDPSeparateHeaderLength = 8 + 8

	// type + unix epoch timestamp + padding length
	UDPClientMessageHeaderFixedLength = 1 + 8 + 2

	// type + unix epoch timestamp + client session id + padding length
	UDPServerMessageHeaderFixedLength = 1 + 8 + 8 + 2

	// type + unix epoch timestamp + padding length + padding + SOCKS address
	UDPClientMessageHeaderMaxLength = UDPClientMessageHeaderFixedLength + MaxPaddingLength + socks5.MaxAddrLen

	// type + unix epoch timestamp + client session id + padding length + padding + SOCKS address
	UDPServerMessageHeaderMaxLength = UDPServerMessageHeaderFixedLength + MaxPaddingLength + socks5.MaxAddrLen

	// MaxEpochDiff is the maximum allowed time difference between a received timestamp and system time.
	MaxEpochDiff = 30

	// MaxTimeDiff is the maximum allowed time difference between a received timestamp and system time.
	MaxTimeDiff = MaxEpochDiff * time.Second

	// ReplayWindowDuration defines the amount of time during which a salt check is necessary.
	ReplayWindowDuration = MaxTimeDiff * 2
)
View Source
const MaxPayloadSize = 0xFFFF

Variables

View Source
var (
	ErrUnknownMethod = errors.New("unknown method")
	ErrBadPSKLength  = errors.New("PSK length does not meet method requirements")
)
View Source
var (
	ErrIncompleteHeaderInFirstChunk  = errors.New("header in first chunk is missing or incomplete")
	ErrPaddingExceedChunkBorder      = errors.New("padding in first chunk is shorter than advertised")
	ErrBadTimestamp                  = errors.New("time diff is over 30 seconds")
	ErrTypeMismatch                  = errors.New("header type mismatch")
	ErrPaddingLengthOutOfRange       = errors.New("padding length is less than 0 or greater than 900")
	ErrClientSaltMismatch            = errors.New("client salt in response header does not match request")
	ErrClientSessionIDMismatch       = errors.New("client session ID in server message header does not match current session")
	ErrTooManyServerSessions         = errors.New("server session changed more than once during the last minute")
	ErrPacketIncompleteHeader        = errors.New("packet contains incomplete header")
	ErrPacketMissingPayload          = errors.New("packet has no payload")
	ErrReplay                        = errors.New("detected replay")
	ErrIdentityHeaderUserPSKNotFound = errors.New("decrypted identity header does not match any known uPSK")
)
View Source
var (
	ErrZeroLengthChunk = errors.New("length in length chunk is zero")
	ErrFirstRead       = errors.New("failed to read fixed-length header in one read call")
	ErrRepeatedSalt    = errors.New("detected replay: repeated salt")
)

Functions

func NoPadding

func NoPadding(_ conn.Addr) bool

NoPadding is a PaddingPolicy that never adds padding.

func PadAll

func PadAll(_ conn.Addr) bool

PadAll is a PaddingPolicy that adds padding to all traffic.

func PadPlainDNS

func PadPlainDNS(targetAddr conn.Addr) bool

PadPlainDNS is a PaddingPolicy that adds padding to plain DNS traffic.

func ParseSessionIDAndPacketID

func ParseSessionIDAndPacketID(b []byte) (sid, pid uint64)

ParseSessionIDAndPacketID parses the session ID and packet ID segment of a decrypted UDP packet.

The buffer must be exactly 16 bytes long. No buffer length checks are performed.

Session ID and packet ID segment:

+------------+-----------+
| session ID | packet ID |
+------------+-----------+
|     8B     |   u64be   |
+------------+-----------+

func ParseTCPRequestFixedLengthHeader

func ParseTCPRequestFixedLengthHeader(b []byte) (n int, err error)

ParseTCPRequestFixedLengthHeader parses a TCP request fixed-length header and returns the length of the variable-length header, or an error if header validation fails.

The buffer must be exactly 11 bytes long. No buffer length checks are performed.

Request fixed-length header:

+------+---------------+--------+
| type |   timestamp   | length |
+------+---------------+--------+
|  1B  | 8B unix epoch |  u16be |
+------+---------------+--------+

func ParseTCPRequestVariableLengthHeader

func ParseTCPRequestVariableLengthHeader(b []byte) (targetAddr conn.Addr, payload []byte, err error)

ParseTCPRequestVariableLengthHeader parses a TCP request variable-length header and returns the target address, the initial payload if available, or an error if header validation fails.

This function does buffer length checks and returns ErrIncompleteHeaderInFirstChunk if the buffer is too short.

Request variable-length header:

+------+----------+-------+----------------+----------+-----------------+
| ATYP |  address |  port | padding length |  padding | initial payload |
+------+----------+-------+----------------+----------+-----------------+
|  1B  | variable | u16be |     u16be      | variable |    variable     |
+------+----------+-------+----------------+----------+-----------------+

func ParseTCPResponseHeader

func ParseTCPResponseHeader(b []byte, requestSalt []byte) (n int, err error)

ParseTCPResponseHeader parses a TCP response fixed-length header and returns the length of the next payload chunk, or an error if header validation fails.

The buffer must be exactly 1 + 8 + salt length + 2 bytes long. No buffer length checks are performed.

Response fixed-length header:

+------+---------------+----------------+--------+
| type |   timestamp   |  request salt  | length |
+------+---------------+----------------+--------+
|  1B  | 8B unix epoch |     16/32B     |  u16be |
+------+---------------+----------------+--------+

func ParseUDPClientMessageHeader

func ParseUDPClientMessageHeader(b []byte, cachedDomain string) (targetAddr conn.Addr, updatedCachedDomain string, payloadStart, payloadLen int, err error)

ParseUDPClientMessageHeader parses a UDP client message header and returns the target address and payload, or an error if header validation fails or no payload is in the buffer.

This function accepts buffers of arbitrary lengths.

The buffer is expected to contain a decrypted client message in the following format:

+------+---------------+----------------+----------+------+----------+-------+----------+
| type |   timestamp   | padding length |  padding | ATYP |  address |  port |  payload |
+------+---------------+----------------+----------+------+----------+-------+----------+
|  1B  | 8B unix epoch |     u16be      | variable |  1B  | variable | u16be | variable |
+------+---------------+----------------+----------+------+----------+-------+----------+

func ParseUDPServerMessageHeader

func ParseUDPServerMessageHeader(b []byte, csid uint64) (payloadSourceAddrPort netip.AddrPort, payloadStart, payloadLen int, err error)

ParseUDPServerMessageHeader parses a UDP server message header and returns the payload source address and payload, or an error if header validation fails or no payload is in the buffer.

This function accepts buffers of arbitrary lengths.

The buffer is expected to contain a decrypted server message in the following format:

+------+---------------+-------------------+----------------+----------+------+----------+-------+----------+
| type |   timestamp   | client session ID | padding length |  padding | ATYP |  address |  port |  payload |
+------+---------------+-------------------+----------------+----------+------+----------+-------+----------+
|  1B  | 8B unix epoch |         8B        |     u16be      | variable |  1B  | variable | u16be | variable |
+------+---------------+-------------------+----------------+----------+------+----------+-------+----------+

func ValidateUnixEpochTimestamp

func ValidateUnixEpochTimestamp(b []byte) error

ValidateUnixEpochTimestamp validates the Unix Epoch timestamp in the buffer and returns an error if the timestamp exceeds the allowed time difference from system time.

This function does not check buffer length. Make sure it's exactly 8 bytes long.

func WriteSessionIDAndPacketID

func WriteSessionIDAndPacketID(b []byte, sid, pid uint64)

WriteSessionIDAndPacketID writes the session ID and packet ID to the buffer.

The buffer must be exactly 16 bytes long. No buffer length checks are performed.

func WriteTCPRequestFixedLengthHeader

func WriteTCPRequestFixedLengthHeader(b []byte, length uint16)

WriteTCPRequestFixedLengthHeader writes a TCP request fixed-length header into the buffer.

The buffer must be at least 11 bytes long. No buffer length checks are performed.

func WriteTCPRequestVariableLengthHeader

func WriteTCPRequestVariableLengthHeader(b []byte, targetAddr conn.Addr, payload []byte) (n int)

WriteTCPRequestVariableLengthHeader writes a TCP request variable-length header into the buffer and returns the number of bytes written.

The buffer must be at least socks5.LengthOfAddrFromConnAddr(targetAddr) + 2 + MaxPaddingLength bytes long if there's no payload, or socks5.LengthOfAddrFromConnAddr(targetAddr) + 2 + len(payload) bytes long if there's initial payload. The total header length must not exceed MaxPayloadSize.

func WriteTCPResponseHeader

func WriteTCPResponseHeader(b []byte, requestSalt []byte, length uint16) (n int)

WriteTCPResponseHeader writes a TCP response fixed-length header into the buffer and returns the number of bytes written.

This function does not check buffer length. The buffer must be at least 1 + 8 + salt length + 2 bytes long.

func WriteUDPClientMessageHeader

func WriteUDPClientMessageHeader(b []byte, targetAddr conn.Addr, shouldPad PaddingPolicy) (n int, err error)

WriteUDPClientMessageHeader writes a UDP client message header into the buffer, starting from the end of the buffer towards the beginning, and returns the number of bytes written.

This function checks buffer length and adds padding with best efforts. The buffer must be at least 1 + 8 + 2 + len(targetAddr) bytes long.

func WriteUDPServerMessageHeader

func WriteUDPServerMessageHeader(b []byte, csid uint64, sourceAddrPort netip.AddrPort, shouldPad PaddingPolicy) (n int, err error)

WriteUDPServerMessageHeader writes a UDP server message header into the buffer, starting from the end of the buffer towards the beginning, and returns the number of bytes written.

This function checks buffer length and adds padding with best efforts. The buffer must be at least 1 + 8 + 8 + 2 + len(targetAddr) bytes long.

Types

type CipherConfig

type CipherConfig struct {
	// Client: uPSK
	// Server: iPSK or uPSK
	PSK []byte

	// Client: iPSKs
	// Server: uPSKs
	PSKs [][]byte
	// contains filtered or unexported fields
}

func NewCipherConfig

func NewCipherConfig(method string, psk []byte, psks [][]byte) (*CipherConfig, error)

func NewRandomCipherConfig

func NewRandomCipherConfig(method string, keySize, eihCount int) (cipherConfig *CipherConfig, err error)

func (*CipherConfig) ClientPSKHashes

func (c *CipherConfig) ClientPSKHashes() [][IdentityHeaderLength]byte

ClientPSKHashes returns the BLAKE3 hashes of c.PSKs[1:] and c.PSK.

func (*CipherConfig) NewAEAD

func (c *CipherConfig) NewAEAD(salt []byte) cipher.AEAD

func (*CipherConfig) NewBlock

func (c *CipherConfig) NewBlock() cipher.Block

func (*CipherConfig) NewShadowStreamCipher

func (c *CipherConfig) NewShadowStreamCipher(salt []byte) *ShadowStreamCipher

func (*CipherConfig) NewTCPIdentityHeaderClientCiphers

func (c *CipherConfig) NewTCPIdentityHeaderClientCiphers(salt []byte) []cipher.Block

NewTCPIdentityHeaderClientCiphers creates block ciphers for a client TCP session's identity headers.

func (*CipherConfig) NewTCPIdentityHeaderServerCipher

func (c *CipherConfig) NewTCPIdentityHeaderServerCipher(salt []byte) cipher.Block

NewTCPIdentityHeaderServerCiphers creates a block cipher for a server TCP session's identity header.

func (*CipherConfig) NewUDPIdentityHeaderClientCiphers

func (c *CipherConfig) NewUDPIdentityHeaderClientCiphers() []cipher.Block

NewUDPIdentityHeaderClientCiphers creates block ciphers for a client UDP service's identity headers.

func (*CipherConfig) NewUDPIdentityHeaderServerCipher

func (c *CipherConfig) NewUDPIdentityHeaderServerCipher() cipher.Block

NewUDPIdentityHeaderServerCipher creates a block cipher for a server UDP service's identity header.

func (*CipherConfig) ServerPSKHashMap

func (c *CipherConfig) ServerPSKHashMap() map[[IdentityHeaderLength]byte]*CipherConfig

ServerPSKHashMap returns a uPSKHash-*CipherConfig map.

type Filter

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

Filter maintains a sliding window of uint64 counters.

func (*Filter) Add

func (f *Filter) Add(counter uint64) bool

Add attempts to add counter to the sliding window and returns whether the counter is successfully added to the sliding window.

func (*Filter) IsOk

func (f *Filter) IsOk(counter uint64) bool

IsOk checks whether counter can be accepted by the sliding window filter.

func (*Filter) MustAdd

func (f *Filter) MustAdd(counter uint64)

MustAdd adds counter to the sliding window without checking if the counter is valid. Call IsOk beforehand to make sure the counter is valid.

func (*Filter) Reset

func (f *Filter) Reset()

Reset resets the filter to its initial state.

type HeaderError

type HeaderError[T any] struct {
	Err      error
	Expected T
	Got      T
}

func (*HeaderError[T]) Error

func (e *HeaderError[T]) Error() string

func (*HeaderError[T]) Unwrap

func (e *HeaderError[T]) Unwrap() error

type PaddingPolicy

type PaddingPolicy func(targetAddr conn.Addr) (shouldPad bool)

PaddingPolicy is a function that takes the target address and returns whether padding should be added.

func ParsePaddingPolicy

func ParsePaddingPolicy(paddingPolicy string) (PaddingPolicy, error)

ParsePaddingPolicy parses a string representation of a PaddingPolicy.

type SaltPool

type SaltPool[T comparable] struct {
	// contains filtered or unexported fields
}

SaltPool stores salts for [retention, 2*retention) to protect against replay attacks during the replay window.

func NewSaltPool

func NewSaltPool[T comparable](retention time.Duration) *SaltPool[T]

NewSaltPool creates a new SaltPool with retention as the minimum amount of time during which an added salt is guaranteed to stay in the pool.

func (*SaltPool[T]) Add

func (p *SaltPool[T]) Add(salt T) bool

Add cleans the pool, checks if the salt already exists in the pool, and adds the salt to the pool if the salt is not already in the pool. Server time, instead of the header timestamp, is used, to prevent potential issues when cleaning up.

type ShadowPacketClientPacker

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

ShadowPacketClientPacker packs UDP packets into authenticated and encrypted Shadowsocks packets.

ShadowPacketClientPacker implements the zerocopy.Packer interface.

Packet format:

+---------------------------+-----+-----+---------------------------+
| encrypted separate header | EIH | ... |       encrypted body      |
+---------------------------+-----+-----+---------------------------+
|            16B            | 16B | ... | variable length + 16B tag |
+---------------------------+-----+-----+---------------------------+

func (*ShadowPacketClientPacker) FrontHeadroom

func (p *ShadowPacketClientPacker) FrontHeadroom() int

FrontHeadroom implements the zerocopy.ClientPacker FrontHeadroom method.

func (*ShadowPacketClientPacker) PackInPlace

func (p *ShadowPacketClientPacker) PackInPlace(b []byte, targetAddr conn.Addr, payloadStart, payloadLen int) (destAddrPort netip.AddrPort, packetStart, packetLen int, err error)

PackInPlace implements the zerocopy.ClientPacker PackInPlace method.

func (*ShadowPacketClientPacker) RearHeadroom

func (p *ShadowPacketClientPacker) RearHeadroom() int

RearHeadroom implements the zerocopy.ClientPacker RearHeadroom method.

type ShadowPacketClientUnpacker

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

ShadowPacketClientUnpacker unpacks Shadowsocks server packets and returns target address and plaintext payload.

When a server session changes, there's a replay window of less than 60 seconds, during which an adversary can replay packets with a valid timestamp from the old session. To protect against such attacks, and to simplify implementation and save resources, we only save information for one previous session.

In an unlikely event where the server session changed more than once within 60s, we simply drop new server sessions.

ShadowPacketClientUnpacker implements the zerocopy.Unpacker interface.

func (*ShadowPacketClientUnpacker) FrontHeadroom

func (p *ShadowPacketClientUnpacker) FrontHeadroom() int

FrontHeadroom implements the zerocopy.ClientUnpacker FrontHeadroom method.

func (*ShadowPacketClientUnpacker) RearHeadroom

func (p *ShadowPacketClientUnpacker) RearHeadroom() int

RearHeadroom implements the zerocopy.ClientUnpacker RearHeadroom method.

func (*ShadowPacketClientUnpacker) UnpackInPlace

func (p *ShadowPacketClientUnpacker) UnpackInPlace(b []byte, packetSourceAddrPort netip.AddrPort, packetStart, packetLen int) (payloadSourceAddrPort netip.AddrPort, payloadStart, payloadLen int, err error)

UnpackInPlace implements the zerocopy.ClientUnpacker UnpackInPlace method.

type ShadowPacketReplayError

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

func (*ShadowPacketReplayError) Error

func (e *ShadowPacketReplayError) Error() string

func (*ShadowPacketReplayError) Unwrap

func (e *ShadowPacketReplayError) Unwrap() error

type ShadowPacketServerPacker

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

ShadowPacketServerPacker packs UDP packets into authenticated and encrypted Shadowsocks packets.

ShadowPacketServerPacker implements the zerocopy.Packer interface.

func (*ShadowPacketServerPacker) FrontHeadroom

func (p *ShadowPacketServerPacker) FrontHeadroom() int

FrontHeadroom implements the zerocopy.ServerPacker FrontHeadroom method.

func (*ShadowPacketServerPacker) PackInPlace

func (p *ShadowPacketServerPacker) PackInPlace(b []byte, sourceAddrPort netip.AddrPort, payloadStart, payloadLen, maxPacketLen int) (packetStart, packetLen int, err error)

PackInPlace implements the zerocopy.ServerPacker PackInPlace method.

func (*ShadowPacketServerPacker) RearHeadroom

func (p *ShadowPacketServerPacker) RearHeadroom() int

RearHeadroom implements the zerocopy.ServerPacker RearHeadroom method.

type ShadowPacketServerUnpacker

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

ShadowPacketServerUnpacker unpacks Shadowsocks client packets and returns target address and plaintext payload.

ShadowPacketServerUnpacker implements the zerocopy.Unpacker interface.

func (*ShadowPacketServerUnpacker) FrontHeadroom

func (p *ShadowPacketServerUnpacker) FrontHeadroom() int

FrontHeadroom implements the zerocopy.ServerUnpacker FrontHeadroom method.

func (*ShadowPacketServerUnpacker) RearHeadroom

func (p *ShadowPacketServerUnpacker) RearHeadroom() int

RearHeadroom implements the zerocopy.ServerUnpacker RearHeadroom method.

func (*ShadowPacketServerUnpacker) UnpackInPlace

func (p *ShadowPacketServerUnpacker) UnpackInPlace(b []byte, sourceAddr netip.AddrPort, packetStart, packetLen int) (targetAddr conn.Addr, payloadStart, payloadLen int, err error)

UnpackInPlace unpacks the AEAD encrypted part of a Shadowsocks client packet and returns target address, payload start offset and payload length, or an error.

UnpackInPlace implements the zerocopy.ServerUnpacker UnpackInPlace method.

type ShadowStreamCipher

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

ShadowStreamCipher wraps an AEAD cipher and provides methods that transparently increments the nonce after each AEAD operation.

func (*ShadowStreamCipher) DecryptInPlace

func (c *ShadowStreamCipher) DecryptInPlace(ciphertext []byte) (plaintext []byte, err error)

DecryptInplace decrypts and authenticates ciphertext in-place.

func (*ShadowStreamCipher) DecryptTo

func (c *ShadowStreamCipher) DecryptTo(dst, ciphertext []byte) (plaintext []byte, err error)

DecryptTo decrypts and authenticates the ciphertext and saves the plaintext to dst.

func (*ShadowStreamCipher) EncryptInPlace

func (c *ShadowStreamCipher) EncryptInPlace(plaintext []byte) (ciphertext []byte)

EncryptInPlace encrypts and authenticates plaintext in-place.

func (*ShadowStreamCipher) EncryptTo

func (c *ShadowStreamCipher) EncryptTo(dst, plaintext []byte) (ciphertext []byte)

EncryptTo encrypts and authenticates the plaintext and saves the ciphertext to dst.

func (*ShadowStreamCipher) Overhead

func (c *ShadowStreamCipher) Overhead() int

Overhead returns the tag size of the AEAD cipher.

type ShadowStreamClientReadWriter

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

ShadowStreamClientReadWriter implements Shadowsocks stream client.

func NewShadowStreamClientReadWriter

func NewShadowStreamClientReadWriter(rwo zerocopy.DirectReadWriteCloserOpener, cipherConfig *CipherConfig, eihPSKHashes [][IdentityHeaderLength]byte, targetAddr conn.Addr, payload []byte) (sscRW *ShadowStreamClientReadWriter, rawRW zerocopy.DirectReadWriteCloser, err error)

NewShadowStreamClientReadWriter writes request headers to rw and returns a Shadowsocks stream client ready for reads and writes.

func (*ShadowStreamClientReadWriter) Close

func (rw *ShadowStreamClientReadWriter) Close() error

Close implements the ReadWriter Close method.

func (*ShadowStreamClientReadWriter) CloseRead

func (rw *ShadowStreamClientReadWriter) CloseRead() error

CloseRead implements the ReadWriter CloseRead method.

func (*ShadowStreamClientReadWriter) CloseWrite

func (rw *ShadowStreamClientReadWriter) CloseWrite() error

CloseWrite implements the ReadWriter CloseWrite method.

func (*ShadowStreamClientReadWriter) FrontHeadroom

func (rw *ShadowStreamClientReadWriter) FrontHeadroom() int

FrontHeadroom implements the Writer FrontHeadroom method.

func (*ShadowStreamClientReadWriter) MaxPayloadSizePerWrite

func (rw *ShadowStreamClientReadWriter) MaxPayloadSizePerWrite() int

MaxPayloadSizePerWrite implements the Writer MaxPayloadSizePerWrite method.

func (*ShadowStreamClientReadWriter) MinPayloadBufferSizePerRead

func (rw *ShadowStreamClientReadWriter) MinPayloadBufferSizePerRead() int

MinPayloadBufferSizePerRead implements the Reader MinPayloadBufferSizePerRead method.

func (*ShadowStreamClientReadWriter) ReadZeroCopy

func (rw *ShadowStreamClientReadWriter) ReadZeroCopy(b []byte, payloadBufStart, payloadBufLen int) (int, error)

ReadZeroCopy implements the Reader ReadZeroCopy method.

func (*ShadowStreamClientReadWriter) RearHeadroom

func (rw *ShadowStreamClientReadWriter) RearHeadroom() int

RearHeadroom implements the Writer RearHeadroom method.

func (*ShadowStreamClientReadWriter) WriteZeroCopy

func (rw *ShadowStreamClientReadWriter) WriteZeroCopy(b []byte, payloadStart, payloadLen int) (int, error)

WriteZeroCopy implements the Writer WriteZeroCopy method.

type ShadowStreamReader

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

ShadowStreamReader wraps an io.ReadCloser and reads from it as an encrypted Shadowsocks stream.

func (*ShadowStreamReader) Close

func (r *ShadowStreamReader) Close() error

Close implements the Reader Close method.

func (*ShadowStreamReader) FrontHeadroom

func (r *ShadowStreamReader) FrontHeadroom() int

FrontHeadroom implements the Reader FrontHeadroom method.

func (*ShadowStreamReader) MinPayloadBufferSizePerRead

func (r *ShadowStreamReader) MinPayloadBufferSizePerRead() int

MinPayloadBufferSizePerRead implements the Reader MinPayloadBufferSizePerRead method.

func (*ShadowStreamReader) ReadZeroCopy

func (r *ShadowStreamReader) ReadZeroCopy(b []byte, payloadBufStart, payloadBufLen int) (payloadLen int, err error)

ReadZeroCopy implements the Reader ReadZeroCopy method.

func (*ShadowStreamReader) RearHeadroom

func (r *ShadowStreamReader) RearHeadroom() int

RearHeadroom implements the Reader RearHeadroom method.

type ShadowStreamServerReadWriter

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

ShadowStreamServerReadWriter implements Shadowsocks stream server.

func NewShadowStreamServerReadWriter

func NewShadowStreamServerReadWriter(rw zerocopy.DirectReadWriteCloser, cipherConfig *CipherConfig, saltPool *SaltPool[string], uPSKMap map[[IdentityHeaderLength]byte]*CipherConfig) (sssRW *ShadowStreamServerReadWriter, targetAddr conn.Addr, payload []byte, err error)

NewShadowStreamServerReadWriter reads the request headers from rw to establish a session.

func (*ShadowStreamServerReadWriter) Close

func (rw *ShadowStreamServerReadWriter) Close() error

Close implements the ReadWriter Close method.

func (*ShadowStreamServerReadWriter) CloseRead

func (rw *ShadowStreamServerReadWriter) CloseRead() error

CloseRead implements the ReadWriter CloseRead method.

func (*ShadowStreamServerReadWriter) CloseWrite

func (rw *ShadowStreamServerReadWriter) CloseWrite() error

CloseWrite implements the ReadWriter CloseWrite method.

func (*ShadowStreamServerReadWriter) FrontHeadroom

func (rw *ShadowStreamServerReadWriter) FrontHeadroom() int

FrontHeadroom implements the Writer FrontHeadroom method.

func (*ShadowStreamServerReadWriter) MaxPayloadSizePerWrite

func (rw *ShadowStreamServerReadWriter) MaxPayloadSizePerWrite() int

MaxPayloadSizePerWrite implements the Writer MaxPayloadSizePerWrite method.

func (*ShadowStreamServerReadWriter) MinPayloadBufferSizePerRead

func (rw *ShadowStreamServerReadWriter) MinPayloadBufferSizePerRead() int

MinPayloadBufferSizePerRead implements the Reader MinPayloadBufferSizePerRead method.

func (*ShadowStreamServerReadWriter) ReadZeroCopy

func (rw *ShadowStreamServerReadWriter) ReadZeroCopy(b []byte, payloadBufStart, payloadBufLen int) (int, error)

ReadZeroCopy implements the Reader ReadZeroCopy method.

func (*ShadowStreamServerReadWriter) RearHeadroom

func (rw *ShadowStreamServerReadWriter) RearHeadroom() int

RearHeadroom implements the Writer RearHeadroom method.

func (*ShadowStreamServerReadWriter) WriteZeroCopy

func (rw *ShadowStreamServerReadWriter) WriteZeroCopy(b []byte, payloadStart, payloadLen int) (int, error)

WriteZeroCopy implements the Writer WriteZeroCopy method.

type ShadowStreamWriter

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

ShadowStreamWriter wraps an io.WriteCloser and feeds an encrypted Shadowsocks stream to it.

Wire format:

+------------------------+---------------------------+
| encrypted length chunk |  encrypted payload chunk  |
+------------------------+---------------------------+
|  2B length + 16B tag   | variable length + 16B tag |
+------------------------+---------------------------+

func (*ShadowStreamWriter) Close

func (w *ShadowStreamWriter) Close() error

Close implements the Writer Close method.

func (*ShadowStreamWriter) FrontHeadroom

func (w *ShadowStreamWriter) FrontHeadroom() int

FrontHeadroom implements the Writer FrontHeadroom method.

func (*ShadowStreamWriter) MaxPayloadSizePerWrite

func (w *ShadowStreamWriter) MaxPayloadSizePerWrite() int

MaxPayloadSizePerWrite implements the Writer MaxPayloadSizePerWrite method.

func (*ShadowStreamWriter) RearHeadroom

func (w *ShadowStreamWriter) RearHeadroom() int

RearHeadroom implements the Writer RearHeadroom method.

func (*ShadowStreamWriter) WriteZeroCopy

func (w *ShadowStreamWriter) WriteZeroCopy(b []byte, payloadStart, payloadLen int) (payloadWritten int, err error)

WriteZeroCopy implements the Writer WriteZeroCopy method.

type TCPClient

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

TCPClient implements the zerocopy TCPClient interface.

func NewTCPClient

func NewTCPClient(address string, dialerTFO bool, dialerFwmark int, cipherConfig *CipherConfig, eihPSKHashes [][IdentityHeaderLength]byte) *TCPClient

func (*TCPClient) Dial

func (c *TCPClient) Dial(targetAddr conn.Addr, payload []byte) (tc *net.TCPConn, rw zerocopy.ReadWriter, err error)

Dial implements the zerocopy.TCPClient Dial method.

func (*TCPClient) NativeInitialPayload

func (c *TCPClient) NativeInitialPayload() bool

NativeInitialPayload implements the zerocopy.TCPClient NativeInitialPayload method.

type TCPServer

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

TCPServer implements the zerocopy TCPServer interface.

func NewTCPServer

func NewTCPServer(cipherConfig *CipherConfig, uPSKMap map[[IdentityHeaderLength]byte]*CipherConfig) *TCPServer

func (*TCPServer) Accept

func (s *TCPServer) Accept(tc *net.TCPConn) (rw zerocopy.ReadWriter, targetAddr conn.Addr, payload []byte, err error)

Accept implements the zerocopy.TCPServer Accept method.

func (*TCPServer) DefaultTCPConnCloser

func (s *TCPServer) DefaultTCPConnCloser() zerocopy.TCPConnCloser

DefaultTCPConnCloser implements the zerocopy.TCPServer DefaultTCPConnCloser method.

func (*TCPServer) NativeInitialPayload

func (s *TCPServer) NativeInitialPayload() bool

NativeInitialPayload implements the zerocopy.TCPServer NativeInitialPayload method.

type UDPClient

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

UDPClient implements the zerocopy UDPClient interface.

func NewUDPClient

func NewUDPClient(addrPort netip.AddrPort, mtu, fwmark int, cipherConfig *CipherConfig, shouldPad PaddingPolicy, eihPSKHashes [][IdentityHeaderLength]byte) *UDPClient

func (*UDPClient) FrontHeadroom

func (c *UDPClient) FrontHeadroom() int

FrontHeadroom implements the UDPClient FrontHeadroom method.

func (*UDPClient) LinkInfo added in v1.1.0

func (c *UDPClient) LinkInfo() (int, int)

LinkInfo implements the UDPClient LinkInfo method.

func (*UDPClient) NewSession

NewSession implements the zerocopy.UDPClient NewSession method.

func (*UDPClient) RearHeadroom

func (c *UDPClient) RearHeadroom() int

RearHeadroom implements the UDPClient RearHeadroom method.

type UDPServer

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

UDPServer implements the zerocopy UDPSessionServer interface.

func NewUDPServer

func NewUDPServer(cipherConfig *CipherConfig, shouldPad PaddingPolicy, uPSKMap map[[IdentityHeaderLength]byte]*CipherConfig) *UDPServer

func (*UDPServer) FrontHeadroom

func (s *UDPServer) FrontHeadroom() int

FrontHeadroom implements the zerocopy.UDPSessionServer FrontHeadroom method.

func (*UDPServer) NewPacker

func (s *UDPServer) NewPacker(csid uint64) (zerocopy.ServerPacker, error)

NewPacker implements the zerocopy.UDPSessionServer NewPacker method.

func (*UDPServer) NewUnpacker

func (s *UDPServer) NewUnpacker(b []byte, csid uint64) (zerocopy.ServerUnpacker, error)

NewUnpacker implements the zerocopy.UDPSessionServer NewUnpacker method.

func (*UDPServer) RearHeadroom

func (s *UDPServer) RearHeadroom() int

RearHeadroom implements the zerocopy.UDPSessionServer RearHeadroom method.

func (*UDPServer) SessionInfo

func (s *UDPServer) SessionInfo(b []byte) (csid uint64, err error)

SessionInfo implements the zerocopy.UDPSessionServer SessionInfo method.

Jump to

Keyboard shortcuts

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