replace hand-written uuid check with Uuid::parse_str

This commit is contained in:
Lion Kortlepel 2023-11-23 15:57:50 +01:00
parent 09c4bb8b33
commit 3b61da9edd
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 12 additions and 14 deletions

View File

@ -1,4 +1,5 @@
use serde::Deserialize;
use uuid::Uuid;
#[derive(Deserialize)]
pub struct Config {
@ -46,18 +47,10 @@ pub struct GeneralSettings {
impl GeneralSettings {
pub fn is_auth_key_valid(&self) -> bool {
// Valid key format
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
// -8--------4----4----4----12---------
if self.auth_key.is_none() {return false}
let key = self.auth_key.clone().unwrap();
let key_check: Vec<&str> = key.split("-").collect();
if key_check.len() != 5 {return false}
else if key_check[0].len() != 8 {return false}
else if key_check[1].len() != 4 {return false}
else if key_check[2].len() != 4 {return false}
else if key_check[3].len() != 4 {return false}
else if key_check[4].len() != 12 {return false}
true
return if let Some(auth_key) = &self.auth_key {
Uuid::parse_str(auth_key.as_str()).is_ok()
} else {
false
}
}
}

View File

@ -21,7 +21,12 @@ struct HeartbeatInfo {
pub async fn backend_heartbeat(config: std::sync::Arc<crate::config::Config>, mut hb_rx: Receiver<crate::server::ServerStatus>) {
if !config.general.is_auth_key_valid() {
debug!{"auth_key has invalid format. canceling hearbeat init"};
if config.general.private {
warn!("AuthKey has invalid format. This is not an error, since your server is private.");
} else {
error!("AuthKey has invalid format. The server will not appear on the server list.");
}
// FIXME: The heartbeat should be started if the config is ever changed/reloaded.
return;
}
let mut info = HeartbeatInfo {