headers.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package protocol
  2. import (
  3. "runtime"
  4. "github.com/xtls/xray-core/common/bitmask"
  5. "github.com/xtls/xray-core/common/net"
  6. "github.com/xtls/xray-core/common/uuid"
  7. "golang.org/x/sys/cpu"
  8. )
  9. // RequestCommand is a custom command in a proxy request.
  10. type RequestCommand byte
  11. const (
  12. RequestCommandTCP = RequestCommand(0x01)
  13. RequestCommandUDP = RequestCommand(0x02)
  14. RequestCommandMux = RequestCommand(0x03)
  15. )
  16. func (c RequestCommand) TransferType() TransferType {
  17. switch c {
  18. case RequestCommandTCP, RequestCommandMux:
  19. return TransferTypeStream
  20. case RequestCommandUDP:
  21. return TransferTypePacket
  22. default:
  23. return TransferTypeStream
  24. }
  25. }
  26. const (
  27. // [DEPRECATED 2023-06] RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
  28. RequestOptionChunkStream bitmask.Byte = 0x01
  29. // 0x02 legacy setting
  30. RequestOptionChunkMasking bitmask.Byte = 0x04
  31. RequestOptionGlobalPadding bitmask.Byte = 0x08
  32. RequestOptionAuthenticatedLength bitmask.Byte = 0x10
  33. )
  34. type RequestHeader struct {
  35. Version byte
  36. Command RequestCommand
  37. Option bitmask.Byte
  38. Security SecurityType
  39. Port net.Port
  40. Address net.Address
  41. User *MemoryUser
  42. }
  43. func (h *RequestHeader) Destination() net.Destination {
  44. if h.Command == RequestCommandUDP {
  45. return net.UDPDestination(h.Address, h.Port)
  46. }
  47. return net.TCPDestination(h.Address, h.Port)
  48. }
  49. const (
  50. ResponseOptionConnectionReuse bitmask.Byte = 0x01
  51. )
  52. type ResponseCommand interface{}
  53. type ResponseHeader struct {
  54. Option bitmask.Byte
  55. Command ResponseCommand
  56. }
  57. type CommandSwitchAccount struct {
  58. Host net.Address
  59. Port net.Port
  60. ID uuid.UUID
  61. Level uint32
  62. ValidMin byte
  63. }
  64. var (
  65. hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
  66. hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
  67. // Keep in sync with crypto/aes/cipher_s390x.go.
  68. hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
  69. (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
  70. hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
  71. runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
  72. runtime.GOARCH == "s390x" && hasGCMAsmS390X
  73. )
  74. func (sc *SecurityConfig) GetSecurityType() SecurityType {
  75. if sc == nil || sc.Type == SecurityType_AUTO {
  76. if hasAESGCMHardwareSupport {
  77. return SecurityType_AES128_GCM
  78. }
  79. return SecurityType_CHACHA20_POLY1305
  80. }
  81. return sc.Type
  82. }
  83. func isDomainTooLong(domain string) bool {
  84. return len(domain) > 256
  85. }