Refactor & stuff

main
E. Almqvist 2 years ago
parent 6978f7d9c0
commit 031e9ed491
  1. 46
      src/ipv4.rs
  2. 86
      src/scanner.rs

@ -4,23 +4,11 @@ use convert_base::Convert;
use std::convert::TryInto; use std::convert::TryInto;
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, SocketAddr};
/*
Algorithm: O(n)
let i = 0 .. u32:max_value()
# Convert each i to base 256 and we get all the ipv4 addresses
# This is waaaay better than a stupid loop
*/
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct IPv4 { pub struct IPv4 {
pub id: u64, pub id: u64,
pub ip: [u8; 4], pub ip: [u8; 4],
pub ignore: bool, pub ignore: bool,
// pub enum __Generator {
// Start(&'static)
// }
} }
impl IPv4 { impl IPv4 {
@ -66,6 +54,40 @@ impl IPv4 {
} }
} }
pub struct IPv4_Range {
pub id_start: u32,
pub id_end: u32,
pub id_ignore: Vec<u32>,
}
impl IPv4_Range {
pub fn new(from: u32, to: u32, id_ignore: Option<Vec<u32>>) -> Self {
Self {
id_start: from,
id_end: to,
id_ignore: id_ignore.unwrap_or(Vec::new()),
}
}
}
impl Iterator for IPv4_Range {
type Item = u32;
fn next(&mut self) -> Option<u32> {
if self.id_start == self.id_end {
None
} else {
let res = Some(self.id_start);
self.id_start += 1;
if self.id_ignore.contains(&res.unwrap()) {
return self.next();
}
res
}
}
}
pub fn get_all(ignorelist: Option<Vec<u64>>) -> Result<Vec<IPv4>> { pub fn get_all(ignorelist: Option<Vec<u64>>) -> Result<Vec<IPv4>> {
// Ignore those that we know // Ignore those that we know
let ignorelist = ignorelist.unwrap_or(Vec::new()); let ignorelist = ignorelist.unwrap_or(Vec::new());

@ -17,62 +17,56 @@ fn tcp_scan(mut target: ipv4::IPv4, target_port: u16) -> bool {
// false // false
} }
fn create_scan_thread( // fn create_scan_thread(
ip_list: Vec<ipv4::IPv4>, // ip_list: Vec<ipv4::IPv4>,
thread_id: u32, // thread_id: u32,
ips_per_thread: u32, // ips_per_thread: u32,
target_port: u16, // target_port: u16,
) -> JoinHandle<Vec<bool>> { // ) -> JoinHandle<Vec<bool>> {
thread::spawn(move || { // thread::spawn(move || {
info!("Starting thread worker #{}", thread_id); // info!("Starting thread worker #{}", thread_id);
let mut results: Vec<bool> = Vec::new(); // let mut results: Vec<bool> = Vec::new();
//
// do the scan thing // // do the scan thing
for i in 0..ips_per_thread { // for i in 0..ips_per_thread {
let id = (thread_id * ips_per_thread) + i; // let id = (thread_id * ips_per_thread) + i;
let ref target = ip_list[id as usize]; // let ref target = ip_list[id as usize];
let result = tcp_scan(*target, target_port); // let result = tcp_scan(*target, target_port);
results.push(result); // results.push(result);
} // }
//
results // results
}) // })
} // }
pub fn start_scan(target_port: u16, num_threads: u32, ignorelist: Option<Vec<u64>>) -> Result<()> { pub fn start_scan(
from: u32,
to: u32,
target_port: u16,
num_threads: u32,
ignorelist: Option<Vec<u32>>,
) -> Result<()> {
println!("Starting wwmap..."); println!("Starting wwmap...");
let ip_list = ipv4::get_all(ignorelist)?; //let ip_list = ipv4::get_all(ignorelist)?;
println!("Post list gen"); let ips_per_thread = (((to - from) as f32) / num_threads as f32) as u32;
let ips_per_thread = ((ip_list.len() as f32) / num_threads as f32) as u32;
let ips_left = num_threads * ips_per_thread; // how many ips we have left after the first threads let ips_left = num_threads * ips_per_thread; // how many ips we have left after the first threads
// container for all of our threads let ip_ranges: Vec<ipv4::IPv4_Range> = Vec::new();
let mut threads: Vec<JoinHandle<Vec<bool>>> = Vec::new();
let result_list: Vec<bool> = Vec::new();
// create all of our threads
for thread_id in 0..num_threads { for thread_id in 0..num_threads {
threads.push(create_scan_thread( let (f, t) = ((thread_id * ips_per_thread), ((thread_id + 1) * ips_per_thread));
ip_list.clone(), let range = ipv4::IPv4_Range::new(f, t, ignorelist);
thread_id,
ips_per_thread,
target_port,
));
}
// create the last thread to do the job ip_ranges.push(range);
if ips_left > 0 {
threads.push(create_scan_thread(
ip_list,
num_threads,
ips_left,
target_port,
));
} }
// Append all of the results into the list // container for all of our threads
let mut threads: Vec<JoinHandle<Vec<bool>>> = Vec::new();
// container for all the results
let mut result_list: Vec<bool> = Vec::new();
Ok(()) Ok(())
} }

Loading…
Cancel
Save