github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/shadowsocks2022/ss2022.go (about)

     1  package shadowsocks2022
     2  
     3  import (
     4  	"crypto/cipher"
     5  	"io"
     6  
     7  	"github.com/lunixbochs/struc"
     8  
     9  	"github.com/v2fly/v2ray-core/v5/common/buf"
    10  	"github.com/v2fly/v2ray-core/v5/common/net"
    11  	"github.com/v2fly/v2ray-core/v5/common/protocol"
    12  )
    13  
    14  //go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
    15  
    16  type KeyDerivation interface {
    17  	GetSessionSubKey(effectivePsk, Salt []byte, OutKey []byte) error
    18  	GetIdentitySubKey(effectivePsk, Salt []byte, OutKey []byte) error
    19  }
    20  
    21  type Method interface {
    22  	GetSessionSubKeyAndSaltLength() int
    23  	GetStreamAEAD(SessionSubKey []byte) (cipher.AEAD, error)
    24  	GenerateEIH(CurrentIdentitySubKey []byte, nextPskHash []byte, out []byte) error
    25  	GetUDPClientProcessor(ipsk [][]byte, psk []byte, derivation KeyDerivation) (UDPClientPacketProcessor, error)
    26  }
    27  
    28  type ExtensibleIdentityHeaders interface {
    29  	struc.Custom
    30  }
    31  
    32  type DestinationAddress interface {
    33  	net.Address
    34  }
    35  
    36  type RequestSalt interface {
    37  	struc.Custom
    38  	isRequestSalt()
    39  	Bytes() []byte
    40  	FillAllFrom(reader io.Reader) error
    41  }
    42  
    43  type TCPRequestHeader1PreSessionKey struct {
    44  	Salt RequestSalt
    45  	EIH  ExtensibleIdentityHeaders
    46  }
    47  
    48  type TCPRequestHeader2FixedLength struct {
    49  	Type         byte
    50  	Timestamp    uint64
    51  	HeaderLength uint16
    52  }
    53  
    54  type TCPRequestHeader3VariableLength struct {
    55  	DestinationAddress DestinationAddress
    56  	Contents           struct {
    57  		PaddingLength uint16 `struc:"sizeof=Padding"`
    58  		Padding       []byte
    59  	}
    60  }
    61  
    62  type TCPRequestHeader struct {
    63  	PreSessionKeyHeader TCPRequestHeader1PreSessionKey
    64  	FixedLengthHeader   TCPRequestHeader2FixedLength
    65  	Header              TCPRequestHeader3VariableLength
    66  }
    67  
    68  type TCPResponseHeader1PreSessionKey struct {
    69  	Salt RequestSalt
    70  }
    71  
    72  type TCPResponseHeader2FixedLength struct {
    73  	Type                 byte
    74  	Timestamp            uint64
    75  	RequestSalt          RequestSalt
    76  	InitialPayloadLength uint16
    77  }
    78  type TCPResponseHeader struct {
    79  	PreSessionKeyHeader TCPResponseHeader1PreSessionKey
    80  	Header              TCPResponseHeader2FixedLength
    81  }
    82  
    83  const (
    84  	TCPHeaderTypeClientToServerStream = byte(0x00)
    85  	TCPHeaderTypeServerToClientStream = byte(0x01)
    86  	TCPMinPaddingLength               = 0
    87  	TCPMaxPaddingLength               = 900
    88  )
    89  
    90  var addrParser = protocol.NewAddressParser(
    91  	protocol.AddressFamilyByte(0x01, net.AddressFamilyIPv4),
    92  	protocol.AddressFamilyByte(0x04, net.AddressFamilyIPv6),
    93  	protocol.AddressFamilyByte(0x03, net.AddressFamilyDomain),
    94  )
    95  
    96  type UDPRequest struct {
    97  	SessionID [8]byte
    98  	PacketID  uint64
    99  	TimeStamp uint64
   100  	Address   DestinationAddress
   101  	Port      int
   102  	Payload   *buf.Buffer
   103  }
   104  
   105  type UDPResponse struct {
   106  	UDPRequest
   107  	ClientSessionID [8]byte
   108  }
   109  
   110  const (
   111  	UDPHeaderTypeClientToServerStream = byte(0x00)
   112  	UDPHeaderTypeServerToClientStream = byte(0x01)
   113  )
   114  
   115  type UDPClientPacketProcessorCachedStateContainer interface {
   116  	GetCachedState(sessionID string) UDPClientPacketProcessorCachedState
   117  	PutCachedState(sessionID string, cache UDPClientPacketProcessorCachedState)
   118  	GetCachedServerState(serverSessionID string) UDPClientPacketProcessorCachedState
   119  	PutCachedServerState(serverSessionID string, cache UDPClientPacketProcessorCachedState)
   120  }
   121  
   122  type UDPClientPacketProcessorCachedState interface{}
   123  
   124  // UDPClientPacketProcessor
   125  // Caller retain and receive all ownership of the buffer
   126  type UDPClientPacketProcessor interface {
   127  	EncodeUDPRequest(request *UDPRequest, out *buf.Buffer, cache UDPClientPacketProcessorCachedStateContainer) error
   128  	DecodeUDPResp(input []byte, resp *UDPResponse, cache UDPClientPacketProcessorCachedStateContainer) error
   129  }