frame.go 2.8 KB

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