From b652032d9d81f39e3d55eaf2a03a0e3f9fa06471 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 7 Feb 2017 21:11:47 +0100 Subject: [PATCH] update for mux --- app/proxyman/config.pb.go | 177 +++++++++++++++++++++++------------ app/proxyman/config.proto | 15 ++- app/proxyman/mux/frame.go | 121 ++++++++++++++++++++++++ app/proxyman/mux/mux.go | 10 ++ app/proxyman/mux/reader.go | 59 ++++++++++++ app/proxyman/mux/writer.go | 40 ++++++++ app/proxyman/outbound/mux.go | 8 ++ 7 files changed, 367 insertions(+), 63 deletions(-) create mode 100644 app/proxyman/mux/frame.go create mode 100644 app/proxyman/mux/mux.go create mode 100644 app/proxyman/mux/reader.go create mode 100644 app/proxyman/mux/writer.go create mode 100644 app/proxyman/outbound/mux.go diff --git a/app/proxyman/config.pb.go b/app/proxyman/config.pb.go index 30d317dc9..5f6f13380 100644 --- a/app/proxyman/config.pb.go +++ b/app/proxyman/config.pb.go @@ -47,6 +47,30 @@ func (x AllocationStrategy_Type) String() string { } func (AllocationStrategy_Type) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{1, 0} } +type SessionFrame_FrameCommand int32 + +const ( + SessionFrame_SessionNew SessionFrame_FrameCommand = 0 + SessionFrame_SessionKeep SessionFrame_FrameCommand = 1 + SessionFrame_SessionEnd SessionFrame_FrameCommand = 2 +) + +var SessionFrame_FrameCommand_name = map[int32]string{ + 0: "SessionNew", + 1: "SessionKeep", + 2: "SessionEnd", +} +var SessionFrame_FrameCommand_value = map[string]int32{ + "SessionNew": 0, + "SessionKeep": 1, + "SessionEnd": 2, +} + +func (x SessionFrame_FrameCommand) String() string { + return proto.EnumName(SessionFrame_FrameCommand_name, int32(x)) +} +func (SessionFrame_FrameCommand) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{9, 0} } + type InboundConfig struct { } @@ -195,6 +219,7 @@ type InboundHandlerConfig struct { Tag string `protobuf:"bytes,1,opt,name=tag" json:"tag,omitempty"` ReceiverSettings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,2,opt,name=receiver_settings,json=receiverSettings" json:"receiver_settings,omitempty"` ProxySettings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,3,opt,name=proxy_settings,json=proxySettings" json:"proxy_settings,omitempty"` + DispatchSettings *DispatchConfig `protobuf:"bytes,4,opt,name=dispatch_settings,json=dispatchSettings" json:"dispatch_settings,omitempty"` } func (m *InboundHandlerConfig) Reset() { *m = InboundHandlerConfig{} } @@ -223,6 +248,13 @@ func (m *InboundHandlerConfig) GetProxySettings() *v2ray_core_common_serial.Type return nil } +func (m *InboundHandlerConfig) GetDispatchSettings() *DispatchConfig { + if m != nil { + return m.DispatchSettings + } + return nil +} + type OutboundConfig struct { } @@ -265,11 +297,12 @@ func (m *SenderConfig) GetProxySettings() *v2ray_core_transport_internet.ProxyCo } type OutboundHandlerConfig struct { - Tag string `protobuf:"bytes,1,opt,name=tag" json:"tag,omitempty"` - SenderSettings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,2,opt,name=sender_settings,json=senderSettings" json:"sender_settings,omitempty"` - ProxySettings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,3,opt,name=proxy_settings,json=proxySettings" json:"proxy_settings,omitempty"` - Expire int64 `protobuf:"varint,4,opt,name=expire" json:"expire,omitempty"` - Comment string `protobuf:"bytes,5,opt,name=comment" json:"comment,omitempty"` + Tag string `protobuf:"bytes,1,opt,name=tag" json:"tag,omitempty"` + SenderSettings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,2,opt,name=sender_settings,json=senderSettings" json:"sender_settings,omitempty"` + ProxySettings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,3,opt,name=proxy_settings,json=proxySettings" json:"proxy_settings,omitempty"` + Expire int64 `protobuf:"varint,4,opt,name=expire" json:"expire,omitempty"` + Comment string `protobuf:"bytes,5,opt,name=comment" json:"comment,omitempty"` + DispatchSettings *DispatchConfig `protobuf:"bytes,6,opt,name=dispatch_settings,json=dispatchSettings" json:"dispatch_settings,omitempty"` } func (m *OutboundHandlerConfig) Reset() { *m = OutboundHandlerConfig{} } @@ -312,6 +345,13 @@ func (m *OutboundHandlerConfig) GetComment() string { return "" } +func (m *OutboundHandlerConfig) GetDispatchSettings() *DispatchConfig { + if m != nil { + return m.DispatchSettings + } + return nil +} + type MultiplexingConfig struct { Enabled bool `protobuf:"varint,1,opt,name=enabled" json:"enabled,omitempty"` } @@ -346,8 +386,9 @@ func (m *DispatchConfig) GetMuxSettings() *MultiplexingConfig { type SessionFrame struct { Id uint32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"` - Target *v2ray_core_common_net2.Endpoint `protobuf:"bytes,2,opt,name=target" json:"target,omitempty"` - Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"` + Command SessionFrame_FrameCommand `protobuf:"varint,2,opt,name=command,enum=v2ray.core.app.proxyman.SessionFrame_FrameCommand" json:"command,omitempty"` + Target *v2ray_core_common_net2.Endpoint `protobuf:"bytes,3,opt,name=target" json:"target,omitempty"` + Payload []byte `protobuf:"bytes,4,opt,name=payload,proto3" json:"payload,omitempty"` } func (m *SessionFrame) Reset() { *m = SessionFrame{} } @@ -362,6 +403,13 @@ func (m *SessionFrame) GetId() uint32 { return 0 } +func (m *SessionFrame) GetCommand() SessionFrame_FrameCommand { + if m != nil { + return m.Command + } + return SessionFrame_SessionNew +} + func (m *SessionFrame) GetTarget() *v2ray_core_common_net2.Endpoint { if m != nil { return m.Target @@ -390,63 +438,70 @@ func init() { proto.RegisterType((*DispatchConfig)(nil), "v2ray.core.app.proxyman.DispatchConfig") proto.RegisterType((*SessionFrame)(nil), "v2ray.core.app.proxyman.SessionFrame") proto.RegisterEnum("v2ray.core.app.proxyman.AllocationStrategy_Type", AllocationStrategy_Type_name, AllocationStrategy_Type_value) + proto.RegisterEnum("v2ray.core.app.proxyman.SessionFrame_FrameCommand", SessionFrame_FrameCommand_name, SessionFrame_FrameCommand_value) } func init() { proto.RegisterFile("v2ray.com/core/app/proxyman/config.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 846 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xb4, 0x55, 0xdd, 0x6e, 0x23, 0x35, - 0x18, 0x65, 0x92, 0x6e, 0xda, 0x7e, 0x6d, 0xd3, 0x60, 0x96, 0xdd, 0x10, 0x40, 0x94, 0x08, 0x41, - 0xc5, 0xa2, 0xc9, 0x92, 0x15, 0x02, 0xae, 0xa0, 0xa4, 0x45, 0xf4, 0xa2, 0x34, 0x38, 0x2b, 0x2e, - 0x10, 0x52, 0x70, 0x67, 0xbc, 0xb3, 0x16, 0x33, 0xb6, 0xb1, 0x9d, 0x6e, 0xe6, 0x95, 0x78, 0x06, - 0x2e, 0x78, 0x00, 0x1e, 0x85, 0x37, 0xe0, 0x06, 0xf9, 0x67, 0x92, 0xa8, 0xc9, 0x50, 0xca, 0x8a, - 0xbb, 0x71, 0x7c, 0xce, 0xb1, 0xcf, 0x77, 0xbe, 0xcf, 0x81, 0xe3, 0xeb, 0xa1, 0x22, 0x65, 0x9c, - 0x88, 0x62, 0x90, 0x08, 0x45, 0x07, 0x44, 0xca, 0x81, 0x54, 0x62, 0x5e, 0x16, 0x84, 0x0f, 0x12, - 0xc1, 0x9f, 0xb1, 0x2c, 0x96, 0x4a, 0x18, 0x81, 0x1e, 0x56, 0x48, 0x45, 0x63, 0x22, 0x65, 0x5c, - 0xa1, 0x7a, 0x8f, 0x6f, 0x48, 0x24, 0xa2, 0x28, 0x04, 0x1f, 0x68, 0xaa, 0x18, 0xc9, 0x07, 0xa6, - 0x94, 0x34, 0x9d, 0x16, 0x54, 0x6b, 0x92, 0x51, 0x2f, 0xd5, 0xfb, 0x60, 0x33, 0x83, 0x53, 0x33, - 0x20, 0x69, 0xaa, 0xa8, 0xd6, 0x01, 0xf8, 0xa8, 0x1e, 0x98, 0x52, 0x6d, 0x18,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0x29, 0xd3, 0x92, - 0x98, 0xe4, 0x79, 0xc0, 0x7e, 0x0b, 0xfb, 0xc5, 0x6c, 0xbe, 0x34, 0x10, 0xdd, 0xf2, 0x70, 0xac, - 0x1f, 0x87, 0xf7, 0x8a, 0xd9, 0x7c, 0x91, 0xc7, 0x2f, 0xb6, 0xfb, 0xb4, 0x66, 0x82, 0x7f, 0xad, - 0x48, 0x41, 0x51, 0x1b, 0x1a, 0x2c, 0x0d, 0xef, 0x6d, 0x83, 0xa5, 0xe8, 0x53, 0x68, 0x19, 0xa2, - 0x32, 0x6a, 0x42, 0xe9, 0xdf, 0xa9, 0x69, 0xc8, 0x33, 0x9e, 0x4a, 0xc1, 0xb8, 0xc1, 0x01, 0x6e, - 0x4d, 0x49, 0x52, 0xe6, 0x82, 0xa4, 0xae, 0xc8, 0xfb, 0xb8, 0x5a, 0x7e, 0x35, 0x82, 0x37, 0x13, - 0x51, 0xd4, 0xdd, 0x78, 0x1c, 0xfd, 0xb0, 0x53, 0x7d, 0xff, 0xda, 0x78, 0xf8, 0xfd, 0x10, 0x93, - 0x32, 0x1e, 0x59, 0xd4, 0x89, 0x94, 0xbe, 0xb3, 0x0a, 0xc2, 0xaf, 0x5a, 0xee, 0x1f, 0xfb, 0xc9, - 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xa8, 0xcf, 0x2c, 0xca, 0xd4, 0x08, 0x00, 0x00, + // 929 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xb4, 0x56, 0xd1, 0x6e, 0x23, 0x35, + 0x14, 0xdd, 0x4c, 0xba, 0x69, 0x7b, 0x93, 0x4e, 0x67, 0xcd, 0xb2, 0x1b, 0x02, 0x88, 0x12, 0x21, + 0xb6, 0x62, 0xd1, 0x64, 0xc9, 0x0a, 0x01, 0x4f, 0x4b, 0x49, 0x8b, 0xa8, 0xa0, 0xdb, 0xe0, 0x54, + 0x3c, 0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 0x9a, 0x09, 0xfe, 0xb5, 0x22, + 0x29, 0x45, 0x3e, 0x78, 0x2c, 0x76, 0xff, 0x1c, 0x3c, 0x16, 0xa3, 0xef, 0x0a, 0x89, 0x08, 0x8f, + 0xad, 0xa5, 0x7e, 0xbf, 0x5f, 0xb9, 0xd7, 0x32, 0x4f, 0x68, 0xff, 0x0e, 0x8a, 0x4a, 0x5c, 0x52, + 0xa0, 0xcf, 0xa0, 0x61, 0x88, 0x9a, 0x50, 0xe3, 0xfc, 0x7c, 0xaf, 0xa2, 0x6b, 0x8e, 0x78, 0x2c, + 0x05, 0xe3, 0x06, 0x3b, 0x78, 0xae, 0x91, 0x24, 0x59, 0x22, 0x48, 0x6c, 0x2d, 0x6c, 0xe1, 0x72, + 0xda, 0x7d, 0x06, 0xad, 0xe5, 0xbd, 0x90, 0x0f, 0xe0, 0x0e, 0xf2, 0x9c, 0x5e, 0x06, 0x77, 0xd0, + 0x2e, 0x34, 0xdd, 0xfc, 0x5b, 0x4a, 0x65, 0x50, 0x5b, 0x02, 0x1c, 0xf1, 0x38, 0xf0, 0xbe, 0x1a, + 0xc0, 0xdb, 0x91, 0x48, 0xab, 0x6e, 0x35, 0xac, 0xfd, 0xb8, 0x55, 0x8e, 0x7f, 0xf7, 0x1e, 0xfe, + 0xd0, 0xc7, 0x24, 0x0b, 0x07, 0x39, 0xea, 0x40, 0xca, 0xa2, 0x7f, 0x52, 0xc2, 0xcf, 0x1b, 0xf6, + 0xd7, 0xce, 0xd3, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0x78, 0xee, 0xf7, 0xe3, 0x10, 0x0a, 0x00, + 0x00, } diff --git a/app/proxyman/config.proto b/app/proxyman/config.proto index 4dadb25b1..36c982553 100644 --- a/app/proxyman/config.proto +++ b/app/proxyman/config.proto @@ -60,6 +60,7 @@ message InboundHandlerConfig { string tag = 1; v2ray.core.common.serial.TypedMessage receiver_settings = 2; v2ray.core.common.serial.TypedMessage proxy_settings = 3; + DispatchConfig dispatch_settings = 4; } message OutboundConfig { @@ -79,6 +80,7 @@ message OutboundHandlerConfig { v2ray.core.common.serial.TypedMessage proxy_settings = 3; int64 expire = 4; string comment = 5; + DispatchConfig dispatch_settings = 6; } message MultiplexingConfig { @@ -91,6 +93,15 @@ message DispatchConfig { message SessionFrame { uint32 id = 1; - v2ray.core.common.net.Endpoint target = 2; - bytes payload = 3; + + enum FrameCommand { + SessionNew = 0; + SessionKeep = 1; + SessionEnd = 2; + } + + FrameCommand command = 2; + + v2ray.core.common.net.Endpoint target = 3; + bytes payload = 4; } diff --git a/app/proxyman/mux/frame.go b/app/proxyman/mux/frame.go new file mode 100644 index 000000000..04eca155f --- /dev/null +++ b/app/proxyman/mux/frame.go @@ -0,0 +1,121 @@ +package mux + +import ( + "errors" + + "v2ray.com/core/common/buf" + "v2ray.com/core/common/net" + "v2ray.com/core/common/serial" +) + +type SessionStatus byte + +const ( + SessionStatusNew SessionStatus = 0x01 + SessionStatusKeep SessionStatus = 0x02 + SessionStatusEnd SessionStatus = 0x03 +) + +type TargetNetwork byte + +const ( + TargetNetworkTCP TargetNetwork = 0x01 + TargetnetworkUDP TargetNetwork = 0x02 +) + +type AddressType byte + +const ( + AddressTypeIPv4 AddressType = 0x01 + AddressTypeDomain AddressType = 0x02 + AddressTypeIPv6 AddressType = 0x03 +) + +type FrameMetadata struct { + SessionId uint16 + SessionStatus SessionStatus + Target net.Destination +} + +func (f FrameMetadata) AsSupplier() buf.Supplier { + return func(b []byte) (int, error) { + b = serial.Uint16ToBytes(uint16(0), b) // place holder for length + + b = serial.Uint16ToBytes(f.SessionId, b) + b = append(b, byte(f.SessionStatus), 0 /* reserved */) + length := 4 + + if f.SessionStatus == SessionStatusNew { + switch f.Target.Network { + case net.Network_TCP: + b = append(b, byte(TargetNetworkTCP)) + case net.Network_UDP: + b = append(b, byte(TargetnetworkUDP)) + } + 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()) + b = append(b, byte(nDomain)) + b = append(b, addr.Domain()...) + length += nDomain + 1 + } + } + return length + 2, nil + } +} + +func ReadFrameFrom(b []byte) (*FrameMetadata, error) { + if len(b) < 4 { + return nil, errors.New("Proxyman|Mux: Insufficient buffer.") + } + + f := &FrameMetadata{ + SessionId: serial.BytesToUint16(b[:2]), + SessionStatus: SessionStatus(b[2]), + } + + 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:] + } + switch network { + case TargetNetworkTCP: + f.Target = net.TCPDestination(addr, port) + case TargetnetworkUDP: + f.Target = net.UDPDestination(addr, port) + } + } + + return f, nil +} diff --git a/app/proxyman/mux/mux.go b/app/proxyman/mux/mux.go new file mode 100644 index 000000000..023e466e3 --- /dev/null +++ b/app/proxyman/mux/mux.go @@ -0,0 +1,10 @@ +package mux + +import "v2ray.com/core/common/net" + +type mergerWorker struct { +} + +type Merger struct { + sessions map[net.Destination]mergerWorker +} diff --git a/app/proxyman/mux/reader.go b/app/proxyman/mux/reader.go new file mode 100644 index 000000000..d35af9ebf --- /dev/null +++ b/app/proxyman/mux/reader.go @@ -0,0 +1,59 @@ +package mux + +import "io" +import "v2ray.com/core/common/buf" +import "v2ray.com/core/common/serial" + +type muxReader struct { + reader io.Reader + remainingLength int + buffer *buf.Buffer +} + +func NewReader(reader buf.Reader) *muxReader { + return &muxReader{ + reader: buf.NewBytesReader(reader), + buffer: buf.NewLocal(1024), + } +} + +func (r *muxReader) ReadMetadata() (*FrameMetadata, error) { + b := r.buffer + b.Clear() + + if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, 2)); err != nil { + return nil, err + } + metaLen := serial.BytesToUint16(b.Bytes()) + b.Clear() + if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, int(metaLen))); err != nil { + return nil, err + } + return ReadFrameFrom(b.Bytes()) +} + +func (r *muxReader) Read() (*buf.Buffer, bool, error) { + b := buf.New() + var dataLen int + if r.remainingLength > 0 { + dataLen = r.remainingLength + r.remainingLength = 0 + } else { + if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, 2)); err != nil { + return nil, false, err + } + dataLen = int(serial.BytesToUint16(b.Bytes())) + b.Clear() + } + + if dataLen > buf.Size { + r.remainingLength = dataLen - buf.Size + dataLen = buf.Size + } + + if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, dataLen)); err != nil { + return nil, false, err + } + + return b, (r.remainingLength > 0), nil +} diff --git a/app/proxyman/mux/writer.go b/app/proxyman/mux/writer.go new file mode 100644 index 000000000..5a0993934 --- /dev/null +++ b/app/proxyman/mux/writer.go @@ -0,0 +1,40 @@ +package mux + +import "v2ray.com/core/common/buf" +import "v2ray.com/core/common/serial" + +type muxWriter struct { + meta *FrameMetadata + writer buf.Writer +} + +func (w *muxWriter) Write(b *buf.Buffer) error { + frame := buf.New() + frame.AppendSupplier(w.meta.AsSupplier()) + if w.meta.SessionStatus == SessionStatusNew { + w.meta.SessionStatus = SessionStatusKeep + } + + frame.AppendSupplier(serial.WriteUint16(0)) + lengthBytes := frame.BytesFrom(-2) + + nBytes, err := frame.Write(b.Bytes()) + if err != nil { + return err + } + + serial.Uint16ToBytes(uint16(nBytes), lengthBytes[:0]) + if err := w.writer.Write(frame); err != nil { + frame.Release() + b.Release() + return err + } + + b.SliceFrom(nBytes) + if !b.IsEmpty() { + return w.Write(b) + } + b.Release() + + return nil +} diff --git a/app/proxyman/outbound/mux.go b/app/proxyman/outbound/mux.go new file mode 100644 index 000000000..81ecf85f6 --- /dev/null +++ b/app/proxyman/outbound/mux.go @@ -0,0 +1,8 @@ +package outbound + +import "v2ray.com/core/proxy" + +type mux struct { + proxy proxy.Outbound + dialer proxy.Dialer +}