Skip to content

Commit fa8e4e0

Browse files
authored
feat: Sync system's connection state on windows (#346)
* sync client connections on windows * add target_os to imports * add target_os to more imports * change log message
1 parent b129b19 commit fa8e4e0

4 files changed

Lines changed: 351 additions & 14 deletions

File tree

‎src-tauri/Cargo.lock‎

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src-tauri/Cargo.toml‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ serde_json = "1.0"
3636
serde = { version = "1.0", features = ["derive"] }
3737
serde_with = "3.9"
3838
sqlx = { version = "0.8", features = [
39-
"chrono",
40-
"sqlite",
41-
"runtime-tokio",
42-
"uuid",
43-
"macros",
39+
"chrono",
40+
"sqlite",
41+
"runtime-tokio",
42+
"uuid",
43+
"macros",
4444
] }
4545
struct-patch = "0.4"
4646
strum = { version = "0.26", features = ["derive"] }
4747
tauri = { version = "1.8", features = [
48-
"dialog-all",
49-
"clipboard-all",
50-
"http-all",
51-
"window-all",
52-
"system-tray",
53-
"native-tls-vendored",
54-
"icon-png",
55-
"fs-all",
48+
"dialog-all",
49+
"clipboard-all",
50+
"http-all",
51+
"window-all",
52+
"system-tray",
53+
"native-tls-vendored",
54+
"icon-png",
55+
"fs-all",
5656
] }
5757
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
5858
tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
@@ -70,6 +70,8 @@ x25519-dalek = { version = "2", features = ["getrandom", "static_secrets"] }
7070

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

7476
[target.'cfg(target_os = "macos")'.dependencies]
7577
nix = { version = "0.29", features = ["net"] }

‎src-tauri/src/bin/defguard-client.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

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

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

224+
// Sync already active connections on windows.
225+
// When windows is restarted, the app doesn't close the active connections
226+
// and they are still running after the restart. We sync them here to
227+
// reflect the real system's state.
228+
// TODO: Find a way to intercept the shutdown event and close all connections
229+
#[cfg(target_os = "windows")]
230+
{
231+
match sync_connections(&app_handle).await {
232+
Ok(_) => {
233+
info!("Synchronized application's active connections with the connections already open on the system, if there were any.")
234+
}
235+
Err(e) => {
236+
warn!(
237+
"Failed to synchronize application's active connections with the connections already open on the system. \
238+
The connections' state in the application may not reflect system's state. Reconnect manually to reset them. Error details: {}",
239+
e
240+
)
241+
}
242+
};
243+
}
244+
222245
// configure tray
223246
debug!("Configuring tray icon...");
224247
if let Ok(settings) = Settings::get(&app_state.get_pool()).await {

0 commit comments

Comments
 (0)