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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2016-11-10 22:41:28 +00:00
|
|
|
|
|
|
|
func (this *ProxyConfig) HasTag() bool {
|
|
|
|
return this != nil && len(this.Tag) > 0
|
|
|
|
}
|