Skip to content

Commit

Permalink
fix(#294): issue when changing from wlan to lan
Browse files Browse the repository at this point in the history
  • Loading branch information
luriusTM authored Dec 9, 2024
1 parent e16d076 commit 93e5f84
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
34 changes: 29 additions & 5 deletions src/background/modules/network/application/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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,
},
};
Expand Down Expand Up @@ -71,18 +77,33 @@ impl NetworkManager {
/// emit connectivity changes, always will emit the current state on registration
pub fn register_events<F>(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;
Expand Down Expand Up @@ -166,8 +187,11 @@ impl NetworkManager {
}

pub fn get_local_ip_address() -> Result<String> {
Ok(get_local_ip_address_base()?.to_string())
}
fn get_local_ip_address_base() -> Result<IpAddr> {
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())
}
5 changes: 2 additions & 3 deletions src/background/modules/network/infrastructure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 93e5f84

Please sign in to comment.