diff --git a/Sources/NetworkProtection/PacketTunnelProvider.swift b/Sources/NetworkProtection/PacketTunnelProvider.swift index 67bc888e0..56f15e22b 100644 --- a/Sources/NetworkProtection/PacketTunnelProvider.swift +++ b/Sources/NetworkProtection/PacketTunnelProvider.swift @@ -349,10 +349,6 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { try loadAuthToken(from: options) } - open func prepareToConnect(using provider: NETunnelProviderProtocol?) { - // no-op - } - open func loadVendorOptions(from provider: NETunnelProviderProtocol?) throws { let vendorOptions = provider?.providerConfiguration @@ -467,6 +463,12 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { .store(in: &cancellables) } + // MARK: - Overrideable Connection Events + + open func prepareToConnect(using provider: NETunnelProviderProtocol?) { + // no-op: abstract method to be overridden in subclass + } + // MARK: - Tunnel Start open override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) { @@ -585,17 +587,19 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { return } - Task { [weak self] in + Task { @MainActor [weak self] in // It's important to call this completion handler before running the tester // as if we don't, the tester will just fail. It seems like the connection // won't fully work until the completion handler is called. completionHandler(nil) + guard let self else { return } + do { let startReason: AdapterStartReason = onDemand ? .onDemand : .manual - try await self?.handleAdapterStarted(startReason: startReason) + try await self.handleAdapterStarted(startReason: startReason) } catch { - self?.cancelTunnelWithError(error) + self.cancelTunnelWithError(error) return } } @@ -1202,7 +1206,6 @@ extension WireGuardAdapterError: LocalizedError, CustomDebugStringConvertible { public var debugDescription: String { errorDescription! } - } // swiftlint:enable file_length diff --git a/Sources/NetworkProtection/Session/ConnectionSessionUtilities.swift b/Sources/NetworkProtection/Session/ConnectionSessionUtilities.swift index 3937a23e3..09ca7fcb0 100644 --- a/Sources/NetworkProtection/Session/ConnectionSessionUtilities.swift +++ b/Sources/NetworkProtection/Session/ConnectionSessionUtilities.swift @@ -80,7 +80,8 @@ public class ConnectionSessionUtilities { /// Retrieves a session from a `NEVPNStatusDidChange` notification. /// public static func session(from notification: Notification) -> NETunnelProviderSession? { - guard let session = (notification.object as? NETunnelProviderSession) else { + guard let session = (notification.object as? NETunnelProviderSession), + session.manager is NETunnelProviderManager else { return nil }