Documentation
¶
Index ¶
- Constants
- Variables
- func CheckPSKLength(method string, psk []byte, psks [][]byte) error
- func NoPadding(_ conn.Addr) bool
- func PSKHash(psk []byte) [IdentityHeaderLength]byte
- func PSKLengthForMethod(method string) (int, error)
- func PadAll(_ conn.Addr) bool
- func PadPlainDNS(targetAddr conn.Addr) bool
- func ParseSessionIDAndPacketID(b []byte) (sid, pid uint64)
- func ParseTCPRequestFixedLengthHeader(b []byte) (n int, err error)
- func ParseTCPRequestVariableLengthHeader(b []byte) (targetAddr conn.Addr, payload []byte, err error)
- func ParseTCPResponseHeader(b []byte, requestSalt []byte) (n int, err error)
- func ParseUDPClientMessageHeader(b []byte, cachedDomain string) (targetAddr conn.Addr, updatedCachedDomain string, payloadStart, payloadLen int, ...)
- func ParseUDPServerMessageHeader(b []byte, csid uint64) (payloadSourceAddrPort netip.AddrPort, payloadStart, payloadLen int, err error)
- func ShadowPacketClientMessageHeadroom(identityHeadersLen int) zerocopy.Headroom
- func ValidateUnixEpochTimestamp(b []byte) error
- func WriteSessionIDAndPacketID(b []byte, sid, pid uint64)
- func WriteTCPRequestFixedLengthHeader(b []byte, length uint16)
- func WriteTCPRequestVariableLengthHeader(b []byte, targetAddr conn.Addr, payload []byte)
- func WriteTCPResponseHeader(b []byte, requestSalt []byte, length uint16)
- func WriteUDPClientMessageHeader(b []byte, paddingLen int, targetAddr conn.Addr)
- func WriteUDPServerMessageHeader(b []byte, csid uint64, paddingLen int, sourceAddrPort netip.AddrPort)
- type ClientCipherConfig
- func (c *ClientCipherConfig) EIHPSKHashes() [][IdentityHeaderLength]byte
- func (c *ClientCipherConfig) TCPIdentityHeaderCiphers(salt []byte) ([]cipher.Block, error)
- func (c *ClientCipherConfig) UDPIdentityHeaderCiphers() []cipher.Block
- func (c *ClientCipherConfig) UDPSeparateHeaderPackerCipher() cipher.Block
- type CredStore
- type HeaderError
- type PSKLengthError
- type PaddingPolicy
- type SaltPool
- type ServerIdentityCipherConfig
- type ServerUserCipherConfig
- type ShadowPacketClientPacker
- type ShadowPacketClientUnpacker
- type ShadowPacketReplayError
- type ShadowPacketServerPacker
- type ShadowPacketServerUnpacker
- func (p *ShadowPacketServerUnpacker) NewPacker() (zerocopy.ServerPacker, error)
- func (p *ShadowPacketServerUnpacker) ServerUnpackerInfo() zerocopy.ServerUnpackerInfo
- func (p *ShadowPacketServerUnpacker) UnpackInPlace(b []byte, sourceAddr netip.AddrPort, packetStart, packetLen int) (targetAddr conn.Addr, payloadStart, payloadLen int, err error)
- type ShadowStreamCipher
- func (c *ShadowStreamCipher) DecryptInPlace(ciphertext []byte) (plaintext []byte, err error)
- func (c *ShadowStreamCipher) DecryptTo(dst, ciphertext []byte) (plaintext []byte, err error)
- func (c *ShadowStreamCipher) EncryptInPlace(plaintext []byte) (ciphertext []byte)
- func (c *ShadowStreamCipher) EncryptTo(dst, plaintext []byte) (ciphertext []byte)
- func (c *ShadowStreamCipher) Overhead() int
- type ShadowStreamClientReadWriter
- type ShadowStreamReader
- type ShadowStreamServerReadWriter
- type ShadowStreamWriter
- type SlidingWindowFilter
- type TCPClient
- type TCPServer
- type UDPClient
- type UDPServer
- type UserCipherConfig
- type UserLookupMap
Constants ¶
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.IPv6AddrLen // 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 // DefaultSlidingWindowFilterSize is the default size of the sliding window filter. DefaultSlidingWindowFilterSize = 256 )
const MaxPayloadSize = 0xFFFF
Variables ¶
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") 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") ErrReplay = errors.New("detected replay") ErrIdentityHeaderUserPSKNotFound = errors.New("decrypted identity header does not match any known uPSK") )
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") )
var ErrUnsafeStreamPrefixMismatch = errors.New("unsafe stream prefix mismatch")
var ShadowPacketServerMessageHeadroom = zerocopy.Headroom{ Front: UDPSeparateHeaderLength + UDPServerMessageHeaderMaxLength, Rear: 16, }
ShadowPacketServerMessageHeadroom is the headroom required by an encrypted Shadowsocks server message.
var ShadowStreamHeadroom = zerocopy.Headroom{
Front: 2 + 16,
Rear: 16,
}
ShadowStreamHeadroom is the headroom required by an encrypted Shadowsocks stream.
Front is the size of an encrypted length chunk. Rear is the size of an AEAD tag.
var ShadowStreamReaderInfo = zerocopy.ReaderInfo{ Headroom: ShadowStreamHeadroom, MinPayloadBufferSizePerRead: MaxPayloadSize, }
ShadowStreamReaderInfo contains information about a ShadowStreamReader.
var ShadowStreamWriterInfo = zerocopy.WriterInfo{ Headroom: ShadowStreamHeadroom, MaxPayloadSizePerWrite: MaxPayloadSize, }
ShadowStreamWriterInfo contains information about a ShadowStreamWriter.
Functions ¶
func CheckPSKLength ¶ added in v1.6.0
CheckPSKLength checks that the PSK is the correct length for the given method.
func PSKHash ¶ added in v1.6.0
func PSKHash(psk []byte) [IdentityHeaderLength]byte
PSKHash returns the given PSK's BLAKE3 hash truncated to IdentityHeaderLength bytes.
func PSKLengthForMethod ¶ added in v1.6.0
PSKLengthForMethod returns the required length of the PSK for the given method.
func PadPlainDNS ¶
PadPlainDNS is a PaddingPolicy that adds padding to plain DNS traffic.
func ParseSessionIDAndPacketID ¶
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 ¶
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 ¶
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 ShadowPacketClientMessageHeadroom ¶ added in v1.4.0
ShadowPacketClientMessageHeadroom returns the headroom required by an encrypted Shadowsocks client message.
func ValidateUnixEpochTimestamp ¶
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 ¶
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 ¶
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 ¶
WriteTCPRequestVariableLengthHeader writes a TCP request variable-length header into the buffer.
The header fills the whole buffer. Excess bytes are used as padding.
The buffer size can be calculated with:
socks5.LengthOfAddrFromConnAddr(targetAddr) + 2 + len(payload) + paddingLen
The buffer size must not exceed MaxPayloadSize. The excess space in the buffer must not be larger than MaxPaddingLength bytes.
func WriteTCPResponseHeader ¶
WriteTCPResponseHeader writes a TCP response fixed-length header into the buffer.
The buffer size must be exactly 1 + 8 + len(requestSalt) + 2 bytes.
func WriteUDPClientMessageHeader ¶
WriteUDPClientMessageHeader writes a UDP client message header into the buffer.
The buffer size must be exactly 1 + 8 + 2 + paddingLen + socks5.LengthOfAddrFromConnAddr(targetAddr) bytes.
func WriteUDPServerMessageHeader ¶
func WriteUDPServerMessageHeader(b []byte, csid uint64, paddingLen int, sourceAddrPort netip.AddrPort)
WriteUDPServerMessageHeader writes a UDP server message header into the buffer.
The buffer size must be exactly 1 + 8 + 8 + 2 + paddingLen + socks5.LengthOfAddrFromAddrPort(sourceAddrPort) bytes.
Types ¶
type ClientCipherConfig ¶ added in v1.6.0
type ClientCipherConfig struct { UserCipherConfig // contains filtered or unexported fields }
ClientCipherConfig stores cipher configuration for a client.
func NewClientCipherConfig ¶ added in v1.6.0
func NewClientCipherConfig(psk []byte, iPSKs [][]byte, enableUDP bool) (c *ClientCipherConfig, err error)
NewClientCipherConfig returns a new ClientCipherConfig.
func (*ClientCipherConfig) EIHPSKHashes ¶ added in v1.6.0
func (c *ClientCipherConfig) EIHPSKHashes() [][IdentityHeaderLength]byte
EIHPSKHashes returns the truncated BLAKE3 hashes of c.iPSKs[1:] and c.PSK.
func (*ClientCipherConfig) TCPIdentityHeaderCiphers ¶ added in v1.6.0
func (c *ClientCipherConfig) TCPIdentityHeaderCiphers(salt []byte) ([]cipher.Block, error)
TCPIdentityHeaderCiphers creates block ciphers for a client TCP session's identity headers.
func (*ClientCipherConfig) UDPIdentityHeaderCiphers ¶ added in v1.6.0
func (c *ClientCipherConfig) UDPIdentityHeaderCiphers() []cipher.Block
UDPIdentityHeaderCiphers returns the block ciphers for a client UDP service's identity headers.
func (*ClientCipherConfig) UDPSeparateHeaderPackerCipher ¶ added in v1.6.0
func (c *ClientCipherConfig) UDPSeparateHeaderPackerCipher() cipher.Block
UDPSeparateHeaderPackerCipher returns the block cipher used by the client packer to encrypt the separate header.
type CredStore ¶ added in v1.6.0
type CredStore struct {
// contains filtered or unexported fields
}
CredStore stores credentials for a Shadowsocks 2022 server.
func (*CredStore) ReplaceUserLookupMap ¶ added in v1.6.0
func (s *CredStore) ReplaceUserLookupMap(ulm UserLookupMap)
ReplaceUserLookupMap replaces the current user lookup map with the given one.
func (*CredStore) Unlock ¶ added in v1.6.0
func (s *CredStore) Unlock()
Unlock unlocks its internal mutex.
func (*CredStore) UpdateUserLookupMap ¶ added in v1.6.0
func (s *CredStore) UpdateUserLookupMap(f func(ulm UserLookupMap))
UpdateUserLookupMap calls the given function with the current user lookup map.
type HeaderError ¶
func (*HeaderError[T]) Error ¶
func (e *HeaderError[T]) Error() string
func (*HeaderError[T]) Unwrap ¶
func (e *HeaderError[T]) Unwrap() error
type PSKLengthError ¶ added in v1.6.0
func (PSKLengthError) Error ¶ added in v1.6.0
func (e PSKLengthError) Error() string
type PaddingPolicy ¶
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.
SaltPool is not safe for concurrent use.
func NewSaltPool ¶
func NewSaltPool[T comparable](retention time.Duration) *SaltPool[T]
NewSaltPool returns a new SaltPool with the given retention.
type ServerIdentityCipherConfig ¶ added in v1.6.0
type ServerIdentityCipherConfig struct { IPSK []byte // contains filtered or unexported fields }
ServerIdentityCipherConfig stores cipher configuration for a server's identity header.
func NewServerIdentityCipherConfig ¶ added in v1.6.0
func NewServerIdentityCipherConfig(iPSK []byte, enableUDP bool) (c ServerIdentityCipherConfig, err error)
NewServerIdentityCipherConfig returns a new ServerIdentityCipherConfig.
func (ServerIdentityCipherConfig) TCP ¶ added in v1.6.0
func (c ServerIdentityCipherConfig) TCP(salt []byte) (cipher.Block, error)
TCP creates a block cipher for a server TCP session's identity header.
func (ServerIdentityCipherConfig) UDP ¶ added in v1.6.0
func (c ServerIdentityCipherConfig) UDP() cipher.Block
UDP returns the block cipher for a server UDP service's identity header.
type ServerUserCipherConfig ¶ added in v1.6.0
type ServerUserCipherConfig struct { UserCipherConfig Name string }
ServerUserCipherConfig stores cipher configuration for a server's EIH user.
func NewServerUserCipherConfig ¶ added in v1.6.0
func NewServerUserCipherConfig(name string, psk []byte, enableUDP bool) (c *ServerUserCipherConfig, err error)
NewServerUserCipherConfig returns a new ServerUserCipherConfig.
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) ClientPackerInfo ¶ added in v1.6.0
func (p *ShadowPacketClientPacker) ClientPackerInfo() zerocopy.ClientPackerInfo
ClientPackerInfo implements the zerocopy.ClientPacker ClientPackerInfo method.
func (*ShadowPacketClientPacker) PackInPlace ¶
func (p *ShadowPacketClientPacker) PackInPlace(ctx context.Context, b []byte, targetAddr conn.Addr, payloadStart, payloadLen int) (destAddrPort netip.AddrPort, packetStart, packetLen int, err error)
PackInPlace implements the zerocopy.ClientPacker PackInPlace 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) ClientUnpackerInfo ¶ added in v1.6.0
func (p *ShadowPacketClientUnpacker) ClientUnpackerInfo() zerocopy.ClientUnpackerInfo
ClientUnpackerInfo implements the zerocopy.ClientUnpacker ClientUnpackerInfo 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) 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) ServerPackerInfo ¶ added in v1.6.0
func (p *ShadowPacketServerPacker) ServerPackerInfo() zerocopy.ServerPackerInfo
ServerPackerInfo implements the zerocopy.ServerPacker ServerPackerInfo 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.ServerUnpacker interface.
func (*ShadowPacketServerUnpacker) NewPacker ¶ added in v1.6.0
func (p *ShadowPacketServerUnpacker) NewPacker() (zerocopy.ServerPacker, error)
NewPacker implements the zerocopy.ServerUnpacker NewPacker method.
func (*ShadowPacketServerUnpacker) ServerUnpackerInfo ¶ added in v1.6.0
func (p *ShadowPacketServerUnpacker) ServerUnpackerInfo() zerocopy.ServerUnpackerInfo
ServerUnpackerInfo implements the zerocopy.ServerUnpacker ServerUnpackerInfo 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 NewShadowStreamCipher ¶ added in v1.6.0
func NewShadowStreamCipher(aead cipher.AEAD) *ShadowStreamCipher
NewShadowStreamCipher wraps the given AEAD cipher into a new ShadowStreamCipher.
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 { *ShadowStreamReader *ShadowStreamWriter // contains filtered or unexported fields }
ShadowStreamClientReadWriter implements Shadowsocks stream client.
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) ReadZeroCopy ¶
func (rw *ShadowStreamClientReadWriter) ReadZeroCopy(b []byte, payloadBufStart, payloadBufLen int) (int, error)
ReadZeroCopy implements the Reader ReadZeroCopy 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) ReadZeroCopy ¶
func (r *ShadowStreamReader) ReadZeroCopy(b []byte, payloadBufStart, payloadBufLen int) (payloadLen int, err error)
ReadZeroCopy implements the Reader ReadZeroCopy method.
func (*ShadowStreamReader) ReaderInfo ¶ added in v1.6.0
func (r *ShadowStreamReader) ReaderInfo() zerocopy.ReaderInfo
ReaderInfo implements the Reader ReaderInfo method.
type ShadowStreamServerReadWriter ¶
type ShadowStreamServerReadWriter struct { *ShadowStreamReader *ShadowStreamWriter // contains filtered or unexported fields }
ShadowStreamServerReadWriter implements Shadowsocks stream server.
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) 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) WriteZeroCopy ¶
func (w *ShadowStreamWriter) WriteZeroCopy(b []byte, payloadStart, payloadLen int) (payloadWritten int, err error)
WriteZeroCopy implements the Writer WriteZeroCopy method.
func (*ShadowStreamWriter) WriterInfo ¶ added in v1.6.0
func (w *ShadowStreamWriter) WriterInfo() zerocopy.WriterInfo
WriterInfo implements the Writer WriterInfo method.
type SlidingWindowFilter ¶ added in v1.8.0
type SlidingWindowFilter struct {
// contains filtered or unexported fields
}
SlidingWindowFilter maintains a sliding window of uint64 counters.
func NewSlidingWindowFilter ¶ added in v1.8.0
func NewSlidingWindowFilter(size uint64) *SlidingWindowFilter
NewSlidingWindowFilter returns a new sliding window filter with the given size.
func (*SlidingWindowFilter) Add ¶ added in v1.8.0
func (f *SlidingWindowFilter) 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 (*SlidingWindowFilter) IsOk ¶ added in v1.8.0
func (f *SlidingWindowFilter) IsOk(counter uint64) bool
IsOk checks whether counter can be accepted by the sliding window filter.
func (*SlidingWindowFilter) MustAdd ¶ added in v1.8.0
func (f *SlidingWindowFilter) 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 (*SlidingWindowFilter) Reset ¶ added in v1.8.0
func (f *SlidingWindowFilter) Reset()
Reset resets the filter to its initial state.
func (*SlidingWindowFilter) Size ¶ added in v1.8.0
func (f *SlidingWindowFilter) Size() uint64
Size returns the size of the sliding window.
type TCPClient ¶
type TCPClient struct {
// contains filtered or unexported fields
}
TCPClient implements the zerocopy TCPClient interface.
func NewTCPClient ¶
func (*TCPClient) Dial ¶
func (c *TCPClient) Dial(ctx context.Context, targetAddr conn.Addr, payload []byte) (rawRW zerocopy.DirectReadWriteCloser, rw zerocopy.ReadWriter, err error)
Dial implements the zerocopy.TCPClient Dial method.
func (*TCPClient) Info ¶ added in v1.6.0
func (c *TCPClient) Info() zerocopy.TCPClientInfo
Info implements the zerocopy.TCPClient Info method.
type TCPServer ¶
type TCPServer struct { CredStore // contains filtered or unexported fields }
TCPServer implements the zerocopy TCPServer interface.
func NewTCPServer ¶
func NewTCPServer(allowSegmentedFixedLengthHeader bool, userCipherConfig UserCipherConfig, identityCipherConfig ServerIdentityCipherConfig, unsafeRequestStreamPrefix, unsafeResponseStreamPrefix []byte) *TCPServer
func (*TCPServer) Accept ¶
func (s *TCPServer) Accept(rawRW zerocopy.DirectReadWriteCloser) (rw zerocopy.ReadWriter, targetAddr conn.Addr, payload []byte, username string, err error)
Accept implements the zerocopy.TCPServer Accept method.
func (*TCPServer) Info ¶ added in v1.6.0
func (s *TCPServer) Info() zerocopy.TCPServerInfo
Info implements the zerocopy.TCPServer Info method.
type UDPClient ¶
type UDPClient struct {
// contains filtered or unexported fields
}
UDPClient implements the zerocopy UDPClient interface.
func NewUDPClient ¶
func NewUDPClient(name, network string, addr conn.Addr, mtu int, listenConfig conn.ListenConfig, filterSize uint64, cipherConfig *ClientCipherConfig, shouldPad PaddingPolicy) *UDPClient
func (*UDPClient) Info ¶ added in v1.6.0
func (c *UDPClient) Info() zerocopy.UDPClientInfo
Info implements the zerocopy.UDPClient Info method.
func (*UDPClient) NewSession ¶
func (c *UDPClient) NewSession(ctx context.Context) (zerocopy.UDPClientInfo, zerocopy.UDPClientSession, error)
NewSession implements the zerocopy.UDPClient NewSession method.
type UDPServer ¶
type UDPServer struct { CredStore // contains filtered or unexported fields }
UDPServer implements the zerocopy UDPSessionServer interface.
func NewUDPServer ¶
func NewUDPServer(filterSize uint64, userCipherConfig UserCipherConfig, identityCipherConfig ServerIdentityCipherConfig, shouldPad PaddingPolicy) *UDPServer
func (*UDPServer) Info ¶ added in v1.6.0
func (s *UDPServer) Info() zerocopy.UDPSessionServerInfo
Info implements the zerocopy.UDPSessionServer Info method.
func (*UDPServer) NewUnpacker ¶
NewUnpacker implements the zerocopy.UDPSessionServer NewUnpacker method.
type UserCipherConfig ¶ added in v1.6.0
type UserCipherConfig struct { PSK []byte // contains filtered or unexported fields }
UserCipherConfig stores cipher configuration for a non-EIH client/server or an EIH user.
func NewUserCipherConfig ¶ added in v1.6.0
func NewUserCipherConfig(psk []byte, enableUDP bool) (c UserCipherConfig, err error)
NewUserCipherConfig returns a new UserCipherConfig.
func (UserCipherConfig) AEAD ¶ added in v1.6.0
func (c UserCipherConfig) AEAD(salt []byte) (cipher.AEAD, error)
AEAD derives a subkey from the salt and returns a new AEAD cipher.
func (UserCipherConfig) Block ¶ added in v1.6.0
func (c UserCipherConfig) Block() cipher.Block
Block returns the block cipher for UDP separate header.
func (UserCipherConfig) ShadowStreamCipher ¶ added in v1.6.0
func (c UserCipherConfig) ShadowStreamCipher(salt []byte) (*ShadowStreamCipher, error)
type UserLookupMap ¶ added in v1.6.0
type UserLookupMap map[[IdentityHeaderLength]byte]*ServerUserCipherConfig
UserLookupMap is a map of uPSK hashes to *ServerUserCipherConfig. Upon decryption of an identity header, the uPSK hash is looked up in this map.