mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-04 23:11:29 +03:00
basic saving functionality
This commit is contained in:
committed by
Ferdinand Schober
parent
18bba183ee
commit
a29489cd40
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -1872,6 +1872,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
"toml",
|
"toml",
|
||||||
|
"toml_edit",
|
||||||
"webrtc-dtls",
|
"webrtc-dtls",
|
||||||
"webrtc-util",
|
"webrtc-util",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ shadow-rs = { version = "1.2.0", features = ["metadata"] }
|
|||||||
|
|
||||||
hickory-resolver = "0.25.2"
|
hickory-resolver = "0.25.2"
|
||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
|
toml_edit = { version = "0.22", features = ["serde"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
env_logger = "0.11.3"
|
env_logger = "0.11.3"
|
||||||
|
|||||||
@@ -71,6 +71,8 @@ enum CliSubcommand {
|
|||||||
},
|
},
|
||||||
/// deauthorize a public key
|
/// deauthorize a public key
|
||||||
RemoveAuthorizedKey { sha256_fingerprint: String },
|
RemoveAuthorizedKey { sha256_fingerprint: String },
|
||||||
|
/// save configuration to file
|
||||||
|
SaveConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run(args: CliArgs) -> Result<(), CliError> {
|
pub async fn run(args: CliArgs) -> Result<(), CliError> {
|
||||||
@@ -162,6 +164,7 @@ async fn execute(cmd: CliSubcommand) -> Result<(), CliError> {
|
|||||||
tx.request(FrontendRequest::RemoveAuthorizedKey(sha256_fingerprint))
|
tx.request(FrontendRequest::RemoveAuthorizedKey(sha256_fingerprint))
|
||||||
.await?
|
.await?
|
||||||
}
|
}
|
||||||
|
CliSubcommand::SaveConfig => tx.request(FrontendRequest::SaveConfiguration).await?,
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::{collections::HashSet, io};
|
use std::{collections::HashSet, io};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use toml;
|
use toml;
|
||||||
|
use toml_edit::{self, DocumentMut};
|
||||||
|
|
||||||
use lan_mouse_cli::CliArgs;
|
use lan_mouse_cli::CliArgs;
|
||||||
use lan_mouse_ipc::{DEFAULT_PORT, Position};
|
use lan_mouse_ipc::{DEFAULT_PORT, Position};
|
||||||
@@ -44,7 +45,7 @@ fn default_path() -> Result<PathBuf, VarError> {
|
|||||||
Ok(PathBuf::from(default_path))
|
Ok(PathBuf::from(default_path))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
||||||
struct ConfigToml {
|
struct ConfigToml {
|
||||||
capture_backend: Option<CaptureBackend>,
|
capture_backend: Option<CaptureBackend>,
|
||||||
emulation_backend: Option<EmulationBackend>,
|
emulation_backend: Option<EmulationBackend>,
|
||||||
@@ -414,15 +415,21 @@ impl Config {
|
|||||||
|
|
||||||
/// set configured clients
|
/// set configured clients
|
||||||
pub fn set_clients(&mut self, clients: Vec<ConfigClient>) {
|
pub fn set_clients(&mut self, clients: Vec<ConfigClient>) {
|
||||||
|
if clients.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if self.config_toml.is_none() {
|
if self.config_toml.is_none() {
|
||||||
self.config_toml = Default::default();
|
self.config_toml = Default::default();
|
||||||
}
|
}
|
||||||
self.config_toml.as_mut().expect("config").clients =
|
self.config_toml.as_mut().expect("config").clients =
|
||||||
clients.into_iter().map(|c| c.into()).collect::<Vec<_>>();
|
Some(clients.into_iter().map(|c| c.into()).collect::<Vec<_>>());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// set authorized keys
|
/// set authorized keys
|
||||||
pub fn set_authorized_keys(&mut self, fingerprints: HashMap<String, String>) {
|
pub fn set_authorized_keys(&mut self, fingerprints: HashMap<String, String>) {
|
||||||
|
if fingerprints.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if self.config_toml.is_none() {
|
if self.config_toml.is_none() {
|
||||||
self.config_toml = Default::default();
|
self.config_toml = Default::default();
|
||||||
}
|
}
|
||||||
@@ -432,7 +439,24 @@ impl Config {
|
|||||||
.authorized_fingerprints = Some(fingerprints);
|
.authorized_fingerprints = Some(fingerprints);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_back(&self) {
|
pub fn write_back(&self) -> Result<(), io::Error> {
|
||||||
todo!()
|
log::info!("writing config to {:?}", &self.config_path);
|
||||||
|
/* load the current configuration file */
|
||||||
|
let current_config = fs::read_to_string(&self.config_path)?;
|
||||||
|
let current_config = current_config.parse::<DocumentMut>().expect("fix me");
|
||||||
|
let _current_config =
|
||||||
|
toml_edit::de::from_document::<ConfigToml>(current_config).expect("fixme");
|
||||||
|
|
||||||
|
/* the new config */
|
||||||
|
let new_config = self.config_toml.clone().unwrap_or_default();
|
||||||
|
// let new_config = toml_edit::ser::to_document::<ConfigToml>(&new_config).expect("fixme");
|
||||||
|
let new_config = toml_edit::ser::to_string_pretty(&new_config).expect("todo");
|
||||||
|
|
||||||
|
/* TODO merge documents */
|
||||||
|
|
||||||
|
/* write new config to file */
|
||||||
|
fs::write(&self.config_path, new_config)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -223,7 +223,9 @@ impl Service {
|
|||||||
self.config.set_clients(clients);
|
self.config.set_clients(clients);
|
||||||
let authorized_keys = self.authorized_keys.read().expect("lock").clone();
|
let authorized_keys = self.authorized_keys.read().expect("lock").clone();
|
||||||
self.config.set_authorized_keys(authorized_keys);
|
self.config.set_authorized_keys(authorized_keys);
|
||||||
self.config.write_back();
|
if let Err(e) = self.config.write_back() {
|
||||||
|
log::warn!("failed to write config: {e}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_frontend_pending(&mut self) {
|
async fn handle_frontend_pending(&mut self) {
|
||||||
|
|||||||
Reference in New Issue
Block a user