forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (60 loc) · 1.8 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
package internet
import (
"errors"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
)
type ConfigCreator func() interface{}
var (
globalNetworkConfigCreatorCache = make(map[v2net.Network]ConfigCreator)
globalNetworkSettings []*NetworkSettings
ErrUnconfiguredNetwork = errors.New("Network config creator not set.")
)
func RegisterNetworkConfigCreator(network v2net.Network, creator ConfigCreator) error {
// TODO: check duplicate
globalNetworkConfigCreatorCache[network] = creator
return nil
}
func CreateNetworkConfig(network v2net.Network) (interface{}, error) {
creator, ok := globalNetworkConfigCreatorCache[network]
if !ok {
log.Warning("Internet: Network config creator not found: ", network)
return nil, ErrUnconfiguredNetwork
}
return creator(), nil
}
func (v *NetworkSettings) GetTypedSettings() (interface{}, error) {
return v.Settings.GetInstance()
}
func (v *StreamConfig) GetEffectiveNetworkSettings() (interface{}, error) {
for _, settings := range v.NetworkSettings {
if settings.Network == v.Network {
return settings.GetTypedSettings()
}
}
for _, settings := range globalNetworkSettings {
if settings.Network == v.Network {
return settings.GetTypedSettings()
}
}
return CreateNetworkConfig(v.Network)
}
func (v *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range v.SecuritySettings {
if settings.Type == v.SecurityType {
return settings.GetInstance()
}
}
return loader.GetInstance(v.SecurityType)
}
func (v *StreamConfig) HasSecuritySettings() bool {
return len(v.SecurityType) > 0
}
func ApplyGlobalNetworkSettings(settings []*NetworkSettings) error {
globalNetworkSettings = settings
return nil
}
func (v *ProxyConfig) HasTag() bool {
return v != nil && len(v.Tag) > 0
}