feat: 🎸 Ip check

This commit is contained in:
Atsukoro1 2022-12-03 21:30:36 +01:00
parent 3ab8ca5ec8
commit 23502e6c53

View file

@ -28,104 +28,64 @@ pub fn get_ipaddr() -> String {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn get_ipaddr() -> String { pub fn get_ipaddr() -> String {
let mut final_str = String::new(); let final_str: Mutex<String> = Mutex::new(String::new());
let mut selected_intr = String::new(); let intr = file_open("/proc/net/route");
const KNOWN_INTERFACES: [&str; 10] = [
"enp0s", "eth", "wlan", "wlp",
"enx", "en", "wl", "wwp",
"wwan", "wwn",
];
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() lines.into_iter().for_each(|line| {
.collect::<Vec<&str>>(); if line.contains("00000000") {
interface = line.split("\t").collect::<Vec<&str>>()[0].to_string();
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;
}
} }
});
// Get ip for the selected interface let output = Command::new("ifconfig")
let connect_tree: String = file_open("/proc/net/if_inet6"); .arg(interface.clone())
.output();
let lines = connect_tree.lines() if output.is_err() {
.collect::<Vec<&str>>(); return String::from("Unknown");
for line in lines.iter() {
let mut line = line.split_whitespace();
let hexa = line.next().unwrap().trim();
let interface = line.nth(4).unwrap().trim();
if interface == selected_intr {
final_str = format!("{} ({})", hexa, interface);
}; };
let unw = output.unwrap().stdout;
let stdout = String::from_utf8_lossy(&unw);
let lines: &Vec<&str> = &stdout.lines().clone().collect();
let mut next: bool = false;
let process_ip = |line: &str| {
let ip = line.split(" ").collect::<Vec<&str>>()[1].to_string();
final_str.lock().unwrap().push_str(&ip);
};
lines.into_iter().for_each(|line| {
if next {
line.replace("\t", "")
.split(" ")
.collect::<Vec<&str>>()
.into_iter()
.for_each(|item| {
if item.contains("inet") {
process_ip(item);
}
});
next = false;
} }
final_str if line.contains(&interface) {
next = !next;
}
});
let x = final_str
.lock()
.unwrap()
.to_string()
.add(format!(" ({})", interface).as_str());
x
} }
// #[cfg(target_os = "linux")]
// pub fn get_ipaddr() -> String {
// let final_str: Mutex<String> = 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::<Vec<&str>>()[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::<Vec<&str>>()[1].to_string();
// final_str.lock().unwrap().push_str(&ip);
// };
// lines.into_iter().for_each(|line| {
// if next {
// line.replace("\t", "")
// .split(" ")
// .collect::<Vec<&str>>()
// .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
// }