TunnelsManager: Support for on-demand rules

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-11-11 01:31:38 +05:30
parent c5e8b05e8b
commit 39a067cb96
1 changed files with 36 additions and 0 deletions

View File

@ -72,6 +72,14 @@ class TunnelsManager {
tunnelProviderManager.localizedDescription = tunnelName tunnelProviderManager.localizedDescription = tunnelName
tunnelProviderManager.isEnabled = true 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 tunnelProviderManager.saveToPreferences { [weak self] (error) in
defer { self?.isAddingTunnel = false } defer { self?.isAddingTunnel = false }
guard (error == nil) else { guard (error == nil) else {
@ -130,6 +138,14 @@ class TunnelsManager {
tunnelProviderManager.localizedDescription = tunnelName tunnelProviderManager.localizedDescription = tunnelName
tunnelProviderManager.isEnabled = true 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 tunnelProviderManager.saveToPreferences { [weak self] (error) in
defer { self?.isModifyingTunnel = false } defer { self?.isModifyingTunnel = false }
guard (error == nil) else { guard (error == nil) else {
@ -213,6 +229,26 @@ class TunnelsManager {
t.refreshConnectionStatus() 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 { class TunnelContainer: NSObject {