chaser-cf 0.1.0

High-performance Cloudflare bypass library with stealth browser automation. Rust-native with C FFI bindings.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Solver implementations for chaser-cf operations

use super::BrowserManager;
use crate::error::{ChaserError, ChaserResult};
use crate::models::{Cookie, Profile, ProxyConfig, WafSession};

use chaser_oxide::auth::Credentials;
use std::collections::HashMap;
use std::time::Duration;

/// Embedded fake page HTML for turnstile-min mode
const FAKE_PAGE_HTML: &str = include_str!("../resources/fake_page.html");

/// Get page source from a Cloudflare-protected URL
pub async fn get_source(
    manager: &BrowserManager,
    url: &str,
    proxy: Option<ProxyConfig>,
    profile: Profile,
) -> ChaserResult<String> {
    // Build the stealth profile for this request
    let chaser_profile = BrowserManager::build_profile(profile);

    // Acquire context permit
    let _permit = manager.acquire_permit().await?;

    // Create context with proxy if provided
    let ctx_id = manager.create_context(proxy.as_ref()).await?;

    // Create page in context with the specified profile
    let page = manager
        .new_page_in_context(ctx_id, "about:blank", Some(&chaser_profile))
        .await?;

    // Set up proxy authentication if credentials provided
    if let Some(ref p) = proxy {
        if let (Some(username), Some(password)) = (&p.username, &p.password) {
            page
                .authenticate(Credentials {
                    username: username.clone(),
                    password: password.clone(),
                })
                .await
                .map_err(|e| ChaserError::Internal(format!("Proxy auth failed: {}", e)))?;
        }
    }

    // Navigate and wait for load
    page.goto(url)
        .await
        .map_err(|e| ChaserError::NavigationFailed(e.to_string()))?;

    // Wait for potential CF challenge to complete
    // We look for a successful response by waiting for the page to stabilize
    tokio::time::sleep(Duration::from_millis(2000)).await;

    // Try to detect if we're still on a CF challenge page
    let mut attempts = 0;
    let max_attempts = 30;
    loop {
        let html = page
            .content()
            .await
            .map_err(|e| ChaserError::Internal(e.to_string()))?;

        // Check if we've passed the challenge (no CF challenge indicators)
        if !is_challenge_page(&html) || attempts >= max_attempts {
            return Ok(html);
        }

        attempts += 1;
        tokio::time::sleep(Duration::from_millis(1000)).await;
    }
}

/// Create WAF session with cookies and headers
pub async fn solve_waf_session(
    manager: &BrowserManager,
    url: &str,
    proxy: Option<ProxyConfig>,
    profile: Profile,
) -> ChaserResult<WafSession> {
    // Build the stealth profile for this request
    let chaser_profile = BrowserManager::build_profile(profile);

    // Acquire context permit
    let _permit = manager.acquire_permit().await?;

    // Create context with proxy if provided
    let ctx_id = manager.create_context(proxy.as_ref()).await?;

    // Create page in context with the specified profile
    let page = manager
        .new_page_in_context(ctx_id, "about:blank", Some(&chaser_profile))
        .await?;

    // Set up proxy authentication if credentials provided
    if let Some(ref p) = proxy {
        if let (Some(username), Some(password)) = (&p.username, &p.password) {
            page
                .authenticate(Credentials {
                    username: username.clone(),
                    password: password.clone(),
                })
                .await
                .map_err(|e| ChaserError::Internal(format!("Proxy auth failed: {}", e)))?;
        }
    }

    // First, get Accept-Language via httpbin
    let accept_language = get_accept_language(&page).await.unwrap_or_default();

    // Navigate to target URL
    page.goto(url)
        .await
        .map_err(|e| ChaserError::NavigationFailed(e.to_string()))?;

    // Wait for potential CF challenge to complete
    let mut attempts = 0;
    let max_attempts = 30;
    loop {
        let html = page
            .content()
            .await
            .map_err(|e| ChaserError::Internal(e.to_string()))?;

        if !is_challenge_page(&html) || attempts >= max_attempts {
            break;
        }

        attempts += 1;
        tokio::time::sleep(Duration::from_millis(1000)).await;
    }

    // Extract cookies
    let cookies = page
        .get_cookies()
        .await
        .map_err(|e| ChaserError::CookieExtractionFailed(e.to_string()))?;

    let cookies: Vec<Cookie> = cookies
        .into_iter()
        .map(|c| Cookie {
            name: c.name,
            value: c.value,
            domain: Some(c.domain),
            path: Some(c.path),
            expires: Some(c.expires), // Convert f64 to Option<f64>
            http_only: Some(c.http_only),
            secure: Some(c.secure),
            same_site: c.same_site.map(|s| format!("{:?}", s)),
        })
        .collect();

    // Build headers
    let mut headers = HashMap::new();

    // Get user agent from page using stealth evaluation
    let chaser = chaser_oxide::ChaserPage::new(page.clone());
    let user_agent = chaser
        .evaluate("navigator.userAgent")
        .await
        .ok()
        .and_then(|v| v?.as_str().map(|s| s.to_string()))
        .unwrap_or_default();

    headers.insert("user-agent".to_string(), user_agent);
    if !accept_language.is_empty() {
        headers.insert("accept-language".to_string(), accept_language);
    }

    Ok(WafSession::new(cookies, headers))
}

/// Solve Turnstile with full page load
pub async fn solve_turnstile_max(
    manager: &BrowserManager,
    url: &str,
    proxy: Option<ProxyConfig>,
    profile: Profile,
) -> ChaserResult<String> {
    // Build the stealth profile for this request
    let chaser_profile = BrowserManager::build_profile(profile);

    // Acquire context permit
    let _permit = manager.acquire_permit().await?;

    // Create context with proxy if provided
    let ctx_id = manager.create_context(proxy.as_ref()).await?;

    // Create page in context with the specified profile
    let page = manager
        .new_page_in_context(ctx_id, "about:blank", Some(&chaser_profile))
        .await?;

    // Set up proxy authentication if credentials provided
    if let Some(ref p) = proxy {
        if let (Some(username), Some(password)) = (&p.username, &p.password) {
            page
                .authenticate(Credentials {
                    username: username.clone(),
                    password: password.clone(),
                })
                .await
                .map_err(|e| ChaserError::Internal(format!("Proxy auth failed: {}", e)))?;
        }
    }

    // Inject token extraction script before navigation
    page.evaluate_on_new_document(TURNSTILE_EXTRACTOR_SCRIPT)
        .await
        .map_err(|e| ChaserError::Internal(e.to_string()))?;

    // Navigate to the page with Turnstile
    page.goto(url)
        .await
        .map_err(|e| ChaserError::NavigationFailed(e.to_string()))?;

    // Wait for the cf-response element to appear
    let token = wait_for_turnstile_token(&page, 60).await?;

    Ok(token)
}

/// Solve Turnstile with minimal resource usage
///
/// This mode uses request interception to serve a lightweight HTML page
/// that only loads the Turnstile widget, avoiding full page resource loading.
pub async fn solve_turnstile_min(
    manager: &BrowserManager,
    url: &str,
    site_key: &str,
    proxy: Option<ProxyConfig>,
    profile: Profile,
) -> ChaserResult<String> {
    use chaser_oxide::cdp::browser_protocol::fetch::EventRequestPaused;
    use chaser_oxide::cdp::browser_protocol::network::ResourceType;
    use futures::StreamExt;

    // Build the stealth profile for this request
    let chaser_profile = BrowserManager::build_profile(profile);

    // Acquire context permit
    let _permit = manager.acquire_permit().await?;

    // Create context with proxy if provided
    let ctx_id = manager.create_context(proxy.as_ref()).await?;

    // Create page in context with the specified profile
    let page = manager
        .new_page_in_context(ctx_id, "about:blank", Some(&chaser_profile))
        .await?;

    // Set up proxy authentication if credentials provided
    if let Some(ref p) = proxy {
        if let (Some(username), Some(password)) = (&p.username, &p.password) {
            page
                .authenticate(Credentials {
                    username: username.clone(),
                    password: password.clone(),
                })
                .await
                .map_err(|e| ChaserError::Internal(format!("Proxy auth failed: {}", e)))?;
        }
    }

    // Prepare fake page HTML with site key
    let fake_html = FAKE_PAGE_HTML.replace("<site-key>", site_key);

    // Wrap in ChaserPage for request interception API
    let chaser = chaser_oxide::ChaserPage::new(page.clone());

    // Enable request interception for document requests
    chaser
        .enable_request_interception("*", Some(ResourceType::Document))
        .await
        .map_err(|e| ChaserError::Internal(format!("Failed to enable interception: {}", e)))?;

    // Set up event listener for intercepted requests
    let mut request_events = page
        .event_listener::<EventRequestPaused>()
        .await
        .map_err(|e| ChaserError::Internal(format!("Failed to listen for requests: {}", e)))?;

    // Clone values for the spawned task
    let url_clone = url.to_string();
    let fake_html_clone = fake_html.clone();
    let chaser_clone = chaser.clone();

    // Spawn task to handle intercepted requests
    let intercept_handle = tokio::spawn(async move {
        while let Some(event) = request_events.next().await {
            let request_url = &event.request.url;

            // Check if this is the document request for our target URL
            let is_target = request_url == &url_clone
                || request_url == &format!("{}/", url_clone)
                || request_url.starts_with(&url_clone);

            if is_target && event.resource_type == ResourceType::Document {
                // Fulfill with our minimal Turnstile page
                let _ = chaser_clone
                    .fulfill_request_html(event.request_id.clone(), &fake_html_clone, 200)
                    .await;
            } else {
                // Continue other requests
                let _ = chaser_clone
                    .continue_request(event.request_id.clone())
                    .await;
            }
        }
    });

    // Navigate to the URL (will be intercepted)
    page.goto(url)
        .await
        .map_err(|e| ChaserError::NavigationFailed(e.to_string()))?;

    // Wait for the cf-response element to appear
    let token = wait_for_turnstile_token(&page, 60).await?;

    // Clean up
    intercept_handle.abort();
    let _ = chaser.disable_request_interception().await;

    Ok(token)
}

/// Script injected to extract Turnstile token
const TURNSTILE_EXTRACTOR_SCRIPT: &str = r#"
    let token = null;
    async function waitForToken() {
        while (!token) {
            try {
                token = window.turnstile.getResponse();
            } catch (e) {}
            await new Promise(resolve => setTimeout(resolve, 500));
        }
        var c = document.createElement("input");
        c.type = "hidden";
        c.name = "cf-response";
        c.value = token;
        document.body.appendChild(c);
    }
    waitForToken();
"#;

/// Wait for Turnstile token to be available
async fn wait_for_turnstile_token(
    page: &chaser_oxide::Page,
    timeout_seconds: u64,
) -> ChaserResult<String> {
    let start = std::time::Instant::now();
    let timeout = Duration::from_secs(timeout_seconds);

    // Wrap in ChaserPage for stealth evaluation
    let chaser = chaser_oxide::ChaserPage::new(page.clone());

    loop {
        if start.elapsed() > timeout {
            return Err(ChaserError::CaptchaFailed(
                "Timeout waiting for token".to_string(),
            ));
        }

        let result = chaser
            .evaluate(
                r#"
                (function() {
                    // First check if turnstile object exists and has a response
                    if (window.turnstile && typeof window.turnstile.getResponse === 'function') {
                        var token = window.turnstile.getResponse();
                        if (token) return token;
                    }
                    // Fallback: check for cf-response element (from our injected script)
                    var el = document.querySelector('[name="cf-response"]');
                    return el ? el.value : null;
                })()
            "#,
            )
            .await;

        if let Ok(Some(value)) = result {
            if let Some(token) = value.as_str() {
                if token.len() > 10 {
                    return Ok(token.to_string());
                }
            }
        }

        tokio::time::sleep(Duration::from_millis(500)).await;
    }
}

/// Get Accept-Language header via httpbin
async fn get_accept_language(page: &chaser_oxide::Page) -> Option<String> {
    let chaser = chaser_oxide::ChaserPage::new(page.clone());
    
    let result = chaser
        .evaluate(
            r#"
            fetch("https://httpbin.org/get")
                .then(r => r.json())
                .then(r => r.headers["Accept-Language"] || r.headers["accept-language"])
                .catch(() => null)
        "#,
        )
        .await
        .ok()?;

    result?.as_str().map(|s| s.to_string())
}

/// Check if page content appears to be a Cloudflare challenge page
fn is_challenge_page(html: &str) -> bool {
    let challenge_indicators = [
        "challenge-platform",
        "cf-spinner",
        "cf_chl_opt",
        "Just a moment",
        "Checking your browser",
        "ray ID",
        "__cf_chl",
    ];

    let html_lower = html.to_lowercase();
    challenge_indicators
        .iter()
        .any(|indicator| html_lower.contains(&indicator.to_lowercase()))
}