use serde::{Deserialize, Serialize};
use super::{profile_to_overrides_at, StealthProfile};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NavigatorProfile {
pub user_agent: String,
pub platform: String,
pub vendor: String,
pub screen_width: u32,
pub screen_height: u32,
pub color_depth: u8,
pub device_memory: u8,
pub hardware_concurrency: u8,
pub timezone: String,
pub locale: String,
pub languages: Vec<String>,
pub latitude: f64,
pub longitude: f64,
pub webgl_vendor: String,
pub webgl_renderer: String,
pub canvas_seed: f64,
pub do_not_track: Option<String>,
}
#[derive(Debug, Clone, Copy)]
struct NavigatorProfileTemplate {
stealth_profile: StealthProfile,
hardware_index: usize,
timezone: &'static str,
locale: &'static str,
languages: &'static [&'static str],
latitude: f64,
longitude: f64,
canvas_seed: f64,
do_not_track: Option<&'static str>,
}
impl NavigatorProfileTemplate {
fn materialize(self) -> NavigatorProfile {
let overrides = profile_to_overrides_at(&self.stealth_profile, self.hardware_index);
NavigatorProfile {
user_agent: overrides.user_agent,
platform: overrides.platform,
vendor: overrides.navigator_vendor,
screen_width: overrides.screen_width,
screen_height: overrides.screen_height,
color_depth: overrides.color_depth,
device_memory: overrides.device_memory as u8,
hardware_concurrency: overrides.hardware_concurrency as u8,
timezone: self.timezone.to_string(),
locale: self.locale.to_string(),
languages: self
.languages
.iter()
.map(|language| (*language).to_string())
.collect(),
latitude: self.latitude,
longitude: self.longitude,
webgl_vendor: overrides.webgl_vendor,
webgl_renderer: overrides.webgl_renderer,
canvas_seed: self.canvas_seed,
do_not_track: self.do_not_track.map(str::to_string),
}
}
}
const PROFILES: &[NavigatorProfileTemplate] = &[
NavigatorProfileTemplate {
stealth_profile: StealthProfile::ChromeWindowsStable,
hardware_index: 1,
timezone: "America/New_York",
locale: "en-US",
languages: &["en-US", "en"],
latitude: 40.7128,
longitude: -74.006,
canvas_seed: 0.3847,
do_not_track: None,
},
NavigatorProfileTemplate {
stealth_profile: StealthProfile::ChromeWindowsStable,
hardware_index: 2,
timezone: "America/Los_Angeles",
locale: "en-US",
languages: &["en-US", "en"],
latitude: 37.7749,
longitude: -122.4194,
canvas_seed: -0.6291,
do_not_track: None,
},
NavigatorProfileTemplate {
stealth_profile: StealthProfile::ChromeWindowsStable,
hardware_index: 3,
timezone: "Europe/London",
locale: "en-GB",
languages: &["en-GB", "en"],
latitude: 51.5074,
longitude: -0.1278,
canvas_seed: 0.1523,
do_not_track: Some("1"),
},
NavigatorProfileTemplate {
stealth_profile: StealthProfile::ChromeMacStable,
hardware_index: 0,
timezone: "America/Chicago",
locale: "en-US",
languages: &["en-US", "en"],
latitude: 41.8781,
longitude: -87.6298,
canvas_seed: -0.4102,
do_not_track: None,
},
NavigatorProfileTemplate {
stealth_profile: StealthProfile::ChromeLinux,
hardware_index: 1,
timezone: "Europe/Berlin",
locale: "de-DE",
languages: &["de-DE", "de", "en"],
latitude: 52.52,
longitude: 13.405,
canvas_seed: 0.7834,
do_not_track: Some("1"),
},
NavigatorProfileTemplate {
stealth_profile: StealthProfile::ChromeWindowsStable,
hardware_index: 4,
timezone: "Asia/Tokyo",
locale: "ja-JP",
languages: &["ja", "en-US", "en"],
latitude: 35.6762,
longitude: 139.6503,
canvas_seed: -0.2519,
do_not_track: None,
},
];
pub fn random() -> NavigatorProfile {
crate::choice::random_item(PROFILES)
.unwrap_or(&PROFILES[0])
.materialize()
}
pub fn for_timezone(tz: &str) -> NavigatorProfile {
for profile in PROFILES {
if profile.timezone == tz {
return profile.materialize();
}
}
let region = tz.split('/').next().unwrap_or("");
for profile in PROFILES {
if profile.timezone.starts_with(region) {
return profile.materialize();
}
}
random()
}
pub fn by_index(idx: usize) -> NavigatorProfile {
PROFILES[idx % PROFILES.len()].materialize()
}
pub fn profile_count() -> usize {
PROFILES.len()
}
#[cfg(test)]
mod tests {
use super::super::{profile_hardware_at, profile_to_overrides};
use super::*;
#[test]
fn all_profiles_materialize() {
for (i, profile) in PROFILES.iter().enumerate() {
let id = profile.materialize();
assert!(!id.user_agent.is_empty(), "profile {i} has empty UA");
assert!(!id.platform.is_empty(), "profile {i} has empty platform");
assert!(!id.timezone.is_empty(), "profile {i} has empty timezone");
assert!(id.screen_width > 0, "profile {i} has empty width");
assert!(id.screen_height > 0, "profile {i} has empty height");
}
}
#[test]
fn identity_user_agents_delegate_to_stealth_profiles() {
for profile in PROFILES {
let id = profile.materialize();
let overrides = profile_to_overrides(&profile.stealth_profile);
let hardware = profile_hardware_at(profile.stealth_profile, profile.hardware_index);
assert_eq!(
id.user_agent, overrides.user_agent,
"identity UA drifted from stealth profile {:?}",
profile.stealth_profile,
);
assert_eq!(
id.platform, overrides.platform,
"identity platform drifted from stealth profile {:?}",
profile.stealth_profile,
);
assert_eq!(
id.vendor, overrides.navigator_vendor,
"identity navigator vendor drifted from stealth profile {:?}",
profile.stealth_profile,
);
assert_eq!(id.screen_width, hardware.screen_width);
assert_eq!(id.screen_height, hardware.screen_height);
assert_eq!(id.color_depth, hardware.color_depth);
assert_eq!(id.device_memory, hardware.device_memory);
assert_eq!(id.hardware_concurrency, hardware.hardware_concurrency);
assert_eq!(id.webgl_vendor, hardware.webgl_vendor);
assert_eq!(id.webgl_renderer, hardware.webgl_renderer);
assert!(!id.user_agent.contains("Chrome/130."));
}
}
#[test]
fn random_returns_valid() {
let id = random();
assert!(id.user_agent.contains("Chrome"));
assert!(id.screen_width >= 1024);
}
#[test]
fn for_timezone_us_east() {
let id = for_timezone("America/New_York");
assert_eq!(id.timezone, "America/New_York");
}
#[test]
fn for_timezone_fallback() {
let id = for_timezone("Africa/Lagos");
assert!(!id.user_agent.is_empty());
}
#[test]
fn profiles_internally_consistent() {
for profile in PROFILES {
let id = profile.materialize();
if id.platform == "Win32" {
assert!(
id.user_agent.contains("Windows"),
"Win32 platform but no Windows UA"
);
}
if id.platform == "MacIntel" {
assert!(
id.user_agent.contains("Macintosh"),
"MacIntel platform but no Mac UA"
);
}
if id.platform == "Linux x86_64" {
assert!(
id.user_agent.contains("Linux"),
"Linux platform but no Linux UA"
);
}
assert!(!id.languages.is_empty(), "empty languages");
}
}
#[test]
fn by_index_deterministic() {
let a = by_index(0);
let b = by_index(0);
assert_eq!(a.user_agent, b.user_agent);
}
#[test]
fn profile_count_matches() {
assert_eq!(profile_count(), PROFILES.len());
}
}