TunnelsManager: Support for on-demand rules

This commit is contained in:
Roopesh Chander 2018-11-11 01:31:38 +05:30
parent 151ad9de84
commit b09f3e3992
1 changed files with 36 additions and 0 deletions

View File

@ -72,6 +72,14 @@ class TunnelsManager {
tunnelProviderManager.localizedDescription = tunnelName
tunnelProviderManager.isEnabled = true
if (tunnelConfiguration.activationType == .activateManually) {
tunnelProviderManager.onDemandRules = []
tunnelProviderManager.isOnDemandEnabled = false
} else {
tunnelProviderManager.onDemandRules = onDemandRules(for: tunnelConfiguration.activationType)
tunnelProviderManager.isOnDemandEnabled = true
}
tunnelProviderManager.saveToPreferences { [weak self] (error) in
defer { self?.isAddingTunnel = false }
guard (error == nil) else {
@ -130,6 +138,14 @@ class TunnelsManager {
tunnelProviderManager.localizedDescription = tunnelName
tunnelProviderManager.isEnabled = true
if (tunnelConfiguration.activationType == .activateManually) {
tunnelProviderManager.onDemandRules = []
tunnelProviderManager.isOnDemandEnabled = false
} else {
tunnelProviderManager.onDemandRules = onDemandRules(for: tunnelConfiguration.activationType)
tunnelProviderManager.isOnDemandEnabled = true
}
tunnelProviderManager.saveToPreferences { [weak self] (error) in
defer { self?.isModifyingTunnel = false }
guard (error == nil) else {
@ -213,6 +229,26 @@ class TunnelsManager {
t.refreshConnectionStatus()
}
}
func onDemandRules(for activationType: ActivationType) -> [NEOnDemandRule] {
switch (activationType) {
case .activateManually: return []
case .useOnDemandOverWifiAndCellular:
return [NEOnDemandRuleConnect()]
case .useOnDemandOverWifiOnly:
let connectOnWifiRule = NEOnDemandRuleConnect()
connectOnWifiRule.interfaceTypeMatch = .wiFi
let disconnectOnCellularRule = NEOnDemandRuleDisconnect()
disconnectOnCellularRule.interfaceTypeMatch = .cellular
return [connectOnWifiRule, disconnectOnCellularRule]
case .useOnDemandOverCellularOnly:
let connectOnCellularRule = NEOnDemandRuleConnect()
connectOnCellularRule.interfaceTypeMatch = .cellular
let disconnectOnWifiRule = NEOnDemandRuleDisconnect()
disconnectOnWifiRule.interfaceTypeMatch = .wiFi
return [connectOnCellularRule, disconnectOnWifiRule]
}
}
}
class TunnelContainer: NSObject {