headers.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package protocol
  2. import (
  3. "runtime"
  4. "v2ray.com/core/common/net"
  5. "v2ray.com/core/common/uuid"
  6. )
  7. type RequestCommand byte
  8. const (
  9. RequestCommandTCP = RequestCommand(0x01)
  10. RequestCommandUDP = RequestCommand(0x02)
  11. )
  12. // RequestOption is the options of a request.
  13. type RequestOption byte
  14. const (
  15. // RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
  16. RequestOptionChunkStream = RequestOption(0x01)
  17. // RequestOptionConnectionReuse indicates client side expects to reuse the connection.
  18. RequestOptionConnectionReuse = RequestOption(0x02)
  19. // RequestOptionCompressedStream indicates request payload is compressed.
  20. RequestOptionCompressedStream = RequestOption(0x04)
  21. )
  22. func (v RequestOption) Has(option RequestOption) bool {
  23. return (v & option) == option
  24. }
  25. func (v *RequestOption) Set(option RequestOption) {
  26. *v = (*v | option)
  27. }
  28. func (v *RequestOption) Clear(option RequestOption) {
  29. *v = (*v & (^option))
  30. }
  31. type Security byte
  32. func (v Security) Is(t SecurityType) bool {
  33. return v == Security(t)
  34. }
  35. func NormSecurity(s Security) Security {
  36. if s.Is(SecurityType_UNKNOWN) {
  37. return Security(SecurityType_LEGACY)
  38. }
  39. return s
  40. }
  41. type RequestHeader struct {
  42. Version byte
  43. Command RequestCommand
  44. Option RequestOption
  45. Security Security
  46. Port net.Port
  47. Address net.Address
  48. User *User
  49. }
  50. func (v *RequestHeader) Destination() net.Destination {
  51. if v.Command == RequestCommandUDP {
  52. return net.UDPDestination(v.Address, v.Port)
  53. }
  54. return net.TCPDestination(v.Address, v.Port)
  55. }
  56. type ResponseOption byte
  57. const (
  58. ResponseOptionConnectionReuse = ResponseOption(0x01)
  59. )
  60. func (v *ResponseOption) Set(option ResponseOption) {
  61. *v = (*v | option)
  62. }
  63. func (v ResponseOption) Has(option ResponseOption) bool {
  64. return (v & option) == option
  65. }
  66. func (v *ResponseOption) Clear(option ResponseOption) {
  67. *v = (*v & (^option))
  68. }
  69. type ResponseCommand interface{}
  70. type ResponseHeader struct {
  71. Option ResponseOption
  72. Command ResponseCommand
  73. }
  74. type CommandSwitchAccount struct {
  75. Host net.Address
  76. Port net.Port
  77. ID *uuid.UUID
  78. AlterIds uint16
  79. Level uint32
  80. ValidMin byte
  81. }
  82. func (v *SecurityConfig) AsSecurity() Security {
  83. if v == nil {
  84. return Security(SecurityType_LEGACY)
  85. }
  86. if v.Type == SecurityType_AUTO {
  87. if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
  88. return Security(SecurityType_AES128_GCM)
  89. }
  90. return Security(SecurityType_CHACHA20_POLY1305)
  91. }
  92. return NormSecurity(Security(v.Type))
  93. }