Move mumble UDP ping into common crate (#33)
The desktop GUI doesn't go through the proxy to reach the Mumble server, so its login screen needs to ping directly. Rather than duplicate the ping logic, move it into the common crate behind an optional `networking` feature (so common stays lightweight when the feature isn't requested). - common: add `ping_server(address, port)` behind `networking` feature - proxy: replace the inline UdpSocket ping + ping.rs codec with a call to common::ping_server; drop the rand dep - client: PlatformInterface::get_status now takes an address; desktop and mobile call common::ping_server directly, web still uses the proxy's /status endpoint - gui: thread the address from the login input through get_status, so it re-pings when the user edits the address Assisted-by: claude-opus-4-7 Reviewed-on: #33 Reviewed-by: restitux <restitux@ohea.xyz> Co-authored-by: Sam Sartor <me@samsartor.com> Co-committed-by: Sam Sartor <me@samsartor.com>
This commit was merged in pull request #33.
This commit is contained in:
+1
-2
@@ -12,7 +12,7 @@ tokio-rustls = "0.26"
|
||||
toml = "0.8"
|
||||
tracing = { version = "^0.1.40", features = ["async-await"] }
|
||||
tracing-subscriber = { version = "^0.3.18", features = ["env-filter"] }
|
||||
mumble-web2-common = { workspace = true }
|
||||
mumble-web2-common = { workspace = true, features = ["networking"] }
|
||||
salvo = { version = "^0.84.2", features = [
|
||||
"quinn",
|
||||
"eyre",
|
||||
@@ -28,4 +28,3 @@ rcgen = "^0.13.2"
|
||||
hmac-sha256 = "^1.1.8"
|
||||
time = "0.3"
|
||||
url = { version = "2", features = ["serde"] }
|
||||
rand = "0.9.2"
|
||||
|
||||
+6
-65
@@ -1,6 +1,5 @@
|
||||
use color_eyre::eyre::{anyhow, bail, Context, Result};
|
||||
use mumble_web2_common::{ProxyOverrides, ServerStatus};
|
||||
use rand::Rng;
|
||||
use mumble_web2_common::{ping_server, ProxyOverrides, ServerStatus};
|
||||
use salvo::conn::rustls::{Keycert, RustlsConfig};
|
||||
use salvo::cors::{AllowOrigin, Cors};
|
||||
use salvo::logging::Logger;
|
||||
@@ -26,8 +25,6 @@ use tracing_subscriber::filter::LevelFilter;
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use url::Url;
|
||||
|
||||
mod ping;
|
||||
|
||||
fn default_cert_alt_names() -> Vec<String> {
|
||||
vec!["localhost".into()]
|
||||
}
|
||||
@@ -179,70 +176,14 @@ pub struct StatusCraft {
|
||||
impl StatusCraft {
|
||||
#[craft(handler)]
|
||||
async fn get_status(&self) -> Json<ServerStatus> {
|
||||
let mut server_status = ServerStatus::default();
|
||||
|
||||
let ping_packet = ping::PingPacket {
|
||||
id: rand::rng().random(),
|
||||
};
|
||||
|
||||
let sock = match tokio::net::UdpSocket::bind("0.0.0.0:0").await {
|
||||
Ok(s) => s,
|
||||
let addr = self.mumble_server_address;
|
||||
match ping_server(&addr.ip().to_string(), addr.port()).await {
|
||||
Ok(status) => Json(status),
|
||||
Err(e) => {
|
||||
error!("Could not bind udp socket: {}", e);
|
||||
return Json(server_status);
|
||||
}
|
||||
};
|
||||
|
||||
match sock.connect(self.mumble_server_address).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
error!("Could not send ping packet: {}", e);
|
||||
return Json(server_status);
|
||||
error!("ping failed: {e:#}");
|
||||
Json(ServerStatus::default())
|
||||
}
|
||||
}
|
||||
|
||||
match sock.send(&<[u8; 12]>::from(ping_packet)).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
error!("Could not send ping packet");
|
||||
return Json(server_status);
|
||||
}
|
||||
}
|
||||
|
||||
let mut pong_buf: [u8; 24] = [0; 24];
|
||||
|
||||
match tokio::time::timeout(
|
||||
tokio::time::Duration::from_secs(1),
|
||||
sock.recv(&mut pong_buf),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
error!("Could not send ping packet");
|
||||
return Json(server_status);
|
||||
}
|
||||
}
|
||||
|
||||
let pong_packet = match ping::PongPacket::try_from(pong_buf.as_slice()) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
error!("Could not parse pong packet: {:?}", e);
|
||||
return Json(server_status);
|
||||
}
|
||||
};
|
||||
|
||||
server_status.success = true;
|
||||
server_status.version = Some((
|
||||
pong_packet.version & 0xFF,
|
||||
(pong_packet.version >> 8) & 0xFF,
|
||||
(pong_packet.version >> 16) & 0xFF,
|
||||
));
|
||||
server_status.users = Some(pong_packet.users);
|
||||
server_status.max_users = Some(pong_packet.max_users);
|
||||
server_status.bandwidth = Some(pong_packet.bandwidth);
|
||||
|
||||
Json(server_status)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
// This code was taken from mumble-protocol-2x (https://github.com/dblsaiko/rust-mumble-protocol)
|
||||
// and originally from mumble-protocol (https://github.com/Johni0702/rust-mumble-protocol)
|
||||
// These projects are licensed under MIT and Apache 2.0.
|
||||
|
||||
//! Ping messages and codec
|
||||
//!
|
||||
//! A Mumble client can send periodic UDP [PingPacket]s to servers
|
||||
//! in order to query their current state and measure latency.
|
||||
//! A server will usually respond with a corresponding [PongPacket] containing
|
||||
//! the requested details.
|
||||
//!
|
||||
//! Both packets are of fixed size and can be converted to/from `u8` arrays/slices via
|
||||
//! the respective `From`/`TryFrom` impls.
|
||||
|
||||
/// A ping packet sent to the server.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PingPacket {
|
||||
/// Opaque, client-generated id.
|
||||
///
|
||||
/// Will be returned by the server unmodified and can be used to correlate
|
||||
/// pong replies to ping requests to e.g. calculate latency.
|
||||
pub id: u64,
|
||||
}
|
||||
|
||||
/// A pong packet sent to the client in reply to a previously received [PingPacket].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct PongPacket {
|
||||
/// Opaque, client-generated id.
|
||||
///
|
||||
/// Should match the value in the corresponding [PingPacket].
|
||||
pub id: u64,
|
||||
|
||||
/// Server version. E.g. `0x010300` for `1.3.0`.
|
||||
pub version: u32,
|
||||
|
||||
/// Current amount of users connected to the server.
|
||||
pub users: u32,
|
||||
|
||||
/// Configured limit on the amount of users which can be connected to the server.
|
||||
pub max_users: u32,
|
||||
|
||||
/// Maximum bandwidth for server-bound speech per client in bits per second
|
||||
pub bandwidth: u32,
|
||||
}
|
||||
|
||||
/// Error during parsing of a [PingPacket].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ParsePingError {
|
||||
/// Ping packets must always be 12 bytes in size.
|
||||
InvalidSize,
|
||||
/// Ping packets must have an all zero header of 4 bytes.
|
||||
InvalidHeader,
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for PingPacket {
|
||||
type Error = ParsePingError;
|
||||
fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
|
||||
match <[u8; 12]>::try_from(buf) {
|
||||
Ok(array) => {
|
||||
if array[0..4] != [0, 0, 0, 0] {
|
||||
Err(ParsePingError::InvalidHeader)
|
||||
} else {
|
||||
Ok(Self {
|
||||
id: u64::from_be_bytes(array[4..12].try_into().unwrap()),
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(_) => Err(ParsePingError::InvalidSize),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PingPacket> for [u8; 12] {
|
||||
fn from(packet: PingPacket) -> Self {
|
||||
let id = packet.id.to_be_bytes();
|
||||
// Is there no nicer way to do this?
|
||||
[
|
||||
0, 0, 0, 0, id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
/// Error during parsing of a [PongPacket].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum ParsePongError {
|
||||
/// Pong packets must always be 24 bytes in size.
|
||||
InvalidSize,
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for PongPacket {
|
||||
type Error = ParsePongError;
|
||||
fn try_from(buf: &[u8]) -> Result<Self, Self::Error> {
|
||||
match <[u8; 24]>::try_from(buf) {
|
||||
Ok(array) => Ok(Self {
|
||||
version: u32::from_be_bytes(array[0..4].try_into().unwrap()),
|
||||
id: u64::from_be_bytes(array[4..12].try_into().unwrap()),
|
||||
users: u32::from_be_bytes(array[12..16].try_into().unwrap()),
|
||||
max_users: u32::from_be_bytes(array[16..20].try_into().unwrap()),
|
||||
bandwidth: u32::from_be_bytes(array[20..24].try_into().unwrap()),
|
||||
}),
|
||||
Err(_) => Err(ParsePongError::InvalidSize),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PongPacket> for [u8; 24] {
|
||||
fn from(packet: PongPacket) -> Self {
|
||||
let version = packet.version.to_be_bytes();
|
||||
let id = packet.id.to_be_bytes();
|
||||
let users = packet.users.to_be_bytes();
|
||||
let max_users = packet.max_users.to_be_bytes();
|
||||
let bandwidth = packet.bandwidth.to_be_bytes();
|
||||
// Is there no nicer way to do this?
|
||||
[
|
||||
version[0],
|
||||
version[1],
|
||||
version[2],
|
||||
version[3],
|
||||
id[0],
|
||||
id[1],
|
||||
id[2],
|
||||
id[3],
|
||||
id[4],
|
||||
id[5],
|
||||
id[6],
|
||||
id[7],
|
||||
users[0],
|
||||
users[1],
|
||||
users[2],
|
||||
users[3],
|
||||
max_users[0],
|
||||
max_users[1],
|
||||
max_users[2],
|
||||
max_users[3],
|
||||
bandwidth[0],
|
||||
bandwidth[1],
|
||||
bandwidth[2],
|
||||
bandwidth[3],
|
||||
]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user