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

69 lines
1.7 KiB
Go
Raw Normal View History

2016-10-02 21:43:58 +00:00
package internet
import (
"errors"
2016-10-16 12:22:21 +00:00
"v2ray.com/core/common/loader"
2016-10-02 21:43:58 +00:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
)
var (
2016-10-16 12:22:21 +00:00
globalNetworkConfigCreatorCache = make(map[v2net.Network]loader.ConfigCreator)
2016-10-02 21:43:58 +00:00
globalNetworkSettings []*NetworkSettings
ErrUnconfiguredNetwork = errors.New("Network config creator not set.")
)
2016-10-16 12:22:21 +00:00
func RegisterNetworkConfigCreator(network v2net.Network, creator loader.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
}
func (this *NetworkSettings) GetTypedSettings() (interface{}, error) {
2016-10-16 12:22:21 +00:00
return this.Settings.GetInstance()
2016-10-02 21:43:58 +00:00
}
func (this *StreamConfig) GetEffectiveNetworkSettings() (interface{}, error) {
for _, settings := range this.NetworkSettings {
if settings.Network == this.Network {
return settings.GetTypedSettings()
}
}
for _, settings := range globalNetworkSettings {
if settings.Network == this.Network {
return settings.GetTypedSettings()
}
}
return CreateNetworkConfig(this.Network)
}
func (this *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range this.SecuritySettings {
if settings.Type == this.SecurityType {
2016-10-16 12:22:21 +00:00
return settings.GetInstance()
2016-10-02 21:43:58 +00:00
}
}
2016-10-16 12:22:21 +00:00
return loader.GetInstance(this.SecurityType)
}
func (this *StreamConfig) HasSecuritySettings() bool {
return len(this.SecurityType) > 0
2016-10-02 21:43:58 +00:00
}
func ApplyGlobalNetworkSettings(settings []*NetworkSettings) error {
globalNetworkSettings = settings
return nil
}