From 0c6d6c70bb3ac16264f95a539caabaab2d5ff87b Mon Sep 17 00:00:00 2001 From: epi Date: Mon, 1 Feb 2021 06:39:11 -0600 Subject: [PATCH] removed CONFIG from progress; CONFIG completely gone --- src/config.rs | 3 --- src/event_handlers/scans.rs | 2 +- src/event_handlers/statistics.rs | 1 + src/main.rs | 4 ++-- src/progress.rs | 27 +++++++++++---------------- src/scan_manager/scan.rs | 24 +++++++++++++++++++++--- src/scan_manager/scan_container.rs | 29 ++++++++++++++++++++++++++--- src/scan_manager/tests.rs | 15 ++++++++++++--- src/url.rs | 2 +- 9 files changed, 75 insertions(+), 32 deletions(-) diff --git a/src/config.rs b/src/config.rs index 21a04ea..53b02e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,9 +21,6 @@ use std::{ }; lazy_static! { - /// Global configuration state - pub static ref CONFIGURATION: Configuration = Configuration::new().expect("Could not create Configuration"); - /// Global progress bar that houses other progress bars pub static ref PROGRESS_BAR: MultiProgress = MultiProgress::with_draw_target(ProgressDrawTarget::stdout()); diff --git a/src/event_handlers/scans.rs b/src/event_handlers/scans.rs index a0fe52b..e03c949 100644 --- a/src/event_handlers/scans.rs +++ b/src/event_handlers/scans.rs @@ -117,7 +117,7 @@ impl ScanHandler { pub fn initialize(handles: Arc) -> (Joiner, ScanHandle) { log::trace!("enter: initialize"); - let data = Arc::new(FeroxScans::default()); + let data = Arc::new(FeroxScans::new(handles.config.quiet)); let (tx, rx): FeroxChannel = mpsc::unbounded_channel(); let max_depth = handles.config.depth; diff --git a/src/event_handlers/statistics.rs b/src/event_handlers/statistics.rs index 98f7b16..80d1757 100644 --- a/src/event_handlers/statistics.rs +++ b/src/event_handlers/statistics.rs @@ -108,6 +108,7 @@ impl StatsHandler { } Command::UpdateF64Field(field, value) => self.stats.update_f64_field(field, value), Command::CreateBar => { + // todo qhat baout -q self.bar = add_bar("", self.stats.total_expected() as u64, BarType::Total); } Command::LoadStats(filename) => { diff --git a/src/main.rs b/src/main.rs index 4b62b9f..2bcb203 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,8 +86,8 @@ async fn scan(targets: Vec, handles: Arc) -> Result<()> { // - scanner initialized (this sent expected requests per directory to the stats thread, which // having been set, makes it so the progress bar doesn't flash as full before anything has // even happened - handles.stats.send(CreateBar)?; - // blocks until the bar is created / avoids race condition in first two bars + handles.stats.send(CreateBar)?; // todo qhat about -q ? + // blocks until the bar is created / avoids race condition in first two bars handles.stats.sync().await?; if handles.config.resumed { diff --git a/src/progress.rs b/src/progress.rs index 777973e..713fb07 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -1,4 +1,4 @@ -use crate::config::{CONFIGURATION, PROGRESS_BAR}; +use crate::config::PROGRESS_BAR; use indicatif::{ProgressBar, ProgressStyle}; /// Types of ProgressBars that can be added to `PROGRESS_BAR` @@ -22,21 +22,16 @@ pub enum BarType { pub fn add_bar(prefix: &str, length: u64, bar_type: BarType) -> ProgressBar { let mut style = ProgressStyle::default_bar().progress_chars("#>-"); - style = if CONFIGURATION.quiet { - style.template("") - } else { - match bar_type { - BarType::Hidden => style.template(""), - BarType::Default => style.template( - "[{bar:.cyan/blue}] - {elapsed:<4} {pos:>7}/{len:7} {per_sec:7} {prefix}", - ), - BarType::Message => style.template(&format!( - "[{{bar:.cyan/blue}}] - {{elapsed:<4}} {{pos:>7}}/{{len:7}} {:7} {{prefix}}", - "-" - )), - BarType::Total => { - style.template("[{bar:.yellow/blue}] - {elapsed:<4} {pos:>7}/{len:7} {eta:7} {msg}") - } + style = match bar_type { + BarType::Hidden => style.template(""), + BarType::Default => style + .template("[{bar:.cyan/blue}] - {elapsed:<4} {pos:>7}/{len:7} {per_sec:7} {prefix}"), + BarType::Message => style.template(&format!( + "[{{bar:.cyan/blue}}] - {{elapsed:<4}} {{pos:>7}}/{{len:7}} {:7} {{prefix}}", + "-" + )), + BarType::Total => { + style.template("[{bar:.yellow/blue}] - {elapsed:<4} {pos:>7}/{len:7} {eta:7} {msg}") } }; diff --git a/src/scan_manager/scan.rs b/src/scan_manager/scan.rs index 9ac0193..1b71383 100644 --- a/src/scan_manager/scan.rs +++ b/src/scan_manager/scan.rs @@ -22,7 +22,7 @@ use uuid::Uuid; pub struct FeroxScan { /// UUID that uniquely ID's the scan pub id: String, - + // todo consider pub(super) or similar /// The URL that to be scanned pub url: String, @@ -43,6 +43,9 @@ pub struct FeroxScan { /// The progress bar associated with this scan pub progress_bar: Mutex>, + + /// whether or not the user passed -q on the command line + pub quiet: bool, } /// Default implementation for FeroxScan @@ -60,6 +63,7 @@ impl Default for FeroxScan { url: String::new(), progress_bar: Mutex::new(None), scan_type: ScanType::File, + quiet: false, } } } @@ -112,7 +116,13 @@ impl FeroxScan { if guard.is_some() { (*guard).as_ref().unwrap().clone() } else { - let pb = add_bar(&self.url, self.num_requests, BarType::Default); + let bar_type = if self.quiet { + BarType::Hidden + } else { + BarType::Default + }; + + let pb = add_bar(&self.url, self.num_requests, bar_type); pb.reset_elapsed(); let _ = std::mem::replace(&mut *guard, Some(pb.clone())); @@ -121,7 +131,13 @@ impl FeroxScan { } Err(_) => { log::warn!("Could not unlock progress bar on {:?}", self); - let pb = add_bar(&self.url, self.num_requests, BarType::Default); + let bar_type = if self.quiet { + BarType::Hidden + } else { + BarType::Default + }; + + let pb = add_bar(&self.url, self.num_requests, bar_type); pb.reset_elapsed(); pb @@ -135,6 +151,7 @@ impl FeroxScan { scan_type: ScanType, scan_order: ScanOrder, num_requests: u64, + quiet: bool, pb: Option, ) -> Arc { Arc::new(Self { @@ -142,6 +159,7 @@ impl FeroxScan { scan_type, scan_order, num_requests, + quiet, progress_bar: Mutex::new(pb), ..Default::default() }) diff --git a/src/scan_manager/scan_container.rs b/src/scan_manager/scan_container.rs index 5a19576..2452ab3 100644 --- a/src/scan_manager/scan_container.rs +++ b/src/scan_manager/scan_container.rs @@ -40,6 +40,9 @@ pub struct FeroxScans { /// number of requests expected per scan (mirrors the same on Stats); used for initializing /// progress bars and feroxscans bar_length: Mutex, + + /// whether or not the user passed -q on the command line + quiet: bool, } /// Serialize implementation for FeroxScans @@ -68,6 +71,14 @@ impl Serialize for FeroxScans { /// Implementation of `FeroxScans` impl FeroxScans { + /// given the value for -q, create a new FeroxScans object + pub fn new(quiet: bool) -> Self { + Self { + quiet, + ..Default::default() + } + } + /// Add a `FeroxScan` to the internal container /// /// If the internal container did NOT contain the scan, true is returned; else false @@ -239,14 +250,21 @@ impl FeroxScans { /// if a resumed scan is already complete, display a completed progress bar to the user pub fn print_completed_bars(&self, bar_length: usize) -> Result<()> { + // todo check this with -q, probably just check for self.quiet and return doing nowhting if let Ok(scans) = self.scans.read() { for scan in scans.iter() { if scan.is_complete() { // these scans are complete, and just need to be shown to the user + let bar_type = if self.quiet { + BarType::Hidden + } else { + BarType::Message + }; + let pb = add_bar( &scan.url, bar_length.try_into().unwrap_or_default(), - BarType::Message, + bar_type, ); pb.finish(); } @@ -321,7 +339,12 @@ impl FeroxScans { let bar = match scan_type { ScanType::Directory => { - let progress_bar = add_bar(&url, bar_length, BarType::Default); + let bar_type = if self.quiet { + BarType::Hidden + } else { + BarType::Default + }; + let progress_bar = add_bar(&url, bar_length, bar_type); progress_bar.reset_elapsed(); @@ -330,7 +353,7 @@ impl FeroxScans { ScanType::File => None, }; - let ferox_scan = FeroxScan::new(&url, scan_type, scan_order, bar_length, bar); + let ferox_scan = FeroxScan::new(&url, scan_type, scan_order, bar_length, self.quiet, bar); // If the set did not contain the scan, true is returned. // If the set did contain the scan, false is returned. diff --git a/src/scan_manager/tests.rs b/src/scan_manager/tests.rs index b7da6fc..4b81375 100644 --- a/src/scan_manager/tests.rs +++ b/src/scan_manager/tests.rs @@ -61,6 +61,7 @@ fn add_url_to_list_of_scanned_urls_with_known_url() { ScanType::Directory, ScanOrder::Latest, pb.length(), + false, Some(pb), ); @@ -82,6 +83,7 @@ fn stop_progress_bar_stops_bar() { ScanType::Directory, ScanOrder::Latest, pb.length(), + false, Some(pb), ); @@ -114,7 +116,7 @@ fn add_url_to_list_of_scanned_urls_with_known_url_without_slash() { let urls = FeroxScans::default(); let url = "http://unknown_url"; - let scan = FeroxScan::new(url, ScanType::File, ScanOrder::Latest, 0, None); + let scan = FeroxScan::new(url, ScanType::File, ScanOrder::Latest, 0, false, None); assert_eq!(urls.insert(scan), true); @@ -136,6 +138,7 @@ async fn call_display_scans() { ScanType::Directory, ScanOrder::Latest, pb.length(), + false, Some(pb), ); let scan_two = FeroxScan::new( @@ -143,6 +146,7 @@ async fn call_display_scans() { ScanType::Directory, ScanOrder::Latest, pb_two.length(), + false, Some(pb_two), ); @@ -164,8 +168,8 @@ async fn call_display_scans() { /// ensure that PartialEq compares FeroxScan.id fields fn partial_eq_compares_the_id_field() { let url = "http://unknown_url/"; - let scan = FeroxScan::new(url, ScanType::Directory, ScanOrder::Latest, 0, None); - let scan_two = FeroxScan::new(url, ScanType::Directory, ScanOrder::Latest, 0, None); + let scan = FeroxScan::new(url, ScanType::Directory, ScanOrder::Latest, 0, false, None); + let scan_two = FeroxScan::new(url, ScanType::Directory, ScanOrder::Latest, 0, false, None); assert!(!scan.eq(&scan_two)); @@ -240,6 +244,7 @@ fn ferox_scan_serialize() { ScanType::Directory, ScanOrder::Latest, 0, + false, None, ); let fs_json = format!( @@ -257,6 +262,7 @@ fn ferox_scans_serialize() { ScanType::Directory, ScanOrder::Latest, 0, + false, None, ); let ferox_scans = FeroxScans::default(); @@ -317,6 +323,7 @@ fn feroxstates_feroxserialize_implementation() { ScanType::Directory, ScanOrder::Latest, 0, + false, None, ); let ferox_scans = FeroxScans::default(); @@ -404,6 +411,7 @@ fn feroxscan_display() { scan_order: ScanOrder::Latest, scan_type: Default::default(), num_requests: 0, + quiet: false, status: Default::default(), task: tokio::sync::Mutex::new(None), progress_bar: std::sync::Mutex::new(None), @@ -443,6 +451,7 @@ async fn ferox_scan_abort() { scan_order: ScanOrder::Latest, scan_type: Default::default(), num_requests: 0, + quiet: false, status: std::sync::Mutex::new(ScanStatus::Running), task: tokio::sync::Mutex::new(Some(tokio::spawn(async move { sleep(Duration::from_millis(SLEEP_DURATION * 2)); diff --git a/src/url.rs b/src/url.rs index c53b6ea..cb0534a 100644 --- a/src/url.rs +++ b/src/url.rs @@ -2,7 +2,7 @@ use crate::{event_handlers::Handles, statistics::StatError::UrlFormat, Command:: use anyhow::{anyhow, bail, Result}; use reqwest::Url; use std::{convert::TryInto, fmt, sync::Arc}; -// todo rename this and other ferox_* modules to just be the base name + /// abstraction around target urls; collects all Url related shenanigans in one place #[derive(Debug)] pub struct FeroxUrl {