headers.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. )
  32. type RequestHeader struct {
  33. Version byte
  34. Command RequestCommand
  35. Option bitmask.Byte
  36. Security SecurityType
  37. Port net.Port
  38. Address net.Address
  39. User *User
  40. }
  41. func (h *RequestHeader) Destination() net.Destination {
  42. if h.Command == RequestCommandUDP {
  43. return net.UDPDestination(h.Address, h.Port)
  44. }
  45. return net.TCPDestination(h.Address, h.Port)
  46. }
  47. const (
  48. ResponseOptionConnectionReuse bitmask.Byte = 0x01
  49. )
  50. type ResponseCommand interface{}
  51. type ResponseHeader struct {
  52. Option bitmask.Byte
  53. Command ResponseCommand
  54. }
  55. type CommandSwitchAccount struct {
  56. Host net.Address
  57. Port net.Port
  58. ID uuid.UUID
  59. Level uint32
  60. AlterIds uint16
  61. ValidMin byte
  62. }
  63. func (sc *SecurityConfig) GetSecurityType() SecurityType {
  64. if sc == nil || sc.Type == SecurityType_AUTO {
  65. if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
  66. return SecurityType_AES128_GCM
  67. }
  68. return SecurityType_CHACHA20_POLY1305
  69. }
  70. return sc.Type
  71. }
  72. func isDomainTooLong(domain string) bool {
  73. return len(domain) > 256
  74. }