2016-12-16 17:02:11 -05:00
|
|
|
package outbound
|
|
|
|
|
2017-04-08 19:43:25 -04:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg outbound -path App,Proxyman,Outbound
|
|
|
|
|
2016-12-16 17:02:11 -05:00
|
|
|
import (
|
2017-01-13 07:41:40 -05:00
|
|
|
"context"
|
2017-01-14 18:48:37 -05:00
|
|
|
"sync"
|
2017-01-13 07:41:40 -05:00
|
|
|
|
2016-12-16 17:02:11 -05:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-01-06 09:32:36 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-16 17:02:11 -05:00
|
|
|
)
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
// Manager is to manage all outbound handlers.
|
|
|
|
type Manager struct {
|
2016-12-16 17:02:11 -05:00
|
|
|
sync.RWMutex
|
2017-01-26 14:46:44 -05:00
|
|
|
defaultHandler *Handler
|
|
|
|
taggedHandler map[string]*Handler
|
2016-12-16 17:02:11 -05:00
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
// New creates a new Manager.
|
|
|
|
func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error) {
|
|
|
|
return &Manager{
|
2017-01-26 14:46:44 -05:00
|
|
|
taggedHandler: make(map[string]*Handler),
|
2017-01-13 07:41:40 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
// Interface implements Application.Interface.
|
|
|
|
func (*Manager) Interface() interface{} {
|
2017-01-13 07:41:40 -05:00
|
|
|
return (*proxyman.OutboundHandlerManager)(nil)
|
2016-12-16 17:02:11 -05:00
|
|
|
}
|
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
// Start implements Application.Start
|
|
|
|
func (*Manager) Start() error { return nil }
|
2017-02-01 15:35:40 -05:00
|
|
|
|
2017-04-09 08:49:40 -04:00
|
|
|
// Close implements Application.Close
|
|
|
|
func (*Manager) Close() {}
|
2017-02-01 15:35:40 -05:00
|
|
|
|
2017-04-23 13:16:56 -04:00
|
|
|
func (m *Manager) GetDefaultHandler() proxyman.OutboundHandler {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
if m.defaultHandler == nil {
|
2016-12-16 17:02:11 -05:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-23 13:16:56 -04:00
|
|
|
return m.defaultHandler
|
2016-12-16 17:02:11 -05:00
|
|
|
}
|
|
|
|
|
2017-04-23 13:16:56 -04:00
|
|
|
func (m *Manager) GetHandler(tag string) proxyman.OutboundHandler {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
if handler, found := m.taggedHandler[tag]; found {
|
2016-12-16 17:02:11 -05:00
|
|
|
return handler
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-23 13:16:56 -04:00
|
|
|
func (m *Manager) AddHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) error {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2016-12-16 17:02:11 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
handler, err := NewHandler(ctx, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-23 13:16:56 -04:00
|
|
|
if m.defaultHandler == nil {
|
|
|
|
m.defaultHandler = handler
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Tag) > 0 {
|
2017-04-23 13:16:56 -04:00
|
|
|
m.taggedHandler[config.Tag] = handler
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2016-12-16 17:02:11 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-01-13 07:41:40 -05:00
|
|
|
common.Must(common.RegisterConfig((*proxyman.OutboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*proxyman.OutboundConfig))
|
|
|
|
}))
|
2016-12-16 17:02:11 -05:00
|
|
|
}
|