Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ fn main() {
opts.accessible,
opts.exclude_ports.unwrap_or_default(),
opts.udp,
);
)
.with_open_port_output();
debug!("Scanner finished building: {scanner:?}");

let mut portscan_bench = NamedTimer::start("Portscan");
Expand Down
49 changes: 47 additions & 2 deletions src/scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Scanner {
accessible: bool,
exclude_ports: Vec<u16>,
udp: bool,
print_open_ports: bool,
}

// Allowing too many arguments for clippy.
Expand All @@ -63,9 +64,19 @@ impl Scanner {
accessible,
exclude_ports,
udp,
print_open_ports: false,
}
}

/// Enables the CLI's incremental open-port output.
///
/// Library callers are quiet by default and can inspect the sockets returned by [`Self::run`].
#[must_use]
pub fn with_open_port_output(mut self) -> Self {
self.print_open_ports = true;
self
}

/// Runs scan_range with chunk sizes
/// If you want to run RustScan normally, this is the entry point used
/// Returns all open ports as `Vec<u16>`
Expand Down Expand Up @@ -288,15 +299,15 @@ impl Scanner {
}
}
Err(e) => {
println!("Err E binding sock {e:?}");
debug!("Error binding UDP socket: {e:?}");
Err(e)
}
}
}

/// Formats and prints the port status
fn fmt_ports(&self, socket: SocketAddr) {
if !self.greppable {
if self.print_open_ports && !self.greppable {
if self.accessible {
println!("Open {socket}");
} else {
Expand All @@ -313,6 +324,40 @@ mod tests {
use async_std::task::block_on;
use std::{net::IpAddr, time::Duration};

fn test_scanner() -> Scanner {
let addrs = vec!["127.0.0.1".parse::<IpAddr>().unwrap()];
let strategy = PortStrategy::pick(
&Some(PortRange { start: 1, end: 1 }),
None,
ScanOrder::Serial,
);
Scanner::new(
&addrs,
1,
Duration::from_millis(100),
1,
false,
strategy,
false,
Vec::new(),
false,
)
}

#[test]
fn library_scanner_is_quiet_by_default() {
let scanner = test_scanner();

assert!(!scanner.print_open_ports);
}

#[test]
fn cli_can_enable_open_port_output() {
let scanner = test_scanner().with_open_port_output();

assert!(scanner.print_open_ports);
}

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