1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/common/mux/frame.go

146 lines
3.3 KiB
Go
Raw Normal View History

2017-02-07 21:11:47 +01:00
package mux
import (
2018-11-02 15:01:33 +01:00
"encoding/binary"
2018-09-26 13:01:12 +02:00
"io"
2018-05-27 01:19:05 +02:00
"v2ray.com/core/common"
2017-10-21 20:40:31 +02:00
"v2ray.com/core/common/bitmask"
2017-02-07 21:11:47 +01:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
2017-10-21 21:59:46 +02:00
"v2ray.com/core/common/protocol"
2018-11-03 13:03:02 +01:00
"v2ray.com/core/common/serial"
2017-02-07 21:11:47 +01:00
)
type SessionStatus byte
const (
2017-04-12 11:52:10 +02:00
SessionStatusNew SessionStatus = 0x01
SessionStatusKeep SessionStatus = 0x02
SessionStatusEnd SessionStatus = 0x03
SessionStatusKeepAlive SessionStatus = 0x04
2017-02-07 21:11:47 +01:00
)
2017-04-01 00:53:01 +02:00
const (
2018-04-04 21:33:33 +02:00
OptionData bitmask.Byte = 0x01
OptionError bitmask.Byte = 0x02
2017-04-01 00:53:01 +02:00
)
2017-02-07 21:11:47 +01:00
type TargetNetwork byte
const (
TargetNetworkTCP TargetNetwork = 0x01
2017-04-02 09:48:30 +02:00
TargetNetworkUDP TargetNetwork = 0x02
2017-02-07 21:11:47 +01:00
)
2018-02-24 00:57:54 +01:00
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(),
)
2017-03-27 01:47:01 +02:00
/*
Frame format
2 bytes - length
2 bytes - session id
1 bytes - status
2017-04-01 00:53:01 +02:00
1 bytes - option
2017-03-27 01:47:01 +02:00
1 byte - network
2 bytes - port
n bytes - address
*/
2017-02-07 21:11:47 +01:00
type FrameMetadata struct {
Target net.Destination
2017-10-21 19:29:27 +02:00
SessionID uint16
2017-10-21 20:40:31 +02:00
Option bitmask.Byte
SessionStatus SessionStatus
2017-02-07 21:11:47 +01:00
}
2018-02-24 00:57:54 +01:00
func (f FrameMetadata) WriteTo(b *buf.Buffer) error {
2018-11-14 22:11:05 +01:00
lenBytes := b.Extend(2)
2018-02-24 00:57:54 +01:00
len0 := b.Len()
2018-11-14 22:11:05 +01:00
sessionBytes := b.Extend(2)
binary.BigEndian.PutUint16(sessionBytes, f.SessionID)
2018-02-24 00:57:54 +01:00
2018-11-14 22:11:05 +01:00
common.Must(b.WriteByte(byte(f.SessionStatus)))
common.Must(b.WriteByte(byte(f.Option)))
2018-02-24 00:57:54 +01:00
if f.SessionStatus == SessionStatusNew {
switch f.Target.Network {
case net.Network_TCP:
2018-11-14 22:11:05 +01:00
common.Must(b.WriteByte(byte(TargetNetworkTCP)))
2018-02-24 00:57:54 +01:00
case net.Network_UDP:
2018-11-14 22:11:05 +01:00
common.Must(b.WriteByte(byte(TargetNetworkUDP)))
2017-02-07 21:11:47 +01:00
}
2017-04-03 12:55:46 +02:00
2018-02-24 00:57:54 +01:00
if err := addrParser.WriteAddressPort(b, f.Target.Address, f.Target.Port); err != nil {
return err
}
2017-02-07 21:11:47 +01:00
}
2018-02-24 00:57:54 +01:00
len1 := b.Len()
2018-11-02 15:01:33 +01:00
binary.BigEndian.PutUint16(lenBytes, uint16(len1-len0))
2018-02-24 00:57:54 +01:00
return nil
2017-02-07 21:11:47 +01:00
}
2018-10-14 08:05:23 +02:00
// Unmarshal reads FrameMetadata from the given reader.
func (f *FrameMetadata) Unmarshal(reader io.Reader) error {
2018-11-03 13:03:02 +01:00
metaLen, err := serial.ReadUint16(reader)
2018-09-26 13:01:12 +02:00
if err != nil {
return err
}
if metaLen > 512 {
return newError("invalid metalen ", metaLen).AtError()
}
b := buf.New()
defer b.Release()
2018-11-02 15:01:33 +01:00
if _, err := b.ReadFullFrom(reader, int32(metaLen)); err != nil {
2018-09-26 13:01:12 +02:00
return err
}
2018-10-14 08:05:23 +02:00
return f.UnmarshalFromBuffer(b)
2018-09-26 13:01:12 +02:00
}
2018-10-14 08:05:23 +02:00
// UnmarshalFromBuffer reads a FrameMetadata from the given buffer.
2018-05-27 01:19:05 +02:00
// Visible for testing only.
2018-10-14 08:05:23 +02:00
func (f *FrameMetadata) UnmarshalFromBuffer(b *buf.Buffer) error {
2018-02-24 00:57:54 +01:00
if b.Len() < 4 {
2018-09-26 13:01:12 +02:00
return newError("insufficient buffer: ", b.Len())
2017-02-07 21:11:47 +01:00
}
2018-11-02 15:47:58 +01:00
f.SessionID = binary.BigEndian.Uint16(b.BytesTo(2))
2018-09-26 13:01:12 +02:00
f.SessionStatus = SessionStatus(b.Byte(2))
f.Option = bitmask.Byte(b.Byte(3))
f.Target.Network = net.Network_Unknown
2017-02-07 21:11:47 +01:00
if f.SessionStatus == SessionStatusNew {
2018-11-14 22:55:33 +01:00
if b.Len() < 8 {
return newError("insufficient buffer: ", b.Len())
}
2018-02-24 00:57:54 +01:00
network := TargetNetwork(b.Byte(4))
b.Advance(5)
2018-02-24 00:57:54 +01:00
addr, port, err := addrParser.ReadAddressPort(nil, b)
if err != nil {
2018-09-26 13:01:12 +02:00
return newError("failed to parse address and port").Base(err)
2017-02-07 21:11:47 +01:00
}
2018-02-24 00:57:54 +01:00
2017-02-07 21:11:47 +01:00
switch network {
case TargetNetworkTCP:
f.Target = net.TCPDestination(addr, port)
2017-04-02 09:48:30 +02:00
case TargetNetworkUDP:
2017-02-07 21:11:47 +01:00
f.Target = net.UDPDestination(addr, port)
2017-04-03 13:07:12 +02:00
default:
2018-09-26 13:01:12 +02:00
return newError("unknown network type: ", network)
2017-02-07 21:11:47 +01:00
}
}
2018-09-26 13:01:12 +02:00
return nil
2017-02-07 21:11:47 +01:00
}