]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
UI: When setting on-demand, enable the tunnel if required
authorRoopesh Chander <roop@roopc.net>
Wed, 28 Jul 2021 05:59:34 +0000 (11:29 +0530)
committerRoopesh Chander <roop@roopc.net>
Wed, 28 Jul 2021 06:22:54 +0000 (11:52 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
Sources/WireGuardApp/Tunnel/TunnelsManager.swift

index 7bfb33961a0ae011dcbb7e1e208fcb9615d13e5e..20bd1cad508162368997a39f1cc8278c0e689c98 100644 (file)
@@ -357,11 +357,27 @@ class TunnelsManager {
                 return
             }
             if isActivatingOnDemand {
+                // If we're enabling on-demand, we want to make sure the tunnel is enabled.
+                // If not enabled, the OS will not turn the tunnel on/off based on our rules.
                 tunnelProviderManager.loadFromPreferences { error in
+                    // isActivateOnDemandEnabled will get changed in reload(), but no harm in setting it here too
                     tunnel.isActivateOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled
                     if let error = error {
                         wg_log(.error, message: "Modify On-Demand: Re-loading after saving configuration failed: \(error)")
                         completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel(systemError: error))
+                        return
+                    }
+                    if !tunnelProviderManager.isEnabled {
+                        // In case the tunnel has gotten disabled, re-enable and save it.
+                        wg_log(.debug, staticMessage: "Modify On-Demand: Tunnel is disabled. Re-enabling and saving")
+                        tunnelProviderManager.isEnabled = true
+                        tunnelProviderManager.saveToPreferences { error in
+                            if let error = error {
+                                wg_log(.error, message: "Modify On-Demand: Error saving tunnel after re-enabling: \(error)")
+                                completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel(systemError: error))
+                                return
+                            }
+                        }
                     } else {
                         completionHandler(nil)
                     }