headers.go 2.6 KB

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