Skip to content

Commit

Permalink
feat: Sync system's connection state on windows (#346)
Browse files Browse the repository at this point in the history
* sync client connections on windows

* add target_os to imports

* add target_os to more imports

* change log message
  • Loading branch information
t-aleksander authored Oct 29, 2024
1 parent b129b19 commit fa8e4e0
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 14 deletions.
10 changes: 9 additions & 1 deletion src-tauri/Cargo.lock

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

28 changes: 15 additions & 13 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
serde_with = "3.9"
sqlx = { version = "0.8", features = [
"chrono",
"sqlite",
"runtime-tokio",
"uuid",
"macros",
"chrono",
"sqlite",
"runtime-tokio",
"uuid",
"macros",
] }
struct-patch = "0.4"
strum = { version = "0.26", features = ["derive"] }
tauri = { version = "1.8", features = [
"dialog-all",
"clipboard-all",
"http-all",
"window-all",
"system-tray",
"native-tls-vendored",
"icon-png",
"fs-all",
"dialog-all",
"clipboard-all",
"http-all",
"window-all",
"system-tray",
"native-tls-vendored",
"icon-png",
"fs-all",
] }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
Expand All @@ -70,6 +70,8 @@ x25519-dalek = { version = "2", features = ["getrandom", "static_secrets"] }

[target.'cfg(target_os = "windows")'.dependencies]
windows-service = "0.7"
winapi = { version = "0.3", features = ["winsvc", "winerror"] }
widestring = "0.4"

[target.'cfg(target_os = "macos")'.dependencies]
nix = { version = "0.29", features = ["net"] }
Expand Down
23 changes: 23 additions & 0 deletions src-tauri/src/bin/defguard-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use std::{env, str::FromStr};

#[cfg(target_os = "windows")]
use defguard_client::utils::sync_connections;
use defguard_client::{
__cmd__active_connection, __cmd__all_connections, __cmd__all_instances, __cmd__all_locations,
__cmd__all_tunnels, __cmd__connect, __cmd__delete_instance, __cmd__delete_tunnel,
Expand Down Expand Up @@ -219,6 +221,27 @@ async fn main() {
*app_state.db.lock().unwrap() = Some(db);
debug!("Database setup has been completed successfully.");

// Sync already active connections on windows.
// When windows is restarted, the app doesn't close the active connections
// and they are still running after the restart. We sync them here to
// reflect the real system's state.
// TODO: Find a way to intercept the shutdown event and close all connections
#[cfg(target_os = "windows")]
{
match sync_connections(&app_handle).await {
Ok(_) => {
info!("Synchronized application's active connections with the connections already open on the system, if there were any.")
}
Err(e) => {
warn!(
"Failed to synchronize application's active connections with the connections already open on the system. \
The connections' state in the application may not reflect system's state. Reconnect manually to reset them. Error details: {}",
e
)
}
};
}

// configure tray
debug!("Configuring tray icon...");
if let Ok(settings) = Settings::get(&app_state.get_pool()).await {
Expand Down
Loading

0 comments on commit fa8e4e0

Please sign in to comment.