From 32a12ef8234ad19edbfbdf0610d7a98e24b46b45 Mon Sep 17 00:00:00 2001 From: shellrow <81893184+shellrow@users.noreply.github.com> Date: Thu, 5 May 2022 16:30:41 +0900 Subject: [PATCH] Linux support --- src/interface/linux.rs | 22 ++++++++++++++++++++++ src/interface/unix.rs | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/src/interface/linux.rs b/src/interface/linux.rs index bd9fae9..b00b8c4 100644 --- a/src/interface/linux.rs +++ b/src/interface/linux.rs @@ -22,3 +22,25 @@ pub fn get_interface_type(if_name: String) -> InterfaceType { } }; } + +pub fn get_interface_speed(if_name: String) -> Option { + let if_speed_path: String = format!("/sys/class/net/{}/speed", if_name); + let r = read_to_string(if_speed_path); + match r { + Ok(content) => { + let if_speed_string = content.trim().to_string(); + match if_speed_string.parse::() { + Ok(if_speed) => { + // Convert Mbps to bps + return Some(if_speed * 1000000); + }, + Err(_) => { + return None; + } + } + }, + Err(_) => { + return None; + } + }; +} diff --git a/src/interface/unix.rs b/src/interface/unix.rs index 2285ebe..ec5fb4c 100644 --- a/src/interface/unix.rs +++ b/src/interface/unix.rs @@ -95,6 +95,9 @@ pub fn interfaces() -> Vec { }; 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; match local_ip { IpAddr::V4(local_ipv4) => { if iface.ipv4.iter().any(|x| x.addr == local_ipv4) { @@ -248,6 +251,8 @@ pub fn unix_interfaces() -> Vec { ipv4: ini_ipv4, ipv6: ini_ipv6, flags: addr_ref.ifa_flags, + transmit_speed: None, + receive_speed: None, gateway: None, }; let mut found: bool = false;