From befd6c862bc86f09cf0487dd9aabfb29217a7319 Mon Sep 17 00:00:00 2001 From: Justine Pelletreau Date: Sat, 22 Jul 2023 22:04:50 +0200 Subject: [PATCH] Added timeout on http --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 1 - src/http.rs | 6 ++++-- src/main.rs | 8 +++++--- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e929f10..26cdbf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -678,7 +678,7 @@ dependencies = [ [[package]] name = "portnut" -version = "0.1.1" +version = "0.1.2" dependencies = [ "atty", "clap", diff --git a/Cargo.toml b/Cargo.toml index bdf841a..18aaa72 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "portnut" -version = "0.1.1" +version = "0.1.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index fb7d501..c2e67db 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ Portnut is a port stressing / scanning multithreaded utility. It can handle raw # TODO * Make a distributed version that allows stressing from multiple computers at the same time * Return more stats (low 1%, high 1%) -* Http : Read more about "blocking" reqwest, add a timeout # Install Go to the releases page and download the latest version. diff --git a/src/http.rs b/src/http.rs index 9374444..8f3480c 100644 --- a/src/http.rs +++ b/src/http.rs @@ -5,7 +5,7 @@ use std::sync::mpsc; use crate::results::*; -pub fn http_stress(url: String, interval: Duration, sleep: Duration, threads: u32) -> std::io::Result<()> { +pub fn http_stress(url: String, interval: Duration, sleep: Duration, threads: u32, timeout: Duration) -> std::io::Result<()> { let mut handles = vec![]; let (tx, rx) = mpsc::channel(); @@ -17,6 +17,7 @@ pub fn http_stress(url: String, interval: Duration, sleep: Duration, threads: u3 let end_time = Instant::now() + interval; let wait = sleep.clone(); let tx2 = tx.clone(); + let delay = timeout.clone(); @@ -25,7 +26,8 @@ pub fn http_stress(url: String, interval: Duration, sleep: Duration, threads: u3 if Instant::now() >= end_time { break; } let b4 = Instant::now(); - let resp = reqwest::blocking::get(add.clone()); + let client = reqwest::blocking::Client::new(); + let resp = client.get(add.clone()).timeout(delay).send(); let time = Instant::now() - b4; match resp { Ok(_) => { diff --git a/src/main.rs b/src/main.rs index cdc1612..da46f98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,7 +48,7 @@ fn main() -> std::io::Result<()> { } "httpstress" => { println!("===== HTTP STRESS TEST ====="); - http_stress(address, duration, sleep, threads)?; + http_stress(address, duration, sleep, threads, timeout)?; return Ok(()); } _ => { @@ -98,10 +98,12 @@ struct Args { ///Mode of use : either tcpstress, tcpscan or httpstress mode: String, ///IP address or hostname to scan - or url if using http + ///ex of address : 127.0.0.1 + ///ex of url : http://example.org #[arg(short, long)] address: String, - ///Timeout for each connection in seconds - #[arg(short, long, default_value_t = 1)] + ///Timeout for each connection in seconds, default 30 + #[arg(short, long, default_value_t = 30)] timeout: u64, ///Number of milliseconds to wait in between scans or requests #[arg(short, long, default_value_t = 30)]