Skip to main content

utilvo_edge_shield/
lib.rs

1//! **Utilvo EdgeShield** (`v1.1.0`) — Server firewall generator, CIDR/ASN threat
2//! intelligence library (`AS132203`, `AS45102`, `AS136907`), and GA4 headless bot detector.
3//!
4//! Official Documentation & Live Threat DB: <https://utilvo.com/edge-shield>
5
6/// Canonical URL for the Utilvo EdgeShield documentation and interactive log analyzer.
7pub const DOCS_URL: &str = "https://utilvo.com/edge-shield";
8
9/// Canonical URL for the self-updating JSON Threat Intelligence API.
10pub const THREAT_API_URL: &str = "https://utilvo.com/api/edge-shield/threats";
11
12/// Result of inspecting an incoming HTTP request.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct BotInspectionResult {
15    pub is_bot: bool,
16    pub is_seo_crawler: bool,
17    pub reasons: Vec<&'static str>,
18}
19
20/// Inspects an IPv4 address and User-Agent string to detect rotating-IP cloud scrapers
21/// while whitelisting verified search engine crawlers.
22pub fn inspect_request(ip: &str, user_agent: &str) -> BotInspectionResult {
23    let ua_lower = user_agent.to_ascii_lowercase();
24    for seo in ["googlebot", "google-inspectiontool", "bingbot", "adidxbot", "applebot", "duckduckbot", "yandexbot"] {
25        if ua_lower.contains(seo) {
26            return BotInspectionResult {
27                is_bot: false,
28                is_seo_crawler: true,
29                reasons: Vec::new(),
30            };
31        }
32    }
33
34    let clean_ip = ip.split(',').next().unwrap_or("").trim();
35    let mut reasons = Vec::new();
36
37    if clean_ip.starts_with("43.13")
38        || clean_ip.starts_with("43.15")
39        || clean_ip.starts_with("43.16")
40        || clean_ip.starts_with("43.17")
41        || clean_ip.starts_with("47.74.")
42        || clean_ip.starts_with("47.82.")
43        || clean_ip.starts_with("101.32.")
44        || clean_ip.starts_with("101.33.")
45        || clean_ip.starts_with("159.138.")
46        || clean_ip.starts_with("149.232.")
47    {
48        reasons.push("CLOUD_SCRAPER_SUBNET");
49    }
50
51    for bad_ua in ["cpu iphone os 13_2_3", "tokenfoundrybot", "qlyzebot", "bytespider", "petalbot", "headlesschrome"] {
52        if ua_lower.contains(bad_ua) {
53            reasons.push("ROGUE_SCRAPER_USER_AGENT");
54            break;
55        }
56    }
57
58    BotInspectionResult {
59        is_bot: !reasons.is_empty(),
60        is_seo_crawler: false,
61        reasons,
62    }
63}
64
65/// Generates a Cloudflare WAF Custom Rule expression blocking high-abuse cloud scraper ASNs.
66pub fn generate_cloudflare_waf_expression() -> &'static str {
67    "(not cf.client.bot and (ip.src.asnum in {132203 45090 45102 37963 136907} or http.user_agent contains \"CPU iPhone OS 13_2_3\" or http.user_agent contains \"TokenFoundryBot\" or http.user_agent contains \"QlyzeBot\"))"
68}