use crate::error::ValidationIssue;
use crate::profiles::chrome_linux::chrome_linux;
use crate::profiles::chrome_macos::chrome_macos;
use crate::profiles::chrome_windows::chrome_windows;
use crate::profiles::{
BrowserProfile, ColorGamut, ColorScheme, ForcedColors, MediaFeaturesConfig, NavigatorProfile,
ReducedMotion, ScreenConfig, TouchConfig, UserAgentClientHintsProfile,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlatformProfile {
Linux,
MacOS,
Windows,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Patch {
Identity,
Locale,
Timezone,
Screen,
MediaFeatures,
Touch,
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum PatchMode<T> {
Native,
Override(T),
Disabled,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatchState {
Native,
Override,
Disabled,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ConsistencyPolicy {
#[default]
Strict,
Warn,
Permissive,
}
impl PlatformProfile {
pub fn profile(self) -> BrowserProfile {
match self {
Self::Linux => chrome_linux(),
Self::MacOS => chrome_macos(),
Self::Windows => chrome_windows(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct StealthConfig {
identity: PatchMode<NavigatorProfile>,
locale: PatchMode<String>,
timezone: PatchMode<String>,
screen: PatchMode<ScreenConfig>,
media_features: PatchMode<MediaFeaturesConfig>,
touch: PatchMode<TouchConfig>,
policy: ConsistencyPolicy,
defaults: BrowserProfile,
}
impl StealthConfig {
pub fn none() -> Self {
let defaults = PlatformProfile::Linux.profile();
Self {
identity: PatchMode::Disabled,
locale: PatchMode::Disabled,
timezone: PatchMode::Disabled,
screen: PatchMode::Disabled,
media_features: PatchMode::Disabled,
touch: PatchMode::Disabled,
policy: ConsistencyPolicy::Strict,
defaults,
}
}
pub fn for_platform(platform: PlatformProfile) -> Self {
Self::from_profile(platform.profile())
}
pub fn from_profile(profile: BrowserProfile) -> Self {
Self {
identity: PatchMode::Override(profile.navigator.clone()),
locale: PatchMode::Override(profile.locale.locale.clone()),
timezone: PatchMode::Override(profile.locale.timezone.clone()),
screen: PatchMode::Override((&profile.screen).into()),
media_features: PatchMode::Override((&profile.device_environment).into()),
touch: PatchMode::Override((&profile.device_environment).into()),
policy: ConsistencyPolicy::Strict,
defaults: profile,
}
}
pub const fn consistency_policy(mut self, policy: ConsistencyPolicy) -> Self {
self.policy = policy;
self
}
pub fn enable(mut self, patch: Patch) -> Self {
match patch {
Patch::Identity => self.identity = PatchMode::Override(self.defaults.navigator.clone()),
Patch::Locale => self.locale = PatchMode::Override(self.defaults.locale.locale.clone()),
Patch::Timezone => {
self.timezone = PatchMode::Override(self.defaults.locale.timezone.clone());
}
Patch::Screen => self.screen = PatchMode::Override((&self.defaults.screen).into()),
Patch::MediaFeatures => {
self.media_features =
PatchMode::Override((&self.defaults.device_environment).into());
}
Patch::Touch => {
self.touch = PatchMode::Override((&self.defaults.device_environment).into());
}
}
self
}
pub fn disable(mut self, patch: Patch) -> Self {
match patch {
Patch::Identity => self.identity = PatchMode::Disabled,
Patch::Locale => self.locale = PatchMode::Disabled,
Patch::Timezone => self.timezone = PatchMode::Disabled,
Patch::Screen => self.screen = PatchMode::Disabled,
Patch::MediaFeatures => self.media_features = PatchMode::Disabled,
Patch::Touch => self.touch = PatchMode::Disabled,
}
self
}
pub fn use_native(mut self, patch: Patch) -> Self {
match patch {
Patch::Identity => self.identity = PatchMode::Native,
Patch::Locale => self.locale = PatchMode::Native,
Patch::Timezone => self.timezone = PatchMode::Native,
Patch::Screen => self.screen = PatchMode::Native,
Patch::MediaFeatures => self.media_features = PatchMode::Native,
Patch::Touch => self.touch = PatchMode::Native,
}
self
}
pub fn identity(mut self, identity: NavigatorProfile) -> Self {
self.identity = PatchMode::Override(identity);
self
}
pub fn identity_patch(mut self, mode: PatchMode<NavigatorProfile>) -> Self {
self.identity = mode;
self
}
pub fn client_hints(mut self, hints: Option<UserAgentClientHintsProfile>) -> Self {
self.identity_value_mut().client_hints = hints;
self
}
pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.identity_value_mut().user_agent = user_agent.into();
self
}
pub fn navigator_platform(mut self, platform: impl Into<String>) -> Self {
self.identity_value_mut().platform = platform.into();
self
}
pub fn languages<I, S>(mut self, languages: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.identity_value_mut().languages = languages.into_iter().map(Into::into).collect();
self
}
pub fn locale(mut self, locale: impl Into<String>) -> Self {
self.locale = PatchMode::Override(locale.into());
self
}
pub fn locale_patch(mut self, mode: PatchMode<String>) -> Self {
self.locale = mode;
self
}
pub fn timezone(mut self, timezone: impl Into<String>) -> Self {
self.timezone = PatchMode::Override(timezone.into());
self
}
pub fn timezone_patch(mut self, mode: PatchMode<String>) -> Self {
self.timezone = mode;
self
}
pub fn screen(mut self, screen: ScreenConfig) -> Self {
self.screen = PatchMode::Override(screen);
self
}
pub fn screen_patch(mut self, mode: PatchMode<ScreenConfig>) -> Self {
self.screen = mode;
self
}
pub fn screen_size(mut self, width: u32, height: u32) -> Self {
let screen = self.screen_value_mut();
screen.width = width;
screen.height = height;
self
}
pub fn device_scale_factor(mut self, factor: f64) -> Self {
self.screen_value_mut().device_scale_factor = factor;
self
}
pub fn media_features(mut self, media: MediaFeaturesConfig) -> Self {
self.media_features = PatchMode::Override(media);
self
}
pub fn media_features_patch(mut self, mode: PatchMode<MediaFeaturesConfig>) -> Self {
self.media_features = mode;
self
}
pub fn color_scheme(mut self, value: ColorScheme) -> Self {
self.media_features_value_mut().color_scheme = value;
self
}
pub fn reduced_motion(mut self, value: ReducedMotion) -> Self {
self.media_features_value_mut().reduced_motion = value;
self
}
pub fn forced_colors(mut self, value: ForcedColors) -> Self {
self.media_features_value_mut().forced_colors = value;
self
}
pub fn color_gamut(mut self, value: ColorGamut) -> Self {
self.media_features_value_mut().color_gamut = value;
self
}
pub fn touch(mut self, enabled: bool, max_touch_points: u32) -> Self {
self.touch = PatchMode::Override(TouchConfig {
enabled,
max_touch_points,
});
self
}
pub fn touch_patch(mut self, mode: PatchMode<TouchConfig>) -> Self {
self.touch = mode;
self
}
pub const fn policy(&self) -> ConsistencyPolicy {
self.policy
}
pub fn patch_state(&self, patch: Patch) -> PatchState {
match patch {
Patch::Identity => state(&self.identity),
Patch::Locale => state(&self.locale),
Patch::Timezone => state(&self.timezone),
Patch::Screen => state(&self.screen),
Patch::MediaFeatures => state(&self.media_features),
Patch::Touch => state(&self.touch),
}
}
pub fn plan(&self) -> PatchPlan {
let operations = [
Patch::Locale,
Patch::Timezone,
Patch::Identity,
Patch::Screen,
Patch::MediaFeatures,
Patch::Touch,
]
.into_iter()
.map(|patch| (patch, self.patch_state(patch)))
.collect();
PatchPlan {
operations,
issues: crate::validation::validate_config(self),
}
}
pub fn validation_issues(&self) -> Vec<ValidationIssue> {
self.plan().issues
}
pub fn identity_override(&self) -> Option<&NavigatorProfile> {
override_value(&self.identity)
}
pub fn locale_override(&self) -> Option<&str> {
override_value(&self.locale).map(String::as_str)
}
pub fn timezone_override(&self) -> Option<&str> {
override_value(&self.timezone).map(String::as_str)
}
pub fn screen_override(&self) -> Option<&ScreenConfig> {
override_value(&self.screen)
}
pub fn media_features_override(&self) -> Option<&MediaFeaturesConfig> {
override_value(&self.media_features)
}
pub fn touch_override(&self) -> Option<&TouchConfig> {
override_value(&self.touch)
}
pub(crate) fn identity_mode(&self) -> &PatchMode<NavigatorProfile> {
&self.identity
}
pub(crate) fn locale_mode(&self) -> &PatchMode<String> {
&self.locale
}
pub(crate) fn timezone_mode(&self) -> &PatchMode<String> {
&self.timezone
}
pub(crate) fn screen_mode(&self) -> &PatchMode<ScreenConfig> {
&self.screen
}
pub(crate) fn media_features_mode(&self) -> &PatchMode<MediaFeaturesConfig> {
&self.media_features
}
pub(crate) fn touch_mode(&self) -> &PatchMode<TouchConfig> {
&self.touch
}
fn identity_value_mut(&mut self) -> &mut NavigatorProfile {
if !matches!(self.identity, PatchMode::Override(_)) {
self.identity = PatchMode::Override(self.defaults.navigator.clone());
}
let PatchMode::Override(value) = &mut self.identity else {
unreachable!("identity was replaced with an override")
};
value
}
fn screen_value_mut(&mut self) -> &mut ScreenConfig {
if !matches!(self.screen, PatchMode::Override(_)) {
self.screen = PatchMode::Override((&self.defaults.screen).into());
}
let PatchMode::Override(value) = &mut self.screen else {
unreachable!("screen was replaced with an override")
};
value
}
fn media_features_value_mut(&mut self) -> &mut MediaFeaturesConfig {
if !matches!(self.media_features, PatchMode::Override(_)) {
self.media_features = PatchMode::Override((&self.defaults.device_environment).into());
}
let PatchMode::Override(value) = &mut self.media_features else {
unreachable!("media features were replaced with an override")
};
value
}
}
fn state<T>(mode: &PatchMode<T>) -> PatchState {
match mode {
PatchMode::Native => PatchState::Native,
PatchMode::Override(_) => PatchState::Override,
PatchMode::Disabled => PatchState::Disabled,
}
}
fn override_value<T>(mode: &PatchMode<T>) -> Option<&T> {
match mode {
PatchMode::Override(value) => Some(value),
PatchMode::Native | PatchMode::Disabled => None,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ApplyReport {
pub(crate) applied: Vec<Patch>,
pub(crate) skipped: Vec<Patch>,
pub(crate) native: Vec<Patch>,
pub(crate) warnings: Vec<ValidationIssue>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchPlan {
operations: Vec<(Patch, PatchState)>,
issues: Vec<ValidationIssue>,
}
impl PatchPlan {
pub fn operations(&self) -> &[(Patch, PatchState)] {
&self.operations
}
pub fn issues(&self) -> &[ValidationIssue] {
&self.issues
}
}
impl ApplyReport {
pub fn applied(&self) -> &[Patch] {
&self.applied
}
pub fn skipped(&self) -> &[Patch] {
&self.skipped
}
pub fn native(&self) -> &[Patch] {
&self.native
}
pub fn warnings(&self) -> &[ValidationIssue] {
&self.warnings
}
}