Skip to main content

CoherencePort

Trait CoherencePort 

Source
pub trait CoherencePort:
    Send
    + Sync
    + 'static {
    // Required method
    fn evaluate(&self, ctx: &CoherenceContext) -> CoherenceVerdict;
}
Expand description

Network-identity coherence port.

Implementors decide whether ctx is safe to send through: a clean context returns CoherenceVerdict::Coherent, a disagreement on a specific vector returns CoherenceVerdict::Mismatch with the matching MismatchField and MismatchSeverity, and a missing observation (no DNS data, WebRTC disabled, …) returns CoherenceVerdict::Unknown.

Send + Sync + 'static so the implementation can live behind an Arc<dyn CoherencePort> on the manager. The default implementation in [crate::adapters::coherence::DefaultCoherenceValidator] is Send + Sync + 'static and stateless; an alternative adapter that needed caching would add a Mutex but the trait itself never requires one.

§Example

use std::net::IpAddr;
use std::str::FromStr;
use stygian_proxy::ports::coherence::{
    AcceptLanguage, CoherenceContext, CoherencePort, CoherenceVerdict,
    IsoCountry, Locale, MismatchField, MismatchSeverity, Tz,
};

// Custom adapter: only fail when proxy country disagrees with DNS.
struct StrictGeo;

impl CoherencePort for StrictGeo {
    fn evaluate(&self, ctx: &CoherenceContext) -> CoherenceVerdict {
        match (ctx.proxy_geo_country.as_ref(), ctx.dns_resolver_country.as_ref()) {
            (Some(proxy), Some(dns)) if proxy == dns => CoherenceVerdict::Coherent,
            (Some(_), Some(_)) => CoherenceVerdict::Mismatch {
                field: MismatchField::ProxyGeoVsDns,
                severity: MismatchSeverity::Hard,
            },
            _ => CoherenceVerdict::unknown("missing_geo"),
        }
    }
}

let ctx = CoherenceContext {
    proxy_geo_country: Some(IsoCountry::new("US").unwrap()),
    dns_resolver_country: Some(IsoCountry::new("PK").unwrap()),
    browser_locale: Locale::new("en-US").unwrap(),
    browser_timezone: Tz::new("America/New_York").unwrap(),
    accept_language: AcceptLanguage::new("en-US").unwrap(),
    webrtc_local_ip: None,
    webrtc_public_ip: Some(IpAddr::from_str("192.0.2.7").unwrap()),
    proxy_ip: Some(IpAddr::from_str("192.0.2.7").unwrap()),
};
let v = StrictGeo.evaluate(&ctx);
assert!(matches!(
    v,
    CoherenceVerdict::Mismatch {
        field: MismatchField::ProxyGeoVsDns,
        severity: MismatchSeverity::Hard,
    }
));

Required Methods§

Source

fn evaluate(&self, ctx: &CoherenceContext) -> CoherenceVerdict

Run the coherence check on ctx.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§