-
Notifications
You must be signed in to change notification settings - Fork 425
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
Implement config changes to UA #2037
Changes from 1 commit
0b88ec5
d70da24
f86b67c
8276f59
cb75566
e6d53bf
a1e761e
3e60d53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,8 +85,16 @@ public class DefaultUserAgentManager: UserAgentManager { | |
} | ||
|
||
struct UserAgent { | ||
|
||
private enum DefaultPolicy: String { | ||
|
||
case ddg | ||
case ddgFixed | ||
case closest | ||
|
||
} | ||
|
||
private struct Constants { | ||
private enum Constants { | ||
// swiftlint:disable line_length | ||
static let fallbackWekKitVersion = "605.1.15" | ||
static let fallbackSafariComponent = "Safari/\(fallbackWekKitVersion)" | ||
|
@@ -96,6 +104,10 @@ struct UserAgent { | |
|
||
static let uaOmitSitesConfigKey = "omitApplicationSites" | ||
static let uaOmitDomainConfigKey = "domain" | ||
|
||
static let defaultPolicyConfigKey = "defaultPolicy" | ||
static let ddgDefaultSitesConfigKey = "ddgDefaultSites" | ||
static let ddgFixedSitesConfigKey = "ddgFixedSites" | ||
// swiftlint:enable line_length | ||
} | ||
|
||
|
@@ -124,23 +136,101 @@ struct UserAgent { | |
|
||
return omitApplicationObjs.map { $0[Constants.uaOmitDomainConfigKey] ?? "" } | ||
} | ||
|
||
public func agent(forUrl url: URL?, isDesktop: Bool, | ||
|
||
private func defaultPolicy(forConfig config: PrivacyConfiguration) -> DefaultPolicy { | ||
let uaSettings = config.settings(for: .customUserAgent) | ||
guard let policy = uaSettings[Constants.defaultPolicyConfigKey] as? String else { return .ddg } | ||
|
||
return DefaultPolicy(rawValue: policy) ?? .ddg | ||
} | ||
|
||
private func ddgDefaultSites(forConfig config: PrivacyConfiguration) -> [String] { | ||
let uaSettings = config.settings(for: .customUserAgent) | ||
let defaultSitesObjs = uaSettings[Constants.ddgDefaultSitesConfigKey] as? [[String: String]] ?? [] | ||
|
||
return defaultSitesObjs.map { $0[Constants.uaOmitDomainConfigKey] ?? "" } | ||
} | ||
|
||
private func ddgFixedSites(forConfig config: PrivacyConfiguration) -> [String] { | ||
let uaSettings = config.settings(for: .customUserAgent) | ||
let fixedSitesObjs = uaSettings[Constants.ddgFixedSitesConfigKey] as? [[String: String]] ?? [] | ||
|
||
return fixedSitesObjs.map { $0[Constants.uaOmitDomainConfigKey] ?? "" } | ||
} | ||
|
||
public func agent(forUrl url: URL?, | ||
isDesktop: Bool, | ||
privacyConfig: PrivacyConfiguration = ContentBlocking.shared.privacyConfigurationManager.privacyConfig) -> String { | ||
|
||
guard privacyConfig.isEnabled(featureKey: .customUserAgent) else { return oldLogic(forUrl: url, isDesktop: isDesktop) } | ||
|
||
if ddgDefaultSites(forConfig: privacyConfig).contains(where: { domain in | ||
url?.isPart(ofDomain: domain) ?? false | ||
}) { return oldLogic(forUrl: url, isDesktop: isDesktop) } | ||
|
||
if ddgFixedSites(forConfig: privacyConfig).contains(where: { domain in | ||
url?.isPart(ofDomain: domain) ?? false | ||
}) { return ddgFixedLogic(isDesktop: isDesktop) } | ||
|
||
switch defaultPolicy(forConfig: privacyConfig) { | ||
case .ddg: return oldLogic(forUrl: url, isDesktop: isDesktop) | ||
case .ddgFixed: return ddgFixedLogic(isDesktop: isDesktop) | ||
case .closest: return closestLogic(forUrl: url, isDesktop: isDesktop) | ||
} | ||
|
||
} | ||
|
||
private func oldLogic(forUrl url: URL?, | ||
isDesktop: Bool, | ||
privacyConfig: PrivacyConfiguration = ContentBlocking.shared.privacyConfigurationManager.privacyConfig) -> String { | ||
let omittedSites = omitApplicationSites(forConfig: privacyConfig) | ||
let customUAEnabled = privacyConfig.isEnabled(featureKey: .customUserAgent) | ||
|
||
let omitApplicationComponent = !customUAEnabled || omittedSites.contains { domain in | ||
url?.isPart(ofDomain: domain) ?? false | ||
} | ||
|
||
let resolvedApplicationComponent = !omitApplicationComponent ? applicationComponent : nil | ||
|
||
if isDesktop { | ||
return concatWithSpaces(baseDesktopAgent, resolvedApplicationComponent, safariComponent) | ||
} else { | ||
return concatWithSpaces(baseAgent, resolvedApplicationComponent, safariComponent) | ||
} | ||
} | ||
|
||
private func closestLogic(forUrl url: URL?, | ||
isDesktop: Bool, | ||
privacyConfig: PrivacyConfiguration = ContentBlocking.shared.privacyConfigurationManager.privacyConfig) -> String { | ||
let customUAEnabled = privacyConfig.isEnabled(featureKey: .customUserAgent) | ||
let omittedSites = omitApplicationSites(forConfig: privacyConfig) | ||
let omitApplicationComponent = !customUAEnabled || omittedSites.contains { domain in | ||
url?.isPart(ofDomain: domain) ?? false | ||
} | ||
let resolvedApplicationComponent = !omitApplicationComponent ? applicationComponent : nil | ||
|
||
var defaultSafari = ddgFixedLogic(isDesktop: isDesktop) | ||
// If the UA should have DuckDuckGo append it prior to Safari | ||
if let resolvedApplicationComponent { | ||
if let index = defaultSafari.range(of: "Safari")?.lowerBound { | ||
defaultSafari.insert(contentsOf: resolvedApplicationComponent + " ", at: index) | ||
} | ||
} | ||
return defaultSafari | ||
} | ||
|
||
private func ddgFixedLogic(isDesktop: Bool) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should be the Maybe the confusion came in that this is now a fixed UA, but the fixed policy is about repairing known issues in our existing UA. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method shouldn't be used when the user is <16.6 also. |
||
if isDesktop { | ||
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15" | ||
} | ||
var deviceProfile = "iPhone; CPU iPhone OS 16_6 like Mac OS X" | ||
if baseAgent.contains("iPad") { | ||
deviceProfile = "iPad; CPU OS 16_6 like Mac OS X" | ||
} else if baseAgent.contains("iPod") { | ||
deviceProfile = "iPod touch; CPU iPhone 16_6 like Mac OS X" | ||
} | ||
return "Mozilla/5.0 (" + deviceProfile + ") AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1" | ||
} | ||
|
||
private func concatWithSpaces(_ elements: String?...) -> String { | ||
return elements | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the logic it turns out that
func closestLogic
is only called when thecustomUAEnabled
is enabled.