1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-30 15:57:35 -04:00
v2fly/proxy/registry/handler_cache.go

80 lines
2.3 KiB
Go
Raw Normal View History

package registry
2016-01-02 17:08:36 -05:00
import (
2016-10-02 17:43:58 -04:00
"errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/common"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
2016-01-02 17:08:36 -05:00
)
var (
2016-06-14 16:54:08 -04:00
inboundFactories = make(map[string]InboundHandlerFactory)
outboundFactories = make(map[string]OutboundHandlerFactory)
2016-01-02 17:08:36 -05:00
)
2016-06-14 16:54:08 -04:00
func RegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) error {
2016-01-02 17:08:36 -05:00
if _, found := inboundFactories[name]; found {
2016-08-18 02:34:21 -04:00
return common.ErrDuplicatedName
2016-01-02 17:08:36 -05:00
}
inboundFactories[name] = creator
return nil
}
2016-06-14 16:54:08 -04:00
func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) {
if err := RegisterInboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
2016-06-14 16:54:08 -04:00
func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error {
2016-01-02 17:08:36 -05:00
if _, found := outboundFactories[name]; found {
2016-08-18 02:34:21 -04:00
return common.ErrDuplicatedName
2016-01-02 17:08:36 -05:00
}
outboundFactories[name] = creator
return nil
}
2016-06-14 16:54:08 -04:00
func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) {
if err := RegisterOutboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
2016-10-14 16:21:45 -04:00
func CreateInboundHandler(name string, space app.Space, config interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
2016-01-02 17:08:36 -05:00
creator, found := inboundFactories[name]
if !found {
2016-10-17 08:35:13 -04:00
return nil, errors.New("Proxy|Registry: Unknown inbound name: " + name)
2016-01-02 17:08:36 -05:00
}
2016-06-14 16:54:08 -04:00
if meta.StreamSettings == nil {
2016-10-02 17:43:58 -04:00
meta.StreamSettings = &internet.StreamConfig{
Network: creator.StreamCapability().Get(0),
2016-06-14 16:54:08 -04:00
}
} else {
2016-10-02 17:43:58 -04:00
if !creator.StreamCapability().HasNetwork(meta.StreamSettings.Network) {
return nil, errors.New("Proxy|Registry: Invalid network: " + meta.StreamSettings.Network.String())
}
2016-06-14 16:54:08 -04:00
}
2016-10-14 16:21:45 -04:00
return creator.Create(space, config, meta)
2016-01-02 17:08:36 -05:00
}
2016-10-14 16:21:45 -04:00
func CreateOutboundHandler(name string, space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
2016-01-02 17:08:36 -05:00
creator, found := outboundFactories[name]
if !found {
2016-10-17 08:35:13 -04:00
return nil, errors.New("Proxy|Registry: Unknown outbound name: " + name)
2016-01-02 17:08:36 -05:00
}
2016-06-14 16:54:08 -04:00
if meta.StreamSettings == nil {
2016-10-02 17:43:58 -04:00
meta.StreamSettings = &internet.StreamConfig{
Network: creator.StreamCapability().Get(0),
2016-06-14 16:54:08 -04:00
}
} else {
2016-10-02 17:43:58 -04:00
if !creator.StreamCapability().HasNetwork(meta.StreamSettings.Network) {
return nil, errors.New("Proxy|Registry: Invalid network: " + meta.StreamSettings.Network.String())
}
2016-06-14 16:54:08 -04:00
}
2016-01-02 17:08:36 -05:00
2016-10-14 16:21:45 -04:00
return creator.Create(space, config, meta)
2016-01-02 17:08:36 -05:00
}