1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 00:07:09 -05:00
v2fly/app/proxyman/outbound/outbound.go

176 lines
3.8 KiB
Go
Raw Normal View History

2016-12-16 17:02:11 -05:00
package outbound
//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
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
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) {
m := &Manager{
taggedHandler: make(map[string]outbound.Handler),
}
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
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
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
for _, h := range m.taggedHandler {
2018-12-04 08:17:08 -05:00
errs = append(errs, h.Close())
}
for _, h := range m.untaggedHandlers {
2018-12-04 08:17:08 -05:00
errs = append(errs, h.Close())
}
2018-12-04 08:17:08 -05:00
return errors.Combine(errs...)
}
2017-02-01 15:35:40 -05:00
2018-10-12 17:57:56 -04:00
// GetDefaultHandler implements outbound.Manager.
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.
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.
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()
tag := handler.Tag()
2016-12-16 17:02:11 -05:00
if m.defaultHandler == nil ||
(len(tag) > 0 && tag == m.defaultHandler.Tag()) {
2017-04-23 13:16:56 -04:00
m.defaultHandler = handler
}
if len(tag) > 0 {
if oldHandler, found := m.taggedHandler[tag]; found {
errors.New("will replace the existed outbound with the tag: " + tag).AtWarning().WriteToLog()
_ = oldHandler.Close()
}
m.taggedHandler[tag] = handler
} else {
m.untaggedHandlers = append(m.untaggedHandlers, handler)
}
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 tag == "" {
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 != 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))
}))
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
}