leverage global object creator in proxies.

This commit is contained in:
Darien Raymond
2017-01-13 00:56:21 +01:00
parent db1c9131f0
commit 148e4832eb
23 changed files with 353 additions and 269 deletions

View File

@@ -1,45 +1,34 @@
package proxy
import (
"v2ray.com/core/app"
"context"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
)
var (
inboundFactories = make(map[string]InboundHandlerFactory)
outboundFactories = make(map[string]OutboundHandlerFactory)
)
func RegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) error {
if _, found := inboundFactories[name]; found {
return common.ErrDuplicatedName
func CreateInboundHandler(ctx context.Context, config interface{}) (InboundHandler, error) {
handler, err := common.CreateObject(ctx, config)
if err != nil {
return nil, err
}
switch h := handler.(type) {
case InboundHandler:
return h, nil
default:
return nil, errors.New("Proxy: Not a InboundHandler.")
}
inboundFactories[name] = creator
return nil
}
func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error {
if _, found := outboundFactories[name]; found {
return common.ErrDuplicatedName
func CreateOutboundHandler(ctx context.Context, config interface{}) (OutboundHandler, error) {
handler, err := common.CreateObject(ctx, config)
if err != nil {
return nil, err
}
outboundFactories[name] = creator
return nil
}
func CreateInboundHandler(name string, space app.Space, config interface{}, meta *InboundHandlerMeta) (InboundHandler, error) {
creator, found := inboundFactories[name]
if !found {
return nil, errors.New("Proxy: Unknown inbound name: " + name)
switch h := handler.(type) {
case OutboundHandler:
return h, nil
default:
return nil, errors.New("Proxy: Not a OutboundHandler.")
}
return creator.Create(space, config, meta)
}
func CreateOutboundHandler(name string, space app.Space, config interface{}, meta *OutboundHandlerMeta) (OutboundHandler, error) {
creator, found := outboundFactories[name]
if !found {
return nil, errors.New("Proxy: Unknown outbound name: " + name)
}
return creator.Create(space, config, meta)
}