Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions LocalDevVPN/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
21 changes: 21 additions & 0 deletions LocalDevVPN/LocalDevVPNApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down