headers.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package protocol
  2. import (
  3. "runtime"
  4. "github.com/xtls/xray-core/common/bitmask"
  5. "github.com/xtls/xray-core/common/net"
  6. "golang.org/x/sys/cpu"
  7. )
  8. // RequestCommand is a custom command in a proxy request.
  9. type RequestCommand byte
  10. const (
  11. RequestCommandTCP = RequestCommand(0x01)
  12. RequestCommandUDP = RequestCommand(0x02)
  13. RequestCommandMux = RequestCommand(0x03)
  14. RequestCommandRvs = RequestCommand(0x04)
  15. )
  16. func (c RequestCommand) TransferType() TransferType {
  17. switch c {
  18. case RequestCommandTCP, RequestCommandMux, RequestCommandRvs:
  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. var (
  58. // Keep in sync with crypto/tls/cipher_suites.go.
  59. hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ && cpu.X86.HasSSE41 && cpu.X86.HasSSSE3
  60. hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
  61. hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCTR && cpu.S390X.HasGHASH
  62. hasGCMAsmPPC64 = runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le"
  63. HasAESGCMHardwareSupport = hasGCMAsmAMD64 || hasGCMAsmARM64 || hasGCMAsmS390X || hasGCMAsmPPC64
  64. )
  65. func (sc *SecurityConfig) GetSecurityType() SecurityType {
  66. if sc == nil || sc.Type == SecurityType_AUTO {
  67. if HasAESGCMHardwareSupport {
  68. return SecurityType_AES128_GCM
  69. }
  70. return SecurityType_CHACHA20_POLY1305
  71. }
  72. return sc.Type
  73. }
  74. func isDomainTooLong(domain string) bool {
  75. return len(domain) > 256
  76. }