Skip to content

Commit

Permalink
Re-enable the tunnel if it's disabled before connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
KristijanMitrik committed Dec 18, 2024
1 parent 0dc4967 commit f7dfc8a
Showing 1 changed file with 48 additions and 34 deletions.
82 changes: 48 additions & 34 deletions darwin/Classes/WireguardDartPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ public class WireguardDartPlugin: NSObject, FlutterPlugin {
Logger.main.debug("Connection configuration: \(cfg)")
Task {
do {
mgr.isEnabled = true
if !mgr.isEnabled {
mgr.isEnabled = true
try await mgr.saveToPreferences()
try await mgr.loadFromPreferences()

}

try await mgr.saveToPreferences()
try await mgr.loadFromPreferences()
try mgr.connection.startVPNTunnel(options: [
"cfg": cfg as NSObject
])
Expand Down Expand Up @@ -216,31 +219,34 @@ public class WireguardDartPlugin: NSObject, FlutterPlugin {
}
case "checkTunnelConfiguration":
guard let args = call.arguments as? [String: Any],
let bundleId = args["bundleId"] as? String, !bundleId.isEmpty
let bundleId = args["bundleId"] as? String, !bundleId.isEmpty
else {
result(
nativeFlutterError(message: "required argument: 'bundleId'")
)
return
}
guard let args = call.arguments as? [String: Any],
let tunnelName = args["tunnelName"] as? String, !tunnelName.isEmpty
let tunnelName = args["tunnelName"] as? String,
!tunnelName.isEmpty
else {
result(
nativeFlutterError(message: "required argument: 'tunnelName'")
nativeFlutterError(
message: "required argument: 'tunnelName'")
)
return
}
checkTunnelConfiguration(bundleId: bundleId, tunnelName: tunnelName) { manager in
if let vpnManager = manager {
self.vpnManager = vpnManager
Logger.main.debug("Tunnel is set up and existing")
result(true)
} else {
Logger.main.debug("Tunnel is not set up")
result(false)
}
}
checkTunnelConfiguration(bundleId: bundleId, tunnelName: tunnelName)
{ manager in
if let vpnManager = manager {
self.vpnManager = vpnManager
Logger.main.debug("Tunnel is set up and existing")
result(true)
} else {
Logger.main.debug("Tunnel is not set up")
result(false)
}
}

default:
result(FlutterMethodNotImplemented)
Expand Down Expand Up @@ -270,26 +276,34 @@ public class WireguardDartPlugin: NSObject, FlutterPlugin {
return mgr
}

func isVpnManagerConfigured(bundleId: String, tunnelName: String) async throws -> NETunnelProviderManager? {
// Load all managers from preferences
let mgrs = try await NETunnelProviderManager.loadAllFromPreferences()
if let existingMgr = mgrs.first(where: {
($0.protocolConfiguration as? NETunnelProviderProtocol)?.providerBundleIdentifier == bundleId
}) {
return existingMgr
func isVpnManagerConfigured(bundleId: String, tunnelName: String)
async throws -> NETunnelProviderManager?
{
// Load all managers from preferences
let mgrs = try await NETunnelProviderManager.loadAllFromPreferences()
if let existingMgr = mgrs.first(where: {
($0.protocolConfiguration as? NETunnelProviderProtocol)?
.providerBundleIdentifier == bundleId
}) {
return existingMgr
}
return nil
}
return nil
}

func checkTunnelConfiguration(bundleId: String, tunnelName: String, result: @escaping (NETunnelProviderManager?) -> Void) {
Task {
do {
let mgr = try await isVpnManagerConfigured(bundleId: bundleId, tunnelName: tunnelName)
result(mgr)
} catch {
Logger.main.error("Error checking tunnel configuration: \(error)")
result(nil)
func checkTunnelConfiguration(
bundleId: String, tunnelName: String,
result: @escaping (NETunnelProviderManager?) -> Void
) {
Task {
do {
let mgr = try await isVpnManagerConfigured(
bundleId: bundleId, tunnelName: tunnelName)
result(mgr)
} catch {
Logger.main.error(
"Error checking tunnel configuration: \(error)")
result(nil)
}
}
}
}
}

0 comments on commit f7dfc8a

Please sign in to comment.