mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.3 KiB
78 lines
2.3 KiB
8 years ago
|
package proxy
|
||
9 years ago
|
|
||
|
import (
|
||
8 years ago
|
"v2ray.com/core/app"
|
||
|
"v2ray.com/core/common"
|
||
8 years ago
|
"v2ray.com/core/common/errors"
|
||
8 years ago
|
"v2ray.com/core/transport/internet"
|
||
9 years ago
|
)
|
||
|
|
||
|
var (
|
||
9 years ago
|
inboundFactories = make(map[string]InboundHandlerFactory)
|
||
|
outboundFactories = make(map[string]OutboundHandlerFactory)
|
||
9 years ago
|
)
|
||
|
|
||
9 years ago
|
func RegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) error {
|
||
9 years ago
|
if _, found := inboundFactories[name]; found {
|
||
8 years ago
|
return common.ErrDuplicatedName
|
||
9 years ago
|
}
|
||
|
inboundFactories[name] = creator
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) {
|
||
9 years ago
|
if err := RegisterInboundHandlerCreator(name, creator); err != nil {
|
||
9 years ago
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error {
|
||
9 years ago
|
if _, found := outboundFactories[name]; found {
|
||
8 years ago
|
return common.ErrDuplicatedName
|
||
9 years ago
|
}
|
||
|
outboundFactories[name] = creator
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) {
|
||
9 years ago
|
if err := RegisterOutboundHandlerCreator(name, creator); err != nil {
|
||
9 years ago
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func CreateInboundHandler(name string, space app.Space, config interface{}, meta *InboundHandlerMeta) (InboundHandler, error) {
|
||
9 years ago
|
creator, found := inboundFactories[name]
|
||
|
if !found {
|
||
8 years ago
|
return nil, errors.New("Proxy: Unknown inbound name: " + name)
|
||
9 years ago
|
}
|
||
9 years ago
|
if meta.StreamSettings == nil {
|
||
8 years ago
|
meta.StreamSettings = &internet.StreamConfig{
|
||
|
Network: creator.StreamCapability().Get(0),
|
||
9 years ago
|
}
|
||
|
} else {
|
||
8 years ago
|
if !creator.StreamCapability().HasNetwork(meta.StreamSettings.Network) {
|
||
8 years ago
|
return nil, errors.New("Proxy: Invalid network: " + meta.StreamSettings.Network.String())
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
return creator.Create(space, config, meta)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func CreateOutboundHandler(name string, space app.Space, config interface{}, meta *OutboundHandlerMeta) (OutboundHandler, error) {
|
||
9 years ago
|
creator, found := outboundFactories[name]
|
||
|
if !found {
|
||
8 years ago
|
return nil, errors.New("Proxy: Unknown outbound name: " + name)
|
||
9 years ago
|
}
|
||
9 years ago
|
if meta.StreamSettings == nil {
|
||
8 years ago
|
meta.StreamSettings = &internet.StreamConfig{
|
||
|
Network: creator.StreamCapability().Get(0),
|
||
9 years ago
|
}
|
||
|
} else {
|
||
8 years ago
|
if !creator.StreamCapability().HasNetwork(meta.StreamSettings.Network) {
|
||
8 years ago
|
return nil, errors.New("Proxy: Invalid network: " + meta.StreamSettings.Network.String())
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
return creator.Create(space, config, meta)
|
||
9 years ago
|
}
|