headers.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // Keep in sync with crypto/tls/cipher_suites.go.
  66. hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ && cpu.X86.HasSSE41 && cpu.X86.HasSSSE3
  67. hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
  68. hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCTR && cpu.S390X.HasGHASH
  69. hasGCMAsmPPC64 = runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le"
  70. HasAESGCMHardwareSupport = hasGCMAsmAMD64 || hasGCMAsmARM64 || hasGCMAsmS390X || hasGCMAsmPPC64
  71. )
  72. func (sc *SecurityConfig) GetSecurityType() SecurityType {
  73. if sc == nil || sc.Type == SecurityType_AUTO {
  74. if HasAESGCMHardwareSupport {
  75. return SecurityType_AES128_GCM
  76. }
  77. return SecurityType_CHACHA20_POLY1305
  78. }
  79. return sc.Type
  80. }
  81. func isDomainTooLong(domain string) bool {
  82. return len(domain) > 256
  83. }