Skip to content

Commit

Permalink
add CLI config options
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Wójcik committed Nov 8, 2023
1 parent 8fb812c commit b787f5d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 7 deletions.
101 changes: 101 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions src-tauri/src/bin/defguard-service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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(())
}
10 changes: 10 additions & 0 deletions src-tauri/src/service/config.rs
Original file line number Diff line number Diff line change
@@ -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,
}
24 changes: 19 additions & 5 deletions src-tauri/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<Box<dyn Stream<Item = Result<InterfaceData, Status>> + Send>>;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}");

Expand Down

0 comments on commit b787f5d

Please sign in to comment.