From 22162521992a20e5736b703fbbe3d467f87da3ce Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Tue, 5 Mar 2024 14:45:37 +0100 Subject: [PATCH 1/2] Add pixels for our main VPN funnels --- .../PacketTunnelProvider.swift | 99 ++++++++++++------- 1 file changed, 61 insertions(+), 38 deletions(-) diff --git a/Sources/NetworkProtection/PacketTunnelProvider.swift b/Sources/NetworkProtection/PacketTunnelProvider.swift index 5009022d6..29a547e2b 100644 --- a/Sources/NetworkProtection/PacketTunnelProvider.swift +++ b/Sources/NetworkProtection/PacketTunnelProvider.swift @@ -33,11 +33,23 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { public enum Event { case userBecameActive case reportConnectionAttempt(attempt: ConnectionAttempt) + case tunnelStartAttempt(_ step: TunnelStartAttemptStep) + case tunnelUpdateAttempt(_ step: TunnelUpdateAttemptStep) case reportTunnelFailure(result: NetworkProtectionTunnelFailureMonitor.Result) case reportLatency(result: NetworkProtectionLatencyMonitor.Result) - case rekeyCompleted + case rekeyAttempt(_ step: RekeyAttemptStep) } + public enum AttemptStep { + case begin + case success + case failure(_ error: Error) + } + + public typealias TunnelStartAttemptStep = AttemptStep + public typealias TunnelUpdateAttemptStep = AttemptStep + public typealias RekeyAttemptStep = AttemptStep + public enum ConnectionAttempt { case connecting case success @@ -181,12 +193,14 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { } os_log("Rekeying...", log: .networkProtectionKeyManagement) + providerEvents.fire(.rekeyAttempt(.begin)) do { try await updateTunnelConfiguration(reassert: false, regenerateKey: true) - providerEvents.fire(.rekeyCompleted) + providerEvents.fire(.rekeyAttempt(.success)) } catch { os_log("Rekey attempt failed. This is not an error if you're using debug Key Management options: %{public}@", log: .networkProtectionKeyManagement, type: .error, String(describing: error)) + providerEvents.fire(.rekeyAttempt(.failure(error))) } } @@ -480,6 +494,12 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { open override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) { Task { @MainActor in + os_log("🚀 Options are: %{public}@", String(reflecting: options)) + os_log("🚀 Error 1: %{public}@", String((TunnelError.startingTunnelWithoutAuthToken as NSError).code)) + os_log("🚀 Error 2: %{public}@", String((TunnelError.couldNotGenerateTunnelConfiguration(internalError: NSError(domain: "asd", code: 0)) as NSError).code)) + os_log("🚀 Error 3: %{public}@", String((TunnelError.simulateTunnelFailureError as NSError).code)) + + providerEvents.fire(.tunnelStartAttempt(.begin)) prepareToConnect(using: tunnelProviderProtocol) connectionStatus = .connecting @@ -489,16 +509,10 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { resetIssueStateOnTunnelStart(startupOptions) - let startTime = DispatchTime.now() - - let internalCompletionHandler = { [weak self] (error: Error?) in - guard let self else { - completionHandler(error) - return - } - + let internalCompletionHandler = { [weak self, providerEvents] (error: Error?) in guard let error else { completionHandler(nil) + providerEvents.fire(.tunnelStartAttempt(.success)) return } @@ -506,9 +520,10 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { let errorDescription = (error as? LocalizedError)?.localizedDescription ?? String(describing: error) os_log("Tunnel startup error: %{public}@", type: .error, errorDescription) - self.controllerErrorStore.lastErrorMessage = errorDescription - self.connectionStatus = .disconnected + self?.controllerErrorStore.lastErrorMessage = errorDescription + self?.connectionStatus = .disconnected + providerEvents.fire(.tunnelStartAttempt(.failure(error))) completionHandler(error) } @@ -708,43 +723,51 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { reassert: Bool, regenerateKey: Bool = false) async throws { + providerEvents.fire(.tunnelUpdateAttempt(.begin)) + if reassert { await stopMonitors() } - let tunnelConfiguration = try await generateTunnelConfiguration(environment: environment, - serverSelectionMethod: serverSelectionMethod, - includedRoutes: includedRoutes ?? [], - excludedRoutes: settings.excludedRanges, - regenerateKey: regenerateKey) - - try await withCheckedThrowingContinuation { [weak self] (continuation: CheckedContinuation) in - guard let self = self else { - continuation.resume() - return - } - - self.adapter.update(tunnelConfiguration: tunnelConfiguration, reassert: reassert) { [weak self] error in - if let error = error { - os_log("🔵 Failed to update the configuration: %{public}@", type: .error, error.localizedDescription) - self?.debugEvents?.fire(error.networkProtectionError) - continuation.resume(throwing: error) + do { + let tunnelConfiguration = try await generateTunnelConfiguration(environment: environment, + serverSelectionMethod: serverSelectionMethod, + includedRoutes: includedRoutes ?? [], + excludedRoutes: settings.excludedRanges, + regenerateKey: regenerateKey) + + try await withCheckedThrowingContinuation { [weak self] (continuation: CheckedContinuation) in + guard let self = self else { + continuation.resume() return } - Task { [weak self] in - if reassert { - do { - try await self?.handleAdapterStarted(startReason: .reconnected) - } catch { - continuation.resume(throwing: error) - return - } + self.adapter.update(tunnelConfiguration: tunnelConfiguration, reassert: reassert) { [weak self] error in + if let error = error { + os_log("🔵 Failed to update the configuration: %{public}@", type: .error, error.localizedDescription) + self?.debugEvents?.fire(error.networkProtectionError) + continuation.resume(throwing: error) + return } - continuation.resume() + Task { [weak self] in + if reassert { + do { + try await self?.handleAdapterStarted(startReason: .reconnected) + } catch { + continuation.resume(throwing: error) + return + } + } + + continuation.resume() + } } } + + providerEvents.fire(.tunnelUpdateAttempt(.success)) + } catch { + providerEvents.fire(.tunnelUpdateAttempt(.failure(error))) } } From a05b619aed9911f41718ae44889174b08a213e52 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Thu, 7 Mar 2024 10:14:16 +0100 Subject: [PATCH 2/2] Removes some unnecessary log lines --- Sources/NetworkProtection/PacketTunnelProvider.swift | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Sources/NetworkProtection/PacketTunnelProvider.swift b/Sources/NetworkProtection/PacketTunnelProvider.swift index 29a547e2b..4769f73f4 100644 --- a/Sources/NetworkProtection/PacketTunnelProvider.swift +++ b/Sources/NetworkProtection/PacketTunnelProvider.swift @@ -494,11 +494,6 @@ open class PacketTunnelProvider: NEPacketTunnelProvider { open override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) { Task { @MainActor in - os_log("🚀 Options are: %{public}@", String(reflecting: options)) - os_log("🚀 Error 1: %{public}@", String((TunnelError.startingTunnelWithoutAuthToken as NSError).code)) - os_log("🚀 Error 2: %{public}@", String((TunnelError.couldNotGenerateTunnelConfiguration(internalError: NSError(domain: "asd", code: 0)) as NSError).code)) - os_log("🚀 Error 3: %{public}@", String((TunnelError.simulateTunnelFailureError as NSError).code)) - providerEvents.fire(.tunnelStartAttempt(.begin)) prepareToConnect(using: tunnelProviderProtocol)