frame.go 4.0 KB

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