Skip to content

Commit a86671b

Browse files
committed
don't guess check_website type
1 parent 0ce2f1a commit a86671b

File tree

3 files changed

+26
-149
lines changed

3 files changed

+26
-149
lines changed

src/config.rs

Lines changed: 7 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -5,125 +5,25 @@ use std::{
55

66
use color_eyre::eyre::{OptionExt as _, WrapErr as _};
77

8-
use crate::{
9-
parsers::parse_ipv4,
10-
proxy::ProxyType,
11-
raw_config::RawConfig,
12-
utils::{is_docker, pretty_error},
13-
};
8+
use crate::{proxy::ProxyType, raw_config::RawConfig, utils::is_docker};
149

1510
pub const APP_DIRECTORY_NAME: &str = "proxy_scraper_checker";
1611
pub const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
1712
AppleWebKit/537.36 (KHTML, like Gecko) \
1813
Chrome/135.0.0.0 Safari/537.36";
1914

20-
#[derive(Clone)]
21-
pub enum CheckWebsiteType {
22-
Unknown,
23-
PlainIp,
24-
HttpbinIp,
25-
}
26-
2715
#[derive(serde::Deserialize)]
2816
pub struct HttpbinResponse {
2917
pub origin: String,
3018
}
3119

32-
impl CheckWebsiteType {
33-
pub async fn guess(
34-
check_website: &str,
35-
http_client: reqwest::Client,
36-
) -> Self {
37-
if check_website.is_empty() {
38-
return Self::Unknown;
39-
}
40-
41-
let response = match http_client.get(check_website).send().await {
42-
Ok(resp) => resp,
43-
Err(err) => {
44-
log::error!(
45-
"Failed to open check_website without proxy, it will be \
46-
impossible to determine anonymity and geolocation of \
47-
proxies: {err}",
48-
);
49-
return Self::Unknown;
50-
}
51-
};
52-
53-
let response = match response.error_for_status() {
54-
Ok(response) => response,
55-
Err(err) => {
56-
if let Some(status) = err.status() {
57-
log::error!(
58-
"check_website returned error HTTP status code: \
59-
{status}"
60-
);
61-
} else {
62-
log::error!(
63-
"check_website returned error HTTP status code"
64-
);
65-
}
66-
return Self::Unknown;
67-
}
68-
};
69-
70-
let body = match response.text().await {
71-
Ok(text) => text,
72-
Err(err) => {
73-
log::error!("Failed to decode check_website response: {err}");
74-
return Self::Unknown;
75-
}
76-
};
77-
78-
if let Ok(httpbin) = serde_json::from_str::<HttpbinResponse>(&body) {
79-
match parse_ipv4(&httpbin.origin) {
80-
Ok(Some(_)) => {
81-
return Self::HttpbinIp;
82-
}
83-
Ok(None) => {
84-
log::error!("Failed to parse ipv4 from httpbin response");
85-
}
86-
Err(e) => {
87-
log::error!(
88-
"Failed to parse ipv4 from httpbin response: {}",
89-
pretty_error(&e)
90-
);
91-
}
92-
}
93-
} else {
94-
match parse_ipv4(&body) {
95-
Ok(Some(_)) => {
96-
return Self::PlainIp;
97-
}
98-
Ok(None) => {}
99-
Err(e) => {
100-
log::error!(
101-
"Failed to parse ipv4 from response: {}",
102-
pretty_error(&e)
103-
);
104-
}
105-
}
106-
}
107-
108-
Self::Unknown
109-
}
110-
111-
pub const fn supports_geolocation(&self) -> bool {
112-
match self {
113-
Self::Unknown => false,
114-
Self::PlainIp | Self::HttpbinIp => true,
115-
}
116-
}
117-
}
118-
11920
#[expect(clippy::struct_excessive_bools)]
12021
pub struct Config {
12122
pub timeout: tokio::time::Duration,
12223
pub source_timeout: tokio::time::Duration,
12324
pub proxies_per_source_limit: usize,
12425
pub max_concurrent_checks: usize,
12526
pub check_website: String,
126-
pub check_website_type: CheckWebsiteType,
12727
pub sort_by_speed: bool,
12828
pub enable_geolocation: bool,
12929
pub debug: bool,
@@ -157,23 +57,17 @@ async fn get_output_path(
15757
impl Config {
15858
pub async fn from_raw_config(
15959
raw_config: RawConfig,
160-
http_client: reqwest::Client,
16160
) -> color_eyre::Result<Self> {
162-
let (check_website_type, output_path) = tokio::try_join!(
163-
async {
164-
Ok(CheckWebsiteType::guess(
165-
&raw_config.check_website,
166-
http_client,
167-
)
168-
.await)
169-
},
170-
get_output_path(&raw_config)
171-
)?;
61+
let output_path = get_output_path(&raw_config).await?;
17262

17363
let max_concurrent_checks =
17464
match rlimit::increase_nofile_limit(u64::MAX) {
17565
Ok(lim) => {
66+
#[cfg(target_pointer_width = "32")]
67+
let lim = cast::usize(lim).unwrap_or(usize::MAX);
68+
#[cfg(not(target_pointer_width = "32"))]
17669
let lim = cast::usize(lim);
70+
17771
if raw_config.max_concurrent_checks > lim {
17872
log::warn!(
17973
"max_concurrent_checks config value is too high \
@@ -196,10 +90,8 @@ impl Config {
19690
proxies_per_source_limit: raw_config.proxies_per_source_limit,
19791
max_concurrent_checks,
19892
check_website: raw_config.check_website,
199-
check_website_type: check_website_type.clone(),
20093
sort_by_speed: raw_config.sort_by_speed,
201-
enable_geolocation: raw_config.enable_geolocation
202-
&& check_website_type.supports_geolocation(),
94+
enable_geolocation: raw_config.enable_geolocation,
20395
debug: raw_config.debug,
20496
output_path,
20597
output_json: raw_config.output.json,

src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,13 @@ async fn main() -> color_eyre::Result<()> {
6868
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
6969
let ui_task = tokio::task::spawn(ui_impl.run(tx.clone(), rx));
7070

71-
let http_client = create_reqwest_client()
72-
.wrap_err("failed to create reqwest HTTP client")?;
73-
7471
let raw_config_path = raw_config::get_config_path();
7572
let raw_config = raw_config::read_config(&raw_config_path)
7673
.await
7774
.wrap_err_with(move || format!("failed to read {raw_config_path}"))?;
7875

7976
let config = Arc::new(
80-
config::Config::from_raw_config(raw_config, http_client.clone())
77+
config::Config::from_raw_config(raw_config)
8178
.await
8279
.wrap_err("failed to create Config from RawConfig")?,
8380
);
@@ -86,6 +83,9 @@ async fn main() -> color_eyre::Result<()> {
8683
ui::UIImpl::set_log_level(log::LevelFilter::Debug);
8784
}
8885

86+
let http_client = create_reqwest_client()
87+
.wrap_err("failed to create reqwest HTTP client")?;
88+
8989
let maybe_geodb_task = config.enable_geolocation.then(|| {
9090
let http_client = http_client.clone();
9191
#[cfg(feature = "tui")]

src/proxy.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::{fmt, fmt::Write as _, sync::Arc};
22

3-
use color_eyre::eyre::{OptionExt as _, WrapErr as _, eyre};
3+
use color_eyre::eyre::{WrapErr as _, eyre};
44

55
use crate::{
6-
config::{CheckWebsiteType, Config, HttpbinResponse, USER_AGENT},
6+
config::{Config, HttpbinResponse, USER_AGENT},
77
parsers::parse_ipv4,
88
};
99

@@ -109,35 +109,20 @@ impl Proxy {
109109
.wrap_err("Got error HTTP status code when checking proxy")?;
110110
drop(client);
111111
self.timeout = Some(start.elapsed());
112-
self.exit_ip = match config.check_website_type {
113-
CheckWebsiteType::HttpbinIp => {
114-
let httpbin = response
115-
.json::<HttpbinResponse>()
116-
.await
117-
.wrap_err("failed to parse response as HttpBin")?;
118-
Some(
119-
parse_ipv4(&httpbin.origin)
120-
.wrap_err("failed to parse ipv4 from httpbin response")?
121-
.ok_or_eyre(
122-
"failed to parse ipv4 from httpbin response",
123-
)?,
124-
)
125-
}
126-
CheckWebsiteType::PlainIp => {
127-
let text = response
128-
.text()
129-
.await
130-
.wrap_err("failed to decode response text")?;
131-
Some(
132-
parse_ipv4(&text)
133-
.wrap_err("failed to parse ipv4 from response text")?
134-
.ok_or_eyre(
135-
"failed to parse ipv4 from response text",
136-
)?,
137-
)
112+
self.exit_ip = response.text().await.map_or(None, move |text| {
113+
if let Ok(httpbin) = serde_json::from_str::<HttpbinResponse>(&text)
114+
{
115+
match parse_ipv4(&httpbin.origin) {
116+
Ok(Some(ipv4)) => Some(ipv4),
117+
Ok(None) | Err(_) => None,
118+
}
119+
} else {
120+
match parse_ipv4(&text) {
121+
Ok(Some(ipv4)) => Some(ipv4),
122+
Ok(None) | Err(_) => None,
123+
}
138124
}
139-
CheckWebsiteType::Unknown => None,
140-
};
125+
});
141126
Ok(())
142127
}
143128

0 commit comments

Comments
 (0)