captchaforge 0.2.35

[DO NOT USE — UNDER ACTIVE DEVELOPMENT, NOT PRODUCTION-READY] Captcha solver scaffolding for chromiumoxide-driven browsers. The architecture is in place (vendor solvers, retry-loop iframe walking, VLM provider abstraction, real-WAF bench harness) but the live-vendor success rate is still 0% — Cloudflare Turnstile / hCaptcha / reCAPTCHA detect us at a TLS / CDP fingerprint layer that no flag-based stealth has cleared. Watch the repo; do not depend on this for any real workload.
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Generic slider-puzzle solver.
//!
//! Slider captchas (DataDome, GeeTest, PerimeterX/HUMAN, AWS WAF
//! Captcha, Akamai's slider variant, generic homegrown sliders)
//! all share the same UX:
//!
//! 1. Show a background image with a piece-shaped gap.
//! 2. Show a draggable puzzle piece on the side.
//! 3. The user drags the piece into the gap.
//! 4. Server validates by replaying the trajectory + landing offset.
//!
//! The protocols differ in detail (DataDome posts cookies,
//! GeeTest posts a JSON challenge, PX posts an event stream) but
//! the *click-and-drag* mechanic is identical.
//!
//! `SliderCaptchaSolver` handles the mechanic in three steps:
//!
//! 1. **Locate** the puzzle canvas + the draggable handle via
//!    well-known selectors per vendor.
//! 2. **Detect the gap** by reading the puzzle canvas pixels and
//!    finding the column with the largest brightness delta against
//!    its neighbour (the gap edge). Done in-page via canvas
//!    `getImageData` so we don't move pixels over CDP.
//! 3. **Drag** the handle to the gap with a Bézier trajectory,
//!    realistic acceleration / deceleration / micro-jitter / slight
//!    overshoot + correction. Uses the same physics as the
//!    behavioural mouse layer.
//!
//! When detection fails (no canvas, or unrecognised vendor selector
//! set), `solve()` returns failure cleanly so the chain falls
//! through. Each vendor-specific solver (`DataDomeSolver`,
//! `GeeTestV3Solver`, etc.) wraps this generic one with the right
//! selectors + post-success cookie/token harvesting.

use super::*;
use crate::captcha_detect::DetectedCaptcha;
use rand::{Rng, SeedableRng};
use std::time::{Duration, Instant};

/// Per-vendor selector triple: canvas (for gap detection), handle
/// (the draggable element), and an optional success-marker selector
/// to poll after dragging.
#[derive(Debug, Clone, Copy)]
pub struct SliderSelectors {
    pub canvas: &'static str,
    pub handle: &'static str,
    pub success_marker: Option<&'static str>,
}

/// JS that finds a puzzle canvas, scans pixel columns for the gap
/// (sharp brightness delta), and returns the gap's centre x in
/// canvas-local coordinates. Returns null when no canvas / no gap
/// signal found.
const GAP_DETECT_JS: &str = r#"
(canvasSelector) => {
    const canvas = document.querySelector(canvasSelector);
    if (!canvas || !(canvas instanceof HTMLCanvasElement)) return null;
    const ctx = canvas.getContext('2d');
    if (!ctx) return null;
    let img;
    try { img = ctx.getImageData(0, 0, canvas.width, canvas.height); }
    catch (e) { return null; /* CORS-tainted */ }
    const w = img.width, h = img.height, data = img.data;

    /* Per-column average luminance, then find the column with the
       largest delta vs its neighbour (the gap edge). */
    const lum = new Float32Array(w);
    for (let x = 0; x < w; x++) {
        let sum = 0;
        for (let y = 0; y < h; y++) {
            const i = (y * w + x) * 4;
            sum += 0.299 * data[i] + 0.587 * data[i+1] + 0.114 * data[i+2];
        }
        lum[x] = sum / h;
    }
    let bestX = -1, bestDelta = 0;
    for (let x = 1; x < w - 1; x++) {
        const d = Math.abs(lum[x] - lum[x-1]) + Math.abs(lum[x+1] - lum[x]);
        if (d > bestDelta) { bestDelta = d; bestX = x; }
    }
    if (bestX < 0 || bestDelta < 6) return null; /* too noisy */
    return { gapX: bestX, canvasWidth: w };
}
"#;

/// JS that returns the handle's bounding-rect centre + the
/// canvas's bounding-rect left, in viewport coordinates. Lets the
/// solver translate canvas-local gap-x into a target-x to drag to.
const ANCHORS_JS: &str = r#"
(canvasSelector, handleSelector) => {
    const canvas = document.querySelector(canvasSelector);
    const handle = document.querySelector(handleSelector);
    if (!canvas || !handle) return null;
    const c = canvas.getBoundingClientRect();
    const h = handle.getBoundingClientRect();
    return {
        canvasLeft: c.left, canvasTop: c.top,
        canvasWidth: c.width, canvasHeight: c.height,
        handleX: h.left + h.width / 2,
        handleY: h.top + h.height / 2,
        handleWidth: h.width
    };
}
"#;

/// Solver for slider-puzzle captchas. Generic across vendors;
/// caller supplies the per-vendor selectors.
pub struct SliderCaptchaSolver {
    /// Vendor-specific selectors. The default constructor walks a
    /// list of well-known selectors and uses the first match.
    selectors: Vec<SliderSelectors>,
}

impl Default for SliderCaptchaSolver {
    fn default() -> Self {
        Self::new()
    }
}

impl SliderCaptchaSolver {
    /// Construct with a built-in selector set covering the major
    /// vendors. The solver tries each set in order until one
    /// matches the page.
    pub fn new() -> Self {
        Self {
            selectors: vec![
                // GeeTest v3
                SliderSelectors {
                    canvas: ".geetest_canvas_slice",
                    handle: ".geetest_slider_button",
                    success_marker: Some(".geetest_success"),
                },
                // GeeTest v4
                SliderSelectors {
                    canvas: ".geetest_item_img",
                    handle: ".geetest_btn",
                    success_marker: Some(".geetest_success_radar_tip"),
                },
                // DataDome
                SliderSelectors {
                    canvas: "#captcha__puzzle",
                    handle: "#sliderIcon",
                    success_marker: None,
                },
                // PerimeterX / HUMAN
                SliderSelectors {
                    canvas: ".px-captcha-puzzle",
                    handle: ".px-captcha-slider",
                    success_marker: Some(".px-captcha-success"),
                },
                // AWS WAF Captcha
                SliderSelectors {
                    canvas: "[data-puzzle-canvas]",
                    handle: "[data-puzzle-slider]",
                    success_marker: None,
                },
                // Yandex SmartCaptcha (slider variant)
                SliderSelectors {
                    canvas: ".SmartCaptcha-CheckboxCaptcha-Image",
                    handle: ".SmartCaptcha-CheckboxCaptcha-Slider",
                    success_marker: Some(".SmartCaptcha-checked"),
                },
                // Tencent Captcha (slider variant)
                SliderSelectors {
                    canvas: "#tcaptcha_drag_button + .tcaptcha-bg",
                    handle: "#tcaptcha_drag_button",
                    success_marker: None,
                },
                // KeyCaptcha
                SliderSelectors {
                    canvas: "#capcode .keycaptcha-bg",
                    handle: "#capcode .keycaptcha-button",
                    success_marker: None,
                },
                // Generic (homegrown sliders)
                SliderSelectors {
                    canvas: ".slider-captcha-canvas",
                    handle: ".slider-captcha-handle",
                    success_marker: Some(".slider-captcha-success"),
                },
                // Bench-shape generic — `<canvas class="captcha">` plus
                // a `#knob` sibling. Useful as a final fallback for
                // rolled-its-own slider widgets that just expose a
                // canvas + a knob without vendor-specific class names.
                SliderSelectors {
                    canvas: "canvas.captcha",
                    handle: "#knob",
                    success_marker: None,
                },
                // Swipe-bar widget: no puzzle canvas, just a track + knob
                // where the user drags far right. Common on "swipe to
                // verify" anti-bot UIs.
                SliderSelectors {
                    canvas: "#swipe-area",
                    handle: "#swipe-knob",
                    success_marker: None,
                },
            ],
        }
    }

    /// Override the selector list (for vendors not in the default
    /// set, or to force a specific one).
    pub fn with_selectors(mut self, selectors: Vec<SliderSelectors>) -> Self {
        self.selectors = selectors;
        self
    }
}

#[derive(Debug, serde::Deserialize)]
struct GapResult {
    #[serde(rename = "gapX")]
    gap_x: f64,
    #[serde(rename = "canvasWidth")]
    canvas_width: f64,
}

#[derive(Debug, serde::Deserialize)]
#[allow(dead_code)] // canvas_top fields read by other branches; suppress to keep struct stable
struct Anchors {
    #[serde(rename = "canvasLeft")]
    canvas_left: f64,
    #[serde(rename = "handleX")]
    handle_x: f64,
    #[serde(rename = "handleY")]
    handle_y: f64,
    #[serde(rename = "handleWidth")]
    handle_width: f64,
    #[serde(rename = "canvasWidth")]
    canvas_width: f64,
}

#[async_trait]
impl CaptchaSolver for SliderCaptchaSolver {
    fn name(&self) -> &'static str {
        "SliderCaptchaSolver"
    }

    fn method(&self) -> SolveMethod {
        SolveMethod::BehavioralBypass
    }

    fn supports(&self, kind: &DetectedCaptcha) -> bool {
        match kind {
            DetectedCaptcha::SliderCaptcha => true,
            DetectedCaptcha::Custom(name) => matches!(
                name.as_str(),
                "datadome"
                    | "geetest_v3"
                    | "geetest_v4"
                    | "perimeterx_human"
                    | "aws_waf_captcha"
                    | "akamai_bot_manager"
                    | "yandex_smartcaptcha"
                    | "tencent_captcha"
                    | "keycaptcha"
            ),
            _ => false,
        }
    }

    async fn solve(&self, page: &Page, _info: &CaptchaInfo) -> Result<CaptchaSolveResult> {
        let t0 = Instant::now();

        for sel in &self.selectors {
            // Try gap detection on this selector set.
            let gap_js = format!(
                "({})({})",
                GAP_DETECT_JS,
                serde_json::to_string(sel.canvas).unwrap_or_else(|_| "\"\"".into())
            );
            let gap_raw = match page.evaluate(gap_js).await {
                Ok(r) => r,
                Err(_) => continue,
            };
            let gap: Option<GapResult> = gap_raw.into_value().ok().flatten();
            let Some(gap) = gap else { continue };

            // Get anchor coordinates so we can map canvas-local gapX
            // to viewport-coordinate target.
            let anchors_js = format!(
                "({})({},{})",
                ANCHORS_JS,
                serde_json::to_string(sel.canvas).unwrap_or_else(|_| "\"\"".into()),
                serde_json::to_string(sel.handle).unwrap_or_else(|_| "\"\"".into())
            );
            let anchors_raw = match page.evaluate(anchors_js).await {
                Ok(r) => r,
                Err(_) => continue,
            };
            let anchors: Option<Anchors> = anchors_raw.into_value().ok().flatten();
            let Some(anchors) = anchors else { continue };

            // Map gap from canvas-local to viewport. The puzzle
            // piece's centre needs to land at canvas_left +
            // (gap_x / canvas_width) * canvas_width = canvas_left + gap_x
            // (canvas_width on the JS side equals canvas natural
            // width, but we already renormalised — gap_x is the px
            // column index in the bitmap, anchors.canvas_width is
            // the on-screen rect width; handle the ratio).
            let scale = anchors.canvas_width / gap.canvas_width.max(1.0);
            // The drag's destination clientX equals where we want the
            // handle's CENTER to land. Production puzzle sliders bind
            // handle.style.left to puzzle-piece.left via a delta from
            // mousedown; the resulting handle.style.left == clientX -
            // handle_starting_center. Subtracting handle_width/2 here
            // would land the handle one half-width left of the gap.
            let target_x = anchors.canvas_left + gap.gap_x * scale;

            // Drag with realistic Bézier trajectory + jitter +
            // slight overshoot.
            drag_handle(
                page,
                anchors.handle_x,
                anchors.handle_y,
                target_x,
                anchors.handle_y,
            )
            .await?;

            // Optionally wait for a success marker.
            if let Some(marker) = sel.success_marker {
                let deadline = Instant::now() + Duration::from_secs(5);
                while Instant::now() < deadline {
                    let exists_js = format!(
                        "(() => !!document.querySelector({}))()",
                        serde_json::to_string(marker).unwrap_or_else(|_| "\"\"".into())
                    );
                    if let Ok(raw) = page.evaluate(exists_js).await {
                        if raw.into_value::<bool>().unwrap_or(false) {
                            break;
                        }
                    }
                    tokio::time::sleep(Duration::from_millis(150)).await;
                }
            }

            let cookies = crate::cookies::capture_from_page(page)
                .await
                .unwrap_or_default();
            return Ok(CaptchaSolveResult {
                solution: format!("slider-drag-{}", gap.gap_x as i64),
                confidence: 0.8,
                method: self.method(),
                time_ms: t0.elapsed().as_millis() as u64,
                success: true,
                screenshot: None,
                cookies,
                verified_outcome: None,
            });
        }

        // Track-fallback pass: no canvas-puzzle gap was found, but
        // some widgets are pure swipe-bars (no canvas, just a track +
        // a handle that drags to the far right). Walk the selectors
        // again; if the "canvas" selector matches a non-canvas
        // element with measurable width and a handle, drag the
        // handle to the right edge minus its own width.
        for sel in &self.selectors {
            let track_anchors_js = format!(
                r#"
                ((trackSel, handleSel) => {{
                    const t = document.querySelector(trackSel);
                    const h = document.querySelector(handleSel);
                    if (!t || !h) return null;
                    if (t instanceof HTMLCanvasElement) return null; /* gap-detect handles these */
                    const tr = t.getBoundingClientRect();
                    const hr = h.getBoundingClientRect();
                    return {{
                        trackLeft: tr.left, trackTop: tr.top,
                        trackWidth: tr.width, trackRight: tr.right,
                        handleX: hr.left + hr.width / 2,
                        handleY: hr.top + hr.height / 2,
                        handleWidth: hr.width
                    }};
                }})({}, {})
                "#,
                serde_json::to_string(sel.canvas).unwrap_or_else(|_| "\"\"".into()),
                serde_json::to_string(sel.handle).unwrap_or_else(|_| "\"\"".into())
            );
            let raw = match page.evaluate(track_anchors_js).await {
                Ok(r) => r,
                Err(_) => continue,
            };
            #[derive(Debug, serde::Deserialize)]
            struct TrackAnchors {
                #[serde(rename = "trackRight")]
                track_right: f64,
                #[serde(rename = "handleX")]
                handle_x: f64,
                #[serde(rename = "handleY")]
                handle_y: f64,
                #[serde(rename = "handleWidth")]
                handle_width: f64,
            }
            let anchors: Option<TrackAnchors> = raw.into_value().ok().flatten();
            let Some(anchors) = anchors else { continue };
            let target_x = anchors.track_right - anchors.handle_width / 2.0 - 4.0;
            drag_handle(
                page,
                anchors.handle_x,
                anchors.handle_y,
                target_x,
                anchors.handle_y,
            )
            .await?;
            let cookies = crate::cookies::capture_from_page(page)
                .await
                .unwrap_or_default();
            return Ok(CaptchaSolveResult {
                solution: format!("swipe-track-{}", target_x as i64),
                confidence: 0.7,
                method: self.method(),
                time_ms: t0.elapsed().as_millis() as u64,
                success: true,
                screenshot: None,
                cookies,
                verified_outcome: None,
            });
        }

        Ok(CaptchaSolveResult::failure(
            self.method(),
            t0.elapsed().as_millis() as u64,
        ))
    }
}

/// Drag a captcha handle from (x0, y0) to (x1, y1) with realistic
/// physics. Uses CDP-level mouse events via page.evaluate so we
/// don't depend on the chromiumoxide mouse API surface.
async fn drag_handle(page: &Page, x0: f64, y0: f64, x1: f64, y1: f64) -> Result<()> {
    let mut rng = rand::rngs::StdRng::from_entropy();
    // mousedown at (x0, y0)
    let _ = page
        .evaluate(format!(
            "(() => {{ const e = new MouseEvent('mousedown', {{ \
                bubbles: true, button: 0, clientX: {x}, clientY: {y} \
            }}); document.elementFromPoint({x}, {y})?.dispatchEvent(e); }})()",
            x = x0,
            y = y0
        ))
        .await;

    // Bezier trajectory with slight overshoot
    let overshoot = rng.gen_range(8.0..18.0);
    let cx1 = x0 + (x1 - x0) * 0.3 + rng.gen_range(-15.0..15.0);
    let cy1 = y0 + rng.gen_range(-10.0..10.0);
    let cx2 = x0 + (x1 - x0) * 0.7 + rng.gen_range(-15.0..15.0);
    let cy2 = y1 + rng.gen_range(-10.0..10.0);
    let steps = 28;
    for i in 0..=steps {
        let t = i as f64 / steps as f64;
        let omt = 1.0 - t;
        let bx = omt.powi(3) * x0
            + 3.0 * omt.powi(2) * t * cx1
            + 3.0 * omt * t.powi(2) * cx2
            + t.powi(3) * (x1 + overshoot);
        let by = omt.powi(3) * y0
            + 3.0 * omt.powi(2) * t * cy1
            + 3.0 * omt * t.powi(2) * cy2
            + t.powi(3) * y1;
        let _ = page
            .evaluate(format!(
                "(() => {{ const e = new MouseEvent('mousemove', {{ \
                    bubbles: true, button: 0, clientX: {x}, clientY: {y} \
                }}); document.elementFromPoint({x}, {y})?.dispatchEvent(e); }})()",
                x = bx,
                y = by
            ))
            .await;
        tokio::time::sleep(Duration::from_millis(rng.gen_range(8..18))).await;
    }
    // Correction: pull back from overshoot to landing
    for i in 0..6 {
        let t = i as f64 / 5.0;
        let bx = x1 + overshoot * (1.0 - t);
        let _ = page
            .evaluate(format!(
                "(() => {{ const e = new MouseEvent('mousemove', {{ \
                    bubbles: true, button: 0, clientX: {x}, clientY: {y} \
                }}); document.elementFromPoint({x}, {y})?.dispatchEvent(e); }})()",
                x = bx,
                y = y1
            ))
            .await;
        tokio::time::sleep(Duration::from_millis(rng.gen_range(20..40))).await;
    }
    // mouseup at (x1, y1)
    let _ = page
        .evaluate(format!(
            "(() => {{ const e = new MouseEvent('mouseup', {{ \
                bubbles: true, button: 0, clientX: {x}, clientY: {y} \
            }}); document.elementFromPoint({x}, {y})?.dispatchEvent(e); }})()",
            x = x1,
            y = y1
        ))
        .await;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn defaults_cover_major_vendors() {
        let s = SliderCaptchaSolver::new();
        let canvases: Vec<_> = s.selectors.iter().map(|x| x.canvas).collect();
        for needle in [
            ".geetest_canvas_slice",
            "#captcha__puzzle",
            ".px-captcha-puzzle",
            "[data-puzzle-canvas]",
        ] {
            assert!(
                canvases.contains(&needle),
                "default selectors must include: {needle}"
            );
        }
    }

    #[test]
    fn supports_slider_and_known_vendors() {
        let s = SliderCaptchaSolver::new();
        assert!(s.supports(&DetectedCaptcha::SliderCaptcha));
        assert!(s.supports(&DetectedCaptcha::Custom("datadome".into())));
        assert!(s.supports(&DetectedCaptcha::Custom("geetest_v3".into())));
        assert!(s.supports(&DetectedCaptcha::Custom("perimeterx_human".into())));
        assert!(s.supports(&DetectedCaptcha::Custom("aws_waf_captcha".into())));
        assert!(!s.supports(&DetectedCaptcha::Custom("friendly_captcha".into())));
        assert!(!s.supports(&DetectedCaptcha::Turnstile));
    }

    #[test]
    fn name_and_method_stable() {
        let s = SliderCaptchaSolver::new();
        assert_eq!(s.name(), "SliderCaptchaSolver");
        assert_eq!(s.method(), SolveMethod::BehavioralBypass);
    }

    #[test]
    fn with_selectors_overrides_default() {
        let custom = SliderSelectors {
            canvas: ".my-canvas",
            handle: ".my-handle",
            success_marker: None,
        };
        let s = SliderCaptchaSolver::new().with_selectors(vec![custom]);
        assert_eq!(s.selectors.len(), 1);
        assert_eq!(s.selectors[0].canvas, ".my-canvas");
    }

    #[test]
    fn gap_detect_js_uses_canvas_imagedata() {
        assert!(GAP_DETECT_JS.contains("getImageData"));
        assert!(GAP_DETECT_JS.contains("HTMLCanvasElement"));
    }

    #[test]
    fn anchors_js_returns_handle_and_canvas_geometry() {
        assert!(ANCHORS_JS.contains("getBoundingClientRect"));
        assert!(ANCHORS_JS.contains("handleX"));
        assert!(ANCHORS_JS.contains("canvasLeft"));
    }
}