mirror of
https://github.com/Atsukoro1/ponyfetch.git
synced 2024-11-23 12:47:59 +01:00
feat: 🎸 Ip check
This commit is contained in:
parent
3ab8ca5ec8
commit
23502e6c53
1 changed files with 54 additions and 94 deletions
|
@ -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<String> = 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::<Vec<&str>>();
|
||||
|
||||
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::<Vec<&str>>()[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();
|
||||
|
||||
let lines = connect_tree.lines()
|
||||
.collect::<Vec<&str>>();
|
||||
if output.is_err() {
|
||||
return String::from("Unknown");
|
||||
};
|
||||
|
||||
for line in lines.iter() {
|
||||
let mut line = line.split_whitespace();
|
||||
let unw = output.unwrap().stdout;
|
||||
|
||||
let hexa = line.next().unwrap().trim();
|
||||
let interface = line.nth(4).unwrap().trim();
|
||||
let stdout = String::from_utf8_lossy(&unw);
|
||||
|
||||
if interface == selected_intr {
|
||||
final_str = format!("{} ({})", hexa, interface);
|
||||
};
|
||||
}
|
||||
let lines: &Vec<&str> = &stdout.lines().clone().collect();
|
||||
|
||||
final_str
|
||||
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
|
||||
}
|
||||
|
||||
// #[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
|
||||
// }
|
Loading…
Reference in a new issue