From 980722c6cb1da6fc7e1df7c4943289f0760bef96 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 12 Feb 2023 18:14:22 +0100 Subject: [PATCH] Begin add uds_windows (Not working) + add config load --- Cargo.lock | 11 +++++++++++ Cargo.toml | 3 +++ src/daemon/configuration/mod.rs | 8 ++++++++ src/daemon/main.rs | 17 +++++++++-------- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 318c317..80d7071 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -139,6 +139,7 @@ dependencies = [ "tonic", "tonic-build", "tower", + "uds_windows", "uuid", ] @@ -959,6 +960,16 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" +[[package]] +name = "uds_windows" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce65604324d3cce9b966701489fbd0cf318cb1f7bd9dd07ac9a4ee6fb791930d" +dependencies = [ + "tempfile", + "winapi", +] + [[package]] name = "unicode-ident" version = "1.0.6" diff --git a/Cargo.toml b/Cargo.toml index 87c3163..6e8ecc7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,5 +34,8 @@ tower = "0.4" # Required for tonic with unix socket prost = "0.11" # Required for tonic tonic = "0.8" +[target.'cfg(windows)'.dependencies] +uds_windows = "1.0" + [build-dependencies] tonic-build = { version = "0.8", features = ["prost"] } # Required for tonic \ No newline at end of file diff --git a/src/daemon/configuration/mod.rs b/src/daemon/configuration/mod.rs index ee1412e..8f83f1f 100644 --- a/src/daemon/configuration/mod.rs +++ b/src/daemon/configuration/mod.rs @@ -8,6 +8,14 @@ pub struct Configuration { } impl Configuration { + pub fn read_or_create() -> Self { + let path = std::path::Path::new("configuration.yml"); + let file = std::fs::File::open(path).unwrap(); + let buffer = std::io::BufReader::new(file); + use std::io::Read; + serde_yaml::from_reader(buffer).unwrap() + } + pub fn command_allowed(&self, command: &str) -> bool { self.whitelist == command } diff --git a/src/daemon/main.rs b/src/daemon/main.rs index 258d494..434af7e 100644 --- a/src/daemon/main.rs +++ b/src/daemon/main.rs @@ -6,6 +6,7 @@ mod sessions; mod configuration; pub(self) static SESSIONS : Mutex> = Mutex::new(Vec::new()); +pub(self) static CONFIGURATION : Mutex> = Mutex::new(None); pub use sessions::{ get_sessions_lock, @@ -14,20 +15,25 @@ pub use sessions::{ use std::path::Path; use std::time::Duration; -#[cfg(unix)] -use tokio::net::UnixListener; #[cfg(unix)] use tokio_stream::wrappers::UnixListenerStream; use tonic::transport::Server; +#[cfg(unix)] +use tokio::net::UnixListener; + +#[cfg(windows)] +use uds_windows::UnixListener; + use libcommand::interpreter::unix_server::UnixServer; -#[cfg(unix)] #[tokio::main] async fn main() -> Result<(), Box> { std::fs::create_dir_all(Path::new(libcommand::SOCK_FILE).parent().unwrap())?; + *CONFIGURATION.get_mut().unwrap() = Some(configuration::Configuration::read_or_create()); + let server = server::DaemonServer::default(); let uds = UnixListener::bind(libcommand::SOCK_FILE)?; @@ -51,9 +57,4 @@ async fn main() -> Result<(), Box> { .await?; Ok(()) -} - -#[cfg(not(unix))] -fn main() { - panic!("The `uds` example only works on unix systems!"); } \ No newline at end of file