headers.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package protocol
  2. import (
  3. "runtime"
  4. "v2ray.com/core/common/buf"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/uuid"
  7. )
  8. type RequestCommand byte
  9. const (
  10. RequestCommandTCP = RequestCommand(0x01)
  11. RequestCommandUDP = RequestCommand(0x02)
  12. )
  13. // RequestOption is the options of a request.
  14. type RequestOption byte
  15. const (
  16. // RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
  17. RequestOptionChunkStream = RequestOption(0x01)
  18. // RequestOptionConnectionReuse indicates client side expects to reuse the connection.
  19. RequestOptionConnectionReuse = RequestOption(0x02)
  20. // RequestOptionCompressedStream indicates request payload is compressed.
  21. RequestOptionCompressedStream = RequestOption(0x04)
  22. )
  23. func (v RequestOption) Has(option RequestOption) bool {
  24. return (v & option) == option
  25. }
  26. func (v *RequestOption) Set(option RequestOption) {
  27. *v = (*v | option)
  28. }
  29. func (v *RequestOption) Clear(option RequestOption) {
  30. *v = (*v & (^option))
  31. }
  32. type Security byte
  33. func (v Security) Is(t SecurityType) bool {
  34. return v == 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 RequestOption
  46. Security Security
  47. Port net.Port
  48. Address net.Address
  49. User *User
  50. }
  51. func (v *RequestHeader) Destination() net.Destination {
  52. if v.Command == RequestCommandUDP {
  53. return net.UDPDestination(v.Address, v.Port)
  54. }
  55. return net.TCPDestination(v.Address, v.Port)
  56. }
  57. type ResponseOption byte
  58. const (
  59. ResponseOptionConnectionReuse = ResponseOption(0x01)
  60. )
  61. func (v *ResponseOption) Set(option ResponseOption) {
  62. *v = (*v | option)
  63. }
  64. func (v ResponseOption) Has(option ResponseOption) bool {
  65. return (v & option) == option
  66. }
  67. func (v *ResponseOption) Clear(option ResponseOption) {
  68. *v = (*v & (^option))
  69. }
  70. type ResponseCommand interface{}
  71. type ResponseHeader struct {
  72. Option ResponseOption
  73. Command ResponseCommand
  74. }
  75. type CommandSwitchAccount struct {
  76. Host net.Address
  77. Port net.Port
  78. ID *uuid.UUID
  79. AlterIds uint16
  80. Level uint32
  81. ValidMin byte
  82. }
  83. func (v *SecurityConfig) AsSecurity() Security {
  84. if v == nil {
  85. return Security(SecurityType_LEGACY)
  86. }
  87. if v.Type == SecurityType_AUTO {
  88. if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
  89. return Security(SecurityType_AES128_GCM)
  90. }
  91. return Security(SecurityType_CHACHA20_POLY1305)
  92. }
  93. return NormSecurity(Security(v.Type))
  94. }
  95. type SessionFrameOption byte
  96. const (
  97. SessionFrameOptionDefault SessionFrameOption = 0x00
  98. SessionFrameOptionNew SessionFrameOption = 0x01
  99. SessionFrameOptionContinue SessionFrameOption = 0x02
  100. SessionFrameOptionEnd SessionFrameOption = 0x03
  101. )
  102. type SessionId uint32
  103. const (
  104. DefaultSessionId SessionId = 0
  105. )
  106. type SessionFrame struct {
  107. Id SessionId
  108. Option SessionFrameOption
  109. Address net.Address
  110. Port net.Port
  111. Payload *buf.Buffer
  112. }