2016-12-16 17:02:11 -05:00
|
|
|
package outbound
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/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
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/proxyman"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/errors"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/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
|
|
|
|
|
2018-12-04 08:17:08 -05:00
|
|
|
var errs []error
|
2018-02-08 11:24:14 -05:00
|
|
|
for _, h := range m.taggedHandler {
|
2018-12-04 08:17:08 -05:00
|
|
|
errs = append(errs, h.Close())
|
2018-02-08 11:24:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range m.untaggedHandlers {
|
2018-12-04 08:17:08 -05:00
|
|
|
errs = append(errs, h.Close())
|
2018-02-08 11:24:14 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 08:17:08 -05:00
|
|
|
return errors.Combine(errs...)
|
2018-02-08 11:24:14 -05:00
|
|
|
}
|
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
|
|
|
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()
|
2021-12-10 22:22:38 -05:00
|
|
|
tag := handler.Tag()
|
2016-12-16 17:02:11 -05:00
|
|
|
|
2021-12-10 22:22:38 -05:00
|
|
|
if m.defaultHandler == nil ||
|
|
|
|
(len(tag) > 0 && tag == m.defaultHandler.Tag()) {
|
2017-04-23 13:16:56 -04:00
|
|
|
m.defaultHandler = handler
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
if len(tag) > 0 {
|
2021-12-10 22:22:38 -05:00
|
|
|
if oldHandler, found := m.taggedHandler[tag]; found {
|
|
|
|
errors.New("will replace the existed outbound with the tag: " + tag).AtWarning().WriteToLog()
|
|
|
|
_ = oldHandler.Close()
|
|
|
|
}
|
2018-01-10 06:22:37 -05:00
|
|
|
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 {
|
2019-06-14 09:47:28 -04:00
|
|
|
if tag == "" {
|
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)
|
2019-07-20 04:54:58 -04:00
|
|
|
if m.defaultHandler != nil && m.defaultHandler.Tag() == tag {
|
2018-02-05 17:38:24 -05:00
|
|
|
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
|
|
|
}
|