Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 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/client-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ owo-colors = { version = "4", features = ["supports-colors"] }

common = { package = "defguard-client-common", path = "../common" }
defguard_core = { package = "defguard-client-core", path = "../core" }
defguard_client_config_sync = { package = "defguard-client-config-sync", path = "../enterprise/config-sync" }
defguard_client_posture = { package = "defguard-client-posture", path = "../enterprise/posture" }
defguard_client_proto = { package = "defguard-client-proto", path = "../client-proto" }
base64.workspace = true
Expand Down
1 change: 1 addition & 0 deletions src-tauri/client-cli/src/brand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub fn print_banner() {
#[cfg(windows)]
pub fn print_banner() {
let project = common::version_string("defguard-cli");
println!();
println!(" {project}");
println!(" {COPYRIGHT}");
println!();
Expand Down
84 changes: 84 additions & 0 deletions src-tauri/client-cli/src/config_poll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::collections::{HashMap, HashSet};

use defguard_client_config_sync::{poll_instances, PollInstanceResult};
use defguard_core::{
connection::active_state::active_state,
database::models::{location::Location, Id},
error::Error,
ConnectionType,
};
use tracing::debug;

use crate::state::State;

pub async fn poll_config(state: &State) {
let active_instance_ids = match active_instance_ids(state).await {
Ok(ids) => ids,
Err(err) => {
debug!("Skipping configuration polling, failed to detect active connections: {err}");
return;
}
};

let outcomes = match poll_instances(&state.pool, &active_instance_ids).await {
Ok(outcomes) => outcomes,
Err(err) => {
debug!("Skipping configuration polling: {err}");
return;
}
};

for outcome in outcomes {
match outcome.result {
Ok(PollInstanceResult::ChangedWhileActive { .. }) => {
eprintln!(
Comment thread
j-chmielewski marked this conversation as resolved.
"Instance {} configuration changed, disconnect to apply changes",
outcome.instance_name
);
}
Ok(PollInstanceResult::Updated { .. } | PollInstanceResult::Unchanged { .. }) => {}
Err(Error::CoreNotEnterprise) => {
debug!(
"Instance {} is not enterprise, skipping configuration polling",
outcome.instance_name
);
}
Err(Error::NoToken) => {
debug!(
"Instance {} has no polling token, skipping configuration polling",
outcome.instance_name
);
}
Err(err) => {
debug!(
"Failed to poll configuration for instance {}: {err}",
outcome.instance_name
);
}
}
}
}

async fn active_instance_ids(state: &State) -> Result<HashSet<Id>, Error> {
let active_location_ids = active_state(&state.pool)
.await?
.into_iter()
.filter(|connection| connection.connection_type == ConnectionType::Location)
.map(|connection| connection.target_id)
.collect::<HashSet<_>>();

if active_location_ids.is_empty() {
return Ok(HashSet::new());
}

let location_instances = Location::all(&state.pool, false)
.await?
.into_iter()
.map(|location| (location.id, location.instance_id))
.collect::<HashMap<_, _>>();

Ok(active_location_ids
.into_iter()
.filter_map(|location_id| location_instances.get(&location_id).copied())
.collect())
}
3 changes: 3 additions & 0 deletions src-tauri/client-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use common::check_version_flag;
mod brand;
mod cli;
mod commands;
mod config_poll;
mod exit;
mod logging;
mod mfa;
Expand Down Expand Up @@ -51,6 +52,8 @@ async fn main() -> ExitCode {
}
};

config_poll::poll_config(&state).await;

// Dispatch command.
match cli.command {
Commands::List => output::finish(list::handle(&state).await, cli.json),
Expand Down
Loading
Loading