use super::transport::{persona_transport_coherence, TransportIncoherence};
use crate::fingerprint::{
os_network_coherence, os_network_stack, NetworkOsCoherence, StealthProfile, UserAgentPlatform,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PersonaIncoherence {
Browser(String),
Transport(TransportIncoherence),
}
impl std::fmt::Display for PersonaIncoherence {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Browser(msg) => write!(f, "persona JS-surface incoherence: {msg}"),
Self::Transport(t) => write!(f, "persona transport incoherence: {t:?}"),
}
}
}
impl std::error::Error for PersonaIncoherence {}
#[must_use = "a coherence verdict that is ignored gates nothing"]
pub fn persona_full_stack_coherence(profile: StealthProfile) -> Result<(), PersonaIncoherence> {
full_stack_coherence_of(profile, &crate::fingerprint::profile_to_overrides(&profile))
}
pub(crate) fn full_stack_coherence_of(
profile: StealthProfile,
overrides: &crate::fingerprint::ProfileOverrides,
) -> Result<(), PersonaIncoherence> {
crate::fingerprint::bundle::validate_overrides(overrides)
.map_err(|e| PersonaIncoherence::Browser(e.to_string()))?;
persona_transport_coherence(profile).map_err(PersonaIncoherence::Transport)?;
Ok(())
}
#[must_use]
pub fn host_platform() -> UserAgentPlatform {
match std::env::consts::OS {
"linux" => UserAgentPlatform::Linux,
"macos" => UserAgentPlatform::MacOs,
"windows" => UserAgentPlatform::Windows,
"android" => UserAgentPlatform::Android,
"ios" => UserAgentPlatform::Ios,
_ => UserAgentPlatform::Unknown,
}
}
#[must_use]
pub fn host_initial_ttl() -> Option<u8> {
os_network_stack(host_platform()).map(|stack| stack.initial_ttl)
}
#[must_use]
pub fn configured_host_initial_ttl() -> Option<u8> {
#[cfg(target_os = "linux")]
{
let raw = std::fs::read_to_string("/proc/sys/net/ipv4/ip_default_ttl").ok()?;
raw.trim().parse::<u8>().ok()
}
#[cfg(not(target_os = "linux"))]
{
None
}
}
#[must_use]
pub fn persona_host_network_coherence(profile: StealthProfile) -> NetworkOsCoherence {
match host_initial_ttl() {
Some(ttl) => os_network_coherence(profile, ttl),
None => NetworkOsCoherence::Unknown,
}
}