cfprobe 0.1.0

Cloudflare reverse proxy and CDN detection engine
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
use std::sync::Arc;
use std::time::{Duration, Instant};

use futures::{Stream, StreamExt, stream};

use serde::Serialize;

use tokio::sync::Mutex;
use tokio::time::timeout;

use tokio_util::sync::CancellationToken;

use crate::{CfProbeError, DetectionClassification};

use super::{CfProbe, ProbeResult, Target};

/// 批量扫描配置:并发度、单目标超时、RPS 限流、容量上限。
///
/// # Default
///
/// ```text
/// concurrency           = 32
/// target_timeout        = 30s
/// requests_per_second   = 无限制
/// max_targets           = 无限制
/// ```
#[derive(Debug, Clone)]
pub struct BatchScanConfig {
    /// 同时执行的 Target 数量。
    ///
    /// 每个 Target 内部还会并发进行 DNS / TLS / HTTP 三路探测,
    /// 因此实际"飞"中的连接数约为 `concurrency × (1 + 解析器数 + 1 + 1)`,
    /// 请按出口带宽与 fd 上限谨慎设置。
    pub concurrency: usize,

    /// 单个 Target 的总超时(包含所有阶段)。超过后该目标置为 `TimedOut`,不影响其他目标。
    pub target_timeout: Duration,

    /// 每秒最多启动多少个新 Target。`None` 表示不限制启动速率。
    ///
    /// 用于公网批量扫描时控制对端压力。实现为"预约式 sleep + 解锁再等待",
    /// 不会因持锁 sleep 导致所有任务串行。
    pub requests_per_second: Option<u32>,

    /// 本次 Batch 允许的最大 Target 数量。`None` 表示不限制。
    ///
    /// 用于防止误输入超大文件把内存打爆。
    pub max_targets: Option<usize>,
}

impl Default for BatchScanConfig {
    fn default() -> Self {
        Self {
            concurrency: 32,

            target_timeout: Duration::from_secs(30),

            requests_per_second: None,

            max_targets: None,
        }
    }
}

impl BatchScanConfig {
    /// 校验配置合法性(并发度非零、超时非零等)。
    pub fn validate(&self) -> Result<(), CfProbeError> {
        if self.concurrency == 0 {
            return Err(CfProbeError::InvalidResponse(
                "batch concurrency cannot be 0".to_string(),
            ));
        }

        if self.target_timeout.is_zero() {
            return Err(CfProbeError::InvalidResponse(
                "target timeout cannot be zero".to_string(),
            ));
        }

        if let Some(rps) = self.requests_per_second {
            if rps == 0 {
                return Err(CfProbeError::InvalidResponse(
                    "requests_per_second cannot be 0".to_string(),
                ));
            }
        }

        if let Some(max_targets) = self.max_targets {
            if max_targets == 0 {
                return Err(CfProbeError::InvalidResponse(
                    "max_targets cannot be 0".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// 链式设置并发度。
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency;

        self
    }

    /// 链式设置单目标总超时。
    pub fn with_target_timeout(mut self, timeout: Duration) -> Self {
        self.target_timeout = timeout;

        self
    }

    /// 链式设置 RPS 启动限流(`None` 为不限)。
    pub fn with_requests_per_second(mut self, rps: Option<u32>) -> Self {
        self.requests_per_second = rps;

        self
    }

    /// 链式设置本次 Batch 的最大目标数上限(`None` 为不限)。
    pub fn with_max_targets(mut self, max_targets: Option<usize>) -> Self {
        self.max_targets = max_targets;

        self
    }
}

/// 批量扫描中单项目的执行状态。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum BatchItemStatus {
    /// 探测成功完成。
    Completed,

    /// 探测执行中发生错误(非超时 / 非取消)。
    Failed,

    /// 单目标执行超过 `BatchScanConfig.target_timeout`。
    TimedOut,

    /// 由 `CancellationToken` 主动取消。
    Cancelled,
}

/// 批量扫描中单个目标的结果。
#[derive(Debug, Clone, Serialize)]
pub struct BatchItemResult {
    /// 目标在原始输入切片中的下标(用于恢复输入顺序)。
    pub index: usize,

    /// 原始目标定义。
    pub target: Target,

    /// 执行状态。
    pub status: BatchItemStatus,

    /// 仅当 `status == Completed` 时有值。
    pub result: Option<ProbeResult>,

    /// 失败 / 超时 / 取消时的可读错误信息。
    pub error: Option<String>,

    /// 本目标从启动到完成的真实耗时。
    pub elapsed: Duration,
}

impl BatchItemResult {
    /// `status == Completed` 的快捷判断。
    pub fn is_completed(&self) -> bool {
        self.status == BatchItemStatus::Completed
    }

    /// `status == Failed` 的快捷判断。
    pub fn is_failed(&self) -> bool {
        self.status == BatchItemStatus::Failed
    }

    /// `status == TimedOut` 的快捷判断。
    pub fn is_timed_out(&self) -> bool {
        self.status == BatchItemStatus::TimedOut
    }

    /// `status == Cancelled` 的快捷判断。
    pub fn is_cancelled(&self) -> bool {
        self.status == BatchItemStatus::Cancelled
    }

    /// 快捷取出最终分类(仅 `Completed` 时有值)。
    pub fn classification(&self) -> Option<DetectionClassification> {
        self.result
            .as_ref()
            .map(|result| result.detection.classification)
    }
}

/// 整个批量扫描的汇总结果。
#[derive(Debug, Clone, Serialize)]
pub struct BatchResult {
    /// 总目标数。
    pub total: usize,

    /// 探测成功完成的数量。
    pub completed: usize,

    /// 探测失败的数量(不含超时 / 取消)。
    pub failed: usize,

    /// 单目标超时数量。
    pub timed_out: usize,

    /// 被取消的数量。
    pub cancelled: usize,

    /// Completed 中判定为 Cloudflare 的数量。
    pub cloudflare: usize,

    /// Completed 中判定为 NotCloudflare 的数量。
    pub not_cloudflare: usize,

    /// Completed 中判定为 Unknown 的数量。
    pub unknown: usize,

    /// 整个 batch 从启动到收集完毕的耗时。
    pub elapsed: Duration,

    /// 所有单项结果;[`scan()`](CfProbe::scan) 保证按输入顺序排列,
    /// 而 `scan_unordered()` 返回的 Stream 是完成顺序。
    pub items: Vec<BatchItemResult>,
}

impl BatchResult {
    /// 成功率:`completed / total`。
    pub fn success_rate(&self) -> f64 {
        if self.total == 0 {
            return 0.0;
        }

        self.completed as f64 / self.total as f64
    }

    /// 已完成目标中 Cloudflare 所占比例:`cloudflare / completed`。
    pub fn cloudflare_rate(&self) -> f64 {
        if self.completed == 0 {
            return 0.0;
        }

        self.cloudflare as f64 / self.completed as f64
    }
}

/// Target 启动速率限制器。
#[derive(Debug)]
struct StartRateLimiter {
    interval: Option<Duration>,

    next_start: Mutex<Instant>,
}

impl StartRateLimiter {
    fn new(requests_per_second: Option<u32>) -> Self {
        let interval = requests_per_second.map(|rps| Duration::from_secs_f64(1.0 / rps as f64));

        Self {
            interval,

            next_start: Mutex::new(Instant::now()),
        }
    }

    /// 等待一个 Target 的启动配额。
    ///
    /// 返回:
    ///
    /// true  = 可以启动
    /// false = 已经取消
    async fn wait(&self, cancellation: &CancellationToken) -> bool {
        let Some(interval) = self.interval else {
            return !cancellation.is_cancelled();
        };

        /*
         * 重要:不能在持锁期间 sleep!
         *
         * 之前:guard = lock().await; sleep(wait); *guard = ...
         *       → 所有并发任务串行在锁上等待前一个任务 sleep 完。
         * 现在:
         *   1) 快速持锁:计算 wait 到什么时候 (deadline),并预约 next_start。
         *   2) 释放锁后再真正 sleep,让其他 task 能立刻进入步骤 1 预约自己的槽位。
         */
        let deadline = {
            let mut next_start = self.next_start.lock().await;
            let now = Instant::now();
            let start_at = if *next_start > now { *next_start } else { now };
            let deadline = start_at;
            *next_start = start_at + interval;
            deadline
        };

        let now = Instant::now();
        if deadline > now {
            let wait = deadline - now;
            tokio::select! {
                _ = cancellation.cancelled() => {
                    return false;
                }
                _ = tokio::time::sleep(wait) => {}
            }
        }

        !cancellation.is_cancelled()
    }
}

impl CfProbe {
    /// 等待全部目标完成,按**输入顺序**返回汇总结果。
    ///
    /// 内部创建独立的 `CancellationToken`;如需外部取消请用
    /// [`scan_with_cancel`](Self::scan_with_cancel)。
    pub async fn scan(
        &self,
        targets: Vec<Target>,
        config: BatchScanConfig,
    ) -> Result<BatchResult, CfProbeError> {
        self.scan_with_cancel(targets, config, CancellationToken::new())
            .await
    }

    /// 可取消版本的 [`scan()`](Self::scan)。
    ///
    /// `CancellationToken` 被触发时,尚未启动的目标会立即置为 `Cancelled`,
    /// 已在执行中的目标会在各阶段检查点尽快退出并返回 `Cancelled`。
    /// 返回结果中 `items` 仍按输入顺序排序。
    pub async fn scan_with_cancel(
        &self,
        targets: Vec<Target>,
        config: BatchScanConfig,
        cancellation: CancellationToken,
    ) -> Result<BatchResult, CfProbeError> {
        config.validate()?;

        validate_batch_size(targets.len(), config.max_targets)?;

        let total = targets.len();

        let started = Instant::now();

        let mut stream = self.scan_unordered_with_cancel(targets, config, cancellation)?;

        let mut items = Vec::with_capacity(total);

        while let Some(item) = stream.next().await {
            items.push(item);
        }

        /*
         * buffer_unordered() 是无序完成的。
         *
         * scan() 对外保证稳定输入顺序。
         */
        items.sort_by_key(|item| item.index);

        Ok(build_batch_result(items, started.elapsed()))
    }

    /// 以 Stream 形式按**完成顺序**返回单项结果(不等待全部完成)。
    ///
    /// 适合边扫描边落盘或驱动进度 UI。内部自动新建 `CancellationToken`,
    /// 若需外部取消请使用 [`scan_unordered_with_cancel`](Self::scan_unordered_with_cancel)。
    ///
    /// 注意:调用者 drop Stream 后未消费的结果不会再产生,这是 Stream 的常规语义。
    pub fn scan_unordered(
        &self,
        targets: Vec<Target>,
        config: BatchScanConfig,
    ) -> Result<impl Stream<Item = BatchItemResult> + 'static, CfProbeError> {
        self.scan_unordered_with_cancel(targets, config, CancellationToken::new())
    }

    /// 可取消版本的 [`scan_unordered()`](Self::scan_unordered)。
    pub fn scan_unordered_with_cancel(
        &self,
        targets: Vec<Target>,
        config: BatchScanConfig,
        cancellation: CancellationToken,
    ) -> Result<impl Stream<Item = BatchItemResult> + 'static, CfProbeError> {
        config.validate()?;

        validate_batch_size(targets.len(), config.max_targets)?;

        let limiter = Arc::new(StartRateLimiter::new(config.requests_per_second));

        let probe = self.clone();

        let concurrency = config.concurrency;

        let target_timeout = config.target_timeout;

        let stream = stream::iter(targets.into_iter().enumerate())
            .map(move |(index, target)| {
                let probe = probe.clone();

                let limiter = limiter.clone();

                let cancellation = cancellation.clone();

                async move {
                    execute_one(probe, index, target, target_timeout, limiter, cancellation).await
                }
            })
            .buffer_unordered(concurrency);

        Ok(stream)
    }
}

/// 检查 Batch 大小。
fn validate_batch_size(actual: usize, max_targets: Option<usize>) -> Result<(), CfProbeError> {
    let Some(max_targets) = max_targets else {
        return Ok(());
    };

    if actual > max_targets {
        return Err(CfProbeError::InvalidResponse(format!(
            "batch contains {} targets, exceeding max_targets {}",
            actual, max_targets,
        )));
    }

    Ok(())
}

/// 执行单个 Target。
async fn execute_one(
    probe: CfProbe,

    index: usize,

    target: Target,

    target_timeout: Duration,

    limiter: Arc<StartRateLimiter>,

    cancellation: CancellationToken,
) -> BatchItemResult {
    let started = Instant::now();

    /*
     * 这里只做 Target 自身的结构校验:
     *
     * - hostname 非空
     * - hostname 合法
     * - port != 0
     *
     * 真正的 TargetPolicy / SSRF 校验
     * 仍然由 CfProbe::detect_with_cancel()
     * 统一执行。
     *
     * 因此不存在 CLI / Batch / API
     * 绕过 TargetPolicy 的问题。
     */
    if let Err(error) = target.validate() {
        return BatchItemResult {
            index,

            target,

            status: BatchItemStatus::Failed,

            result: None,

            error: Some(error.to_string()),

            elapsed: started.elapsed(),
        };
    }

    /*
     * Cancellation 优先级高于 RPS。
     */
    let can_start = limiter.wait(&cancellation).await;

    if !can_start {
        return BatchItemResult {
            index,

            target,

            status: BatchItemStatus::Cancelled,

            result: None,

            error: Some("batch was cancelled".to_string()),

            elapsed: started.elapsed(),
        };
    }

    /*
     * 外层 Target timeout。
     *
     * 这是整个:
     *
     * TargetPolicy
     * DNS
     * TLS
     * HTTP
     * Evidence
     *
     * 的最终保险。
     */
    let result = timeout(
        target_timeout,
        probe.detect_with_cancel(target.clone(), cancellation.clone()),
    )
    .await;

    let elapsed = started.elapsed();

    match result {
        /*
         * Target 完整完成。
         */
        Ok(Ok(result)) => BatchItemResult {
            index,

            target,

            status: BatchItemStatus::Completed,

            result: Some(result),

            error: None,

            elapsed,
        },

        /*
         * CfProbe 主动响应 cancellation。
         */
        Ok(Err(CfProbeError::Cancelled)) => BatchItemResult {
            index,

            target,

            status: BatchItemStatus::Cancelled,

            result: None,

            error: Some("batch was cancelled".to_string()),

            elapsed,
        },

        /*
         * 其他 Probe Error。
         */
        Ok(Err(error)) => BatchItemResult {
            index,

            target,

            status: BatchItemStatus::Failed,

            result: None,

            error: Some(error.to_string()),

            elapsed,
        },

        /*
         * 外层 Target timeout。
         *
         * 不等同于 Cancelled。
         */
        Err(_) => BatchItemResult {
            index,

            target,

            status: BatchItemStatus::TimedOut,

            result: None,

            error: Some(format!("target probe timed out after {:?}", target_timeout,)),

            elapsed,
        },
    }
}

/// 将单个 item 集合转换成 BatchResult。
fn build_batch_result(items: Vec<BatchItemResult>, elapsed: Duration) -> BatchResult {
    let total = items.len();

    let completed = items
        .iter()
        .filter(|item| item.status == BatchItemStatus::Completed)
        .count();

    let failed = items
        .iter()
        .filter(|item| item.status == BatchItemStatus::Failed)
        .count();

    let timed_out = items
        .iter()
        .filter(|item| item.status == BatchItemStatus::TimedOut)
        .count();

    let cancelled = items
        .iter()
        .filter(|item| item.status == BatchItemStatus::Cancelled)
        .count();

    let cloudflare = items
        .iter()
        .filter(|item| item.classification() == Some(DetectionClassification::Cloudflare))
        .count();

    let not_cloudflare = items
        .iter()
        .filter(|item| item.classification() == Some(DetectionClassification::NotCloudflare))
        .count();

    let unknown = items
        .iter()
        .filter(|item| item.classification() == Some(DetectionClassification::Unknown))
        .count();

    BatchResult {
        total,

        completed,

        failed,

        timed_out,

        cancelled,

        cloudflare,

        not_cloudflare,

        unknown,

        elapsed,

        items,
    }
}