Added wake indicator. moved to new wake API endpoint. commented out some things. we dont need none of ts.
This commit is contained in:
+1
-1
@@ -154,7 +154,7 @@ pub mod mac {
|
||||
}
|
||||
|
||||
/// Serialize a MacAddr as a string
|
||||
pub fn serialize_mac<S>(mac: &MacAddr, serializer: S) -> Result<S::Ok, S::Error>
|
||||
pub fn _serialize_mac<S>(mac: &MacAddr, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
use crate::arpparse::NUDState;
|
||||
use crate::dhcpparse::DhcpLeaseLine;
|
||||
use crate::utils::parse::serialize_mac;
|
||||
use serde_with::skip_serializing_none;
|
||||
use std::net::IpAddr;
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct DhcpLeaseOut {
|
||||
pub expires_epoch: u64,
|
||||
pub ip: IpAddr,
|
||||
#[serde(serialize_with = "serialize_mac")]
|
||||
pub mac: macaddr::MacAddr,
|
||||
pub name: Option<String>,
|
||||
#[serde(flatten)]
|
||||
pub lease_line: DhcpLeaseLine,
|
||||
pub nud_state: Option<NUDState>,
|
||||
pub rank: Option<u8>,
|
||||
}
|
||||
@@ -37,13 +33,13 @@ pub async fn enrich_leases_with_nud_state(leases: Vec<DhcpLeaseLine>) -> Vec<Dhc
|
||||
}
|
||||
leases
|
||||
.into_iter()
|
||||
.map(|l| DhcpLeaseOut {
|
||||
expires_epoch: l.expires_epoch,
|
||||
ip: l.ip,
|
||||
mac: l.mac,
|
||||
name: l.name,
|
||||
nud_state: map.get(&l.ip).map(|(s, _)| *s),
|
||||
rank: map.get(&l.ip).map(|(_, r)| *r),
|
||||
.map(|lease_line| {
|
||||
let (nud_state, rank) = map.get(&lease_line.ip).copied().unzip();
|
||||
DhcpLeaseOut {
|
||||
lease_line,
|
||||
nud_state,
|
||||
rank,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
+80
-39
@@ -7,14 +7,14 @@ use macaddr::MacAddr;
|
||||
use std::collections::HashSet;
|
||||
use std::net::IpAddr;
|
||||
|
||||
pub async fn get_ips(machine_name: &str) -> error::Result<Vec<IpAddr>> {
|
||||
let it = tokio::net::lookup_host((machine_name, 0))
|
||||
pub async fn get_ips(machine_name: &str) -> error::Result<impl Iterator<Item = IpAddr>> {
|
||||
Ok(tokio::net::lookup_host((machine_name, 0))
|
||||
.await
|
||||
.map_err(|e| error::Error::DnsResolve {
|
||||
name: machine_name.to_string(),
|
||||
source: e,
|
||||
})?;
|
||||
Ok(it.map(|c| c.ip()).collect())
|
||||
})?
|
||||
.map(|c| c.ip()))
|
||||
}
|
||||
|
||||
pub async fn _get_macs_2_1(machine_name: &str) -> Result<HashSet<(IpAddr, MacAddr, NUDState)>> {
|
||||
@@ -50,7 +50,7 @@ pub async fn get_macs_2_mac(machine_name: &str) -> Result<HashSet<MacAddr>> {
|
||||
pub async fn get_macs_1(machine_name: &str) -> Result<Vec<arpparse::IpNeighLine>> {
|
||||
let dev = "br-lan";
|
||||
let ips = get_ips(machine_name).await?;
|
||||
let futures = ips.iter().map(|ip| {
|
||||
let futures = ips.map(|ip| {
|
||||
let ip = ip.to_canonical();
|
||||
async move {
|
||||
let cmd = "ip";
|
||||
@@ -84,42 +84,10 @@ pub async fn get_macs(
|
||||
ips.map(|slice| slice.iter().copied().map(|ip| ip.to_canonical()).collect());
|
||||
let ip_list = match (ip_list, machine_name) {
|
||||
(Some(list), _) => list,
|
||||
(None, Some(name)) => get_ips(name).await?.into_iter().collect(),
|
||||
(None, Some(name)) => get_ips(name).await?.collect(),
|
||||
(None, None) => Vec::new(),
|
||||
};
|
||||
let nud_arg = state.map(NUDState::as_ip_neigh_arg);
|
||||
let run_one = |to_ip: Option<IpAddr>| async move {
|
||||
let mut args: Vec<String> = vec!["neigh".into(), "show".into()];
|
||||
if let Some(ip) = to_ip {
|
||||
args.push("to".into());
|
||||
args.push(ip.to_string());
|
||||
}
|
||||
if let Some(d) = dev {
|
||||
args.push("dev".into());
|
||||
args.push(d.to_string());
|
||||
}
|
||||
if let Some(nud) = nud_arg {
|
||||
args.push("nud".into());
|
||||
args.push(nud.to_string());
|
||||
}
|
||||
let o = exec_command("ip", args.iter().map(String::as_str).collect::<Vec<_>>()).await?;
|
||||
if !o.status.success() {
|
||||
return Err(Error::CommandFailed {
|
||||
cmd: "ip",
|
||||
args,
|
||||
status: o.status.code(),
|
||||
stderr: String::from_utf8_lossy(&o.stderr).into(),
|
||||
});
|
||||
}
|
||||
let lines = String::from_utf8_lossy(&o.stdout);
|
||||
let parsed = lines.lines().flat_map(arpparse::parse_ip_neigh_line);
|
||||
let rows: Vec<IpNeighLine> = if let Some(d) = dev {
|
||||
parsed.map(IpNeighLine::with_dev(d)).collect()
|
||||
} else {
|
||||
parsed.collect()
|
||||
};
|
||||
Ok::<Vec<IpNeighLine>, error::Error>(rows)
|
||||
};
|
||||
let run_one = |to_ip: Option<IpAddr>| get_mac(to_ip, dev, state);
|
||||
if !ip_list.is_empty() {
|
||||
let futures = ip_list.into_iter().map(|ip| run_one(Some(ip)));
|
||||
let res = futures::future::try_join_all(futures).await?;
|
||||
@@ -128,3 +96,76 @@ pub async fn get_macs(
|
||||
run_one(None).await
|
||||
}
|
||||
}
|
||||
|
||||
/// the atomic get_macs. handle ONE thing only.
|
||||
pub async fn get_mac(
|
||||
ip: Option<IpAddr>,
|
||||
dev: Option<&str>,
|
||||
state: Option<NUDState>,
|
||||
) -> error::Result<Vec<IpNeighLine>> {
|
||||
let mut args: Vec<String> = vec!["neigh".into(), "show".into()];
|
||||
if let Some(ip) = ip {
|
||||
args.push("to".into());
|
||||
args.push(ip.to_string());
|
||||
}
|
||||
if let Some(d) = dev {
|
||||
args.push("dev".into());
|
||||
args.push(d.to_string());
|
||||
}
|
||||
if let Some(nud) = state {
|
||||
args.push("nud".into());
|
||||
args.push(nud.as_ip_neigh_arg().into());
|
||||
}
|
||||
let cmd = "ip";
|
||||
let out = exec_command(cmd, args.iter().map(String::as_str).collect::<Vec<_>>()).await?;
|
||||
if !out.status.success() {
|
||||
return Err(error::Error::CommandFailed {
|
||||
cmd,
|
||||
args,
|
||||
status: out.status.code(),
|
||||
stderr: String::from_utf8_lossy(&out.stderr).into(),
|
||||
});
|
||||
}
|
||||
let lines = String::from_utf8_lossy(&out.stdout);
|
||||
let parsed = lines.lines().flat_map(arpparse::parse_ip_neigh_line);
|
||||
let rows: Vec<IpNeighLine> = if let Some(d) = dev {
|
||||
parsed.map(IpNeighLine::with_dev(d)).collect()
|
||||
} else {
|
||||
parsed.collect()
|
||||
};
|
||||
Ok(rows)
|
||||
}
|
||||
|
||||
/* pub async fn _get_macs_good(
|
||||
machine_names: Option<&str>,
|
||||
ips: Option<&[IpAddr]>,
|
||||
devs: Option<&[&str]>,
|
||||
states: Option<&[NUDState]>,
|
||||
) -> Result<Vec<IpNeighLine>> {
|
||||
let mut ip_map: HashSet<IpAddr> = HashSet::new();
|
||||
let mut machine_map = HashSet::new();
|
||||
if let Some(ips) = ips {
|
||||
ip_map.extend(ips);
|
||||
}
|
||||
if let Some(m) = machine_names {
|
||||
machine_map.extend(get_ips(m).await.into_iter().flatten());
|
||||
}
|
||||
|
||||
let real = match (ip_map.is_empty(), machine_map.is_empty()) {
|
||||
(true, true) => HashSet::new(),
|
||||
(true, false) => machine_map,
|
||||
(false, true) => ip_map,
|
||||
(false, false) => ip_map.intersection(&machine_map).copied().collect(),
|
||||
};
|
||||
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
pub async fn _get_machines(m: &[&str]) -> Vec<IpAddr> {
|
||||
let futs = m.iter().map(|c| get_ips(c));
|
||||
futures::future::join_all(futs)
|
||||
.await
|
||||
.into_iter()
|
||||
.flat_map(|f| f.into_iter().flatten())
|
||||
.collect()
|
||||
} */
|
||||
|
||||
+3
-1
@@ -74,7 +74,7 @@ impl WakeTargetResult {
|
||||
pub async fn _wake_multi(
|
||||
targets: impl IntoIterator<Item = WakeTarget>,
|
||||
) -> io::Result<Vec<WakeTargetResult>> {
|
||||
let sock = UdpSocket::bind("0.0.0.0:0").await?;
|
||||
let sock = UdpSocket::bind(":0").await?;
|
||||
sock.set_broadcast(true)?;
|
||||
let fs = targets.into_iter().map(|t| wake_one(&sock, t));
|
||||
Ok(futures::future::join_all(fs).await)
|
||||
@@ -96,3 +96,5 @@ pub async fn wake_one(sock: &UdpSocket, t: WakeTarget) -> WakeTargetResult {
|
||||
Err(_) => t.errored(),
|
||||
}
|
||||
}
|
||||
|
||||
// pub async fn wake_query();
|
||||
|
||||
Reference in New Issue
Block a user