Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion 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 @@ -34,6 +34,7 @@ toml = "0.5.6"
serde = "1.0.115"
serde_derive = "1.0.115"
cidr-utils = "0.5.0"
pbr = "1.0.3"

[package.metadata.deb]
depends = "$auto, nmap"
Expand Down
45 changes: 30 additions & 15 deletions src/scanner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use super::PortStrategy;
use std::{
io::{ErrorKind, Stdout},
net::{IpAddr, Shutdown, SocketAddr},
time::Duration,
};

use async_std::io;
use async_std::net::TcpStream;
use async_std::prelude::*;
use colored::*;
use futures::stream::FuturesUnordered;
use std::time::Duration;
use std::{
io::ErrorKind,
net::{IpAddr, Shutdown, SocketAddr},
};
use pbr::ProgressBar;

use super::PortStrategy;

/// The class for the scanner
/// IP is data type IpAddr and is the IP address
Expand Down Expand Up @@ -48,20 +50,31 @@ impl Scanner {
/// Returns all open ports as Vec<u16>
pub async fn run(&self) -> Vec<SocketAddr> {
let ports: Vec<u16> = self.port_strategy.order();
let mut progress: ProgressBar<Stdout> = ProgressBar::new(ports.len() as u64);
let batch_per_ip: usize = self.batch_size as usize / self.ips.len();
let mut open_sockets: Vec<SocketAddr> = Vec::new();

for batch in ports.chunks(batch_per_ip) {
let mut sockets = self.scan_ports(batch).await;
let mut sockets = self.scan_ports(batch, &mut progress).await;
open_sockets.append(&mut sockets);
}
progress.finish();
if !self.quiet {
for socket in &open_sockets {
println!("Open {}", socket.to_string().purple());
}
}

open_sockets
}

/// Given a slice of sockets, scan them all.
/// Returns a vector of open sockets.
async fn scan_ports(&self, ports: &[u16]) -> Vec<SocketAddr> {
async fn scan_ports(
&self,
ports: &[u16],
progress: &mut ProgressBar<Stdout>,
) -> Vec<SocketAddr> {
let mut ftrs = FuturesUnordered::new();
for port in ports {
for ip in &self.ips {
Expand All @@ -75,8 +88,10 @@ impl Scanner {
Ok(socket) => open_sockets.push(socket),
_ => {}
}
if !self.quiet {
progress.inc();
}
}

open_sockets
}

Expand All @@ -100,9 +115,6 @@ impl Scanner {
match x.shutdown(Shutdown::Both) {
_ => {}
}
if !self.quiet {
println!("Open {}", socket.to_string().purple());
}

Ok(socket)
}
Expand Down Expand Up @@ -138,11 +150,14 @@ impl Scanner {

#[cfg(test)]
mod tests {
use super::*;
use crate::{PortRange, ScanOrder};
use async_std::task::block_on;
use std::{net::IpAddr, time::Duration};

use async_std::task::block_on;

use crate::{PortRange, ScanOrder};

use super::*;

#[test]
fn scanner_runs() {
// Makes sure the program still runs and doesn't panic
Expand Down