feat: add login_start packet parsing for username extraction

This commit is contained in:
Tamipes 2026-01-19 16:26:05 +01:00
parent c697449b20
commit d531ba59a0
4 changed files with 55 additions and 5 deletions

View file

@ -11,6 +11,7 @@ use crate::mc_server::{MinecraftAPI, MinecraftServerHandle, ServerDeploymentStat
use crate::opaque_error::OpaqueError; use crate::opaque_error::OpaqueError;
use crate::packets::clientbound::status::StatusStructNew; use crate::packets::clientbound::status::StatusStructNew;
use crate::packets::serverbound::handshake::Handshake; use crate::packets::serverbound::handshake::Handshake;
use crate::packets::serverbound::login::LoginStart;
use crate::packets::{Packet, SendPacket}; use crate::packets::{Packet, SendPacket};
mod kube_cache; mod kube_cache;
@ -111,7 +112,15 @@ async fn process_connection<T: MinecraftServerHandle>(
packets::ProtocolState::Status => { packets::ProtocolState::Status => {
handle_status(&mut client_stream, &handshake, api).await?; handle_status(&mut client_stream, &handshake, api).await?;
} }
packets::ProtocolState::Login => handle_login(&mut client_stream, &handshake, api).await?, packets::ProtocolState::Login => {
// This block of packet parsing is needed here, so the span with the
// username is correctly propagated due to the async nature of things
let packet = Packet::parse(&mut client_stream).await?;
let login_packet = packets::serverbound::login::LoginStart::parse(packet)
.await
.ok_or("Failed to parse login start packet".to_string())?;
handle_login(&mut client_stream, &handshake, login_packet, api).await?
}
packets::ProtocolState::Transfer => { packets::ProtocolState::Transfer => {
return Err(OpaqueError::create( return Err(OpaqueError::create(
"next state is transfer; Not yet implemented!", "next state is transfer; Not yet implemented!",
@ -212,10 +221,11 @@ async fn handle_status<T: MinecraftServerHandle>(
return mc_server::handle_ping(client_stream).await; return mc_server::handle_ping(client_stream).await;
} }
#[tracing::instrument(level = "info", fields(server_addr = handshake.get_server_address(),server_port = handshake.server_port.get_value()),skip(client_stream, handshake, api))] #[tracing::instrument(level = "info", fields(server_addr = handshake.get_server_address(),server_port = handshake.server_port.get_value(),username = login_start.name.get_value()),skip(client_stream, handshake, api, login_start))]
async fn handle_login<T: MinecraftServerHandle>( async fn handle_login<T: MinecraftServerHandle>(
client_stream: &mut TcpStream, client_stream: &mut TcpStream,
handshake: &Handshake, handshake: &Handshake,
login_start: LoginStart,
api: impl MinecraftAPI<T>, api: impl MinecraftAPI<T>,
) -> Result<(), OpaqueError> { ) -> Result<(), OpaqueError> {
let server = api let server = api
@ -244,6 +254,13 @@ async fn handle_login<T: MinecraftServerHandle>(
.await .await
.map_err(|_| "failed to forward handshake packet to minecraft server")?; .map_err(|_| "failed to forward handshake packet to minecraft server")?;
login_start
.send_packet(&mut server_stream)
.await
.map_err(|e| {
"failed to forward login packet to server before splicing".to_string()
})?;
tracing::info!("proxying with splice"); tracing::info!("proxying with splice");
let traffic = tokio_splice2::io::SpliceBidiIo { io_sl2sr, io_sr2sl } let traffic = tokio_splice2::io::SpliceBidiIo { io_sl2sr, io_sr2sl }
.execute(client_stream, &mut server_stream) .execute(client_stream, &mut server_stream)

View file

@ -42,14 +42,12 @@ pub async fn complete_status_request(
/// Disconnects the client. /// Disconnects the client.
/// ///
/// It works if the client is in the login state, and it /// It works if the client is in the login state, and it
/// has *already* and *only* sent the handshake packet. /// has *already* and *only* sent the **handshake** and **login_start** packet.
#[tracing::instrument(skip(client_stream))] #[tracing::instrument(skip(client_stream))]
pub async fn send_disconnect( pub async fn send_disconnect(
client_stream: &mut TcpStream, client_stream: &mut TcpStream,
reason: &str, reason: &str,
) -> Result<(), OpaqueError> { ) -> Result<(), OpaqueError> {
let _client_packet = Packet::parse(client_stream).await?;
let disconnect_packet = let disconnect_packet =
crate::packets::clientbound::login::Disconnect::set_reason(reason.to_owned()) crate::packets::clientbound::login::Disconnect::set_reason(reason.to_owned())
.await .await

View file

@ -0,0 +1,34 @@
use tokio::io::AsyncWriteExt;
use crate::{
packets::{Packet, SendPacket},
types::VarString,
};
/// id: 0x00
pub struct LoginStart {
all: Vec<u8>,
pub name: VarString,
// TODO: implement uuid parsing
// uuid: Vec<u8>,
}
impl LoginStart {
pub async fn parse(packet: Packet) -> Option<LoginStart> {
let mut reader = packet.data.clone();
let name = VarString::parse(&mut reader).await?;
Some(LoginStart {
all: packet.all,
name: name,
})
}
}
impl SendPacket for LoginStart {
async fn send_packet(&self, stream: &mut tokio::net::TcpStream) -> std::io::Result<()> {
stream.write_all(&self.all).await?;
stream.flush().await?;
Ok(())
}
}

View file

@ -1,2 +1,3 @@
pub mod handshake; pub mod handshake;
pub mod login;
pub mod status; pub mod status;