forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (79 loc) · 2.42 KB
/
config.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package internet
import "v2ray.com/core/common/serial"
type ConfigCreator func() interface{}
var (
globalTransportConfigCreatorCache = make(map[TransportProtocol]ConfigCreator)
globalTransportSettings []*TransportConfig
)
func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
if _, found := globalTransportConfigCreatorCache[protocol]; found {
return newError("protocol ", TransportProtocol_name[int32(protocol)], " is already registered").AtError()
}
globalTransportConfigCreatorCache[protocol] = creator
return nil
}
func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
creator, ok := globalTransportConfigCreatorCache[protocol]
if !ok {
return nil, newError("unknown transport protocol: ", protocol)
}
return creator(), nil
}
func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
return c.Settings.GetInstance()
}
func (c *StreamConfig) GetEffectiveProtocol() TransportProtocol {
if c == nil {
return TransportProtocol_TCP
}
return c.Protocol
}
func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
protocol := c.GetEffectiveProtocol()
if c != nil {
for _, settings := range c.TransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
}
}
for _, settings := range globalTransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
}
return CreateTransportConfig(protocol)
}
func (c *StreamConfig) GetTransportSettingsFor(protocol TransportProtocol) (interface{}, error) {
if c != nil {
for _, settings := range c.TransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
}
}
for _, settings := range globalTransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
}
return CreateTransportConfig(protocol)
}
func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range c.SecuritySettings {
if settings.Type == c.SecurityType {
return settings.GetInstance()
}
}
return serial.GetInstance(c.SecurityType)
}
func (c *StreamConfig) HasSecuritySettings() bool {
return len(c.SecurityType) > 0
}
func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
globalTransportSettings = settings
return nil
}
func (c *ProxyConfig) HasTag() bool {
return c != nil && len(c.Tag) > 0
}