use aho_corasick::{AhoCorasick, AhoCorasickBuilder};
use once_cell::sync::Lazy;
use regex::Regex;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AntiBotTech {
Cloudflare,
Akamai,
PerimeterX,
DataDome,
Imperva,
Sucuri,
Kasada,
AwsWaf,
RateLimited,
StructuralBlock,
Unknown,
}
pub static TIER1_PATTERNS: &[(&str, AntiBotTech)] = &[
(r"(?i)/cdn-cgi/challenge-platform/", AntiBotTech::Cloudflare),
(r"(?i)cf-browser-verification", AntiBotTech::Cloudflare),
(r"(?i)cf-challenge", AntiBotTech::Cloudflare),
(r"(?i)just a moment[\.\.\.]", AntiBotTech::Cloudflare),
(r"(?i)__cf_bm", AntiBotTech::Cloudflare),
(r"(?i)cf-ray[:\s]", AntiBotTech::Cloudflare),
(
r"(?i)cloudflare[\s\-_]?browser[\s\-_]?check",
AntiBotTech::Cloudflare,
),
(r"(?i)reference\s*#(\d+)", AntiBotTech::Akamai),
(r"(?i)akamai_bmbl", AntiBotTech::Akamai),
(r"(?i)\b_abck\b", AntiBotTech::Akamai),
(r"(?i)\bak_bmsc\b", AntiBotTech::Akamai),
(r"(?i)\bbm_sv\b", AntiBotTech::Akamai),
(r"(?i)pardon our interruption", AntiBotTech::Akamai),
(r"(?i)px-captcha", AntiBotTech::PerimeterX),
(r"(?i)perimeterx", AntiBotTech::PerimeterX),
(r"(?i)_pxhd\b", AntiBotTech::PerimeterX),
(r"(?i)px-cdn\.net", AntiBotTech::PerimeterX),
(r"(?i)datadome", AntiBotTech::DataDome),
(r"(?i)captcha-delivery\.com", AntiBotTech::DataDome),
(r"(?i)ct\.captcha-delivery", AntiBotTech::DataDome),
(r"(?i)incap_ses", AntiBotTech::Imperva),
(r"(?i)visid_incap", AntiBotTech::Imperva),
(r"(?i)incapsula", AntiBotTech::Imperva),
(r"(?i)sucuri_cloudproxy", AntiBotTech::Sucuri),
(r"(?i)x-sucuri-id", AntiBotTech::Sucuri),
(r"(?i)sucuri[\s\-_]?firewall", AntiBotTech::Sucuri),
(r"(?i)kasada", AntiBotTech::Kasada),
(r"(?i)pointdefense", AntiBotTech::Kasada),
(r"(?i)aws-waf", AntiBotTech::AwsWaf),
(r"(?i)captcha\.awswaf\.com", AntiBotTech::AwsWaf),
(r"(?i)awsWafCookie", AntiBotTech::AwsWaf),
];
pub static TIER1_REGEXES: Lazy<Vec<(Regex, AntiBotTech)>> = Lazy::new(|| {
TIER1_PATTERNS
.iter()
.map(|(pat, tech)| (Regex::new(pat).expect("antibot tier1 regex"), *tech))
.collect()
});
pub static TIER2_LITERALS: &[&str] = &[
"access denied",
"blocked",
"captcha",
"challenge",
"forbidden",
"rate limit",
"rate-limited",
"verify you are human",
"checking your browser",
"ddos protection",
"please verify you are not a bot",
"robot check",
"security check",
"human verification",
"bot detection",
"protection by",
"are you a robot",
"incapsula incident",
"attention required",
"unusual traffic",
];
pub static TIER2_AUTOMATON: Lazy<AhoCorasick> = Lazy::new(|| {
AhoCorasickBuilder::new()
.ascii_case_insensitive(true)
.build(TIER2_LITERALS)
.expect("antibot tier2 automaton")
});
pub const TIER2_BODY_SIZE_LIMIT: usize = 10 * 1024;
pub static TIER3_NO_BODY: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?is)<body\b").expect("antibot tier3 no-body regex"));
pub static TIER3_SCRIPT_BLOCK: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?is)<script\b[^>]*>.*?</script>").expect("antibot tier3 script-block")
});
pub static TIER3_ANY_TAG: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(?s)<[^>]+>").expect("antibot tier3 any-tag"));
pub const TIER3_MIN_SIGNALS: usize = 2;
pub const TIER3_VISIBLE_TEXT_MIN: usize = 50;
pub const TIER3_SCRIPT_HEAVY_BYTES: usize = 5 * 1024;
pub const TIER3_SCRIPT_HEAVY_VISIBLE_MAX: usize = 200;
pub const NEAR_EMPTY_BODY_LEN: usize = 200;
#[cfg(test)]
mod tests {
use super::*;
fn tier1_match(body: &str, expected: AntiBotTech) -> bool {
TIER1_REGEXES
.iter()
.find(|(re, _)| re.is_match(body))
.map(|(_, t)| *t)
== Some(expected)
}
#[test]
fn tier1_cloudflare_challenge_platform() {
assert!(tier1_match(
"<script src=\"/cdn-cgi/challenge-platform/h/g/orchestrate/jsch/v1\"></script>",
AntiBotTech::Cloudflare
));
}
#[test]
fn tier1_cloudflare_just_a_moment() {
assert!(tier1_match(
"<title>Just a moment...</title>",
AntiBotTech::Cloudflare
));
}
#[test]
fn tier1_akamai_reference() {
assert!(tier1_match(
"<p>Reference #18.abc123.456</p>",
AntiBotTech::Akamai
));
}
#[test]
fn tier1_akamai_pardon_interruption() {
assert!(tier1_match(
"<h1>Pardon Our Interruption</h1><p>As you were browsing something about your browser made us think you were a bot.</p>",
AntiBotTech::Akamai
));
}
#[test]
fn tier1_perimeterx_captcha() {
assert!(tier1_match(
"<div class=\"px-captcha\"></div>",
AntiBotTech::PerimeterX
));
}
#[test]
fn tier1_datadome_cookie() {
assert!(tier1_match(
"Set-Cookie: datadome=ABCDEF;",
AntiBotTech::DataDome
));
}
#[test]
fn tier1_imperva_incap_ses() {
assert!(tier1_match(
"Set-Cookie: incap_ses_123='XYZ';",
AntiBotTech::Imperva
));
}
#[test]
fn tier1_sucuri_cloudproxy() {
assert!(tier1_match(
"<!-- sucuri_cloudproxy -->",
AntiBotTech::Sucuri
));
}
#[test]
fn tier1_kasada_pointdefense() {
assert!(tier1_match(
"<script src=\"/pointdefense/agent.js\"></script>",
AntiBotTech::Kasada
));
}
#[test]
fn tier1_awswaf_captcha_host() {
assert!(tier1_match(
"<iframe src=\"https://captcha.awswaf.com/captcha/captcha.html\"></iframe>",
AntiBotTech::AwsWaf
));
}
#[test]
fn tier1_pattern_count_meets_minimum() {
assert!(
TIER1_PATTERNS.len() >= 20,
"expected >= 20 tier1 patterns, got {}",
TIER1_PATTERNS.len()
);
}
#[test]
fn tier1_regexes_compiled() {
assert!(
!TIER1_REGEXES.is_empty(),
"tier1 regexes lazy should be non-empty"
);
let _ = Lazy::force(&TIER1_REGEXES);
}
#[test]
fn tier2_automaton_matches_blocked() {
assert!(TIER2_AUTOMATON.is_match("You have been blocked by our system"));
}
#[test]
fn tier2_automaton_matches_captcha() {
assert!(TIER2_AUTOMATON.is_match("please complete the CAPTCHA below"));
}
#[test]
fn tier2_automaton_no_match_clean() {
assert!(!TIER2_AUTOMATON.is_match("hello world this is a clean page"));
}
#[test]
fn tier2_literal_count_meets_minimum() {
assert!(
TIER2_LITERALS.len() >= 10,
"expected >= 10 tier2 literals, got {}",
TIER2_LITERALS.len()
);
}
#[test]
fn tier2_body_size_limit_is_10kb() {
assert_eq!(TIER2_BODY_SIZE_LIMIT, 10 * 1024);
}
#[test]
fn tier3_no_body_detects_missing_body_tag() {
assert!(!TIER3_NO_BODY.is_match("<html><head><title>x</title></head></html>"));
}
#[test]
fn tier3_no_body_passes_when_body_present() {
assert!(TIER3_NO_BODY.is_match("<html><body>hi</body></html>"));
}
#[test]
fn tier3_script_block_extracts_one_block() {
let body = "<script>a=1</script><div>x</div><script>b=2</script>";
let count = TIER3_SCRIPT_BLOCK.find_iter(body).count();
assert_eq!(count, 2, "expected 2 script blocks, got {}", count);
}
#[test]
fn tier3_any_tag_strips_tags() {
let body = "<p>hello <b>world</b></p>";
let stripped = TIER3_ANY_TAG.replace_all(body, "");
assert_eq!(stripped, "hello world");
}
#[test]
fn tier3_thresholds_sane() {
assert_eq!(TIER3_MIN_SIGNALS, 2);
assert_eq!(TIER3_VISIBLE_TEXT_MIN, 50);
assert_eq!(TIER3_SCRIPT_HEAVY_BYTES, 5 * 1024);
assert_eq!(TIER3_SCRIPT_HEAVY_VISIBLE_MAX, 200);
assert_eq!(NEAR_EMPTY_BODY_LEN, 200);
}
}