-
Notifications
You must be signed in to change notification settings - Fork 0
/
outbound.go
79 lines (64 loc) · 1.72 KB
/
outbound.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package outbound
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg outbound -path App,Proxyman,Outbound
import (
"context"
"sync"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
)
// Manager is to manage all outbound handlers.
type Manager struct {
sync.RWMutex
defaultHandler *Handler
taggedHandler map[string]*Handler
}
// New creates a new Manager.
func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error) {
return &Manager{
taggedHandler: make(map[string]*Handler),
}, nil
}
// Interface implements Application.Interface.
func (*Manager) Interface() interface{} {
return (*proxyman.OutboundHandlerManager)(nil)
}
// Start implements Application.Start
func (*Manager) Start() error { return nil }
// Close implements Application.Close
func (*Manager) Close() {}
func (m *Manager) GetDefaultHandler() proxyman.OutboundHandler {
m.RLock()
defer m.RUnlock()
if m.defaultHandler == nil {
return nil
}
return m.defaultHandler
}
func (m *Manager) GetHandler(tag string) proxyman.OutboundHandler {
m.RLock()
defer m.RUnlock()
if handler, found := m.taggedHandler[tag]; found {
return handler
}
return nil
}
func (m *Manager) AddHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) error {
m.Lock()
defer m.Unlock()
handler, err := NewHandler(ctx, config)
if err != nil {
return err
}
if m.defaultHandler == nil {
m.defaultHandler = handler
}
if len(config.Tag) > 0 {
m.taggedHandler[config.Tag] = handler
}
return nil
}
func init() {
common.Must(common.RegisterConfig((*proxyman.OutboundConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return New(ctx, config.(*proxyman.OutboundConfig))
}))
}