From 09c4bb8b334107f4807d01450c4610652cb4cc97 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 23 Nov 2023 15:57:32 +0100 Subject: [PATCH 1/2] add uuid package to validate uuids for the AuthKey --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + 2 files changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 3e60eed..61c9ed9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -177,6 +177,7 @@ dependencies = [ "serde_json", "tokio", "toml", + "uuid", ] [[package]] @@ -1643,6 +1644,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "uuid" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 4f3b6dd..3f0a62d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,4 @@ mlua = { version = "0.9.1", features = ["lua54", "vendored", "send"] } ratatui = "0.24.0" crossterm = "0.27.0" +uuid = "1.6.1" From 3b61da9edd9712f9fe1d3a2d831f8909d7b4697b Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 23 Nov 2023 15:57:50 +0100 Subject: [PATCH 2/2] replace hand-written uuid check with Uuid::parse_str --- src/config.rs | 19 ++++++------------- src/heartbeat.rs | 7 ++++++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index 899783e..1d10284 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 + } } } \ No newline at end of file diff --git a/src/heartbeat.rs b/src/heartbeat.rs index cf02ae4..8fa9103 100644 --- a/src/heartbeat.rs +++ b/src/heartbeat.rs @@ -21,7 +21,12 @@ struct HeartbeatInfo { pub async fn backend_heartbeat(config: std::sync::Arc, mut hb_rx: Receiver) { 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 {