2017-02-07 15:11:47 -05:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"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
|
|
|
type Option byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
OptionData Option = 0x01
|
|
|
|
)
|
|
|
|
|
|
|
|
func (o Option) Has(x Option) bool {
|
|
|
|
return (o & x) == x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Option) Add(x Option) {
|
|
|
|
*o = (*o | x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Option) Clear(x Option) {
|
|
|
|
*o = (*o & (^x))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type AddressType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
AddressTypeIPv4 AddressType = 0x01
|
|
|
|
AddressTypeDomain AddressType = 0x02
|
|
|
|
AddressTypeIPv6 AddressType = 0x03
|
|
|
|
)
|
|
|
|
|
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 {
|
2017-03-26 19:47:01 -04:00
|
|
|
SessionID uint16
|
2017-02-07 15:11:47 -05:00
|
|
|
SessionStatus SessionStatus
|
|
|
|
Target net.Destination
|
2017-03-31 18:53:01 -04:00
|
|
|
Option Option
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f FrameMetadata) AsSupplier() buf.Supplier {
|
|
|
|
return func(b []byte) (int, error) {
|
2017-04-02 03:48:30 -04:00
|
|
|
lengthBytes := b
|
|
|
|
b = serial.Uint16ToBytes(uint16(0), b[:0]) // place holder for length
|
2017-02-07 15:11:47 -05:00
|
|
|
|
2017-03-26 19:47:01 -04:00
|
|
|
b = serial.Uint16ToBytes(f.SessionID, b)
|
2017-03-31 18:53:01 -04:00
|
|
|
b = append(b, byte(f.SessionStatus), byte(f.Option))
|
2017-02-07 15:11:47 -05:00
|
|
|
length := 4
|
|
|
|
|
|
|
|
if f.SessionStatus == SessionStatusNew {
|
|
|
|
switch f.Target.Network {
|
|
|
|
case net.Network_TCP:
|
|
|
|
b = append(b, byte(TargetNetworkTCP))
|
|
|
|
case net.Network_UDP:
|
2017-04-02 03:48:30 -04:00
|
|
|
b = append(b, byte(TargetNetworkUDP))
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
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(AddressTypeIPv4))
|
|
|
|
b = append(b, addr.IP()...)
|
|
|
|
length += 5
|
|
|
|
case net.AddressFamilyIPv6:
|
|
|
|
b = append(b, byte(AddressTypeIPv6))
|
|
|
|
b = append(b, addr.IP()...)
|
|
|
|
length += 17
|
|
|
|
case net.AddressFamilyDomain:
|
|
|
|
nDomain := len(addr.Domain())
|
2017-04-02 03:48:30 -04:00
|
|
|
b = append(b, byte(AddressTypeDomain), byte(nDomain))
|
2017-02-07 15:11:47 -05:00
|
|
|
b = append(b, addr.Domain()...)
|
2017-04-02 03:48:30 -04:00
|
|
|
length += nDomain + 2
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-03 06:55:46 -04:00
|
|
|
|
2017-04-02 03:48:30 -04:00
|
|
|
serial.Uint16ToBytes(uint16(length), lengthBytes[:0])
|
2017-02-07 15:11:47 -05:00
|
|
|
return length + 2, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadFrameFrom(b []byte) (*FrameMetadata, error) {
|
|
|
|
if len(b) < 4 {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("insufficient buffer: ", len(b))
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f := &FrameMetadata{
|
2017-03-26 19:47:01 -04:00
|
|
|
SessionID: serial.BytesToUint16(b[:2]),
|
2017-02-07 15:11:47 -05:00
|
|
|
SessionStatus: SessionStatus(b[2]),
|
2017-04-02 03:48:30 -04:00
|
|
|
Option: Option(b[3]),
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
b = b[4:]
|
|
|
|
|
|
|
|
if f.SessionStatus == SessionStatusNew {
|
|
|
|
network := TargetNetwork(b[0])
|
|
|
|
port := net.PortFromBytes(b[1:3])
|
|
|
|
addrType := AddressType(b[3])
|
|
|
|
b = b[4:]
|
|
|
|
|
|
|
|
var addr net.Address
|
|
|
|
switch addrType {
|
|
|
|
case AddressTypeIPv4:
|
|
|
|
addr = net.IPAddress(b[0:4])
|
|
|
|
b = b[4:]
|
|
|
|
case AddressTypeIPv6:
|
|
|
|
addr = net.IPAddress(b[0:16])
|
|
|
|
b = b[16:]
|
|
|
|
case AddressTypeDomain:
|
|
|
|
nDomain := int(b[0])
|
|
|
|
addr = net.DomainAddress(string(b[1 : 1+nDomain]))
|
|
|
|
b = b[nDomain+1:]
|
2017-04-02 03:48:30 -04:00
|
|
|
default:
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("unknown address type: ", addrType)
|
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
|
|
|
|
}
|