proxybroker 0.4.0

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
//! The [`Broker`]: orchestrates providers into a stream of proxies.
//!
//! `grab` scrapes providers without checking; `find` scrapes and checks. (`serve` builds on
//! `find` and lands with the server.)
//!
//! Delivery is a [`ProxyStream`], not proxybroker2's fire-and-forget-onto-a-queue. Termination
//! is the channel closing (drop the sender), not a `None` poison pill — broadcast- and
//! multi-consumer-safe by construction. Dropping the stream drops the receiver, so the source
//! task's next `send` fails and it stops: cancellation for free, without a sentinel or a
//! detached-task leak (design critique #14). See `decisions.md`.
//!
//! `find`'s concurrency is the delicate part (`decisions.md` §1). proxybroker2's `_on_check`
//! is a queue impersonating **two** primitives, which must stay separate here:
//!
//! - a [`Semaphore`](tokio::sync::Semaphore) bounds in-flight checks (the concurrency cap);
//! - a [`TaskTracker`](tokio_util::task::TaskTracker) is the wait-group we drain before
//!   ending the stream, so no check is silently dropped.
//!
//! A [`CancellationToken`] fired when the consumer drops the stream aborts in-flight checks —
//! not a detached-task leak (critique #14).

use crate::checker::{Checker, CheckerConfig, RetryPolicy};
use crate::provider::{fetch, ProviderSpec};
use crate::proxy::Proxy;
use crate::resolver::Resolver;
use crate::types::TypeSpec;
use futures_util::stream::{Stream, StreamExt};
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::sync::{mpsc, Semaphore};
use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;

#[cfg(feature = "geo")]
use crate::geo::GeoDb;

use crate::error::Error;
use crate::stats::StatsCollector;

/// The maximum number of providers fetched concurrently. `api.py:MAX_CONCURRENT_PROVIDERS`.
const MAX_CONCURRENT_PROVIDERS: usize = 3;

/// Channel depth between the source task and the consumer. Bounds memory and provides
/// backpressure: when the consumer is slow, the source task's `send` blocks.
const CHANNEL_CAPACITY: usize = 256;

/// What to gather. `limit: None` means unlimited (the CLI maps `--limit 0` to `None`, since
/// proxybroker2 relies on integer underflow for "unlimited" — see `decisions.md`).
#[derive(Debug, Clone, Default)]
pub struct GrabQuery {
    /// Keep only proxies located in these ISO country codes. `None` = no filter.
    pub countries: Option<Vec<String>>,
    /// Stop after this many proxies. `None` = unlimited.
    pub limit: Option<usize>,
}

/// What to find and check. `types` is required (empty is [`Error::NoTypes`]).
#[derive(Debug, Clone, PartialEq)]
pub struct FindQuery {
    /// Protocols (and optional anonymity levels) a proxy must support.
    pub types: Vec<TypeSpec>,
    /// Keep only proxies in these ISO country codes. `None` = no filter.
    pub countries: Option<Vec<String>>,
    /// Stop after this many working proxies. `None` = unlimited.
    pub limit: Option<usize>,
    /// Judge URLs to probe. Empty = the bundled defaults.
    pub judges: Vec<String>,
    /// DNS blocklist zones; a proxy whose IP is listed in any is rejected. Empty disables.
    pub dnsbl: Vec<String>,
    /// Per-request timeout.
    pub timeout: Duration,
    /// Max concurrent checks in flight. `api.py:max_conn`.
    pub max_conn: usize,
    /// Retry policy: attempts per protocol, which errors retry, and the backoff schedule (A5).
    pub retry: RetryPolicy,
    /// Use `POST` for the test request.
    pub post: bool,
    /// Require the anonymity level to match exactly.
    pub strict: bool,
    /// Fallback liveness URL: when no judge verifies, validate proxies with a 200 from this URL
    /// instead of failing. `None` keeps strict judge-required behavior (A2).
    pub liveness_url: Option<String>,
    /// Relax validity to marker+IP, recording Referer/Cookie as capabilities instead (A4).
    pub relaxed_validity: bool,
    /// Keep only proxies that forwarded our Cookie header through (A4).
    pub require_cookie: bool,
    /// Keep only proxies that forwarded our Referer header through (A4).
    pub require_referer: bool,
    /// Keep only proxies with a confirmed CONNECT:25 (SMTP) tunnel (A4).
    pub require_connect25: bool,
    /// Run honeypot detection and record the verdict (A6).
    pub trust_check: bool,
    /// Keep only proxies with a clean trust verdict (implies `trust_check`) (A6).
    pub require_trusted: bool,
}

impl Default for FindQuery {
    fn default() -> Self {
        FindQuery {
            types: Vec::new(),
            countries: None,
            limit: None,
            judges: Vec::new(),
            dnsbl: Vec::new(),
            timeout: Duration::from_secs(8),
            max_conn: 200,
            retry: RetryPolicy::default(),
            post: false,
            strict: false,
            liveness_url: None,
            relaxed_validity: false,
            require_cookie: false,
            require_referer: false,
            require_connect25: false,
            trust_check: false,
            require_trusted: false,
        }
    }
}

impl FindQuery {
    /// Start building a [`FindQuery`]. Consuming setters, unset fields fall back to
    /// [`FindQuery::default`] — the same shape as [`BrokerBuilder`].
    pub fn builder() -> FindQueryBuilder {
        FindQueryBuilder::default()
    }
}

/// Builds a [`FindQuery`]. `build()` is infallible — `find`/`check` own the [`Error::NoTypes`]
/// guard, so the builder must not duplicate it (a builder that can't fail is more composable).
#[derive(Default)]
pub struct FindQueryBuilder {
    types: Option<Vec<TypeSpec>>,
    countries: Option<Vec<String>>,
    limit: Option<usize>,
    judges: Option<Vec<String>>,
    dnsbl: Option<Vec<String>>,
    timeout: Option<Duration>,
    max_conn: Option<usize>,
    max_tries: Option<usize>,
    retry: Option<RetryPolicy>,
    post: Option<bool>,
    strict: Option<bool>,
    liveness_url: Option<String>,
}

impl FindQueryBuilder {
    /// Protocols (and optional anonymity levels) a proxy must support. Required by `find`/`check`.
    pub fn types(mut self, types: Vec<TypeSpec>) -> Self {
        self.types = Some(types);
        self
    }

    /// Keep only proxies in these ISO country codes.
    pub fn countries(mut self, countries: Vec<String>) -> Self {
        self.countries = Some(countries);
        self
    }

    /// Stop after this many working proxies. `0` maps to unlimited — the one home for the CLI's
    /// "`--limit 0` = unlimited" convention.
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = (limit > 0).then_some(limit);
        self
    }

    /// Judge URLs to probe. Empty defers to the bundled defaults.
    pub fn judges(mut self, judges: Vec<String>) -> Self {
        self.judges = Some(judges);
        self
    }

    /// DNS blocklist zones; a proxy listed in any is rejected.
    pub fn dnsbl(mut self, dnsbl: Vec<String>) -> Self {
        self.dnsbl = Some(dnsbl);
        self
    }

    /// Per-request timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Max concurrent checks in flight.
    pub fn max_conn(mut self, max_conn: usize) -> Self {
        self.max_conn = Some(max_conn);
        self
    }

    /// Attempts per protocol before giving up. Overrides just the count on the retry policy.
    pub fn max_tries(mut self, max_tries: usize) -> Self {
        self.max_tries = Some(max_tries);
        self
    }

    /// The full retry policy (which errors retry + backoff schedule, A5). `max_tries`, if also
    /// set, overrides this policy's attempt count.
    pub fn retry(mut self, retry: RetryPolicy) -> Self {
        self.retry = Some(retry);
        self
    }

    /// Use `POST` for the test request.
    pub fn post(mut self, post: bool) -> Self {
        self.post = Some(post);
        self
    }

    /// Require the anonymity level to match exactly.
    pub fn strict(mut self, strict: bool) -> Self {
        self.strict = Some(strict);
        self
    }

    /// Fallback liveness URL for graceful degradation when no judge verifies (A2).
    pub fn liveness_url(mut self, url: Option<String>) -> Self {
        self.liveness_url = url;
        self
    }

    /// Finalize. Unset fields take their [`FindQuery::default`] values. Note `limit` was already
    /// normalized by [`limit`](Self::limit) (0 → `None`), so `None` here means "unset", which
    /// `Default` also renders as unlimited.
    pub fn build(self) -> FindQuery {
        let d = FindQuery::default();
        FindQuery {
            types: self.types.unwrap_or(d.types),
            countries: self.countries.or(d.countries),
            limit: self.limit.or(d.limit),
            judges: self.judges.unwrap_or(d.judges),
            dnsbl: self.dnsbl.unwrap_or(d.dnsbl),
            timeout: self.timeout.unwrap_or(d.timeout),
            max_conn: self.max_conn.unwrap_or(d.max_conn),
            retry: {
                // `.retry(policy)` sets the whole policy; `.max_tries(n)` overrides just the count.
                let mut r = self.retry.unwrap_or(d.retry);
                if let Some(mt) = self.max_tries {
                    r.max_tries = mt;
                }
                r
            },
            post: self.post.unwrap_or(d.post),
            strict: self.strict.unwrap_or(d.strict),
            liveness_url: self.liveness_url.or(d.liveness_url),
            // A4/A6 flags have no builder setter (callers set the pub fields directly).
            relaxed_validity: d.relaxed_validity,
            require_cookie: d.require_cookie,
            require_referer: d.require_referer,
            require_connect25: d.require_connect25,
            trust_check: d.trust_check,
            require_trusted: d.require_trusted,
        }
    }
}

/// Finds proxies from a set of providers.
/// A callback invoked once for every checked proxy (working or not), right after it is recorded
/// into the shared stats. The `persist` layer (D2) installs one that upserts the proxy into the
/// state DB; kept as a plain `dyn Fn` so the broker never references the `persist` feature.
pub type CheckObserver = Arc<dyn Fn(&Proxy) + Send + Sync>;

#[derive(Clone)]
pub struct Broker {
    providers: Arc<Vec<ProviderSpec>>,
    client: reqwest::Client,
    /// Injectable so tests can stub external-IP discovery and DNS offline. `None` = build a
    /// default resolver on first `find`.
    resolver: Option<Arc<Resolver>>,
    #[cfg(feature = "geo")]
    geo: Option<Arc<GeoDb>>,
    /// ASN database (C8), from `--asn-db`. Separate from `geo`: ASN lives in its own MaxMind/DB-IP
    /// file, and nothing ASN-shaped is bundled, so this is `None` unless the caller supplies one.
    #[cfg(feature = "geo")]
    asn: Option<Arc<GeoDb>>,
    /// Observer fired per checked proxy (D2 persistence hook). `None` = no-op.
    on_checked: Option<CheckObserver>,
}

impl Broker {
    /// Attach a per-checked-proxy observer (builder-style, consuming). Used by the CLI to install
    /// the D2 store's upsert when `--state` is set.
    pub fn with_observer(mut self, obs: Option<CheckObserver>) -> Self {
        self.on_checked = obs;
        self
    }
}

impl Broker {
    /// Start building a broker.
    pub fn builder() -> BrokerBuilder {
        BrokerBuilder::default()
    }

    /// Gather proxies from the providers **without checking** them. `api.py:Broker.grab`.
    ///
    /// Returns immediately with a stream; the work runs in a spawned task. Proxies are
    /// deduplicated by `(host, port)`, optionally country-filtered, and capped at
    /// `query.limit`.
    pub fn grab(&self, query: GrabQuery) -> ProxyStream {
        let (tx, rx) = mpsc::channel(CHANNEL_CAPACITY);
        let broker = self.clone();
        tokio::spawn(async move { broker.grab_task(query, tx).await });
        ProxyStream {
            rx,
            _cancel_on_drop: None,
            stats: None,
        }
    }

    async fn grab_task(self, query: GrabQuery, tx: mpsc::Sender<Proxy>) {
        let countries: Option<BTreeSet<String>> = query
            .countries
            .map(|cs| cs.into_iter().map(|c| c.to_uppercase()).collect());

        let mut seen: BTreeSet<(IpAddr, u16)> = BTreeSet::new();
        let mut sent = 0usize;

        // Each future owns its inputs (a `ProviderSpec` clone and a cheap `reqwest::Client`
        // clone — the client is `Arc` internally) so nothing borrows `self` across the
        // buffered stream, which otherwise trips a higher-ranked-lifetime error.
        let client = self.client.clone();
        let specs: Vec<ProviderSpec> = self.providers.as_ref().clone();
        let mut fetches = futures_util::stream::iter(specs)
            .map(|spec| {
                let client = client.clone();
                async move { fetch(&spec, &client).await }
            })
            .buffer_unordered(MAX_CONCURRENT_PROVIDERS);

        while let Some(candidates) = fetches.next().await {
            for cand in candidates {
                if query.limit.is_some_and(|l| sent >= l) {
                    return; // limit reached — drop tx, ending the stream
                }
                let Ok(host) = cand.host.parse::<IpAddr>() else {
                    continue;
                };
                if !seen.insert((host, cand.port)) {
                    continue; // duplicate (host, port)
                }
                let mut proxy = Proxy::new(host, cand.port, cand.protocols.clone());
                self.attach_geo(&mut proxy);
                if !country_ok(&proxy, countries.as_ref()) {
                    continue;
                }
                if tx.send(proxy).await.is_err() {
                    return; // consumer dropped the stream — stop working
                }
                sent += 1;
            }
        }
    }

    /// Gather **and check** proxies. `api.py:Broker.find`.
    ///
    /// Unlike proxybroker2's `find` (fire-and-forget onto a queue), this returns a
    /// `Result<ProxyStream>`: the up-front work that can fail — discovering the host's
    /// external IPs and verifying at least one judge — happens before the stream is returned,
    /// so [`Error::NoTypes`], [`Error::ExtIpUnknown`], and [`Error::NoJudges`] surface here
    /// rather than silently producing an empty stream.
    pub async fn find(&self, query: FindQuery) -> Result<ProxyStream, Error> {
        if query.types.is_empty() {
            return Err(Error::NoTypes);
        }
        let checker = self.build_checker(&query).await?;
        let (tx, rx, cancel, stats) = new_run();
        let broker = self.clone();
        let task_cancel = cancel.clone();
        let task_stats = stats.clone();
        tokio::spawn(async move {
            broker
                .find_task(query, checker, tx, task_cancel, task_stats)
                .await
        });
        Ok(ProxyStream {
            rx,
            _cancel_on_drop: Some(cancel.drop_guard()),
            stats: Some(stats),
        })
    }

    /// Gather **and check** proxies the caller already has, instead of scraping providers.
    /// The counterpart to [`Broker::find`], sharing the same check pipeline — see `check_stream`.
    ///
    /// `proxies` is any stream of unchecked [`Proxy`]s (parse a file/stdin with
    /// [`crate::parse::parse_proxy_lines`], or build them directly). Geo is attached per proxy so
    /// serialized output carries country. The same errors surface up front as `find`
    /// ([`Error::NoTypes`], [`Error::ExtIpUnknown`], [`Error::NoJudges`]).
    pub async fn check<S>(&self, proxies: S, query: FindQuery) -> Result<ProxyStream, Error>
    where
        S: Stream<Item = Proxy> + Send + 'static,
    {
        if query.types.is_empty() {
            return Err(Error::NoTypes);
        }
        let checker = self.build_checker(&query).await?;
        let (tx, rx, cancel, stats) = new_run();
        let broker = self.clone();
        let task_cancel = cancel.clone();
        let task_stats = stats.clone();
        let countries = uppercase_set(query.countries.clone());
        let (max_conn, limit) = (query.max_conn, query.limit);
        let caps = CapsFilter::from_query(&query);
        let on_checked = self.on_checked.clone();
        tokio::spawn(async move {
            // Attach geo + apply the country filter before checking, mirroring find's source.
            let source = proxies.filter_map(move |mut proxy| {
                broker.attach_geo(&mut proxy);
                let keep = country_ok(&proxy, countries.as_ref()).then_some(proxy);
                std::future::ready(keep)
            });
            check_stream(
                source,
                checker,
                max_conn,
                limit,
                caps,
                on_checked,
                tx,
                task_cancel,
                task_stats,
            )
            .await;
        });
        Ok(ProxyStream {
            rx,
            _cancel_on_drop: Some(cancel.drop_guard()),
            stats: Some(stats),
        })
    }

    /// Convenience (E1): discover proxies with [`find`](Self::find), feed them into a
    /// background-warming [`Pool`](crate::server::Pool), and wrap it in a ready
    /// [`RotatingProxyConnector`](crate::connector::RotatingProxyConnector) — the whole
    /// discover → pool → rotate pipeline in one call. The pool fills as `find` streams; the
    /// connector routes each connection through a healthy proxy (dead ones self-eject via the
    /// pool's health thresholds).
    ///
    /// For a pre-seeded pool or a custom [`PoolConfig`](crate::server::PoolConfig), compose the
    /// pieces yourself: `find` → [`Pool::spawn`](crate::server::Pool::spawn) →
    /// [`RotatingProxyConnector::from_pool`](crate::connector::RotatingProxyConnector::from_pool).
    ///
    /// The connector hands hyper a **raw tunnel** to the target and does not terminate TLS to it;
    /// for an `https://` target, layer TLS over the connector (e.g. a `hyper-rustls`
    /// `HttpsConnector`).
    #[cfg(feature = "connector")]
    pub async fn rotating(
        &self,
        query: FindQuery,
        cfg: crate::connector::RotateConfig,
    ) -> Result<crate::connector::RotatingProxyConnector, Error> {
        // A resolver for the connector's target-host lookups. Reuse the broker's if one was set;
        // otherwise a fresh one (find builds its own internally, so an unset broker briefly holds
        // two — both cheap, and DNS is DNS).
        let resolver = match &self.resolver {
            Some(r) => r.clone(),
            None => Arc::new(Resolver::new(query.timeout)?),
        };
        let stream = self.find(query).await?;
        let pool = crate::server::Pool::spawn(stream, crate::server::PoolConfig::default());
        Ok(crate::connector::RotatingProxyConnector::from_pool(
            pool, resolver, cfg,
        ))
    }

    /// The resolver + external-IP discovery + [`Checker`] setup shared by `find` and `check`.
    /// Proxy candidates are already IP literals, so the resolver is only for external-IP
    /// discovery and judge-host resolution.
    pub async fn build_checker(&self, query: &FindQuery) -> Result<Arc<Checker>, Error> {
        let resolver = match &self.resolver {
            Some(r) => r.clone(),
            None => Arc::new(Resolver::new(query.timeout)?),
        };
        let real_ext_ips = resolver.external_ips().await?;
        let checker = Checker::new(
            CheckerConfig {
                judges: query.judges.clone(),
                types: query.types.clone(),
                timeout: query.timeout,
                retry: query.retry.clone(),
                post: query.post,
                strict: query.strict,
                relaxed_validity: query.relaxed_validity,
                // --require-trusted implies running the check.
                trust_check: query.trust_check || query.require_trusted,
                dnsbl: query.dnsbl.clone(),
                liveness_url: query.liveness_url.clone(),
            },
            resolver,
            &self.client,
            real_ext_ips,
        )
        .await?;
        Ok(Arc::new(checker))
    }

    async fn find_task(
        self,
        query: FindQuery,
        checker: Arc<Checker>,
        tx: mpsc::Sender<Proxy>,
        cancel: CancellationToken,
        stats: Arc<std::sync::Mutex<StatsCollector>>,
    ) {
        let countries = uppercase_set(query.countries.clone());
        let on_checked = self.on_checked.clone();
        let client = self.client.clone();
        let specs: Vec<ProviderSpec> = self.providers.as_ref().clone();
        let fetches = futures_util::stream::iter(specs)
            .map(|spec| {
                let client = client.clone();
                async move { fetch(&spec, &client).await }
            })
            .buffer_unordered(MAX_CONCURRENT_PROVIDERS);

        // Flatten provider batches into a per-proxy stream: dedup on (host, port), attach geo,
        // apply the country filter. `seen` is threaded through the FnMut closure.
        let broker = self.clone();
        let mut seen: BTreeSet<(IpAddr, u16)> = BTreeSet::new();
        let source = fetches
            .flat_map(futures_util::stream::iter)
            .filter_map(move |cand| {
                let keep = match cand.host.parse::<IpAddr>() {
                    Ok(host) if seen.insert((host, cand.port)) => {
                        let mut proxy = Proxy::new(host, cand.port, cand.protocols.clone());
                        broker.attach_geo(&mut proxy);
                        country_ok(&proxy, countries.as_ref()).then_some(proxy)
                    }
                    _ => None,
                };
                std::future::ready(keep)
            });

        check_stream(
            source,
            checker,
            query.max_conn,
            query.limit,
            CapsFilter::from_query(&query),
            on_checked,
            tx,
            cancel,
            stats,
        )
        .await;
    }

    #[cfg(feature = "geo")]
    fn attach_geo(&self, proxy: &mut Proxy) {
        if let Some(db) = &self.geo {
            proxy.geo = db.lookup(proxy.host);
        }
        // C8: ASN attribution from a separate --asn-db, independent of the country lookup above.
        if let Some(db) = &self.asn {
            proxy.asn = db.lookup_asn(proxy.host);
        }
    }

    #[cfg(not(feature = "geo"))]
    fn attach_geo(&self, _proxy: &mut Proxy) {}
}

/// Has the emit count reached the limit? `None` = unlimited.
fn is_limit_reached(sent: &AtomicUsize, limit: Option<usize>) -> bool {
    limit.is_some_and(|l| sent.load(Ordering::SeqCst) >= l)
}

/// The channel + cancellation + stats triple every run (`find`/`check`) is built from.
type Run = (
    mpsc::Sender<Proxy>,
    mpsc::Receiver<Proxy>,
    CancellationToken,
    Arc<std::sync::Mutex<StatsCollector>>,
);

fn new_run() -> Run {
    let (tx, rx) = mpsc::channel(CHANNEL_CAPACITY);
    let cancel = CancellationToken::new();
    // Shared across every check task so stats cover ALL checked proxies, not just the working
    // ones streamed out (design review F4).
    let stats = Arc::new(std::sync::Mutex::new(StatsCollector::default()));
    (tx, rx, cancel, stats)
}

/// Uppercase an optional country list into a set for case-insensitive matching.
fn uppercase_set(countries: Option<Vec<String>>) -> Option<BTreeSet<String>> {
    countries.map(|v| v.into_iter().map(|c| c.to_uppercase()).collect())
}

/// The per-proxy concurrency pipeline shared by `find` and `check`: a [`Semaphore`] concurrency
/// cap, a [`TaskTracker`] wait-group drained before the stream ends, atomic limit accounting,
/// per-check `stats.record`, and cancel-on-drop. Source-agnostic — `find` feeds provider
/// candidates, `check` feeds user input.
// Internal check driver: all eight parameters are distinct run state (source, checker, flow
// control, filter, and the tx/cancel/stats plumbing). Bundling them would only add indirection.
#[allow(clippy::too_many_arguments)]
async fn check_stream<S>(
    source: S,
    checker: Arc<Checker>,
    max_conn: usize,
    limit: Option<usize>,
    caps: CapsFilter,
    on_checked: Option<CheckObserver>,
    tx: mpsc::Sender<Proxy>,
    cancel: CancellationToken,
    stats: Arc<std::sync::Mutex<StatsCollector>>,
) where
    S: Stream<Item = Proxy> + Send,
{
    let sem = Arc::new(Semaphore::new(max_conn));
    let tracker = TaskTracker::new();
    let sent = Arc::new(AtomicUsize::new(0));
    let mut source = std::pin::pin!(source);

    while let Some(mut proxy) = source.next().await {
        if cancel.is_cancelled() || is_limit_reached(&sent, limit) {
            break;
        }
        // Acquire a permit BEFORE spawning; move it into the task so its Drop frees the slot —
        // race-free by construction (decisions.md §1).
        let Ok(permit) = sem.clone().acquire_owned().await else {
            break;
        };
        let checker = checker.clone();
        let tx = tx.clone();
        let sent = sent.clone();
        let cancel = cancel.clone();
        let stats = stats.clone();
        let on_checked = on_checked.clone();
        tracker.spawn(async move {
            let _permit = permit;
            tokio::select! {
                _ = cancel.cancelled() => {} // consumer gone — abort
                working = checker.check(&mut proxy) => {
                    // Record EVERY checked proxy (working or not) before it is sent or dropped,
                    // so the stats reflect the whole checked set (review F4).
                    stats.lock().unwrap().record(&proxy);
                    // D2: fold the finished proxy's outcome into durable state (upsert), beside
                    // the stats record — every checked proxy, working or not.
                    if let Some(obs) = &on_checked {
                        obs(&proxy);
                    }
                    // A working proxy is still dropped if it fails the capability filter (A4);
                    // it stays counted in stats (it *is* working), it just isn't emitted.
                    if working && caps.passes(&proxy) {
                        // Reserve a slot atomically; emit only if under the limit.
                        let n = sent.fetch_add(1, Ordering::SeqCst);
                        match limit {
                            Some(l) if n >= l => cancel.cancel(),
                            _ => {
                                let _ = tx.send(proxy).await;
                                if limit.is_some_and(|l| n + 1 >= l) {
                                    cancel.cancel();
                                }
                            }
                        }
                    }
                }
            }
        });
    }

    // Wait-group: drain every spawned check before dropping tx and ending the stream.
    tracker.close();
    tracker.wait().await;
}

/// The post-check capability filter (A4): a working proxy is kept only if it satisfies every
/// requested capability. All-false (the default) is a no-op.
#[derive(Clone, Copy, Default)]
struct CapsFilter {
    cookie: bool,
    referer: bool,
    connect25: bool,
    trusted: bool,
}

impl CapsFilter {
    fn from_query(q: &FindQuery) -> Self {
        CapsFilter {
            cookie: q.require_cookie,
            referer: q.require_referer,
            connect25: q.require_connect25,
            trusted: q.require_trusted,
        }
    }

    fn passes(&self, p: &Proxy) -> bool {
        let c = p.capabilities();
        (!self.cookie || c.cookie_echo)
            && (!self.referer || c.referer_echo)
            && (!self.connect25 || c.connect25)
            && (!self.trusted || p.trust().trusted())
    }
}

/// A country filter that is a no-op when no countries are requested. Matches `api.py`'s
/// `_geo_passed`: keep the proxy if its country code is in the requested set. `pub(crate)` so the
/// server's pool admission (B4) applies the identical predicate to a warm/BYO pool.
pub(crate) fn country_ok(proxy: &Proxy, countries: Option<&BTreeSet<String>>) -> bool {
    match countries {
        None => true,
        Some(set) => proxy
            .geo
            .as_ref()
            .is_some_and(|c| set.contains(&c.code.to_uppercase())),
    }
}

/// Builds a [`Broker`].
#[derive(Default)]
pub struct BrokerBuilder {
    providers: Option<Vec<ProviderSpec>>,
    client: Option<reqwest::Client>,
    resolver: Option<Arc<Resolver>>,
    #[cfg(feature = "geo")]
    geo: Option<Arc<GeoDb>>,
    #[cfg(feature = "geo")]
    no_geo: bool,
    #[cfg(feature = "geo")]
    asn: Option<Arc<GeoDb>>,
}

impl BrokerBuilder {
    /// Use a specific provider list instead of the bundled registry.
    pub fn providers(mut self, providers: Vec<ProviderSpec>) -> Self {
        self.providers = Some(providers);
        self
    }

    /// Use a specific HTTP client (timeouts, proxy, TLS config).
    pub fn client(mut self, client: reqwest::Client) -> Self {
        self.client = Some(client);
        self
    }

    /// Use a specific resolver — mainly for tests, which stub external-IP discovery and DNS
    /// so `find` runs fully offline.
    pub fn resolver(mut self, resolver: Resolver) -> Self {
        self.resolver = Some(Arc::new(resolver));
        self
    }

    /// Attach a geo database for country lookup and filtering, overriding the bundled default.
    #[cfg(feature = "geo")]
    pub fn geo(mut self, db: GeoDb) -> Self {
        self.geo = Some(Arc::new(db));
        self
    }

    /// Do **not** attach any geo database. Country filtering will then reject every proxy (a
    /// proxy with unknown location cannot match a country), and proxies carry no `geo`. Use
    /// this to skip loading the ~8 MB bundled database when you do not need geolocation.
    #[cfg(feature = "geo")]
    pub fn without_geo(mut self) -> Self {
        self.no_geo = true;
        self
    }

    /// Attach an ASN database (C8) so checked proxies carry their Autonomous System (`proxy.asn`).
    /// This is a **separate** database from [`geo`](Self::geo) — ASN ships in its own MaxMind/DB-IP
    /// file — and there is no bundled default, so without this call `proxy.asn` stays `None`.
    #[cfg(feature = "geo")]
    pub fn asn_db(mut self, db: GeoDb) -> Self {
        self.asn = Some(Arc::new(db));
        self
    }

    pub fn build(self) -> Broker {
        crate::install_default_crypto_provider(); // before building the default reqwest client
                                                  // Auto-attach the bundled geo database when built with `geo-bundled` (the default) and
                                                  // the caller neither supplied one nor opted out. Without this, country filtering
                                                  // silently rejects everything — a footgun for library users, since a proxy with no
                                                  // known location can never match a requested country.
        #[cfg(feature = "geo")]
        let geo = match (self.geo, self.no_geo) {
            (Some(db), _) => Some(db),
            (None, true) => None,
            #[cfg(feature = "geo-bundled")]
            (None, false) => GeoDb::bundled().ok().map(Arc::new),
            #[cfg(not(feature = "geo-bundled"))]
            (None, false) => None,
        };

        Broker {
            providers: Arc::new(
                self.providers
                    .unwrap_or_else(crate::provider::bundled_registry),
            ),
            client: self.client.unwrap_or_default(),
            resolver: self.resolver,
            #[cfg(feature = "geo")]
            geo,
            #[cfg(feature = "geo")]
            asn: self.asn,
            on_checked: None,
        }
    }
}

/// A stream of discovered proxies. Ends when the source is exhausted, the limit is reached,
/// or this stream is dropped (which stops the source task).
#[derive(Debug)]
pub struct ProxyStream {
    rx: mpsc::Receiver<Proxy>,
    /// For `find`: dropping the stream fires this token, aborting in-flight check tasks. For
    /// `grab` it is `None` — dropping the receiver already stops the single source task.
    _cancel_on_drop: Option<tokio_util::sync::DropGuard>,
    /// For `find`: the running aggregate over every checked proxy. `None` for `grab` (nothing
    /// is checked). Read it after the stream has been fully drained — by then every check has
    /// completed and recorded (the source task drains its `TaskTracker` before ending).
    stats: Option<Arc<std::sync::Mutex<StatsCollector>>>,
}

impl ProxyStream {
    /// Aggregate statistics over every proxy checked so far. `Some` only for `find`; call it
    /// after the stream is drained for a complete picture. `None` for `grab`.
    pub fn stats(&self) -> Option<crate::stats::Stats> {
        self.stats.as_ref().map(|s| s.lock().unwrap().finish())
    }
}

impl Stream for ProxyStream {
    type Item = Proxy;
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Proxy>> {
        self.rx.poll_recv(cx)
    }
}

impl std::fmt::Debug for Broker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Broker")
            .field("providers", &self.providers.len())
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{Proto, TypeSpec};

    #[test]
    fn find_query_builder_matches_default() {
        let built = FindQuery::builder()
            .types(vec![TypeSpec::any(Proto::Http)])
            .limit(10)
            .build();
        let hand = FindQuery {
            types: vec![TypeSpec::any(Proto::Http)],
            limit: Some(10),
            ..Default::default()
        };
        assert_eq!(built, hand);
    }

    #[test]
    fn find_query_builder_limit_zero_is_unlimited() {
        // The CLI's "0 = unlimited" convention lives here, so callers never hand find() a
        // take(0) that would yield nothing.
        assert_eq!(FindQuery::builder().limit(0).build().limit, None);
    }

    #[test]
    fn find_query_builder_sets_every_field() {
        let q = FindQuery::builder()
            .types(vec![TypeSpec::any(Proto::Socks5)])
            .countries(vec!["US".into()])
            .limit(5)
            .judges(vec!["http://j/".into()])
            .dnsbl(vec!["zen.example.org".into()])
            .timeout(Duration::from_secs(3))
            .max_conn(7)
            .max_tries(2)
            .post(true)
            .strict(true)
            .build();
        assert_eq!(
            q,
            FindQuery {
                types: vec![TypeSpec::any(Proto::Socks5)],
                countries: Some(vec!["US".into()]),
                limit: Some(5),
                judges: vec!["http://j/".into()],
                dnsbl: vec!["zen.example.org".into()],
                timeout: Duration::from_secs(3),
                max_conn: 7,
                retry: RetryPolicy::tries(2),
                post: true,
                strict: true,
                liveness_url: None,
                relaxed_validity: false,
                require_cookie: false,
                require_referer: false,
                require_connect25: false,
                trust_check: false,
                require_trusted: false,
            }
        );
    }
}