2017-02-07 15:11:47 -05:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
2018-05-26 19:19:05 -04:00
|
|
|
"v2ray.com/core/common"
|
2017-10-21 14:40:31 -04:00
|
|
|
"v2ray.com/core/common/bitmask"
|
2017-02-07 15:11:47 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/net"
|
2017-10-21 15:59:46 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2017-02-07 15:11:47 -05:00
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SessionStatus byte
|
|
|
|
|
|
|
|
const (
|
2017-04-12 05:52:10 -04:00
|
|
|
SessionStatusNew SessionStatus = 0x01
|
|
|
|
SessionStatusKeep SessionStatus = 0x02
|
|
|
|
SessionStatusEnd SessionStatus = 0x03
|
|
|
|
SessionStatusKeepAlive SessionStatus = 0x04
|
2017-02-07 15:11:47 -05:00
|
|
|
)
|
|
|
|
|
2017-03-31 18:53:01 -04:00
|
|
|
const (
|
2018-04-04 15:33:33 -04:00
|
|
|
OptionData bitmask.Byte = 0x01
|
|
|
|
OptionError bitmask.Byte = 0x02
|
2017-03-31 18:53:01 -04:00
|
|
|
)
|
|
|
|
|
2017-02-07 15:11:47 -05:00
|
|
|
type TargetNetwork byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
TargetNetworkTCP TargetNetwork = 0x01
|
2017-04-02 03:48:30 -04:00
|
|
|
TargetNetworkUDP TargetNetwork = 0x02
|
2017-02-07 15:11:47 -05:00
|
|
|
)
|
|
|
|
|
2018-02-23 18:57:54 -05:00
|
|
|
var addrParser = protocol.NewAddressParser(
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv4), net.AddressFamilyIPv4),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeDomain), net.AddressFamilyDomain),
|
|
|
|
protocol.AddressFamilyByte(byte(protocol.AddressTypeIPv6), net.AddressFamilyIPv6),
|
|
|
|
protocol.PortThenAddress(),
|
|
|
|
)
|
|
|
|
|
2017-03-26 19:47:01 -04:00
|
|
|
/*
|
|
|
|
Frame format
|
|
|
|
2 bytes - length
|
|
|
|
2 bytes - session id
|
|
|
|
1 bytes - status
|
2017-03-31 18:53:01 -04:00
|
|
|
1 bytes - option
|
2017-03-26 19:47:01 -04:00
|
|
|
|
|
|
|
1 byte - network
|
|
|
|
2 bytes - port
|
|
|
|
n bytes - address
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2017-02-07 15:11:47 -05:00
|
|
|
type FrameMetadata struct {
|
|
|
|
Target net.Destination
|
2017-10-21 13:29:27 -04:00
|
|
|
SessionID uint16
|
2017-10-21 14:40:31 -04:00
|
|
|
Option bitmask.Byte
|
2017-10-21 13:27:53 -04:00
|
|
|
SessionStatus SessionStatus
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
2018-02-23 18:57:54 -05:00
|
|
|
func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
|
|
|
|
lenBytes := b.Bytes()
|
2018-05-26 19:19:05 -04:00
|
|
|
common.Must2(b.AppendBytes(0x00, 0x00))
|
2018-02-23 18:57:54 -05:00
|
|
|
|
|
|
|
len0 := b.Len()
|
|
|
|
if err := b.AppendSupplier(serial.WriteUint16(f.SessionID)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-26 19:19:05 -04:00
|
|
|
common.Must2(b.AppendBytes(byte(f.SessionStatus), byte(f.Option)))
|
2018-02-23 18:57:54 -05:00
|
|
|
|
|
|
|
if f.SessionStatus == SessionStatusNew {
|
|
|
|
switch f.Target.Network {
|
|
|
|
case net.Network_TCP:
|
2018-05-26 19:19:05 -04:00
|
|
|
common.Must2(b.AppendBytes(byte(TargetNetworkTCP)))
|
2018-02-23 18:57:54 -05:00
|
|
|
case net.Network_UDP:
|
2018-05-26 19:19:05 -04:00
|
|
|
common.Must2(b.AppendBytes(byte(TargetNetworkUDP)))
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
2017-04-03 06:55:46 -04:00
|
|
|
|
2018-02-23 18:57:54 -05:00
|
|
|
if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
2018-02-23 18:57:54 -05:00
|
|
|
|
|
|
|
len1 := b.Len()
|
|
|
|
serial.Uint16ToBytes(uint16(len1-len0), lenBytes)
|
|
|
|
return nil
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
2018-05-26 19:19:05 -04:00
|
|
|
// ReadFrameFrom reads a FrameMetadata from the given buffer.
|
|
|
|
// Visible for testing only.
|
2018-02-23 18:57:54 -05:00
|
|
|
func ReadFrameFrom(b *buf.Buffer) (*FrameMetadata, error) {
|
|
|
|
if b.Len() < 4 {
|
|
|
|
return nil, newError("insufficient buffer: ", b.Len())
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f := &FrameMetadata{
|
2018-02-23 18:57:54 -05:00
|
|
|
SessionID: serial.BytesToUint16(b.BytesTo(2)),
|
|
|
|
SessionStatus: SessionStatus(b.Byte(2)),
|
|
|
|
Option: bitmask.Byte(b.Byte(3)),
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if f.SessionStatus == SessionStatusNew {
|
2018-02-23 18:57:54 -05:00
|
|
|
network := TargetNetwork(b.Byte(4))
|
2018-04-19 17:48:38 -04:00
|
|
|
b.Advance(5)
|
2018-02-23 18:57:54 -05:00
|
|
|
|
|
|
|
addr, port, err := addrParser.ReadAddressPort(nil, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse address and port").Base(err)
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
2018-02-23 18:57:54 -05:00
|
|
|
|
2017-02-07 15:11:47 -05:00
|
|
|
switch network {
|
|
|
|
case TargetNetworkTCP:
|
|
|
|
f.Target = net.TCPDestination(addr, port)
|
2017-04-02 03:48:30 -04:00
|
|
|
case TargetNetworkUDP:
|
2017-02-07 15:11:47 -05:00
|
|
|
f.Target = net.UDPDestination(addr, port)
|
2017-04-03 07:07:12 -04:00
|
|
|
default:
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("unknown network type: ", network)
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|