2017-02-07 15:11:47 -05:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
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 (
|
2017-10-21 14:40:31 -04:00
|
|
|
OptionData bitmask.Byte = 0x01
|
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
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2017-10-21 15:59:46 -04:00
|
|
|
b = append(b, byte(protocol.AddressTypeIPv4))
|
2017-02-07 15:11:47 -05:00
|
|
|
b = append(b, addr.IP()...)
|
|
|
|
length += 5
|
|
|
|
case net.AddressFamilyIPv6:
|
2017-10-21 15:59:46 -04:00
|
|
|
b = append(b, byte(protocol.AddressTypeIPv6))
|
2017-02-07 15:11:47 -05:00
|
|
|
b = append(b, addr.IP()...)
|
|
|
|
length += 17
|
|
|
|
case net.AddressFamilyDomain:
|
2017-10-22 13:48:19 -04:00
|
|
|
domain := addr.Domain()
|
2017-10-22 14:17:06 -04:00
|
|
|
if protocol.IsDomainTooLong(domain) {
|
|
|
|
return 0, newError("domain name too long: ", domain)
|
2017-10-22 13:48:19 -04:00
|
|
|
}
|
2017-10-22 14:17:06 -04:00
|
|
|
nDomain := len(domain)
|
2017-10-21 15:59:46 -04:00
|
|
|
b = append(b, byte(protocol.AddressTypeDomain), byte(nDomain))
|
2017-10-22 13:48:19 -04:00
|
|
|
b = append(b, 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-10-21 14:40:31 -04:00
|
|
|
Option: bitmask.Byte(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])
|
2017-10-21 15:59:46 -04:00
|
|
|
addrType := protocol.AddressType(b[3])
|
2017-02-07 15:11:47 -05:00
|
|
|
b = b[4:]
|
|
|
|
|
|
|
|
var addr net.Address
|
|
|
|
switch addrType {
|
2017-10-21 15:59:46 -04:00
|
|
|
case protocol.AddressTypeIPv4:
|
2017-02-07 15:11:47 -05:00
|
|
|
addr = net.IPAddress(b[0:4])
|
|
|
|
b = b[4:]
|
2017-10-21 15:59:46 -04:00
|
|
|
case protocol.AddressTypeIPv6:
|
2017-02-07 15:11:47 -05:00
|
|
|
addr = net.IPAddress(b[0:16])
|
|
|
|
b = b[16:]
|
2017-10-21 15:59:46 -04:00
|
|
|
case protocol.AddressTypeDomain:
|
2017-02-07 15:11:47 -05:00
|
|
|
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
|
|
|
|
}
|