use super::profiles::{pair_for_profile, H2Profile, HeaderOrder};
use crate::fingerprint::{
profile_os_network_stack, Ja4tError, OsNetworkStack, StealthProfile, UserAgentPlatform,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransportCoherence {
pub network: OsNetworkStack,
pub h2: H2Profile,
pub header_order: HeaderOrder,
}
impl TransportCoherence {
pub fn ja4t(&self) -> Result<String, Ja4tError> {
self.network.ja4t()
}
}
#[must_use]
pub fn transport_coherence_for_profile(profile: StealthProfile) -> Option<TransportCoherence> {
let (header_order, h2) = pair_for_profile(profile)?;
Some(TransportCoherence {
network: profile_os_network_stack(profile),
h2,
header_order,
})
}
#[must_use]
pub fn transport_family_for_browser(
browser: crate::fingerprint::UserAgentBrowser,
) -> Option<&'static str> {
use crate::fingerprint::UserAgentBrowser::{
Chrome, Edge, Firefox, InternetExplorer, Opera, Safari, SamsungInternet, Unknown,
};
match browser {
Chrome | Edge | Opera | SamsungInternet => Some("chrome"),
Firefox => Some("firefox"),
Safari => Some("safari"),
InternetExplorer | Unknown => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransportIncoherence {
Os {
ua_os: UserAgentPlatform,
tcp_os: UserAgentPlatform,
},
HttpFamily {
h2: &'static str,
header_order: &'static str,
},
BrowserFamily {
browser_family: &'static str,
http_family: &'static str,
},
TlsFamily {
tls_family: &'static str,
http_family: &'static str,
},
}
pub fn persona_transport_coherence(profile: StealthProfile) -> Result<(), TransportIncoherence> {
let ua_os = crate::fingerprint::profile_platform(profile);
let tcp_os = profile_os_network_stack(profile).os;
if ua_os != tcp_os {
return Err(TransportIncoherence::Os { ua_os, tcp_os });
}
if let Some((header_order, h2)) = pair_for_profile(profile) {
if h2.family != header_order.family {
return Err(TransportIncoherence::HttpFamily {
h2: h2.family,
header_order: header_order.family,
});
}
let browser =
crate::fingerprint::user_agent_facts(crate::fingerprint::profile_user_agent(profile))
.browser;
if let Some(expected) = transport_family_for_browser(browser) {
if expected != h2.family {
return Err(TransportIncoherence::BrowserFamily {
browser_family: expected,
http_family: h2.family,
});
}
}
#[cfg(feature = "http")]
{
use crate::fingerprint::tls_profiles::{
default_impersonate_profile_for_stealth_profile, impersonate_profile_family,
};
let tls = default_impersonate_profile_for_stealth_profile(profile);
if let Some(tls_family) = impersonate_profile_family(tls) {
if tls_family != h2.family {
return Err(TransportIncoherence::TlsFamily {
tls_family,
http_family: h2.family,
});
}
}
}
}
Ok(())
}