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

145 lines
3.2 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
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
)
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) AsSupplier() buf.Supplier {
return func(b []byte) (int, error) {
8 years ago
lengthBytes := b
b = serial.Uint16ToBytes(uint16(0), b[:0]) // place holder for length
8 years ago
8 years ago
b = serial.Uint16ToBytes(f.SessionID, b)
8 years ago
b = append(b, byte(f.SessionStatus), byte(f.Option))
8 years ago
length := 4
if f.SessionStatus == SessionStatusNew {
switch f.Target.Network {
case net.Network_TCP:
b = append(b, byte(TargetNetworkTCP))
case net.Network_UDP:
8 years ago
b = append(b, byte(TargetNetworkUDP))
8 years ago
}
length++
b = serial.Uint16ToBytes(f.Target.Port.Value(), b)
length += 2
addr := f.Target.Address
switch addr.Family() {
case net.AddressFamilyIPv4:
b = append(b, byte(protocol.AddressTypeIPv4))
8 years ago
b = append(b, addr.IP()...)
length += 5
case net.AddressFamilyIPv6:
b = append(b, byte(protocol.AddressTypeIPv6))
8 years ago
b = append(b, addr.IP()...)
length += 17
case net.AddressFamilyDomain:
domain := addr.Domain()
if protocol.IsDomainTooLong(domain) {
return 0, newError("domain name too long: ", domain)
}
nDomain := len(domain)
b = append(b, byte(protocol.AddressTypeDomain), byte(nDomain))
b = append(b, domain...)
8 years ago
length += nDomain + 2
8 years ago
}
}
8 years ago
8 years ago
serial.Uint16ToBytes(uint16(length), lengthBytes[:0])
8 years ago
return length + 2, nil
}
}
func ReadFrameFrom(b []byte) (*FrameMetadata, error) {
if len(b) < 4 {
return nil, newError("insufficient buffer: ", len(b))
8 years ago
}
f := &FrameMetadata{
8 years ago
SessionID: serial.BytesToUint16(b[:2]),
8 years ago
SessionStatus: SessionStatus(b[2]),
7 years ago
Option: bitmask.Byte(b[3]),
8 years ago
}
b = b[4:]
if f.SessionStatus == SessionStatusNew {
network := TargetNetwork(b[0])
port := net.PortFromBytes(b[1:3])
addrType := protocol.AddressType(b[3])
8 years ago
b = b[4:]
var addr net.Address
switch addrType {
case protocol.AddressTypeIPv4:
8 years ago
addr = net.IPAddress(b[0:4])
b = b[4:]
case protocol.AddressTypeIPv6:
8 years ago
addr = net.IPAddress(b[0:16])
b = b[16:]
case protocol.AddressTypeDomain:
8 years ago
nDomain := int(b[0])
addr = net.DomainAddress(string(b[1 : 1+nDomain]))
b = b[nDomain+1:]
8 years ago
default:
return nil, newError("unknown address type: ", addrType)
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
}