feat: return ping and parse version

This commit is contained in:
əlemi 2024-02-16 03:34:45 +01:00
parent 506d59e203
commit 018bd76718
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 44 additions and 5 deletions

View file

@ -3,7 +3,7 @@ use std::net::ToSocketAddrs;
use clap::Parser; use clap::Parser;
use axum::{extract::Query, routing::get, Json, Router}; use axum::{extract::Query, routing::get, Json, Router};
use mumble::ping_mumble_server; use mumble::{parse_version, ping_mumble_server};
use proto::PongPacket; use proto::PongPacket;
mod proto; mod proto;
@ -32,15 +32,38 @@ struct PingOptions {
port: Option<u16>, port: Option<u16>,
} }
async fn ping_server(Query(options): Query<PingOptions>) -> Result<Json<PongPacket>, 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<PingOptions>) -> Result<Json<PongResponse>, String> {
let tuple = (options.host, options.port.unwrap_or(64738)); let tuple = (options.host, options.port.unwrap_or(64738));
let before = chrono::Utc::now().timestamp_micros() as u64;
match tuple.to_socket_addrs() { match tuple.to_socket_addrs() {
Err(e) => Err(format!("invalid address: {e}")), Err(e) => Err(format!("invalid address: {e}")),
Ok(mut addrs) => match addrs.next() { Ok(mut addrs) => match addrs.next() {
None => Err("could not resolve host".to_string()), None => Err("could not resolve host".to_string()),
Some(addr) => match ping_mumble_server(addr).await { Some(addr) => match ping_mumble_server(addr).await {
Ok(pong) => Ok(Json(pong)),
Err(e) => Err(format!("could not ping server: {e}")), 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),
}))
},
} }
} }
} }

View file

@ -20,3 +20,13 @@ pub async fn ping_mumble_server(addr: SocketAddr) -> std::io::Result<PongPacket>
PongPacket::deserialize(&buf).await PongPacket::deserialize(&buf).await
} }
pub fn parse_version(version: u32) -> String {
format!(
"{}.{}.{}.{}",
version >> 24,
(version >> 16) & 255,
(version >> 8) & 255,
version & 255,
)
}

View file

@ -18,9 +18,15 @@ async function pingServer() {
try { try {
let res = await fetch("https://api.fantabos.co/mumble/ping") let res = await fetch("https://api.fantabos.co/mumble/ping")
let doc = await res.json() let doc = await res.json()
stats.innerHTML = `<tr> stats.innerHTML = `
<tr>
<td><code>users</code> ${doc.users}/${doc.max_users}</td> <td><code>users</code> ${doc.users}/${doc.max_users}</td>
<td><code>bandwidth</code> ${doc.bandwidth/1000}kbps</td> <td><code>bandwidth</code> ${doc.bandwidth}</td>
<td><code>version</code> ${doc.version}</td>
</tr>
<tr>
<td><code>ping</code> ${doc.ping}</td>
<td><code>roundtrip</code> ${doc.roundtrip}</td>
<td><code>time</code> ${doc.time}</td> <td><code>time</code> ${doc.time}</td>
</tr>`; </tr>`;
loading.remove(); loading.remove();