diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index fad586f6..cb8371b1 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -75,6 +75,54 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" + +[[package]] +name = "anstyle-parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.75" @@ -579,6 +627,46 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "clap" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "syn 2.0.38", +] + +[[package]] +name = "clap_lex" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" + [[package]] name = "cocoa" version = "0.24.1" @@ -615,6 +703,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "combine" version = "4.6.6" @@ -864,6 +958,7 @@ dependencies = [ "anyhow", "base64 0.21.5", "chrono", + "clap", "defguard_wireguard_rs", "dirs", "lazy_static", @@ -5062,6 +5157,12 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1" +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "uuid" version = "1.5.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 3c762c1d..9da1e3f1 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -19,6 +19,7 @@ prost-build = { version = "0.12" } [dependencies] anyhow = "1.0.75" base64 = "0.21" +clap = { version = "4.4", features = ["derive", "env"] } chrono = { version = "0.4", features = ["serde"] } defguard_wireguard_rs = { git = "https://github.com/DefGuard/wireguard-rs.git", branch = "main" } dirs = "5.0" diff --git a/src-tauri/src/bin/defguard-service.rs b/src-tauri/src/bin/defguard-service.rs index df12d2d8..831cbfea 100644 --- a/src-tauri/src/bin/defguard-service.rs +++ b/src-tauri/src/bin/defguard-service.rs @@ -3,7 +3,8 @@ //! This binary is meant to run as a daemon with root privileges //! and communicate with the desktop client over HTTP. -use defguard_client::service::run_server; +use clap::Parser; +use defguard_client::service::{config::Config, run_server}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] @@ -18,8 +19,11 @@ async fn main() -> anyhow::Result<()> { .with(tracing_subscriber::fmt::layer()) .init(); + // parse config + let config = Config::parse(); + // run gRPC server - run_server().await?; + run_server(config).await?; Ok(()) } diff --git a/src-tauri/src/service/config.rs b/src-tauri/src/service/config.rs new file mode 100644 index 00000000..7cc678e5 --- /dev/null +++ b/src-tauri/src/service/config.rs @@ -0,0 +1,10 @@ +use clap::Parser; + +#[derive(Debug, Parser, Clone)] +#[clap(about = "Defguard VPN gateway service")] +#[command(version)] +pub struct Config { + /// Defines how often (in seconds) interface statistics are sent to Defguard client + #[arg(long, short = 'p', env = "DEFGUARD_STATS_PERIOD", default_value = "10")] + pub stats_period: u64, +} diff --git a/src-tauri/src/service/mod.rs b/src-tauri/src/service/mod.rs index 607400c0..1a373485 100644 --- a/src-tauri/src/service/mod.rs +++ b/src-tauri/src/service/mod.rs @@ -28,9 +28,12 @@ use proto::{ CreateInterfaceRequest, InterfaceData, ReadInterfaceDataRequest, RemoveInterfaceRequest, }; +use self::config::Config; + +pub mod config; + pub const DAEMON_HTTP_PORT: u16 = 54127; pub const DAEMON_BASE_URL: &str = "http://localhost:54127"; -const STATS_PERIOD: u64 = 60; #[derive(thiserror::Error, Debug)] pub enum DaemonError { @@ -43,7 +46,17 @@ pub enum DaemonError { } #[derive(Default)] -pub struct DaemonService {} +pub struct DaemonService { + stats_period: u64, +} + +impl DaemonService { + pub fn new(config: Config) -> Self { + Self { + stats_period: config.stats_period, + } + } +} type InterfaceDataStream = Pin> + Send>>; @@ -135,13 +148,14 @@ impl DesktopDaemonService for DaemonService { let ifname = request.interface_name; info!("Starting interface data stream for {ifname}"); + let stats_period = self.stats_period; let (tx, rx) = mpsc::channel(64); tokio::spawn(async move { info!("Spawning stats thread for interface {ifname}"); // setup WireGuard API let wgapi = setup_wgapi(ifname.clone()).expect("Failed to initialize WireGuard interface API"); - let period = Duration::from_secs(STATS_PERIOD); + let period = Duration::from_secs(stats_period); let mut interval = interval(period); loop { @@ -172,13 +186,13 @@ impl DesktopDaemonService for DaemonService { } } -pub async fn run_server() -> anyhow::Result<()> { +pub async fn run_server(config: Config) -> anyhow::Result<()> { info!("Starting defguard interface management daemon"); let addr = format!("127.0.0.1:{DAEMON_HTTP_PORT}") .parse() .context("Failed to parse gRPC address")?; - let daemon_service = DaemonService::default(); + let daemon_service = DaemonService::new(config); info!("defguard daemon listening on {addr}");