2020-11-25 13:01:53 +02:00
|
|
|
package outbound
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
2020-11-25 13:01:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-10 18:26:27 +02:00
|
|
|
"sort"
|
2020-11-25 13:01:53 +02:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/app/proxyman"
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
|
"github.com/xtls/xray-core/core"
|
|
|
|
"github.com/xtls/xray-core/features/outbound"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Manager is to manage all outbound handlers.
|
|
|
|
type Manager struct {
|
|
|
|
access sync.RWMutex
|
|
|
|
defaultHandler outbound.Handler
|
|
|
|
taggedHandler map[string]outbound.Handler
|
|
|
|
untaggedHandlers []outbound.Handler
|
|
|
|
running bool
|
2024-01-16 17:52:01 +02:00
|
|
|
tagsCache *sync.Map
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Manager.
|
|
|
|
func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error) {
|
|
|
|
m := &Manager{
|
|
|
|
taggedHandler: make(map[string]outbound.Handler),
|
2024-01-16 17:52:01 +02:00
|
|
|
tagsCache: &sync.Map{},
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type implements common.HasType.
|
|
|
|
func (m *Manager) Type() interface{} {
|
|
|
|
return outbound.ManagerType()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements core.Feature
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements core.Feature
|
|
|
|
func (m *Manager) Close() error {
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
|
|
|
m.running = false
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
for _, h := range m.taggedHandler {
|
|
|
|
errs = append(errs, h.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range m.untaggedHandlers {
|
|
|
|
errs = append(errs, h.Close())
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Combine(errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultHandler implements outbound.Manager.
|
|
|
|
func (m *Manager) GetDefaultHandler() outbound.Handler {
|
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
|
|
|
if m.defaultHandler == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return m.defaultHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandler implements outbound.Manager.
|
|
|
|
func (m *Manager) GetHandler(tag string) outbound.Handler {
|
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
if handler, found := m.taggedHandler[tag]; found {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddHandler implements outbound.Manager.
|
|
|
|
func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) error {
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
2024-01-16 17:52:01 +02:00
|
|
|
m.tagsCache = &sync.Map{}
|
2024-01-12 17:36:48 +02:00
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
if m.defaultHandler == nil {
|
|
|
|
m.defaultHandler = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
tag := handler.Tag()
|
|
|
|
if len(tag) > 0 {
|
2021-02-18 11:53:10 +02:00
|
|
|
if _, found := m.taggedHandler[tag]; found {
|
2024-06-29 21:32:57 +03:00
|
|
|
return errors.New("existing tag found: " + tag)
|
2021-02-18 11:53:10 +02:00
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
m.taggedHandler[tag] = handler
|
|
|
|
} else {
|
|
|
|
m.untaggedHandlers = append(m.untaggedHandlers, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.running {
|
|
|
|
return handler.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveHandler implements outbound.Manager.
|
|
|
|
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
|
|
|
if tag == "" {
|
|
|
|
return common.ErrNoClue
|
|
|
|
}
|
|
|
|
m.access.Lock()
|
|
|
|
defer m.access.Unlock()
|
|
|
|
|
2024-01-16 17:52:01 +02:00
|
|
|
m.tagsCache = &sync.Map{}
|
2024-01-12 17:36:48 +02:00
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
delete(m.taggedHandler, tag)
|
|
|
|
if m.defaultHandler != nil && m.defaultHandler.Tag() == tag {
|
|
|
|
m.defaultHandler = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select implements outbound.HandlerSelector.
|
|
|
|
func (m *Manager) Select(selectors []string) []string {
|
|
|
|
|
2024-01-12 17:36:48 +02:00
|
|
|
key := strings.Join(selectors, ",")
|
2024-01-16 17:52:01 +02:00
|
|
|
if cache, ok := m.tagsCache.Load(key); ok {
|
|
|
|
return cache.([]string)
|
2024-01-12 17:36:48 +02:00
|
|
|
}
|
|
|
|
|
2024-01-16 17:52:01 +02:00
|
|
|
m.access.RLock()
|
|
|
|
defer m.access.RUnlock()
|
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
tags := make([]string, 0, len(selectors))
|
|
|
|
|
|
|
|
for tag := range m.taggedHandler {
|
|
|
|
for _, selector := range selectors {
|
|
|
|
if strings.HasPrefix(tag, selector) {
|
2024-01-10 18:26:27 +02:00
|
|
|
tags = append(tags, tag)
|
2020-11-25 13:01:53 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 17:36:48 +02:00
|
|
|
|
2024-01-10 18:26:27 +02:00
|
|
|
sort.Strings(tags)
|
2024-01-16 17:52:01 +02:00
|
|
|
m.tagsCache.Store(key, tags)
|
2024-01-12 17:36:48 +02:00
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*proxyman.OutboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return New(ctx, config.(*proxyman.OutboundConfig))
|
|
|
|
}))
|
|
|
|
common.Must(common.RegisterConfig((*core.OutboundHandlerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewHandler(ctx, config.(*core.OutboundHandlerConfig))
|
|
|
|
}))
|
|
|
|
}
|