headers.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package protocol
  2. import (
  3. "runtime"
  4. "v2ray.com/core/common/bitmask"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/uuid"
  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. )
  15. func (c RequestCommand) TransferType() TransferType {
  16. switch c {
  17. case RequestCommandTCP, RequestCommandMux:
  18. return TransferTypeStream
  19. case RequestCommandUDP:
  20. return TransferTypePacket
  21. default:
  22. return TransferTypeStream
  23. }
  24. }
  25. const (
  26. // RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
  27. RequestOptionChunkStream bitmask.Byte = 0x01
  28. // RequestOptionConnectionReuse indicates client side expects to reuse the connection.
  29. RequestOptionConnectionReuse bitmask.Byte = 0x02
  30. RequestOptionChunkMasking bitmask.Byte = 0x04
  31. )
  32. type Security byte
  33. func (s Security) Is(t SecurityType) bool {
  34. return s == Security(t)
  35. }
  36. func NormSecurity(s Security) Security {
  37. if s.Is(SecurityType_UNKNOWN) {
  38. return Security(SecurityType_LEGACY)
  39. }
  40. return s
  41. }
  42. type RequestHeader struct {
  43. Version byte
  44. Command RequestCommand
  45. Option bitmask.Byte
  46. Security Security
  47. Port net.Port
  48. Address net.Address
  49. User *User
  50. }
  51. func (h *RequestHeader) Destination() net.Destination {
  52. if h.Command == RequestCommandUDP {
  53. return net.UDPDestination(h.Address, h.Port)
  54. }
  55. return net.TCPDestination(h.Address, h.Port)
  56. }
  57. const (
  58. ResponseOptionConnectionReuse bitmask.Byte = 0x01
  59. )
  60. type ResponseCommand interface{}
  61. type ResponseHeader struct {
  62. Option bitmask.Byte
  63. Command ResponseCommand
  64. }
  65. type CommandSwitchAccount struct {
  66. Host net.Address
  67. Port net.Port
  68. ID uuid.UUID
  69. Level uint32
  70. AlterIds uint16
  71. ValidMin byte
  72. }
  73. func (sc *SecurityConfig) AsSecurity() Security {
  74. if sc == nil || sc.Type == SecurityType_AUTO {
  75. if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
  76. return Security(SecurityType_AES128_GCM)
  77. }
  78. return Security(SecurityType_CHACHA20_POLY1305)
  79. }
  80. return NormSecurity(Security(sc.Type))
  81. }
  82. func IsDomainTooLong(domain string) bool {
  83. return len(domain) > 256
  84. }