diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index d8cbf031..f753c59c 100644 --- a/proxy/blackhole/blackhole.go +++ b/proxy/blackhole/blackhole.go @@ -5,8 +5,8 @@ import ( "github.com/v2ray/v2ray-core/app" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/transport/ray" ) @@ -31,7 +31,7 @@ func (this *BlackHole) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) e } func init() { - if err := proxy.RegisterOutboundConnectionHandlerFactory("blackhole", func(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { + if err := internal.RegisterOutboundConnectionHandlerFactory("blackhole", func(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { return NewBlackHole(), nil }); err != nil { panic(err) diff --git a/proxy/dokodemo/dokodemo_factory.go b/proxy/dokodemo/dokodemo_factory.go index 43f1ef78..b352e356 100644 --- a/proxy/dokodemo/dokodemo_factory.go +++ b/proxy/dokodemo/dokodemo_factory.go @@ -2,12 +2,12 @@ package dokodemo import ( "github.com/v2ray/v2ray-core/app" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal" ) func init() { - if err := proxy.RegisterInboundConnectionHandlerFactory("dokodemo-door", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { + if err := internal.RegisterInboundConnectionHandlerFactory("dokodemo-door", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { config := rawConfig.(Config) return NewDokodemoDoor(space, config), nil }); err != nil { diff --git a/proxy/freedom/freedomfactory.go b/proxy/freedom/freedomfactory.go index dd061afe..1f1843bf 100644 --- a/proxy/freedom/freedomfactory.go +++ b/proxy/freedom/freedomfactory.go @@ -2,12 +2,12 @@ package freedom import ( "github.com/v2ray/v2ray-core/app" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal" ) func init() { - if err := proxy.RegisterOutboundConnectionHandlerFactory("freedom", func(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { + if err := internal.RegisterOutboundConnectionHandlerFactory("freedom", func(space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { return &FreedomConnection{space: space}, nil }); err != nil { panic(err) diff --git a/proxy/http/http_factory.go b/proxy/http/http_factory.go index 4c3f679b..da3d9d78 100644 --- a/proxy/http/http_factory.go +++ b/proxy/http/http_factory.go @@ -2,12 +2,12 @@ package http import ( "github.com/v2ray/v2ray-core/app" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal" ) func init() { - if err := proxy.RegisterInboundConnectionHandlerFactory("http", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { + if err := internal.RegisterInboundConnectionHandlerFactory("http", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { return NewHttpProxyServer(space, rawConfig.(Config)), nil }); err != nil { panic(err) diff --git a/proxy/internal/handler_cache.go b/proxy/internal/handler_cache.go new file mode 100644 index 00000000..87ff131d --- /dev/null +++ b/proxy/internal/handler_cache.go @@ -0,0 +1,66 @@ +package internal + +import ( + "errors" + + "github.com/v2ray/v2ray-core/app" + "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal/config" +) + +var ( + inboundFactories = make(map[string]InboundConnectionHandlerCreator) + outboundFactories = make(map[string]OutboundConnectionHandlerCreator) + + ErrorProxyNotFound = errors.New("Proxy not found.") + ErrorNameExists = errors.New("Proxy with the same name already exists.") + ErrorBadConfiguration = errors.New("Bad proxy configuration.") +) + +func RegisterInboundConnectionHandlerFactory(name string, creator InboundConnectionHandlerCreator) error { + if _, found := inboundFactories[name]; found { + return ErrorNameExists + } + inboundFactories[name] = creator + return nil +} + +func RegisterOutboundConnectionHandlerFactory(name string, creator OutboundConnectionHandlerCreator) error { + if _, found := outboundFactories[name]; found { + return ErrorNameExists + } + outboundFactories[name] = creator + return nil +} + +func CreateInboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.InboundConnectionHandler, error) { + creator, found := inboundFactories[name] + if !found { + return nil, ErrorProxyNotFound + } + if len(rawConfig) > 0 { + proxyConfig, err := config.CreateInboundConnectionConfig(name, rawConfig) + if err != nil { + return nil, err + } + return creator(space, proxyConfig) + } + return creator(space, nil) +} + +func CreateOutboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.OutboundConnectionHandler, error) { + creator, found := outboundFactories[name] + if !found { + return nil, ErrorNameExists + } + + if len(rawConfig) > 0 { + proxyConfig, err := config.CreateOutboundConnectionConfig(name, rawConfig) + if err != nil { + return nil, err + } + return creator(space, proxyConfig) + } + + return creator(space, nil) +} diff --git a/proxy/proxy.go b/proxy/proxy.go index 2be50a49..b29f6d4f 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -3,67 +3,15 @@ package proxy import ( - "errors" - "github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/proxy/common/connhandler" "github.com/v2ray/v2ray-core/proxy/internal" - "github.com/v2ray/v2ray-core/proxy/internal/config" ) -var ( - inboundFactories = make(map[string]internal.InboundConnectionHandlerCreator) - outboundFactories = make(map[string]internal.OutboundConnectionHandlerCreator) - - ErrorProxyNotFound = errors.New("Proxy not found.") - ErrorNameExists = errors.New("Proxy with the same name already exists.") - ErrorBadConfiguration = errors.New("Bad proxy configuration.") -) - -func RegisterInboundConnectionHandlerFactory(name string, creator internal.InboundConnectionHandlerCreator) error { - if _, found := inboundFactories[name]; found { - return ErrorNameExists - } - inboundFactories[name] = creator - return nil -} - -func RegisterOutboundConnectionHandlerFactory(name string, creator internal.OutboundConnectionHandlerCreator) error { - if _, found := outboundFactories[name]; found { - return ErrorNameExists - } - outboundFactories[name] = creator - return nil -} - func CreateInboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.InboundConnectionHandler, error) { - creator, found := inboundFactories[name] - if !found { - return nil, ErrorProxyNotFound - } - if len(rawConfig) > 0 { - proxyConfig, err := config.CreateInboundConnectionConfig(name, rawConfig) - if err != nil { - return nil, err - } - return creator(space, proxyConfig) - } - return creator(space, nil) + return internal.CreateInboundConnectionHandler(name, space, rawConfig) } func CreateOutboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.OutboundConnectionHandler, error) { - creator, found := outboundFactories[name] - if !found { - return nil, ErrorNameExists - } - - if len(rawConfig) > 0 { - proxyConfig, err := config.CreateOutboundConnectionConfig(name, rawConfig) - if err != nil { - return nil, err - } - return creator(space, proxyConfig) - } - - return creator(space, nil) + return internal.CreateOutboundConnectionHandler(name, space, rawConfig) } diff --git a/proxy/socks/socksfactory.go b/proxy/socks/socksfactory.go index 21321468..3ab8be45 100644 --- a/proxy/socks/socksfactory.go +++ b/proxy/socks/socksfactory.go @@ -2,12 +2,12 @@ package socks import ( "github.com/v2ray/v2ray-core/app" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal" ) func init() { - if err := proxy.RegisterInboundConnectionHandlerFactory("socks", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { + if err := internal.RegisterInboundConnectionHandlerFactory("socks", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { return NewSocksServer(space, rawConfig.(Config)), nil }); err != nil { panic(err) diff --git a/proxy/testing/proxy.go b/proxy/testing/proxy.go index 1333894a..b0f2cecf 100644 --- a/proxy/testing/proxy.go +++ b/proxy/testing/proxy.go @@ -3,7 +3,6 @@ package testing import ( "fmt" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/internal" ) @@ -17,8 +16,8 @@ func randomString() string { func RegisterInboundConnectionHandlerCreator(prefix string, creator internal.InboundConnectionHandlerCreator) (string, error) { for { name := prefix + randomString() - err := proxy.RegisterInboundConnectionHandlerFactory(name, creator) - if err != proxy.ErrorNameExists { + err := internal.RegisterInboundConnectionHandlerFactory(name, creator) + if err != internal.ErrorNameExists { return name, err } } @@ -27,8 +26,8 @@ func RegisterInboundConnectionHandlerCreator(prefix string, creator internal.Inb func RegisterOutboundConnectionHandlerCreator(prefix string, creator internal.OutboundConnectionHandlerCreator) (string, error) { for { name := prefix + randomString() - err := proxy.RegisterOutboundConnectionHandlerFactory(name, creator) - if err != proxy.ErrorNameExists { + err := internal.RegisterOutboundConnectionHandlerFactory(name, creator) + if err != internal.ErrorNameExists { return name, err } } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index ab07b2cb..91913ebe 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -12,8 +12,8 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/retry" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/vmess" "github.com/v2ray/v2ray-core/proxy/vmess/protocol" "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" @@ -141,7 +141,7 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha } func init() { - if err := proxy.RegisterInboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { + if err := internal.RegisterInboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { config := rawConfig.(Config) allowedClients := user.NewTimedUserSet() diff --git a/proxy/vmess/outbound/json/outbound.go b/proxy/vmess/outbound/json/outbound.go index de76a889..78381c80 100644 --- a/proxy/vmess/outbound/json/outbound.go +++ b/proxy/vmess/outbound/json/outbound.go @@ -6,7 +6,7 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" v2netjson "github.com/v2ray/v2ray-core/common/net/json" - "github.com/v2ray/v2ray-core/proxy" + "github.com/v2ray/v2ray-core/proxy/internal" proxyconfig "github.com/v2ray/v2ray-core/proxy/internal/config" jsonconfig "github.com/v2ray/v2ray-core/proxy/internal/config/json" "github.com/v2ray/v2ray-core/proxy/vmess" @@ -31,12 +31,12 @@ func (t *ConfigTarget) UnmarshalJSON(data []byte) error { } if len(rawConfig.Users) == 0 { log.Error("0 user configured for VMess outbound.") - return proxy.ErrorBadConfiguration + return internal.ErrorBadConfiguration } t.Users = rawConfig.Users if rawConfig.Address == nil { log.Error("Address is not set in VMess outbound config.") - return proxy.ErrorBadConfiguration + return internal.ErrorBadConfiguration } t.Destination = v2net.TCPDestination(rawConfig.Address.Address(), rawConfig.Port) return nil @@ -57,7 +57,7 @@ func (this *Outbound) UnmarshalJSON(data []byte) error { } if len(rawOutbound.TargetList) == 0 { log.Error("0 VMess receiver configured.") - return proxy.ErrorBadConfiguration + return internal.ErrorBadConfiguration } this.TargetList = rawOutbound.TargetList return nil diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 495f8a15..90552652 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -11,8 +11,8 @@ import ( v2crypto "github.com/v2ray/v2ray-core/common/crypto" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/common/connhandler" + "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/vmess/protocol" "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" "github.com/v2ray/v2ray-core/transport/ray" @@ -197,7 +197,7 @@ func (this *VMessOutboundHandlerFactory) Create(space app.Space, rawConfig inter } func init() { - if err := proxy.RegisterOutboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) { + if err := internal.RegisterOutboundConnectionHandlerFactory("vmess", func(space app.Space, rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) { vOutConfig := rawConfig.(Config) return &VMessOutboundHandler{ space: space,