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