frame.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package mux
  2. import (
  3. "v2ray.com/core/common/buf"
  4. "v2ray.com/core/common/net"
  5. "v2ray.com/core/common/serial"
  6. )
  7. type SessionStatus byte
  8. const (
  9. SessionStatusNew SessionStatus = 0x01
  10. SessionStatusKeep SessionStatus = 0x02
  11. SessionStatusEnd SessionStatus = 0x03
  12. )
  13. type Option byte
  14. const (
  15. OptionData Option = 0x01
  16. )
  17. func (o Option) Has(x Option) bool {
  18. return (o & x) == x
  19. }
  20. func (o *Option) Add(x Option) {
  21. *o = (*o | x)
  22. }
  23. func (o *Option) Clear(x Option) {
  24. *o = (*o & (^x))
  25. }
  26. type TargetNetwork byte
  27. const (
  28. TargetNetworkTCP TargetNetwork = 0x01
  29. TargetNetworkUDP TargetNetwork = 0x02
  30. )
  31. type AddressType byte
  32. const (
  33. AddressTypeIPv4 AddressType = 0x01
  34. AddressTypeDomain AddressType = 0x02
  35. AddressTypeIPv6 AddressType = 0x03
  36. )
  37. /*
  38. Frame format
  39. 2 bytes - length
  40. 2 bytes - session id
  41. 1 bytes - status
  42. 1 bytes - option
  43. 1 byte - network
  44. 2 bytes - port
  45. n bytes - address
  46. */
  47. type FrameMetadata struct {
  48. SessionID uint16
  49. SessionStatus SessionStatus
  50. Target net.Destination
  51. Option Option
  52. }
  53. func (f FrameMetadata) AsSupplier() buf.Supplier {
  54. return func(b []byte) (int, error) {
  55. lengthBytes := b
  56. b = serial.Uint16ToBytes(uint16(0), b[:0]) // place holder for length
  57. b = serial.Uint16ToBytes(f.SessionID, b)
  58. b = append(b, byte(f.SessionStatus), byte(f.Option))
  59. length := 4
  60. if f.SessionStatus == SessionStatusNew {
  61. switch f.Target.Network {
  62. case net.Network_TCP:
  63. b = append(b, byte(TargetNetworkTCP))
  64. case net.Network_UDP:
  65. b = append(b, byte(TargetNetworkUDP))
  66. }
  67. length++
  68. b = serial.Uint16ToBytes(f.Target.Port.Value(), b)
  69. length += 2
  70. addr := f.Target.Address
  71. switch addr.Family() {
  72. case net.AddressFamilyIPv4:
  73. b = append(b, byte(AddressTypeIPv4))
  74. b = append(b, addr.IP()...)
  75. length += 5
  76. case net.AddressFamilyIPv6:
  77. b = append(b, byte(AddressTypeIPv6))
  78. b = append(b, addr.IP()...)
  79. length += 17
  80. case net.AddressFamilyDomain:
  81. nDomain := len(addr.Domain())
  82. b = append(b, byte(AddressTypeDomain), byte(nDomain))
  83. b = append(b, addr.Domain()...)
  84. length += nDomain + 2
  85. }
  86. }
  87. serial.Uint16ToBytes(uint16(length), lengthBytes[:0])
  88. return length + 2, nil
  89. }
  90. }
  91. func ReadFrameFrom(b []byte) (*FrameMetadata, error) {
  92. if len(b) < 4 {
  93. return nil, newError("insufficient buffer: ", len(b))
  94. }
  95. f := &FrameMetadata{
  96. SessionID: serial.BytesToUint16(b[:2]),
  97. SessionStatus: SessionStatus(b[2]),
  98. Option: Option(b[3]),
  99. }
  100. b = b[4:]
  101. if f.SessionStatus == SessionStatusNew {
  102. network := TargetNetwork(b[0])
  103. port := net.PortFromBytes(b[1:3])
  104. addrType := AddressType(b[3])
  105. b = b[4:]
  106. var addr net.Address
  107. switch addrType {
  108. case AddressTypeIPv4:
  109. addr = net.IPAddress(b[0:4])
  110. b = b[4:]
  111. case AddressTypeIPv6:
  112. addr = net.IPAddress(b[0:16])
  113. b = b[16:]
  114. case AddressTypeDomain:
  115. nDomain := int(b[0])
  116. addr = net.DomainAddress(string(b[1 : 1+nDomain]))
  117. b = b[nDomain+1:]
  118. default:
  119. return nil, newError("unknown address type: ", addrType)
  120. }
  121. switch network {
  122. case TargetNetworkTCP:
  123. f.Target = net.TCPDestination(addr, port)
  124. case TargetNetworkUDP:
  125. f.Target = net.UDPDestination(addr, port)
  126. default:
  127. return nil, newError("unknown network type: ", network)
  128. }
  129. }
  130. return f, nil
  131. }