diff --git a/Cargo.lock b/Cargo.lock index f46b28f..8f1f7a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anyhow" version = "1.0.68" @@ -47,6 +56,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "bumpalo" +version = "3.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" + [[package]] name = "bytemuck" version = "1.13.0" @@ -85,6 +100,8 @@ version = "0.1.0" dependencies = [ "anyhow", "bytemuck", + "chrono", + "hyper", "serde", "serde_json", "tokio", @@ -96,12 +113,157 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits", + "time", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" + +[[package]] +name = "cxx" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86d3488e7665a7a483b57e25bdd90d0aeb2bc7608c8d0346acf2ad3f1caf1d62" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48fcaf066a053a41a81dfb14d57d99738b767febb8b735c3016e469fac5da690" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2ef98b8b717a829ca5603af80e1f9e2e48013ab227b68ef37872ef84ee479bf" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "086c685979a698443656e5cf7856c95c642295a38599f12fb1ff76fb28d19892" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures-channel" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e5317663a9089767a1ec00a487df42e0ca174b61b4483213ac24448e4664df5" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608" + +[[package]] +name = "futures-sink" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f310820bb3e8cfd46c80db4d7fb8353e15dfff853a127158425f31e0be6c8364" + +[[package]] +name = "futures-task" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf79a1bf610b10f42aea489289c5a2c478a786509693b80cd39c44ccd936366" + +[[package]] +name = "futures-util" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + [[package]] name = "gimli" version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dec7af912d60cdbd3677c1af9352ebae6fb8394d165568a2234df0fa00f87793" +[[package]] +name = "h2" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f9f29bc9dda355256b2916cf526ab02ce0aeaaaf2bad60d65ef3f12f11dd0f4" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hermit-abi" version = "0.2.6" @@ -111,18 +273,128 @@ dependencies = [ "libc", ] +[[package]] +name = "http" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd6effc99afb63425aff9b05836f029929e345a6148a14b7ecd5ab67af944482" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" + +[[package]] +name = "httpdate" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" + +[[package]] +name = "hyper" +version = "0.14.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e011372fa0b68db8350aa7a248930ecc7839bf46d8485577d69f117a75f164c" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "winapi", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "itoa" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" +[[package]] +name = "js-sys" +version = "0.3.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "libc" version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "log" version = "0.4.17" @@ -155,10 +427,29 @@ checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" dependencies = [ "libc", "log", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "windows-sys 0.45.0", ] +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.15.0" @@ -178,12 +469,24 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" + [[package]] name = "pin-project-lite" version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "proc-macro2" version = "1.0.50" @@ -214,6 +517,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" +[[package]] +name = "scratch" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" + [[package]] name = "serde" version = "1.0.152" @@ -245,6 +554,15 @@ dependencies = [ "serde", ] +[[package]] +name = "slab" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef" +dependencies = [ + "autocfg", +] + [[package]] name = "socket2" version = "0.4.7" @@ -266,6 +584,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tokio" version = "1.24.2" @@ -295,18 +633,140 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-util" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" + [[package]] name = "unicode-ident" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d" + [[package]] name = "winapi" version = "0.3.9" @@ -323,6 +783,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 0ccf29a..d4fc38b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,10 @@ anyhow = { version = "1.0.68", features = ["backtrace"] } bytemuck = { version = "1.13.0", features = ["derive"] } serde = { version = "1.0.152", features = ["derive"] } serde_json = "1.0.91" +hyper = { version = "0.14.24", optional = true, features = ["full"] } # TODO: reduce +chrono = { version = "0.4.23", optional = true } # TODO: reduce [features] -default = ["debug_server"] -debug_server = [] \ No newline at end of file +default = ["debug_server", "chrono"] +chrono = ["dep:chrono"] +debug_server = ["dep:hyper"] diff --git a/config-template.json b/config-template.json index 597011a..43471ef 100644 --- a/config-template.json +++ b/config-template.json @@ -1,10 +1,11 @@ { "dyn_ip_server": "127.0.0.1:11811", - "listen_addr": "0.0.0.0:11812", + "listen_addr": "0.0.0.0:11820", + "debug_server_addr": "0.0.0.0:4885", "allowed_ports": [ [ 49152, - 65536 + 65535 ] ] } \ No newline at end of file diff --git a/src/debug_server.rs b/src/debug_server.rs index 03ecf07..82ff745 100644 --- a/src/debug_server.rs +++ b/src/debug_server.rs @@ -1 +1,29 @@ -pub async fn debug_server() {} +use hyper::service::{make_service_fn, service_fn}; +use hyper::{Body, Response, Server}; +use std::convert::Infallible; +use std::net::SocketAddr; +use std::sync::Arc; +use tokio::sync::Mutex; + +use crate::ports::PortHandler; + +pub async fn debug_server(addr: SocketAddr, port_handler: Arc>) { + let server = Server::bind(&addr).serve(make_service_fn(move |_conn| { + let port_handler = port_handler.clone(); + async move { + Ok::<_, Infallible>(service_fn(move |_req| { + let port_handler = port_handler.clone(); + async move { + Ok::<_, Infallible>(Response::new(Body::from( + port_handler.lock().await.status_string(), + ))) + } + })) + } + })); + + // Run this server for... forever! + if let Err(e) = server.await { + eprintln!("server error: {}", e); + } +} diff --git a/src/main.rs b/src/main.rs index 20ea299..05bc274 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,6 +49,27 @@ pub struct Config { listen_addr: SocketAddr, #[serde(deserialize_with = "parse_socket_addr")] dyn_ip_server: SocketAddr, + #[cfg(feature = "debug_server")] + #[serde(deserialize_with = "maybe_parse_socket_addr")] + #[serde(default)] + debug_server_addr: Option, +} + +fn maybe_parse_socket_addr<'de, D: Deserializer<'de>>( + deserializer: D, +) -> Result, D::Error> { + use serde::de::Error; + + Option::::deserialize(deserializer)? + .map(|s| { + Ok::<_, D::Error>( + s.to_socket_addrs() + .map_err(|err| D::Error::custom(err))? + .next() + .ok_or_else(|| D::Error::invalid_length(0, &"one or more"))?, + ) + }) + .transpose() } fn parse_socket_addr<'de, D: Deserializer<'de>>(deserializer: D) -> Result { @@ -109,9 +130,13 @@ async fn main() -> anyhow::Result<()> { } #[cfg(feature = "debug_server")] - tokio::spawn(debug_server()); + if let Some(debug_server_addr) = config.debug_server_addr { + println!("starting debug server on {debug_server_addr:?}"); + tokio::spawn(debug_server(debug_server_addr, port_handler.clone())); + } let listener = TcpListener::bind(config.listen_addr).await?; + println!("listening on {}", config.listen_addr); while let Ok((mut stream, addr)) = listener.accept().await { println!("connection from {addr}"); @@ -127,7 +152,7 @@ async fn main() -> anyhow::Result<()> { .await; if let Err(err) = res { - println!("client at {addr} had an error: {err}"); + println!("client at {addr} had an error: {err:?}"); let mut packet = Packet::default(); @@ -148,6 +173,7 @@ async fn main() -> anyhow::Result<()> { if let Some(port_state) = port_handler.port_state.get_mut(&port) { port_state.new_state(PortStatus::Disconnected); + port_handler.register_update(); } if let Some(listener) = handler_metadata.listener.take() { @@ -256,6 +282,7 @@ async fn connection_handler( .context("dy-ip update")?; } + port_handler.register_update(); port_handler .port_state .entry(port) diff --git a/src/ports.rs b/src/ports.rs index 0029a57..201d2dd 100644 --- a/src/ports.rs +++ b/src/ports.rs @@ -1,6 +1,7 @@ use std::{ + borrow::Cow, collections::{BTreeSet, HashMap, HashSet}, - fmt::Debug, + fmt::{Debug, Display}, fs::File, io::{BufReader, BufWriter}, ops::Range, @@ -17,7 +18,7 @@ use crate::{ packets::Packet, Config, Number, Port, UnixTimestamp, PORT_OWNERSHIP_TIMEOUT, PORT_RETRY_TIME, }; -#[derive(Default, Debug, Serialize, Deserialize)] +#[derive(Default, Serialize, Deserialize)] pub struct PortHandler { #[serde(skip)] pub last_update: Option, @@ -27,20 +28,130 @@ pub struct PortHandler { allowed_ports: AllowedPorts, + #[serde(skip)] free_ports: HashSet, errored_ports: BTreeSet<(UnixTimestamp, Port)>, allocated_ports: HashMap, - #[serde(skip)] pub port_state: HashMap, } -#[derive(Default, Debug)] +#[derive(Hash, PartialEq, Eq)] +struct DisplayAsDebug(T); +impl Debug for DisplayAsDebug { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +fn duration_in_hours(duration: Duration) -> String { + let seconds_elapsed = duration.as_secs(); + + let hours = seconds_elapsed / (60 * 60); + let minutes = (seconds_elapsed / 60) % 60; + let seconds = seconds_elapsed % 60; + + match (hours > 0, minutes > 0) { + (true, _) => format!("{hours}h {minutes}min {seconds}s"), + (false, true) => format!("{minutes}min {seconds}s"), + _ => format!("{:.0?}", duration), + } +} + +fn format_instant(instant: Instant) -> String { + let when = duration_in_hours(instant.elapsed()) + " ago"; + + #[cfg(feature = "chrono")] + let when = (|| -> anyhow::Result<_> { + use chrono::{Local, TimeZone}; + + let date = Local + .timestamp_opt( + (SystemTime::now().duration_since(UNIX_EPOCH)? - instant.elapsed()) + .as_secs() + .try_into()?, + 0, + ) + .latest() + .ok_or(anyhow!("invalid update timestamp"))? + .format("%Y-%m-%d %H:%M:%S"); + + Ok(format!("{date} ({when})")) + })() + .unwrap_or(when); + + when +} + +fn instant_from_timestamp(timestamp: UnixTimestamp) -> Instant { + Instant::now() - UNIX_EPOCH.elapsed().unwrap() + Duration::from_secs(timestamp) +} + +impl Debug for PortHandler { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + const SHOW_N_FREE_PORTS: usize = 20; + + let last_update = self + .last_update + .map(|last_update| Cow::from(format_instant(last_update))) + .unwrap_or(Cow::from("?")); + + let mut free_ports = self + .free_ports + .iter() + .take(SHOW_N_FREE_PORTS) + .map(|x| DisplayAsDebug(x.to_string())) + .collect::>(); + + if let Some(n_not_shown) = self.free_ports.len().checked_sub(SHOW_N_FREE_PORTS) { + if n_not_shown > 0 { + free_ports.push(DisplayAsDebug(format!("[{n_not_shown} more]"))); + } + } + + let errored_ports = self + .errored_ports + .iter() + .rev() + .map(|&(since, port)| { + DisplayAsDebug(format!( + "{port:5}: {}", + format_instant(instant_from_timestamp(since)) + )) + }) + .collect::>(); + + f.debug_struct("PortHandler") + .field("last_update", &DisplayAsDebug(last_update)) + .field("port_guards", &self.port_guards) + .field("allowed_ports", &self.allowed_ports) + .field("free_ports", &free_ports) + .field("errored_ports", &errored_ports) + .field("allocated_ports", &self.allocated_ports) + .field("port_state", &self.port_state) + .finish() + } +} + +#[derive(Default, Serialize, Deserialize)] pub struct PortState { last_change: UnixTimestamp, + #[serde(skip)] status: PortStatus, } +impl Debug for PortState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PortState") + .field( + "last_change", + &DisplayAsDebug(format_instant(instant_from_timestamp(self.last_change))), + ) + .field("status", &self.status) + .finish() + } +} + impl PortState { pub fn new_state(&mut self, status: PortStatus) { self.last_change = SystemTime::now() @@ -52,7 +163,7 @@ impl PortState { } } -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq, Serialize)] pub enum PortStatus { Disconnected, Idle, @@ -78,13 +189,21 @@ impl AllowedPorts { } impl PortHandler { + pub fn status_string(&self) -> String { + format!("{self:#?}") + } + pub fn register_update(&mut self) { self.last_update = Some(Instant::now()); } pub fn store(&self, cache: &Path) -> anyhow::Result<()> { println!("storing database"); - serde_json::to_writer(BufWriter::new(File::create(cache)?), self)?; + let temp_file = cache.with_extension(".temp"); + + serde_json::to_writer(BufWriter::new(File::create(&temp_file)?), self)?; + std::fs::rename(temp_file, cache)?; + Ok(()) } @@ -105,28 +224,32 @@ impl PortHandler { self.allowed_ports = allowed_ports.clone(); - self.free_ports.clear(); + self.free_ports.clear(); // remove all ports self.free_ports - .extend(self.allowed_ports.0.iter().cloned().flatten()); + .extend(self.allowed_ports.0.iter().cloned().flatten()); // add allowed ports self.free_ports.shrink_to_fit(); // we are at the maximum number of ports we'll ever reach self.errored_ports - .retain(|(_, port)| self.allowed_ports.is_allowed(*port)); + .retain(|(_, port)| self.allowed_ports.is_allowed(*port)); // remove errored ports that are no longer allowed self.allocated_ports - .retain(|_, port| self.allowed_ports.is_allowed(*port)); + .retain(|_, port| self.allowed_ports.is_allowed(*port)); // remove allocated ports that are no longer allowed self.free_ports.retain(|port| { - self.allocated_ports + let is_allocted = self + .allocated_ports .iter() .find(|(_, allocated_port)| *allocated_port == port) - .is_none() - && self - .errored_ports - .iter() - .find(|(_, errored_port)| errored_port == port) - .is_none() + .is_some(); + + let is_errored = self + .errored_ports + .iter() + .find(|(_, errored_port)| errored_port == port) + .is_some(); + + !(is_allocted || is_errored) }); } @@ -177,7 +300,9 @@ struct Rejector { impl Debug for Rejector { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("PortGuard").finish() + f.debug_struct("PortGuard") + .field("message", &self.state.1) + .finish() } } @@ -302,6 +427,7 @@ impl PortHandler { } pub fn mark_port_error(&mut self, number: Number, port: Port) { + println!("registering an error on port {port} for number {number}"); self.register_update(); self.errored_ports.insert(( @@ -314,5 +440,6 @@ impl PortHandler { self.allocated_ports.remove(&number); self.free_ports.remove(&port); + self.port_state.remove(&port); } }