headers.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. RequestOptionChunkMasking = RequestOption(0x04)
  20. )
  21. func (v RequestOption) Has(option RequestOption) bool {
  22. return (v & option) == option
  23. }
  24. func (v *RequestOption) Set(option RequestOption) {
  25. *v = (*v | option)
  26. }
  27. func (v *RequestOption) Clear(option RequestOption) {
  28. *v = (*v & (^option))
  29. }
  30. type Security byte
  31. func (v Security) Is(t SecurityType) bool {
  32. return v == Security(t)
  33. }
  34. func NormSecurity(s Security) Security {
  35. if s.Is(SecurityType_UNKNOWN) {
  36. return Security(SecurityType_LEGACY)
  37. }
  38. return s
  39. }
  40. type RequestHeader struct {
  41. Version byte
  42. Command RequestCommand
  43. Option RequestOption
  44. Security Security
  45. Port net.Port
  46. Address net.Address
  47. User *User
  48. }
  49. func (v *RequestHeader) Destination() net.Destination {
  50. if v.Command == RequestCommandUDP {
  51. return net.UDPDestination(v.Address, v.Port)
  52. }
  53. return net.TCPDestination(v.Address, v.Port)
  54. }
  55. type ResponseOption byte
  56. const (
  57. ResponseOptionConnectionReuse = ResponseOption(0x01)
  58. )
  59. func (v *ResponseOption) Set(option ResponseOption) {
  60. *v = (*v | option)
  61. }
  62. func (v ResponseOption) Has(option ResponseOption) bool {
  63. return (v & option) == option
  64. }
  65. func (v *ResponseOption) Clear(option ResponseOption) {
  66. *v = (*v & (^option))
  67. }
  68. type ResponseCommand interface{}
  69. type ResponseHeader struct {
  70. Option ResponseOption
  71. Command ResponseCommand
  72. }
  73. type CommandSwitchAccount struct {
  74. Host net.Address
  75. Port net.Port
  76. ID *uuid.UUID
  77. AlterIds uint16
  78. Level uint32
  79. ValidMin byte
  80. }
  81. func (v *SecurityConfig) AsSecurity() Security {
  82. if v == nil {
  83. return Security(SecurityType_LEGACY)
  84. }
  85. if v.Type == SecurityType_AUTO {
  86. if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
  87. return Security(SecurityType_AES128_GCM)
  88. }
  89. return Security(SecurityType_CHACHA20_POLY1305)
  90. }
  91. return NormSecurity(Security(v.Type))
  92. }