headers.go 2.6 KB

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