On essaie de faire démarer un serveur web
This commit is contained in:
41
pmoutils/src/ip_utils.rs
Normal file
41
pmoutils/src/ip_utils.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use std::net::UdpSocket;
|
||||
use get_if_addrs::get_if_addrs;
|
||||
|
||||
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() {
|
||||
result.entry(iface.name)
|
||||
.or_insert_with(Vec::new)
|
||||
.push(ip.to_string());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result.insert("error".to_string(), vec!["Failed to get interfaces".to_string()]);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
3
pmoutils/src/lib.rs
Normal file
3
pmoutils/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
mod ip_utils;
|
||||
|
||||
pub use ip_utils::guess_local_ip;
|
||||
Reference in New Issue
Block a user