Revert "Unify server pinging implementations between app and backend (#5510)" (#5558)

This commit is contained in:
aecsocket
2026-03-13 20:58:57 +00:00
committed by GitHub
parent 4792985e52
commit 31b541007d
13 changed files with 222 additions and 243 deletions

View File

@@ -6,13 +6,14 @@ use crate::state::attached_world_data::AttachedWorldData;
use crate::state::{
Profile, ProfileInstallStage, attached_world_data, server_join_log,
};
use crate::util::io;
use crate::util::protocol_version::OLD_PROTOCOL_VERSIONS;
pub use crate::util::protocol_version::ProtocolVersion;
pub use crate::util::server_ping::{
ServerGameProfile, ServerPlayers, ServerStatus, ServerVersion,
};
use crate::{Context, ErrorKind, Result, State, launcher};
use crate::util::{io, server_ping};
use crate::{Error, ErrorKind, Result, State, launcher};
use async_minecraft_ping::ServerDescription;
use async_walkdir::WalkDir;
use async_zip::{Compression, ZipEntryBuilder};
use chrono::{DateTime, Local, TimeZone, Utc};
@@ -909,54 +910,67 @@ pub async fn get_server_status(
"Pinging {address} with protocol version {protocol_version:?}"
);
// get_server_status_old(address, protocol_version).await
get_server_status_new(address, protocol_version).await
get_server_status_old(address, protocol_version).await
// get_server_status_new(address, protocol_version).await
}
// async fn _get_server_status_old(
// address: &str,
// protocol_version: Option<ProtocolVersion>,
// ) -> Result<ServerStatus> {
// let (original_host, original_port) = parse_server_address(address)?;
// let (host, port) =
// resolve_server_address(original_host, original_port).await?;
// tracing::debug!(
// "Pinging {address} with protocol version {protocol_version:?}"
// );
// server_ping::get_server_status(
// &(&host as &str, port),
// (original_host, original_port),
// protocol_version,
// )
// .await
async fn get_server_status_new(
async fn get_server_status_old(
address: &str,
protocol_version: Option<ProtocolVersion>,
) -> Result<ServerStatus> {
let (original_host, original_port) = parse_server_address(address)?;
let (host, port) =
resolve_server_address(original_host, original_port).await?;
tracing::debug!(
"Pinging {address} with protocol version {protocol_version:?}"
);
server_ping::get_server_status(
&(&host as &str, port),
(original_host, original_port),
protocol_version,
)
.await
}
async fn _get_server_status_new(
address: &str,
protocol_version: Option<ProtocolVersion>,
) -> Result<ServerStatus> {
let (address, port) = match address.rsplit_once(':') {
Some((addr, port)) => {
let port = port.parse::<u16>().map_err(|_err| {
Error::from(ErrorKind::InputError("invalid port number".into()))
})?;
(addr, port)
}
None => (address, 25565),
};
let mut builder = async_minecraft_ping::ConnectionConfig::build(address)
.with_port(port)
.with_srv_lookup();
if let Some(version) = protocol_version {
builder = builder.with_protocol_version(version.version as usize)
}
let conn = builder
.connect()
.await
.wrap_err("failed to connect to server")?;
let conn = builder.connect().await.map_err(|_err| {
Error::from(ErrorKind::InputError("failed to connect to server".into()))
})?;
let ping_conn = conn
.status()
.await
.wrap_err("failed to get server status")?;
let ping_conn = conn.status().await.map_err(|_err| {
Error::from(ErrorKind::InputError("failed to get server status".into()))
})?;
let status = &ping_conn.status;
let description = status.description.as_ref().map(|d| {
let json =
serde_json::to_string(d).expect("serializing should not fail");
RawValue::from_string(json)
.expect("converting to `RawValue` should not fail")
});
let description = match &status.description {
ServerDescription::Plain(text) => {
serde_json::value::to_raw_value(&text).ok()
}
ServerDescription::Object { text } => {
// TODO: `text` always seems to be empty?
RawValue::from_string(text.clone()).ok()
}
};
let players = ServerPlayers {
max: status.players.max,
@@ -986,10 +1000,9 @@ async fn get_server_status_new(
let latency = {
let start = Instant::now();
let ping_magic = Utc::now().timestamp_millis().cast_unsigned();
ping_conn
.ping(ping_magic)
.await
.wrap_err("failed to do ping")?;
ping_conn.ping(ping_magic).await.map_err(|_err| {
Error::from(ErrorKind::InputError("failed to do ping".into()))
})?;
start.elapsed().as_millis() as i64
};