]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
TunnelsManager: If only Activate On Demand has changed, don't restart tunnel
authorRoopesh Chander <roop@roopc.net>
Wed, 19 Dec 2018 10:32:48 +0000 (16:02 +0530)
committerRoopesh Chander <roop@roopc.net>
Wed, 19 Dec 2018 13:05:53 +0000 (18:35 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/Shared/NETunnelProviderProtocol+Extension.swift
WireGuard/WireGuard/Tunnel/TunnelsManager.swift

index 9f4af774daa99e1d12818b12386e32eb248e44fc..776e03b26ec57b4a6d523726c4c52d01c23738d5 100644 (file)
@@ -37,4 +37,10 @@ extension NETunnelProviderProtocol {
     var isActivateOnDemandEnabled: Bool {
         return (providerConfiguration?["isActivateOnDemandEnabled"] as? Bool) ?? false
     }
+
+    func hasTunnelConfiguration(tunnelConfiguration otherTunnelConfiguration: TunnelConfiguration) -> Bool {
+        guard let serializedThisTunnelConfiguration = providerConfiguration?["tunnelConfiguration"] as? Data else { return false }
+        guard let serializedOtherTunnelConfiguration = try? JSONEncoder().encode(otherTunnelConfiguration) else { return false }
+        return serializedThisTunnelConfiguration == serializedOtherTunnelConfiguration
+    }
 }
index 10ef1ef43bbabc64d02e0fcdcec779a9ca7aafd1..83823612cb3167e1ae9a2fc010d29b168f21590c 100644 (file)
@@ -115,6 +115,9 @@ class TunnelsManager {
             }
             tunnel.name = tunnelName
         }
+
+        let shouldRestartIfActive = !((tunnelProviderManager.protocolConfiguration as? NETunnelProviderProtocol)?.hasTunnelConfiguration(tunnelConfiguration: tunnelConfiguration) ?? false)
+
         tunnelProviderManager.protocolConfiguration = NETunnelProviderProtocol(tunnelConfiguration: tunnelConfiguration, isActivateOnDemandEnabled: activateOnDemandSetting.isActivateOnDemandEnabled)
         tunnelProviderManager.localizedDescription = tunnelName
         tunnelProviderManager.isEnabled = true
@@ -137,13 +140,15 @@ class TunnelsManager {
                 self.tunnelsListDelegate?.tunnelMoved(from: oldIndex, to: newIndex)
             }
             self.tunnelsListDelegate?.tunnelModified(at: self.tunnels.firstIndex(of: tunnel)!)
-            
-            if tunnel.status == .active || tunnel.status == .activating || tunnel.status == .reasserting {
-                // Turn off the tunnel, and then turn it back on, so the changes are made effective
-                tunnel.status = .restarting
-                (tunnel.tunnelProvider.connection as? NETunnelProviderSession)?.stopTunnel()
+
+            if shouldRestartIfActive {
+                if tunnel.status == .active || tunnel.status == .activating || tunnel.status == .reasserting {
+                    // Turn off the tunnel, and then turn it back on, so the changes are made effective
+                    tunnel.status = .restarting
+                    (tunnel.tunnelProvider.connection as? NETunnelProviderSession)?.stopTunnel()
+                }
             }
-            
+
             if isActivatingOnDemand {
                 // Reload tunnel after saving.
                 // Without this, the tunnel stopes getting updates on the tunnel status from iOS.