2016-12-16 17:02:11 -05:00
|
|
|
package outbound
|
|
|
|
|
2018-09-30 17:08:41 -04:00
|
|
|
//go:generate errorgen
|
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"
|
2018-11-07 15:08:20 -05:00
|
|
|
"strings"
|
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"
|
2018-10-11 14:43:37 -04:00
|
|
|
"v2ray.com/core/features/outbound"
|
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-10-11 14:43:37 -04:00
|
|
|
defaultHandler outbound.Handler
|
|
|
|
taggedHandler map[string]outbound.Handler
|
|
|
|
untaggedHandlers []outbound.Handler
|
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{
|
2018-10-11 14:43:37 -04:00
|
|
|
taggedHandler: make(map[string]outbound.Handler),
|
2018-01-10 06:22:37 -05:00
|
|
|
}
|
|
|
|
return m, nil
|
2016-12-16 17:02:11 -05:00
|
|
|
}
|
|
|
|
|
2018-10-22 02:42:10 -04:00
|
|
|
// Type implements common.HasType.
|
2018-10-12 17:57:56 -04:00
|
|
|
func (m *Manager) Type() interface{} {
|
|
|
|
return outbound.ManagerType()
|
|
|
|
}
|
|
|
|
|
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-10-12 17:57:56 -04:00
|
|
|
// GetDefaultHandler implements outbound.Manager.
|
2018-10-11 14:43:37 -04:00
|
|
|
func (m *Manager) GetDefaultHandler() outbound.Handler {
|
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-10-12 17:57:56 -04:00
|
|
|
// GetHandler implements outbound.Manager.
|
2018-10-11 14:43:37 -04:00
|
|
|
func (m *Manager) GetHandler(tag string) outbound.Handler {
|
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-10-12 17:57:56 -04:00
|
|
|
// AddHandler implements outbound.Manager.
|
2018-10-11 14:43:37 -04:00
|
|
|
func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) 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-10-12 17:57:56 -04:00
|
|
|
// RemoveHandler implements outbound.Manager.
|
2018-02-05 17:38:24 -05:00
|
|
|
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
|
|
|
if len(tag) == 0 {
|
2018-10-11 14:43:37 -04:00
|
|
|
return common.ErrNoClue
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-11-13 17:19:58 -05:00
|
|
|
// Select implements outbound.HandlerSelector.
|
2018-11-07 15:08:20 -05:00
|
|
|
func (m *Manager) Select(selectors []string) []string {
|
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
|
|
|
tags := make([]string, 0, len(selectors))
|
|
|
|
|
|
|
|
for tag := range m.taggedHandler {
|
|
|
|
match := false
|
|
|
|
for _, selector := range selectors {
|
|
|
|
if strings.HasPrefix(tag, selector) {
|
|
|
|
match = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if match {
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|