From 095c29fa8c31b9c00b9f1813172d0a109a0d2e5d Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 16 Dec 2016 23:02:11 +0100 Subject: [PATCH] decouple outbound handler manager --- all.go | 1 + app/proxy/proxy_test.go | 3 +- app/proxyman/config.pb.go | 66 ++++++++++++++++++++++++++++ app/proxyman/config.proto | 13 ++++++ app/proxyman/outbound/outbound.go | 72 +++++++++++++++++++++++++++++++ app/proxyman/proxyman.go | 51 +--------------------- app/router/router_test.go | 6 ++- proxy/dokodemo/dokodemo_test.go | 5 ++- proxy/freedom/freedom_test.go | 6 ++- v2ray.go | 8 +++- 10 files changed, 173 insertions(+), 58 deletions(-) create mode 100644 app/proxyman/config.pb.go create mode 100644 app/proxyman/config.proto create mode 100644 app/proxyman/outbound/outbound.go diff --git a/all.go b/all.go index 19e06c6b..39a8647a 100644 --- a/all.go +++ b/all.go @@ -5,6 +5,7 @@ import ( _ "v2ray.com/core/app/dispatcher/impl" _ "v2ray.com/core/app/dns/server" _ "v2ray.com/core/app/proxy" + _ "v2ray.com/core/app/proxyman/outbound" _ "v2ray.com/core/app/router" _ "v2ray.com/core/proxy/blackhole" diff --git a/app/proxy/proxy_test.go b/app/proxy/proxy_test.go index 4e71289d..c7261213 100644 --- a/app/proxy/proxy_test.go +++ b/app/proxy/proxy_test.go @@ -6,6 +6,7 @@ import ( "v2ray.com/core/app" . "v2ray.com/core/app/proxy" "v2ray.com/core/app/proxyman" + "v2ray.com/core/app/proxyman/outbound" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" "v2ray.com/core/proxy/freedom" @@ -18,7 +19,7 @@ func TestProxyDial(t *testing.T) { assert := assert.On(t) space := app.NewSpace() - outboundManager := proxyman.NewDefaultOutboundHandlerManager() + outboundManager := outbound.New() outboundManager.SetHandler("tag", freedom.NewFreedomConnection(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{ Tag: "tag", StreamSettings: &internet.StreamConfig{ diff --git a/app/proxyman/config.pb.go b/app/proxyman/config.pb.go new file mode 100644 index 00000000..f5d1764e --- /dev/null +++ b/app/proxyman/config.pb.go @@ -0,0 +1,66 @@ +// Code generated by protoc-gen-go. +// source: v2ray.com/core/app/proxyman/config.proto +// DO NOT EDIT! + +/* +Package proxyman is a generated protocol buffer package. + +It is generated from these files: + v2ray.com/core/app/proxyman/config.proto + +It has these top-level messages: + InboundConfig + OutboundConfig +*/ +package proxyman + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type InboundConfig struct { +} + +func (m *InboundConfig) Reset() { *m = InboundConfig{} } +func (m *InboundConfig) String() string { return proto.CompactTextString(m) } +func (*InboundConfig) ProtoMessage() {} +func (*InboundConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } + +type OutboundConfig struct { +} + +func (m *OutboundConfig) Reset() { *m = OutboundConfig{} } +func (m *OutboundConfig) String() string { return proto.CompactTextString(m) } +func (*OutboundConfig) ProtoMessage() {} +func (*OutboundConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } + +func init() { + proto.RegisterType((*InboundConfig)(nil), "v2ray.core.app.proxyman.InboundConfig") + proto.RegisterType((*OutboundConfig)(nil), "v2ray.core.app.proxyman.OutboundConfig") +} + +func init() { proto.RegisterFile("v2ray.com/core/app/proxyman/config.proto", fileDescriptor0) } + +var fileDescriptor0 = []byte{ + // 129 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xd2, 0x28, 0x33, 0x2a, 0x4a, + 0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0x2c, 0x28, 0xd0, 0x2f, + 0x28, 0xca, 0xaf, 0xa8, 0xcc, 0x4d, 0xcc, 0xd3, 0x4f, 0xce, 0xcf, 0x4b, 0xcb, 0x4c, 0xd7, 0x2b, + 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x87, 0xa9, 0x2c, 0x4a, 0xd5, 0x4b, 0x2c, 0x28, 0xd0, 0x83, + 0xa9, 0x52, 0xe2, 0xe7, 0xe2, 0xf5, 0xcc, 0x4b, 0xca, 0x2f, 0xcd, 0x4b, 0x71, 0x06, 0xab, 0x57, + 0x12, 0xe0, 0xe2, 0xf3, 0x2f, 0x2d, 0x41, 0x12, 0x71, 0x32, 0xe1, 0x92, 0x4e, 0xce, 0xcf, 0xd5, + 0xc3, 0x61, 0x82, 0x13, 0x37, 0x44, 0x59, 0x00, 0xc8, 0x9e, 0x28, 0x0e, 0x98, 0x70, 0x12, 0x1b, + 0xd8, 0x62, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x41, 0x39, 0xa0, 0x25, 0xa4, 0x00, 0x00, + 0x00, +} diff --git a/app/proxyman/config.proto b/app/proxyman/config.proto new file mode 100644 index 00000000..04f29e26 --- /dev/null +++ b/app/proxyman/config.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +package v2ray.core.app.proxyman; +option go_package = "proxyman"; +option java_package = "com.v2ray.core.app.proxyman"; +option java_outer_classname = "ConfigProto"; + +message InboundConfig { +} + +message OutboundConfig { + +} \ No newline at end of file diff --git a/app/proxyman/outbound/outbound.go b/app/proxyman/outbound/outbound.go new file mode 100644 index 00000000..b2dddc92 --- /dev/null +++ b/app/proxyman/outbound/outbound.go @@ -0,0 +1,72 @@ +package outbound + +import ( + "sync" + "v2ray.com/core/app" + "v2ray.com/core/app/proxyman" + "v2ray.com/core/common/serial" + "v2ray.com/core/proxy" +) + +type DefaultOutboundHandlerManager struct { + sync.RWMutex + defaultHandler proxy.OutboundHandler + taggedHandler map[string]proxy.OutboundHandler +} + +func New() *DefaultOutboundHandlerManager { + return &DefaultOutboundHandlerManager{ + taggedHandler: make(map[string]proxy.OutboundHandler), + } +} + +func (v *DefaultOutboundHandlerManager) Release() { + +} + +func (v *DefaultOutboundHandlerManager) GetDefaultHandler() proxy.OutboundHandler { + v.RLock() + defer v.RUnlock() + if v.defaultHandler == nil { + return nil + } + return v.defaultHandler +} + +func (v *DefaultOutboundHandlerManager) SetDefaultHandler(handler proxy.OutboundHandler) error { + v.Lock() + defer v.Unlock() + v.defaultHandler = handler + return nil +} + +func (v *DefaultOutboundHandlerManager) GetHandler(tag string) proxy.OutboundHandler { + v.RLock() + defer v.RUnlock() + if handler, found := v.taggedHandler[tag]; found { + return handler + } + return nil +} + +func (v *DefaultOutboundHandlerManager) SetHandler(tag string, handler proxy.OutboundHandler) error { + v.Lock() + defer v.Unlock() + + v.taggedHandler[tag] = handler + return nil +} + +type OutboundHandlerManagerFactory struct{} + +func (v OutboundHandlerManagerFactory) Create(space app.Space, config interface{}) (app.Application, error) { + return New(), nil +} + +func (v OutboundHandlerManagerFactory) AppId() app.ID { + return proxyman.APP_ID_OUTBOUND_MANAGER +} + +func init() { + app.RegisterApplicationFactory(serial.GetMessageType(new(proxyman.OutboundConfig)), OutboundHandlerManagerFactory{}) +} diff --git a/app/proxyman/proxyman.go b/app/proxyman/proxyman.go index b5fdaa3e..4a0f8b06 100644 --- a/app/proxyman/proxyman.go +++ b/app/proxyman/proxyman.go @@ -1,8 +1,6 @@ package proxyman import ( - "sync" - "v2ray.com/core/app" "v2ray.com/core/proxy" ) @@ -19,51 +17,6 @@ type InboundHandlerManager interface { type OutboundHandlerManager interface { GetHandler(tag string) proxy.OutboundHandler GetDefaultHandler() proxy.OutboundHandler -} - -type DefaultOutboundHandlerManager struct { - sync.RWMutex - defaultHandler proxy.OutboundHandler - taggedHandler map[string]proxy.OutboundHandler -} - -func NewDefaultOutboundHandlerManager() *DefaultOutboundHandlerManager { - return &DefaultOutboundHandlerManager{ - taggedHandler: make(map[string]proxy.OutboundHandler), - } -} - -func (v *DefaultOutboundHandlerManager) Release() { - -} - -func (v *DefaultOutboundHandlerManager) GetDefaultHandler() proxy.OutboundHandler { - v.RLock() - defer v.RUnlock() - if v.defaultHandler == nil { - return nil - } - return v.defaultHandler -} - -func (v *DefaultOutboundHandlerManager) SetDefaultHandler(handler proxy.OutboundHandler) { - v.Lock() - defer v.Unlock() - v.defaultHandler = handler -} - -func (v *DefaultOutboundHandlerManager) GetHandler(tag string) proxy.OutboundHandler { - v.RLock() - defer v.RUnlock() - if handler, found := v.taggedHandler[tag]; found { - return handler - } - return nil -} - -func (v *DefaultOutboundHandlerManager) SetHandler(tag string, handler proxy.OutboundHandler) { - v.Lock() - defer v.Unlock() - - v.taggedHandler[tag] = handler + SetDefaultHandler(handler proxy.OutboundHandler) error + SetHandler(tag string, handler proxy.OutboundHandler) error } diff --git a/app/router/router_test.go b/app/router/router_test.go index eea09fe1..da2f75d9 100644 --- a/app/router/router_test.go +++ b/app/router/router_test.go @@ -7,7 +7,9 @@ import ( "v2ray.com/core/app/dispatcher" dispatchers "v2ray.com/core/app/dispatcher/impl" "v2ray.com/core/app/dns" + dnsserver "v2ray.com/core/app/dns/server" "v2ray.com/core/app/proxyman" + "v2ray.com/core/app/proxyman/outbound" . "v2ray.com/core/app/router" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" @@ -29,9 +31,9 @@ func TestSimpleRouter(t *testing.T) { } space := app.NewSpace() - space.BindApp(dns.APP_ID, dns.NewCacheServer(space, &dns.Config{})) + space.BindApp(dns.APP_ID, dnsserver.NewCacheServer(space, &dns.Config{})) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) - space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) + space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New()) r := NewRouter(config, space) space.BindApp(APP_ID, r) assert.Error(space.Initialize()).IsNil() diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go index 31ab436f..c8146711 100644 --- a/proxy/dokodemo/dokodemo_test.go +++ b/proxy/dokodemo/dokodemo_test.go @@ -8,6 +8,7 @@ import ( "v2ray.com/core/app/dispatcher" dispatchers "v2ray.com/core/app/dispatcher/impl" "v2ray.com/core/app/proxyman" + "v2ray.com/core/app/proxyman/outbound" "v2ray.com/core/common/dice" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" @@ -37,7 +38,7 @@ func TestDokodemoTCP(t *testing.T) { space := app.NewSpace() space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) - ohm := proxyman.NewDefaultOutboundHandlerManager() + ohm := outbound.New() ohm.SetDefaultHandler( freedom.NewFreedomConnection( &freedom.Config{}, @@ -108,7 +109,7 @@ func TestDokodemoUDP(t *testing.T) { space := app.NewSpace() space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) - ohm := proxyman.NewDefaultOutboundHandlerManager() + ohm := outbound.New() ohm.SetDefaultHandler( freedom.NewFreedomConnection( &freedom.Config{}, diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 557d764c..7b829cee 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -7,7 +7,9 @@ import ( "v2ray.com/core/app/dispatcher" dispatchers "v2ray.com/core/app/dispatcher/impl" "v2ray.com/core/app/dns" + dnsserver "v2ray.com/core/app/dns/server" "v2ray.com/core/app/proxyman" + "v2ray.com/core/app/proxyman/outbound" "v2ray.com/core/app/router" "v2ray.com/core/common/buf" v2net "v2ray.com/core/common/net" @@ -64,11 +66,11 @@ func TestIPResolution(t *testing.T) { assert := assert.On(t) space := app.NewSpace() - space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) + space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New()) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) r := router.NewRouter(&router.Config{}, space) space.BindApp(router.APP_ID, r) - dnsServer := dns.NewCacheServer(space, &dns.Config{ + dnsServer := dnsserver.NewCacheServer(space, &dns.Config{ Hosts: map[string]*v2net.IPOrDomain{ "v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP), }, diff --git a/v2ray.go b/v2ray.go index d09c1ea0..b083a102 100644 --- a/v2ray.go +++ b/v2ray.go @@ -41,8 +41,12 @@ func NewPoint(pConfig *Config) (*Point, error) { vpoint.space = space vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint) - outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager() - vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager) + outboundManagerConfig := new(proxyman.OutboundConfig) + if err := space.BindFromConfig(serial.GetMessageType(outboundManagerConfig), outboundManagerConfig); err != nil { + return nil, err + } + + outboundHandlerManager := space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager) proxyDialer := proxydialer.NewOutboundProxy(space) proxyDialer.RegisterDialer()