You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/app/proxyman/mux/frame.go

120 lines
2.6 KiB

8 years ago
package mux
import (
7 years ago
"v2ray.com/core/common/bitmask"
8 years ago
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
8 years ago
"v2ray.com/core/common/serial"
)
type SessionStatus byte
const (
SessionStatusNew SessionStatus = 0x01
SessionStatusKeep SessionStatus = 0x02
SessionStatusEnd SessionStatus = 0x03
SessionStatusKeepAlive SessionStatus = 0x04
SessionStatusError SessionStatus = 0x05
8 years ago
)
8 years ago
const (
7 years ago
OptionData bitmask.Byte = 0x01
8 years ago
)
8 years ago
type TargetNetwork byte
const (
TargetNetworkTCP TargetNetwork = 0x01
8 years ago
TargetNetworkUDP TargetNetwork = 0x02
8 years ago
)
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(),
)
8 years ago
/*
Frame format
2 bytes - length
2 bytes - session id
1 bytes - status
8 years ago
1 bytes - option
8 years ago
1 byte - network
2 bytes - port
n bytes - address
*/
8 years ago
type FrameMetadata struct {
Target net.Destination
7 years ago
SessionID uint16
7 years ago
Option bitmask.Byte
SessionStatus SessionStatus
8 years ago
}
func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
lenBytes := b.Bytes()
b.AppendBytes(0x00, 0x00)
len0 := b.Len()
if err := b.AppendSupplier(serial.WriteUint16(f.SessionID)); err != nil {
return err
}
b.AppendBytes(byte(f.SessionStatus), byte(f.Option))
if f.SessionStatus == SessionStatusNew {
switch f.Target.Network {
case net.Network_TCP:
b.AppendBytes(byte(TargetNetworkTCP))
case net.Network_UDP:
b.AppendBytes(byte(TargetNetworkUDP))
8 years ago
}
8 years ago
if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
return err
}
8 years ago
}
len1 := b.Len()
serial.Uint16ToBytes(uint16(len1-len0), lenBytes)
return nil
8 years ago
}
func ReadFrameFrom(b *buf.Buffer) (*FrameMetadata, error) {
if b.Len() < 4 {
return nil, newError("insufficient buffer: ", b.Len())
8 years ago
}
f := &FrameMetadata{
SessionID: serial.BytesToUint16(b.BytesTo(2)),
SessionStatus: SessionStatus(b.Byte(2)),
Option: bitmask.Byte(b.Byte(3)),
8 years ago
}
if f.SessionStatus == SessionStatusNew {
network := TargetNetwork(b.Byte(4))
b.SliceFrom(5)
addr, port, err := addrParser.ReadAddressPort(nil, b)
if err != nil {
return nil, newError("failed to parse address and port").Base(err)
8 years ago
}
8 years ago
switch network {
case TargetNetworkTCP:
f.Target = net.TCPDestination(addr, port)
8 years ago
case TargetNetworkUDP:
8 years ago
f.Target = net.UDPDestination(addr, port)
default:
return nil, newError("unknown network type: ", network)
8 years ago
}
}
return f, nil
}