feat: add better logging span to process_connection

This commit is contained in:
Tamipes 2026-05-29 10:40:51 +02:00
parent ee7a05152e
commit 1229127101

View file

@ -103,35 +103,49 @@ async fn process_connection<T: MinecraftServerHandle>(
api: impl MinecraftAPI<T>,
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<Option<packets::serverbound::handshake::Handshake>, 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?;