extern crate clap;
extern crate reqwest;
extern crate tokio;
extern crate trust_dns_resolver;
extern crate whois_rust;
extern crate url;
mod http;
mod dns;
mod whois;
use clap::Parser;
use tokio::task;
use std::sync::Arc;
use tokio::sync::Semaphore;
use std::fs::{OpenOptions, remove_file};
use std::io::Write;
use url::Url;
#[derive(Parser)]
struct Args {
#[arg(short, long)]
input_file: String,
#[arg(short, long)]
output_file: String,
#[arg(short, long, default_value = "")]
exclude: String,
#[arg(short, long, default_value_t = 10)]
concurrency: usize,
#[arg(short, long)]
verbose: bool,
}
async fn check_domain_or_url(input: String, semaphore: Arc<Semaphore>, output_file: String, exclude: String, verbose: bool) {
let permit = semaphore.acquire().await.unwrap();
if verbose {
println!("Checking: {}", input);
}
let (url, domain) = if let Ok(parsed_url) = Url::parse(&input) {
(input.clone(), parsed_url.host_str().map(|s| s.to_string()))
} else {
(format!("http://{}", input), Some(input.clone()))
};
let (http_success, redirected_to_www) = http::check_http(&url, verbose).await.unwrap_or((false, false));
let status = if http_success || redirected_to_www {
"ACTIVE"
} else {
let dns_result = if let Some(domain) = &domain {
dns::check_dns(domain, verbose).await
} else {
Ok(())
};
if dns_result.is_ok() {
let whois_result = if let Some(domain) = &domain {
whois::check_whois(domain, verbose).await
} else {
Ok(())
};
if whois_result.is_ok() {
"ACTIVE"
} else {
"INACTIVE"
}
} else {
"INACTIVE"
}
};
if status != exclude {
let file_path = format!("{}_{}.txt", output_file, status);
let mut file = OpenOptions::new().append(true).create(true).open(&file_path).unwrap();
writeln!(file, "{}", input).unwrap();
}
if verbose {
println!("{}: {}", input, status);
println!("Finished checking: {}", input);
}
drop(permit); }
fn delete_output_files(output_file: &str) {
let active_file = format!("{}_ACTIVE.txt", output_file);
let inactive_file = format!("{}_INACTIVE.txt", output_file);
if std::path::Path::new(&active_file).exists() {
remove_file(&active_file).unwrap();
}
if std::path::Path::new(&inactive_file).exists() {
remove_file(&inactive_file).unwrap();
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
delete_output_files(&args.output_file);
let contents = std::fs::read_to_string(args.input_file)?;
let inputs: Vec<String> = contents.lines().map(|s| s.to_string()).collect();
let semaphore = Arc::new(Semaphore::new(args.concurrency));
let mut handles = vec![];
for input in inputs {
let sem_clone = semaphore.clone();
let output_file = args.output_file.clone();
let exclude = args.exclude.clone();
let verbose = args.verbose;
let handle = task::spawn(async move {
check_domain_or_url(input, sem_clone, output_file, exclude, verbose).await;
});
handles.push(handle);
}
for handle in handles {
if let Err(e) = handle.await {
eprintln!("Task failed: {:?}", e);
}
}
if args.verbose {
println!("All tasks completed.");
}
Ok(())
}