headers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
  28. RequestOptionChunkStream bitmask.Byte = 0x01
  29. // RequestOptionConnectionReuse indicates client side expects to reuse the connection.
  30. RequestOptionConnectionReuse bitmask.Byte = 0x02
  31. RequestOptionChunkMasking bitmask.Byte = 0x04
  32. RequestOptionGlobalPadding bitmask.Byte = 0x08
  33. RequestOptionAuthenticatedLength bitmask.Byte = 0x10
  34. )
  35. type RequestHeader struct {
  36. Version byte
  37. Command RequestCommand
  38. Option bitmask.Byte
  39. Security SecurityType
  40. Port net.Port
  41. Address net.Address
  42. User *MemoryUser
  43. }
  44. func (h *RequestHeader) Destination() net.Destination {
  45. if h.Command == RequestCommandUDP {
  46. return net.UDPDestination(h.Address, h.Port)
  47. }
  48. return net.TCPDestination(h.Address, h.Port)
  49. }
  50. const (
  51. ResponseOptionConnectionReuse bitmask.Byte = 0x01
  52. )
  53. type ResponseCommand interface{}
  54. type ResponseHeader struct {
  55. Option bitmask.Byte
  56. Command ResponseCommand
  57. }
  58. type CommandSwitchAccount struct {
  59. Host net.Address
  60. Port net.Port
  61. ID uuid.UUID
  62. Level uint32
  63. AlterIds uint16
  64. ValidMin byte
  65. }
  66. var (
  67. hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
  68. hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
  69. // Keep in sync with crypto/aes/cipher_s390x.go.
  70. hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
  71. (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
  72. hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
  73. runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
  74. runtime.GOARCH == "s390x" && hasGCMAsmS390X
  75. )
  76. func (sc *SecurityConfig) GetSecurityType() SecurityType {
  77. if sc == nil || sc.Type == SecurityType_AUTO {
  78. if hasAESGCMHardwareSupport {
  79. return SecurityType_AES128_GCM
  80. }
  81. return SecurityType_CHACHA20_POLY1305
  82. }
  83. return sc.Type
  84. }
  85. func isDomainTooLong(domain string) bool {
  86. return len(domain) > 256
  87. }