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
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
//! C FFI bindings for chaser-cf
//!
//! This module provides a C-compatible API for using chaser-cf from
//! other languages (Python, Go, Node.js, C/C++, etc.).
//!
//! # Async Model
//!
//! Operations are callback-based. Each async function takes:
//! - Required parameters (URL, etc.)
//! - `user_data: *mut c_void` - Caller's context, returned untouched in callback
//! - `callback: ChaserCallback` - Function called with result
//!
//! # Thread Safety
//!
//! The library maintains an internal Tokio runtime. All operations are thread-safe
//! and can be called from any thread.
//!
//! # Memory Management
//!
//! - Strings returned to C (via callbacks) must be freed with `chaser_free_string`
//! - The library does not take ownership of `user_data`
//!
//! # Example (C)
//!
//! ```c
//! #include "chaser_cf.h"
//!
//! void on_result(const char* json_result, void* ctx) {
//!     printf("Result: %s\n", json_result);
//!     chaser_free_string((char*)json_result);
//! }
//!
//! int main() {
//!     ChaserConfig config = chaser_config_default();
//!     int err = chaser_init(&config);
//!     if (err != 0) return 1;
//!
//!     chaser_solve_waf_async("https://example.com", NULL, NULL, on_result);
//!
//!     // Wait for callback...
//!     sleep(10);
//!
//!     chaser_shutdown();
//!     return 0;
//! }
//! ```

use crate::core::{ChaserCF, ChaserConfig};
use crate::error::ChaserError;
use crate::models::{ChaserResult as ChaserResultModel, Profile, ProxyConfig};

use once_cell::sync::OnceCell;
use std::ffi::{c_char, c_void, CStr, CString};
use std::ptr;
use std::sync::Arc;
use tokio::runtime::Runtime;
use tokio::sync::RwLock;

/// Global Tokio runtime for async operations
static RUNTIME: OnceCell<Runtime> = OnceCell::new();

/// Global ChaserCF instance
static CHASER: OnceCell<Arc<RwLock<Option<ChaserCF>>>> = OnceCell::new();

/// Callback type for async operations
///
/// # Parameters
/// - `result`: JSON-encoded result string (must be freed with `chaser_free_string`)
/// - `user_data`: The user_data pointer passed to the async function
pub type ChaserCallback = extern "C" fn(result: *const c_char, user_data: *mut c_void);

/// C-compatible configuration structure
#[repr(C)]
pub struct ChaserConfigFFI {
    /// Maximum concurrent browser contexts
    pub context_limit: u32,
    /// Request timeout in milliseconds
    pub timeout_ms: u64,
    /// Profile: 0 = Windows, 1 = Linux, 2 = macOS
    pub profile: u32,
    /// Whether to defer browser init (0 = false, 1 = true)
    pub lazy_init: u32,
    /// Whether to run headless (0 = false, 1 = true)
    pub headless: u32,
    /// Chrome binary path (NULL for auto-detect)
    pub chrome_path: *const c_char,
}

/// C-compatible proxy configuration
#[repr(C)]
pub struct ProxyConfigFFI {
    /// Proxy host
    pub host: *const c_char,
    /// Proxy port
    pub port: u16,
    /// Optional username (NULL if none)
    pub username: *const c_char,
    /// Optional password (NULL if none)
    pub password: *const c_char,
}

/// Get default configuration
#[no_mangle]
pub extern "C" fn chaser_config_default() -> ChaserConfigFFI {
    ChaserConfigFFI {
        context_limit: 20,
        timeout_ms: 60000,
        profile: 0, // Windows
        lazy_init: 0,
        headless: 0,
        chrome_path: ptr::null(),
    }
}

/// Initialize chaser-cf
///
/// # Parameters
/// - `config`: Configuration pointer (NULL for defaults)
///
/// # Returns
/// - 0 on success
/// - Non-zero error code on failure
///
/// # Safety
/// - `config` must be either NULL or a valid pointer to a `ChaserConfigFFI` struct
/// - If `config.chrome_path` is not NULL, it must be a valid null-terminated C string
#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn chaser_init(config: *const ChaserConfigFFI) -> i32 {
    // Initialize runtime
    let runtime = RUNTIME.get_or_try_init(|| {
        tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
    });

    let runtime = match runtime {
        Ok(rt) => rt,
        Err(_) => return -1,
    };

    // Parse config
    let chaser_config = if config.is_null() {
        ChaserConfig::default()
    } else {
        unsafe { config_from_ffi(&*config) }
    };

    // Initialize ChaserCF
    let result = runtime.block_on(async {
        let suite = ChaserCF::new(chaser_config).await?;
        Ok::<_, ChaserError>(suite)
    });

    match result {
        Ok(suite) => {
            let _ = CHASER.set(Arc::new(RwLock::new(Some(suite))));
            0
        }
        Err(e) => e.code(),
    }
}

/// Shutdown chaser-cf and release resources
#[no_mangle]
pub extern "C" fn chaser_shutdown() {
    if let Some(runtime) = RUNTIME.get() {
        if let Some(chaser) = CHASER.get() {
            runtime.block_on(async {
                let mut guard = chaser.write().await;
                if let Some(suite) = guard.take() {
                    suite.shutdown().await;
                }
            });
        }
    }
}

/// Check if chaser-cf is initialized and ready
///
/// # Returns
/// - 1 if ready
/// - 0 if not ready
#[no_mangle]
pub extern "C" fn chaser_is_ready() -> i32 {
    if let (Some(runtime), Some(chaser)) = (RUNTIME.get(), CHASER.get()) {
        let ready = runtime.block_on(async {
            let guard = chaser.read().await;
            match guard.as_ref() {
                Some(s) => s.is_ready().await,
                None => false,
            }
        });
        if ready {
            1
        } else {
            0
        }
    } else {
        0
    }
}

/// Solve WAF session asynchronously
///
/// # Parameters
/// - `url`: Target URL (null-terminated C string)
/// - `proxy`: Optional proxy config (NULL for no proxy)
/// - `user_data`: User context, returned in callback
/// - `callback`: Function called with JSON result
///
/// # Safety
/// - `url` must be a valid null-terminated C string
/// - `proxy` must be either NULL or a valid pointer to a `ProxyConfigFFI` struct
/// - `callback` must be a valid function pointer
#[no_mangle]
pub unsafe extern "C" fn chaser_solve_waf_async(
    url: *const c_char,
    proxy: *const ProxyConfigFFI,
    user_data: *mut c_void,
    callback: ChaserCallback,
) {
    let url = match cstr_to_string(url) {
        Some(s) => s,
        None => {
            let result = make_error_json(ChaserError::InvalidUrl("NULL url".to_string()));
            callback(result, user_data);
            return;
        }
    };

    let proxy = proxy_from_ffi(proxy);

    spawn_async_operation(
        user_data,
        callback,
        AsyncOp {
            url,
            site_key: None,
            proxy,
            op_type: OpType::WafSession,
        },
    );
}

/// Get page source asynchronously
///
/// # Parameters
/// - `url`: Target URL (null-terminated C string)
/// - `proxy`: Optional proxy config (NULL for no proxy)
/// - `user_data`: User context, returned in callback
/// - `callback`: Function called with JSON result
///
/// # Safety
/// - `url` must be a valid null-terminated C string
/// - `proxy` must be either NULL or a valid pointer to a `ProxyConfigFFI` struct
/// - `callback` must be a valid function pointer
#[no_mangle]
pub unsafe extern "C" fn chaser_get_source_async(
    url: *const c_char,
    proxy: *const ProxyConfigFFI,
    user_data: *mut c_void,
    callback: ChaserCallback,
) {
    let url = match cstr_to_string(url) {
        Some(s) => s,
        None => {
            let result = make_error_json(ChaserError::InvalidUrl("NULL url".to_string()));
            callback(result, user_data);
            return;
        }
    };

    let proxy = proxy_from_ffi(proxy);

    spawn_async_operation(
        user_data,
        callback,
        AsyncOp {
            url,
            site_key: None,
            proxy,
            op_type: OpType::Source,
        },
    );
}

/// Solve Turnstile (full page) asynchronously
///
/// # Parameters
/// - `url`: URL with Turnstile widget
/// - `proxy`: Optional proxy config (NULL for no proxy)
/// - `user_data`: User context, returned in callback
/// - `callback`: Function called with JSON result
///
/// # Safety
/// - `url` must be a valid null-terminated C string
/// - `proxy` must be either NULL or a valid pointer to a `ProxyConfigFFI` struct
/// - `callback` must be a valid function pointer
#[no_mangle]
pub unsafe extern "C" fn chaser_solve_turnstile_async(
    url: *const c_char,
    proxy: *const ProxyConfigFFI,
    user_data: *mut c_void,
    callback: ChaserCallback,
) {
    let url = match cstr_to_string(url) {
        Some(s) => s,
        None => {
            let result = make_error_json(ChaserError::InvalidUrl("NULL url".to_string()));
            callback(result, user_data);
            return;
        }
    };

    let proxy = proxy_from_ffi(proxy);

    spawn_async_operation(
        user_data,
        callback,
        AsyncOp {
            url,
            site_key: None,
            proxy,
            op_type: OpType::Turnstile,
        },
    );
}

/// Solve Turnstile (minimal page) asynchronously
///
/// # Parameters
/// - `url`: Origin URL for Turnstile
/// - `site_key`: Turnstile site key
/// - `proxy`: Optional proxy config (NULL for no proxy)
/// - `user_data`: User context, returned in callback
/// - `callback`: Function called with JSON result
///
/// # Safety
/// - `url` must be a valid null-terminated C string
/// - `site_key` must be a valid null-terminated C string
/// - `proxy` must be either NULL or a valid pointer to a `ProxyConfigFFI` struct
/// - `callback` must be a valid function pointer
#[no_mangle]
pub unsafe extern "C" fn chaser_solve_turnstile_min_async(
    url: *const c_char,
    site_key: *const c_char,
    proxy: *const ProxyConfigFFI,
    user_data: *mut c_void,
    callback: ChaserCallback,
) {
    let url = match cstr_to_string(url) {
        Some(s) => s,
        None => {
            let result = make_error_json(ChaserError::InvalidUrl("NULL url".to_string()));
            callback(result, user_data);
            return;
        }
    };

    let site_key = match cstr_to_string(site_key) {
        Some(s) => s,
        None => {
            let result = make_error_json(ChaserError::MissingParameter("site_key".to_string()));
            callback(result, user_data);
            return;
        }
    };

    let proxy = proxy_from_ffi(proxy);

    spawn_async_operation(
        user_data,
        callback,
        AsyncOp {
            url,
            site_key: Some(site_key),
            proxy,
            op_type: OpType::TurnstileMin,
        },
    );
}

/// Free a string returned by chaser-cf
///
/// Must be called on any string returned via callbacks.
///
/// # Safety
/// - `s` must be either NULL or a valid pointer previously returned by chaser-cf callbacks
/// - `s` must not be freed more than once
#[no_mangle]
pub unsafe extern "C" fn chaser_free_string(s: *mut c_char) {
    if !s.is_null() {
        let _ = CString::from_raw(s);
    }
}

// ============================================================================
// Internal helpers
// ============================================================================

/// Convert C string to Rust String
unsafe fn cstr_to_string(s: *const c_char) -> Option<String> {
    if s.is_null() {
        return None;
    }
    CStr::from_ptr(s).to_str().ok().map(|s| s.to_owned())
}

/// Convert FFI config to Rust config
unsafe fn config_from_ffi(ffi: &ChaserConfigFFI) -> ChaserConfig {
    let mut config = ChaserConfig::default()
        .with_context_limit(ffi.context_limit as usize)
        .with_timeout_ms(ffi.timeout_ms)
        .with_lazy_init(ffi.lazy_init != 0)
        .with_headless(ffi.headless != 0);

    config.profile = match ffi.profile {
        1 => Profile::Linux,
        2 => Profile::Macos,
        _ => Profile::Windows,
    };

    if !ffi.chrome_path.is_null() {
        if let Some(path) = cstr_to_string(ffi.chrome_path) {
            config = config.with_chrome_path(path);
        }
    }

    config
}

/// Convert FFI proxy to Rust proxy
unsafe fn proxy_from_ffi(ffi: *const ProxyConfigFFI) -> Option<ProxyConfig> {
    if ffi.is_null() {
        return None;
    }

    let ffi = &*ffi;
    let host = cstr_to_string(ffi.host)?;

    let mut proxy = ProxyConfig::new(host, ffi.port);

    if let (Some(username), Some(password)) =
        (cstr_to_string(ffi.username), cstr_to_string(ffi.password))
    {
        proxy = proxy.with_auth(username, password);
    }

    Some(proxy)
}

/// Make error JSON string
fn make_error_json(error: ChaserError) -> *const c_char {
    let result = ChaserResultModel::error(error.code(), error.to_string());
    let json = serde_json::to_string(&result).unwrap_or_else(|_| {
        r#"{"type":"Error","data":{"code":99,"message":"Serialization failed"}}"#.to_string()
    });
    CString::new(json).unwrap().into_raw()
}

/// Wrapper struct for async operations that captures all needed data
struct AsyncOp {
    url: String,
    site_key: Option<String>,
    proxy: Option<ProxyConfig>,
    op_type: OpType,
}

#[derive(Clone, Copy)]
enum OpType {
    WafSession,
    Source,
    Turnstile,
    TurnstileMin,
}

/// Spawn an async operation on the runtime
fn spawn_async_operation(user_data: *mut c_void, callback: ChaserCallback, op: AsyncOp) {
    let user_data = user_data as usize; // Convert to usize for Send

    let runtime = match RUNTIME.get() {
        Some(rt) => rt,
        None => {
            let result = make_error_json(ChaserError::NotInitialized);
            callback(result, user_data as *mut c_void);
            return;
        }
    };

    let chaser = match CHASER.get() {
        Some(g) => g.clone(),
        None => {
            let result = make_error_json(ChaserError::NotInitialized);
            callback(result, user_data as *mut c_void);
            return;
        }
    };

    runtime.spawn(async move {
        let result = {
            let guard = chaser.read().await;
            match guard.as_ref() {
                Some(suite) => match op.op_type {
                    OpType::WafSession => match suite.solve_waf_session(&op.url, op.proxy).await {
                        Ok(session) => ChaserResultModel::waf_session(session),
                        Err(e) => ChaserResultModel::error(e.code(), e.to_string()),
                    },
                    OpType::Source => match suite.get_source(&op.url, op.proxy).await {
                        Ok(source) => ChaserResultModel::source(source),
                        Err(e) => ChaserResultModel::error(e.code(), e.to_string()),
                    },
                    OpType::Turnstile => match suite.solve_turnstile(&op.url, op.proxy).await {
                        Ok(token) => ChaserResultModel::token(token),
                        Err(e) => ChaserResultModel::error(e.code(), e.to_string()),
                    },
                    OpType::TurnstileMin => {
                        let site_key = op.site_key.as_deref().unwrap_or("");
                        match suite.solve_turnstile_min(&op.url, site_key, op.proxy).await {
                            Ok(token) => ChaserResultModel::token(token),
                            Err(e) => ChaserResultModel::error(e.code(), e.to_string()),
                        }
                    }
                },
                None => ChaserResultModel::error(
                    ChaserError::NotInitialized.code(),
                    "chaser-cf not initialized",
                ),
            }
        };

        let json = serde_json::to_string(&result).unwrap_or_else(|_| {
            r#"{"type":"Error","data":{"code":99,"message":"Serialization failed"}}"#.to_string()
        });
        let c_result = CString::new(json).unwrap().into_raw();

        callback(c_result, user_data as *mut c_void);
    });
}