Skip to content

Commit

Permalink
refactor types, borrow strings instead of cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
junnlikestea committed Jul 3, 2020
1 parent 8e559dd commit a210a3b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use std::io::{self, Read};

#[tokio::main]
async fn main() -> Result<()> {
let args = create_clap_app("0.1.0");
let args = create_clap_app("0.1.1");
let matches = args.get_matches();
// what if instead of creating a vec of strings, we make a lazy_static vec of &str, and avoid
// cloning between function calls?
let mut verbose = false;
let mut urls: Vec<String> = Vec::new();
let location = matches.value_of("location").unwrap();
Expand All @@ -28,7 +26,7 @@ async fn main() -> Result<()> {
urls = read_stdin()?;
}

run(urls, location.to_owned(), timeout, verbose).await;
run(urls, location.into(), timeout, verbose).await;
Ok(())
}

Expand Down
50 changes: 29 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use futures::future::join_all;
use std::sync::Arc;
use std::time::Duration;

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync + 'static>>;
Expand All @@ -20,7 +20,6 @@ const INJECTABLE_HEADERS: &[&str] = &[
"Redirect",
"Real-Ip",
"Referer",
"Referer",
"Referrer",
"Refferer",
"Uri",
Expand All @@ -37,61 +36,70 @@ const INJECTABLE_HEADERS: &[&str] = &[
];

async fn fetch(
url: String,
url: Arc<String>,
header: &str,
location: String,
location: Arc<String>,
timeout: u64,
verbose: bool,
) -> Result<()> {
let time = Duration::from_secs(timeout);

let resp = reqwest::Client::new()
.get(&url)
.get(url.as_str())
.timeout(time)
.header(header, location)
.header(header, location.as_str())
.send()
.await;

match resp {
Ok(r) => println!("[{}] -> {}", r.status().as_str(), &url),
Ok(r) => println!("[{}] -> {}", r.status().as_str(), url),

Err(e) => {
if verbose {
eprintln!(
"Requested: {} but was unreachable, with error: {}.",
&url, e
)
eprintln!("Requested: {} but was unreachable, with error: {}.", url, e)
}
}
}
Ok(())
}

async fn inject_headers(url: String, location: String, timeout: u64, verbose: bool) -> Result<()> {
async fn inject_headers(
url: Arc<String>,
location: Arc<String>,
timeout: u64,
verbose: bool,
) -> Result<()> {
let mut tasks = Vec::new();

for header in INJECTABLE_HEADERS.iter() {
let l = location.clone();
let u = url.clone();
let url = url.clone();
let location = location.clone();

tasks.push(tokio::spawn(async move {
if verbose {
println!("Injecting:{} into {} -> {}", &l, header, &u);
println!("Injecting:{} into {} -> {}", location, header, url);
}

fetch(u, header, l, timeout, verbose).await
fetch(url, header, location, timeout, verbose).await
}))
}

join_all(tasks).await;
for t in tasks {
t.await?;
}

Ok(())
}

pub async fn run(urls: Vec<String>, location: String, timeout: u64, verbose: bool) {
const ACTIVE_REQUESTS: usize = 100;

use futures::stream::StreamExt;

const ACTIVE_REQUESTS: usize = 100;
let shared_location = Arc::new(location);
let responses = futures::stream::iter(urls.into_iter().map(|url| {
let l = location.clone();
tokio::spawn(async move { inject_headers(url, l, timeout, verbose).await })
let url = Arc::new(url);
let loc = shared_location.clone();
tokio::spawn(async move { inject_headers(url, loc, timeout, verbose).await })
}))
.buffer_unordered(ACTIVE_REQUESTS)
.collect::<Vec<_>>();
Expand Down

0 comments on commit a210a3b

Please sign in to comment.