From 23502e6c5381a3f882305255e987a9b1756ecf59 Mon Sep 17 00:00:00 2001 From: Atsukoro1 Date: Sat, 3 Dec 2022 21:30:36 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Ip=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/system/net.rs | 148 +++++++++++++++++----------------------------- 1 file changed, 54 insertions(+), 94 deletions(-) diff --git a/src/system/net.rs b/src/system/net.rs index d1e4356..d1ec04c 100644 --- a/src/system/net.rs +++ b/src/system/net.rs @@ -28,104 +28,64 @@ pub fn get_ipaddr() -> String { #[cfg(target_os = "linux")] pub fn get_ipaddr() -> String { - let mut final_str = String::new(); - let mut selected_intr = String::new(); - const KNOWN_INTERFACES: [&str; 10] = [ - "enp0s", "eth", "wlan", "wlp", - "enx", "en", "wl", "wwp", - "wwan", "wwn", - ]; + let final_str: Mutex = Mutex::new(String::new()); + let intr = file_open("/proc/net/route"); - let intr_tree: String = file_open("/proc/net/dev"); + let lines: &Vec<&str> = &intr.lines().collect(); + let mut interface = String::new(); - let lines = intr_tree.lines() - .collect::>(); - - for line in lines.iter().skip(2) { - let mut line = line.split_whitespace(); - let interface = line.next().unwrap(); - - if KNOWN_INTERFACES.iter().any(|&x| interface.contains(x)) { - selected_intr = interface.to_string().replace(":", ""); - break; + lines.into_iter().for_each(|line| { + if line.contains("00000000") { + interface = line.split("\t").collect::>()[0].to_string(); } - } + }); - // Get ip for the selected interface - let connect_tree: String = file_open("/proc/net/if_inet6"); + let output = Command::new("ifconfig") + .arg(interface.clone()) + .output(); + + if output.is_err() { + return String::from("Unknown"); + }; - let lines = connect_tree.lines() - .collect::>(); + let unw = output.unwrap().stdout; + + let stdout = String::from_utf8_lossy(&unw); - for line in lines.iter() { - let mut line = line.split_whitespace(); + let lines: &Vec<&str> = &stdout.lines().clone().collect(); + + let mut next: bool = false; + + let process_ip = |line: &str| { + let ip = line.split(" ").collect::>()[1].to_string(); + final_str.lock().unwrap().push_str(&ip); + }; + + lines.into_iter().for_each(|line| { + if next { + line.replace("\t", "") + .split(" ") + .collect::>() + .into_iter() + .for_each(|item| { + if item.contains("inet") { + process_ip(item); + } + }); + + next = false; + } + + if line.contains(&interface) { + next = !next; + } + }); + + let x = final_str + .lock() + .unwrap() + .to_string() + .add(format!(" ({})", interface).as_str()); - let hexa = line.next().unwrap().trim(); - let interface = line.nth(4).unwrap().trim(); - - if interface == selected_intr { - final_str = format!("{} ({})", hexa, interface); - }; - } - - final_str -} - -// #[cfg(target_os = "linux")] -// pub fn get_ipaddr() -> String { -// let final_str: Mutex = Mutex::new(String::new()); -// let intr = file_open("/proc/net/route"); - -// let lines: &Vec<&str> = &intr.lines().collect(); -// let mut interface = String::new(); - -// lines.into_iter().for_each(|line| { -// if line.contains("00000000") { -// interface = line.split("\t").collect::>()[0].to_string(); -// } -// }); - -// let output = Command::new("ifconfig") -// .arg(interface.clone()) -// .output() -// .expect("Failed to execute ifconfig"); - -// let output = String::from_utf8(output.stdout).unwrap(); - -// let lines: &Vec<&str> = &output.lines().clone().collect(); - -// let mut next: bool = false; - -// let process_ip = |line: &str| { -// let ip = line.split(" ").collect::>()[1].to_string(); -// final_str.lock().unwrap().push_str(&ip); -// }; - -// lines.into_iter().for_each(|line| { -// if next { -// line.replace("\t", "") -// .split(" ") -// .collect::>() -// .into_iter() -// .for_each(|item| { -// if item.contains("inet") { -// process_ip(item); -// } -// }); - -// next = false; -// } - -// if line.contains(&interface) { -// next = !next; -// } -// }); - -// let x = final_str -// .lock() -// .unwrap() -// .to_string() -// .add(format!(" ({})", interface).as_str()); - -// x -// } \ No newline at end of file + x +} \ No newline at end of file