UI: When saving on-demand rules on a config, enable on-demand if active

When a user saves on-demand rules on the configuration, set
onDemandEnabled to true if the tunnel is active, and false if it isn't.
Then deactivate the tunnel.

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-09-24 00:41:46 +05:30
parent 10da5cfdef
commit ba644415c7
2 changed files with 13 additions and 3 deletions

View File

@ -42,7 +42,9 @@ extension ActivateOnDemandOption {
} }
} }
tunnelProviderManager.onDemandRules = rules tunnelProviderManager.onDemandRules = rules
tunnelProviderManager.isOnDemandEnabled = false let status = tunnelProviderManager.connection.status
let isActive = status == .connected || status == .connecting
tunnelProviderManager.isOnDemandEnabled = (rules != nil) && (tunnelProviderManager.isOnDemandEnabled || isActive)
} }
init(from tunnelProviderManager: NETunnelProviderManager) { init(from tunnelProviderManager: NETunnelProviderManager) {

View File

@ -231,8 +231,10 @@ class TunnelsManager {
} }
tunnelProviderManager.isEnabled = true tunnelProviderManager.isEnabled = true
let isActivatingOnDemand = !tunnelProviderManager.isOnDemandEnabled && onDemandOption != .off let wasOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled
let isIntroducingOnDemandRules = (tunnelProviderManager.onDemandRules ?? []).isEmpty && onDemandOption != .off
onDemandOption.apply(on: tunnelProviderManager) onDemandOption.apply(on: tunnelProviderManager)
let isActivatingOnDemand = !wasOnDemandEnabled && tunnelProviderManager.isOnDemandEnabled
tunnelProviderManager.saveToPreferences { [weak self] error in tunnelProviderManager.saveToPreferences { [weak self] error in
if let error = error { if let error = error {
@ -264,8 +266,11 @@ class TunnelsManager {
if isActivatingOnDemand { if isActivatingOnDemand {
// Reload tunnel after saving. // Reload tunnel after saving.
// Without this, the tunnel stopes getting updates on the tunnel status from iOS. // Without this, the tunnel stopes getting updates on the tunnel status from iOS.
tunnelProviderManager.loadFromPreferences { error in tunnelProviderManager.loadFromPreferences { [weak self] error in
tunnel.isActivateOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled tunnel.isActivateOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled
if isIntroducingOnDemandRules {
self?.startDeactivation(of: tunnel)
}
if let error = error { if let error = error {
wg_log(.error, message: "Modify: Re-loading after saving configuration failed: \(error)") wg_log(.error, message: "Modify: Re-loading after saving configuration failed: \(error)")
completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel(systemError: error)) completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel(systemError: error))
@ -274,6 +279,9 @@ class TunnelsManager {
} }
} }
} else { } else {
if isIntroducingOnDemandRules {
self.startDeactivation(of: tunnel)
}
completionHandler(nil) completionHandler(nil)
} }
} }