Skip to content

Commit b3923d7

Browse files
committed
don't log to stdout if not cli
1 parent d9dec00 commit b3923d7

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

src/input.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Provides a means to read, parse and hold configuration options for scans.
2+
use crate::print_log;
3+
24
use clap::{Parser, ValueEnum};
35
use serde_derive::Deserialize;
46
use std::collections::HashMap;
@@ -298,7 +300,7 @@ impl Config {
298300
let config: Config = match toml::from_str(&content) {
299301
Ok(config) => config,
300302
Err(e) => {
301-
println!("Found {e} in configuration file.\nAborting scan.\n");
303+
print_log!(error, "Found {e} in configuration file.\nAborting scan.\n");
302304
std::process::exit(1);
303305
}
304306
};

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ pub mod scripts;
5656
pub mod address;
5757

5858
pub mod generated;
59+
60+
/// Static variable defining the current state of execution. The cli binary should
61+
/// set it to true by calling set_cli_mode.
62+
#[doc(hidden)]
63+
pub static IS_CLI_MODE: once_cell::sync::OnceCell<bool> = once_cell::sync::OnceCell::new();
64+
65+
/// Set IS_CLI_MODE to true.
66+
#[doc(hidden)]
67+
pub fn set_cli_mode() {
68+
let _ = IS_CLI_MODE.set(true);
69+
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustscan::input::{self, Config, Opts, ScriptsRequired};
77
use rustscan::port_strategy::PortStrategy;
88
use rustscan::scanner::Scanner;
99
use rustscan::scripts::{init_scripts, Script, ScriptFile};
10+
use rustscan::set_cli_mode;
1011
use rustscan::{detail, funny_opening, output, warning};
1112

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

42+
set_cli_mode();
4143
env_logger::init();
4244
let mut benchmarks = Benchmark::init();
4345
let mut rustscan_bench = NamedTimer::start("RustScan");

src/scanner/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Core functionality for actual scanning behaviour.
22
use crate::generated::get_parsed_data;
33
use crate::port_strategy::PortStrategy;
4+
use crate::print_log;
45
use log::debug;
56

67
mod socket_iterator;
@@ -290,7 +291,7 @@ impl Scanner {
290291
}
291292
}
292293
Err(e) => {
293-
println!("Err E binding sock {:?}", e);
294+
print_log!(error, "Err E binding sock {:?}", e);
294295
Err(e)
295296
}
296297
}
@@ -300,9 +301,9 @@ impl Scanner {
300301
fn fmt_ports(&self, socket: SocketAddr) {
301302
if !self.greppable {
302303
if self.accessible {
303-
println!("Open {socket}");
304+
print_log!(info, "Open {socket}");
304305
} else {
305-
println!("Open {}", socket.to_string().purple());
306+
print_log!(info, "Open {}", socket.to_string().purple());
306307
}
307308
}
308309
}

src/scanner/socket_iterator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<'s> SocketIterator<'s> {
3030
}
3131

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

3636
/// Returns a socket based on the combination of one of the provided

src/tui.rs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@
55
#[macro_export]
66
macro_rules! warning {
77
($name:expr) => {
8-
println!("{} {}", ansi_term::Colour::Red.bold().paint("[!]"), $name);
8+
$crate::print_log!(
9+
warn,
10+
"{} {}",
11+
ansi_term::Colour::Red.bold().paint("[!]"),
12+
$name
13+
);
914
};
1015
($name:expr, $greppable:expr, $accessible:expr) => {
1116
// if not greppable then print, otherwise no else statement so do not print.
1217
if !$greppable {
1318
if $accessible {
1419
// Don't print the ascii art
15-
println!("{}", $name);
20+
$crate::print_log!(warn, "{}", $name);
1621
} else {
17-
println!("{} {}", ansi_term::Colour::Red.bold().paint("[!]"), $name);
22+
$crate::print_log!(
23+
warn,
24+
"{} {}",
25+
ansi_term::Colour::Red.bold().paint("[!]"),
26+
$name
27+
);
1828
}
1929
}
2030
};
@@ -23,16 +33,26 @@ macro_rules! warning {
2333
#[macro_export]
2434
macro_rules! detail {
2535
($name:expr) => {
26-
println!("{} {}", ansi_term::Colour::Blue.bold().paint("[~]"), $name);
36+
$crate::print_log!(
37+
info,
38+
"{} {}",
39+
ansi_term::Colour::Blue.bold().paint("[~]"),
40+
$name
41+
);
2742
};
2843
($name:expr, $greppable:expr, $accessible:expr) => {
2944
// if not greppable then print, otherwise no else statement so do not print.
3045
if !$greppable {
3146
if $accessible {
3247
// Don't print the ascii art
33-
println!("{}", $name);
48+
$crate::print_log!(info, "{}", $name);
3449
} else {
35-
println!("{} {}", ansi_term::Colour::Blue.bold().paint("[~]"), $name);
50+
$crate::print_log!(
51+
info,
52+
"{} {}",
53+
ansi_term::Colour::Blue.bold().paint("[~]"),
54+
$name
55+
);
3656
}
3757
}
3858
};
@@ -41,7 +61,8 @@ macro_rules! detail {
4161
#[macro_export]
4262
macro_rules! output {
4363
($name:expr) => {
44-
println!(
64+
$crate::print_log!(
65+
info,
4566
"{} {}",
4667
RGansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"),
4768
$name
@@ -52,9 +73,10 @@ macro_rules! output {
5273
if !$greppable {
5374
if $accessible {
5475
// Don't print the ascii art
55-
println!("{}", $name);
76+
$crate::print_log!(info, "{}", $name);
5677
} else {
57-
println!(
78+
$crate::print_log!(
79+
info,
5880
"{} {}",
5981
ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"),
6082
$name
@@ -103,3 +125,23 @@ macro_rules! funny_opening {
103125
println!("{}\n", random_quote);
104126
};
105127
}
128+
129+
/// Wrapper macro for printing/logging wraps println! and log::$level!
130+
/// 1. if rustscan::IS_CLI_MODE is true calls `println!`
131+
/// 2. if rustscan::IS_CLI_MODE is undefined or false `log::$level!` also sets IS_CLI_MODE
132+
/// to false if it was previously undefined.
133+
///
134+
/// Library code should call this macro to print information that the binary
135+
/// is expected to print to stdout and library is expected to log at a
136+
/// level specified by parameter $level.
137+
#[doc(hidden)]
138+
#[macro_export]
139+
macro_rules! print_log {
140+
($level:ident, $($fmt_args:tt)*) => {
141+
if *$crate::IS_CLI_MODE.get_or_init(|| false) {
142+
println!($($fmt_args)*);
143+
} else {
144+
log::$level!($($fmt_args)*);
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)