feat: add login_start packet parsing for username extraction
This commit is contained in:
parent
c697449b20
commit
d531ba59a0
4 changed files with 55 additions and 5 deletions
21
src/main.rs
21
src/main.rs
|
|
@ -11,6 +11,7 @@ use crate::mc_server::{MinecraftAPI, MinecraftServerHandle, ServerDeploymentStat
|
|||
use crate::opaque_error::OpaqueError;
|
||||
use crate::packets::clientbound::status::StatusStructNew;
|
||||
use crate::packets::serverbound::handshake::Handshake;
|
||||
use crate::packets::serverbound::login::LoginStart;
|
||||
use crate::packets::{Packet, SendPacket};
|
||||
|
||||
mod kube_cache;
|
||||
|
|
@ -111,7 +112,15 @@ async fn process_connection<T: MinecraftServerHandle>(
|
|||
packets::ProtocolState::Status => {
|
||||
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 => {
|
||||
return Err(OpaqueError::create(
|
||||
"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;
|
||||
}
|
||||
|
||||
#[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>(
|
||||
client_stream: &mut TcpStream,
|
||||
handshake: &Handshake,
|
||||
login_start: LoginStart,
|
||||
api: impl MinecraftAPI<T>,
|
||||
) -> Result<(), OpaqueError> {
|
||||
let server = api
|
||||
|
|
@ -244,6 +254,13 @@ async fn handle_login<T: MinecraftServerHandle>(
|
|||
.await
|
||||
.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");
|
||||
let traffic = tokio_splice2::io::SpliceBidiIo { io_sl2sr, io_sr2sl }
|
||||
.execute(client_stream, &mut server_stream)
|
||||
|
|
|
|||
|
|
@ -42,14 +42,12 @@ pub async fn complete_status_request(
|
|||
/// Disconnects the client.
|
||||
///
|
||||
/// 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))]
|
||||
pub async fn send_disconnect(
|
||||
client_stream: &mut TcpStream,
|
||||
reason: &str,
|
||||
) -> Result<(), OpaqueError> {
|
||||
let _client_packet = Packet::parse(client_stream).await?;
|
||||
|
||||
let disconnect_packet =
|
||||
crate::packets::clientbound::login::Disconnect::set_reason(reason.to_owned())
|
||||
.await
|
||||
|
|
|
|||
34
src/packets/serverbound/login.rs
Normal file
34
src/packets/serverbound/login.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod handshake;
|
||||
pub mod login;
|
||||
pub mod status;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue