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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
use std::collections::{HashMap, HashSet};
use std::net::IpAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};

use futures::stream::{FuturesUnordered, StreamExt};
use tokio::sync::{RwLock, Semaphore};
use tokio_util::sync::CancellationToken;

use crate::CloudflareRanges;
use crate::error::CfProbeError;

use super::backend::DnsBackend;
use super::model::{DnsDetection, DnsDetectionStatus, ResolverHealth, ResolverObservation};
use super::pool::DnsCache;

/// DnsDetector 使用的解析器条目(名称 + 实现)。
#[derive(Clone)]
pub struct DnsResolverEntry {
    /// 解析器标识名(local / cloudflare / google 等)。
    pub name: String,
    /// 实际执行 DNS 查询的后端。
    pub backend: Arc<dyn DnsBackend>,
}

impl DnsResolverEntry {
    /// 构造一个解析器条目。
    pub fn new(name: impl Into<String>, backend: Arc<dyn DnsBackend>) -> Self {
        Self {
            name: name.into(),
            backend,
        }
    }
}

/// 多解析器 DNS 探测器。
///
/// 功能:
/// - 并行调用多个 DNS 后端
/// - 失败后端自动熔断(ban 一段时间)
/// - 支持可选的答案缓存
/// - 健康度统计,用于后续路由策略
pub struct DnsDetector {
    resolvers: Vec<DnsResolverEntry>,
    max_concurrency: usize,
    cache: Option<Arc<DnsCache>>,
    health: Arc<RwLock<HashMap<String, ResolverHealth>>>,
    banned_until: Arc<RwLock<HashMap<String, Instant>>>,
    cname_max_depth: usize,
    ban_duration: Duration,
}

impl DnsDetector {
    /// 用一组解析器构造 DnsDetector。
    pub fn new(resolvers: Vec<DnsResolverEntry>) -> Self {
        Self {
            resolvers,
            max_concurrency: 8,
            cache: None,
            health: Arc::new(RwLock::new(HashMap::new())),
            banned_until: Arc::new(RwLock::new(HashMap::new())),
            cname_max_depth: 5,
            ban_duration: Duration::from_secs(30),
        }
    }

    /// 设置失败解析器的熔断时长。
    pub fn with_ban_duration(mut self, duration: Duration) -> Self {
        self.ban_duration = duration;
        self
    }

    /// 从 `DnsPool` 复制后端 + 缓存,构造 DnsDetector。
    pub fn from_pool(pool: &crate::dns::pool::DnsPool) -> Self {
        let entries: Vec<DnsResolverEntry> = pool
            .backends()
            .iter()
            .map(|(name, backend)| DnsResolverEntry {
                name: name.clone(),
                backend: Arc::clone(backend),
            })
            .collect();

        let mut detector = Self::new(entries);
        detector = detector.with_cache(Arc::new(pool.cache().clone()));
        detector
    }

    /// 设置多解析器并行查询的并发上限。
    pub fn with_concurrency(mut self, n: usize) -> Self {
        self.max_concurrency = n;
        self
    }

    /// 附加 DNS 答案缓存。
    pub fn with_cache(mut self, cache: Arc<DnsCache>) -> Self {
        self.cache = Some(cache);
        self
    }

    pub fn with_cname_max_depth(mut self, depth: usize) -> Self {
        self.cname_max_depth = depth;
        self
    }

    pub fn resolver_count(&self) -> usize {
        self.resolvers.len()
    }

    pub fn resolvers(&self) -> &[DnsResolverEntry] {
        &self.resolvers
    }

    pub async fn resolver_health(&self) -> HashMap<String, ResolverHealth> {
        self.health.read().await.clone()
    }

    pub async fn unhealthy_resolver_names(&self) -> Vec<String> {
        let health = self.health.read().await;
        let banned = self.banned_until.read().await;
        let now = Instant::now();

        health
            .iter()
            .filter(|(_, h)| !h.is_healthy())
            .map(|(name, _)| name.clone())
            .chain(
                banned
                    .iter()
                    .filter(|(_, until)| **until > now)
                    .map(|(name, _)| name.clone()),
            )
            .collect()
    }

    pub async fn ban_resolver(&self, name: &str) {
        let mut banned = self.banned_until.write().await;
        banned.insert(name.to_string(), Instant::now() + self.ban_duration);
    }

    pub async fn unban_resolver(&self, name: &str) {
        let mut banned = self.banned_until.write().await;
        banned.remove(name);
    }

    pub async fn banned_resolver_names(&self) -> Vec<String> {
        let banned = self.banned_until.read().await;
        let now = Instant::now();
        banned
            .iter()
            .filter(|(_, until)| **until > now)
            .map(|(name, _)| name.clone())
            .collect()
    }

    pub fn is_cloudflare_cname(cname: &str) -> bool {
        let cname_lower = cname.to_ascii_lowercase();
        let cf_domains = [
            "cloudflare.net",
            "cloudflare.com",
            "cfargoson.com",
            "cfargoson.link",
            "cflare.co",
        ];
        cf_domains
            .iter()
            .any(|d| cname_lower.ends_with(d) || cname_lower.contains(d))
    }

    fn normalize_hostname(hostname: &str) -> String {
        let trimmed = hostname.trim().trim_end_matches('.').to_string();
        if trimmed.is_empty() {
            return hostname.to_string();
        }
        trimmed
    }

    fn fqdn(hostname: &str) -> String {
        let trimmed = hostname.trim().trim_end_matches('.').to_string();
        format!("{trimmed}.")
    }

    pub fn validate_hostname(hostname: &str) -> Result<(), CfProbeError> {
        let trimmed = hostname.trim().trim_end_matches('.');
        if trimmed.is_empty() {
            return Err(CfProbeError::InvalidResponse(
                "hostname cannot be empty".to_string(),
            ));
        }
        if trimmed.len() > 253 {
            return Err(CfProbeError::InvalidResponse(format!(
                "hostname too long: {} chars (max 253)",
                trimmed.len()
            )));
        }
        for label in trimmed.split('.') {
            if label.is_empty() {
                return Err(CfProbeError::InvalidResponse(format!(
                    "empty label in hostname: {}",
                    hostname
                )));
            }
            if label.len() > 63 {
                return Err(CfProbeError::InvalidResponse(format!(
                    "label too long: {} chars (max 63)",
                    label.len()
                )));
            }
            if label.starts_with('-') || label.ends_with('-') {
                return Err(CfProbeError::InvalidResponse(format!(
                    "label starts or ends with hyphen: {}",
                    label
                )));
            }
            if !label.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
                return Err(CfProbeError::InvalidResponse(format!(
                    "label contains invalid characters: {}",
                    label
                )));
            }
        }
        Ok(())
    }

    pub async fn detect(
        &self,
        hostname: &str,
        cf_ranges: &CloudflareRanges,
    ) -> Result<DnsDetection, CfProbeError> {
        self.detect_with_cancel(hostname, cf_ranges, CancellationToken::new())
            .await
    }

    pub async fn detect_with_cancel(
        &self,
        hostname: &str,
        cf_ranges: &CloudflareRanges,
        cancellation: CancellationToken,
    ) -> Result<DnsDetection, CfProbeError> {
        let total_start = Instant::now();

        let normalized = Self::normalize_hostname(hostname);
        let fqdn = Self::fqdn(hostname);

        let semaphore = Arc::new(Semaphore::new(self.max_concurrency));

        let mut futures = FuturesUnordered::new();

        let unhealthy: HashSet<String> =
            self.unhealthy_resolver_names().await.into_iter().collect();

        for resolver_entry in &self.resolvers {
            if cancellation.is_cancelled() {
                break;
            }

            if unhealthy.contains(&resolver_entry.name) {
                continue;
            }

            let resolver = resolver_entry.clone();
            let fqdn = fqdn.clone();
            let semaphore = Arc::clone(&semaphore);
            let cache = self.cache.clone();
            let cname_max_depth = self.cname_max_depth;
            let cancellation = cancellation.clone();

            futures.push(tokio::spawn(async move {
                let _permit = semaphore
                    .acquire()
                    .await
                    .expect("semaphore should not be closed");
                Self::resolve_one_cached(resolver, &fqdn, cache, cname_max_depth, cancellation)
                    .await
            }));
        }

        let mut observations: Vec<ResolverObservation> = Vec::new();
        let mut all_mx: Vec<(u16, String)> = Vec::new();
        let mut all_txt: Vec<String> = Vec::new();
        let mut all_ns: Vec<String> = Vec::new();
        let mut all_cname_chain: Vec<String> = Vec::new();
        let mut mx_seen = HashSet::new();
        let mut txt_seen = HashSet::new();
        let mut ns_seen = HashSet::new();
        let mut cname_seen = HashSet::new();

        while let Some(result) = futures.next().await {
            if cancellation.is_cancelled() {
                break;
            }

            match result {
                Ok(observation) => {
                    Self::record_health_with_ban(
                        self.health.clone(),
                        self.banned_until.clone(),
                        &observation,
                        self.ban_duration,
                    )
                    .await;

                    for mx in &observation.mx_records {
                        if mx_seen.insert(mx.clone()) {
                            all_mx.push(mx.clone());
                        }
                    }
                    for txt in &observation.txt_records {
                        if txt_seen.insert(txt.clone()) {
                            all_txt.push(txt.clone());
                        }
                    }
                    for ns in &observation.ns_records {
                        if ns_seen.insert(ns.clone()) {
                            all_ns.push(ns.clone());
                        }
                    }
                    for cname in &observation.cname_chain {
                        if cname_seen.insert(cname.clone()) {
                            all_cname_chain.push(cname.clone());
                        }
                    }

                    observations.push(observation);
                }
                Err(_) => continue,
            }
        }

        let total_duration = total_start.elapsed();

        Ok(self.aggregate(
            &normalized,
            &fqdn,
            observations,
            total_duration,
            cf_ranges,
            all_mx,
            all_txt,
            all_ns,
            all_cname_chain,
        ))
    }

    pub async fn detect_ptr(&self, ip: IpAddr) -> Result<Vec<String>, CfProbeError> {
        self.detect_ptr_with_cancel(ip, CancellationToken::new())
            .await
    }

    pub async fn detect_ptr_with_cancel(
        &self,
        ip: IpAddr,
        cancellation: CancellationToken,
    ) -> Result<Vec<String>, CfProbeError> {
        let key = ip.to_string();

        if let Some(ref cache) = self.cache {
            if let Some(cached) = cache.get_ptr(&key).await {
                return Ok(cached);
            }
        }

        /*
         * 对多个 resolver 的 PTR 查询并发执行。
         * 典型配置中会有 2~4 个 resolver,
         * 串行耗时 ≈ sum(time_i),改为并发后 ≈ max(time_i)。
         */
        let mut futures = Vec::with_capacity(self.resolvers.len());
        for resolver_entry in &self.resolvers {
            let backend = resolver_entry.backend.clone();
            futures.push(async move { backend.lookup_ptr(ip).await });
        }

        use futures::StreamExt;
        let mut stream = futures::stream::iter(futures).buffer_unordered(self.resolvers.len());

        let mut results: Vec<String> = Vec::new();
        let mut seen = std::collections::HashSet::new();

        while let Some(result) = stream.next().await {
            if cancellation.is_cancelled() {
                break;
            }
            match result {
                Ok(ptr_list) => {
                    for ptr in ptr_list {
                        if seen.insert(ptr.clone()) {
                            results.push(ptr);
                        }
                    }
                }
                Err(_) => continue,
            }
        }

        if !results.is_empty() {
            if let Some(ref cache) = self.cache {
                cache.set_ptr(&key, results.clone()).await;
            }
        }

        Ok(results)
    }

    pub async fn detect_batch(
        &self,
        hostnames: &[String],
        cf_ranges: &CloudflareRanges,
    ) -> Vec<(String, Result<DnsDetection, CfProbeError>)> {
        self.detect_batch_with_cancel(hostnames, cf_ranges, CancellationToken::new())
            .await
    }

    pub async fn detect_batch_with_cancel(
        &self,
        hostnames: &[String],
        cf_ranges: &CloudflareRanges,
        cancellation: CancellationToken,
    ) -> Vec<(String, Result<DnsDetection, CfProbeError>)> {
        let mut results = Vec::with_capacity(hostnames.len());

        for hostname in hostnames {
            if cancellation.is_cancelled() {
                break;
            }

            let result = self
                .detect_with_cancel(hostname, cf_ranges, cancellation.clone())
                .await;

            results.push((hostname.clone(), result));
        }

        results
    }

    async fn resolve_one_cached(
        entry: DnsResolverEntry,
        fqdn: &str,
        cache: Option<Arc<DnsCache>>,
        cname_max_depth: usize,
        cancellation: CancellationToken,
    ) -> ResolverObservation {
        let resolver_name = entry.name.clone();
        let backend = entry.backend.clone();
        let start = Instant::now();

        let mut ips: Vec<IpAddr> = Vec::new();
        let mut success = true;
        let mut error: Option<String> = None;

        if cancellation.is_cancelled() {
            return ResolverObservation {
                resolver: resolver_name,
                success: false,
                ips: Vec::new(),
                cname_chain: Vec::new(),
                duration: start.elapsed(),
                error: Some("cancelled".to_string()),
                mx_records: Vec::new(),
                txt_records: Vec::new(),
                ns_records: Vec::new(),
            };
        }

        let fqdn_owned = fqdn.to_string();
        let cache_clone = cache.clone();
        let backend_clone = backend.clone();

        /*
         * CNAME 分支:
         *   1) cache lookup / backend CNAME query
         *   2) walk_cname_chain (可能多轮 CNAME lookup,串行)
         *   3) 用 final_fqdn 发起 IP 查询 (依赖 CNAME 结果)
         *
         * 但 MX/TXT/NS 三件事完全不依赖 CNAME,可以和整个 CNAME+IP 流水线同时跑。
         *
         * 因此:
         *  Task A: (CNAME → walk chain → IP)   (总 = 2~3 RTT)
         *  Task B: MX   (1 RTT)  ┐
         *  Task C: TXT  (1 RTT)  ├─ 这三者独立并发
         *  Task D: NS   (1 RTT)  ┘
         *              ↓
         *  总时间 = max(Task A, max(B,C,D))
         *         = max(3 RTT, 1 RTT) = 3 RTT
         *  之前:    = 1 RTT (CNAME) + walk + 1 RTT (IP) + 1 (MX) + 1 (TXT) + 1 (NS)
         *         ≈ 5 RTT
         */

        let cname_ip_task = async move {
            let cname_result = if let Some(ref cache) = cache_clone {
                if let Some(cached) = cache.get_cname(&fqdn_owned).await {
                    Ok(cached)
                } else {
                    let result = backend_clone.lookup_cname(&fqdn_owned).await;
                    if let Ok(ref cnames) = result {
                        cache.set_cname(&fqdn_owned, cnames.clone()).await;
                    }
                    result
                }
            } else {
                backend_clone.lookup_cname(&fqdn_owned).await
            };

            let mut walked_chain: Vec<String> = Vec::new();
            let mut final_fqdn = fqdn_owned.clone();

            if let Ok(cnames) = &cname_result {
                walked_chain =
                    Self::walk_cname_chain(&backend_clone, &fqdn_owned, cnames, cname_max_depth)
                        .await;
                if let Some(last) = walked_chain.last() {
                    final_fqdn = format!("{}.", last.trim_end_matches('.'));
                }
            }

            let ip_lookup_target = if walked_chain.is_empty() {
                fqdn_owned.clone()
            } else {
                final_fqdn.clone()
            };

            let ip_result = if let Some(ref cache) = cache_clone {
                if let Some(cached) = cache.get_ip(&ip_lookup_target).await {
                    Ok(cached)
                } else {
                    let result = backend_clone.lookup_ip(&ip_lookup_target).await;
                    if let Ok(ref ips) = result {
                        cache.set_ip(&ip_lookup_target, ips.clone()).await;
                    }
                    result
                }
            } else {
                backend_clone.lookup_ip(&ip_lookup_target).await
            };

            (cname_result, walked_chain, ip_result)
        };

        let backend2 = backend.clone();
        let cache2 = cache.clone();
        let fqdn_mx = fqdn.to_string();
        let mx_task = async move {
            if let Some(ref cache) = cache2 {
                if let Some(cached) = cache.get_mx(&fqdn_mx).await {
                    return Ok(cached);
                }
                let result = backend2.lookup_mx(&fqdn_mx).await;
                if let Ok(ref mx) = result {
                    cache.set_mx(&fqdn_mx, mx.clone()).await;
                }
                result
            } else {
                backend2.lookup_mx(&fqdn_mx).await
            }
        };

        let backend3 = backend.clone();
        let cache3 = cache.clone();
        let fqdn_txt = fqdn.to_string();
        let txt_task = async move {
            if let Some(ref cache) = cache3 {
                if let Some(cached) = cache.get_txt(&fqdn_txt).await {
                    return Ok(cached);
                }
                let result = backend3.lookup_txt(&fqdn_txt).await;
                if let Ok(ref txt) = result {
                    cache.set_txt(&fqdn_txt, txt.clone()).await;
                }
                result
            } else {
                backend3.lookup_txt(&fqdn_txt).await
            }
        };

        let backend4 = backend.clone();
        let cache4 = cache;
        let fqdn_ns = fqdn.to_string();
        let ns_task = async move {
            if let Some(ref cache) = cache4 {
                if let Some(cached) = cache.get_ns(&fqdn_ns).await {
                    return Ok(cached);
                }
                let result = backend4.lookup_ns(&fqdn_ns).await;
                if let Ok(ref ns) = result {
                    cache.set_ns(&fqdn_ns, ns.clone()).await;
                }
                result
            } else {
                backend4.lookup_ns(&fqdn_ns).await
            }
        };

        let ((cname_result, walked_chain, ip_result), mx_result, txt_result, ns_result) =
            tokio::join!(cname_ip_task, mx_task, txt_task, ns_task);

        let duration = start.elapsed();

        match &ip_result {
            Ok(list) => {
                ips = list.iter().copied().collect();
            }
            Err(err) => {
                success = false;
                error = Some(err.to_string());
            }
        }

        // cname_result 本身的错误不置 success=false,
        // 保持与原代码语义一致。
        let _ = cname_result;

        let cname_chain = walked_chain;

        let filtered_ips = Self::filter_private_ips(&ips);

        let mx_records = mx_result.unwrap_or_default();
        let txt_records = txt_result.unwrap_or_default();
        let ns_records = ns_result.unwrap_or_default();

        ResolverObservation {
            resolver: resolver_name,
            success,
            ips: filtered_ips,
            cname_chain,
            duration,
            error,
            mx_records,
            txt_records,
            ns_records,
        }
    }

    async fn walk_cname_chain(
        backend: &Arc<dyn DnsBackend>,
        _original_fqdn: &str,
        initial_cnames: &[String],
        max_depth: usize,
    ) -> Vec<String> {
        if initial_cnames.is_empty() || max_depth == 0 {
            return initial_cnames.to_vec();
        }

        let mut chain: Vec<String> = initial_cnames.to_vec();
        let mut visited: HashSet<String> = HashSet::new();

        for cname in initial_cnames {
            visited.insert(cname.clone());
        }

        let mut current = initial_cnames.last().cloned();
        let depth_limit = max_depth.saturating_sub(initial_cnames.len());

        for _ in 0..depth_limit {
            let Some(target) = current.clone() else {
                break;
            };

            let fqdn = if target.ends_with('.') {
                target.clone()
            } else {
                format!("{}.", target)
            };

            match backend.lookup_cname(&fqdn).await {
                Ok(more) => {
                    if more.is_empty() {
                        break;
                    }
                    let mut progressed = false;
                    for next in more {
                        if !visited.contains(&next) {
                            visited.insert(next.clone());
                            chain.push(next.clone());
                            current = Some(next);
                            progressed = true;
                        }
                    }
                    if !progressed {
                        break;
                    }
                }
                Err(_) => break,
            }
        }

        chain
    }

    async fn record_health_with_ban(
        health: Arc<RwLock<HashMap<String, ResolverHealth>>>,
        banned: Arc<RwLock<HashMap<String, Instant>>>,
        observation: &ResolverObservation,
        ban_duration: Duration,
    ) {
        let mut map = health.write().await;
        let h = map.entry(observation.resolver.clone()).or_default();

        if observation.success {
            h.record_success(observation.duration);
        } else {
            h.record_failure();
            if h.failure_count >= 3 && !h.is_healthy() {
                drop(map);
                let mut banned_map = banned.write().await;
                banned_map.insert(observation.resolver.clone(), Instant::now() + ban_duration);
            }
        }
    }

    fn aggregate(
        &self,
        normalized: &str,
        _fqdn: &str,
        observations: Vec<ResolverObservation>,
        total_duration: std::time::Duration,
        cf_ranges: &CloudflareRanges,
        all_mx: Vec<(u16, String)>,
        all_txt: Vec<String>,
        all_ns: Vec<String>,
        all_cname_chain: Vec<String>,
    ) -> DnsDetection {
        let successful = observations
            .iter()
            .filter(|o| o.success && !o.ips.is_empty())
            .count();

        let mut union_ips: Vec<IpAddr> = observations
            .iter()
            .flat_map(|o| o.ips.iter().copied())
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();

        union_ips.sort();

        let mut cloudflare_ips: Vec<IpAddr> = union_ips
            .iter()
            .copied()
            .filter(|ip| cf_ranges.contains(*ip))
            .collect();

        cloudflare_ips.sort();

        let cloudflare_resolver_count = observations
            .iter()
            .filter(|o| o.success && o.ips.iter().any(|ip| cf_ranges.contains(*ip)))
            .count();

        let has_cloudflare_ip = !cloudflare_ips.is_empty();

        let all_resolvers_agree = observations.iter().filter(|o| o.success).count() <= 1
            || observations.iter().filter(|o| o.success).all(|o| {
                o.ips.len() == cloudflare_ips.len()
                    && o.ips.iter().all(|ip| cf_ranges.contains(*ip))
            });

        let status = if has_cloudflare_ip {
            DnsDetectionStatus::CloudflareIp
        } else if !union_ips.is_empty() {
            DnsDetectionStatus::NoCloudflareIp
        } else {
            DnsDetectionStatus::Unknown
        };

        DnsDetection {
            hostname: normalized.to_string(),
            normalized_hostname: normalized.to_string(),
            observations,
            union_ips,
            cloudflare_ips,
            cloudflare_resolver_count,
            successful_resolver_count: successful,
            resolver_count: self.resolvers.len(),
            all_resolvers_agree,
            has_cloudflare_ip,
            total_duration,
            status,
            mx_records: all_mx,
            txt_records: all_txt,
            ns_records: all_ns,
            cname_chain: all_cname_chain,
        }
    }

    fn filter_private_ips(ips: &[IpAddr]) -> Vec<IpAddr> {
        ips.iter()
            .filter(|ip| match ip {
                IpAddr::V4(v4) => {
                    !v4.is_private()
                        && !v4.is_loopback()
                        && !v4.is_link_local()
                        && !v4.is_broadcast()
                        && !v4.is_multicast()
                        && !v4.is_unspecified()
                        && !Self::is_cgnat(v4)
                }
                IpAddr::V6(v6) => {
                    !v6.is_loopback()
                        && !v6.is_multicast()
                        && !v6.is_unspecified()
                        && !Self::is_ula(v6)
                        && !Self::is_link_local_v6(v6)
                }
            })
            .copied()
            .collect()
    }

    fn is_cgnat(ip: &std::net::Ipv4Addr) -> bool {
        let octets = ip.octets();
        octets[0] == 100 && octets[1] >= 64 && octets[1] <= 127
    }

    fn is_ula(ip: &std::net::Ipv6Addr) -> bool {
        ip.segments()[0] & 0xfe00 == 0xfc00
    }

    fn is_link_local_v6(ip: &std::net::Ipv6Addr) -> bool {
        ip.segments()[0] & 0xffc0 == 0xfe80
    }
}