feat: add better logging span to process_connection
This commit is contained in:
parent
ee7a05152e
commit
1229127101
1 changed files with 40 additions and 26 deletions
66
src/main.rs
66
src/main.rs
|
|
@ -103,35 +103,49 @@ async fn process_connection<T: MinecraftServerHandle>(
|
||||||
api: impl MinecraftAPI<T>,
|
api: impl MinecraftAPI<T>,
|
||||||
config: Config,
|
config: Config,
|
||||||
) -> Result<(), OpaqueError> {
|
) -> 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 ---
|
// --- Handshake ---
|
||||||
let handshake;
|
let handshake;
|
||||||
let next_server_state;
|
let packet_id = client_packet.id.get_int();
|
||||||
let packet_id = client_packet.id.get_int();
|
if packet_id != 0 {
|
||||||
if packet_id != 0 {
|
return Err(OpaqueError::create(&format!(
|
||||||
return Err(OpaqueError::create(&format!(
|
"Client HANDSHAKE -> bad packet; id={packet_id} Disconnecting..."
|
||||||
"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)
|
let handshake = match first_packet(&mut client_stream, config).await? {
|
||||||
.await
|
Some(x) => x,
|
||||||
.ok_or_else(|| "Client HANDSHAKE -> malformed packet; Disconnecting...".to_string())?;
|
// this is needed because of the filter
|
||||||
|
None => return Ok(()),
|
||||||
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 next_server_state = handshake.get_next_state();
|
||||||
match next_server_state {
|
match next_server_state {
|
||||||
packets::ProtocolState::Status => {
|
packets::ProtocolState::Status => {
|
||||||
handle_status(&mut client_stream, &handshake, api).await?;
|
handle_status(&mut client_stream, &handshake, api).await?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue