mangadex-home-rs/src/stop.rs

49 lines
1.3 KiB
Rust
Raw Normal View History

2021-07-20 20:47:04 +00:00
#![cfg(not(tarpaulin_include))]
2021-05-12 01:01:01 +00:00
use reqwest::StatusCode;
2021-03-22 21:47:56 +00:00
use serde::Serialize;
2021-07-13 03:23:51 +00:00
use tracing::{info, warn};
2021-03-22 21:47:56 +00:00
2021-07-15 16:29:55 +00:00
use crate::client::HTTP_CLIENT;
2021-07-09 23:48:25 +00:00
use crate::config::ClientSecret;
2021-07-15 16:37:55 +00:00
const CONTROL_CENTER_STOP_URL: &str = "https://api.mangadex.network/stop";
2021-03-22 21:47:56 +00:00
#[derive(Serialize)]
struct StopRequest<'a> {
2021-07-09 23:48:25 +00:00
secret: &'a ClientSecret,
2021-03-22 21:47:56 +00:00
}
2021-07-09 23:48:25 +00:00
pub async fn send_stop(secret: &ClientSecret) {
2021-07-15 16:29:55 +00:00
match HTTP_CLIENT
.inner()
2021-03-22 21:47:56 +00:00
.post(CONTROL_CENTER_STOP_URL)
2021-07-15 16:29:55 +00:00
.json(&StopRequest { secret })
2021-03-22 21:47:56 +00:00
.send()
.await
{
Ok(resp) => {
2021-05-12 01:01:01 +00:00
if resp.status() == StatusCode::OK {
2021-03-23 03:19:56 +00:00
info!("Successfully sent stop message to control center.");
2021-03-22 21:47:56 +00:00
} else {
warn!("Got weird response from server: {:?}", resp.headers());
}
}
Err(e) => warn!("Got error while sending stop message: {}", e),
}
2021-03-18 01:45:16 +00:00
}
2021-07-16 19:26:18 +00:00
#[cfg(test)]
mod stop {
use super::CONTROL_CENTER_STOP_URL;
#[test]
fn stop_url_does_not_have_ping_in_url() {
// This looks like a dumb test, yes, but it ensures that clients don't
// get marked compromised because apparently just sending a json obj
// with just the secret is acceptable to the ping endpoint, which messes
// up non-trivial client configs.
assert!(!CONTROL_CENTER_STOP_URL.contains("ping"))
}
}