Properly return the URL if --max-redirects is used.

Signed-off-by: Eduard Tolosa <edu4rdshl@protonmail.com>
This commit is contained in:
Eduard Tolosa 2022-04-12 02:00:36 -05:00
parent 4c2ad48c2d
commit 8293e8b49d
No known key found for this signature in database
GPG key ID: 3A574A4009F553E5

View file

@ -5,7 +5,7 @@ use rand::{distributions::Alphanumeric, thread_rng as rng, Rng};
use reqwest::{ use reqwest::{
header::{CONTENT_LENGTH, CONTENT_TYPE}, header::{CONTENT_LENGTH, CONTENT_TYPE},
redirect::Policy, redirect::Policy,
Client, Response, Client, Response, Url,
}; };
use crate::structs::{HTTPFilters, LibOptions}; use crate::structs::{HTTPFilters, LibOptions};
@ -29,22 +29,20 @@ pub async fn return_http_data(options: &LibOptions) -> HashMap<String, HttpData>
futures::stream::iter(options.hosts.clone().into_iter().map(|host| { futures::stream::iter(options.hosts.clone().into_iter().map(|host| {
// Use a random user agent // Use a random user agent
let user_agent = utils::return_random_string(&options.user_agents); let user_agent = utils::return_random_string(&options.user_agents);
// HTTP/HTTP URLs
let https_url = format!("https://{}", host);
let http_url = format!("http://{}", host);
// Create futures // Create futures
let https_send_fut = options let https_send_fut = options
.client .client
.get(&https_url) .get(&format!("https://{}", host))
.header(USER_AGENT, &user_agent); .header(USER_AGENT, &user_agent);
let http_send_fut = options let http_send_fut = options
.client .client
.get(&http_url) .get(&format!("http://{}", host))
.header(USER_AGENT, &user_agent); .header(USER_AGENT, &user_agent);
let mut http_data = HttpData::default(); let mut http_data = HttpData::default();
let mut is_http = false;
let mut response = Option::<Response>::None; let mut response = Option::<Response>::None;
async move { async move {
@ -65,7 +63,6 @@ pub async fn return_http_data(options: &LibOptions) -> HashMap<String, HttpData>
.send() .send()
.await .await
{ {
is_http = true;
response = Some(resp); response = Some(resp);
break; break;
} }
@ -74,13 +71,12 @@ pub async fn return_http_data(options: &LibOptions) -> HashMap<String, HttpData>
} else if let Ok(resp) = https_send_fut.send().await { } else if let Ok(resp) = https_send_fut.send().await {
response = Some(resp); response = Some(resp);
} else if let Ok(resp) = http_send_fut.send().await { } else if let Ok(resp) = http_send_fut.send().await {
is_http = true;
response = Some(resp); response = Some(resp);
} }
match response { match response {
Some(resp) => { Some(resp) => {
http_data.host_url = if is_http { http_url } else { https_url.clone() }; http_data.host_url = return_url(resp.url().to_owned()).await;
if options.assign_response_data { if options.assign_response_data {
http_data = http_data =
assign_response_data(http_data, resp, options.return_filters).await; assign_response_data(http_data, resp, options.return_filters).await;
@ -115,9 +111,15 @@ pub async fn return_http_data(options: &LibOptions) -> HashMap<String, HttpData>
} }
pub fn return_http_client(timeout: u64, max_redirects: usize) -> Client { pub fn return_http_client(timeout: u64, max_redirects: usize) -> Client {
let policy = if max_redirects == 0 {
Policy::none()
} else {
Policy::limited(max_redirects)
};
reqwest::Client::builder() reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(timeout)) .timeout(std::time::Duration::from_secs(timeout))
.redirect(Policy::limited(max_redirects)) .redirect(policy)
.danger_accept_invalid_certs(true) .danger_accept_invalid_certs(true)
.trust_dns(true) .trust_dns(true)
.use_native_tls() .use_native_tls()
@ -125,6 +127,12 @@ pub fn return_http_client(timeout: u64, max_redirects: usize) -> Client {
.expect("Failed to create HTTP client") .expect("Failed to create HTTP client")
} }
pub async fn return_url(mut url: Url) -> String {
url.set_path("");
url.set_query(None);
url.to_string()
}
#[allow(clippy::field_reassign_with_default)] #[allow(clippy::field_reassign_with_default)]
pub async fn assign_response_data( pub async fn assign_response_data(
mut http_data: HttpData, mut http_data: HttpData,