From d902b281f783dc0c138b66b89d1fe99900eebfe5 Mon Sep 17 00:00:00 2001 From: aecsocket Date: Wed, 4 Mar 2026 00:03:20 +0000 Subject: [PATCH] comment out impl (#5468) --- Cargo.lock | 1 + packages/app-lib/Cargo.toml | 1 + packages/app-lib/src/api/worlds.rs | 101 ++++++++++++++++++++++++++++- 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 467e34285..7ba5a9fce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10049,6 +10049,7 @@ version = "1.0.0-local" dependencies = [ "ariadne", "async-compression", + "async-minecraft-ping", "async-recursion", "async-tungstenite", "async-walkdir", diff --git a/packages/app-lib/Cargo.toml b/packages/app-lib/Cargo.toml index ad504e250..d2470da7d 100644 --- a/packages/app-lib/Cargo.toml +++ b/packages/app-lib/Cargo.toml @@ -7,6 +7,7 @@ edition.workspace = true [dependencies] ariadne = { workspace = true } async-compression = { workspace = true, features = ["gzip", "tokio"] } +async-minecraft-ping = { workspace = true, features = ["srv"] } async-recursion = { workspace = true } async-tungstenite = { workspace = true, features = [ "tokio-runtime", diff --git a/packages/app-lib/src/api/worlds.rs b/packages/app-lib/src/api/worlds.rs index 06f757fde..be8f2fb6d 100644 --- a/packages/app-lib/src/api/worlds.rs +++ b/packages/app-lib/src/api/worlds.rs @@ -12,7 +12,8 @@ pub use crate::util::server_ping::{ ServerGameProfile, ServerPlayers, ServerStatus, ServerVersion, }; use crate::util::{io, server_ping}; -use crate::{ErrorKind, Result, State, launcher}; +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}; @@ -23,10 +24,12 @@ use futures::StreamExt; use quartz_nbt::{NbtCompound, NbtTag}; use regex::{Regex, RegexBuilder}; use serde::{Deserialize, Serialize}; +use serde_json::value::RawValue; use std::cmp::Reverse; use std::io::Cursor; use std::path::{Path, PathBuf}; use std::sync::LazyLock; +use std::time::Instant; use tokio::io::AsyncWriteExt; use tokio::task::JoinSet; use tokio_util::compat::FuturesAsyncWriteCompatExt; @@ -913,6 +916,18 @@ pub async fn get_profile_protocol_version( pub async fn get_server_status( address: &str, protocol_version: Option, +) -> Result { + tracing::debug!( + "Pinging {address} with protocol version {protocol_version:?}" + ); + + 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, ) -> Result { let (original_host, original_port) = parse_server_address(address)?; let (host, port) = @@ -927,3 +942,87 @@ pub async fn get_server_status( ) .await } + +async fn _get_server_status_new( + address: &str, + protocol_version: Option, +) -> Result { + let (address, port) = match address.rsplit_once(':') { + Some((addr, port)) => { + let port = port.parse::().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.map_err(|_err| { + Error::from(ErrorKind::InputError("failed to connect to server".into())) + })?; + + 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 = 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.cast_signed(), + online: status.players.online.cast_signed(), + sample: status + .players + .sample + .as_ref() + .map(|sample| { + sample + .iter() + .map(|player| ServerGameProfile { + id: player.id.clone(), + name: player.name.clone(), + }) + .collect() + }) + .unwrap_or_default(), + }; + let version = ServerVersion { + name: status.version.name.clone(), + protocol: status.version.protocol, + legacy: false, + }; + let favicon = status.favicon.as_ref().and_then(|url| url.parse().ok()); + + let latency = { + let start = Instant::now(); + let ping_magic = Utc::now().timestamp_millis().cast_unsigned(); + ping_conn.ping(ping_magic).await.map_err(|_err| { + Error::from(ErrorKind::InputError("failed to do ping".into())) + })?; + start.elapsed().as_millis() as i64 + }; + + Ok(ServerStatus { + description, + players: Some(players), + version: Some(version), + favicon, + enforces_secure_chat: false, + ping: Some(latency), + }) +}