()
| 245 | } |
| 246 | |
| 247 | fn detect_default_gateway_ip() -> Option<String> { |
| 248 | #[cfg(target_os = "macos")] |
| 249 | { |
| 250 | let output = bitfun_core::util::process_manager::create_command("route") |
| 251 | .args(["-n", "get", "default"]) |
| 252 | .output() |
| 253 | .ok()?; |
| 254 | if !output.status.success() { |
| 255 | return None; |
| 256 | } |
| 257 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 258 | let re = Regex::new(r"(?m)^\s*gateway:\s*([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s*$").ok()?; |
| 259 | return re |
| 260 | .captures(&stdout) |
| 261 | .and_then(|c| c.get(1).map(|m| m.as_str().to_string())); |
| 262 | } |
| 263 | |
| 264 | #[cfg(target_os = "linux")] |
| 265 | { |
| 266 | let output = bitfun_core::util::process_manager::create_command("ip") |
| 267 | .args(["route", "show", "default"]) |
| 268 | .output() |
| 269 | .ok()?; |
| 270 | if !output.status.success() { |
| 271 | return None; |
| 272 | } |
| 273 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 274 | let re = Regex::new(r"(?m)^default\s+via\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\b").ok()?; |
| 275 | return re |
| 276 | .captures(&stdout) |
| 277 | .and_then(|c| c.get(1).map(|m| m.as_str().to_string())); |
| 278 | } |
| 279 | |
| 280 | #[cfg(target_os = "windows")] |
| 281 | { |
| 282 | let output = bitfun_core::util::process_manager::create_command("route") |
| 283 | .args(["print", "-4"]) |
| 284 | .output() |
| 285 | .ok()?; |
| 286 | if !output.status.success() { |
| 287 | return None; |
| 288 | } |
| 289 | let stdout = String::from_utf8_lossy(&output.stdout); |
| 290 | let re = |
| 291 | Regex::new(r"(?m)^\s*0\.0\.0\.0\s+0\.0\.0\.0\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\s+") |
| 292 | .ok()?; |
| 293 | return re |
| 294 | .captures(&stdout) |
| 295 | .and_then(|c| c.get(1).map(|m| m.as_str().to_string())); |
| 296 | } |
| 297 | |
| 298 | #[allow(unreachable_code)] |
| 299 | None |
| 300 | } |
| 301 | |
| 302 | /// Detect per-interface gateway IPs by parsing the system routing table. |
| 303 | /// |
no test coverage detected