use super::*;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct ClientHintBrand {
pub brand: String,
pub version: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientHints {
pub brands: Vec<ClientHintBrand>,
pub full_version_list: Vec<ClientHintBrand>,
pub mobile: bool,
pub platform: String,
pub platform_version: String,
pub architecture: String,
pub bitness: String,
pub model: String,
pub ua_full_version: String,
pub wow64: bool,
}
#[must_use]
pub fn client_hints_from_overrides(overrides: &ProfileOverrides) -> Option<ClientHints> {
if overrides.brands.is_empty() {
return None;
}
let brands = overrides
.brands
.iter()
.map(|(brand, version)| ClientHintBrand {
brand: brand.clone(),
version: version.clone(),
})
.collect::<Vec<_>>();
let full_version_list = overrides
.brands
.iter()
.map(|(brand, version)| ClientHintBrand {
brand: brand.clone(),
version: full_version_for_brand(brand, version, &overrides.user_agent),
})
.collect::<Vec<_>>();
let ua_full_version = match preferred_ua_full_version(&full_version_list) {
Some(version) => version,
None => ua_full_version_from_user_agent(&overrides.user_agent)?,
};
Some(ClientHints {
brands,
full_version_list,
mobile: overrides.mobile,
platform: client_hint_platform(&overrides.platform),
platform_version: client_hint_platform_version(overrides),
architecture: client_hint_architecture(&overrides.platform).to_string(),
bitness: client_hint_bitness(&overrides.platform).to_string(),
model: client_hint_model(overrides),
ua_full_version,
wow64: false,
})
}
#[must_use]
pub fn profile_client_hints(profile: &StealthProfile) -> Option<ClientHints> {
client_hints_from_overrides(&profile_to_overrides(profile))
}
#[must_use]
pub fn client_hint_brands_json(overrides: &ProfileOverrides) -> String {
client_hints_from_overrides(overrides)
.map(|hints| json_array(&hints.brands))
.unwrap_or_else(|| "[]".into())
}
#[must_use]
pub fn client_hint_full_version_list_json(overrides: &ProfileOverrides) -> String {
client_hints_from_overrides(overrides)
.map(|hints| json_array(&hints.full_version_list))
.unwrap_or_else(|| "[]".into())
}
#[allow(clippy::panic)] pub(crate) fn json_array<T: serde::Serialize>(value: &T) -> String {
serde_json::to_string(value).unwrap_or_else(|e| {
panic!(
"guise: serializing persona fingerprint JSON failed ({e}); persona \
override data is crate-controlled and must always serialize"
)
})
}
fn client_hint_platform(platform: &str) -> String {
if platform.starts_with("Win") {
"Windows"
} else if platform.starts_with("Mac") {
"macOS"
} else if platform.starts_with("iPhone") || platform.starts_with("iPad") {
"iOS"
} else if platform.starts_with("Linux arm") {
"Android"
} else if platform.starts_with("Linux") {
"Linux"
} else {
"Unknown"
}
.to_string()
}
fn client_hint_platform_version(overrides: &ProfileOverrides) -> String {
if overrides.platform.starts_with("Win") {
"15.0.0".into()
} else if overrides.platform.starts_with("Mac") {
"14.0.0".into()
} else if overrides.platform.starts_with("Linux arm") {
android_version_from_ua(&overrides.user_agent).unwrap_or_else(|| "14.0.0".into())
} else {
String::new()
}
}
fn client_hint_architecture(platform: &str) -> &'static str {
if platform.starts_with("Linux arm") {
"arm"
} else {
"x86"
}
}
fn client_hint_bitness(platform: &str) -> &'static str {
if platform.starts_with("Linux arm") {
""
} else {
"64"
}
}
fn client_hint_model(overrides: &ProfileOverrides) -> String {
if overrides.platform.starts_with("Linux arm") {
android_model_from_ua(&overrides.user_agent).unwrap_or_default()
} else {
String::new()
}
}
fn full_version_for_brand(brand: &str, major_version: &str, ua: &str) -> String {
if major_version == "99" || brand.contains("Brand") {
return "99.0.0.0".into();
}
let token = match brand {
"Microsoft Edge" => "Edg/",
"Opera" => "OPR/",
"Samsung Internet" => "SamsungBrowser/",
"Chromium" | "Google Chrome" | "Brave" => "Chrome/",
_ => "",
};
if !token.is_empty() {
if let Some(version) = version_after_token(ua, token) {
return normalize_full_version(&version);
}
}
normalize_full_version(major_version)
}
fn preferred_ua_full_version(full_version_list: &[ClientHintBrand]) -> Option<String> {
for preferred in [
"Google Chrome",
"Microsoft Edge",
"Brave",
"Opera",
"Samsung Internet",
"Chromium",
] {
if let Some(entry) = full_version_list
.iter()
.find(|entry| entry.brand == preferred && entry.version != "99.0.0.0")
{
return Some(entry.version.clone());
}
}
full_version_list
.iter()
.find(|entry| entry.version != "99.0.0.0")
.map(|entry| entry.version.clone())
}
fn ua_full_version_from_user_agent(ua: &str) -> Option<String> {
version_after_token(ua, "Chrome/").map(|version| normalize_full_version(&version))
}
fn version_after_token(ua: &str, token: &str) -> Option<String> {
ua.split(token)
.nth(1)
.and_then(|rest| rest.split_whitespace().next())
.filter(|version| !version.is_empty())
.map(ToString::to_string)
}
fn normalize_full_version(version: &str) -> String {
let mut parts = version.split('.').collect::<Vec<_>>();
while parts.len() < 4 {
parts.push("0");
}
parts.truncate(4);
parts.join(".")
}
fn android_version_from_ua(ua: &str) -> Option<String> {
let version = ua
.split("Android ")
.nth(1)
.and_then(|rest| rest.split([';', ')']).next())
.map(str::trim)
.filter(|version| !version.is_empty())?;
Some(normalize_platform_version(version))
}
fn normalize_platform_version(version: &str) -> String {
let mut parts = version.split('.').collect::<Vec<_>>();
while parts.len() < 3 {
parts.push("0");
}
parts.truncate(3);
parts.join(".")
}
fn android_model_from_ua(ua: &str) -> Option<String> {
let segment = ua
.split('(')
.nth(1)
.and_then(|rest| rest.split(')').next())?;
let mut parts = segment.split(';').map(str::trim);
parts.find(|part| part.starts_with("Android "))?;
parts
.find(|part| !part.is_empty() && !part.starts_with("wv"))
.map(ToString::to_string)
}