headers.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. switch c {
  17. case RequestCommandTCP, RequestCommandMux:
  18. return TransferTypeStream
  19. case RequestCommandUDP:
  20. return TransferTypePacket
  21. default:
  22. return TransferTypeStream
  23. }
  24. }
  25. const (
  26. // RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
  27. RequestOptionChunkStream bitmask.Byte = 0x01
  28. // RequestOptionConnectionReuse indicates client side expects to reuse the connection.
  29. RequestOptionConnectionReuse bitmask.Byte = 0x02
  30. RequestOptionChunkMasking bitmask.Byte = 0x04
  31. RequestOptionGlobalPadding bitmask.Byte = 0x08
  32. RequestOptionEarlyChecksum bitmask.Byte = 0x16
  33. )
  34. type RequestHeader struct {
  35. Version byte
  36. Command RequestCommand
  37. Option bitmask.Byte
  38. Security SecurityType
  39. Port net.Port
  40. Address net.Address
  41. User *MemoryUser
  42. }
  43. func (h *RequestHeader) Destination() net.Destination {
  44. if h.Command == RequestCommandUDP {
  45. return net.UDPDestination(h.Address, h.Port)
  46. }
  47. return net.TCPDestination(h.Address, h.Port)
  48. }
  49. const (
  50. ResponseOptionConnectionReuse bitmask.Byte = 0x01
  51. )
  52. type ResponseCommand interface{}
  53. type ResponseHeader struct {
  54. Option bitmask.Byte
  55. Command ResponseCommand
  56. }
  57. type CommandSwitchAccount struct {
  58. Host net.Address
  59. Port net.Port
  60. ID uuid.UUID
  61. Level uint32
  62. AlterIds uint16
  63. ValidMin byte
  64. }
  65. func (sc *SecurityConfig) GetSecurityType() SecurityType {
  66. if sc == nil || sc.Type == SecurityType_AUTO {
  67. if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64" {
  68. return SecurityType_AES128_GCM
  69. }
  70. return SecurityType_CHACHA20_POLY1305
  71. }
  72. return sc.Type
  73. }
  74. func isDomainTooLong(domain string) bool {
  75. return len(domain) > 256
  76. }