1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

refactor proxy registry

This commit is contained in:
Darien Raymond 2016-12-15 15:46:20 +01:00
parent 4c7faadfb9
commit 1ab02e0a6b
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
14 changed files with 38 additions and 49 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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))
}

16
proxy/creator.go Normal file
View File

@ -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)
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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())
}
}

View File

@ -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))
}

View File

@ -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)
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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(),