Added wake indicator. moved to new wake API endpoint. commented out some things. we dont need none of ts.
This commit is contained in:
@@ -26,6 +26,7 @@ pub async fn read_dhcp_leases_with_names() -> io::Result<Vec<DhcpLeaseLine>> {
|
||||
for mut l in leases {
|
||||
let mac_s = l.mac.to_string();
|
||||
if let Some(ref name) = l.name {
|
||||
// if no name in cache file or it changed
|
||||
if cache.get(&mac_s).map(|v| v != name).unwrap_or(true) {
|
||||
cache.insert(mac_s, name.clone());
|
||||
changed = true;
|
||||
|
||||
+10
-9
@@ -1,16 +1,16 @@
|
||||
use crate::{
|
||||
arpparse::{IpNeighLine, NUDState},
|
||||
arpparse::{NUDState},
|
||||
utils::parse::{de_many, serialize_macs},
|
||||
};
|
||||
use axum::{Json, http::StatusCode, response::IntoResponse};
|
||||
use axum_extra::extract::Query;
|
||||
use macaddr::MacAddr;
|
||||
use serde::Serialize;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use serde_with::skip_serializing_none;
|
||||
use std::collections::HashSet;
|
||||
use std::net::IpAddr;
|
||||
|
||||
#[derive(Debug, Default, Clone, Hash, serde::Deserialize, Serialize)]
|
||||
#[derive(Debug, Default, Clone, Hash, Deserialize, Serialize)]
|
||||
pub struct DeviceQuery {
|
||||
pub name: Option<String>,
|
||||
#[serde(default, deserialize_with = "de_many::vec_from_strs")]
|
||||
@@ -27,20 +27,20 @@ pub struct DeviceQuery {
|
||||
pub nud: Vec<NUDState>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Hash, serde::Deserialize)]
|
||||
#[derive(Debug, Default, Clone, Hash, Deserialize)]
|
||||
pub struct NamePath {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, Default, serde::Serialize)]
|
||||
pub struct Status {
|
||||
#[derive(Debug, Default, Serialize)]
|
||||
pub struct Status<T> {
|
||||
pub name: Option<String>,
|
||||
pub table: Vec<IpNeighLine>,
|
||||
pub table: Vec<T>,
|
||||
pub filters: Filters,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize)]
|
||||
#[derive(Debug, Default, Serialize)]
|
||||
pub struct Filters {
|
||||
#[serde(skip_serializing_if = "Vec::is_empty")]
|
||||
pub ip: Vec<IpAddr>,
|
||||
@@ -56,7 +56,7 @@ pub struct Filters {
|
||||
}
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, serde::Serialize, Default)]
|
||||
#[derive(Debug, Serialize, Default)]
|
||||
pub struct StatusError {
|
||||
pub name: Option<String>,
|
||||
pub error: String,
|
||||
@@ -103,6 +103,7 @@ pub async fn get_status_json(
|
||||
));
|
||||
}
|
||||
}
|
||||
// why try join all?
|
||||
match futures::future::try_join_all(tasks)
|
||||
.await
|
||||
.map(|v| v.into_iter().flatten().collect::<Vec<_>>())
|
||||
|
||||
+24
-2
@@ -2,6 +2,8 @@
|
||||
use std::io;
|
||||
use std::net::IpAddr;
|
||||
|
||||
/* use crate::arpparse::IpNeighLine;
|
||||
use crate::route::api::Status; */
|
||||
use crate::utils::parse::mac::{des_opm, ser_opm};
|
||||
use crate::utils::wake::wake_one;
|
||||
use axum::{extract::Json, http::StatusCode, response::IntoResponse};
|
||||
@@ -26,7 +28,7 @@ pub struct WakeTargetResult {
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone, Copy, Hash)]
|
||||
#[serde(rename_all="snake_case")]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum WakeTargetStatus {
|
||||
Succeed,
|
||||
/// not a real address...
|
||||
@@ -39,8 +41,9 @@ pub enum WakeTargetStatus {
|
||||
#[skip_serializing_none]
|
||||
#[derive(Debug, Deserialize, Clone, Copy)]
|
||||
pub struct WakeTarget {
|
||||
#[serde(default)]
|
||||
pub ip: Option<IpAddr>,
|
||||
#[serde(deserialize_with = "des_opm")]
|
||||
#[serde(default, deserialize_with = "des_opm")]
|
||||
pub mac: Option<MacAddr>,
|
||||
}
|
||||
|
||||
@@ -91,3 +94,22 @@ pub async fn wake_multi_split(
|
||||
.await,
|
||||
)
|
||||
}
|
||||
/* #[derive(Debug, Serialize)]
|
||||
pub struct WakeStatusLine {
|
||||
#[serde(flatten)]
|
||||
pub status: IpNeighLine, // most powerful find of the century
|
||||
pub wake_status: WakeTargetStatus,
|
||||
}
|
||||
pub type WakeStatus = Status<WakeStatusLine>; */
|
||||
// /// return status BUT plus a indicator of i sent a wake.
|
||||
// pub async fn wake_status(
|
||||
// Query(DeviceQuery {
|
||||
// name,
|
||||
// ip,
|
||||
// dev,
|
||||
// nud,
|
||||
// mac,
|
||||
// ..
|
||||
// }): Query<DeviceQuery>,
|
||||
// ) -> impl IntoResponse {
|
||||
// }
|
||||
|
||||
+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