1
0

frame.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package mux
  2. import (
  3. "encoding/binary"
  4. "io"
  5. "github.com/xtls/xray-core/common"
  6. "github.com/xtls/xray-core/common/bitmask"
  7. "github.com/xtls/xray-core/common/buf"
  8. "github.com/xtls/xray-core/common/net"
  9. "github.com/xtls/xray-core/common/protocol"
  10. "github.com/xtls/xray-core/common/serial"
  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. GlobalID [8]byte
  50. }
  51. func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
  52. lenBytes := b.Extend(2)
  53. len0 := b.Len()
  54. sessionBytes := b.Extend(2)
  55. binary.BigEndian.PutUint16(sessionBytes, f.SessionID)
  56. common.Must(b.WriteByte(byte(f.SessionStatus)))
  57. common.Must(b.WriteByte(byte(f.Option)))
  58. if f.SessionStatus == SessionStatusNew {
  59. switch f.Target.Network {
  60. case net.Network_TCP:
  61. common.Must(b.WriteByte(byte(TargetNetworkTCP)))
  62. case net.Network_UDP:
  63. common.Must(b.WriteByte(byte(TargetNetworkUDP)))
  64. }
  65. if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
  66. return err
  67. }
  68. if b.UDP != nil { // make sure it's user's proxy request
  69. b.Write(f.GlobalID[:]) // no need to check whether it's empty
  70. }
  71. } else if b.UDP != nil {
  72. b.WriteByte(byte(TargetNetworkUDP))
  73. addrParser.WriteAddressPort(b, b.UDP.Address, b.UDP.Port)
  74. }
  75. len1 := b.Len()
  76. binary.BigEndian.PutUint16(lenBytes, uint16(len1-len0))
  77. return nil
  78. }
  79. // Unmarshal reads FrameMetadata from the given reader.
  80. func (f *FrameMetadata) Unmarshal(reader io.Reader) error {
  81. metaLen, err := serial.ReadUint16(reader)
  82. if err != nil {
  83. return err
  84. }
  85. if metaLen > 512 {
  86. return newError("invalid metalen ", metaLen).AtError()
  87. }
  88. b := buf.New()
  89. defer b.Release()
  90. if _, err := b.ReadFullFrom(reader, int32(metaLen)); err != nil {
  91. return err
  92. }
  93. return f.UnmarshalFromBuffer(b)
  94. }
  95. // UnmarshalFromBuffer reads a FrameMetadata from the given buffer.
  96. // Visible for testing only.
  97. func (f *FrameMetadata) UnmarshalFromBuffer(b *buf.Buffer) error {
  98. if b.Len() < 4 {
  99. return newError("insufficient buffer: ", b.Len())
  100. }
  101. f.SessionID = binary.BigEndian.Uint16(b.BytesTo(2))
  102. f.SessionStatus = SessionStatus(b.Byte(2))
  103. f.Option = bitmask.Byte(b.Byte(3))
  104. f.Target.Network = net.Network_Unknown
  105. if f.SessionStatus == SessionStatusNew || (f.SessionStatus == SessionStatusKeep && b.Len() > 4 &&
  106. TargetNetwork(b.Byte(4)) == TargetNetworkUDP) { // MUST check the flag first
  107. if b.Len() < 8 {
  108. return newError("insufficient buffer: ", b.Len())
  109. }
  110. network := TargetNetwork(b.Byte(4))
  111. b.Advance(5)
  112. addr, port, err := addrParser.ReadAddressPort(nil, b)
  113. if err != nil {
  114. return newError("failed to parse address and port").Base(err)
  115. }
  116. switch network {
  117. case TargetNetworkTCP:
  118. f.Target = net.TCPDestination(addr, port)
  119. case TargetNetworkUDP:
  120. f.Target = net.UDPDestination(addr, port)
  121. default:
  122. return newError("unknown network type: ", network)
  123. }
  124. }
  125. // Application data is essential, to test whether the pipe is closed.
  126. if f.SessionStatus == SessionStatusNew && f.Option.Has(OptionData) &&
  127. f.Target.Network == net.Network_UDP && b.Len() >= 8 {
  128. copy(f.GlobalID[:], b.Bytes())
  129. }
  130. return nil
  131. }