revert ts change now that i have the GOAT lda-performance
This commit is contained in:
+2
-2
@@ -89,7 +89,7 @@ pub enum NUDState {
|
|||||||
|
|
||||||
impl NUDState {
|
impl NUDState {
|
||||||
/// Argument form expected by `ip neigh ... nud <state>` (lowercase)
|
/// Argument form expected by `ip neigh ... nud <state>` (lowercase)
|
||||||
pub const fn _as_ip_neigh_arg(self) -> &'static str {
|
pub const fn as_ip_neigh_arg(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
NUDState::Permanent => "permanent",
|
NUDState::Permanent => "permanent",
|
||||||
NUDState::Reachable => "reachable",
|
NUDState::Reachable => "reachable",
|
||||||
@@ -183,7 +183,7 @@ impl IpNeighLine {
|
|||||||
Self { state, ..self }
|
Self { state, ..self }
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
pub fn _with_dev(dev: impl Into<String>) -> impl FnMut(Self) -> Self {
|
pub fn with_dev(dev: impl Into<String>) -> impl FnMut(Self) -> Self {
|
||||||
let dev = dev.into();
|
let dev = dev.into();
|
||||||
move |self_| Self {
|
move |self_| Self {
|
||||||
dev: Some(dev.clone()),
|
dev: Some(dev.clone()),
|
||||||
|
|||||||
+46
-5
@@ -1,17 +1,58 @@
|
|||||||
use lda_ipjs::subcommands::address::json as ipjs_json;
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
// /// 50ms
|
||||||
|
// pub async fn get_dev() -> HashSet<String> {
|
||||||
|
// use lda_ipjs::subcommands::address::json as ipjs_json;
|
||||||
|
// let mut devs: HashSet<String> = HashSet::new();
|
||||||
|
// if let Ok(items) = ipjs_json::get(None).await {
|
||||||
|
// for item in items {
|
||||||
|
// if item.ifname != "lo" && !item.ifname.is_empty() {
|
||||||
|
// devs.insert(item.ifname);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// devs
|
||||||
|
// }
|
||||||
|
|
||||||
|
/// 3ms
|
||||||
pub async fn get_dev() -> HashSet<String> {
|
pub async fn get_dev() -> HashSet<String> {
|
||||||
|
use std::fs;
|
||||||
|
fn get_dev() -> HashSet<String> {
|
||||||
let mut devs: HashSet<String> = HashSet::new();
|
let mut devs: HashSet<String> = HashSet::new();
|
||||||
if let Ok(items) = ipjs_json::get(None).await {
|
if let Ok(rd) = std::fs::read_dir("/sys/class/net") {
|
||||||
for item in items {
|
for e in rd.flatten() {
|
||||||
if item.ifname != "lo" && !item.ifname.is_empty() {
|
if e.file_type()
|
||||||
devs.insert(item.ifname);
|
.map(|ft| {
|
||||||
|
if ft.is_symlink() {
|
||||||
|
// true
|
||||||
|
fs::metadata(e.path()).map(|m| m.is_dir()).unwrap_or(false)
|
||||||
|
} else {
|
||||||
|
ft.is_dir()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or(false)
|
||||||
|
&& let Ok(name) = e.file_name().into_string()
|
||||||
|
&& name != "lo"
|
||||||
|
&& !name.is_empty()
|
||||||
|
{
|
||||||
|
devs.insert(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Ok(txt) = std::fs::read_to_string("/proc/net/dev") {
|
||||||
|
for line in txt.lines().skip(2) {
|
||||||
|
if let Some((name, _rest)) = line.split_once(':') {
|
||||||
|
let n = name.trim().to_string();
|
||||||
|
if n != "lo" && !n.is_empty() {
|
||||||
|
devs.insert(n);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
devs
|
devs
|
||||||
}
|
}
|
||||||
|
tokio::task::spawn_blocking(get_dev)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn devs_sorted() -> Vec<String> {
|
pub async fn devs_sorted() -> Vec<String> {
|
||||||
let mut v: Vec<String> = get_dev().await.into_iter().collect();
|
let mut v: Vec<String> = get_dev().await.into_iter().collect();
|
||||||
|
|||||||
+48
-10
@@ -1,8 +1,7 @@
|
|||||||
use macaddr::MacAddr;
|
use macaddr::MacAddr;
|
||||||
|
|
||||||
use crate::arpparse::{IpNeighLine, NUDState};
|
use crate::arpparse::{self, IpNeighLine, NUDState};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result, bail};
|
||||||
use lda_ipjs::subcommands::neighbor as ipjs_neigh;
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
|
||||||
@@ -95,21 +94,60 @@ pub async fn get_macs(
|
|||||||
Ok(ip_filtered)
|
Ok(ip_filtered)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// /// the atomic get_macs. handle ONE thing only.
|
||||||
|
// pub async fn get_mac(
|
||||||
|
// ip: Option<IpAddr>,
|
||||||
|
// dev: Option<&str>,
|
||||||
|
// state: &[NUDState],
|
||||||
|
// ) -> Result<Vec<IpNeighLine>> {
|
||||||
|
// use lda_ipjs::subcommands::neighbor as ipjs_neigh;
|
||||||
|
// let ipjs_states: Vec<ipjs_neigh::NUDState> = state.iter().copied().map(Into::into).collect();
|
||||||
|
|
||||||
|
// let items = ipjs_neigh::json::get(ip, dev, &ipjs_states)
|
||||||
|
// .await
|
||||||
|
// .context("Calling ip -j neigh failed")?;
|
||||||
|
|
||||||
|
// let lines = items.into_iter().map(Into::into).collect();
|
||||||
|
|
||||||
|
// Ok(lines)
|
||||||
|
// }
|
||||||
|
|
||||||
/// the atomic get_macs. handle ONE thing only.
|
/// the atomic get_macs. handle ONE thing only.
|
||||||
pub async fn get_mac(
|
pub async fn get_mac(
|
||||||
ip: Option<IpAddr>,
|
ip: Option<IpAddr>,
|
||||||
dev: Option<&str>,
|
dev: Option<&str>,
|
||||||
state: &[NUDState],
|
state: &[NUDState],
|
||||||
) -> Result<Vec<IpNeighLine>> {
|
) -> Result<Vec<IpNeighLine>> {
|
||||||
let ipjs_states: Vec<ipjs_neigh::NUDState> = state.iter().copied().map(Into::into).collect();
|
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());
|
||||||
|
}
|
||||||
|
for nud in state {
|
||||||
|
args.push("nud".into());
|
||||||
|
args.push(nud.as_ip_neigh_arg().into());
|
||||||
|
}
|
||||||
|
let cmd = "ip";
|
||||||
|
let mut u = tokio::process::Command::new(cmd);
|
||||||
|
u.args(args);
|
||||||
|
let out = u.output().await?;
|
||||||
|
|
||||||
let items = ipjs_neigh::json::get(ip, dev, &ipjs_states)
|
if !out.status.success() {
|
||||||
.await
|
bail!(String::from_utf8_lossy(&out.stderr).into_owned());
|
||||||
.context("Calling ip -j neigh failed")?;
|
}
|
||||||
|
|
||||||
let lines = items.into_iter().map(Into::into).collect();
|
let lines = String::from_utf8_lossy(&out.stdout);
|
||||||
|
let parsed = lines.lines().flat_map(arpparse::parse_ip_neigh_line);
|
||||||
Ok(lines)
|
let rows: Vec<IpNeighLine> = if let Some(d) = dev {
|
||||||
|
parsed.map(IpNeighLine::with_dev(d)).collect()
|
||||||
|
} else {
|
||||||
|
parsed.collect()
|
||||||
|
};
|
||||||
|
Ok(rows)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user