From 6949c751888adecfb66b032d1e0996d3639abeaf Mon Sep 17 00:00:00 2001 From: doinkythederp Date: Sun, 10 Mar 2024 13:16:49 -0700 Subject: [PATCH] feat: improve support for older versions --- src/parse.rs | 13 +++++++++++-- src/protocol.rs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index bbff892..db8db39 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -15,7 +15,7 @@ pub struct JavaServerInfo { #[serde(deserialize_with = "de_description")] pub description: ServerDescription, pub favicon: Option, - #[serde(rename = "deserialize_description")] + #[serde(rename = "modinfo")] pub mod_info: Option, } @@ -87,7 +87,16 @@ pub struct ServerDescription { pub struct ServerModInfo { #[serde(rename = "type")] pub loader_type: String, - // pub mod_list: Vec, + #[serde(rename = "modList")] + pub mod_list: Vec, +} + +#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)] +#[non_exhaustive] +pub struct ServerMod { + #[serde(rename = "modid")] + pub mod_id: String, + pub version: String, } impl std::str::FromStr for JavaServerInfo { diff --git a/src/protocol.rs b/src/protocol.rs index 8590401..499ffa4 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -17,6 +17,8 @@ use tokio::{ io::{AsyncReadExt, AsyncWriteExt, BufWriter}, net::TcpStream, }; +use tracing::error; +use tracing::info; use tracing::{debug, event, instrument, trace, Level}; mod frame; @@ -52,7 +54,11 @@ pub enum ProtocolError { backtrace: Backtrace, }, /// Packet received out of order. - FrameOutOfOrder { backtrace: Backtrace }, + FrameOutOfOrder { + backtrace: Backtrace, + expected: &'static str, + got: Frame, + }, /// Failed to parse JSON response. #[snafu(display("Failed to parse JSON response: {source}"), context(false))] JsonParse { @@ -100,7 +106,6 @@ impl SlpProtocol { } /// Sends frame data over the connection as a packet. - #[instrument] pub async fn write_frame(&mut self, frame: Frame) -> Result<(), ProtocolError> { debug!("Writing frame: {frame:?}"); @@ -170,6 +175,7 @@ impl SlpProtocol { // Attempt to parse a frame from the buffered data. If enough data // has been buffered, the frame is returned. if let Some(frame) = self.parse_frame(server_state)? { + debug!("Received frame: {frame:?}"); return Ok(Some(frame)); } @@ -185,12 +191,13 @@ impl SlpProtocol { // there is, this means that the peer closed the socket while // sending a frame. if self.buffer.is_empty() { + info!("Connection closed cleanly"); return Ok(None); - } else { - return Err(ProtocolError::ConnectionClosed { - backtrace: Backtrace::generate(), - }); } + error!("Connection closed unexpectedly"); + return Err(ProtocolError::ConnectionClosed { + backtrace: Backtrace::generate(), + }); } } } @@ -245,7 +252,13 @@ impl SlpProtocol { .context(ConnectionClosedSnafu)?; let frame_data = match frame { Frame::StatusResponse { json } => json, - _ => return FrameOutOfOrderSnafu.fail(), + frame => { + return FrameOutOfOrderSnafu { + expected: "StatusResponse", + got: frame, + } + .fail() + } }; Ok(JavaServerInfo::from_str(&frame_data)?) } @@ -266,8 +279,12 @@ impl SlpProtocol { .await? .context(ConnectionClosedSnafu)?; match frame { - Frame::PingResponse { payload: _ } => Ok(ping_time.elapsed()), - _ => FrameOutOfOrderSnafu.fail(), + Frame::PingResponse { .. } | Frame::StatusResponse { .. } => Ok(ping_time.elapsed()), + frame => FrameOutOfOrderSnafu { + expected: "PingResponse", + got: frame, + } + .fail(), } } }