Skip to content

Commit

Permalink
make following redirects a config option
Browse files Browse the repository at this point in the history
  • Loading branch information
Paddyk45 committed Feb 8, 2025
1 parent dcfe531 commit e158ce1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u16>,
}

impl Config {
Expand All @@ -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();

Expand Down
23 changes: 12 additions & 11 deletions src/monitor/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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()));
}
Expand Down
4 changes: 3 additions & 1 deletion stbmon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ password_sha256 = "ba01338ba5fa0c1584a6d41f93fe550b1d715a8de2da10d6c673131a85658
allow_guest = true

[http]
5xx_status_code_down = true
5xx_status_code_down = true
follow_redirects = false
#max_follow_redirects = 10

0 comments on commit e158ce1

Please sign in to comment.