From 122912710136b8589c80e36b83d28bcac08988f1 Mon Sep 17 00:00:00 2001 From: Tamipes Date: Fri, 29 May 2026 10:40:51 +0200 Subject: [PATCH] feat: add better logging span to `process_connection` --- src/main.rs | 66 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index fe32e77..e4039f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,35 +103,49 @@ async fn process_connection( api: impl MinecraftAPI, config: Config, ) -> Result<(), OpaqueError> { - let client_packet = Packet::parse(&mut client_stream).await?; + // this is wrapper so that async doesnt mess up the span, and + // to make sure this doesn't propagate to later `handle_*` + #[tracing::instrument(level = "info", skip(client_stream, config))] + async fn first_packet( + client_stream: &mut TcpStream, + config: Config, + ) -> Result, OpaqueError> { + let client_packet = Packet::parse(client_stream).await?; - // --- Handshake --- - let handshake; - let next_server_state; - let packet_id = client_packet.id.get_int(); - if packet_id != 0 { - return Err(OpaqueError::create(&format!( - "Client HANDSHAKE -> bad packet; id={packet_id} Disconnecting..." - ))); + // --- Handshake --- + let handshake; + let packet_id = client_packet.id.get_int(); + if packet_id != 0 { + return Err(OpaqueError::create(&format!( + "Client HANDSHAKE -> bad packet; id={packet_id} Disconnecting..." + ))); + } + handshake = packets::serverbound::handshake::Handshake::parse(client_packet) + .await + .ok_or_else(|| "Client HANDSHAKE -> malformed packet; Disconnecting...".to_string())?; + + let filter = eval_boolean(&format!( + "addr=\"{}\";{}", + handshake.get_server_address(), + config.filter_conn + )) + .map_err(|e| format!("filter error! err={:?}", e))?; + if filter { + // TODO: if the server just returns here, the client does not know it + // and sends a packet with the 122 WeirdID + tracing::trace!("filtered out the connection"); + return Ok(None); + } + + Ok(Some(handshake)) } - handshake = packets::serverbound::handshake::Handshake::parse(client_packet) - .await - .ok_or_else(|| "Client HANDSHAKE -> malformed packet; Disconnecting...".to_string())?; - - let filter = eval_boolean(&format!( - "addr=\"{}\";{}", - handshake.get_server_address(), - config.filter_conn - )) - .map_err(|e| format!("filter error! err={:?}", e))?; - if filter { - // TODO: if the server just returns here, the client does not know it - // and sends a packet with the 122 WeirdID - return Ok(()); - } - - next_server_state = handshake.get_next_state(); + let handshake = match first_packet(&mut client_stream, config).await? { + Some(x) => x, + // this is needed because of the filter + None => return Ok(()), + }; + let next_server_state = handshake.get_next_state(); match next_server_state { packets::ProtocolState::Status => { handle_status(&mut client_stream, &handshake, api).await?;