Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pixels for our main VPN funnels #698

Merged
merged 2 commits into from
Mar 7, 2024
Merged
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
94 changes: 56 additions & 38 deletions Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
}
}

Expand Down Expand Up @@ -480,6 +494,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {

open override func startTunnel(options: [String: NSObject]?, completionHandler: @escaping (Error?) -> Void) {
Task { @MainActor in
providerEvents.fire(.tunnelStartAttempt(.begin))
prepareToConnect(using: tunnelProviderProtocol)

connectionStatus = .connecting
Expand All @@ -489,26 +504,21 @@ 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
}

let handler = {
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)
}

Expand Down Expand Up @@ -708,43 +718,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<Void, Error>) 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<Void, Error>) 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)))
}
}

Expand Down
Loading