Skip to content

Commit

Permalink
Merge pull request #45 from epi052/38-integrate-logging-with-progress…
Browse files Browse the repository at this point in the history
…-bars

logging and bars print nicely; closes #38
  • Loading branch information
epi052 authored Oct 1, 2020
2 parents f9b2a31 + 389bacf commit 9a5e680
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 52 deletions.
61 changes: 37 additions & 24 deletions src/heuristics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::{CONFIGURATION, PROGRESS_PRINTER};
use crate::scanner::{format_url, make_request};
use crate::utils::{get_url_path_length, status_colorizer, ferox_print};
use crate::utils::{ferox_print, get_url_path_length, status_colorizer};
use ansi_term::Color::{Cyan, Yellow};
use indicatif::ProgressBar;
use reqwest::Response;
Expand Down Expand Up @@ -158,37 +158,46 @@ async fn make_wildcard_request(target_url: &str, length: usize) -> Option<Respon
let content_len = response.content_length().unwrap_or(0);

if !CONFIGURATION.quiet {
ferox_print(&format!(
"{} {:>10} Got {} for {} (url length: {})",
wildcard,
content_len,
status_colorizer(&response.status().as_str()),
response.url(),
url_len
), &PROGRESS_PRINTER);
ferox_print(
&format!(
"{} {:>10} Got {} for {} (url length: {})",
wildcard,
content_len,
status_colorizer(&response.status().as_str()),
response.url(),
url_len
),
&PROGRESS_PRINTER,
);
}
if response.status().is_redirection() {
// show where it goes, if possible
if let Some(next_loc) = response.headers().get("Location") {
if let Ok(next_loc_str) = next_loc.to_str() {
if !CONFIGURATION.quiet {
ferox_print(&format!(
"{} {:>10} {} redirects to => {}",
wildcard,
content_len,
response.url(),
next_loc_str
), &PROGRESS_PRINTER);
ferox_print(
&format!(
"{} {:>10} {} redirects to => {}",
wildcard,
content_len,
response.url(),
next_loc_str
),
&PROGRESS_PRINTER,
);
}
} else {
if !CONFIGURATION.quiet {
ferox_print(&format!(
"{} {:>10} {} redirects to => {:?}",
wildcard,
content_len,
response.url(),
next_loc
), &PROGRESS_PRINTER);
ferox_print(
&format!(
"{} {:>10} {} redirects to => {:?}",
wildcard,
content_len,
response.url(),
next_loc
),
&PROGRESS_PRINTER,
);
}
}
}
Expand Down Expand Up @@ -238,7 +247,11 @@ pub async fn connectivity_test(target_urls: &[String]) -> Vec<String> {
}
Err(e) => {
if !CONFIGURATION.quiet {
ferox_print(&format!("Could not connect to {}, skipping...", target_url), &PROGRESS_PRINTER);
// todo unwrap
ferox_print(
&format!("Could not connect to {}, skipping...", target_url),
&PROGRESS_PRINTER,
);
}
log::error!("{}", e);
}
Expand Down
27 changes: 23 additions & 4 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::config::PROGRESS_PRINTER;
use console::{style, Color};
use env_logger::Builder;
use std::env;
use std::io::Write;
use std::time::Instant;

/// Create an instance of an `env_logger` with an added time offset
/// Create an instance of an [Logger](struct.Logger.html) and set the log level based on `verbosity`
pub fn initialize(verbosity: u8) {
// use occurrences of -v on commandline to or verbosity = N in feroxconfig.toml to set
// log level for the application; respects already specified RUST_LOG environment variable
Expand All @@ -25,9 +26,27 @@ pub fn initialize(verbosity: u8) {
let mut builder = Builder::from_default_env();

builder
.format(move |buf, rec| {
.format(move |_, record| {
let t = start.elapsed().as_secs_f32();
writeln!(buf, "{:.03} [{}] - {}", t, rec.level(), rec.args())
let level = record.level();

let (level_name, level_color) = match level {
log::Level::Error => ("ERR", Color::Red),
log::Level::Warn => ("WRN", Color::Red),
log::Level::Info => ("INF", Color::Cyan),
log::Level::Debug => ("DBG", Color::Yellow),
log::Level::Trace => ("TRC", Color::Magenta),
};

let msg = format!(
"{} {:10.03} {}",
style(format!("{}", level_name)).bg(level_color).black(),
style(t).dim(),
style(record.args()).dim(),
);

PROGRESS_PRINTER.println(msg);
Ok(())
})
.init();
}
4 changes: 2 additions & 2 deletions src/progress.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::{PROGRESS_BAR, CONFIGURATION};
use indicatif::{ProgressBar, ProgressStyle, ProgressDrawTarget};
use crate::config::{CONFIGURATION, PROGRESS_BAR};
use indicatif::{ProgressBar, ProgressStyle};

pub fn add_bar(prefix: &str, length: u64, hidden: bool) -> ProgressBar {
let style = if hidden || CONFIGURATION.quiet {
Expand Down
23 changes: 13 additions & 10 deletions src/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::{CONFIGURATION, PROGRESS_BAR, PROGRESS_PRINTER};
use crate::heuristics::WildcardFilter;
use crate::utils::{get_current_depth, get_url_path_length, status_colorizer, ferox_print};
use crate::utils::{ferox_print, get_current_depth, get_url_path_length, status_colorizer};
use crate::{heuristics, progress, FeroxResult};
use futures::future::{BoxFuture, FutureExt};
use futures::{stream, StreamExt};
Expand Down Expand Up @@ -127,7 +127,7 @@ async fn spawn_file_reporter(mut report_channel: UnboundedReceiver<Response>) {
Ok(outfile) => {
log::debug!("{:?} opened in append mode", outfile);

let mut writer = io::BufWriter::new(outfile); // tokio BufWriter
let mut writer = io::BufWriter::new(outfile); // tokio BufWriter

while let Some(resp) = report_channel.recv().await {
log::debug!("received {} on reporting channel", resp.url());
Expand Down Expand Up @@ -192,14 +192,17 @@ async fn spawn_terminal_reporter(mut report_channel: UnboundedReceiver<Response>
ferox_print(&format!("{}", resp.url()), &PROGRESS_PRINTER);
} else {
let status = status_colorizer(&resp.status().as_str());
ferox_print(&format!(
// example output
// 200 3280 https://localhost.com/FAQ
"{} {:>10} {}",
status,
resp.content_length().unwrap_or(0),
resp.url()
), &PROGRESS_PRINTER);
ferox_print(
&format!(
// example output
// 200 3280 https://localhost.com/FAQ
"{} {:>10} {}",
status,
resp.content_length().unwrap_or(0),
resp.url()
),
&PROGRESS_PRINTER,
);
}
}
log::debug!("report complete: {}", resp.url());
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ansi_term::Color::{Blue, Cyan, Green, Red, Yellow};
use console::{strip_ansi_codes, user_attended};
use indicatif::ProgressBar;
use reqwest::Url;
use std::convert::TryInto;
use console::{user_attended, strip_ansi_codes};
use indicatif::ProgressBar;

/// Helper function that determines the current depth of a given url
///
Expand Down
14 changes: 4 additions & 10 deletions tests/test_heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ fn test_static_wildcard_request_found() -> Result<(), Box<dyn std::error::Error>
predicate::str::contains("WLD")
.and(predicate::str::contains("Got"))
.and(predicate::str::contains("200"))
.and(predicate::str::contains(
"(url length: 32)",
)),
.and(predicate::str::contains("(url length: 32)")),
);

assert_eq!(mock.times_called(), 1);
Expand Down Expand Up @@ -159,17 +157,13 @@ fn test_dynamic_wildcard_request_found() -> Result<(), Box<dyn std::error::Error
teardown_tmp_directory(tmp_dir);

cmd.assert().success().stdout(
predicate::str::contains("WLD")
predicate::str::contains("WLD")
.and(predicate::str::contains("Got"))
.and(predicate::str::contains("200"))
.and(predicate::str::contains("(url length: 32)"))
.and(predicate::str::contains("(url length: 96)"))
.and(predicate::str::contains(
"Wildcard response is dynamic;",
))
.and(predicate::str::contains(
"auto-filtering",
))
.and(predicate::str::contains("Wildcard response is dynamic;"))
.and(predicate::str::contains("auto-filtering"))
.and(predicate::str::contains(
"(14 + url length) responses; toggle this behavior by using",
)),
Expand Down

0 comments on commit 9a5e680

Please sign in to comment.