2016-01-31 11:01:28 -05:00
|
|
|
package proxyman
|
|
|
|
|
|
|
|
import (
|
2016-05-18 02:05:52 -04:00
|
|
|
"sync"
|
|
|
|
|
2016-01-31 11:01:28 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-05-18 02:05:52 -04:00
|
|
|
APP_ID_INBOUND_MANAGER = app.ID(4)
|
|
|
|
APP_ID_OUTBOUND_MANAGER = app.ID(6)
|
2016-01-31 11:01:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type InboundHandlerManager interface {
|
|
|
|
GetHandler(tag string) (proxy.InboundHandler, int)
|
|
|
|
}
|
|
|
|
|
2016-05-18 02:05:52 -04:00
|
|
|
type OutboundHandlerManager interface {
|
|
|
|
GetHandler(tag string) proxy.OutboundHandler
|
|
|
|
GetDefaultHandler() proxy.OutboundHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultOutboundHandlerManager struct {
|
|
|
|
sync.RWMutex
|
|
|
|
defaultHandler proxy.OutboundHandler
|
|
|
|
taggedHandler map[string]proxy.OutboundHandler
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-18 11:12:04 -04:00
|
|
|
func NewDefaultOutboundHandlerManager() *DefaultOutboundHandlerManager {
|
|
|
|
return &DefaultOutboundHandlerManager{
|
|
|
|
taggedHandler: make(map[string]proxy.OutboundHandler),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *DefaultOutboundHandlerManager) Release() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-18 02:05:52 -04:00
|
|
|
func (this *DefaultOutboundHandlerManager) GetDefaultHandler() proxy.OutboundHandler {
|
|
|
|
this.RLock()
|
|
|
|
defer this.RUnlock()
|
|
|
|
if this.defaultHandler == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return this.defaultHandler
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-18 02:05:52 -04:00
|
|
|
func (this *DefaultOutboundHandlerManager) SetDefaultHandler(handler proxy.OutboundHandler) {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
this.defaultHandler = handler
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-18 02:05:52 -04:00
|
|
|
func (this *DefaultOutboundHandlerManager) GetHandler(tag string) proxy.OutboundHandler {
|
|
|
|
this.RLock()
|
|
|
|
defer this.RUnlock()
|
|
|
|
if handler, found := this.taggedHandler[tag]; found {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *DefaultOutboundHandlerManager) SetHandler(tag string, handler proxy.OutboundHandler) {
|
|
|
|
this.Lock()
|
|
|
|
defer this.Unlock()
|
|
|
|
|
|
|
|
this.taggedHandler[tag] = handler
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|