adding bloat + use the copilot page this time

This commit is contained in:
lda
2025-08-24 20:50:47 +07:00 Unverified
parent 93bb04bbab
commit 811116715c
5 changed files with 42 additions and 19 deletions
+15
View File
@@ -159,6 +159,7 @@ impl FromStr for IpNeighLine {
}
/// pls dont touch ts
impl IpNeighLine {
/* // these are some not needed fns
pub fn set_ip(&mut self, ip: IpAddr) {
self.ip = ip;
}
@@ -171,6 +172,20 @@ impl IpNeighLine {
pub fn state(self, state: NUDState) -> Self {
Self { state, ..self }
}
*/
pub fn with_dev(dev: impl Into<String>) -> impl FnMut(Self) -> Self {
let dev = dev.into();
move |self_| Self {
dev: Some(dev.clone()),
..self_
}
}
pub fn with_mac(mac: MacAddr) -> impl FnMut(Self) -> Self {
move |self_| Self {
mac: Some(mac),
..self_
}
}
}
// ideas from copilot:
+4 -3
View File
@@ -43,7 +43,7 @@ async fn home() -> Html<String> {
}
async fn wake_handler(
Query(DeviceQuery { name }): Query<DeviceQuery>,
Query(DeviceQuery { name, .. }): Query<DeviceQuery>,
) -> axum::response::Result<impl IntoResponse> {
match wake(name.as_deref().unwrap_or(MACHINE_NAME)).await {
Ok(x) if x > 0 => Ok((StatusCode::ACCEPTED, format!("{x} packets sent!"))),
@@ -93,11 +93,12 @@ pub async fn status_build(machine_name: &str) -> String {
#[cfg(target_os = "linux")]
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
use crate::route::{api_status, get_status_2, home_2_route};
use crate::route::{api_status, get_status_2, home_2, home_2_route};
color_eyre::install()?;
let app = Router::new()
.route("/", get(home))
.route("/home", get(home))
.route("/", get(home_2))
.merge(home_2_route())
.route("/wake", post(wake_handler))
.route("/status", get(get_status_2))
+9 -6
View File
@@ -1,3 +1,5 @@
use std::net::IpAddr;
use axum::{
Json, Router,
extract::{Path, Query},
@@ -5,10 +7,11 @@ use axum::{
response::{Html, IntoResponse, Redirect},
routing::get,
};
use macaddr::MacAddr;
use crate::{MACHINE_NAME, arpparse, status_build, utils::query::get_macs_1};
use crate::{MACHINE_NAME, arpparse::{self, des_opm}, status_build, utils::query::get_macs_1};
async fn home_2() -> Html<&'static str> {
pub async fn home_2() -> Html<&'static str> {
Html(include_str!("../static/home_2"))
}
async fn home_2_css() -> impl IntoResponse {
@@ -41,9 +44,9 @@ pub fn home_2_route() -> Router {
#[derive(Debug, Default, Clone, Hash, serde::Deserialize)]
pub struct DeviceQuery {
pub name: Option<String>,
// ip: Option<IpAddr>,
// #[serde(deserialize_with = "des_opm")]
// mac: Option<MacAddr>,
ip: Option<IpAddr>,
#[serde(deserialize_with = "des_opm")]
mac: Option<MacAddr>,
}
#[derive(Debug, Default, Clone, Hash, serde::Deserialize)]
pub struct NamePath {
@@ -64,7 +67,7 @@ pub struct StatusError {
pub async fn get_status_json(
// p: Option<Path<NamePath>>,
Query(DeviceQuery { name }): Query<DeviceQuery>,
Query(DeviceQuery { name, .. }): Query<DeviceQuery>,
) -> impl IntoResponse {
let name = /* p
.map(|Path(n)| n.name)
+11 -7
View File
@@ -38,15 +38,13 @@ pub async fn get_macs_2_mac(machine_name: &str) -> io::Result<HashSet<MacAddr>>
}
pub async fn get_macs_1(machine_name: &str) -> io::Result<Vec<arpparse::IpNeighLine>> {
let dev = "br-lan";
let ips = get_ips(machine_name).await?;
let futures = ips.iter().map(|ip| {
let ip = ip.to_canonical();
async move {
let o = exec_command(
"ip",
["neigh", "show", "to", &ip.to_string(), "dev", "br-lan"],
)
.await?;
let o =
exec_command("ip", ["neigh", "show", "to", &ip.to_string(), "dev", dev]).await?;
if !o.status.success() {
return Err(io::Error::other(format!(
"`ip neigh` failed for {ip} (status: {st}): {err}",
@@ -56,14 +54,20 @@ pub async fn get_macs_1(machine_name: &str) -> io::Result<Vec<arpparse::IpNeighL
};
Ok(String::from_utf8_lossy(&o.stdout)
.lines()
.map(arpparse::parse_ip_neigh_line)
.flat_map(arpparse::parse_ip_neigh_line)
// .map(IpNeighLine::with_dev(dev)) // this could be after flatmap up there
.collect::<Vec<_>>())
}
});
let res = futures::future::try_join_all(futures).await?; // async move block errs.
Ok(res
.into_iter()
.flatten() /* resolve double vec */
.flatten() /* drop parse errors */
// .flatten() /* drop parse errors (flat_map cleared) */
.collect())
}
pub async fn get_macs(machine_name: Option<&str>, ips: Option<impl IntoIterator<Item = IpAddr>>, dev: Option<&str>, state: Option<NUDState>) -> io::Result<Vec<IpNeighLine>> {
todo!()
}