UI: When saving on-demand rules, deactivate if reqd and then save

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-09-27 16:07:20 +05:30
parent ba644415c7
commit 23618f994f
2 changed files with 30 additions and 14 deletions

View File

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

View File

@ -206,7 +206,10 @@ class TunnelsManager {
} }
} }
func modify(tunnel: TunnelContainer, tunnelConfiguration: TunnelConfiguration, onDemandOption: ActivateOnDemandOption, completionHandler: @escaping (TunnelsManagerError?) -> Void) { func modify(tunnel: TunnelContainer, tunnelConfiguration: TunnelConfiguration,
onDemandOption: ActivateOnDemandOption,
shouldEnsureOnDemandEnabled: Bool = false,
completionHandler: @escaping (TunnelsManagerError?) -> Void) {
let tunnelName = tunnelConfiguration.name ?? "" let tunnelName = tunnelConfiguration.name ?? ""
if tunnelName.isEmpty { if tunnelName.isEmpty {
completionHandler(TunnelsManagerError.tunnelNameEmpty) completionHandler(TunnelsManagerError.tunnelNameEmpty)
@ -214,6 +217,20 @@ class TunnelsManager {
} }
let tunnelProviderManager = tunnel.tunnelProvider let tunnelProviderManager = tunnel.tunnelProvider
let isIntroducingOnDemandRules = (tunnelProviderManager.onDemandRules ?? []).isEmpty && onDemandOption != .off
if isIntroducingOnDemandRules && tunnel.status != .inactive && tunnel.status != .deactivating {
tunnel.onDeactivated = { [weak self] in
self?.modify(tunnel: tunnel, tunnelConfiguration: tunnelConfiguration,
onDemandOption: onDemandOption, shouldEnsureOnDemandEnabled: true,
completionHandler: completionHandler)
}
self.startDeactivation(of: tunnel)
return
} else {
tunnel.onDeactivated = nil
}
let oldName = tunnelProviderManager.localizedDescription ?? "" let oldName = tunnelProviderManager.localizedDescription ?? ""
let isNameChanged = tunnelName != oldName let isNameChanged = tunnelName != oldName
if isNameChanged { if isNameChanged {
@ -231,10 +248,11 @@ class TunnelsManager {
} }
tunnelProviderManager.isEnabled = true tunnelProviderManager.isEnabled = true
let wasOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled let isActivatingOnDemand = !tunnelProviderManager.isOnDemandEnabled && shouldEnsureOnDemandEnabled
let isIntroducingOnDemandRules = (tunnelProviderManager.onDemandRules ?? []).isEmpty && onDemandOption != .off
onDemandOption.apply(on: tunnelProviderManager) onDemandOption.apply(on: tunnelProviderManager)
let isActivatingOnDemand = !wasOnDemandEnabled && tunnelProviderManager.isOnDemandEnabled if shouldEnsureOnDemandEnabled {
tunnelProviderManager.isOnDemandEnabled = true
}
tunnelProviderManager.saveToPreferences { [weak self] error in tunnelProviderManager.saveToPreferences { [weak self] error in
if let error = error { if let error = error {
@ -266,11 +284,8 @@ 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 { [weak self] error in tunnelProviderManager.loadFromPreferences { 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))
@ -279,9 +294,6 @@ class TunnelsManager {
} }
} }
} else { } else {
if isIntroducingOnDemandRules {
self.startDeactivation(of: tunnel)
}
completionHandler(nil) completionHandler(nil)
} }
} }
@ -507,6 +519,11 @@ class TunnelsManager {
} }
} }
if session.status == .disconnected {
tunnel.onDeactivated?()
tunnel.onDeactivated = nil
}
if tunnel.status == .restarting && session.status == .disconnected { if tunnel.status == .restarting && session.status == .disconnected {
tunnel.startActivation(activationDelegate: self.activationDelegate) tunnel.startActivation(activationDelegate: self.activationDelegate)
return return
@ -577,6 +594,7 @@ class TunnelContainer: NSObject {
var activationAttemptId: String? var activationAttemptId: String?
var activationTimer: Timer? var activationTimer: Timer?
var deactivationTimer: Timer? var deactivationTimer: Timer?
var onDeactivated: (() -> Void)?
fileprivate var tunnelProvider: NETunnelProviderManager { fileprivate var tunnelProvider: NETunnelProviderManager {
didSet { didSet {