use super::profiles::pair_for_profile;
use crate::fingerprint::{profile_os_network_stack, StealthProfile, UserAgentPlatform};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct WireCapture {
pub observed_ttl: Option<u8>,
pub akamai_fingerprint: Option<String>,
pub observed_ja4t: Option<String>,
}
impl WireCapture {
#[must_use]
pub fn is_empty(&self) -> bool {
self.observed_ttl.is_none()
&& self.akamai_fingerprint.is_none()
&& self.observed_ja4t.is_none()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WireLayerMismatch {
Ttl {
expected_os: UserAgentPlatform,
expected_initial_ttl: u8,
observed_initial_ttl: u8,
},
Akamai {
expected: String,
observed: String,
},
Ja4t {
expected: String,
observed: String,
},
}
impl WireLayerMismatch {
#[must_use]
pub fn akamai_field_divergences(
&self,
) -> Option<Vec<crate::fingerprint::akamai_h2::AkamaiH2Divergence>> {
let Self::Akamai { expected, observed } = self else {
return None;
};
use crate::fingerprint::akamai_h2::AkamaiH2Fingerprint;
let expected = AkamaiH2Fingerprint::parse(expected).ok()?;
let observed = AkamaiH2Fingerprint::parse(observed).ok()?;
Some(observed.diff(&expected))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WireSelfProbe {
Coherent,
Incoherent(Vec<WireLayerMismatch>),
Unmeasured,
}
impl WireSelfProbe {
#[must_use]
pub const fn is_coherent(&self) -> bool {
matches!(self, Self::Coherent)
}
}
#[must_use]
pub fn persona_wire_self_probe(profile: StealthProfile, capture: &WireCapture) -> WireSelfProbe {
let mut mismatches = Vec::new();
let mut compared = 0_usize;
if let Some(observed_ttl) = capture.observed_ttl {
let observed_initial = crate::fingerprint::infer_initial_ttl(observed_ttl);
if observed_initial != 0 {
compared += 1;
let stack = profile_os_network_stack(profile);
if stack.initial_ttl != observed_initial {
mismatches.push(WireLayerMismatch::Ttl {
expected_os: stack.os,
expected_initial_ttl: stack.initial_ttl,
observed_initial_ttl: observed_initial,
});
}
}
}
if let Some(observed_akamai) = capture.akamai_fingerprint.as_deref() {
if let Some((_, h2)) = pair_for_profile(profile) {
compared += 1;
let expected = h2.akamai_fingerprint();
if expected != observed_akamai {
mismatches.push(WireLayerMismatch::Akamai {
expected,
observed: observed_akamai.to_string(),
});
}
}
}
if let Some(observed_ja4t) = capture.observed_ja4t.as_deref() {
compared += 1;
let stack = profile_os_network_stack(profile);
if !stack.ja4t_matches_observed(observed_ja4t) {
let expected = stack
.ja4t()
.unwrap_or_else(|_| "<unrenderable-ja4t>".to_string());
mismatches.push(WireLayerMismatch::Ja4t {
expected,
observed: observed_ja4t.to_string(),
});
}
}
if compared == 0 {
WireSelfProbe::Unmeasured
} else if mismatches.is_empty() {
WireSelfProbe::Coherent
} else {
WireSelfProbe::Incoherent(mismatches)
}
}