headers.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. type RequestHeader struct {
  31. Version byte
  32. User *User
  33. Command RequestCommand
  34. Option RequestOption
  35. Security Security
  36. Address v2net.Address
  37. Port v2net.Port
  38. }
  39. func (v *RequestHeader) Destination() v2net.Destination {
  40. if v.Command == RequestCommandUDP {
  41. return v2net.UDPDestination(v.Address, v.Port)
  42. }
  43. return v2net.TCPDestination(v.Address, v.Port)
  44. }
  45. type ResponseOption byte
  46. const (
  47. ResponseOptionConnectionReuse = ResponseOption(1)
  48. )
  49. func (v *ResponseOption) Set(option ResponseOption) {
  50. *v = (*v | option)
  51. }
  52. func (v ResponseOption) Has(option ResponseOption) bool {
  53. return (v | option) == option
  54. }
  55. func (v *ResponseOption) Clear(option ResponseOption) {
  56. *v = (*v & (^option))
  57. }
  58. type ResponseCommand interface{}
  59. type ResponseHeader struct {
  60. Option ResponseOption
  61. Command ResponseCommand
  62. }
  63. type CommandSwitchAccount struct {
  64. Host v2net.Address
  65. Port v2net.Port
  66. ID *uuid.UUID
  67. AlterIds uint16
  68. Level uint32
  69. ValidMin byte
  70. }
  71. func (v *SecurityConfig) AsSecurity() Security {
  72. if v == nil {
  73. return Security(SecurityType_LEGACY)
  74. }
  75. if v.Type == SecurityType_AUTO {
  76. if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
  77. return Security(SecurityType_AES128_GCM)
  78. }
  79. return Security(SecurityType_CHACHA20_POLY1305)
  80. }
  81. return Security(v.Type)
  82. }