woah calm down blud

This commit is contained in:
lda
2026-04-06 03:01:15 +07:00 Unverified
parent 310b0b462d
commit 0c1e0e222b
4 changed files with 133 additions and 14 deletions
+45 -2
View File
@@ -2,9 +2,11 @@ use anyhow::{Context, Result};
use futures::future::try_join_all;
use std::collections::HashSet;
use std::net::IpAddr;
use wakey_core::{DeviceQuery, NeighborEntry, NeighborState, QueryInput, parse};
use wakey_core::{
DeviceQuery, InterfaceAddr, InterfaceSummary, NeighborEntry, NeighborState, QueryInput, parse,
};
use lda_ipjs::subcommands::neighbor;
use lda_ipjs::subcommands::{address, neighbor};
pub async fn get_ips(machine_name: &str) -> Result<impl Iterator<Item = IpAddr>> {
Ok(tokio::net::lookup_host((machine_name, 0))
@@ -159,6 +161,47 @@ pub async fn devs_sorted() -> Vec<String> {
v
}
pub async fn list_interface_summaries() -> Result<Vec<InterfaceSummary>> {
#[cfg(unix)]
let rows = address::nl::get(None)
.await
.context("rtnetlink address query failed")?;
#[cfg(not(unix))]
let rows = address::get_with_backend(address::Backend::Json, None)
.await
.context("ip -j address show failed")?;
let mut out: Vec<InterfaceSummary> = rows
.into_iter()
.filter(|row| row.ifname != "lo")
.map(|row| InterfaceSummary {
ifindex: row.ifindex,
ifname: row.ifname,
operstate: row.operstate.as_str().to_ascii_lowercase(),
mac: row.address,
addrs: row
.addr_info
.into_iter()
.map(|info| InterfaceAddr {
family: info.family.map(|family| family.as_str().to_string()),
cidr: info
.cidr
.local
.zip(info.cidr.prefixlen)
.map(|(addr, prefixlen)| format!("{addr}/{prefixlen}")),
broadcast: info.broadcast,
scope: info.scope,
label: info.label,
})
.collect(),
})
.collect();
out.sort_by(|a, b| a.ifname.cmp(&b.ifname));
Ok(out)
}
pub async fn has_dev(name: &str) -> bool {
list_devs().await.contains(name)
}