proxybroker 0.1.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
//! 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, Proto, TypeSpec};
use crate::utils::{fresh_marker, get_all_ip, get_status_code, request_headers};
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,
    max_tries: usize,
    post: bool,
    strict: 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>,
}

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()
    }
}

/// 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,
    pub max_tries: usize,
    /// 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,
    /// DNS blocklist zones (`--dnsbl`); a proxy whose IP is listed in any zone is rejected.
    pub dnsbl: Vec<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;
        if judges.is_empty() {
            return Err(Error::NoJudges);
        }
        Ok(Checker {
            judges,
            real_ext_ips,
            requested: cfg.types,
            timeout: cfg.timeout,
            max_tries: cfg.max_tries,
            post: cfg.post,
            strict: cfg.strict,
            dnsbl: cfg.dnsbl,
            resolver,
        })
    }

    /// 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();
        let Some(judge) = self.judges.random(scheme) else {
            return false; // no judge for this scheme
        };
        let target = Target {
            host: judge.host.clone(),
            ip: judge.ip,
            port: scheme.default_port(),
        };

        for _ in 0..self.max_tries {
            let start = Instant::now();
            match self.attempt(proxy, proto, &judge, &target).await {
                Ok(Attempt::Working(level)) => {
                    proxy.record_attempt(Some(start.elapsed().as_secs_f64()), None);
                    proxy.add_type(proto, level);
                    return true;
                }
                Ok(Attempt::Invalid) => {
                    // Response did not validate — the proxy does not work for this protocol.
                    // Python breaks here (no retry).
                    proxy.record_attempt(None, Some(ProxyError::BadResponse));
                    return false;
                }
                Err(ProxyError::Timeout) => {
                    proxy.record_attempt(None, Some(ProxyError::Timeout));
                    continue; // retry with a fresh connection
                }
                Err(e) => {
                    proxy.record_attempt(None, Some(e));
                    return false; // break
                }
            }
        }
        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,
        judge: &Judge,
        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)?;

        let mut stream = negotiate(proto, tcp, target, self.timeout).await?;

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

        let (request, marker) = self.build_request(judge, proto);
        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);
        }
        let content = decompress(head, body);

        if !response_is_valid(&content, &marker) {
            return Ok(Attempt::Invalid);
        }
        let level = if proto.checks_anon_level() {
            Some(self.anonymity_level(&content, judge))
        } else {
            None
        };
        Ok(Attempt::Working(level))
    }

    /// 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 {
        if self.requested.is_empty() {
            return true;
        }
        let requested: BTreeMap<Proto, Option<Vec<AnonLevel>>> = self
            .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,
                Some(Some(levels)) => lvl.is_some_and(|l| levels.contains(&l)),
            };
            if matches {
                if !self.strict {
                    return true;
                }
            } else if self.strict {
                to_remove.push(proto);
            }
        }
        for p in to_remove {
            proxy.remove_type(p);
        }
        self.strict && !proxy.types().is_empty()
    }
}

/// Outcome of one connection attempt.
enum Attempt {
    /// The proxy works for this protocol; carries the anonymity level (HTTP only).
    Working(Option<AnonLevel>),
    /// Connected and responded, but the response failed validation.
    Invalid,
}

/// Read the whole 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);
    let n = tokio::time::timeout(deadline, stream.read_to_end(&mut buf))
        .await
        .map_err(|_| ProxyError::Timeout)?
        .map_err(|_| ProxyError::Reset)?;
    if n == 0 {
        return Err(ProxyError::EmptyRecv);
    }
    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,
    }
}

/// 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 {
    content.contains(marker)
        && !get_all_ip(content).is_empty()
        && content.contains("https://www.google.com/") // Referer
        && content.contains("cookie=ok") // Cookie
}

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

    #[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");
    }

    #[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"));
    }
}