Use /proc/net/route instead of ip route command

Fix #811, allegedly /proc/net/route is almost always available, and no
additional dependency is needed at all.
This commit is contained in:
Markus Unterwaditzer
2026-01-30 14:39:05 +01:00
committed by Cooper Quintin
parent a3d0d8f4f9
commit be15035ad4

View File

@@ -181,11 +181,8 @@ pub struct RouteStatus {
}
pub async fn get_route_status() -> Json<RouteStatus> {
let mut cmd = Command::new("busybox");
cmd.args(["ip", "route"]);
let has_default_route = match get_cmd_output(cmd).await {
Ok(output) => output.lines().any(|line| line.starts_with("default ")),
let has_default_route = match check_default_route().await {
Ok(result) => result,
Err(err) => {
log::warn!("Failed to check default route: {err}");
// More likely than not, this is a portability issue. The logic hasn't been fully
@@ -196,3 +193,14 @@ pub async fn get_route_status() -> Json<RouteStatus> {
Json(RouteStatus { has_default_route })
}
// Checks for an IPv4 default route by reading /proc/net/route
// instead of shelling out to `ip route`, which may not be available on all devices.
async fn check_default_route() -> std::io::Result<bool> {
let contents = tokio::fs::read_to_string("/proc/net/route").await?;
Ok(contents.lines().skip(1).any(|line| {
line.split_whitespace()
.nth(1)
.is_some_and(|dest| dest == "00000000")
}))
}