headers.go 2.1 KB

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