proxy-scraper-checker 0.1.3

A command-line tool for scraping and checking HTTP and SOCKS5 proxies from the checkerproxy.net proxies archive
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
use anyhow::{bail, Context, Result};
use futures::future;
use indicatif::ProgressBar;
use scraper::{Html, Selector};
use serde_json::Value;
use std::cmp::Ordering;
use std::collections::HashSet;
use std::fmt;
use std::hash::Hash;
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{AcquireError, Semaphore};

/// Represents the type of proxy.
#[derive(Debug, PartialEq)]
pub enum ProxyType {
    Http,
    Socks5,
}

/// Represents a proxy, which can be either an HTTP or SOCKS5 proxy.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum Proxy {
    Http(String),
    Socks5(String),
}

impl fmt::Display for Proxy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Proxy::Http(host) | Proxy::Socks5(host) => write!(f, "{host}"),
        }
    }
}

impl PartialOrd for Proxy {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Proxy {
    fn cmp(&self, other: &Self) -> Ordering {
        let (addr, port) = self
            .parse_host()
            .unwrap_or_else(|_| (IpAddr::from([0, 0, 0, 0]), 0));

        let (other_addr, other_port) = other
            .parse_host()
            .unwrap_or_else(|_| (IpAddr::from([0, 0, 0, 0]), 0));

        if addr < other_addr {
            Ordering::Less
        } else if addr > other_addr {
            Ordering::Greater
        } else if port < other_port {
            Ordering::Less
        } else if port > other_port {
            Ordering::Greater
        } else {
            Ordering::Equal
        }
    }
}

impl Proxy {
    /// Returns the URL of the proxy.
    fn url(&self) -> String {
        match self {
            Proxy::Http(host) => format!("http://{host}"),
            Proxy::Socks5(host) => format!("socks5://{host}"),
        }
    }

    /// Returns a tuple containing the IP address and port of the proxy.
    ///
    /// ## Errors
    ///
    /// Returns an error if the proxy is invalid.
    fn parse_host(&self) -> Result<(IpAddr, u16)> {
        let host = self.to_string();
        let parts: Vec<&str> = host.split(':').collect();

        if parts.len() != 2 {
            bail!("Invalid proxy: {host}");
        }

        let addr = IpAddr::from_str(parts[0]).context("Invalid IP address")?;
        let port: u16 = parts[1].parse().context("Invalid port")?;
        Ok((addr, port))
    }
}

/// A collection of unique HTTP and SOCKS5 proxies.
///
/// ## Fields
///
/// * `http` - A [`HashSet`] of unique HTTP proxies.
/// * `socks5` - A [`HashSet`] of unique SOCKS5 proxies.
#[derive(Debug)]
pub struct Proxies {
    pub http: HashSet<Proxy>,
    pub socks5: HashSet<Proxy>,
}

impl Proxies {
    /// Returns a new [`Proxies`] instance.
    pub fn new() -> Self {
        Self {
            http: HashSet::new(),
            socks5: HashSet::new(),
        }
    }

    /// Adds a proxy to the respective proxy list.
    ///
    /// ## Parameters
    ///
    /// * `proxy` - The proxy to add.
    ///
    /// ## Returns
    ///
    /// `true` if the proxy was added, `false` if it was already present.
    pub fn add(&mut self, proxy: Proxy) -> bool {
        match proxy {
            Proxy::Http(_) => self.http.insert(proxy),
            Proxy::Socks5(_) => self.socks5.insert(proxy),
        }
    }
}

impl FromIterator<Proxy> for Proxies {
    fn from_iter<T: IntoIterator<Item = Proxy>>(iter: T) -> Self {
        let mut proxy_lists = Self::new();

        for proxy in iter {
            proxy_lists.add(proxy);
        }

        proxy_lists
    }
}

/// Used to scrape proxies from the checkerproxy.net proxies archive.
///
/// ## Fields
///
/// * `client` - The [`reqwest::Client`] to use for making requests.
#[derive(Clone, Debug)]
pub struct ProxyScraper {
    client: reqwest::Client,
}

impl ProxyScraper {
    /// Returns a new [`ProxyScraper`] instance.
    ///
    /// ## Parameters
    ///
    /// * `client` - The [`reqwest::Client`] to use for making requests.
    pub fn new(client: reqwest::Client) -> Self {
        Self { client }
    }

    /// Scrapes the archive URLs from the <https://checkerproxy.net/getAllProxy> page
    /// and converts them into the API URLs.
    ///
    /// ## Returns
    ///
    /// A [`Vec`] of the API URLs.
    ///
    /// ## Errors
    ///
    /// Returns an error if the request to the <https://checkerproxy.net/getAllProxy> page fails.
    pub async fn scrape_archive_urls(&self) -> Result<Vec<String>> {
        let response = self
            .client
            .get("https://checkerproxy.net/getAllProxy")
            .send()
            .await
            .context("Failed to get archive URLs")?;

        let html = response.text().await?;
        let parser = Html::parse_document(&html);
        let selector = Selector::parse("li > a").unwrap();

        let mut archive_urls = Vec::new();

        for element in parser.select(&selector) {
            let uri_path = match element.value().attr("href") {
                Some(path) => path,
                None => continue,
            };

            if !uri_path.contains("/archive/") {
                continue;
            }

            let url = format!("https://checkerproxy.net/api{uri_path}");
            archive_urls.push(url);
        }

        Ok(archive_urls)
    }

    /// Scrapes the proxies from the given checkerproxy.net archive API URL.
    ///
    /// ## Parameters
    ///
    /// * `archive_url` - The archive API URL to scrape the proxies from.
    /// * `anonymous_only` - Whether to only scrape anonymous proxies.
    ///
    /// ## Returns
    ///
    /// A [`Vec`] of the scraped proxies.
    ///
    /// ## Errors
    ///
    /// Returns an error if the request to the archive API URL fails or
    /// if the JSON received is invalid.
    pub async fn scrape_proxies(
        &self,
        archive_url: String,
        anonymous_only: bool,
    ) -> Result<Vec<Proxy>> {
        let response = self
            .client
            .get(archive_url)
            .send()
            .await
            .context("Request failed")?
            .error_for_status()
            .context("Request returned an error status code")?;

        let json: Value = serde_json::from_str(&response.text().await?)?;
        let mut proxies = Vec::new();

        for proxy_dict in json.as_array().context("Invalid JSON received")? {
            if anonymous_only {
                let kind = match proxy_dict.get("kind") {
                    Some(value) => match value.as_u64() {
                        Some(value) => value,
                        None => continue,
                    },
                    None => continue,
                };

                if kind != 2 {
                    continue;
                }
            }

            let host = match proxy_dict.get("addr") {
                Some(value) => match value.as_str() {
                    Some(str_value) => str_value.to_string(),
                    None => continue,
                },
                None => continue,
            };

            let proxy = match proxy_dict.get("type") {
                Some(value) => match value.as_u64() {
                    Some(1) => Proxy::Http(host),
                    Some(2) => Proxy::Http(host),
                    Some(4) => Proxy::Socks5(host),
                    _ => continue,
                },
                None => continue,
            };

            proxies.push(proxy);
        }

        Ok(proxies)
    }
}

impl Default for ProxyScraper {
    /// Returns a new [`ProxyScraper`] instance with a 30 second timeout.
    fn default() -> Self {
        Self::new(
            reqwest::Client::builder()
                .timeout(Duration::from_secs(30))
                .build()
                .unwrap(),
        )
    }
}

/// Used to check a collection of proxies.
///
/// ## Fields
///
/// * `semaphore` - The semaphore used to limit the number of concurrent requests.
/// * `progress_bar` - The progress bar used to display the progress of the proxy checks.
#[derive(Debug)]
pub struct ProxyChecker {
    semaphore: Arc<Semaphore>,
    progress_bar: ProgressBar,
}

impl ProxyChecker {
    /// Returns a new [`ProxyChecker`] instance.
    ///
    /// ## Parameters
    ///
    /// * `semaphore` - The semaphore used to limit the number of concurrent requests.
    /// * `progress_bar` - The progress bar used to display the progress of the proxy checks.
    pub fn new(semaphore: Arc<Semaphore>, progress_bar: ProgressBar) -> Self {
        Self {
            semaphore,
            progress_bar,
        }
    }

    /// Checks if the given proxy is working by making an HTTP request to the given URL.
    ///
    /// ## Parameters
    ///
    /// * `proxy` - The proxy to check.
    /// * `url` - The URL that the proxy will be checked against.
    /// * `timeout` - The request timeout in seconds.
    ///
    /// ## Returns
    ///
    /// The proxy if the check succeeds.
    ///
    /// ## Errors
    ///
    /// An error is returned if the [`reqwest::Client`] cannot be built, if the request fails,
    /// or if the HTTP response is an error status code.
    async fn check_proxy(proxy: Proxy, url: String, timeout: u64) -> Result<Proxy> {
        let client = reqwest::Client::builder()
            .proxy(reqwest::Proxy::all(proxy.url())?)
            .timeout(Duration::from_secs(timeout))
            .build()?;

        client
            .get(url)
            .send()
            .await
            .context("Request failed")?
            .error_for_status()
            .context("Request returned an error status code")?;

        Ok(proxy)
    }

    /// Checks if the given proxies are working by making an HTTP request to the given URL.
    ///
    /// ## Parameters
    ///
    /// * `proxies` - A set of proxies to check.
    /// * `url` - The URL that the proxies will be checked against.
    /// * `timeout` - The request timeout in seconds.
    ///
    /// ## Returns
    ///
    /// A [`Vec`] of the working proxies.
    ///
    /// ## Errors
    ///
    /// An error is returned if the semaphore has been closed.
    pub async fn check_proxies(
        &self,
        proxies: HashSet<Proxy>,
        url: String,
        timeout: u64,
    ) -> Result<Vec<Proxy>> {
        let mut tasks = Vec::new();

        for proxy in proxies {
            let semaphore = self.semaphore.clone();
            let progress_bar = self.progress_bar.clone();
            let url = url.clone();

            let task = tokio::spawn(async move {
                let _permit = semaphore.acquire().await?;
                let result = Self::check_proxy(proxy, url, timeout).await;
                progress_bar.inc(1);
                result
            });

            tasks.push(task);
        }

        let results = future::try_join_all(tasks).await?;
        let mut working_proxies = Vec::new();

        for result in results {
            match result {
                Ok(proxy) => working_proxies.push(proxy),
                Err(err) => match err.downcast_ref::<AcquireError>() {
                    Some(_) => bail!("Semaphore has been closed"),
                    None => continue,
                },
            }
        }

        Ok(working_proxies)
    }
}