From 6d16747c1907428bdde914c71e43914fa221df41 Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Sun, 23 Mar 2025 11:48:39 +0100 Subject: [PATCH] add setters for clients and authorized keys --- lan-mouse-ipc/src/lib.rs | 2 ++ src/config.rs | 49 +++++++++++++++++++++++++++++++++++++++- src/service.rs | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lan-mouse-ipc/src/lib.rs b/lan-mouse-ipc/src/lib.rs index 60c0125..a2c7112 100644 --- a/lan-mouse-ipc/src/lib.rs +++ b/lan-mouse-ipc/src/lib.rs @@ -253,6 +253,8 @@ pub enum FrontendRequest { RemoveAuthorizedKey(String), /// change the hook command UpdateEnterHook(u64, Option), + /// save config file + SaveConfiguration, } #[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Serialize, Deserialize)] diff --git a/src/config.rs b/src/config.rs index c0584cc..06f9552 100644 --- a/src/config.rs +++ b/src/config.rs @@ -44,7 +44,7 @@ fn default_path() -> Result { Ok(PathBuf::from(default_path)) } -#[derive(Serialize, Deserialize, Debug)] +#[derive(Serialize, Deserialize, Debug, Default)] struct ConfigToml { capture_backend: Option, emulation_backend: Option, @@ -274,6 +274,33 @@ impl From for ConfigClient { } } +impl From for TomlClient { + fn from(client: ConfigClient) -> Self { + let hostname = client.hostname; + let host_name = None; + let mut ips = client.ips.into_iter().collect::>(); + ips.sort(); + let ips = Some(ips); + let port = if client.port == DEFAULT_PORT { + None + } else { + Some(client.port) + }; + let position = Some(client.pos); + let activate_on_startup = if client.active { Some(true) } else { None }; + let enter_hook = client.enter_hook; + Self { + hostname, + host_name, + ips, + port, + position, + activate_on_startup, + enter_hook, + } + } +} + #[derive(Debug, Error)] pub enum ConfigError { #[error(transparent)] @@ -384,4 +411,24 @@ impl Config { .and_then(|c| c.release_bind.clone()) .unwrap_or(Vec::from_iter(DEFAULT_RELEASE_KEYS.iter().cloned())) } + + /// set configured clients + pub fn set_clients(&mut self, clients: Vec) { + if self.config_toml.is_none() { + self.config_toml = Default::default(); + } + self.config_toml.as_mut().expect("config").clients = + clients.into_iter().map(|c| c.into()).collect::>(); + } + + /// set authorized keys + pub fn set_authorized_keys(&mut self, fingerprints: HashMap) { + if self.config_toml.is_none() { + self.config_toml = Default::default(); + } + self.config_toml + .as_mut() + .expect("config") + .authorized_fingerprints = Some(fingerprints); + } } diff --git a/src/service.rs b/src/service.rs index f708193..2ff1e1c 100644 --- a/src/service.rs +++ b/src/service.rs @@ -200,6 +200,7 @@ impl Service { FrontendRequest::UpdateEnterHook(handle, enter_hook) => { self.update_enter_hook(handle, enter_hook) } + FrontendRequest::SaveConfiguration => todo!(), } }