use std::net::{IpAddr, UdpSocket};
use super::interface::InterfaceInfo;
const PROBE_TARGETS: &[&str] = &[
"8.8.8.8:80",
"1.1.1.1:80",
"114.114.114.114:80",
"223.5.5.5:80",
];
pub fn detect_egress_ip() -> Option<IpAddr> {
for target in PROBE_TARGETS {
if let Some(ip) = probe_target(target) {
return Some(ip);
}
}
None
}
fn probe_target(target: &str) -> Option<IpAddr> {
let socket = UdpSocket::bind("0.0.0.0:0").ok()?;
socket.connect(target).ok()?;
Some(socket.local_addr().ok()?.ip())
}
pub fn find_egress_interface(egress_ip: &IpAddr, interfaces: &[InterfaceInfo]) -> Option<String> {
let target = egress_ip.to_string();
interfaces
.iter()
.find(|i| i.ipv4 == target)
.map(|i| i.name.clone())
}