reorganize code

This commit is contained in:
monosans
2025-07-24 10:42:23 +03:00
parent c77b583148
commit ba24577365
4 changed files with 38 additions and 33 deletions
+18 -7
View File
@@ -1,16 +1,14 @@
use std::{
collections::{HashMap, HashSet, hash_map},
path::PathBuf,
path::{Path, PathBuf},
sync::Arc,
};
use color_eyre::eyre::{OptionExt as _, WrapErr as _};
use crate::{proxy::ProxyType, raw_config::RawConfig, utils::is_docker};
use crate::{proxy::ProxyType, raw_config, utils::is_docker};
pub const APP_DIRECTORY_NAME: &str = "proxy_scraper_checker";
pub const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/138.0.0.0 Safari/537.36";
#[derive(serde::Deserialize)]
pub struct HttpbinResponse {
@@ -54,7 +52,7 @@ pub struct Config {
}
async fn get_output_path(
raw_config: &RawConfig,
raw_config: &raw_config::RawConfig,
) -> color_eyre::Result<PathBuf> {
let output_path = if is_docker().await {
let mut path = tokio::task::spawn_blocking(dirs::data_local_dir)
@@ -94,7 +92,7 @@ impl Config {
}
pub async fn from_raw_config(
raw_config: RawConfig,
raw_config: raw_config::RawConfig,
) -> color_eyre::Result<Self> {
let output_path = get_output_path(&raw_config).await?;
@@ -158,3 +156,16 @@ impl Config {
})
}
}
pub async fn load_config() -> color_eyre::Result<Arc<Config>> {
let raw_config_path = raw_config::get_config_path();
let raw_config = raw_config::read_config(Path::new(&raw_config_path))
.await
.wrap_err_with(move || format!("failed to read {raw_config_path}"))?;
let config = Config::from_raw_config(raw_config)
.await
.wrap_err("failed to create Config from RawConfig")?;
Ok(Arc::new(config))
}
+15
View File
@@ -2,6 +2,12 @@ use std::time::{Duration, SystemTime};
use color_eyre::Result;
pub const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/138.0.0.0 Safari/537.36";
const TIMEOUT: Duration = tokio::time::Duration::from_secs(60);
const CONNECT_TIMEOUT: Duration = tokio::time::Duration::from_secs(5);
const DEFAULT_MAX_RETRIES: u32 = 2;
const INITIAL_RETRY_DELAY: Duration = Duration::from_millis(500);
const MAX_RETRY_DELAY: Duration = Duration::from_secs(8);
@@ -119,3 +125,12 @@ pub async fn fetch_text(
}
}
}
pub fn create_reqwest_client() -> reqwest::Result<reqwest::Client> {
reqwest::Client::builder()
.user_agent(USER_AGENT)
.timeout(TIMEOUT)
.connect_timeout(CONNECT_TIMEOUT)
.use_rustls_tls()
.build()
}
+3 -25
View File
@@ -51,26 +51,13 @@ mod scraper;
mod tui;
mod utils;
use std::{path::Path, sync::Arc};
use std::sync::Arc;
use color_eyre::eyre::WrapErr as _;
use tracing_subscriber::{
layer::SubscriberExt as _, util::SubscriberInitExt as _,
};
async fn load_config() -> color_eyre::Result<Arc<config::Config>> {
let raw_config_path = raw_config::get_config_path();
let raw_config = raw_config::read_config(Path::new(&raw_config_path))
.await
.wrap_err_with(move || format!("failed to read {raw_config_path}"))?;
let config = config::Config::from_raw_config(raw_config)
.await
.wrap_err("failed to create Config from RawConfig")?;
Ok(Arc::new(config))
}
fn create_logging_filter(
config: &config::Config,
) -> tracing_subscriber::filter::Targets {
@@ -96,15 +83,6 @@ fn create_logging_filter(
}
}
fn create_reqwest_client() -> reqwest::Result<reqwest::Client> {
reqwest::Client::builder()
.user_agent(config::USER_AGENT)
.timeout(tokio::time::Duration::from_secs(60))
.connect_timeout(tokio::time::Duration::from_secs(5))
.use_rustls_tls()
.build()
}
async fn download_output_dependencies(
config: &config::Config,
http_client: reqwest::Client,
@@ -161,7 +139,7 @@ async fn main_task(
event::Event,
>,
) -> color_eyre::Result<()> {
let http_client = create_reqwest_client()
let http_client = http::create_reqwest_client()
.wrap_err("failed to create reqwest HTTP client")?;
let ((), mut proxies) = tokio::try_join!(
@@ -314,7 +292,7 @@ async fn run_without_tui(
async fn main() -> color_eyre::Result<()> {
color_eyre::install().wrap_err("failed to install color_eyre hooks")?;
let config = load_config().await?;
let config = config::load_config().await?;
let logging_filter = create_logging_filter(&config);
#[cfg(feature = "tui")]
+2 -1
View File
@@ -6,7 +6,8 @@ use std::{
use color_eyre::eyre::{WrapErr as _, eyre};
use crate::{
config::{Config, HttpbinResponse, USER_AGENT},
config::{Config, HttpbinResponse},
http::USER_AGENT,
parsers::parse_ipv4,
};