2016-12-16 17:02:11 -05:00
|
|
|
package outbound
|
|
|
|
|
2017-12-02 19:04:57 -05:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg outbound -path App,Proxyman,Outbound
|
2017-04-08 19:43:25 -04:00
|
|
|
|
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
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
"v2ray.com/core"
|
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 {
|
2018-02-07 06:34:15 -05:00
|
|
|
access sync.RWMutex
|
2018-02-06 07:47:50 -05:00
|
|
|
defaultHandler core.OutboundHandler
|
|
|
|
taggedHandler map[string]core.OutboundHandler
|
|
|
|
untaggedHandlers []core.OutboundHandler
|
2018-02-07 06:34:15 -05:00
|
|
|
running bool
|
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) {
|
2018-01-10 06:22:37 -05:00
|
|
|
m := &Manager{
|
|
|
|
taggedHandler: make(map[string]core.OutboundHandler),
|
|
|
|
}
|
2018-02-21 11:05:29 -05:00
|
|
|
v := core.MustFromContext(ctx)
|
2018-01-10 06:22:37 -05:00
|
|
|
if err := v.RegisterFeature((*core.OutboundHandlerManager)(nil), m); err != nil {
|
|
|
|
return nil, newError("unable to register OutboundHandlerManager").Base(err)
|
|
|
|
}
|
|
|
|
return m, nil
|
2016-12-16 17:02:11 -05:00
|
|
|
}
|
|
|
|
|
2018-02-07 06:34:15 -05:00
|
|
|
// Start implements core.Feature
|
2018-02-08 11:24:14 -05:00
|
|
|
func (m *Manager) Start() error {
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
|
|
|
m.running = true
|
|
|
|
|
|
|
|
for _, h := range m.taggedHandler {
|
|
|
|
if err := h.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range m.untaggedHandlers {
|
|
|
|
if err := h.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-01 15:35:40 -05:00
|
|
|
|
2018-02-07 06:34:15 -05:00
|
|
|
// Close implements core.Feature
|
2018-02-08 11:24:14 -05:00
|
|
|
func (m *Manager) Close() error {
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
|
|
|
m.running = false
|
|
|
|
|
|
|
|
for _, h := range m.taggedHandler {
|
|
|
|
h.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range m.untaggedHandlers {
|
|
|
|
h.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-01 15:35:40 -05:00
|
|
|
|
2018-02-07 06:38:12 -05:00
|
|
|
// GetDefaultHandler implements core.OutboundHandlerManager.
|
2018-01-10 06:22:37 -05:00
|
|
|
func (m *Manager) GetDefaultHandler() core.OutboundHandler {
|
2018-02-07 06:34:15 -05:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
2017-04-23 13:16:56 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-07 06:38:12 -05:00
|
|
|
// GetHandler implements core.OutboundHandlerManager.
|
2018-01-10 06:22:37 -05:00
|
|
|
func (m *Manager) GetHandler(tag string) core.OutboundHandler {
|
2018-02-07 06:34:15 -05:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
2017-04-23 13:16:56 -04:00
|
|
|
if handler, found := m.taggedHandler[tag]; found {
|
2016-12-16 17:02:11 -05:00
|
|
|
return handler
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:38:12 -05:00
|
|
|
// AddHandler implements core.OutboundHandlerManager.
|
2018-01-10 06:22:37 -05:00
|
|
|
func (m *Manager) AddHandler(ctx context.Context, handler core.OutboundHandler) error {
|
2018-02-07 06:34:15 -05:00
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
2016-12-16 17:02:11 -05:00
|
|
|
|
2017-04-23 13:16:56 -04:00
|
|
|
if m.defaultHandler == nil {
|
|
|
|
m.defaultHandler = handler
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
tag := handler.Tag()
|
|
|
|
if len(tag) > 0 {
|
|
|
|
m.taggedHandler[tag] = handler
|
2018-02-06 07:47:50 -05:00
|
|
|
} else {
|
|
|
|
m.untaggedHandlers = append(m.untaggedHandlers, handler)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 11:24:14 -05:00
|
|
|
if m.running {
|
|
|
|
return handler.Start()
|
|
|
|
}
|
|
|
|
|
2016-12-16 17:02:11 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 06:38:12 -05:00
|
|
|
// RemoveHandler implements core.OutboundHandlerManager.
|
2018-02-05 17:38:24 -05:00
|
|
|
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
|
|
|
if len(tag) == 0 {
|
|
|
|
return core.ErrNoClue
|
|
|
|
}
|
2018-02-07 06:34:15 -05:00
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
2018-02-05 17:38:24 -05:00
|
|
|
|
|
|
|
delete(m.taggedHandler, tag)
|
|
|
|
if m.defaultHandler.Tag() == tag {
|
|
|
|
m.defaultHandler = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-16 17:02:11 -05:00
|
|
|
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))
|
|
|
|
}))
|
2018-01-10 06:22:37 -05:00
|
|
|
common.Must(common.RegisterConfig((*core.OutboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewHandler(ctx, config.(*core.OutboundHandlerConfig))
|
|
|
|
}))
|
2016-12-16 17:02:11 -05:00
|
|
|
}
|