1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-30 07:46:41 -04:00
v2fly/proxy/internal/handler_cache.go

94 lines
2.5 KiB
Go
Raw Normal View History

2016-01-02 17:08:36 -05:00
package internal
import (
"errors"
"github.com/v2ray/v2ray-core/app"
2016-01-02 17:32:18 -05:00
"github.com/v2ray/v2ray-core/proxy"
2016-06-14 16:54:08 -04:00
"github.com/v2ray/v2ray-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-27 02:53:35 -04:00
ErrProxyNotFound = errors.New("Proxy not found.")
ErrNameExists = errors.New("Proxy with the same name already exists.")
ErrBadConfiguration = errors.New("Bad proxy configuration.")
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-06-27 02:53:35 -04:00
return ErrNameExists
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-06-27 02:53:35 -04:00
return ErrNameExists
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-06-04 08:25:13 -04:00
func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
2016-01-02 17:08:36 -05:00
creator, found := inboundFactories[name]
if !found {
2016-06-27 02:53:35 -04:00
return nil, ErrProxyNotFound
2016-01-02 17:08:36 -05:00
}
2016-06-14 16:54:08 -04:00
if meta.StreamSettings == nil {
meta.StreamSettings = &internet.StreamSettings{
Type: creator.StreamCapability(),
}
} else {
meta.StreamSettings.Type &= creator.StreamCapability()
}
2016-01-02 17:08:36 -05:00
if len(rawConfig) > 0 {
2016-06-10 16:26:39 -04:00
proxyConfig, err := CreateInboundConfig(name, rawConfig)
2016-01-02 17:08:36 -05:00
if err != nil {
return nil, err
}
2016-06-14 16:54:08 -04:00
return creator.Create(space, proxyConfig, meta)
2016-01-02 17:08:36 -05:00
}
2016-06-14 16:54:08 -04:00
return creator.Create(space, nil, meta)
2016-01-02 17:08:36 -05:00
}
2016-06-04 08:25:13 -04:00
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
2016-01-02 17:08:36 -05:00
creator, found := outboundFactories[name]
if !found {
2016-06-27 02:53:35 -04:00
return nil, ErrProxyNotFound
2016-01-02 17:08:36 -05:00
}
2016-06-14 16:54:08 -04:00
if meta.StreamSettings == nil {
meta.StreamSettings = &internet.StreamSettings{
Type: creator.StreamCapability(),
}
} else {
meta.StreamSettings.Type &= creator.StreamCapability()
}
2016-01-02 17:08:36 -05:00
if len(rawConfig) > 0 {
2016-06-10 16:26:39 -04:00
proxyConfig, err := CreateOutboundConfig(name, rawConfig)
2016-01-02 17:08:36 -05:00
if err != nil {
return nil, err
}
2016-06-14 16:54:08 -04:00
return creator.Create(space, proxyConfig, meta)
2016-01-02 17:08:36 -05:00
}
2016-06-14 16:54:08 -04:00
return creator.Create(space, nil, meta)
2016-01-02 17:08:36 -05:00
}