cfrp_detector/lib.rs
1//! Cloudflare edge detection and network quality probing.
2//!
3//! This crate implements a multi-layer detection pipeline to decide whether a
4//! given `(IP, port)` is actually a Cloudflare edge POP, and then optionally
5//! runs a multi-threaded throughput speed test against the host to measure
6//! real-world download speed.
7//!
8//! # Architecture
9//!
10//! The library is organised around a few core components:
11//!
12//! * **[`Detector`]** — orchestrator that pulls together every layer below and
13//! produces a [`DetectionResult`] with a [`Confidence`] score.
14//! * **[`CloudflareRanges`]** + **[`LocationStore`]** — cached data sources
15//! loaded from the official Cloudflare CIDR lists and a community-maintained
16//! colo code → geography mapping.
17//! * **[`ProbeEngine`]** (inside `probe`) — TLS + HTTP probes that check for
18//! Cloudflare-specific response headers and certificate fingerprints.
19//! * **[`SpeedTester`]** — multi-threaded byte-range downloader that measures
20//! effective throughput to a specific colo.
21//! * **[`MasscanScanner`]** *(optional)* — drives an external `masscan` process
22//! for large-scale SYN scans before feeding the results into batch detection.
23//!
24//! # Example
25//!
26//! ```no_run
27//! # use cfrp_detector::{Detector, DetectorConfig, Target, parse_target};
28//! # use std::net::{IpAddr, Ipv4Addr};
29//! # #[tokio::main]
30//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
31//! // 1. 构建默认配置的检测器 (首次加载会下载 CF CIDR 和 colo 数据)
32//! let detector = Detector::new(DetectorConfig::default()).await?;
33//!
34//! // 2a. 用 Target::new 构造 (IP + port)
35//! let cf_dns = Target::new(IpAddr::V4(Ipv4Addr::new(1, 1, 1, 1)), 443);
36//!
37//! // 2b. 或者用 parse_target 从字符串解析
38//! let cf_home = parse_target("104.16.132.229:443", 443).map_err(|e| e.to_string())?;
39//!
40//! // 3. 单独探测
41//! let res = detector.detect(&cf_home, None).await?;
42//! let colo = res.edge_info.as_ref().and_then(|e| e.colo_code.as_deref()).unwrap_or("?");
43//! println!("{:?} colo={} edge={}", res.confidence, colo, res.is_cloudflare_edge);
44//! # Ok(())
45//! # }
46//! ```
47
48pub mod cache;
49pub mod cidr;
50pub mod connector;
51pub mod detector;
52pub mod error;
53pub mod governor;
54pub mod location;
55pub mod masscan;
56pub mod masscan_pipeline;
57pub mod model;
58pub mod probe;
59pub mod speedtest;
60
61pub use cache::{CacheConfig, FileCache};
62pub use cidr::{CidrSource, CloudflareCidrs, CloudflareRanges};
63pub use connector::{
64 ConnectorConfig, HandshakeType, PinnedClientConfig, PinnedConnector, PinnedDownload, Timing,
65 build_rustls_client_config, build_rustls_client_config_sized, connect_tcp, connect_tls,
66};
67pub use detector::{AdaptiveConfig, BatchProgress, Detector, DetectorConfig, GovernorFeedback};
68pub use error::{DetectorError, Result, RetryConfig, is_retryable_error};
69pub use governor::{
70 FdCounter, GovernorSnapshot, MockFdCounter, ResourceGovernor, ResourceGovernorConfig,
71 ResourceKind, SystemFdCounter, classify_resource_error,
72};
73pub use location::{CfLocation, LocationSource, LocationStore};
74pub use masscan::{
75 AsnTask, MasscanConfig, MasscanScanner, NetworkInterface, OpenPort, ScanMode,
76 ScanPipelineConfig, clear_cache, parse_masscan_output,
77};
78pub use masscan_pipeline::{
79 MasscanPipeline, PipelineAsnTask, PipelineOptions, PipelineOutput, PipelineResult,
80 guess_tls_by_port,
81};
82pub use model::{
83 BatchResult, BatchTarget, Confidence, DetectionResult, EdgeInfo, Protocol, Target, parse_target,
84};
85pub use probe::ProbeConfig;
86pub use speedtest::{SpeedTestConfig, SpeedTestResult, SpeedTester};