From ba24577365db6a3cf6a7fb9360cb0c5eac4ae939 Mon Sep 17 00:00:00 2001 From: monosans Date: Thu, 24 Jul 2025 10:42:23 +0300 Subject: [PATCH] reorganize code --- src/config.rs | 25 ++++++++++++++++++------- src/http.rs | 15 +++++++++++++++ src/main.rs | 28 +++------------------------- src/proxy.rs | 3 ++- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/config.rs b/src/config.rs index 664909b..f6149f8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 { 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 { let output_path = get_output_path(&raw_config).await?; @@ -158,3 +156,16 @@ impl Config { }) } } + +pub async fn load_config() -> color_eyre::Result> { + 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)) +} diff --git a/src/http.rs b/src/http.rs index d2daf07..a06032a 100644 --- a/src/http.rs +++ b/src/http.rs @@ -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::builder() + .user_agent(USER_AGENT) + .timeout(TIMEOUT) + .connect_timeout(CONNECT_TIMEOUT) + .use_rustls_tls() + .build() +} diff --git a/src/main.rs b/src/main.rs index 12da234..bb8b443 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { - 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::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")] diff --git a/src/proxy.rs b/src/proxy.rs index a242104..c4672b3 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -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, };