Files
proxy-scraper-checker/src/scraper.rs
T

164 lines
4.8 KiB
Rust
Raw Normal View History

2025-05-24 01:31:42 +03:00
use std::{collections::HashSet, sync::Arc};
2025-04-10 06:55:12 +00:00
2025-04-29 13:06:18 +03:00
use color_eyre::eyre::{OptionExt as _, WrapErr as _};
2025-04-10 06:55:12 +00:00
2025-04-28 08:24:01 +03:00
#[cfg(feature = "tui")]
use crate::event::{AppEvent, Event};
2025-04-10 06:55:12 +00:00
use crate::{
config::Config,
parsers::PROXY_REGEX,
proxy::{Proxy, ProxyType},
2025-04-29 13:06:18 +03:00
utils::{is_http_url, pretty_error},
2025-04-10 06:55:12 +00:00
};
async fn fetch_text(
2025-05-24 01:31:42 +03:00
config: &Config,
2025-04-10 06:55:12 +00:00
http_client: reqwest::Client,
source: &str,
) -> color_eyre::Result<String> {
2025-05-24 01:31:42 +03:00
Ok(if is_http_url(source) {
2025-04-10 06:55:12 +00:00
http_client
.get(source)
2025-05-24 01:31:42 +03:00
.timeout(config.scraping.timeout)
2025-04-10 06:55:12 +00:00
.send()
2025-05-24 01:31:42 +03:00
.await?
.error_for_status()?
2025-04-10 06:55:12 +00:00
.text()
2025-05-24 01:31:42 +03:00
.await?
2025-04-10 06:55:12 +00:00
} else {
tokio::fs::read_to_string(
source.strip_prefix("file://").unwrap_or(source),
)
2025-05-24 01:31:42 +03:00
.await?
})
2025-04-10 06:55:12 +00:00
}
async fn scrape_one(
config: Arc<Config>,
http_client: reqwest::Client,
proto: ProxyType,
2025-05-24 01:31:42 +03:00
proxies: Arc<tokio::sync::Mutex<HashSet<Proxy>>>,
2025-04-10 06:55:12 +00:00
source: &str,
2025-04-27 21:59:32 +03:00
#[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender<Event>,
2025-05-08 18:20:19 +03:00
) -> color_eyre::Result<()> {
2025-05-24 01:31:42 +03:00
let text_result = fetch_text(&config, http_client.clone(), source).await;
2025-04-10 06:55:12 +00:00
2025-04-27 21:59:32 +03:00
#[cfg(feature = "tui")]
2025-04-10 06:55:12 +00:00
tx.send(Event::App(AppEvent::SourceScraped(proto.clone())))?;
let text = match text_result {
Ok(text) => text,
Err(e) => {
2025-04-29 13:06:18 +03:00
log::warn!("{} | {}", source, pretty_error(&e));
2025-05-08 18:20:19 +03:00
return Ok(());
2025-04-10 06:55:12 +00:00
}
};
2025-05-14 14:52:15 +03:00
let matches =
PROXY_REGEX.captures_iter(&text).collect::<Result<Vec<_>, _>>()?;
2025-04-10 06:55:12 +00:00
if matches.is_empty() {
log::warn!("{source} | No proxies found");
2025-05-08 18:20:19 +03:00
return Ok(());
2025-04-10 06:55:12 +00:00
}
2025-05-24 01:31:42 +03:00
if config.scraping.max_proxies_per_source != 0
&& matches.len() > config.scraping.max_proxies_per_source
2025-04-10 06:55:12 +00:00
{
log::warn!(
"{} | Too many proxies ({}) - skipped",
source,
matches.len(),
);
2025-05-08 18:20:19 +03:00
return Ok(());
2025-04-10 06:55:12 +00:00
}
2025-05-08 18:20:19 +03:00
#[cfg(feature = "tui")]
2025-05-24 01:31:42 +03:00
let mut seen_protocols = HashSet::new();
let mut proxies = proxies.lock().await;
2025-04-10 06:55:12 +00:00
for capture in matches {
2025-05-24 01:31:42 +03:00
let protocol = match capture.name("protocol") {
Some(m) => m.as_str().parse()?,
None => proto.clone(),
2025-04-10 06:55:12 +00:00
};
2025-05-24 01:31:42 +03:00
if config.protocol_is_enabled(&protocol) {
#[cfg(feature = "tui")]
seen_protocols.insert(protocol.clone());
proxies.insert(Proxy {
protocol,
host: capture
.name("host")
.ok_or_eyre("failed to match \"host\" regex capture group")?
.as_str()
.to_owned(),
port: capture
.name("port")
.ok_or_eyre("failed to match \"port\" regex capture group")?
.as_str()
.parse()?,
username: capture
.name("username")
2025-05-24 16:19:17 +03:00
.map(|m| m.as_str().to_owned()),
2025-05-24 01:31:42 +03:00
password: capture
.name("password")
2025-05-24 16:19:17 +03:00
.map(|m| m.as_str().to_owned()),
2025-05-24 01:31:42 +03:00
timeout: None,
exit_ip: None,
});
}
2025-04-10 06:55:12 +00:00
}
2025-05-08 18:20:19 +03:00
#[cfg(feature = "tui")]
for proto in seen_protocols {
2025-05-24 01:31:42 +03:00
let count = proxies.iter().filter(|p| p.protocol == proto).count();
2025-05-08 18:20:19 +03:00
tx.send(Event::App(AppEvent::TotalProxies(proto, count)))?;
}
2025-05-24 01:31:42 +03:00
drop(proxies);
2025-05-08 18:20:19 +03:00
Ok(())
2025-04-10 06:55:12 +00:00
}
2025-04-24 08:57:50 +03:00
pub async fn scrape_all(
2025-04-24 13:44:02 +03:00
config: Arc<Config>,
2025-04-10 06:55:12 +00:00
http_client: reqwest::Client,
2025-04-27 21:59:32 +03:00
#[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender<Event>,
2025-05-24 01:31:42 +03:00
) -> color_eyre::Result<HashSet<Proxy>> {
let proxies = Arc::new(tokio::sync::Mutex::new(HashSet::new()));
2025-05-08 18:20:19 +03:00
2025-04-10 06:55:12 +00:00
let mut join_set = tokio::task::JoinSet::new();
2025-05-24 01:31:42 +03:00
for (proto, sources) in config.scraping.sources.clone() {
2025-04-27 21:59:32 +03:00
#[cfg(feature = "tui")]
2025-04-10 06:55:12 +00:00
tx.send(Event::App(AppEvent::SourcesTotal(
proto.clone(),
sources.len(),
)))?;
for source in sources {
2025-04-24 08:57:50 +03:00
let config = Arc::clone(&config);
2025-04-10 06:55:12 +00:00
let http_client = http_client.clone();
let proto = proto.clone();
2025-05-24 01:31:42 +03:00
let proxies = Arc::clone(&proxies);
2025-04-27 21:59:32 +03:00
#[cfg(feature = "tui")]
2025-04-10 06:55:12 +00:00
let tx = tx.clone();
join_set.spawn(async move {
2025-04-27 21:59:32 +03:00
scrape_one(
config,
http_client,
proto,
2025-05-24 01:31:42 +03:00
proxies,
2025-04-27 21:59:32 +03:00
&source,
#[cfg(feature = "tui")]
tx,
)
.await
2025-04-10 06:55:12 +00:00
});
}
}
while let Some(res) = join_set.join_next().await {
2025-05-08 18:20:19 +03:00
res.wrap_err("failed to join proxy scrape task")?
.wrap_err("proxy scrape task failed")?;
2025-04-10 06:55:12 +00:00
}
2025-05-08 18:20:19 +03:00
2025-05-24 01:31:42 +03:00
Ok(Arc::into_inner(proxies)
2025-05-08 18:20:19 +03:00
.ok_or_eyre("failed to unwrap Arc")?
.into_inner())
2025-04-10 06:55:12 +00:00
}