frame.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package mux
  2. import (
  3. "io"
  4. "v2ray.com/core/common"
  5. "v2ray.com/core/common/bitmask"
  6. "v2ray.com/core/common/buf"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/protocol"
  9. "v2ray.com/core/common/serial"
  10. )
  11. type SessionStatus byte
  12. const (
  13. SessionStatusNew SessionStatus = 0x01
  14. SessionStatusKeep SessionStatus = 0x02
  15. SessionStatusEnd SessionStatus = 0x03
  16. SessionStatusKeepAlive SessionStatus = 0x04
  17. )
  18. const (
  19. OptionData bitmask.Byte = 0x01
  20. OptionError bitmask.Byte = 0x02
  21. )
  22. type TargetNetwork byte
  23. const (
  24. TargetNetworkTCP TargetNetwork = 0x01
  25. TargetNetworkUDP TargetNetwork = 0x02
  26. )
  27. var addrParser = protocol.NewAddressParser(
  28. protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4),
  29. protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain),
  30. protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6),
  31. protocol.PortThenAddress(),
  32. )
  33. /*
  34. Frame format
  35. 2 bytes - length
  36. 2 bytes - session id
  37. 1 bytes - status
  38. 1 bytes - option
  39. 1 byte - network
  40. 2 bytes - port
  41. n bytes - address
  42. */
  43. type FrameMetadata struct {
  44. Target net.Destination
  45. SessionID uint16
  46. Option bitmask.Byte
  47. SessionStatus SessionStatus
  48. }
  49. func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
  50. lenBytes := b.Bytes()
  51. common.Must2(b.WriteBytes(0x00, 0x00))
  52. len0 := b.Len()
  53. if err := b.AppendSupplier(serial.WriteUint16(f.SessionID)); err != nil {
  54. return err
  55. }
  56. common.Must2(b.WriteBytes(byte(f.SessionStatus), byte(f.Option)))
  57. if f.SessionStatus == SessionStatusNew {
  58. switch f.Target.Network {
  59. case net.Network_TCP:
  60. common.Must2(b.WriteBytes(byte(TargetNetworkTCP)))
  61. case net.Network_UDP:
  62. common.Must2(b.WriteBytes(byte(TargetNetworkUDP)))
  63. }
  64. if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
  65. return err
  66. }
  67. }
  68. len1 := b.Len()
  69. serial.Uint16ToBytes(uint16(len1-len0), lenBytes)
  70. return nil
  71. }
  72. // ReadFrom reads FrameMetadata from the given reader.
  73. func (f *FrameMetadata) ReadFrom(reader io.Reader) error {
  74. metaLen, err := serial.ReadUint16(reader)
  75. if err != nil {
  76. return err
  77. }
  78. if metaLen > 512 {
  79. return newError("invalid metalen ", metaLen).AtError()
  80. }
  81. b := buf.New()
  82. defer b.Release()
  83. if err := b.Reset(buf.ReadFullFrom(reader, int32(metaLen))); err != nil {
  84. return err
  85. }
  86. return f.ReadFromBuffer(b)
  87. }
  88. // ReadFromBuffer reads a FrameMetadata from the given buffer.
  89. // Visible for testing only.
  90. func (f *FrameMetadata) ReadFromBuffer(b *buf.Buffer) error {
  91. if b.Len() < 4 {
  92. return newError("insufficient buffer: ", b.Len())
  93. }
  94. f.SessionID = serial.BytesToUint16(b.BytesTo(2))
  95. f.SessionStatus = SessionStatus(b.Byte(2))
  96. f.Option = bitmask.Byte(b.Byte(3))
  97. f.Target.Network = net.Network_Unknown
  98. if f.SessionStatus == SessionStatusNew {
  99. network := TargetNetwork(b.Byte(4))
  100. b.Advance(5)
  101. addr, port, err := addrParser.ReadAddressPort(nil, b)
  102. if err != nil {
  103. return newError("failed to parse address and port").Base(err)
  104. }
  105. switch network {
  106. case TargetNetworkTCP:
  107. f.Target = net.TCPDestination(addr, port)
  108. case TargetNetworkUDP:
  109. f.Target = net.UDPDestination(addr, port)
  110. default:
  111. return newError("unknown network type: ", network)
  112. }
  113. }
  114. return nil
  115. }