2018-01-10 11:22:37 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
2018-10-11 19:14:53 +00:00
|
|
|
"v2ray.com/core/features/inbound"
|
2018-10-11 18:43:37 +00:00
|
|
|
"v2ray.com/core/features/outbound"
|
2018-01-10 11:22:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type syncInboundHandlerManager struct {
|
|
|
|
sync.RWMutex
|
2018-10-11 19:14:53 +00:00
|
|
|
inbound.Manager
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
func (m *syncInboundHandlerManager) GetHandler(ctx context.Context, tag string) (inbound.Handler, error) {
|
2018-01-10 11:22:37 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
if m.Manager == nil {
|
|
|
|
return nil, newError("inbound.Manager not set.").AtError()
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
return m.Manager.GetHandler(ctx, tag)
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
func (m *syncInboundHandlerManager) AddHandler(ctx context.Context, handler inbound.Handler) error {
|
2018-01-10 11:22:37 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
if m.Manager == nil {
|
|
|
|
return newError("inbound.Manager not set.").AtError()
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
return m.Manager.AddHandler(ctx, handler)
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *syncInboundHandlerManager) Start() error {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
if m.Manager == nil {
|
|
|
|
return newError("inbound.Manager not set.").AtError()
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
return m.Manager.Start()
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:39:46 +00:00
|
|
|
func (m *syncInboundHandlerManager) Close() error {
|
2018-01-10 11:22:37 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
return common.Close(m.Manager)
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
func (m *syncInboundHandlerManager) Set(manager inbound.Manager) {
|
2018-02-20 20:22:41 +00:00
|
|
|
if manager == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-10-11 19:14:53 +00:00
|
|
|
common.Close(m.Manager) // nolint: errcheck
|
|
|
|
m.Manager = manager
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type syncOutboundHandlerManager struct {
|
|
|
|
sync.RWMutex
|
2018-10-11 18:43:37 +00:00
|
|
|
outbound.HandlerManager
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
func (m *syncOutboundHandlerManager) GetHandler(tag string) outbound.Handler {
|
2018-01-10 11:22:37 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
if m.HandlerManager == nil {
|
2018-01-10 11:22:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
return m.HandlerManager.GetHandler(tag)
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
func (m *syncOutboundHandlerManager) GetDefaultHandler() outbound.Handler {
|
2018-01-10 11:22:37 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
if m.HandlerManager == nil {
|
2018-01-10 11:22:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
return m.HandlerManager.GetDefaultHandler()
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
func (m *syncOutboundHandlerManager) AddHandler(ctx context.Context, handler outbound.Handler) error {
|
2018-01-10 11:22:37 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
if m.HandlerManager == nil {
|
2018-01-10 11:22:37 +00:00
|
|
|
return newError("OutboundHandlerManager not set.").AtError()
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
return m.HandlerManager.AddHandler(ctx, handler)
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *syncOutboundHandlerManager) Start() error {
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
if m.HandlerManager == nil {
|
2018-01-10 11:22:37 +00:00
|
|
|
return newError("OutboundHandlerManager not set.").AtError()
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
return m.HandlerManager.Start()
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 14:39:46 +00:00
|
|
|
func (m *syncOutboundHandlerManager) Close() error {
|
2018-01-10 11:22:37 +00:00
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
return common.Close(m.HandlerManager)
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
func (m *syncOutboundHandlerManager) Set(manager outbound.HandlerManager) {
|
2018-02-20 20:22:41 +00:00
|
|
|
if manager == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:22:37 +00:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2018-10-11 18:43:37 +00:00
|
|
|
common.Close(m.HandlerManager) // nolint: errcheck
|
|
|
|
m.HandlerManager = manager
|
2018-01-10 11:22:37 +00:00
|
|
|
}
|