frame.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package mux
  2. import (
  3. "v2ray.com/core/common/bitmask"
  4. "v2ray.com/core/common/buf"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/serial"
  8. )
  9. type SessionStatus byte
  10. const (
  11. SessionStatusNew SessionStatus = 0x01
  12. SessionStatusKeep SessionStatus = 0x02
  13. SessionStatusEnd SessionStatus = 0x03
  14. SessionStatusKeepAlive SessionStatus = 0x04
  15. )
  16. const (
  17. OptionData bitmask.Byte = 0x01
  18. OptionError bitmask.Byte = 0x02
  19. )
  20. type TargetNetwork byte
  21. const (
  22. TargetNetworkTCP TargetNetwork = 0x01
  23. TargetNetworkUDP TargetNetwork = 0x02
  24. )
  25. var addrParser = protocol.NewAddressParser(
  26. protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4),
  27. protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain),
  28. protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6),
  29. protocol.PortThenAddress(),
  30. )
  31. /*
  32. Frame format
  33. 2 bytes - length
  34. 2 bytes - session id
  35. 1 bytes - status
  36. 1 bytes - option
  37. 1 byte - network
  38. 2 bytes - port
  39. n bytes - address
  40. */
  41. type FrameMetadata struct {
  42. Target net.Destination
  43. SessionID uint16
  44. Option bitmask.Byte
  45. SessionStatus SessionStatus
  46. }
  47. func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
  48. lenBytes := b.Bytes()
  49. b.AppendBytes(0x00, 0x00)
  50. len0 := b.Len()
  51. if err := b.AppendSupplier(serial.WriteUint16(f.SessionID)); err != nil {
  52. return err
  53. }
  54. b.AppendBytes(byte(f.SessionStatus), byte(f.Option))
  55. if f.SessionStatus == SessionStatusNew {
  56. switch f.Target.Network {
  57. case net.Network_TCP:
  58. b.AppendBytes(byte(TargetNetworkTCP))
  59. case net.Network_UDP:
  60. b.AppendBytes(byte(TargetNetworkUDP))
  61. }
  62. if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
  63. return err
  64. }
  65. }
  66. len1 := b.Len()
  67. serial.Uint16ToBytes(uint16(len1-len0), lenBytes)
  68. return nil
  69. }
  70. func ReadFrameFrom(b *buf.Buffer) (*FrameMetadata, error) {
  71. if b.Len() < 4 {
  72. return nil, newError("insufficient buffer: ", b.Len())
  73. }
  74. f := &FrameMetadata{
  75. SessionID: serial.BytesToUint16(b.BytesTo(2)),
  76. SessionStatus: SessionStatus(b.Byte(2)),
  77. Option: bitmask.Byte(b.Byte(3)),
  78. }
  79. if f.SessionStatus == SessionStatusNew {
  80. network := TargetNetwork(b.Byte(4))
  81. b.Advance(5)
  82. addr, port, err := addrParser.ReadAddressPort(nil, b)
  83. if err != nil {
  84. return nil, newError("failed to parse address and port").Base(err)
  85. }
  86. switch network {
  87. case TargetNetworkTCP:
  88. f.Target = net.TCPDestination(addr, port)
  89. case TargetNetworkUDP:
  90. f.Target = net.UDPDestination(addr, port)
  91. default:
  92. return nil, newError("unknown network type: ", network)
  93. }
  94. }
  95. return f, nil
  96. }