diff --git a/src/config.rs b/src/config.rs index 8e8d697..af47f9c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,6 +20,8 @@ pub struct Config { pub struct ConfigHttp { #[serde(rename = "5xx_status_code_down")] pub fivexx_status_code_down: bool, + pub follow_redirects: bool, + pub max_follow_redirects: Option, } impl Config { @@ -45,6 +47,10 @@ pub async fn init_config(path: String) -> anyhow::Result<()> { if config.password.len() != 256 / 4 || config.password.chars().any(|c| !c.is_ascii_hexdigit()) { bail!("Password is not a valid SHA256") } + + if config.http.follow_redirects && config.http.max_follow_redirects.is_none() { + bail!("max_follow_redirects must be set if follow_redirects is enabled"); + } CONFIG.set(Arc::new(Mutex::new(config))).unwrap(); diff --git a/src/monitor/http.rs b/src/monitor/http.rs index 701a6fe..1aca384 100644 --- a/src/monitor/http.rs +++ b/src/monitor/http.rs @@ -6,7 +6,7 @@ use std::{ use adler32::adler32; use axum::http::{HeaderName, HeaderValue}; -use reqwest::StatusCode; +use reqwest::{redirect::Policy, StatusCode}; use serde::{Deserialize, Serialize}; use crate::config::CONFIG; @@ -168,8 +168,16 @@ pub async fn http_service( request_data: &HttpRequest, ) -> MonitorResult { let start_time = Instant::now(); - - let client = reqwest::Client::new(); + let config = CONFIG.get().unwrap().lock().await; + let client = reqwest::ClientBuilder::new() + .redirect(if config.http.follow_redirects { + let limit = config.http.max_follow_redirects; + Policy::limited(limit.unwrap().into()) + } else { + Policy::none() + }) + .build() + .unwrap(); let res = client .request(request_data.method.to_reqwest(), url) @@ -189,14 +197,7 @@ pub async fn http_service( } }; - if CONFIG - .get() - .unwrap() - .lock() - .await - .http - .fivexx_status_code_down - && (500..599).contains(&res.status().as_u16()) + if config.http.fivexx_status_code_down && (500..599).contains(&res.status().as_u16()) { return MonitorResult::Down(format!("Server replied with status {}", res.status())); } diff --git a/stbmon.toml b/stbmon.toml index 69934c7..09136bb 100644 --- a/stbmon.toml +++ b/stbmon.toml @@ -4,4 +4,6 @@ password_sha256 = "ba01338ba5fa0c1584a6d41f93fe550b1d715a8de2da10d6c673131a85658 allow_guest = true [http] -5xx_status_code_down = true \ No newline at end of file +5xx_status_code_down = true +follow_redirects = false +#max_follow_redirects = 10 \ No newline at end of file