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

Warn when conflicting browser extension detected #253

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
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
56 changes: 56 additions & 0 deletions WakaTime/Helpers/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,62 @@ class Dependencies {
Bundle.main.version == "local-build"
}

public static func recentBrowserExtension() async -> String? {
guard
let apiKey = ConfigFile.getSetting(section: "settings", key: "api_key"),
!apiKey.isEmpty
else { return nil }
let url = "https://api.wakatime.com/api/v1/users/current/user_agents?api_key=\(apiKey)"
let request = URLRequest(url: URL(string: url)!, cachePolicy: .reloadIgnoringCacheData)
do {
let (data, response) = try await URLSession.shared.data(for: request)
guard
let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200
else { return nil }

struct Resp: Decodable {
let data: [UserAgent]
}
struct UserAgent: Decodable {
let isBrowserExtension: Bool
let editor: String?
let lastSeenAt: String?
enum CodingKeys: String, CodingKey {
case isBrowserExtension = "is_browser_extension"
case editor
case lastSeenAt = "last_seen_at"
}
}

let release = try JSONDecoder().decode(Resp.self, from: data)
let now = Date()
for agent in release.data {
guard
agent.isBrowserExtension,
let editor = agent.editor,
!editor.isEmpty,
let lastSeenAt = agent.lastSeenAt
else { continue }

let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
isoDateFormatter.formatOptions = [.withInternetDateTime]
if let lastSeen = isoDateFormatter.date(from: lastSeenAt) {
if now.timeIntervalSince(lastSeen) > 600 {
break
}
}

return agent.editor
}
} catch {
Logging.default.log("Request error checking for conflicting browser extension: \(error)")
return nil
}
return nil
}

private static func getLatestVersion() async throws -> String? {
struct Release: Decodable {
let tagName: String
Expand Down
10 changes: 10 additions & 0 deletions WakaTime/WakaTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class WakaTime: HeartbeatEventHandler {
}
PropertiesManager.hasLaunchedBefore = true
}

if MonitoringManager.isMonitoringBrowsing {
Task {
if let browser = await Dependencies.recentBrowserExtension() {
delegate.toastNotification("Warning: WakaTime \(browser) extension detected. " +
"It’s recommended to only track browsing activity with the \(browser) " +
"extension or Mac Desktop app, but not both.")
}
}
}
}

private func configureFirebase() {
Expand Down
Loading