frame.go 2.6 KB

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