1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-03 20:45:23 +00:00
v2fly/proxy/internal/handler_cache.go

80 lines
2.2 KiB
Go
Raw Normal View History

2016-01-02 22:08:36 +00:00
package internal
import (
"errors"
"github.com/v2ray/v2ray-core/app"
2016-06-03 22:38:22 +00:00
v2net "github.com/v2ray/v2ray-core/common/net"
2016-01-02 22:32:18 +00:00
"github.com/v2ray/v2ray-core/proxy"
2016-01-02 22:08:36 +00:00
"github.com/v2ray/v2ray-core/proxy/internal/config"
)
var (
2016-01-25 16:27:15 +00:00
inboundFactories = make(map[string]InboundHandlerCreator)
outboundFactories = make(map[string]OutboundHandlerCreator)
2016-01-02 22:08:36 +00:00
ErrorProxyNotFound = errors.New("Proxy not found.")
ErrorNameExists = errors.New("Proxy with the same name already exists.")
ErrorBadConfiguration = errors.New("Bad proxy configuration.")
)
2016-01-25 16:27:15 +00:00
func RegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) error {
2016-01-02 22:08:36 +00:00
if _, found := inboundFactories[name]; found {
return ErrorNameExists
}
inboundFactories[name] = creator
return nil
}
2016-01-25 16:29:26 +00:00
func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) {
if err := RegisterInboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
2016-01-25 16:27:15 +00:00
func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) error {
2016-01-02 22:08:36 +00:00
if _, found := outboundFactories[name]; found {
return ErrorNameExists
}
outboundFactories[name] = creator
return nil
}
2016-01-25 16:29:26 +00:00
func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) {
if err := RegisterOutboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
2016-06-03 22:38:22 +00:00
func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
2016-01-02 22:08:36 +00:00
creator, found := inboundFactories[name]
if !found {
return nil, ErrorProxyNotFound
}
if len(rawConfig) > 0 {
2016-01-25 16:23:10 +00:00
proxyConfig, err := config.CreateInboundConfig(name, rawConfig)
2016-01-02 22:08:36 +00:00
if err != nil {
return nil, err
}
2016-06-03 22:38:22 +00:00
return creator(space, proxyConfig, listen, port)
2016-01-02 22:08:36 +00:00
}
2016-06-03 22:38:22 +00:00
return creator(space, nil, listen, port)
2016-01-02 22:08:36 +00:00
}
2016-06-03 22:38:22 +00:00
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
2016-01-02 22:08:36 +00:00
creator, found := outboundFactories[name]
if !found {
return nil, ErrorNameExists
}
if len(rawConfig) > 0 {
2016-01-25 16:23:10 +00:00
proxyConfig, err := config.CreateOutboundConfig(name, rawConfig)
2016-01-02 22:08:36 +00:00
if err != nil {
return nil, err
}
2016-06-03 22:38:22 +00:00
return creator(space, proxyConfig, sendThrough)
2016-01-02 22:08:36 +00:00
}
2016-06-03 22:38:22 +00:00
return creator(space, nil, sendThrough)
2016-01-02 22:08:36 +00:00
}