ss2022.go 3.4 KB

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