mirror of
https://git.alemi.dev/mumble-stats-api.git
synced 2024-12-22 12:44:53 +01:00
feat: return ping and parse version
This commit is contained in:
parent
506d59e203
commit
018bd76718
3 changed files with 44 additions and 5 deletions
29
src/main.rs
29
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<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 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),
|
||||
}))
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,3 +20,13 @@ pub async fn ping_mumble_server(addr: SocketAddr) -> std::io::Result<PongPacket>
|
|||
|
||||
PongPacket::deserialize(&buf).await
|
||||
}
|
||||
|
||||
pub fn parse_version(version: u32) -> String {
|
||||
format!(
|
||||
"{}.{}.{}.{}",
|
||||
version >> 24,
|
||||
(version >> 16) & 255,
|
||||
(version >> 8) & 255,
|
||||
version & 255,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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 = `<tr>
|
||||
stats.innerHTML = `
|
||||
<tr>
|
||||
<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>
|
||||
</tr>`;
|
||||
loading.remove();
|
||||
|
|
Loading…
Reference in a new issue