From 3856c3ce3b997b4e36a6ff5419acb8dae6109857 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 13 Aug 2022 17:47:01 +0200 Subject: [PATCH] Refactor --- src/cli.rs | 2 +- src/main.rs | 2 +- src/scanner.rs | 32 ++++++++++++++++---------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 6348c71..a54b57f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -13,7 +13,7 @@ pub struct Args { long = "threads", default_value_t = 1 )] - pub threads: u8, + pub threads: u64, #[clap( help = "A file containing ignored IPv4 addresses (seperated by linebreaks).", diff --git a/src/main.rs b/src/main.rs index b8b04a9..2c37015 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use scanner::start_scan; fn main() { let args = Args::parse(); - let results = start_scan(args.from, args.to, args.port, args.threads as u32, None); + let results = start_scan(args.from, args.to, args.port, args.threads, None); for result in results { println!("{:?}", result); diff --git a/src/scanner.rs b/src/scanner.rs index 4fc78e3..3b4d82c 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -20,12 +20,12 @@ fn tcp_scan(mut target: IPv4, target_port: u16) -> bool { } fn create_scan_thread( - thread_id: u32, + thread_id: u64, ip_range: IPv4Range, target_port: u16, ) -> JoinHandle> { thread::spawn(move || { - info!("Starting thread worker #{}", thread_id); + info!("Creating thread worker #{}", thread_id); let mut results: Vec<(u32, bool)> = Vec::new(); // do the scan thing @@ -40,8 +40,8 @@ fn create_scan_thread( } fn create_scan_worker( - thread_id: u32, - ips_per_thread: u32, + thread_id: u64, + ips_per_thread: u64, target_port: u16, ignorelist: Vec, ) -> JoinHandle> { @@ -49,7 +49,7 @@ fn create_scan_worker( (thread_id * ips_per_thread), ((thread_id + 1) * ips_per_thread), ); - let range = IPv4Range::new(f, t, ignorelist); + let range = IPv4Range::new(f as u32, t as u32, ignorelist); create_scan_thread(thread_id, range, target_port) } @@ -57,13 +57,13 @@ fn get_scan_workers( from: u32, to: u32, target_port: u16, - num_threads: u32, + num_threads: u64, ignorelist: Option>, ) -> Vec>> { - println!("Starting wwmap..."); - - let ips_per_thread = (((to - from) as f32) / num_threads as f32) as u32; - let ips_left = (to - from) - (num_threads * ips_per_thread); // how many ips we have left after the first threads + let ips_per_thread: u64 = (((to - from) as f32) / num_threads as f32) as u64; + println!("{} : {}", num_threads, ips_per_thread); + let ips_left: u64 = (to - from) as u64 - (num_threads * ips_per_thread) as u64; // how many ips we have left after the first threads + println!("{} | {}", to - from, 2); // container for all of our threads let mut threads: Vec>> = Vec::new(); @@ -83,7 +83,7 @@ fn get_scan_workers( warn!("Number of IPv4 addresses is not divisible by the amount of threads! Creating extra thread..."); let id_ignorelist = ignorelist.clone().unwrap_or_default(); let worker = create_scan_worker( - threads.len() as u32 + 1, + threads.len() as u64 + 1, ips_per_thread, target_port, id_ignorelist, @@ -112,21 +112,21 @@ pub fn start_scan( from: u32, to: u32, target_port: u16, - num_threads: u32, + num_threads: u64, ignorelist: Option>, ) -> Vec { info!("Starting wwmap scan..."); // Get the workers - debug!("Getting scan workers..."); + println!("Getting scan workers..."); let scan_workers = get_scan_workers(from, to, target_port, num_threads, ignorelist); - debug!("Loaded {} scan worker(s).", scan_workers.len()); + println!("Loaded {} scan worker(s).", scan_workers.len()); let mut results: Vec = Vec::new(); // Run all the workers for worker in scan_workers { - debug!("* Running worker: {:?}", worker); + println!("* Running worker: {:?}", worker); let result_tuples = match worker.join() { Ok(r) => r, Err(e) => panic!("{:?}", e), @@ -137,7 +137,7 @@ pub fn start_scan( .map(|res| ScanResult::from_tuple(*res)) .collect(); - debug!("\t* Worker got results: {:?}", result_tuples); + println!("\t* Worker got results: {:?}", result_tuples); results.append(&mut worker_results); }