diff --git a/src/main.rs b/src/main.rs index e417055..d354aa4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use std::net::ToSocketAddrs; use clap::Parser; use axum::{extract::Query, routing::get, Json, Router}; -use mumble::ping_mumble_server; +use mumble::{parse_version, ping_mumble_server}; use proto::PongPacket; mod proto; @@ -32,15 +32,38 @@ struct PingOptions { port: Option, } -async fn ping_server(Query(options): Query) -> Result, String> { +#[derive(serde::Serialize)] +struct PongResponse { + version: String, + users: i32, + max_users: i32, + bandwidth: String, + time: u64, + ping: u64, + roundtrip: u64, +} + +async fn ping_server(Query(options): Query) -> Result, String> { let tuple = (options.host, options.port.unwrap_or(64738)); + let before = chrono::Utc::now().timestamp_micros() as u64; match tuple.to_socket_addrs() { Err(e) => Err(format!("invalid address: {e}")), Ok(mut addrs) => match addrs.next() { None => Err("could not resolve host".to_string()), Some(addr) => match ping_mumble_server(addr).await { - Ok(pong) => Ok(Json(pong)), Err(e) => Err(format!("could not ping server: {e}")), + Ok(pong) => { + let after = chrono::Utc::now().timestamp_micros() as u64; + Ok(Json(PongResponse { + users: pong.users, + max_users: pong.max_users, + bandwidth: format!("{}kbit/s", pong.bandwidth / 1000), + time: pong.time, + ping: pong.time - before, + roundtrip: after - before, + version: parse_version(pong.version), + })) + }, } } } diff --git a/src/mumble.rs b/src/mumble.rs index e9f6e3a..512a868 100644 --- a/src/mumble.rs +++ b/src/mumble.rs @@ -20,3 +20,13 @@ pub async fn ping_mumble_server(addr: SocketAddr) -> std::io::Result PongPacket::deserialize(&buf).await } + +pub fn parse_version(version: u32) -> String { + format!( + "{}.{}.{}.{}", + version >> 24, + (version >> 16) & 255, + (version >> 8) & 255, + version & 255, + ) +} diff --git a/web/matrix_widget.html b/web/matrix_widget.html index 1754ecf..40fe213 100644 --- a/web/matrix_widget.html +++ b/web/matrix_widget.html @@ -18,9 +18,15 @@ async function pingServer() { try { let res = await fetch("https://api.fantabos.co/mumble/ping") let doc = await res.json() - stats.innerHTML = ` + stats.innerHTML = ` + users ${doc.users}/${doc.max_users} - bandwidth ${doc.bandwidth/1000}kbps + bandwidth ${doc.bandwidth} + version ${doc.version} + + + ping ${doc.ping} + roundtrip ${doc.roundtrip} time ${doc.time} `; loading.remove();