From cfbfa69c6798275b5329df73df11845f7b9ad0c5 Mon Sep 17 00:00:00 2001 From: Luo Hanjun <173594756+Elvis-Luo@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:02:33 +0800 Subject: [PATCH] Add localdevvpn://restart A tunnel can look connected while its packet tunnel extension is gone (iOS ends it under memory pressure): the utun interface stays up but carries nothing, and localdevvpn://enable does nothing because the tunnel already counts as started. localdevvpn://restart loads the tunnel manager first (as the Shortcuts action does, so it also works when the URL cold-launches the app), then TunnelManager.restartVPN() stops the tunnel and starts it again once it reports disconnected, through the existing ShouldStartLocalDevVPNAfterDisconnect path. A tunnel that is not running is just started. With ?scheme=NAME it opens NAME:// once the tunnel has gone down and come back up, or after 10 s at most. Compiled, not run on a device. --- LocalDevVPN/ContentView.swift | 25 +++++++++++++++++++++++++ LocalDevVPN/LocalDevVPNApp.swift | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/LocalDevVPN/ContentView.swift b/LocalDevVPN/ContentView.swift index a49a4df..9132eb4 100644 --- a/LocalDevVPN/ContentView.swift +++ b/LocalDevVPN/ContentView.swift @@ -595,6 +595,31 @@ class TunnelManager: ObservableObject { VPNLogger.shared.log("LocalDevVPN tunnel stop initiated") } + /// Stops the tunnel and starts it again once it is down (localdevvpn://restart), through the same + /// ShouldStartLocalDevVPNAfterDisconnect path as a configuration change. A tunnel that is not running + /// is just started. + func restartVPN() { + if isSimulator { + simulateStopVPN() + simulateStartVPN() + return + } + + guard let manager = vpnManager, + manager.connection.status != .disconnected, manager.connection.status != .invalid else { + startVPN() + return + } + + pendingStartAddresses = nil + UserDefaults.standard.set(true, forKey: "ShouldStartLocalDevVPNAfterDisconnect") + DispatchQueue.main.async { [weak self] in + self?.tunnelStatus = .disconnecting + } + manager.connection.stopVPNTunnel() + VPNLogger.shared.log("LocalDevVPN tunnel restart initiated") + } + func updateConfigAndRestart(shouldRestart: Bool) { if isSimulator { return } diff --git a/LocalDevVPN/LocalDevVPNApp.swift b/LocalDevVPN/LocalDevVPNApp.swift index 166eda7..e5396f8 100644 --- a/LocalDevVPN/LocalDevVPNApp.swift +++ b/LocalDevVPN/LocalDevVPNApp.swift @@ -33,6 +33,27 @@ struct LocalDevVPNApp: App { UIApplication.shared.open(callbackURL) } } + case "restart": + // A tunnel can look connected while its extension is gone (iOS ended it under memory pressure): + // "enable" then does nothing. Stop it and start it again; with ?scheme=, go back to the caller + // once the tunnel is up again (at most 10 s). + let scheme = URLComponents(url: url, resolvingAgainstBaseURL: false)? + .queryItems?.first(where: { $0.name == "scheme" })?.value + Task { @MainActor in + _ = try? await tunnelManager.isVPNActive() // loads the manager on a cold launch + tunnelManager.restartVPN() + guard let scheme, let callbackURL = URL(string: "\(scheme)://") else { return } + var sawDown = false + for _ in 0..<40 { + try? await Task.sleep(nanoseconds: 250_000_000) + if tunnelManager.tunnelStatus != .connected { + sawDown = true + } else if sawDown { + break + } + } + _ = await UIApplication.shared.open(callbackURL) + } case "disable": tunnelManager.stopVPN() if let components = URLComponents(url: url, resolvingAgainstBaseURL: false),