2025-09-23 20:54:50 +02:00
|
|
|
use get_if_addrs::get_if_addrs;
|
2025-10-01 07:42:16 +02:00
|
|
|
use std::net::UdpSocket;
|
2025-09-23 20:54:50 +02:00
|
|
|
|
|
|
|
|
pub fn guess_local_ip() -> String {
|
|
|
|
|
// On tente de deviner l'IP locale
|
|
|
|
|
match UdpSocket::bind("0.0.0.0:0") {
|
|
|
|
|
Ok(socket) => {
|
|
|
|
|
if socket.connect("8.8.8.8:80").is_ok() {
|
|
|
|
|
if let Ok(local_addr) = socket.local_addr() {
|
|
|
|
|
return local_addr.ip().to_string();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Si erreur sur connect ou récupération de l'adresse
|
|
|
|
|
"127.0.0.1".to_string()
|
|
|
|
|
}
|
|
|
|
|
Err(_) => "127.0.0.1".to_string(), // Si bind échoue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn list_all_ips() -> std::collections::HashMap<String, Vec<String>> {
|
|
|
|
|
let mut result = std::collections::HashMap::new();
|
|
|
|
|
|
|
|
|
|
if let Ok(interfaces) = get_if_addrs() {
|
|
|
|
|
for iface in interfaces {
|
|
|
|
|
let ip = iface.ip();
|
|
|
|
|
if ip.is_loopback() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if ip.is_ipv4() {
|
2025-10-01 07:42:16 +02:00
|
|
|
result
|
|
|
|
|
.entry(iface.name)
|
|
|
|
|
.or_insert_with(Vec::new)
|
|
|
|
|
.push(ip.to_string());
|
2025-09-23 20:54:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-10-01 07:42:16 +02:00
|
|
|
result.insert(
|
|
|
|
|
"error".to_string(),
|
|
|
|
|
vec!["Failed to get interfaces".to_string()],
|
|
|
|
|
);
|
2025-09-23 20:54:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
}
|