proxybroker 0.4.2

Find, check, and serve public HTTP(S) and SOCKS4/5 proxies. Library + CLI.
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
//! The [`Checker`]: validate one proxy across the requested protocols, and classify its
//! anonymity. `checker.py:Checker`.
//!
//! The judges are probed **eagerly** when the checker is built and owned by it — so
//! `Checker::new` returns [`Error::NoJudges`] if none verify (`checker.py:137`), and
//! `check` is simply unconstructible before the baseline exists. This turns the
//! probe-before-check ordering into a type fact and removes the process-global judge state
//! (and the deadlock-prone `asyncio.Event`) entirely. See `decisions.md`.

use crate::error::{Error, ProxyError};
use crate::judge::{Judge, JudgePool};
use crate::negotiator::{negotiate, Target};
use crate::proxy::Proxy;
use crate::resolver::Resolver;
use crate::types::{AnonLevel, Caps, JudgeScheme, Proto, TypeSpec};
use crate::utils::{fresh_marker, get_all_ip, get_status_code, request_headers};
use rand::Rng;
use std::collections::{BTreeMap, HashSet};
use std::net::IpAddr;
use std::time::{Duration, Instant};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;

/// Everything needed to run checks against one proxy.
pub struct Checker {
    judges: JudgePool,
    real_ext_ips: HashSet<IpAddr>,
    requested: Vec<TypeSpec>,
    timeout: Duration,
    post: bool,
    strict: bool,
    relaxed_validity: bool,
    trust_check: bool,
    /// DNS blocklist zones; empty disables the check. Kept with the resolver so `check` can
    /// reject a listed proxy before doing any real work.
    dnsbl: Vec<String>,
    resolver: std::sync::Arc<Resolver>,
    /// Judged (a verified judge pool) or Liveness (graceful degradation, A2).
    mode: CheckMode,
    retry: RetryPolicy,
}

/// How the checker validates a proxy: against a verified judge, or — when no judge came up and the
/// caller supplied a liveness URL — by a plain fetch-through-the-proxy 200 check (A2).
enum CheckMode {
    Judged,
    Liveness(LivenessTarget),
}

/// A resolved liveness endpoint: a plain URL the checker GETs through the proxy, expecting 200.
struct LivenessTarget {
    host: String,
    path: String,
    ip: IpAddr,
    scheme: JudgeScheme,
}

impl LivenessTarget {
    /// Parse + resolve a liveness URL. Reuses [`Judge::parse`] for scheme/host/path. `None` if the
    /// URL is malformed, is an SMTP URL (liveness is an HTTP GET), or the host does not resolve.
    async fn resolve(url: &str, resolver: &Resolver) -> Option<LivenessTarget> {
        let judge = Judge::parse(url)?;
        if judge.scheme == JudgeScheme::Smtp {
            return None;
        }
        let ip = resolver.resolve(&judge.host).await.ok()?;
        Some(LivenessTarget {
            host: judge.host,
            path: judge.path,
            ip,
            scheme: judge.scheme,
        })
    }
}

/// What a single [`Checker::attempt`] validates against — a judge (with anonymity classification)
/// or a liveness target (200 = working, level `None`).
enum Probe<'a> {
    Judged(&'a Judge),
    Liveness(&'a LivenessTarget),
}

impl std::fmt::Debug for Checker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Resolver holds a hickory resolver that is not Debug; summarize instead.
        f.debug_struct("Checker")
            .field("judges", &self.judges.counts())
            .field("requested", &self.requested)
            .field("strict", &self.strict)
            .field("dnsbl", &self.dnsbl)
            .finish_non_exhaustive()
    }
}

/// Which errors retry and how long to wait between attempts (A5). Replaces a bare `max_tries`.
///
/// The [`Default`] reproduces the historical behavior exactly: 3 attempts, retry **only**
/// `Timeout`, no delay — so existing callers are unaffected.
#[derive(Debug, Clone, PartialEq)]
pub struct RetryPolicy {
    /// Total attempts per protocol (`>= 1`).
    pub max_tries: usize,
    /// Which per-proxy errors are retryable. Default: just `Timeout` (parity).
    pub retry_on: HashSet<ProxyError>,
    /// Base delay before the first retry. Zero = no delay (parity).
    pub backoff: Duration,
    /// Per-attempt multiplier on the delay (exponential). `1.0` = constant.
    pub factor: f64,
    /// Symmetric jitter fraction applied to each delay, `0.0..=1.0`.
    pub jitter: f64,
    /// Upper bound on any single delay. Zero = uncapped.
    pub max_backoff: Duration,
}

impl Default for RetryPolicy {
    fn default() -> Self {
        RetryPolicy {
            max_tries: 3,
            retry_on: HashSet::from([ProxyError::Timeout]),
            backoff: Duration::ZERO,
            factor: 1.0,
            jitter: 0.0,
            max_backoff: Duration::ZERO,
        }
    }
}

impl RetryPolicy {
    /// `max_tries` attempts, otherwise the default policy (retry only `Timeout`, no delay).
    pub fn tries(max_tries: usize) -> Self {
        RetryPolicy {
            max_tries,
            ..Default::default()
        }
    }

    /// Retry the transient set — `{Timeout, Reset, ConnFailed, EmptyRecv}` — for `max_tries`.
    pub fn transient(max_tries: usize) -> Self {
        RetryPolicy {
            max_tries,
            retry_on: HashSet::from([
                ProxyError::Timeout,
                ProxyError::Reset,
                ProxyError::ConnFailed,
                ProxyError::EmptyRecv,
            ]),
            ..Default::default()
        }
    }

    /// The delay before retry number `i` (0-based): `backoff * factor^i`, then symmetric jitter of
    /// `±jitter` fraction, then clamped to `max_backoff`. The cap is applied **last** so it is a
    /// hard upper bound on any single delay even with jitter. Zero base → zero (no wall-clock sleep
    /// on the parity path).
    pub fn backoff_for(&self, i: usize) -> Duration {
        if self.backoff.is_zero() {
            return Duration::ZERO;
        }
        let mut d = self.backoff.as_secs_f64() * self.factor.powi(i as i32);
        if self.jitter > 0.0 {
            let r: f64 = rand::rng().random_range(-1.0..=1.0);
            d *= 1.0 + self.jitter * r;
        }
        if !self.max_backoff.is_zero() {
            d = d.min(self.max_backoff.as_secs_f64());
        }
        Duration::from_secs_f64(d.max(0.0))
    }
}

/// Configuration for [`Checker::new`].
#[derive(Debug, Clone, Default)]
pub struct CheckerConfig {
    /// Judge URLs to probe. Empty → the bundled defaults.
    pub judges: Vec<String>,
    /// Protocols (and optional anonymity levels) to check. Required — empty is an error.
    pub types: Vec<TypeSpec>,
    pub timeout: Duration,
    /// Retry policy: attempt count, which errors retry, and the backoff schedule (A5).
    pub retry: RetryPolicy,
    /// Use `POST` instead of `GET` for the test request.
    pub post: bool,
    /// Require the proxy's anonymity level to match exactly (`--strict`).
    pub strict: bool,
    /// Relax response validity to marker + IP only, demoting Referer/Cookie from validity gates to
    /// recorded capability signals (A4). Default `false` = parity (all four required).
    pub relaxed_validity: bool,
    /// Run honeypot detection on each proxy and record the trust verdict (A6). Default `false` =
    /// no assessment, zero cost.
    pub trust_check: bool,
    /// DNS blocklist zones (`--dnsbl`); a proxy whose IP is listed in any zone is rejected.
    pub dnsbl: Vec<String>,
    /// When the judge pool is empty, fall back to a plain liveness check against this URL instead
    /// of failing with `NoJudges`. `None` keeps the strict judge-required behavior. Proxies
    /// confirmed this way carry anonymity level `None` (unclassifiable without a judge).
    pub liveness_url: Option<String>,
}

impl Checker {
    /// Build a checker, probing the judges eagerly. Errors:
    /// - [`Error::NoTypes`] if no protocol was requested (`api.py:249`);
    /// - [`Error::NoJudges`] if no judge verifies (`checker.py:137`).
    pub async fn new(
        cfg: CheckerConfig,
        resolver: std::sync::Arc<Resolver>,
        client: &reqwest::Client,
        real_ext_ips: HashSet<IpAddr>,
    ) -> Result<Checker, Error> {
        if cfg.types.is_empty() {
            return Err(Error::NoTypes);
        }
        let urls = if cfg.judges.is_empty() {
            crate::judge::default_judges()
        } else {
            cfg.judges.clone()
        };
        let candidates: Vec<Judge> = urls.iter().filter_map(|u| Judge::parse(u)).collect();
        let judges =
            JudgePool::probe_all(candidates, &resolver, client, &real_ext_ips, cfg.timeout).await;
        // No judge came up: fail as before, unless a liveness URL enables graceful degradation
        // (A2). A malformed/unresolvable liveness URL still errors — there is nothing to check.
        let mode = if judges.is_empty() {
            match &cfg.liveness_url {
                Some(url) => CheckMode::Liveness(
                    LivenessTarget::resolve(url, &resolver)
                        .await
                        .ok_or(Error::NoJudges)?,
                ),
                None => return Err(Error::NoJudges),
            }
        } else {
            CheckMode::Judged
        };
        Ok(Checker {
            judges,
            real_ext_ips,
            requested: cfg.types,
            timeout: cfg.timeout,
            retry: cfg.retry,
            post: cfg.post,
            strict: cfg.strict,
            relaxed_validity: cfg.relaxed_validity,
            trust_check: cfg.trust_check,
            dnsbl: cfg.dnsbl,
            resolver,
            mode,
        })
    }

    /// Check a proxy across the protocols it is expected to support (intersected with the
    /// requested set), record its working types, and return whether it passes. `checker.py:check`.
    pub async fn check(&self, proxy: &mut Proxy) -> bool {
        // A proxy listed in a configured DNS blocklist is rejected before any real work
        // (`checker.py:167` runs this first).
        if !self.dnsbl.is_empty() && self.in_dnsbl(proxy.host).await {
            tracing::debug!(addr = %proxy.addr(), "rejected: found in DNSBL");
            return false;
        }

        let requested: HashSet<Proto> = self.requested.iter().map(|t| t.proto).collect();

        // ngtrs = expected ∩ requested, iterated in Proto::ALL order (never HashMap order,
        // which is randomized and would make check order nondeterministic). An empty
        // expected set means "unknown", so check all requested (api.py's else branch).
        let ngtrs: Vec<Proto> = Proto::ALL
            .into_iter()
            .filter(|p| {
                requested.contains(p)
                    && (proxy.expected_types.is_empty() || proxy.expected_types.contains(p))
            })
            .collect();

        let mut any = false;
        for proto in ngtrs {
            if self.check_one(proxy, proto).await {
                any = true;
            }
        }

        any && self.types_passed(proxy)
    }

    /// Is the proxy's IP listed in any configured DNS blocklist? Mirrors `checker.py:_in_DNSBL`:
    /// reverse the address, prepend it to each zone, and if ANY such name resolves, the IP is
    /// listed. Queries run concurrently. IPv6 is not checked (see [`dnsbl_query`]).
    async fn in_dnsbl(&self, host: IpAddr) -> bool {
        let queries: Vec<String> = self
            .dnsbl
            .iter()
            .filter_map(|zone| dnsbl_query(host, zone))
            .collect();
        if queries.is_empty() {
            return false;
        }
        let probes = queries.iter().map(|q| self.resolver.resolve(q));
        futures_util::future::join_all(probes)
            .await
            .iter()
            .any(|r| r.is_ok())
    }

    /// Check one protocol, retrying on timeout up to `max_tries`. Mirrors `checker.py:_check`:
    /// a timeout retries (`continue`), any other proxy error stops (`break`).
    async fn check_one(&self, proxy: &mut Proxy, proto: Proto) -> bool {
        let scheme = proto.judge_scheme();
        // Resolve the probe + target for this protocol. Judged mode routes to a random judge for
        // the scheme; Liveness mode always probes the single liveness endpoint.
        let judge; // holds the Arc alive across the retry loop in Judged mode
        let (probe, target) = match &self.mode {
            CheckMode::Judged => {
                let Some(j) = self.judges.random(scheme) else {
                    return false; // no judge for this scheme
                };
                judge = j;
                let target = Target {
                    host: judge.host.clone(),
                    ip: judge.ip,
                    port: scheme.default_port(),
                };
                (Probe::Judged(&judge), target)
            }
            CheckMode::Liveness(lt) => {
                let target = Target {
                    host: lt.host.clone(),
                    ip: Some(lt.ip),
                    port: lt.scheme.default_port(),
                };
                (Probe::Liveness(lt), target)
            }
        };

        for i in 0..self.retry.max_tries {
            let start = Instant::now();
            match self.attempt(proxy, proto, &probe, &target).await {
                Ok(Attempt::Working(obs)) => {
                    let rtt = start.elapsed().as_secs_f64();
                    // F3: one structured event per terminal outcome, on target proxybroker::check
                    // (mutable independently of the default filter, e.g. proxybroker::check=info).
                    tracing::info!(
                        target: "proxybroker::check",
                        addr = %proxy.addr(),
                        proto = proto.as_str(),
                        outcome = "working",
                        rtt,
                        "check outcome",
                    );
                    proxy.record_attempt(Some(rtt), None);
                    // F1: the same RTT is the judge-probe latency. Record it into a dedicated
                    // check-time signal so the probe-latency metric never blends with serve traffic
                    // (which feeds only `record_attempt`).
                    proxy.record_probe_latency(rtt);
                    proxy.add_type(proto, obs.level);
                    proxy.record_caps(obs.caps);
                    proxy.record_trust(obs.trust);
                    return true;
                }
                Ok(Attempt::Invalid) => {
                    // Response did not validate — the proxy does not work for this protocol.
                    // Python breaks here (no retry).
                    tracing::info!(
                        target: "proxybroker::check",
                        addr = %proxy.addr(),
                        proto = proto.as_str(),
                        outcome = "invalid",
                        "check outcome",
                    );
                    proxy.record_attempt(None, Some(ProxyError::BadResponse));
                    return false;
                }
                Err(e) => {
                    // `outcome` is a fixed lowercase label for grouping; the specific error keeps
                    // its byte-for-byte ProxyError::as_str in a separate `error` field.
                    let outcome = if e == ProxyError::Timeout {
                        "timeout"
                    } else {
                        "error"
                    };
                    tracing::info!(
                        target: "proxybroker::check",
                        addr = %proxy.addr(),
                        proto = proto.as_str(),
                        outcome,
                        error = e.as_str(),
                        "check outcome",
                    );
                    proxy.record_attempt(None, Some(e));
                    // Retry only errors the policy marks retryable, and only if attempts remain.
                    // Default policy = {Timeout}, zero backoff → the historical timeout-only,
                    // no-delay retry, exactly.
                    if self.retry.retry_on.contains(&e) && i + 1 < self.retry.max_tries {
                        tokio::time::sleep(self.retry.backoff_for(i)).await;
                        continue;
                    }
                    return false;
                }
            }
        }
        false
    }

    /// One connection attempt: connect, negotiate, and (for non-`CONNECT:25`) run the test
    /// request. Returns the anonymity outcome, or a [`ProxyError`] to drive the retry logic.
    async fn attempt(
        &self,
        proxy: &Proxy,
        proto: Proto,
        probe: &Probe<'_>,
        target: &Target,
    ) -> Result<Attempt, ProxyError> {
        let tcp = tokio::time::timeout(self.timeout, TcpStream::connect((proxy.host, proxy.port)))
            .await
            .map_err(|_| ProxyError::Timeout)?
            .map_err(|_| ProxyError::ConnFailed)?;

        // The checker probes public candidates; it has no upstream credentials (B8 auth is inert
        // on the check path).
        let mut stream = negotiate(proto, tcp, target, self.timeout, None).await?;

        // CONNECT:25 has no test request — a granted tunnel is the whole check.
        if proto == Proto::Connect25 {
            return Ok(Attempt::Working(Observation::default()));
        }

        // Build + send the request; the connect/negotiate/send/read/status handling is identical
        // for both modes — only what we assert about the 200 response differs.
        let (request, marker) = match probe {
            Probe::Judged(judge) => {
                let (req, marker) = self.build_request(judge, proto);
                (req, Some(marker))
            }
            Probe::Liveness(lt) => (self.build_liveness_request(lt, proto), None),
        };
        tokio::time::timeout(self.timeout, stream.write_all(&request))
            .await
            .map_err(|_| ProxyError::Timeout)?
            .map_err(|_| ProxyError::Reset)?;

        let raw = read_response(&mut stream, self.timeout).await?;
        let (head, body) = split_head_body(&raw);
        if get_status_code(head, 9, 12) != 200 {
            return Err(ProxyError::BadStatus);
        }

        match probe {
            // Liveness: a 200 through the proxy is the whole check; there is no judge to reflect
            // markers/IPs, so anonymity is unclassifiable → level None, no capability profile.
            Probe::Liveness(_) => Ok(Attempt::Working(Observation::default())),
            Probe::Judged(judge) => {
                let content = decompress(head, body);
                let marker = marker.expect("judged mode always builds a marker");
                let caps = caps_from_content(&content);
                // Default (strict): Referer + Cookie are validity gates (parity). Relaxed: only
                // marker + a non-empty IP set are required, and the echo flags become recorded
                // capability signals that can vary per proxy (A4).
                let valid = if self.relaxed_validity {
                    content.contains(&marker) && !get_all_ip(&content).is_empty()
                } else {
                    response_is_valid(&content, &marker)
                };
                if !valid {
                    return Ok(Attempt::Invalid);
                }
                let level = if proto.checks_anon_level() {
                    Some(self.anonymity_level(&content, judge))
                } else {
                    None
                };
                // Honeypot verdict (A6): only when opted in — the marker doubles as the canary.
                let trust = if self.trust_check {
                    TrustReport::assess(&sent_header_names(), &marker, &content)
                } else {
                    TrustReport::default()
                };
                Ok(Attempt::Working(Observation { level, caps, trust }))
            }
        }
    }

    /// A plain `GET` for the liveness endpoint (A2), routed like the test request: absolute-form
    /// for plain HTTP (it goes to the proxy), origin-form when tunnelled. No marker/cookie/referer
    /// — there is no judge to reflect them; only the 200 matters.
    fn build_liveness_request(&self, lt: &LivenessTarget, proto: Proto) -> Vec<u8> {
        let mut hdrs = request_headers(None);
        hdrs.insert("Host", lt.host.clone());
        hdrs.insert("Connection", "close".to_owned());
        let path = if proto.uses_full_path() {
            format!("http://{}{}", lt.host, lt.path)
        } else {
            lt.path.clone()
        };
        let headers: String = hdrs
            .iter()
            .map(|(k, v)| format!("{k}: {v}"))
            .collect::<Vec<_>>()
            .join("\r\n");
        format!("GET {path} HTTP/1.1\r\n{headers}\r\n\r\n").into_bytes()
    }

    /// Build the test request. HTTP uses an absolute-form request URI (it goes to the proxy,
    /// which forwards it); tunnelled protocols use origin-form. Mirrors `checker.py:_request`.
    fn build_request(&self, judge: &Judge, proto: Proto) -> (Vec<u8>, String) {
        let marker = fresh_marker();
        let mut hdrs = request_headers(Some(&marker));
        hdrs.insert("Host", judge.host.clone());
        hdrs.insert("Connection", "close".to_owned());
        hdrs.insert("Content-Length", "0".to_owned());
        let method = if self.post { "POST" } else { "GET" };
        let path = if proto.uses_full_path() {
            format!("http://{}{}", judge.host, judge.path)
        } else {
            judge.path.clone()
        };
        let headers: String = hdrs
            .iter()
            .map(|(k, v)| format!("{k}: {v}"))
            .collect::<Vec<_>>()
            .join("\r\n");
        let req = format!("{method} {path} HTTP/1.1\r\n{headers}\r\n\r\n").into_bytes();
        (req, marker)
    }

    /// Classify anonymity. `checker.py:_get_anonymity_lvl`: Transparent if any of the host's
    /// real external IPs appears in the (lowercased) page; else Anonymous if `via`/`proxy`
    /// counts exceed the judge's baseline; else High.
    fn anonymity_level(&self, content: &str, judge: &Judge) -> AnonLevel {
        let lower = content.to_lowercase();
        let found = get_all_ip(&lower);
        let real_visible = self
            .real_ext_ips
            .iter()
            .any(|ip| found.contains(&ip.to_string()));
        let via = lower.matches("via").count() > judge.marks.via
            || lower.matches("proxy").count() > judge.marks.proxy;

        if real_visible {
            AnonLevel::Transparent
        } else if via {
            AnonLevel::Anonymous
        } else {
            AnonLevel::High
        }
    }

    /// `checker.py:_types_passed`. Non-strict: pass if any confirmed type matches the request
    /// (by protocol, and level if one was requested). Strict: drop confirmed types whose level
    /// does not match, then pass if any survive.
    fn types_passed(&self, proxy: &mut Proxy) -> bool {
        types_passed(&self.requested, self.strict, proxy)
    }
}

/// The type/level admission test behind [`Checker::types_passed`], split out so it can be unit
/// tested without a judge pool.
///
/// `types_from` attaches `--lvl` to every requested type, but only HTTP *has* an anonymity level
/// ([`Proto::checks_anon_level`]); a tunnel type (HTTPS / CONNECT / SOCKS) is classified `None`.
/// Under `--strict`, a level filter on such a type is treated as not applying, so the type
/// survives — otherwise `--types HTTP HTTPS --lvl High --strict` strips every HTTPS type and the
/// server has no tunnel to give a CONNECT client (every `https://` request 502s). Stripping the
/// *HTTP* type of a `HTTP:Transparent + HTTPS` proxy is exactly what strict is for: the proxy
/// stays in the pool as HTTPS-only, so plain-HTTP clients never get routed through it.
///
/// Non-strict is unchanged: a tunnel type alone does not satisfy a level filter, so admission
/// still hinges on a confirmed HTTP type at a requested level (and then admits the proxy whole).
fn types_passed(requested: &[TypeSpec], strict: bool, proxy: &mut Proxy) -> bool {
    if requested.is_empty() {
        return true;
    }
    let requested: BTreeMap<Proto, Option<Vec<AnonLevel>>> = requested
        .iter()
        .map(|t| (t.proto, t.levels.clone()))
        .collect();

    let confirmed: Vec<(Proto, Option<AnonLevel>)> =
        proxy.types().iter().map(|(p, l)| (*p, *l)).collect();
    let mut to_remove = Vec::new();
    for (proto, lvl) in confirmed {
        let matches = match requested.get(&proto) {
            // proto not requested, or requested with no/empty level filter → matches any
            None | Some(None) => true,
            Some(Some(levels)) if levels.is_empty() => true,
            // strict only: a level filter does not apply to a protocol that carries no level
            Some(Some(_)) if !proto.checks_anon_level() => strict,
            Some(Some(levels)) => lvl.is_some_and(|l| levels.contains(&l)),
        };
        if matches {
            if !strict {
                return true;
            }
        } else if strict {
            to_remove.push(proto);
        }
    }
    for p in to_remove {
        proxy.remove_type(p);
    }
    strict && !proxy.types().is_empty()
}

/// Outcome of one connection attempt.
enum Attempt {
    /// The proxy works for this protocol; carries the classification [`Observation`].
    Working(Observation),
    /// Connected and responded, but the response failed validation.
    Invalid,
}

/// What a working attempt observed about the proxy: its anonymity level (HTTP only), the
/// capability profile (A4), and the trust verdict (A6).
#[derive(Debug, Default)]
struct Observation {
    level: Option<AnonLevel>,
    caps: Caps,
    trust: TrustReport,
}

/// A specific way a proxy's judge round-trip looked hostile (A6). Reported individually — never a
/// bare "untrusted" boolean — so the caller sees *which* signal fired.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrustSignal {
    /// Our canary nonce did not survive the round-trip unmutated (content tampering).
    CanaryMismatch,
    /// The echoed request carried a header we never sent (injection).
    InjectedHeader,
    /// The HTTPS judge presented a certificate that did not match the pin (MITM). Reserved for the
    /// optional cert-pin follow-up; the dependency-free core never emits it.
    CertMismatch,
}

/// The honeypot/hostility verdict for a proxy (A6). Empty = trusted.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TrustReport {
    pub signals: Vec<TrustSignal>,
}

impl TrustReport {
    /// No signal fired.
    pub fn trusted(&self) -> bool {
        self.signals.is_empty()
    }

    /// Assess a **decompressed** judge response against what we sent. Pure — the unit under test.
    ///
    /// - **Canary:** our nonce must appear verbatim (content tampering else).
    /// - **Injected headers:** the echoed request (scanned as `Name: value` lines) must carry no
    ///   header name outside the set we sent ∪ a benign-hop allowlist. `Via`/`X-Forwarded-For` are
    ///   the *anonymity* signal, not trust, so they are allow-listed to avoid double-counting.
    ///
    /// The scan splits on `": "` (colon-space), so a URL value like `https://…` on its own line is
    /// not mistaken for a header. It is a heuristic: opt-in, with documented false-positive guards.
    ///
    /// **Scope / limitations** (opt-in heuristic, honest about what it does *not* catch):
    /// - The injected-header scan only works against judges that echo the **raw request headers**
    ///   as `Name: value` lines. The bundled defaults do not: httpbin emits JSON (`"Name": "…"`,
    ///   whose quoted key fails the name check) and azenv-style judges emit `HTTP_NAME = value`
    ///   (no `": "`). So with the default judge pool this scan is inert (safe — no false positives,
    ///   but no detection). For real injection detection, pass a raw-header-echo judge via
    ///   `--judges`.
    /// - On the standard check path `canary` is the validity marker, which validation already
    ///   requires to be present — so `CanaryMismatch` cannot fire there. The signal exists for the
    ///   pure-function contract (callers assessing content that did not pass validity).
    pub fn assess(sent_header_names: &[&str], canary: &str, content: &str) -> TrustReport {
        let mut signals = Vec::new();
        if !content.contains(canary) {
            signals.push(TrustSignal::CanaryMismatch);
        }
        const BENIGN: &[&str] = &[
            "host",
            "connection",
            "content-length",
            "x-forwarded-for",
            "via",
        ];
        let sent: HashSet<String> = sent_header_names
            .iter()
            .map(|h| h.to_ascii_lowercase())
            .collect();
        for line in content.lines() {
            let Some((name, _)) = line.split_once(": ") else {
                continue;
            };
            let name = name.trim().to_ascii_lowercase();
            // Only plausible header names (letters/digits/hyphen) — skips `REMOTE_ADDR = …` etc.
            if name.is_empty() || !name.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') {
                continue;
            }
            if !sent.contains(&name) && !BENIGN.contains(&name.as_str()) {
                signals.push(TrustSignal::InjectedHeader);
                break;
            }
        }
        TrustReport { signals }
    }
}

/// The header names the checker sends on the test request (A6 injected-header baseline): the
/// constant request headers plus the three `build_request` adds.
fn sent_header_names() -> Vec<&'static str> {
    request_headers(None)
        .keys()
        .copied()
        .chain(["Host", "Connection", "Content-Length"])
        .collect()
}

/// Upper bound on the bytes the checker will buffer from one judge response. A judge page is a
/// few KiB; the response arrives through an **untrusted** proxy that can stream arbitrary bytes at
/// line rate for the whole `deadline`, and `max_conn` of those at once would exhaust memory. A
/// response that exceeds this is `BadResponse` (the page is not a judge page).
const MAX_JUDGE_RESPONSE: u64 = 1024 * 1024;

/// Read the whole response, up to [`MAX_JUDGE_RESPONSE`]. We send `Connection: close`, so the
/// judge closes the connection after the body — read-to-end is correct and captures the full page.
async fn read_response(
    stream: &mut crate::negotiator::Stream,
    deadline: Duration,
) -> Result<Vec<u8>, ProxyError> {
    let mut buf = Vec::with_capacity(4096);
    // Read one byte past the cap so an exactly-at-cap body is distinguishable from an oversized one.
    let mut limited = stream.take(MAX_JUDGE_RESPONSE + 1);
    let n = tokio::time::timeout(deadline, limited.read_to_end(&mut buf))
        .await
        .map_err(|_| ProxyError::Timeout)?
        .map_err(|_| ProxyError::Reset)?;
    if n == 0 {
        return Err(ProxyError::EmptyRecv);
    }
    if buf.len() as u64 > MAX_JUDGE_RESPONSE {
        return Err(ProxyError::BadResponse);
    }
    Ok(buf)
}

/// Split at the first `\r\n\r\n`. If there is none, everything is head and the body is empty.
fn split_head_body(raw: &[u8]) -> (&[u8], &[u8]) {
    match raw.windows(4).position(|w| w == b"\r\n\r\n") {
        Some(i) => (&raw[..i], &raw[i + 4..]),
        None => (raw, &[]),
    }
}

/// Decompress the body per `Content-Encoding`, matching `checker.py:_decompress_content`.
/// gzip/deflate are inflated; anything else (or a decode failure) falls back to a lossy UTF-8
/// read of the raw bytes.
fn decompress(head: &[u8], body: &[u8]) -> String {
    use flate2::read::{DeflateDecoder, GzDecoder};
    use std::io::Read;

    let head_lower = String::from_utf8_lossy(head).to_lowercase();
    let enc = head_lower
        .lines()
        .find_map(|l| l.strip_prefix("content-encoding:"))
        .map(|v| v.trim());

    let decoded = match enc {
        Some("gzip") => {
            let mut out = String::new();
            GzDecoder::new(body)
                .read_to_string(&mut out)
                .ok()
                .map(|_| out)
        }
        Some("deflate") => {
            let mut out = String::new();
            DeflateDecoder::new(body)
                .read_to_string(&mut out)
                .ok()
                .map(|_| out)
        }
        _ => None,
    };
    decoded.unwrap_or_else(|| String::from_utf8_lossy(body).into_owned())
}

/// The DNSBL query name for `ip` in `zone`: the reversed IPv4 octets, then the zone
/// (`1.2.3.4` + `zen.spamhaus.org` → `4.3.2.1.zen.spamhaus.org`). Returns `None` for IPv6:
/// DNSBLs use a nibble-reversed format for v6 that `checker.py`'s simple `split(".")` reversal
/// does not produce, so — like the Python — v6 is effectively unsupported and skipped here.
fn dnsbl_query(ip: IpAddr, zone: &str) -> Option<String> {
    match ip {
        IpAddr::V4(v4) => {
            let o = v4.octets();
            Some(format!("{}.{}.{}.{}.{}", o[3], o[2], o[1], o[0], zone))
        }
        IpAddr::V6(_) => None,
    }
}

/// The capability signals in a judge response (A4): whether the proxy forwarded our constant
/// Referer and Cookie header values through. Shared with [`response_is_valid`] so the marker
/// literals live in one place.
fn caps_from_content(content: &str) -> Caps {
    Caps {
        cookie_echo: content.contains("cookie=ok"),
        referer_echo: content.contains("https://www.google.com/"),
    }
}

/// A valid judge response echoes our marker, at least one IP, and the constant Referer and
/// Cookie header values — proving the proxy forwarded our request intact.
/// `checker.py:_check_test_response`. Case-sensitive (Python does not lowercase here).
fn response_is_valid(content: &str, marker: &str) -> bool {
    let caps = caps_from_content(content);
    content.contains(marker)
        && !get_all_ip(content).is_empty()
        && caps.referer_echo
        && caps.cookie_echo
}

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

    fn high_only() -> Vec<TypeSpec> {
        let levels = Some(vec![AnonLevel::High]);
        vec![
            TypeSpec {
                proto: Proto::Http,
                levels: levels.clone(),
            },
            TypeSpec {
                proto: Proto::Https,
                levels,
            },
        ]
    }

    fn proxy_with(types: &[(Proto, Option<AnonLevel>)]) -> Proxy {
        let mut p = Proxy::new("10.0.0.1".parse().unwrap(), 8080, BTreeSet::new());
        for (proto, lvl) in types {
            p.add_type(*proto, *lvl);
        }
        p
    }

    #[test]
    fn strict_keeps_tunnel_types_that_carry_no_anon_level() {
        // `--types HTTP HTTPS --lvl High --strict` on a proxy confirmed as both: HTTPS has no
        // level, so the filter does not apply to it and it must survive.
        let mut p = proxy_with(&[(Proto::Http, Some(AnonLevel::High)), (Proto::Https, None)]);
        assert!(types_passed(&high_only(), true, &mut p));
        assert!(p.types().contains_key(&Proto::Https));
        assert!(p.types().contains_key(&Proto::Http));
    }

    #[test]
    fn strict_strips_the_low_level_http_type_but_keeps_the_tunnel() {
        // The case strict exists for: a transparent HTTP proxy that also tunnels. It stays in
        // the pool as HTTPS-only, so a plain-HTTP client is never routed through it.
        let mut p = proxy_with(&[
            (Proto::Http, Some(AnonLevel::Transparent)),
            (Proto::Https, None),
        ]);
        assert!(types_passed(&high_only(), true, &mut p));
        assert!(!p.types().contains_key(&Proto::Http));
        assert!(p.types().contains_key(&Proto::Https));
    }

    #[test]
    fn strict_rejects_a_proxy_with_nothing_left() {
        let mut p = proxy_with(&[(Proto::Http, Some(AnonLevel::Transparent))]);
        assert!(!types_passed(&high_only(), true, &mut p));
        assert!(p.types().is_empty());
    }

    #[test]
    fn non_strict_is_unchanged() {
        // A confirmed HTTP type at a requested level admits the proxy whole, tunnel included.
        let mut p = proxy_with(&[(Proto::Http, Some(AnonLevel::High)), (Proto::Https, None)]);
        assert!(types_passed(&high_only(), false, &mut p));
        assert_eq!(p.types().len(), 2);
        // A tunnel type alone does not satisfy the level filter: `HTTP:Transparent + HTTPS` is
        // rejected, as before, so a plain-HTTP client is never routed through its HTTP side.
        let mut p = proxy_with(&[
            (Proto::Http, Some(AnonLevel::Transparent)),
            (Proto::Https, None),
        ]);
        assert!(!types_passed(&high_only(), false, &mut p));
    }

    #[test]
    fn split_head_body_splits_on_blank_line() {
        let (h, b) = split_head_body(b"HTTP/1.1 200 OK\r\nX: y\r\n\r\nbody here");
        assert_eq!(h, b"HTTP/1.1 200 OK\r\nX: y");
        assert_eq!(b, b"body here");
    }

    /// A local "judge" that writes `total` bytes and closes. Returns the connected client stream.
    async fn stream_of(total: usize) -> crate::negotiator::Stream {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            let (mut sock, _) = listener.accept().await.unwrap();
            let chunk = vec![b'A'; 64 * 1024];
            let mut left = total;
            while left > 0 {
                let n = left.min(chunk.len());
                if sock.write_all(&chunk[..n]).await.is_err() {
                    break;
                }
                left -= n;
            }
            let _ = sock.shutdown().await;
        });
        crate::negotiator::Stream::Plain(TcpStream::connect(addr).await.unwrap())
    }

    #[tokio::test]
    async fn read_response_accepts_a_body_at_the_cap() {
        let mut s = stream_of(MAX_JUDGE_RESPONSE as usize).await;
        let buf = read_response(&mut s, Duration::from_secs(5)).await.unwrap();
        assert_eq!(buf.len() as u64, MAX_JUDGE_RESPONSE);
    }

    #[tokio::test]
    async fn read_response_rejects_an_oversized_body_without_buffering_it() {
        // A hostile proxy streaming far past the cap: the read stops at cap+1 and fails as
        // BadResponse instead of growing the buffer until the deadline.
        let mut s = stream_of(8 * MAX_JUDGE_RESPONSE as usize).await;
        let err = read_response(&mut s, Duration::from_secs(5))
            .await
            .unwrap_err();
        assert_eq!(err, ProxyError::BadResponse);
    }

    #[test]
    fn dnsbl_query_reverses_ipv4_octets() {
        // checker.py: ".".join(reversed(host.split("."))) + "." + zone
        assert_eq!(
            dnsbl_query("1.2.3.4".parse().unwrap(), "zen.spamhaus.org").as_deref(),
            Some("4.3.2.1.zen.spamhaus.org")
        );
        // IPv6 is skipped (Python's dot-split reversal does not produce a valid v6 query).
        assert_eq!(
            dnsbl_query("2001:db8::1".parse().unwrap(), "zen.spamhaus.org"),
            None
        );
    }

    #[test]
    fn response_valid_requires_all_markers() {
        let good = "REMOTE_ADDR=1.2.3.4 UA=PxBroker/x/5555 \
                    Referer=https://www.google.com/ Cookie=cookie=ok";
        assert!(response_is_valid(good, "5555"));
        // missing cookie
        let no_cookie = "1.2.3.4 5555 https://www.google.com/";
        assert!(!response_is_valid(no_cookie, "5555"));
        // missing marker
        assert!(!response_is_valid(good, "9999"));
    }

    #[test]
    fn caps_extracted_from_content() {
        let both = "REMOTE_ADDR=1.2.3.4 cookie=ok Referer=https://www.google.com/";
        assert_eq!(
            caps_from_content(both),
            Caps {
                cookie_echo: true,
                referer_echo: true
            }
        );
        // drop the cookie echo
        let no_cookie = "REMOTE_ADDR=1.2.3.4 Referer=https://www.google.com/";
        assert_eq!(
            caps_from_content(no_cookie),
            Caps {
                cookie_echo: false,
                referer_echo: true
            }
        );
        // neither
        assert_eq!(caps_from_content("nothing here"), Caps::default());
    }

    #[test]
    fn default_policy_retries_only_timeout() {
        let p = RetryPolicy::default();
        assert_eq!(p.max_tries, 3);
        assert_eq!(p.retry_on, HashSet::from([ProxyError::Timeout]));
        assert!(!p.retry_on.contains(&ProxyError::Reset));
        assert!(p.backoff.is_zero());
    }

    #[test]
    fn retry_policy_backoff_schedule() {
        // Constant (factor 1.0): 100ms every attempt.
        let c = RetryPolicy {
            backoff: Duration::from_millis(100),
            factor: 1.0,
            ..Default::default()
        };
        for i in 0..=2 {
            assert_eq!(c.backoff_for(i).as_millis(), 100);
        }
        // Exponential (factor 2.0): 100 / 200 / 400.
        let e = RetryPolicy {
            backoff: Duration::from_millis(100),
            factor: 2.0,
            ..Default::default()
        };
        assert_eq!(e.backoff_for(0).as_millis(), 100);
        assert_eq!(e.backoff_for(1).as_millis(), 200);
        assert_eq!(e.backoff_for(2).as_millis(), 400);
        // max_backoff caps the delay.
        let cap = RetryPolicy {
            backoff: Duration::from_millis(100),
            factor: 2.0,
            max_backoff: Duration::from_millis(250),
            ..Default::default()
        };
        assert_eq!(cap.backoff_for(2).as_millis(), 250);
        // jitter 0.5 keeps each delay within [0.5x, 1.5x].
        let j = RetryPolicy {
            backoff: Duration::from_millis(100),
            jitter: 0.5,
            ..Default::default()
        };
        for _ in 0..64 {
            let ms = j.backoff_for(0).as_secs_f64() * 1000.0;
            assert!((50.0..=150.0).contains(&ms), "jitter out of band: {ms}ms");
        }
        // max_backoff is a HARD ceiling even with jitter (clamp applied last).
        let capped_jitter = RetryPolicy {
            backoff: Duration::from_millis(100),
            factor: 2.0,
            jitter: 0.5,
            max_backoff: Duration::from_millis(250),
            ..Default::default()
        };
        for i in 0..=4 {
            for _ in 0..32 {
                assert!(
                    capped_jitter.backoff_for(i) <= Duration::from_millis(250),
                    "backoff exceeded the cap at i={i}"
                );
            }
        }
        // Zero base → zero: the parity path never sleeps.
        assert_eq!(RetryPolicy::default().backoff_for(0), Duration::ZERO);
    }

    // The header names the checker sends (the injected-header baseline).
    const SENT: &[&str] = &[
        "User-Agent",
        "Accept",
        "Accept-Encoding",
        "Pragma",
        "Cache-control",
        "Cookie",
        "Referer",
        "Host",
        "Connection",
        "Content-Length",
    ];

    #[test]
    fn sent_header_names_matches_the_request() {
        // The baseline must equal what build_request actually sends, or the scan false-positives.
        let mut got = sent_header_names();
        got.sort_unstable();
        let mut want = SENT.to_vec();
        want.sort_unstable();
        assert_eq!(got, want);
    }

    #[test]
    fn trust_clean_response_is_trusted() {
        let content = "User-Agent: PxBroker/0.1/CANARY42\n\
             Cookie: cookie=ok\n\
             Referer: https://www.google.com/\n\
             Host: judge.example\n\
             REMOTE_ADDR = 8.8.8.8\n";
        let r = TrustReport::assess(SENT, "CANARY42", content);
        assert!(r.trusted(), "unexpected signals: {:?}", r.signals);
    }

    #[test]
    fn trust_injected_header_is_flagged() {
        let content = "User-Agent: PxBroker/0.1/CANARY42\n\
             Cookie: cookie=ok\n\
             X-Ad-Inject: buy now\n";
        let r = TrustReport::assess(SENT, "CANARY42", content);
        assert_eq!(r.signals, vec![TrustSignal::InjectedHeader]);
    }

    #[test]
    fn trust_canary_mutation_is_flagged() {
        let content = "User-Agent: PxBroker/0.1/MUTATED\n\
             Cookie: cookie=ok\n\
             Host: judge.example\n";
        let r = TrustReport::assess(SENT, "CANARY42", content);
        assert_eq!(r.signals, vec![TrustSignal::CanaryMismatch]);
    }

    #[test]
    fn trust_forwarded_via_headers_are_not_injection() {
        // Via / X-Forwarded-For are the anonymity signal, allow-listed so they don't double-count.
        let content = "User-Agent: PxBroker/0.1/CANARY42\n\
             Via: 1.1 someproxy\n\
             X-Forwarded-For: 8.8.8.8\n\
             Host: judge.example\n";
        let r = TrustReport::assess(SENT, "CANARY42", content);
        assert!(r.trusted(), "unexpected signals: {:?}", r.signals);
    }

    #[test]
    fn trust_url_value_on_its_own_is_not_a_header() {
        // A bare URL must not be parsed as a `https:` header (the `": "` split guards this).
        let content = "User-Agent: PxBroker/0.1/CANARY42\nhttps://www.google.com/\n";
        let r = TrustReport::assess(SENT, "CANARY42", content);
        assert!(r.trusted(), "unexpected signals: {:?}", r.signals);
    }
}