2017-01-26 14:46:44 -05:00
|
|
|
package inbound
|
|
|
|
|
2017-04-08 19:43:25 -04:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg inbound -path App,Proxyman,Inbound
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"v2ray.com/core/app/proxyman"
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
)
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
// Manager is to manage all inbound handlers.
|
|
|
|
type Manager struct {
|
2017-01-26 14:46:44 -05:00
|
|
|
handlers []proxyman.InboundHandler
|
|
|
|
taggedHandlers map[string]proxyman.InboundHandler
|
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
|
|
|
|
return &Manager{
|
2017-01-26 14:46:44 -05:00
|
|
|
taggedHandlers: make(map[string]proxyman.InboundHandler),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
func (m *Manager) AddHandler(ctx context.Context, config *proxyman.InboundHandlerConfig) error {
|
2017-01-26 14:46:44 -05:00
|
|
|
rawReceiverSettings, err := config.ReceiverSettings.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
|
|
|
|
if !ok {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("not a ReceiverConfig")
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
proxySettings, err := config.ProxySettings.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var handler proxyman.InboundHandler
|
|
|
|
tag := config.Tag
|
|
|
|
allocStrategy := receiverSettings.AllocationStrategy
|
|
|
|
if allocStrategy == nil || allocStrategy.Type == proxyman.AllocationStrategy_Always {
|
|
|
|
h, err := NewAlwaysOnInboundHandler(ctx, tag, receiverSettings, proxySettings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
handler = h
|
|
|
|
} else if allocStrategy.Type == proxyman.AllocationStrategy_Random {
|
|
|
|
h, err := NewDynamicInboundHandler(ctx, tag, receiverSettings, proxySettings)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
handler = h
|
|
|
|
}
|
|
|
|
|
|
|
|
if handler == nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m.handlers = append(m.handlers, handler)
|
|
|
|
if len(tag) > 0 {
|
|
|
|
m.taggedHandlers[tag] = handler
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
func (m *Manager) GetHandler(ctx context.Context, tag string) (proxyman.InboundHandler, error) {
|
2017-01-26 14:46:44 -05:00
|
|
|
handler, found := m.taggedHandlers[tag]
|
|
|
|
if !found {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("handler not found: ", tag)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
func (m *Manager) Start() error {
|
2017-01-26 14:46:44 -05:00
|
|
|
for _, handler := range m.handlers {
|
|
|
|
if err := handler.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
func (m *Manager) Close() {
|
2017-01-26 14:46:44 -05:00
|
|
|
for _, handler := range m.handlers {
|
|
|
|
handler.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
func (m *Manager) Interface() interface{} {
|
2017-01-26 14:46:44 -05:00
|
|
|
return (*proxyman.InboundHandlerManager)(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*proxyman.InboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*proxyman.InboundConfig))
|
|
|
|
}))
|
|
|
|
}
|