frame.go 3.2 KB

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