diff --git a/Cargo.lock b/Cargo.lock index ff656e0..5563b42 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -610,6 +610,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "cpufeatures" version = "0.2.16" @@ -637,6 +643,25 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.20" @@ -1182,11 +1207,13 @@ dependencies = [ "aya", "aya-log", "cargo_metadata", + "lazy_static", "mime_guess", "net-guardia-common", "rust-embed", "serde", "serde_json", + "sysinfo", "thiserror 2.0.3", "tokio", "toml", @@ -1220,6 +1247,15 @@ version = "0.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e82e9f64c09f56aa7c80c3fa087997bd99a913f91d9c74d36cf5fd75dd5773e6" +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -1419,6 +1455,26 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.5.7" @@ -1728,6 +1784,20 @@ dependencies = [ "syn", ] +[[package]] +name = "sysinfo" +version = "0.32.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c33cd241af0f2e9e3b5c32163b873b29956890b5342e6745b917ce9d490f4af" +dependencies = [ + "core-foundation-sys", + "libc", + "memchr", + "ntapi", + "rayon", + "windows", +] + [[package]] name = "thiserror" version = "1.0.69" @@ -2081,6 +2151,59 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result", + "windows-targets", +] + +[[package]] +name = "windows-implement" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.57.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-result" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/net-guardia/Cargo.toml b/net-guardia/Cargo.toml index c1667d6..5d966b0 100644 --- a/net-guardia/Cargo.toml +++ b/net-guardia/Cargo.toml @@ -24,6 +24,8 @@ actix = "0.13.5" mime_guess = "2.0.5" actix-web-actors = "4.3.0" serde_json = "1.0.133" +lazy_static = "1.5.0" +sysinfo = "0.32.1" [build-dependencies] cargo_metadata = { workspace = true } diff --git a/net-guardia/src/core/monitor.rs b/net-guardia/src/core/monitor.rs index 8d65c06..dfaa621 100644 --- a/net-guardia/src/core/monitor.rs +++ b/net-guardia/src/core/monitor.rs @@ -114,11 +114,12 @@ impl Monitor { where K: Pod, { + let boot_time = System::boot_time().await; let expired_keys: Vec = map .iter() .filter_map(|result| { if let Ok((key, stats)) = result { - if now - stats[2] > window { + if now - stats[2] - boot_time > window { Some(key) } else { None diff --git a/net-guardia/src/core/system.rs b/net-guardia/src/core/system.rs index c9b1864..72f2d82 100644 --- a/net-guardia/src/core/system.rs +++ b/net-guardia/src/core/system.rs @@ -3,7 +3,7 @@ use crate::core::monitor::Monitor; use crate::utils::log_entry::ebpf::EbpfEntry; use crate::utils::log_entry::system::SystemEntry; use crate::utils::logging::Logging; -use crate::web::api::{control, default, monitor}; +use crate::web::api::{control, default, misc, monitor}; use actix_web::web::route; use actix_web::{App, HttpServer}; use anyhow::Context; @@ -13,11 +13,13 @@ use std::sync::OnceLock; use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}; use tracing::{error, info, warn}; use crate::core::control::Control; +use sysinfo::System as SystemInfo; static SYSTEM: OnceLock> = OnceLock::new(); pub struct System { pub ebpf: Ebpf, + pub boot_time: u64, } impl System { @@ -48,7 +50,8 @@ impl System { program .attach(&interface, XdpFlags::default()) .context(EbpfEntry::AttachProgramFailed)?; - SYSTEM.get_or_init(|| RwLock::new(System { ebpf })); + let boot_time = SystemInfo::boot_time() * 1_000_000_000; + SYSTEM.get_or_init(|| RwLock::new(System { ebpf, boot_time })); info!("{}", EbpfEntry::AttachProgramSuccess); Ok(()) } @@ -67,6 +70,7 @@ impl System { .wrap(cors) .service(monitor::initialize()) .service(control::initialize()) + .service(misc::initialize()) .default_service(route().to(default::default_route)) }) .bind(format!("0.0.0.0:{}", config.http_server_bind_port))? @@ -95,4 +99,9 @@ impl System { // There is no lock acquired multiple times, so this is safe once_lock.write().await } + + pub async fn boot_time() -> u64 { + let system = System::instance().await; + system.boot_time + } } diff --git a/net-guardia/src/web/api/misc.rs b/net-guardia/src/web/api/misc.rs new file mode 100644 index 0000000..e1aebe0 --- /dev/null +++ b/net-guardia/src/web/api/misc.rs @@ -0,0 +1,13 @@ +use actix_web::{get, web, HttpResponse, Responder, Scope}; +use crate::core::system::System; + +pub fn initialize() -> Scope { + web::scope("/misc") + .service(boot_time) +} + +#[get("/boot_time")] +async fn boot_time() -> impl Responder { + let boot_time = System::boot_time().await; + HttpResponse::Ok().json(boot_time) +} diff --git a/net-guardia/src/web/api/mod.rs b/net-guardia/src/web/api/mod.rs index 5aef403..709054e 100644 --- a/net-guardia/src/web/api/mod.rs +++ b/net-guardia/src/web/api/mod.rs @@ -1,3 +1,4 @@ pub mod default; pub mod control; +pub mod misc; pub mod monitor;