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

Support DDG branding in UA #2335

Merged
merged 3 commits into from
Jan 12, 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
42 changes: 31 additions & 11 deletions Core/UserAgentManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
// limitations under the License.
//

// swiftlint:disable file_length

import Foundation
import WebKit
import BrowserServicesKit
Expand Down Expand Up @@ -91,6 +93,7 @@ struct UserAgent {
case ddg
case ddgFixed
case closest
case brand

}

Expand Down Expand Up @@ -125,17 +128,21 @@ struct UserAgent {

private let baseAgent: String
private let baseDesktopAgent: String
private let version: String
private let versionComponent: String
private let safariComponent: String
private let brandComponent: String
private let applicationComponent = "DuckDuckGo/\(AppVersion.shared.majorVersionNumber)"
private let statistics: StatisticsStore
private let isTesting: Bool = ProcessInfo().arguments.contains("testing")

init(defaultAgent: String = Constants.fallbackDefaultAgent, statistics: StatisticsStore = StatisticsUserDefaults()) {
versionComponent = UserAgent.createVersionComponent(fromAgent: defaultAgent)
version = UserAgent.getVersion(fromAgent: defaultAgent)
versionComponent = UserAgent.createVersionComponent(withVersion: version)
baseAgent = UserAgent.createBaseAgent(fromAgent: defaultAgent, versionComponent: versionComponent)
baseDesktopAgent = UserAgent.createBaseDesktopAgent(fromAgent: defaultAgent, versionComponent: versionComponent)
safariComponent = UserAgent.createSafariComponent(fromAgent: baseAgent)
brandComponent = UserAgent.createBrandComponent(withVersion: version)
self.statistics = statistics
}

Expand Down Expand Up @@ -225,6 +232,15 @@ struct UserAgent {
} else {
return oldLogic(forUrl: url, isDesktop: isDesktop, privacyConfig: privacyConfig)
}
case .brand:
var ua: String
if canUseClosestLogic {
ua = closestLogic(forUrl: url, isDesktop: isDesktop, privacyConfig: privacyConfig)
} else {
ua = oldLogic(forUrl: url, isDesktop: isDesktop, privacyConfig: privacyConfig)
}
ua.append(" \(brandComponent)")
return ua
}
}

Expand Down Expand Up @@ -276,7 +292,7 @@ struct UserAgent {
if isDesktop {
return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15"
}
return "Mozilla/5.0 (" + deviceProfile + ") AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1"
return "Mozilla/5.0 (" + deviceProfile + ") AppleWebKit/605.1.15 (KHTML, like Gecko) \(versionComponent) Mobile/15E148 Safari/604.1"
}

private var canUseClosestLogic: Bool {
Expand Down Expand Up @@ -311,25 +327,27 @@ struct UserAgent {
.compactMap { $0 }
.joined(separator: " ")
}
private static func createVersionComponent(fromAgent agent: String) -> String {

private static func getVersion(fromAgent agent: String) -> String {
let regex = try? NSRegularExpression(pattern: Regex.osVersion)
let match = regex?.firstMatch(in: agent, options: [], range: NSRange(location: 0, length: agent.count))

guard let range = match?.range(at: 1) else {
return Constants.fallbackVersionComponent
}

let version = (agent as NSString).substring(with: range)
let versionComponents = version.split(separator: "_").prefix(2)

guard versionComponents.count > 1 else {
return Constants.fallbackVersionComponent
}
return "Version/\(versionComponents.joined(separator: "."))"

return versionComponents.joined(separator: ".")
}


private static func createVersionComponent(withVersion version: String) -> String { "Version/\(version)" }

private static func createSafariComponent(fromAgent agent: String) -> String {
let regex = try? NSRegularExpression(pattern: Regex.webKitVersion)
let match = regex?.firstMatch(in: agent, options: [], range: NSRange(location: 0, length: agent.count))
Expand All @@ -341,7 +359,9 @@ struct UserAgent {
let version = (agent as NSString).substring(with: range)
return "Safari/\(version)"
}


private static func createBrandComponent(withVersion version: String) -> String { "Ddg/\(version)" }

private static func createBaseAgent(fromAgent agent: String,
versionComponent: String) -> String {
var agentComponents = agent.split(separator: " ")
Expand Down
12 changes: 6 additions & 6 deletions DuckDuckGoTests/UserAgentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ final class UserAgentTests: XCTestCase {
static let tablet = "Mozilla/5.0 (iPad; CPU OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 DuckDuckGo/7 Safari/605.1.15"
static let desktop = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 DuckDuckGo/7 Safari/605.1.15"
static let oldWebkitVersionMobile = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.14 (KHTML, like Gecko) Version/12.4 Mobile/15E148 DuckDuckGo/7 Safari/605.1.14"
static let newWebkitVersionMobile = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 DuckDuckGo/7 Safari/604.1"
static let sameWebkitVersionMobile = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 DuckDuckGo/7 Safari/604.1"
static let newWebkitVersionMobile = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 DuckDuckGo/7 Safari/604.1"
static let sameWebkitVersionMobile = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 DuckDuckGo/7 Safari/604.1"

static let mobileNoApplication = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 Safari/605.1.15"

// Based on fallback constants in UserAgent
static let mobileFallback = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.5 Mobile/15E148 DuckDuckGo/7 Safari/605.1.15"
static let desktopFallback = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.5 DuckDuckGo/7 Safari/605.1.15"

static let mobileFixed = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 DuckDuckGo/7 Safari/604.1"
static let tabletFixed = "Mozilla/5.0 (iPad; CPU OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 DuckDuckGo/7 Safari/604.1"
static let mobileFixed = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 DuckDuckGo/7 Safari/604.1"
static let tabletFixed = "Mozilla/5.0 (iPad; CPU OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 DuckDuckGo/7 Safari/604.1"
static let desktopFixed = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 DuckDuckGo/7 Safari/605.1.15"

static let mobileClosest = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1"
static let tabletClosest = "Mozilla/5.0 (iPad; CPU OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Mobile/15E148 Safari/604.1"
static let mobileClosest = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 Safari/604.1"
static let tabletClosest = "Mozilla/5.0 (iPad; CPU OS 12_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.4 Mobile/15E148 Safari/604.1"
static let desktopClosest = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15"
// swiftlint:enable line_length

Expand Down
Loading