v2ray-core/app/proxyman/mux/frame.go

145 lines
3.2 KiB
Go
Raw Normal View History

2017-02-07 20:11:47 +00:00
package mux
import (
2017-10-21 18:40:31 +00:00
"v2ray.com/core/common/bitmask"
2017-02-07 20:11:47 +00:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2017-10-21 19:59:46 +00:00
"v2ray.com/core/common/protocol"
2017-02-07 20:11:47 +00:00
"v2ray.com/core/common/serial"
)
type SessionStatus byte
const (
2017-04-12 09:52:10 +00:00
SessionStatusNew SessionStatus = 0x01
SessionStatusKeep SessionStatus = 0x02
SessionStatusEnd SessionStatus = 0x03
SessionStatusKeepAlive SessionStatus = 0x04
2017-02-07 20:11:47 +00:00
)
2017-03-31 22:53:01 +00:00
const (
2017-10-21 18:40:31 +00:00
OptionData bitmask.Byte = 0x01
2017-03-31 22:53:01 +00:00
)
2017-02-07 20:11:47 +00:00
type TargetNetwork byte
const (
TargetNetworkTCP TargetNetwork = 0x01
2017-04-02 07:48:30 +00:00
TargetNetworkUDP TargetNetwork = 0x02
2017-02-07 20:11:47 +00:00
)
2017-03-26 23:47:01 +00:00
/*
Frame format
2 bytes - length
2 bytes - session id
1 bytes - status
2017-03-31 22:53:01 +00:00
1 bytes - option
2017-03-26 23:47:01 +00:00
1 byte - network
2 bytes - port
n bytes - address
*/
2017-02-07 20:11:47 +00:00
type FrameMetadata struct {
Target net.Destination
2017-10-21 17:29:27 +00:00
SessionID uint16
2017-10-21 18:40:31 +00:00
Option bitmask.Byte
SessionStatus SessionStatus
2017-02-07 20:11:47 +00:00
}
func (f FrameMetadata) AsSupplier() buf.Supplier {
return func(b []byte) (int, error) {
2017-04-02 07:48:30 +00:00
lengthBytes := b
b = serial.Uint16ToBytes(uint16(0), b[:0]) // place holder for length
2017-02-07 20:11:47 +00:00
2017-03-26 23:47:01 +00:00
b = serial.Uint16ToBytes(f.SessionID, b)
2017-03-31 22:53:01 +00:00
b = append(b, byte(f.SessionStatus), byte(f.Option))
2017-02-07 20:11:47 +00: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 07:48:30 +00:00
b = append(b, byte(TargetNetworkUDP))
2017-02-07 20:11:47 +00: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 19:59:46 +00:00
b = append(b, byte(protocol.AddressTypeIPv4))
2017-02-07 20:11:47 +00:00
b = append(b, addr.IP()...)
length += 5
case net.AddressFamilyIPv6:
2017-10-21 19:59:46 +00:00
b = append(b, byte(protocol.AddressTypeIPv6))
2017-02-07 20:11:47 +00:00
b = append(b, addr.IP()...)
length += 17
case net.AddressFamilyDomain:
2017-10-22 17:48:19 +00:00
domain := addr.Domain()
if protocol.IsDomainTooLong(domain) {
return 0, newError("domain name too long: ", domain)
2017-10-22 17:48:19 +00:00
}
nDomain := len(domain)
2017-10-21 19:59:46 +00:00
b = append(b, byte(protocol.AddressTypeDomain), byte(nDomain))
2017-10-22 17:48:19 +00:00
b = append(b, domain...)
2017-04-02 07:48:30 +00:00
length += nDomain + 2
2017-02-07 20:11:47 +00:00
}
}
2017-04-03 10:55:46 +00:00
2017-04-02 07:48:30 +00:00
serial.Uint16ToBytes(uint16(length), lengthBytes[:0])
2017-02-07 20:11:47 +00:00
return length + 2, nil
}
}
func ReadFrameFrom(b []byte) (*FrameMetadata, error) {
if len(b) < 4 {
2017-04-08 23:43:25 +00:00
return nil, newError("insufficient buffer: ", len(b))
2017-02-07 20:11:47 +00:00
}
f := &FrameMetadata{
2017-03-26 23:47:01 +00:00
SessionID: serial.BytesToUint16(b[:2]),
2017-02-07 20:11:47 +00:00
SessionStatus: SessionStatus(b[2]),
2017-10-21 18:40:31 +00:00
Option: bitmask.Byte(b[3]),
2017-02-07 20:11:47 +00:00
}
b = b[4:]
if f.SessionStatus == SessionStatusNew {
network := TargetNetwork(b[0])
port := net.PortFromBytes(b[1:3])
2017-10-21 19:59:46 +00:00
addrType := protocol.AddressType(b[3])
2017-02-07 20:11:47 +00:00
b = b[4:]
var addr net.Address
switch addrType {
2017-10-21 19:59:46 +00:00
case protocol.AddressTypeIPv4:
2017-02-07 20:11:47 +00:00
addr = net.IPAddress(b[0:4])
b = b[4:]
2017-10-21 19:59:46 +00:00
case protocol.AddressTypeIPv6:
2017-02-07 20:11:47 +00:00
addr = net.IPAddress(b[0:16])
b = b[16:]
2017-10-21 19:59:46 +00:00
case protocol.AddressTypeDomain:
2017-02-07 20:11:47 +00:00
nDomain := int(b[0])
addr = net.DomainAddress(string(b[1 : 1+nDomain]))
b = b[nDomain+1:]
2017-04-02 07:48:30 +00:00
default:
2017-04-08 23:43:25 +00:00
return nil, newError("unknown address type: ", addrType)
2017-02-07 20:11:47 +00:00
}
switch network {
case TargetNetworkTCP:
f.Target = net.TCPDestination(addr, port)
2017-04-02 07:48:30 +00:00
case TargetNetworkUDP:
2017-02-07 20:11:47 +00:00
f.Target = net.UDPDestination(addr, port)
2017-04-03 11:07:12 +00:00
default:
2017-04-08 23:43:25 +00:00
return nil, newError("unknown network type: ", network)
2017-02-07 20:11:47 +00:00
}
}
return f, nil
}