2016-12-16 22:02:11 +00:00
|
|
|
package outbound
|
|
|
|
|
2017-12-03 00:04:57 +00:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg outbound -path App,Proxyman,Outbound
|
2017-04-08 23:43:25 +00:00
|
|
|
|
2016-12-16 22:02:11 +00:00
|
|
|
import (
|
2017-01-13 12:41:40 +00:00
|
|
|
"context"
|
2017-01-14 23:48:37 +00:00
|
|
|
"sync"
|
2017-01-13 12:41:40 +00:00
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
"v2ray.com/core"
|
2016-12-16 22:02:11 +00:00
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-01-06 14:32:36 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-16 22:02:11 +00:00
|
|
|
)
|
|
|
|
|
2017-04-09 12:49:40 +00:00
|
|
|
// Manager is to manage all outbound handlers.
|
|
|
|
type Manager struct {
|
2016-12-16 22:02:11 +00:00
|
|
|
sync.RWMutex
|
2018-01-10 11:22:37 +00:00
|
|
|
defaultHandler core.OutboundHandler
|
|
|
|
taggedHandler map[string]core.OutboundHandler
|
2016-12-16 22:02:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 12:49:40 +00:00
|
|
|
// New creates a new Manager.
|
|
|
|
func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error) {
|
2018-01-10 11:22:37 +00:00
|
|
|
m := &Manager{
|
|
|
|
taggedHandler: make(map[string]core.OutboundHandler),
|
|
|
|
}
|
|
|
|
v := core.FromContext(ctx)
|
|
|
|
if v == nil {
|
|
|
|
return nil, newError("V is not in context")
|
|
|
|
}
|
|
|
|
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 22:02:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 12:49:40 +00:00
|
|
|
// Start implements Application.Start
|
|
|
|
func (*Manager) Start() error { return nil }
|
2017-02-01 20:35:40 +00:00
|
|
|
|
2017-04-09 12:49:40 +00:00
|
|
|
// Close implements Application.Close
|
|
|
|
func (*Manager) Close() {}
|
2017-02-01 20:35:40 +00:00
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
func (m *Manager) GetDefaultHandler() core.OutboundHandler {
|
2017-04-23 17:16:56 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
if m.defaultHandler == nil {
|
2016-12-16 22:02:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-23 17:16:56 +00:00
|
|
|
return m.defaultHandler
|
2016-12-16 22:02:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
func (m *Manager) GetHandler(tag string) core.OutboundHandler {
|
2017-04-23 17:16:56 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
if handler, found := m.taggedHandler[tag]; found {
|
2016-12-16 22:02:11 +00:00
|
|
|
return handler
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
func (m *Manager) AddHandler(ctx context.Context, handler core.OutboundHandler) error {
|
2017-04-23 17:16:56 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2016-12-16 22:02:11 +00:00
|
|
|
|
2017-04-23 17:16:56 +00:00
|
|
|
if m.defaultHandler == nil {
|
|
|
|
m.defaultHandler = handler
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
tag := handler.Tag()
|
|
|
|
if len(tag) > 0 {
|
|
|
|
m.taggedHandler[tag] = handler
|
2017-01-26 19:46:44 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 22:02:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:38:24 +00:00
|
|
|
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
|
|
|
if len(tag) == 0 {
|
|
|
|
return core.ErrNoClue
|
|
|
|
}
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
delete(m.taggedHandler, tag)
|
|
|
|
if m.defaultHandler.Tag() == tag {
|
|
|
|
m.defaultHandler = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-16 22:02:11 +00:00
|
|
|
func init() {
|
2017-01-13 12:41:40 +00: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 11:22:37 +00: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 22:02:11 +00:00
|
|
|
}
|