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
//! IP:port scanning. The one home for extracting proxy addresses from text.
//!
//! Rust's `regex` crate refuses `IPPortPatternGlobal` — it uses lookahead, and the crate
//! rejects look-around by design (finite automaton → linear-time guarantee). That is a
//! feature, not a limitation: `fancy-regex` would restore lookahead at the cost of the
//! ReDoS risk `utils.py`'s own comments boast of avoiding.
//!
//! So this is a two-pass scanner. Pass 1 uses the `regex` crate with proxybroker2's
//! **exact** IPv4 octet sub-pattern (which has no lookaround), so IP matching — including
//! its leading-zero quirk, `010.1.1.1` and `300.1.2.3 → 00.1.2.3` — is byte-identical.
//! Pass 2 does the IP↔port pairing in code. Verified against a characterization oracle in
//! `tests/ip_scan.rs`.
use Regex;
use LazyLock;
/// proxybroker2's exact IPv4 pattern (`utils.py:IPPattern`), verbatim. No lookaround, so
/// the `regex` crate accepts it. The `[01]?\d\d?` alternative is what preserves leading
/// zeros — matching Python's behaviour rather than "fixing" it here; validity is decided
/// later by `canonicalize_ip`, exactly as in the original.
const IPV4: &str = r"(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)";
pub static IPV4_RE: = new;
/// A 2–5 digit run — Python's `\d{2,5}` port group. `\d` is Unicode in both `re` and the
/// `regex` crate, so the two agree.
static PORT_RE: = new;
/// One proxy per line: the first IPv4 on a line, paired with the first 2–5 digit run after
/// it on that same line. Mirrors `IPPortPatternLine` (`re.MULTILINE`), used by the raw-data
/// loader (`api.py:_load`). A line with no IP, or an IP with no following 2-digit run, is
/// skipped.
/// Every IPv4 in the whole text, each paired with the nearest following token: a 2–5 digit
/// port, or an empty string if the nearest token is another IP. An IP with nothing after it
/// is dropped. Mirrors `IPPortPatternGlobal` (`re.DOTALL`), the default provider pattern.
///
/// The lookahead `(?=.*?(?:IP|port))` means: assert that, somewhere ahead, either another
/// IP or a port begins — and capture the port group only if the port alternative is what
/// matched. Because the alternation lists the IP first, a position where *both* could start
/// (a number that also begins an IP) resolves to the IP, leaving the port empty. That tie
/// rule is why the comparison below is strict (`port.start() < ip.start()`).