Skip to content

Commit

Permalink
Implemented anyhow for better error handling
Browse files Browse the repository at this point in the history
- Documentation improvements
  • Loading branch information
Xewdy444 committed Jan 7, 2024
1 parent ec9a9e8 commit 3f7403f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 71 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ strip = true
lto = "fat"

[dependencies]
anyhow = "1.0.79"
clap = { version = "4.4.13", features = ["derive"] }
futures = "0.3.30"
humantime = "2.1.0"
Expand Down
65 changes: 38 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
mod proxy_utilities;

use anyhow::{bail, Context, Result};
use clap::{command, Parser};
use futures::future;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use proxy_utilities::{Proxies, Proxy, ProxyChecker, ProxyScraper, ProxyType};
#[cfg(not(windows))]
use rlimit::Resource;
use std::fs::{self, File};
use std::io::{self, BufWriter, Write};
use std::io::{BufWriter, Write};
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -25,18 +26,14 @@ struct CheckTaskResult {
}

impl CheckTaskResult {
/// Creates a new [`CheckTaskResult`] instance.
/// Returns a new [`CheckTaskResult`] instance.
///
/// # Arguments
/// # Parameters
///
/// * `proxy_type` - The type of proxies that were checked.
/// * `working_proxies` - The working proxies.
/// * `proxies_checked` - The number of proxies that were checked.
/// * `check_duration` - The duration of the check task.
///
/// # Returns
///
/// The new [`CheckTaskResult`] instance.
fn new(
proxy_type: ProxyType,
working_proxies: Vec<Proxy>,
Expand All @@ -54,56 +51,65 @@ impl CheckTaskResult {

/// Increases the open file limit if necessary on Windows.
///
/// # Arguments
/// # Parameters
///
/// * `limit` - The limit to set.
///
/// # Returns
///
/// A [`Result`] containing the result of the operation.
///
/// # Errors
///
/// Returns an error if the open file limit is greater than 8192
/// or if the limit could not be set.
#[cfg(windows)]
fn set_open_file_limit(limit: u64) -> Result<(), io::Error> {
fn set_open_file_limit(limit: u64) -> Result<()> {
let open_file_limit = rlimit::getmaxstdio() as u64;

if limit <= open_file_limit {
return Ok(());
}

if limit > 8192 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"The open file limit cannot be greater than 8192 on Windows",
));
bail!("The open file limit cannot be greater than 8192 on Windows");
}

rlimit::setmaxstdio(limit as u32)?;
rlimit::setmaxstdio(limit as u32).context("Failed to set open file limit")?;
Ok(())
}

/// Increases the open file limit if necessary on non-Windows platforms.
///
/// # Arguments
/// # Parameters
///
/// * `limit` - The limit to set.
///
/// # Returns
///
/// A [`Result`] containing the result of the operation.
///
/// # Errors
///
/// Returns an error if the limit could not be retrieved or set.
#[cfg(not(windows))]
fn set_open_file_limit(limit: u64) -> Result<(), io::Error> {
let open_file_limit = rlimit::getrlimit(Resource::NOFILE)?;
fn set_open_file_limit(limit: u64) -> Result<()> {
let open_file_limit =
rlimit::getrlimit(Resource::NOFILE).context("Failed to get open file limit")?;

if limit <= open_file_limit.0 {
return Ok(());
}

rlimit::setrlimit(Resource::NOFILE, limit, open_file_limit.1)?;
rlimit::setrlimit(Resource::NOFILE, limit, open_file_limit.1)
.context("Failed to set open file limit")?;

Ok(())
}

/// Writes the proxies to a file.
///
/// # Arguments
/// # Parameters
///
/// * `proxy_type` - The type of proxies.
/// * `proxies` - The proxies to write to a file.
Expand All @@ -112,28 +118,33 @@ fn set_open_file_limit(limit: u64) -> Result<(), io::Error> {
/// # Returns
///
/// A [`Result`] containing the result of the operation.
///
/// # Errors
///
/// Returns an error if the proxies folder could not be created,
/// if the file could not be created, or if the proxies could not be written to the file.
fn write_proxies_to_file(
proxy_type: ProxyType,
proxies: &[Proxy],
proxies_folder: &Path,
) -> Result<(), io::Error> {
) -> Result<()> {
if !proxies_folder.exists() {
fs::create_dir_all(proxies_folder)?;
fs::create_dir_all(proxies_folder).context("Failed to create proxies folder")?;
}

let file_name = match proxy_type {
ProxyType::Http => "http.txt",
ProxyType::Socks5 => "socks5.txt",
};

let file = File::create(proxies_folder.join(file_name))?;
let file = File::create(proxies_folder.join(file_name)).context("Failed to create file")?;
let mut writer = BufWriter::new(file);

for proxy in proxies {
writeln!(writer, "{proxy}")?;
writeln!(writer, "{proxy}").context("Failed to write proxy")?;
}

writer.flush()?;
writer.flush().context("Failed to flush writer")?;
Ok(())
}

Expand Down Expand Up @@ -176,9 +187,9 @@ async fn main() {
let args = Args::parse();
let proxy_scraper = ProxyScraper::default();

set_open_file_limit(args.tasks).expect(
"Failed to set open file limit; decreasing the number of tasks should fix this issue",
);
set_open_file_limit(args.tasks)
.context("Decreasing the number of tasks should fix this issue")
.unwrap();

let archive_urls = proxy_scraper
.scrape_archive_urls()
Expand Down
Loading

0 comments on commit 3f7403f

Please sign in to comment.