1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-03 14:40:42 +00:00
v2fly/transport/internet/config.go

74 lines
1.8 KiB
Go
Raw Normal View History

2016-10-02 21:43:58 +00:00
package internet
import (
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-10-02 21:43:58 +00:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2016-12-21 14:37:16 +00:00
"v2ray.com/core/common/serial"
2016-10-02 21:43:58 +00:00
)
2016-10-18 13:31:48 +00:00
type ConfigCreator func() interface{}
2016-10-02 21:43:58 +00:00
var (
2016-10-18 13:31:48 +00:00
globalNetworkConfigCreatorCache = make(map[v2net.Network]ConfigCreator)
2016-10-02 21:43:58 +00:00
globalNetworkSettings []*NetworkSettings
ErrUnconfiguredNetwork = errors.New("Network config creator not set.")
)
2016-10-18 13:31:48 +00:00
func RegisterNetworkConfigCreator(network v2net.Network, creator ConfigCreator) error {
2016-10-02 21:43:58 +00:00
// TODO: check duplicate
globalNetworkConfigCreatorCache[network] = creator
return nil
}
2016-10-16 12:22:21 +00:00
func CreateNetworkConfig(network v2net.Network) (interface{}, error) {
2016-10-02 21:43:58 +00:00
creator, ok := globalNetworkConfigCreatorCache[network]
if !ok {
log.Warning("Internet: Network config creator not found: ", network)
return nil, ErrUnconfiguredNetwork
}
return creator(), nil
}
2016-11-27 20:39:09 +00:00
func (v *NetworkSettings) GetTypedSettings() (interface{}, error) {
return v.Settings.GetInstance()
2016-10-02 21:43:58 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *StreamConfig) GetEffectiveNetworkSettings() (interface{}, error) {
for _, settings := range v.NetworkSettings {
if settings.Network == v.Network {
2016-10-02 21:43:58 +00:00
return settings.GetTypedSettings()
}
}
for _, settings := range globalNetworkSettings {
2016-11-27 20:39:09 +00:00
if settings.Network == v.Network {
2016-10-02 21:43:58 +00:00
return settings.GetTypedSettings()
}
}
2016-11-27 20:39:09 +00:00
return CreateNetworkConfig(v.Network)
2016-10-02 21:43:58 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range v.SecuritySettings {
if settings.Type == v.SecurityType {
2016-10-16 12:22:21 +00:00
return settings.GetInstance()
2016-10-02 21:43:58 +00:00
}
}
2016-12-15 10:51:09 +00:00
return serial.GetInstance(v.SecurityType)
2016-10-16 12:22:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *StreamConfig) HasSecuritySettings() bool {
return len(v.SecurityType) > 0
2016-10-02 21:43:58 +00:00
}
func ApplyGlobalNetworkSettings(settings []*NetworkSettings) error {
globalNetworkSettings = settings
return nil
}
2016-11-10 22:41:28 +00:00
2016-11-27 20:39:09 +00:00
func (v *ProxyConfig) HasTag() bool {
return v != nil && len(v.Tag) > 0
2016-11-10 22:41:28 +00:00
}