Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit

Permalink
added filters
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-schneider-vtex committed Apr 3, 2023
1 parent b1c6054 commit e75b1f5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
65 changes: 62 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ pub struct CliArgs {
/// File to write the results
#[arg(short)]
output: Option<String>,

/// Extensions to be ignored
#[arg(
long = "fext",
default_value = "jpeg,png,jpg,gif,wof,ttf,otf,eot,swf,ico,svg,css,woff,woff2"
)]
filter_extensions: String,

/// Status codes to be ignored
#[arg(long = "fstatus", default_value = "404")]
filter_status: String,
}

impl CliArgs {
pub fn filter_extensions(&self) -> Vec<String> {
self.filter_extensions
.clone()
.split(",")
.map(|s| format!(".{}", s))
.collect_vec()
}

pub fn filter_status(&self) -> Vec<u16> {
self.filter_status
.clone()
.split(",")
.map(|s| s.parse().unwrap())
.collect_vec()
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -90,7 +119,7 @@ fn request(
let client = ClientBuilder::new()
.danger_accept_invalid_hostnames(true)
.danger_accept_invalid_certs(true)
.timeout(Duration::from_secs(2))
.timeout(Duration::from_secs(30))
.build()
.context("Error building http client")
.unwrap();
Expand Down Expand Up @@ -124,9 +153,17 @@ fn urls(args: &CliArgs) -> Vec<String> {
}
}

fn filter_by_extension(args: &CliArgs, urls: Vec<String>) -> Vec<String> {
let exts = args.filter_extensions();

urls.into_iter()
.filter(|url| exts.iter().all(|ext| !url.ends_with(ext)))
.collect_vec()
}

/// This is a blocking function that will only return when all the requests
fn execute_requests(args: &CliArgs) -> Result<()> {
let params = urls(&args)
let params = filter_by_extension(&args, urls(&args))
.into_iter()
.map(|url| RequestParam {
http_verb: args.verb,
Expand Down Expand Up @@ -169,9 +206,31 @@ fn execute_requests(args: &CliArgs) -> Result<()> {
print::write_results(args, res)
}

fn banner() {
let main = r#"
██ ▄█▀ █ ██ ██▀███ ██▓
██▄█▒ ██ ▓██▒▓██ ▒ ██▒▓██▒
▓███▄░ ▓██ ▒██░▓██ ░▄█ ▒▒██░
▓██ █▄ ▓▓█ ░██░▒██▀▀█▄ ▒██░
▒██▒ █▄▒▒█████▓ ░██▓ ▒██▒░██████▒
▒ ▒▒ ▓▒░▒▓▒ ▒ ▒ ░ ▒▓ ░▒▓░░ ▒░▓ ░
░ ░▒ ▒░░░▒░ ░ ░ ░▒ ░ ▒░░ ░ ▒ ░
░ ░░ ░ ░░░ ░ ░ ░░ ░ ░ ░
░ ░ ░ ░ ░ ░
"#
.bright_yellow();

let version: &str = env!("CARGO_PKG_VERSION");

eprint!("{}", main);

eprintln!("v{} - By: gbrls\n", version);
}

fn main() -> Result<()> {
let args = CliArgs::parse();
execute_requests(&args);
banner();
execute_requests(&args)?;

Ok(())
}
8 changes: 8 additions & 0 deletions src/print.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use strip_ansi_escapes::strip;

use crate::*;
Expand Down Expand Up @@ -64,6 +65,13 @@ pub fn log_response(args: &CliArgs, resp: Response, url: &str) -> Result<String>
let headers = resp.headers().clone();

let status = resp.status();
let filter_status = args.filter_status();

let u16_status = status.as_u16();

if filter_status.into_iter().any(|fs| u16_status == fs) {
return Err(anyhow!("Filtered"));
}

let headers = resp.headers().to_owned();
let content_type = headers
Expand Down

0 comments on commit e75b1f5

Please sign in to comment.