Skip to content

Commit

Permalink
Report path change
Browse files Browse the repository at this point in the history
  • Loading branch information
quanganhdo committed Jan 10, 2024
1 parent 860dc93 commit 3d263b2
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions DuckDuckGo/Feedback/VPNMetadataCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ final class DefaultVPNMetadataCollector: VPNMetadataCollector {
private let statusObserver: ConnectionStatusObserver
private let serverInfoObserver: ConnectionServerInfoObserver
private let errorStore: NetworkProtectionTunnelErrorStore
private let healthStore: NetworkProtectionTunnelHealthStore

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

func collectMetadata() async -> VPNMetadata {
Expand Down Expand Up @@ -143,17 +146,27 @@ final class DefaultVPNMetadataCollector: VPNMetadataCollector {
var path: Network.NWPath?
let startTime = CFAbsoluteTimeGetCurrent()

let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar.current
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

while true {
if !monitor.currentPath.availableInterfaces.isEmpty {
path = monitor.currentPath
healthStore.updateNetworkPath(path, updatesTimestamp: false)
monitor.cancel()
return .init(currentPath: path.debugDescription, lastPathChangeDate: "none", lastPathChange: "none")
return .init(currentPath: path.debugDescription,
lastPathChangeDate: dateFormatter.string(from: healthStore.lastNetworkPathChangeDate),
lastPathChange: healthStore.lastNetworkPathChange)
}

// Wait up to 3 seconds to fetch the path.
let currentExecutionTime = CFAbsoluteTimeGetCurrent() - startTime
if currentExecutionTime >= 3.0 {
return .init(currentPath: "Timed out fetching path", lastPathChangeDate: "none", lastPathChange: "none")
return .init(currentPath: "Timed out fetching path",
lastPathChangeDate: dateFormatter.string(from: healthStore.lastNetworkPathChangeDate),
lastPathChange: healthStore.lastNetworkPathChange)
}
}
}
Expand Down

0 comments on commit 3d263b2

Please sign in to comment.