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

Website breakage triage tools upgraded for efficient mitigation #1787

Merged
merged 8 commits into from
Nov 8, 2023
28 changes: 27 additions & 1 deletion DuckDuckGo/AppDelegate/AppConfigurationURLProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,40 @@ import Configuration

struct AppConfigurationURLProvider: ConfigurationURLProviding {

// MARK: - Debug

internal init(customPrivacyConfiguration: URL? = nil) {
if let customPrivacyConfiguration {
// Overwrite custom privacy configuration if provided
self.customPrivacyConfiguration = customPrivacyConfiguration.absoluteString
}
// Otherwise use the default or already stored custom configuration
}

@UserDefaultsWrapper(key: .customConfigurationUrl, defaultValue: nil)
private var customPrivacyConfiguration: String?

private var customPrivacyConfigurationUrl: URL? {
if let customPrivacyConfiguration {
return URL(string: customPrivacyConfiguration)
}
return nil
}

mutating func resetToDefaultConfigurationUrl() {
self.customPrivacyConfiguration = nil
}

// MARK: - Main

func url(for configuration: Configuration) -> URL {
// URLs for privacyConfiguration and trackerDataSet shall match the ones in update_embedded.sh.
// Danger checks that the URLs match on every PR. If the code changes, the regex that Danger uses may need an update.
switch configuration {
case .bloomFilterBinary: return URL(string: "https://staticcdn.duckduckgo.com/https/https-mobile-v2-bloom.bin")!
case .bloomFilterSpec: return URL(string: "https://staticcdn.duckduckgo.com/https/https-mobile-v2-bloom-spec.json")!
case .bloomFilterExcludedDomains: return URL(string: "https://staticcdn.duckduckgo.com/https/https-mobile-v2-false-positives.json")!
case .privacyConfiguration: return URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/config/v3/macos-config.json")!
case .privacyConfiguration: return customPrivacyConfigurationUrl ?? URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/config/v3/macos-config.json")!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Tom, Im not really fond of complicating that default AppConfigurationURLProvider, I think it should remain unchanged to our default values. Could you please take a look at how I tackled it in https://github.com/duckduckgo/iOS/compare/jacek/custom-config-url?expand=1 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaceklyp, in your PR, I see business logic in the View Controller. Also, the approach can lead to inconsistencies across the codebase in case CustomConfigurationURLProvider is initialized outside of the View Controller. If you think the storing logic should be outside of the AppConfigurationURLProvider, we can create a dedicated class for storing/providing of the custom URL and compose it with AppConfigurationURLProvider.swift.

What do you think?

case .surrogates: return URL(string: "https://staticcdn.duckduckgo.com/surrogates.txt")!
case .trackerDataSet: return URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/v5/current/macos-tds.json")!
// In archived repo, to be refactored shortly (https://staticcdn.duckduckgo.com/useragents/social_ctp_configuration.json)
Expand Down
10 changes: 10 additions & 0 deletions DuckDuckGo/Common/Extensions/NSAlertExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ extension NSAlert {
return alert
}

static func customConfigurationAlert() -> NSAlert {
let alert = NSAlert()
alert.messageText = "Set custom configuration URL:"
alert.addButton(withTitle: UserText.ok)
alert.addButton(withTitle: UserText.cancel)
alert.accessoryView = NSTextField(frame: NSRect(x: 0, y: 0, width: 300, height: 24))
alert.window.initialFirstResponder = alert.accessoryView
return alert
}

@discardableResult
func runModal() async -> NSApplication.ModalResponse {
await withCheckedContinuation { continuation in
Expand Down
1 change: 1 addition & 0 deletions DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public struct UserDefaultsWrapper<T> {
case loggingCategories = "logging.categories"

case firstLaunchDate = "first.app.launch.date"
case customConfigurationUrl = "custom.configuration.url"

// Network Protection

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Configuration/ConfigurationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ final class ConfigurationManager {
static let queue: DispatchQueue = DispatchQueue(label: "Configuration Manager")

@UserDefaultsWrapper(key: .configLastUpdated, defaultValue: .distantPast)
private var lastUpdateTime: Date
private(set) var lastUpdateTime: Date

private var timerCancellable: AnyCancellable?
private var lastRefreshCheckTime: Date = Date()
Expand Down
20 changes: 19 additions & 1 deletion DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Combine
import OSLog // swiftlint:disable:this enforce_os_log_wrapper
import SwiftUI
import WebKit
import Configuration

#if NETWORK_PROTECTION
import NetworkProtection
Expand Down Expand Up @@ -92,6 +93,8 @@ import Subscription
// MARK: - Debug

private var loggingMenu: NSMenu?
let customConfigurationUrlMenuItem = NSMenuItem(title: "Last Update Time", action: #selector(MainViewController.reloadConfigurationNow))
tomasstrba marked this conversation as resolved.
Show resolved Hide resolved
let configurationDateAndTimeMenuItem = NSMenuItem(title: "Configuration URL", action: #selector(MainViewController.reloadConfigurationNow))
tomasstrba marked this conversation as resolved.
Show resolved Hide resolved

// MARK: - Help

Expand Down Expand Up @@ -378,6 +381,7 @@ import Subscription
updateBookmarksBarMenuItem()
updateShortcutMenuItems()
updateLoggingMenuItems()
updateRemoteConfigurationInfo()
}

// MARK: - Bookmarks
Expand Down Expand Up @@ -546,7 +550,15 @@ import Subscription
NSMenuItem(title: "Show Pop Up Window", action: #selector(MainViewController.showPopUpWindow))
}
NSMenuItem(title: "Remote Configuration") {
NSMenuItem(title: "Fetch Configuration Now", action: #selector(MainViewController.fetchConfigurationNow))
customConfigurationUrlMenuItem
configurationDateAndTimeMenuItem
NSMenuItem.separator()
NSMenuItem(title: "Reload Configuration Now", action: #selector(MainViewController.reloadConfigurationNow))
NSMenuItem(title: "Set custom configuration URL…", action: #selector(MainViewController.setCustomConfigurationURL))
NSMenuItem(title: "Reset configuration to default", action: #selector(MainViewController.resetConfigurationToDefault))
}
NSMenuItem(title: "User Scripts") {
NSMenuItem(title: "Remove user scripts from selected tab", action: #selector(MainViewController.removeUserScripts))
}
NSMenuItem(title: "Sync")
.submenu(SyncDebugMenu())
Expand Down Expand Up @@ -607,6 +619,12 @@ import Subscription
}
}

private func updateRemoteConfigurationInfo() {
let dateString = DateFormatter.localizedString(from: ConfigurationManager.shared.lastUpdateTime, dateStyle: .short, timeStyle: .medium)
configurationDateAndTimeMenuItem.title = "Last Update Time: \(dateString)"
customConfigurationUrlMenuItem.title = "Configuration URL: \(AppConfigurationURLProvider().url(for: .privacyConfiguration).absoluteString)"
}

@objc private func loggingMenuItemAction(_ sender: NSMenuItem) {
guard let category = sender.identifier?.rawValue else { return }

Expand Down
40 changes: 39 additions & 1 deletion DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import BrowserServicesKit
import Cocoa
import Common
import WebKit
import Configuration

// Actions are sent to objects of responder chain

Expand Down Expand Up @@ -733,8 +734,45 @@ extension MainViewController {
EmailManager().resetEmailProtectionInContextPrompt()
}

@objc func fetchConfigurationNow(_ sender: Any?) {
@objc func removeUserScripts(_ sender: Any?) {
tabCollectionViewModel.selectedTab?.userContentController?.cleanUpBeforeClosing()
tabCollectionViewModel.selectedTab?.reload()
os_log("User scripts removed from the current tab", type: .info)
}

@objc func reloadConfigurationNow(_ sender: Any?) {
ConfigurationManager.shared.forceRefresh()
}

private func setConfigurationUrl(_ configurationUrl: URL?) {
var configurationProvider = AppConfigurationURLProvider(customPrivacyConfiguration: configurationUrl)
if configurationUrl == nil {
configurationProvider.resetToDefaultConfigurationUrl()
}
Configuration.setURLProvider(configurationProvider)
ConfigurationManager.shared.forceRefresh()
if let configurationUrl {
os_log("New configuration URL set to \(configurationUrl.absoluteString)", type: .info)
} else {
os_log("New configuration URL reset to default", type: .info)
}
}

@objc func setCustomConfigurationURL(_ sender: Any?) {
let alert = NSAlert.customConfigurationAlert()
tomasstrba marked this conversation as resolved.
Show resolved Hide resolved
if alert.runModal() != .cancel {
guard let textField = alert.accessoryView as? NSTextField,
let newConfigurationUrl = URL(string: textField.stringValue) else {
os_log("Failed to set custom configuration URL", type: .error)
return
}

setConfigurationUrl(newConfigurationUrl)
}
}

@objc func resetConfigurationToDefault(_ sender: Any?) {
setConfigurationUrl(nil)
}

// MARK: - Developer Tools
Expand Down