1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-20 10:56:07 -04:00
v2fly/proxy/shadowsocks2022/ss2022.go
Xiaokang Wang (Shelikhoo) b6da3e86a5
Shadowsocks2022 Client Implementation Improvements (#2770)
* Shadowsocks2022 Client Implementation Improvements

1. Added UDP Replay Detection
2. Added UDP Processor State Cache
3. Added More Detailed Output for Time Difference Error
4. Replaced Mutex with RWMutex for reduced lock contention
5. Added per server session tracking of decryption cache and anti-replay window
6. Adjust server session track time
7. Increase per session buffer to 128
8. Fix client crash when EIH is not enabled
9. Fix client log contains non-human-friendly content
10.Remove mark and remove for trackedSessions
11. Fixed packet size uint16 overflow issue
2023-11-24 00:40:07 +00:00

130 lines
3.4 KiB
Go

package shadowsocks2022
import (
"crypto/cipher"
"io"
"github.com/lunixbochs/struc"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
)
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
type KeyDerivation interface {
GetSessionSubKey(effectivePsk, Salt []byte, OutKey []byte) error
GetIdentitySubKey(effectivePsk, Salt []byte, OutKey []byte) error
}
type Method interface {
GetSessionSubKeyAndSaltLength() int
GetStreamAEAD(SessionSubKey []byte) (cipher.AEAD, error)
GenerateEIH(CurrentIdentitySubKey []byte, nextPskHash []byte, out []byte) error
GetUDPClientProcessor(ipsk [][]byte, psk []byte, derivation KeyDerivation) (UDPClientPacketProcessor, error)
}
type ExtensibleIdentityHeaders interface {
struc.Custom
}
type DestinationAddress interface {
net.Address
}
type RequestSalt interface {
struc.Custom
isRequestSalt()
Bytes() []byte
FillAllFrom(reader io.Reader) error
}
type TCPRequestHeader1PreSessionKey struct {
Salt RequestSalt
EIH ExtensibleIdentityHeaders
}
type TCPRequestHeader2FixedLength struct {
Type byte
Timestamp uint64
HeaderLength uint16
}
type TCPRequestHeader3VariableLength struct {
DestinationAddress DestinationAddress
Contents struct {
PaddingLength uint16 `struc:"sizeof=Padding"`
Padding []byte
}
}
type TCPRequestHeader struct {
PreSessionKeyHeader TCPRequestHeader1PreSessionKey
FixedLengthHeader TCPRequestHeader2FixedLength
Header TCPRequestHeader3VariableLength
}
type TCPResponseHeader1PreSessionKey struct {
Salt RequestSalt
}
type TCPResponseHeader2FixedLength struct {
Type byte
Timestamp uint64
RequestSalt RequestSalt
InitialPayloadLength uint16
}
type TCPResponseHeader struct {
PreSessionKeyHeader TCPResponseHeader1PreSessionKey
Header TCPResponseHeader2FixedLength
}
const (
TCPHeaderTypeClientToServerStream = byte(0x00)
TCPHeaderTypeServerToClientStream = byte(0x01)
TCPMinPaddingLength = 0
TCPMaxPaddingLength = 900
)
var addrParser = protocol.NewAddressParser(
protocol.AddressFamilyByte(0x01, net.AddressFamilyIPv4),
protocol.AddressFamilyByte(0x04, net.AddressFamilyIPv6),
protocol.AddressFamilyByte(0x03, net.AddressFamilyDomain),
)
type UDPRequest struct {
SessionID [8]byte
PacketID uint64
TimeStamp uint64
Address DestinationAddress
Port int
Payload *buf.Buffer
}
type UDPResponse struct {
UDPRequest
ClientSessionID [8]byte
}
const (
UDPHeaderTypeClientToServerStream = byte(0x00)
UDPHeaderTypeServerToClientStream = byte(0x01)
)
type UDPClientPacketProcessorCachedStateContainer interface {
GetCachedState(sessionID string) UDPClientPacketProcessorCachedState
PutCachedState(sessionID string, cache UDPClientPacketProcessorCachedState)
GetCachedServerState(serverSessionID string) UDPClientPacketProcessorCachedState
PutCachedServerState(serverSessionID string, cache UDPClientPacketProcessorCachedState)
}
type UDPClientPacketProcessorCachedState interface{}
// UDPClientPacketProcessor
// Caller retain and receive all ownership of the buffer
type UDPClientPacketProcessor interface {
EncodeUDPRequest(request *UDPRequest, out *buf.Buffer, cache UDPClientPacketProcessorCachedStateContainer) error
DecodeUDPResp(input []byte, resp *UDPResponse, cache UDPClientPacketProcessorCachedStateContainer) error
}