Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't log to stdout if not cli #708

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Provides a means to read, parse and hold configuration options for scans.
use crate::print_log;

use clap::{Parser, ValueEnum};
use serde_derive::Deserialize;
use std::collections::HashMap;
Expand Down Expand Up @@ -298,7 +300,7 @@ impl Config {
let config: Config = match toml::from_str(&content) {
Ok(config) => config,
Err(e) => {
println!("Found {e} in configuration file.\nAborting scan.\n");
print_log!(error, "Found {e} in configuration file.\nAborting scan.\n");
std::process::exit(1);
}
};
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,14 @@ pub mod scripts;
pub mod address;

pub mod generated;

/// Static variable defining the current state of execution. The cli binary should
/// set it to true by calling set_cli_mode.
#[doc(hidden)]
pub static IS_CLI_MODE: once_cell::sync::OnceCell<bool> = once_cell::sync::OnceCell::new();

/// Set IS_CLI_MODE to true.
#[doc(hidden)]
pub fn set_cli_mode() {
let _ = IS_CLI_MODE.set(true);
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustscan::input::{self, Config, Opts, ScriptsRequired};
use rustscan::port_strategy::PortStrategy;
use rustscan::scanner::Scanner;
use rustscan::scripts::{init_scripts, Script, ScriptFile};
use rustscan::set_cli_mode;
use rustscan::{detail, funny_opening, output, warning};

use colorful::{Color, Colorful};
Expand Down Expand Up @@ -38,6 +39,7 @@ fn main() {
#[cfg(not(unix))]
let _ = ansi_term::enable_ansi_support();

set_cli_mode();
env_logger::init();
let mut benchmarks = Benchmark::init();
let mut rustscan_bench = NamedTimer::start("RustScan");
Expand Down
7 changes: 4 additions & 3 deletions src/scanner/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Core functionality for actual scanning behaviour.
use crate::generated::get_parsed_data;
use crate::port_strategy::PortStrategy;
use crate::print_log;
use log::debug;

mod socket_iterator;
Expand Down Expand Up @@ -290,7 +291,7 @@ impl Scanner {
}
}
Err(e) => {
println!("Err E binding sock {:?}", e);
print_log!(error, "Err E binding sock {:?}", e);
Err(e)
}
}
Expand All @@ -300,9 +301,9 @@ impl Scanner {
fn fmt_ports(&self, socket: SocketAddr) {
if !self.greppable {
if self.accessible {
println!("Open {socket}");
print_log!(info, "Open {socket}");
} else {
println!("Open {}", socket.to_string().purple());
print_log!(info, "Open {}", socket.to_string().purple());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scanner/socket_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'s> SocketIterator<'s> {
}

#[allow(clippy::doc_link_with_quotes)]
impl<'s> Iterator for SocketIterator<'s> {
impl Iterator for SocketIterator<'_> {
type Item = SocketAddr;

/// Returns a socket based on the combination of one of the provided
Expand Down
60 changes: 51 additions & 9 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@
#[macro_export]
macro_rules! warning {
($name:expr) => {
println!("{} {}", ansi_term::Colour::Red.bold().paint("[!]"), $name);
$crate::print_log!(
warn,
"{} {}",
ansi_term::Colour::Red.bold().paint("[!]"),
$name
);
};
($name:expr, $greppable:expr, $accessible:expr) => {
// if not greppable then print, otherwise no else statement so do not print.
if !$greppable {
if $accessible {
// Don't print the ascii art
println!("{}", $name);
$crate::print_log!(warn, "{}", $name);
} else {
println!("{} {}", ansi_term::Colour::Red.bold().paint("[!]"), $name);
$crate::print_log!(
warn,
"{} {}",
ansi_term::Colour::Red.bold().paint("[!]"),
$name
);
}
}
};
Expand All @@ -23,16 +33,26 @@ macro_rules! warning {
#[macro_export]
macro_rules! detail {
($name:expr) => {
println!("{} {}", ansi_term::Colour::Blue.bold().paint("[~]"), $name);
$crate::print_log!(
info,
"{} {}",
ansi_term::Colour::Blue.bold().paint("[~]"),
$name
);
};
($name:expr, $greppable:expr, $accessible:expr) => {
// if not greppable then print, otherwise no else statement so do not print.
if !$greppable {
if $accessible {
// Don't print the ascii art
println!("{}", $name);
$crate::print_log!(info, "{}", $name);
} else {
println!("{} {}", ansi_term::Colour::Blue.bold().paint("[~]"), $name);
$crate::print_log!(
info,
"{} {}",
ansi_term::Colour::Blue.bold().paint("[~]"),
$name
);
}
}
};
Expand All @@ -41,7 +61,8 @@ macro_rules! detail {
#[macro_export]
macro_rules! output {
($name:expr) => {
println!(
$crate::print_log!(
info,
"{} {}",
RGansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"),
$name
Expand All @@ -52,9 +73,10 @@ macro_rules! output {
if !$greppable {
if $accessible {
// Don't print the ascii art
println!("{}", $name);
$crate::print_log!(info, "{}", $name);
} else {
println!(
$crate::print_log!(
info,
"{} {}",
ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"),
$name
Expand Down Expand Up @@ -103,3 +125,23 @@ macro_rules! funny_opening {
println!("{}\n", random_quote);
};
}

/// Wrapper macro for printing/logging wraps println! and log::$level!
/// 1. if rustscan::IS_CLI_MODE is true calls `println!`
/// 2. if rustscan::IS_CLI_MODE is undefined or false `log::$level!` also sets IS_CLI_MODE
/// to false if it was previously undefined.
///
/// Library code should call this macro to print information that the binary
/// is expected to print to stdout and library is expected to log at a
/// level specified by parameter $level.
#[doc(hidden)]
#[macro_export]
macro_rules! print_log {
($level:ident, $($fmt_args:tt)*) => {
if *$crate::IS_CLI_MODE.get_or_init(|| false) {
println!($($fmt_args)*);
} else {
log::$level!($($fmt_args)*);
}
}
}
Loading