use super::*;
use crate::captcha_detect::DetectedCaptcha;
use std::time::{Duration, Instant};
const DEFAULT_MAX_WAIT_MS: u64 = 20_000;
pub struct CloudflareInterstitialSolver {
max_wait_ms: u64,
}
impl Default for CloudflareInterstitialSolver {
fn default() -> Self {
Self::new()
}
}
impl CloudflareInterstitialSolver {
pub fn new() -> Self {
Self {
max_wait_ms: DEFAULT_MAX_WAIT_MS,
}
}
pub fn with_max_wait_ms(mut self, ms: u64) -> Self {
self.max_wait_ms = ms;
self
}
}
const PHASE_PROBE_JS: &str = r#"
(() => {
const title = (document.title || '').toLowerCase();
const body = document.body ? document.body.innerHTML : '';
if (title.includes('blocked') || title.includes('access denied')) {
return { phase: 'blocked' };
}
if (
title.includes('just a moment') ||
title.includes('attention required') ||
document.querySelector('#challenge-running, #challenge-form, .cf-browser-verification, #cf-challenge-running')
) {
return { phase: 'challenge' };
}
/* Page is no longer on the interstitial — check cookie. */
const hasClearance = document.cookie.split(';').some(c => c.trim().startsWith('cf_clearance='));
return { phase: 'passed', cookieSet: hasClearance };
})()
"#;
#[derive(Debug, serde::Deserialize)]
struct PhaseProbe {
phase: String,
#[serde(default)]
#[serde(rename = "cookieSet")]
cookie_set: bool,
}
#[async_trait]
impl CaptchaSolver for CloudflareInterstitialSolver {
fn name(&self) -> &'static str {
"CloudflareInterstitialSolver"
}
fn method(&self) -> SolveMethod {
SolveMethod::AutoPass
}
fn supports(&self, kind: &DetectedCaptcha) -> bool {
matches!(
kind,
DetectedCaptcha::Turnstile | DetectedCaptcha::Custom(_)
)
}
async fn solve(&self, page: &Page, _info: &CaptchaInfo) -> Result<CaptchaSolveResult> {
let t0 = Instant::now();
let deadline = t0 + Duration::from_millis(self.max_wait_ms);
loop {
let raw = page
.evaluate(PHASE_PROBE_JS)
.await
.map_err(|e| anyhow!("CF interstitial probe failed: {e}"))?;
let probe: PhaseProbe = match raw.into_value() {
Ok(p) => p,
Err(_) => PhaseProbe {
phase: "challenge".to_string(),
cookie_set: false,
},
};
match probe.phase.as_str() {
"passed" if probe.cookie_set => {
let cookies = crate::cookies::capture_from_page(page)
.await
.unwrap_or_default();
return Ok(CaptchaSolveResult {
solution: "cf_clearance".to_string(),
confidence: 1.0,
method: SolveMethod::AutoPass,
time_ms: t0.elapsed().as_millis() as u64,
success: true,
screenshot: None,
cookies,
verified_outcome: None,
});
}
"passed" => {
}
"blocked" => {
return Ok(CaptchaSolveResult::failure(
SolveMethod::AutoPass,
t0.elapsed().as_millis() as u64,
));
}
_ => {} }
if Instant::now() >= deadline {
return Ok(CaptchaSolveResult::failure(
SolveMethod::AutoPass,
t0.elapsed().as_millis() as u64,
));
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::captcha_detect::DetectedCaptcha;
#[test]
fn defaults_are_sane() {
let s = CloudflareInterstitialSolver::new();
assert_eq!(s.max_wait_ms, 20_000);
}
#[test]
fn builders_override() {
let s = CloudflareInterstitialSolver::new().with_max_wait_ms(8_000);
assert_eq!(s.max_wait_ms, 8_000);
}
#[test]
fn supports_turnstile_kind() {
assert!(CloudflareInterstitialSolver::new().supports(&DetectedCaptcha::Turnstile));
}
#[test]
fn name_method_stable() {
let s = CloudflareInterstitialSolver::new();
assert_eq!(s.name(), "CloudflareInterstitialSolver");
assert_eq!(s.method(), SolveMethod::AutoPass);
}
#[test]
fn phase_probe_js_covers_documented_signals() {
for needle in [
"just a moment",
"attention required",
"#challenge-running",
"#challenge-form",
"cf_clearance",
"blocked",
] {
assert!(
PHASE_PROBE_JS.to_lowercase().contains(needle),
"PHASE_PROBE_JS must reference: {needle}"
);
}
}
}