Skip to content

Commit

Permalink
Add secondsSincePathChange
Browse files Browse the repository at this point in the history
  • Loading branch information
quanganhdo committed Jan 15, 2024
1 parent a36034c commit f1fbd34
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions DuckDuckGo/Feedback/VPNMetadataCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct VPNMetadata: Encodable {
let currentPath: String
let lastPathChangeDate: String
let lastPathChange: String
let secondsSincePathChange: TimeInterval
}

struct VPNState: Encodable {
Expand Down Expand Up @@ -97,14 +98,11 @@ protocol VPNMetadataCollector {
final class DefaultVPNMetadataCollector: VPNMetadataCollector {
private let statusObserver: ConnectionStatusObserver
private let serverInfoObserver: ConnectionServerInfoObserver
private let errorStore: NetworkProtectionTunnelErrorStore

init(statusObserver: ConnectionStatusObserver = ConnectionStatusObserverThroughSession(),
serverInfoObserver: ConnectionServerInfoObserver = ConnectionServerInfoObserverThroughSession(),
errorStore: NetworkProtectionTunnelErrorStore = NetworkProtectionTunnelErrorStore()) {
serverInfoObserver: ConnectionServerInfoObserver = ConnectionServerInfoObserverThroughSession()) {
self.statusObserver = statusObserver
self.serverInfoObserver = serverInfoObserver
self.errorStore = errorStore
}

func collectMetadata() async -> VPNMetadata {
Expand Down Expand Up @@ -150,22 +148,28 @@ final class DefaultVPNMetadataCollector: VPNMetadataCollector {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

let networkPathChange = VPNSettings(defaults: .networkProtectionGroupDefaults).networkPathChange
let now = Date()

while true {
if !monitor.currentPath.availableInterfaces.isEmpty {
path = monitor.currentPath
monitor.cancel()

let changeDate = networkPathChange?.date ?? .distantPast
return .init(currentPath: path.debugDescription,
lastPathChangeDate: dateFormatter.string(from: networkPathChange?.date ?? .distantPast),
lastPathChange: String(describing: networkPathChange))
lastPathChangeDate: dateFormatter.string(from: changeDate),
lastPathChange: String(describing: networkPathChange),
secondsSincePathChange: now.timeIntervalSince(changeDate))
}

// Wait up to 3 seconds to fetch the path.
let currentExecutionTime = CFAbsoluteTimeGetCurrent() - startTime
if currentExecutionTime >= 3.0 {
let changeDate = networkPathChange?.date ?? .distantPast
return .init(currentPath: "Timed out fetching path",
lastPathChangeDate: dateFormatter.string(from: networkPathChange?.date ?? .distantPast),
lastPathChange: String(describing: networkPathChange))
lastPathChangeDate: dateFormatter.string(from: changeDate),
lastPathChange: String(describing: networkPathChange),
secondsSincePathChange: now.timeIntervalSince(changeDate))
}
}
}
Expand Down

0 comments on commit f1fbd34

Please sign in to comment.