headers.go 2.0 KB

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