headers.go 2.1 KB

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