-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_cache.go
78 lines (66 loc) · 2.03 KB
/
handler_cache.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
package internal
import (
"errors"
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/internal/config"
)
var (
inboundFactories = make(map[string]InboundHandlerCreator)
outboundFactories = make(map[string]OutboundHandlerCreator)
ErrorProxyNotFound = errors.New("Proxy not found.")
ErrorNameExists = errors.New("Proxy with the same name already exists.")
ErrorBadConfiguration = errors.New("Bad proxy configuration.")
)
func RegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) error {
if _, found := inboundFactories[name]; found {
return ErrorNameExists
}
inboundFactories[name] = creator
return nil
}
func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerCreator) {
if err := RegisterInboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) error {
if _, found := outboundFactories[name]; found {
return ErrorNameExists
}
outboundFactories[name] = creator
return nil
}
func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCreator) {
if err := RegisterOutboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
func CreateInboundHandler(name string, space app.Space, rawConfig []byte) (proxy.InboundHandler, error) {
creator, found := inboundFactories[name]
if !found {
return nil, ErrorProxyNotFound
}
if len(rawConfig) > 0 {
proxyConfig, err := config.CreateInboundConfig(name, rawConfig)
if err != nil {
return nil, err
}
return creator(space, proxyConfig)
}
return creator(space, nil)
}
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte) (proxy.OutboundHandler, error) {
creator, found := outboundFactories[name]
if !found {
return nil, ErrorNameExists
}
if len(rawConfig) > 0 {
proxyConfig, err := config.CreateOutboundConfig(name, rawConfig)
if err != nil {
return nil, err
}
return creator(space, proxyConfig)
}
return creator(space, nil)
}