2016-12-16 22:02:11 +00:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type DefaultOutboundHandlerManager struct {
|
|
|
|
sync.RWMutex
|
2017-01-26 19:46:44 +00:00
|
|
|
defaultHandler *Handler
|
|
|
|
taggedHandler map[string]*Handler
|
2016-12-16 22:02:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 12:41:40 +00:00
|
|
|
func New(ctx context.Context, config *proxyman.OutboundConfig) (*DefaultOutboundHandlerManager, error) {
|
2016-12-16 22:02:11 +00:00
|
|
|
return &DefaultOutboundHandlerManager{
|
2017-01-26 19:46:44 +00:00
|
|
|
taggedHandler: make(map[string]*Handler),
|
2017-01-13 12:41:40 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (DefaultOutboundHandlerManager) Interface() interface{} {
|
|
|
|
return (*proxyman.OutboundHandlerManager)(nil)
|
2016-12-16 22:02:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 20:35:40 +00:00
|
|
|
func (DefaultOutboundHandlerManager) Start() error { return nil }
|
|
|
|
|
|
|
|
func (DefaultOutboundHandlerManager) Close() {}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *DefaultOutboundHandlerManager) GetDefaultHandler() proxyman.OutboundHandler {
|
2016-12-16 22:02:11 +00:00
|
|
|
v.RLock()
|
|
|
|
defer v.RUnlock()
|
|
|
|
if v.defaultHandler == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return v.defaultHandler
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *DefaultOutboundHandlerManager) GetHandler(tag string) proxyman.OutboundHandler {
|
2016-12-16 22:02:11 +00:00
|
|
|
v.RLock()
|
|
|
|
defer v.RUnlock()
|
|
|
|
if handler, found := v.taggedHandler[tag]; found {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (v *DefaultOutboundHandlerManager) AddHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) error {
|
2016-12-16 22:02:11 +00:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
handler, err := NewHandler(ctx, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if v.defaultHandler == nil {
|
|
|
|
v.defaultHandler = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Tag) > 0 {
|
|
|
|
v.taggedHandler[config.Tag] = handler
|
|
|
|
}
|
|
|
|
|
2016-12-16 22:02:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}))
|
2016-12-16 22:02:11 +00:00
|
|
|
}
|