From 04f051a81ea586f80ab4f0ee843a8f41511adc50 Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Sat, 7 Jun 2025 15:35:45 +0000 Subject: [PATCH 1/3] fix: always populate interface fields on linux --- src/interface/unix.rs | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/interface/unix.rs b/src/interface/unix.rs index 62c419c..8db2ec8 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -88,42 +88,51 @@ pub fn interfaces() -> Vec { #[cfg(any(target_os = "linux", target_os = "android"))] pub fn interfaces() -> Vec { + #[cfg(feature = "gateway")] + use std::collections::HashMap; + #[cfg(feature = "gateway")] + use crate::NetworkDevice; + use super::linux; let mut interfaces: Vec = unix_interfaces(); #[cfg(feature = "gateway")] - let local_ip: IpAddr = match super::get_local_ipaddr() { - Some(local_ip) => local_ip, - None => return interfaces, - }; + let local_ip_opt: Option = super::get_local_ipaddr(); + #[cfg(feature = "gateway")] - let gateway_map = gateway::linux::get_gateway_map(); + let gateway_map: HashMap = gateway::linux::get_gateway_map(); + for iface in &mut interfaces { iface.if_type = linux::get_interface_type(iface.name.clone()); let if_speed: Option = linux::get_interface_speed(iface.name.clone()); iface.transmit_speed = if_speed; iface.receive_speed = if_speed; + #[cfg(feature = "gateway")] if let Some(gateway) = gateway_map.get(&iface.name) { iface.gateway = Some(gateway.clone()); } + #[cfg(feature = "gateway")] - match local_ip { - IpAddr::V4(local_ipv4) => { - if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) { - iface.default = true; - iface.dns_servers = get_system_dns_conf(); + if let Some(local_ip) = local_ip_opt { + match local_ip { + IpAddr::V4(local_ipv4) => { + if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) { + iface.default = true; + iface.dns_servers = get_system_dns_conf(); + } } - } - IpAddr::V6(local_ipv6) => { - if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) { - iface.default = true; - iface.dns_servers = get_system_dns_conf(); + IpAddr::V6(local_ipv6) => { + if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) { + iface.default = true; + iface.dns_servers = get_system_dns_conf(); + } } } } } + interfaces } From 4ca40729f830947c6566f43ba6d2c56dc1cd1a5c Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Sat, 7 Jun 2025 15:59:43 +0000 Subject: [PATCH 2/3] refactor: avoid clone in iface funcs --- src/interface/linux.rs | 4 ++-- src/interface/unix.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/interface/linux.rs b/src/interface/linux.rs index 0dbabd7..677713c 100644 --- a/src/interface/linux.rs +++ b/src/interface/linux.rs @@ -19,7 +19,7 @@ pub fn is_virtual_interface(interface_name: &str) -> bool { } } -pub fn get_interface_type(if_name: String) -> InterfaceType { +pub fn get_interface_type(if_name: &str) -> InterfaceType { let if_type_path: String = format!("/sys/class/net/{}/type", if_name); let r = read_to_string(if_type_path); match r { @@ -50,7 +50,7 @@ pub fn get_interface_type(if_name: String) -> InterfaceType { }; } -pub fn get_interface_speed(if_name: String) -> Option { +pub fn get_interface_speed(if_name: &str) -> Option { let if_speed_path: String = format!("/sys/class/net/{}/speed", if_name); let r = read_to_string(if_speed_path); match r { diff --git a/src/interface/unix.rs b/src/interface/unix.rs index 8db2ec8..4d14c28 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -104,8 +104,8 @@ pub fn interfaces() -> Vec { let gateway_map: HashMap = gateway::linux::get_gateway_map(); for iface in &mut interfaces { - iface.if_type = linux::get_interface_type(iface.name.clone()); - let if_speed: Option = linux::get_interface_speed(iface.name.clone()); + iface.if_type = linux::get_interface_type(&iface.name); + let if_speed: Option = linux::get_interface_speed(&iface.name); iface.transmit_speed = if_speed; iface.receive_speed = if_speed; From 2f3a337b5c2578473483f31e7e5c7bdaca3227fc Mon Sep 17 00:00:00 2001 From: shellrow Date: Sun, 8 Jun 2025 10:21:31 +0900 Subject: [PATCH 3/3] format with cargo fmt --- src/interface/unix.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/interface/unix.rs b/src/interface/unix.rs index 4d14c28..22addf0 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -88,10 +88,10 @@ pub fn interfaces() -> Vec { #[cfg(any(target_os = "linux", target_os = "android"))] pub fn interfaces() -> Vec { - #[cfg(feature = "gateway")] - use std::collections::HashMap; #[cfg(feature = "gateway")] use crate::NetworkDevice; + #[cfg(feature = "gateway")] + use std::collections::HashMap; use super::linux;