frame.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/errors"
  9. "github.com/xtls/xray-core/common/net"
  10. "github.com/xtls/xray-core/common/protocol"
  11. "github.com/xtls/xray-core/common/serial"
  12. "github.com/xtls/xray-core/common/session"
  13. )
  14. type SessionStatus byte
  15. const (
  16. SessionStatusNew SessionStatus = 0x01
  17. SessionStatusKeep SessionStatus = 0x02
  18. SessionStatusEnd SessionStatus = 0x03
  19. SessionStatusKeepAlive SessionStatus = 0x04
  20. )
  21. const (
  22. OptionData bitmask.Byte = 0x01
  23. OptionError bitmask.Byte = 0x02
  24. )
  25. type TargetNetwork byte
  26. const (
  27. TargetNetworkTCP TargetNetwork = 0x01
  28. TargetNetworkUDP TargetNetwork = 0x02
  29. )
  30. var addrParser = protocol.NewAddressParser(
  31. protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4),
  32. protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain),
  33. protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6),
  34. protocol.PortThenAddress(),
  35. )
  36. /*
  37. Frame format
  38. 2 bytes - length
  39. 2 bytes - session id
  40. 1 bytes - status
  41. 1 bytes - option
  42. 1 byte - network
  43. 2 bytes - port
  44. n bytes - address
  45. */
  46. type FrameMetadata struct {
  47. Target net.Destination
  48. SessionID uint16
  49. Option bitmask.Byte
  50. SessionStatus SessionStatus
  51. GlobalID [8]byte
  52. Inbound *session.Inbound
  53. }
  54. func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
  55. lenBytes := b.Extend(2)
  56. len0 := b.Len()
  57. sessionBytes := b.Extend(2)
  58. binary.BigEndian.PutUint16(sessionBytes, f.SessionID)
  59. common.Must(b.WriteByte(byte(f.SessionStatus)))
  60. common.Must(b.WriteByte(byte(f.Option)))
  61. if f.SessionStatus == SessionStatusNew {
  62. switch f.Target.Network {
  63. case net.Network_TCP:
  64. common.Must(b.WriteByte(byte(TargetNetworkTCP)))
  65. case net.Network_UDP:
  66. common.Must(b.WriteByte(byte(TargetNetworkUDP)))
  67. }
  68. if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
  69. return err
  70. }
  71. if f.Inbound != nil {
  72. if f.Inbound.Source.Network == net.Network_TCP || f.Inbound.Source.Network == net.Network_UDP {
  73. common.Must(b.WriteByte(byte(f.Inbound.Source.Network - 1)))
  74. if err := addrParser.WriteAddressPort(b, f.Inbound.Source.Address, f.Inbound.Source.Port); err != nil {
  75. return err
  76. }
  77. if f.Inbound.Local.Network == net.Network_TCP || f.Inbound.Local.Network == net.Network_UDP {
  78. common.Must(b.WriteByte(byte(f.Inbound.Local.Network - 1)))
  79. if err := addrParser.WriteAddressPort(b, f.Inbound.Local.Address, f.Inbound.Local.Port); err != nil {
  80. return err
  81. }
  82. }
  83. }
  84. } else if b.UDP != nil { // make sure it's user's proxy request
  85. b.Write(f.GlobalID[:]) // no need to check whether it's empty
  86. }
  87. } else if b.UDP != nil {
  88. b.WriteByte(byte(TargetNetworkUDP))
  89. addrParser.WriteAddressPort(b, b.UDP.Address, b.UDP.Port)
  90. }
  91. len1 := b.Len()
  92. binary.BigEndian.PutUint16(lenBytes, uint16(len1-len0))
  93. return nil
  94. }
  95. // Unmarshal reads FrameMetadata from the given reader.
  96. func (f *FrameMetadata) Unmarshal(reader io.Reader, readSourceAndLocal bool) error {
  97. metaLen, err := serial.ReadUint16(reader)
  98. if err != nil {
  99. return err
  100. }
  101. if metaLen > 512 {
  102. return errors.New("invalid metalen ", metaLen).AtError()
  103. }
  104. b := buf.New()
  105. defer b.Release()
  106. if _, err := b.ReadFullFrom(reader, int32(metaLen)); err != nil {
  107. return err
  108. }
  109. return f.UnmarshalFromBuffer(b, readSourceAndLocal)
  110. }
  111. // UnmarshalFromBuffer reads a FrameMetadata from the given buffer.
  112. // Visible for testing only.
  113. func (f *FrameMetadata) UnmarshalFromBuffer(b *buf.Buffer, readSourceAndLocal bool) error {
  114. if b.Len() < 4 {
  115. return errors.New("insufficient buffer: ", b.Len())
  116. }
  117. f.SessionID = binary.BigEndian.Uint16(b.BytesTo(2))
  118. f.SessionStatus = SessionStatus(b.Byte(2))
  119. f.Option = bitmask.Byte(b.Byte(3))
  120. f.Target.Network = net.Network_Unknown
  121. if f.SessionStatus == SessionStatusNew || (f.SessionStatus == SessionStatusKeep && b.Len() > 4 &&
  122. TargetNetwork(b.Byte(4)) == TargetNetworkUDP) { // MUST check the flag first
  123. if b.Len() < 8 {
  124. return errors.New("insufficient buffer: ", b.Len())
  125. }
  126. network := TargetNetwork(b.Byte(4))
  127. b.Advance(5)
  128. addr, port, err := addrParser.ReadAddressPort(nil, b)
  129. if err != nil {
  130. return errors.New("failed to parse address and port").Base(err)
  131. }
  132. switch network {
  133. case TargetNetworkTCP:
  134. f.Target = net.TCPDestination(addr, port)
  135. case TargetNetworkUDP:
  136. f.Target = net.UDPDestination(addr, port)
  137. default:
  138. return errors.New("unknown network type: ", network)
  139. }
  140. }
  141. if f.SessionStatus == SessionStatusNew && readSourceAndLocal {
  142. f.Inbound = &session.Inbound{}
  143. if b.Len() == 0 {
  144. return nil // for heartbeat, etc.
  145. }
  146. network := TargetNetwork(b.Byte(0))
  147. if network == 0 {
  148. return nil // may be padding
  149. }
  150. b.Advance(1)
  151. addr, port, err := addrParser.ReadAddressPort(nil, b)
  152. if err != nil {
  153. return errors.New("reading source: failed to parse address and port").Base(err)
  154. }
  155. switch network {
  156. case TargetNetworkTCP:
  157. f.Inbound.Source = net.TCPDestination(addr, port)
  158. case TargetNetworkUDP:
  159. f.Inbound.Source = net.UDPDestination(addr, port)
  160. default:
  161. return errors.New("reading source: unknown network type: ", network)
  162. }
  163. if b.Len() == 0 {
  164. return nil
  165. }
  166. network = TargetNetwork(b.Byte(0))
  167. if network == 0 {
  168. return nil
  169. }
  170. b.Advance(1)
  171. addr, port, err = addrParser.ReadAddressPort(nil, b)
  172. if err != nil {
  173. return errors.New("reading local: failed to parse address and port").Base(err)
  174. }
  175. switch network {
  176. case TargetNetworkTCP:
  177. f.Inbound.Local = net.TCPDestination(addr, port)
  178. case TargetNetworkUDP:
  179. f.Inbound.Local = net.UDPDestination(addr, port)
  180. default:
  181. return errors.New("reading local: unknown network type: ", network)
  182. }
  183. return nil
  184. }
  185. // Application data is essential, to test whether the pipe is closed.
  186. if f.SessionStatus == SessionStatusNew && f.Option.Has(OptionData) &&
  187. f.Target.Network == net.Network_UDP && b.Len() >= 8 {
  188. copy(f.GlobalID[:], b.Bytes())
  189. }
  190. return nil
  191. }