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

176 lines
4.8 KiB
Rust
Raw Normal View History

2025-05-14 14:52:15 +03:00
use std::{
2025-08-01 23:02:48 +03:00
hash::{Hash, Hasher},
2025-05-14 14:52:15 +03:00
str::FromStr,
2025-08-12 11:32:17 +03:00
sync::Arc,
2025-08-12 16:11:50 +03:00
time::{Duration, Instant},
2025-05-14 14:52:15 +03:00
};
2025-04-10 06:55:12 +00:00
2025-05-05 15:24:10 +03:00
use color_eyre::eyre::{WrapErr as _, eyre};
2025-04-10 06:55:12 +00:00
use crate::{
2025-07-24 10:42:23 +03:00
config::{Config, HttpbinResponse},
2025-04-10 06:55:12 +00:00
parsers::parse_ipv4,
};
#[derive(
Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize,
)]
2025-05-14 14:52:15 +03:00
#[serde(rename_all = "lowercase")]
2025-04-24 08:57:50 +03:00
pub enum ProxyType {
2025-04-10 06:55:12 +00:00
Http,
Socks4,
Socks5,
}
2025-05-14 14:52:15 +03:00
impl FromStr for ProxyType {
type Err = crate::Error;
2025-04-10 06:55:12 +00:00
2025-05-14 14:52:15 +03:00
fn from_str(s: &str) -> Result<Self, Self::Err> {
2025-08-15 12:06:00 +03:00
if s.eq_ignore_ascii_case("http") || s.eq_ignore_ascii_case("https") {
Ok(Self::Http)
} else if s.eq_ignore_ascii_case("socks4") {
Ok(Self::Socks4)
} else if s.eq_ignore_ascii_case("socks5") {
Ok(Self::Socks5)
} else {
Err(eyre!("failed to convert {s} to ProxyType"))
2025-04-10 06:55:12 +00:00
}
}
}
2025-08-15 12:00:31 +03:00
impl ProxyType {
pub const fn as_str(self) -> &'static str {
match self {
Self::Http => "http",
Self::Socks4 => "socks4",
Self::Socks5 => "socks5",
2025-08-15 12:00:31 +03:00
}
2025-04-10 06:55:12 +00:00
}
}
#[derive(Eq)]
2025-04-24 08:57:50 +03:00
pub struct Proxy {
pub protocol: ProxyType,
pub host: String,
pub port: u16,
pub username: Option<String>,
pub password: Option<String>,
2025-08-12 16:11:50 +03:00
pub timeout: Option<Duration>,
2025-04-24 08:57:50 +03:00
pub exit_ip: Option<String>,
2025-04-10 06:55:12 +00:00
}
2025-05-05 11:02:09 +03:00
impl TryFrom<&mut Proxy> for reqwest::Proxy {
type Error = crate::Error;
2025-05-05 11:02:09 +03:00
fn try_from(value: &mut Proxy) -> Result<Self, Self::Error> {
let proxy = Self::all(format!(
2025-04-10 06:55:12 +00:00
"{}://{}:{}",
2025-08-15 12:00:31 +03:00
value.protocol.as_str(),
value.host,
value.port
2025-04-10 06:55:12 +00:00
))
.wrap_err("failed to create reqwest::Proxy")?;
2025-05-05 11:02:09 +03:00
2025-04-10 06:55:12 +00:00
if let (Some(username), Some(password)) =
2025-05-05 11:02:09 +03:00
(value.username.as_ref(), value.password.as_ref())
2025-04-10 06:55:12 +00:00
{
2025-05-05 11:02:09 +03:00
Ok(proxy.basic_auth(username, password))
} else {
Ok(proxy)
2025-04-10 06:55:12 +00:00
}
2025-05-05 11:02:09 +03:00
}
}
impl Proxy {
pub const fn is_checked(&self) -> bool {
self.timeout.is_some()
}
2025-08-12 11:32:17 +03:00
pub async fn check<R: reqwest::dns::Resolve + 'static>(
&mut self,
config: &Config,
dns_resolver: Arc<R>,
) -> crate::Result<()> {
if let Some(check_url) = &config.checking.check_url {
2025-08-12 16:11:50 +03:00
let builder = reqwest::ClientBuilder::new()
.user_agent(&config.checking.user_agent)
.proxy(self.try_into()?)
.timeout(config.checking.timeout)
.connect_timeout(config.checking.connect_timeout)
2025-08-12 16:11:50 +03:00
.pool_max_idle_per_host(0)
.http1_only()
.tcp_keepalive(None)
.tcp_keepalive_interval(Duration::ZERO)
.tcp_keepalive_retries(0)
.dns_resolver(dns_resolver);
#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux"
))]
let builder = builder.tcp_user_timeout(None);
let client = builder.build()?;
let start = Instant::now();
let response = client
.get(check_url.clone())
.send()
.await?
.error_for_status()?;
drop(client);
self.timeout = Some(start.elapsed());
self.exit_ip = response.text().await.map_or(None, |text| {
if let Ok(httpbin) =
serde_json::from_str::<HttpbinResponse>(&text)
{
parse_ipv4(&httpbin.origin)
} else {
parse_ipv4(&text)
}
});
}
2025-04-10 06:55:12 +00:00
Ok(())
}
2025-04-24 08:57:50 +03:00
pub fn as_str(&self, include_protocol: bool) -> String {
2025-04-10 06:55:12 +00:00
let mut s = String::new();
if include_protocol {
2025-08-15 12:00:31 +03:00
s.push_str(self.protocol.as_str());
s.push_str("://");
2025-04-10 06:55:12 +00:00
}
if let (Some(username), Some(password)) =
(&self.username, &self.password)
2025-04-10 06:55:12 +00:00
{
s.push_str(username);
s.push(':');
s.push_str(password);
s.push('@');
2025-04-10 06:55:12 +00:00
}
s.push_str(&self.host);
s.push(':');
s.push_str(&self.port.to_string());
2025-04-10 06:55:12 +00:00
s
}
}
2025-08-01 23:02:48 +03:00
#[expect(clippy::missing_trait_methods)]
impl PartialEq for Proxy {
fn eq(&self, other: &Self) -> bool {
self.protocol == other.protocol
&& self.host == other.host
&& self.port == other.port
&& self.username == other.username
&& self.password == other.password
}
}
#[expect(clippy::missing_trait_methods)]
impl Hash for Proxy {
fn hash<H: Hasher>(&self, state: &mut H) {
self.protocol.hash(state);
self.host.hash(state);
self.port.hash(state);
self.username.hash(state);
self.password.hash(state);
}
}