From 93e5f849e7a9a685d0f61f0734d12e53e0e034a2 Mon Sep 17 00:00:00 2001 From: lurius Date: Mon, 9 Dec 2024 10:17:06 +0100 Subject: [PATCH] fix(#294): issue when changing from wlan to lan --- .../modules/network/application/mod.rs | 34 ++++++++++++++++--- .../modules/network/infrastructure.rs | 5 ++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/background/modules/network/application/mod.rs b/src/background/modules/network/application/mod.rs index 1f682c15..94075b6e 100644 --- a/src/background/modules/network/application/mod.rs +++ b/src/background/modules/network/application/mod.rs @@ -1,6 +1,9 @@ pub mod scanner; -use std::{env::temp_dir, net::UdpSocket}; +use std::{ + env::temp_dir, + net::{IpAddr, UdpSocket}, +}; use tauri_plugin_shell::ShellExt; use windows::Win32::{ @@ -9,7 +12,10 @@ use windows::Win32::{ IP_ADAPTER_ADDRESSES_LH, }, Networking::{ - NetworkListManager::{INetworkListManager, NetworkListManager, NLM_CONNECTIVITY}, + NetworkListManager::{ + INetworkListManager, NetworkListManager, NLM_CONNECTIVITY, + NLM_CONNECTIVITY_IPV4_INTERNET, NLM_CONNECTIVITY_IPV6_INTERNET, + }, WinSock::AF_UNSPEC, }, }; @@ -71,18 +77,33 @@ impl NetworkManager { /// emit connectivity changes, always will emit the current state on registration pub fn register_events(cb: F) where - F: Fn(NLM_CONNECTIVITY) + Send + 'static, + F: Fn(NLM_CONNECTIVITY, String) + Send + 'static, { spawn_named_thread("Network Manager", move || { let result: Result<()> = Com::run_with_context(|| { let list_manager: INetworkListManager = Com::create_instance(&NetworkListManager)?; let mut last_state = None; + let mut last_ip = None; loop { let current_state = unsafe { list_manager.GetConnectivity() }.ok(); if let (Some(current_state), Some(last_state)) = (current_state, last_state) { if current_state != last_state { - cb(current_state); + last_ip = get_local_ip_address_base().ok(); + cb(current_state, last_ip.unwrap().to_string()); + } else if current_state.0 & NLM_CONNECTIVITY_IPV4_INTERNET.0 + == NLM_CONNECTIVITY_IPV4_INTERNET.0 + || current_state.0 & NLM_CONNECTIVITY_IPV6_INTERNET.0 + == NLM_CONNECTIVITY_IPV6_INTERNET.0 + { + let current_ip = get_local_ip_address_base().ok(); + if let (Some(current_ip), Some(last_ip)) = (current_ip, last_ip) { + if current_ip != last_ip { + cb(current_state, current_ip.to_string()) + } + } + + last_ip = current_ip; } } last_state = current_state; @@ -166,8 +187,11 @@ impl NetworkManager { } pub fn get_local_ip_address() -> Result { + Ok(get_local_ip_address_base()?.to_string()) +} +fn get_local_ip_address_base() -> Result { let socket = UdpSocket::bind("0.0.0.0:0")?; socket.connect("8.8.8.8:80")?; let local_addr = socket.local_addr()?; - Ok(local_addr.ip().to_string()) + Ok(local_addr.ip()) } diff --git a/src/background/modules/network/infrastructure.rs b/src/background/modules/network/infrastructure.rs index 68f586e0..941937c4 100644 --- a/src/background/modules/network/infrastructure.rs +++ b/src/background/modules/network/infrastructure.rs @@ -29,10 +29,9 @@ pub fn register_network_events() -> Result<()> { if !REGISTERED.load(Ordering::Acquire) { REGISTERED.store(true, Ordering::Release); log::trace!("Registering network events"); - NetworkManager::register_events(move |connectivity| { + NetworkManager::register_events(move |connectivity, ip| { log::trace!(target: "network", "Connectivity changed: {:?}", connectivity); - if let (Ok(ip), Ok(adapters)) = (get_local_ip_address(), NetworkManager::get_adapters()) - { + if let Ok(adapters) = NetworkManager::get_adapters() { let has_internet_ipv4 = connectivity.0 & NLM_CONNECTIVITY_IPV4_INTERNET.0 == NLM_CONNECTIVITY_IPV4_INTERNET.0; let has_internet_ipv6 = connectivity.0 & NLM_CONNECTIVITY_IPV6_INTERNET.0