From 1ab02e0a6b97d7849a1d61df2229fdc4129c0ddf Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 15 Dec 2016 15:46:20 +0100 Subject: [PATCH] refactor proxy registry --- inbound_detour_always.go | 3 +-- inbound_detour_dynamic.go | 5 ++--- proxy/blackhole/init.go | 4 ++-- proxy/creator.go | 16 ++++++++++++++++ proxy/dokodemo/dokodemo.go | 3 +-- proxy/freedom/freedom.go | 3 +-- proxy/{registry => }/handler_cache.go | 15 +++++++-------- proxy/http/server.go | 3 +-- proxy/registry/creator.go | 17 ----------------- proxy/shadowsocks/init.go | 6 +++--- proxy/socks/server.go | 3 +-- proxy/vmess/inbound/inbound.go | 3 +-- proxy/vmess/outbound/outbound.go | 3 +-- v2ray.go | 3 +-- 14 files changed, 38 insertions(+), 49 deletions(-) create mode 100644 proxy/creator.go rename proxy/{registry => }/handler_cache.go (77%) delete mode 100644 proxy/registry/creator.go diff --git a/inbound_detour_always.go b/inbound_detour_always.go index 2213aa66f..9c1dc6e5d 100644 --- a/inbound_detour_always.go +++ b/inbound_detour_always.go @@ -6,7 +6,6 @@ import ( "v2ray.com/core/common/log" "v2ray.com/core/common/retry" "v2ray.com/core/proxy" - proxyregistry "v2ray.com/core/proxy/registry" ) // Handler for inbound detour connections. @@ -28,7 +27,7 @@ func NewInboundDetourHandlerAlways(space app.Space, config *InboundConnectionCon if err != nil { return nil, err } - ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{ + ich, err := proxy.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{ Address: config.GetListenOnValue(), Port: i, Tag: config.Tag, diff --git a/inbound_detour_dynamic.go b/inbound_detour_dynamic.go index 7740e677e..1a4b72a11 100644 --- a/inbound_detour_dynamic.go +++ b/inbound_detour_dynamic.go @@ -10,7 +10,6 @@ import ( v2net "v2ray.com/core/common/net" "v2ray.com/core/common/retry" "v2ray.com/core/proxy" - proxyregistry "v2ray.com/core/proxy/registry" ) type InboundDetourHandlerDynamic struct { @@ -36,7 +35,7 @@ func NewInboundDetourHandlerDynamic(space app.Space, config *InboundConnectionCo if err != nil { return nil, err } - ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{ + ich, err := proxy.CreateInboundHandler(config.Settings.Type, space, ichConfig, &proxy.InboundHandlerMeta{ Address: config.GetListenOnValue(), Port: 0, Tag: config.Tag, @@ -108,7 +107,7 @@ func (v *InboundDetourHandlerDynamic) refresh() error { err := retry.Timed(5, 100).On(func() error { port := v.pickUnusedPort() ichConfig, _ := config.GetTypedSettings() - ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, v.space, ichConfig, &proxy.InboundHandlerMeta{ + ich, err := proxy.CreateInboundHandler(config.Settings.Type, v.space, ichConfig, &proxy.InboundHandlerMeta{ Address: config.GetListenOnValue(), Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings}) if err != nil { delete(v.portsInUse, port) diff --git a/proxy/blackhole/init.go b/proxy/blackhole/init.go index b165987ae..df356a3ea 100644 --- a/proxy/blackhole/init.go +++ b/proxy/blackhole/init.go @@ -2,10 +2,10 @@ package blackhole import ( "v2ray.com/core/common/serial" - "v2ray.com/core/proxy/registry" + "v2ray.com/core/proxy" ) func init() { // Must listed after config.pb.go - registry.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) } diff --git a/proxy/creator.go b/proxy/creator.go new file mode 100644 index 000000000..34a6dd213 --- /dev/null +++ b/proxy/creator.go @@ -0,0 +1,16 @@ +package proxy + +import ( + "v2ray.com/core/app" + v2net "v2ray.com/core/common/net" +) + +type InboundHandlerFactory interface { + StreamCapability() v2net.NetworkList + Create(space app.Space, config interface{}, meta *InboundHandlerMeta) (InboundHandler, error) +} + +type OutboundHandlerFactory interface { + StreamCapability() v2net.NetworkList + Create(space app.Space, config interface{}, meta *OutboundHandlerMeta) (OutboundHandler, error) +} diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index acae024f2..023d6a522 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -11,7 +11,6 @@ import ( v2net "v2ray.com/core/common/net" "v2ray.com/core/common/serial" "v2ray.com/core/proxy" - "v2ray.com/core/proxy/registry" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/udp" ) @@ -212,5 +211,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Inb } func init() { - registry.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) } diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 9b7a8fc3f..3cfb31825 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -13,7 +13,6 @@ import ( "v2ray.com/core/common/retry" "v2ray.com/core/common/serial" "v2ray.com/core/proxy" - "v2ray.com/core/proxy/registry" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/tcp" "v2ray.com/core/transport/ray" @@ -142,5 +141,5 @@ func (v *FreedomFactory) Create(space app.Space, config interface{}, meta *proxy } func init() { - registry.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(FreedomFactory)) + proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(FreedomFactory)) } diff --git a/proxy/registry/handler_cache.go b/proxy/handler_cache.go similarity index 77% rename from proxy/registry/handler_cache.go rename to proxy/handler_cache.go index ca2a0b142..5e4d6c5f0 100644 --- a/proxy/registry/handler_cache.go +++ b/proxy/handler_cache.go @@ -1,10 +1,9 @@ -package registry +package proxy import ( "v2ray.com/core/app" "v2ray.com/core/common" "v2ray.com/core/common/errors" - "v2ray.com/core/proxy" "v2ray.com/core/transport/internet" ) @@ -41,10 +40,10 @@ func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerFact } } -func CreateInboundHandler(name string, space app.Space, config interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { +func CreateInboundHandler(name string, space app.Space, config interface{}, meta *InboundHandlerMeta) (InboundHandler, error) { creator, found := inboundFactories[name] if !found { - return nil, errors.New("Proxy|Registry: Unknown inbound name: " + name) + return nil, errors.New("Proxy: Unknown inbound name: " + name) } if meta.StreamSettings == nil { meta.StreamSettings = &internet.StreamConfig{ @@ -52,17 +51,17 @@ func CreateInboundHandler(name string, space app.Space, config interface{}, meta } } else { if !creator.StreamCapability().HasNetwork(meta.StreamSettings.Network) { - return nil, errors.New("Proxy|Registry: Invalid network: " + meta.StreamSettings.Network.String()) + return nil, errors.New("Proxy: Invalid network: " + meta.StreamSettings.Network.String()) } } return creator.Create(space, config, meta) } -func CreateOutboundHandler(name string, space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { +func CreateOutboundHandler(name string, space app.Space, config interface{}, meta *OutboundHandlerMeta) (OutboundHandler, error) { creator, found := outboundFactories[name] if !found { - return nil, errors.New("Proxy|Registry: Unknown outbound name: " + name) + return nil, errors.New("Proxy: Unknown outbound name: " + name) } if meta.StreamSettings == nil { meta.StreamSettings = &internet.StreamConfig{ @@ -70,7 +69,7 @@ func CreateOutboundHandler(name string, space app.Space, config interface{}, met } } else { if !creator.StreamCapability().HasNetwork(meta.StreamSettings.Network) { - return nil, errors.New("Proxy|Registry: Invalid network: " + meta.StreamSettings.Network.String()) + return nil, errors.New("Proxy: Invalid network: " + meta.StreamSettings.Network.String()) } } diff --git a/proxy/http/server.go b/proxy/http/server.go index 4d06ed462..5ab02fc11 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -18,7 +18,6 @@ import ( v2net "v2ray.com/core/common/net" "v2ray.com/core/common/serial" "v2ray.com/core/proxy" - "v2ray.com/core/proxy/registry" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/ray" ) @@ -288,5 +287,5 @@ func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *pro } func init() { - registry.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) + proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) } diff --git a/proxy/registry/creator.go b/proxy/registry/creator.go deleted file mode 100644 index 3ddb35161..000000000 --- a/proxy/registry/creator.go +++ /dev/null @@ -1,17 +0,0 @@ -package registry - -import ( - "v2ray.com/core/app" - v2net "v2ray.com/core/common/net" - "v2ray.com/core/proxy" -) - -type InboundHandlerFactory interface { - StreamCapability() v2net.NetworkList - Create(space app.Space, config interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) -} - -type OutboundHandlerFactory interface { - StreamCapability() v2net.NetworkList - Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) -} diff --git a/proxy/shadowsocks/init.go b/proxy/shadowsocks/init.go index 3942e6cb6..13b25e098 100644 --- a/proxy/shadowsocks/init.go +++ b/proxy/shadowsocks/init.go @@ -2,11 +2,11 @@ package shadowsocks import ( "v2ray.com/core/common/serial" - "v2ray.com/core/proxy/registry" + "v2ray.com/core/proxy" ) func init() { // Must happen after config is initialized - registry.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(ClientConfig)), new(ClientFactory)) - registry.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) + proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(ClientConfig)), new(ClientFactory)) + proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) } diff --git a/proxy/socks/server.go b/proxy/socks/server.go index d78b79abb..b9d7e5f98 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -15,7 +15,6 @@ import ( v2net "v2ray.com/core/common/net" "v2ray.com/core/common/serial" "v2ray.com/core/proxy" - "v2ray.com/core/proxy/registry" "v2ray.com/core/proxy/socks/protocol" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/udp" @@ -334,5 +333,5 @@ func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *pro } func init() { - registry.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) + proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 4ad6cda4d..ef0e0aab6 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -17,7 +17,6 @@ import ( "v2ray.com/core/common/serial" "v2ray.com/core/common/uuid" "v2ray.com/core/proxy" - "v2ray.com/core/proxy/registry" "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/encoding" "v2ray.com/core/transport/internet" @@ -272,5 +271,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Inb } func init() { - registry.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index d345ea331..0daa74a72 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -12,7 +12,6 @@ import ( "v2ray.com/core/common/retry" "v2ray.com/core/common/serial" "v2ray.com/core/proxy" - "v2ray.com/core/proxy/registry" "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/encoding" "v2ray.com/core/transport/internet" @@ -176,5 +175,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Out } func init() { - registry.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) } diff --git a/v2ray.go b/v2ray.go index 3b593c7b3..afcaca38b 100644 --- a/v2ray.go +++ b/v2ray.go @@ -11,7 +11,6 @@ import ( "v2ray.com/core/common/log" v2net "v2ray.com/core/common/net" "v2ray.com/core/proxy" - proxyregistry "v2ray.com/core/proxy/registry" ) // Point shell of V2Ray. @@ -109,7 +108,7 @@ func NewPoint(pConfig *Config) (*Point, error) { if err != nil { return nil, err } - outboundHandler, err := proxyregistry.CreateOutboundHandler( + outboundHandler, err := proxy.CreateOutboundHandler( outbound.Settings.Type, vpoint.space, outboundSettings, &proxy.OutboundHandlerMeta{ Tag: outbound.Tag, Address: outbound.GetSendThroughValue(),