frame.go 3.2 KB

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