Formatting & refactor & stats

main
E. Almqvist 2 years ago
parent c8bbbfbe8b
commit 066b74cb37
  1. 9
      src/ipv4.rs
  2. 20
      src/scanner.rs

@ -43,8 +43,7 @@ impl IPv4 {
Self { id, ip } Self { id, ip }
} }
pub fn to_ipaddr(self: &mut Self) -> Result<IpAddr> { fn to_ipaddr(self: &mut Self) -> Result<IpAddr> {
// TODO: remove unneeded Result returns
if let [a, b, c, d] = self.ip[0..4] { if let [a, b, c, d] = self.ip[0..4] {
Ok(IpAddr::V4(Ipv4Addr::new(a, b, c, d))) Ok(IpAddr::V4(Ipv4Addr::new(a, b, c, d)))
} else { } else {
@ -52,9 +51,9 @@ impl IPv4 {
} }
} }
pub fn to_socketaddr(self: &mut Self, port: u16) -> Result<SocketAddr> { pub fn to_socketaddr(self: &mut Self, port: u16) -> SocketAddr {
let ip_addr = self.to_ipaddr()?; let ip_addr = self.to_ipaddr().unwrap_or_else(|err| panic!("{}", err));
Ok(SocketAddr::new(ip_addr, port)) SocketAddr::new(ip_addr, port)
} }
} }

@ -5,14 +5,18 @@ use std::net::TcpStream;
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::{panic, thread}; use std::{panic, thread};
static mut TCP_SCANS_ISSUED: u64 = 0;
fn tcp_scan(mut target: IPv4, target_port: u16) -> bool { fn tcp_scan(mut target: IPv4, target_port: u16) -> bool {
debug!("Starting scan on {:?}", target); debug!("Starting scan on {:?}", target);
let dest = target let dest = target.to_socketaddr(target_port);
.to_socketaddr(target_port)
.unwrap_or_else(|e| panic!("{}", e));
let timeout = Duration::new(1, 0); let timeout = Duration::new(1, 0);
unsafe {
TCP_SCANS_ISSUED += 1;
}
if let Ok(_res) = TcpStream::connect_timeout(&dest, timeout) { if let Ok(_res) = TcpStream::connect_timeout(&dest, timeout) {
println!("{:?}", dest); println!("{:?}", dest);
true true
@ -31,7 +35,9 @@ fn create_scan_thread(ip_range: IPv4Range, target_port: u16) -> JoinHandle<Vec<(
ip_range.into_iter().for_each(|id| { ip_range.into_iter().for_each(|id| {
let target = IPv4::new(id as u64); let target = IPv4::new(id as u64);
let result = tcp_scan(target, target_port); let result = tcp_scan(target, target_port);
if result {
results.push((id, result)); results.push((id, result));
}
}); });
results results
@ -130,6 +136,12 @@ pub fn start_scan(range: IPv4Range, target_port: u16, num_threads: u64) -> Vec<S
results.append(&mut worker_results); results.append(&mut worker_results);
} }
println!("Scan finished with {} result(s).", results.len()); unsafe {
println!(
"Scan finished with {} result(s) with a total of {} scans.",
results.len(),
TCP_SCANS_ISSUED
);
}
results results
} }

Loading…
Cancel
Save