Skip to content

Commit

Permalink
[Release PR] Update VPN metadata reporter (#2808)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/414235014887631/1207207179448754/f
Tech Design URL:
CC:

Description:

This PR updates the VPN metadata reporter to include the domain, code, and information about any underlying errors. It also includes the localized description.
  • Loading branch information
samsymons authored May 1, 2024
1 parent 41f554d commit 0c972d1
Showing 1 changed file with 39 additions and 43 deletions.
82 changes: 39 additions & 43 deletions DuckDuckGo/Feedback/VPNMetadataCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ struct VPNMetadata: Encodable {

struct VPNState: Encodable {
let connectionState: String
let lastDisconnectError: String
let lastDisconnectError: LastDisconnectError?
let underlyingErrors: [LastDisconnectError]?
let connectedServer: String
let connectedServerIP: String
}
Expand Down Expand Up @@ -77,6 +78,12 @@ struct VPNMetadata: Encodable {
let subscriptionActive: Bool
}

struct LastDisconnectError: Encodable {
let domain: String
let code: Int
let description: String
}

let appInfo: AppInfo
let deviceInfo: DeviceInfo
let networkInfo: NetworkInfo
Expand Down Expand Up @@ -220,61 +227,30 @@ final class DefaultVPNMetadataCollector: VPNMetadataCollector {
let connectionState = String(describing: statusObserver.recentValue)
let connectedServer = serverInfoObserver.recentValue.serverLocation?.serverLocation ?? "none"
let connectedServerIP = serverInfoObserver.recentValue.serverAddress ?? "none"
let lastDisconnectError = await lastDisconnectError()

return .init(connectionState: connectionState,
lastDisconnectError: await lastDisconnectError(),
lastDisconnectError: lastDisconnectError?.error,
underlyingErrors: lastDisconnectError?.underlyingErrors,
connectedServer: connectedServer,
connectedServerIP: connectedServerIP)
}

public func lastDisconnectError() async -> String {
public func lastDisconnectError() async -> (error: VPNMetadata.LastDisconnectError, underlyingErrors: [VPNMetadata.LastDisconnectError])? {
if #available(iOS 16, *) {
guard let tunnelManager = try? await NETunnelProviderManager.loadAllFromPreferences().first else {
return "none"
return nil
}

return await withCheckedContinuation { continuation in
tunnelManager.connection.fetchLastDisconnectError { error in
let message = {
if let error = error as? NSError {
if error.domain == NEVPNConnectionErrorDomain,
let code = NEVPNConnectionError(rawValue: error.code) {
switch code {
case .overslept: return "overslept"
case .noNetworkAvailable: return "noNetworkAvailable"
case .unrecoverableNetworkChange: return "unrecoverableNetworkChange"
case .configurationFailed: return "configurationFailed"
case .serverAddressResolutionFailed: return "serverAddressResolutionFailed"
case .serverNotResponding: return "serverNotResponding"
case .serverDead: return "serverDead"
case .authenticationFailed: return "authenticationFailed"
case .clientCertificateInvalid: return "clientCertificateInvalid"
case .clientCertificateNotYetValid: return "clientCertificateNotYetValid"
case .clientCertificateExpired: return "clientCertificateExpired"
case .pluginFailed: return "pluginFailed"
case .configurationNotFound: return "configurationNotFound"
case .pluginDisabled: return "pluginDisabled"
case .negotiationFailed: return "negotiationFailed"
case .serverDisconnected: return "serverDisconnected"
case .serverCertificateInvalid: return "serverCertificateInvalid"
case .serverCertificateNotYetValid: return "serverCertificateNotYetValid"
case .serverCertificateExpired: return "serverCertificateExpired"
default: return error.localizedDescription
}
} else {
return error.localizedDescription
}
}

return "none"
}()

continuation.resume(returning: message)
}
do {
try await tunnelManager.connection.fetchLastDisconnectError()
return nil
} catch {
return (error as NSError).toMetadataError()
}
}

return "none"
return nil
}

func collectVPNSettingsState() -> VPNMetadata.VPNSettingsState {
Expand Down Expand Up @@ -319,3 +295,23 @@ extension VPNMetadata.PrivacyProInfo.Source {
}
}
}

private extension NSError {

@available(iOS 16.0, *)
func toMetadataError() -> (error: VPNMetadata.LastDisconnectError, underlyingErrors: [VPNMetadata.LastDisconnectError]) {
let metadataError = VPNMetadata.LastDisconnectError(domain: self.domain, code: self.code, description: self.localizedDescription)

let underlyingErrors = self.underlyingErrors.compactMap { underlyingError in
let underlyingNSError = underlyingError as NSError
return VPNMetadata.LastDisconnectError(
domain: underlyingNSError.domain,
code: underlyingNSError.code,
description: underlyingNSError.localizedDescription
)
}

return (metadataError, underlyingErrors)
}

}

0 comments on commit 0c972d1

Please sign in to comment.