From 18bba183eec066891067db77066c8dc826039a1c Mon Sep 17 00:00:00 2001 From: Ferdinand Schober Date: Sun, 23 Mar 2025 12:31:47 +0100 Subject: [PATCH] impl change config request --- src/client.rs | 9 +++++++++ src/config.rs | 4 ++++ src/service.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index 19ec65d..b67f787 100644 --- a/src/client.rs +++ b/src/client.rs @@ -15,6 +15,15 @@ pub struct ClientManager { } impl ClientManager { + /// get all clients + pub fn clients(&self) -> Vec<(ClientConfig, ClientState)> { + self.clients + .borrow() + .iter() + .map(|(_, c)| c.clone()) + .collect::>() + } + /// add a new client to this manager pub fn add_client(&self) -> ClientHandle { self.clients.borrow_mut().insert(Default::default()) as ClientHandle diff --git a/src/config.rs b/src/config.rs index 06f9552..caad4d7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -431,4 +431,8 @@ impl Config { .expect("config") .authorized_fingerprints = Some(fingerprints); } + + pub fn write_back(&self) { + todo!() + } } diff --git a/src/service.rs b/src/service.rs index 2ff1e1c..ad83616 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,7 +1,7 @@ use crate::{ capture::{Capture, CaptureType, ICaptureEvent}, client::ClientManager, - config::Config, + config::{Config, ConfigClient}, connect::LanMouseConnection, crypto, dns::{DnsEvent, DnsResolver}, @@ -39,6 +39,8 @@ pub enum ServiceError { } pub struct Service { + /// configuration + config: Config, /// input capture capture: Capture, /// input emulation @@ -122,6 +124,7 @@ impl Service { let port = config.port(); let service = Self { + config, capture, emulation, frontend_listener, @@ -200,10 +203,29 @@ impl Service { FrontendRequest::UpdateEnterHook(handle, enter_hook) => { self.update_enter_hook(handle, enter_hook) } - FrontendRequest::SaveConfiguration => todo!(), + FrontendRequest::SaveConfiguration => self.save_config(), } } + fn save_config(&mut self) { + let clients = self.client_manager.clients(); + let clients = clients + .into_iter() + .map(|(c, s)| ConfigClient { + ips: HashSet::from_iter(c.fix_ips), + hostname: c.hostname, + port: c.port, + pos: c.pos, + active: s.active, + enter_hook: c.cmd, + }) + .collect(); + self.config.set_clients(clients); + let authorized_keys = self.authorized_keys.read().expect("lock").clone(); + self.config.set_authorized_keys(authorized_keys); + self.config.write_back(); + } + async fn handle_frontend_pending(&mut self) { while let Some(event) = self.pending_frontend_events.pop_front() { self.frontend_listener.broadcast(event).await;