Check protocol version during login

This commit is contained in:
Ryan 2022-08-14 15:18:22 -07:00
parent 7fac522a8e
commit 793a795732
8 changed files with 31 additions and 5 deletions

View file

@ -71,6 +71,7 @@ impl Config for Game {
&self,
_server: &SharedServer<Self>,
_remote_addr: SocketAddr,
_protocol_version: i32,
) -> ServerListPing {
ServerListPing::Respond {
online_players: self.player_count.load(Ordering::SeqCst) as i32,

View file

@ -84,6 +84,7 @@ impl Config for Game {
&self,
_server: &SharedServer<Self>,
_remote_addr: SocketAddr,
_protocol_version: i32,
) -> ServerListPing {
ServerListPing::Respond {
online_players: self.player_count.load(Ordering::SeqCst) as i32,

View file

@ -64,6 +64,7 @@ impl Config for Game {
&self,
_server: &SharedServer<Self>,
_remote_addr: SocketAddr,
_protocol_version: i32,
) -> ServerListPing {
ServerListPing::Respond {
online_players: self.player_count.load(Ordering::SeqCst) as i32,

View file

@ -59,6 +59,7 @@ impl Config for Game {
&self,
_server: &SharedServer<Self>,
_remote_addr: SocketAddr,
_protocol_version: i32,
) -> ServerListPing {
ServerListPing::Respond {
online_players: self.player_count.load(Ordering::SeqCst) as i32,

View file

@ -69,6 +69,7 @@ impl Config for Game {
&self,
_server: &SharedServer<Self>,
_remote_addr: SocketAddr,
_protocol_version: i32,
) -> ServerListPing {
ServerListPing::Respond {
online_players: self.player_count.load(Ordering::SeqCst) as i32,

View file

@ -177,6 +177,7 @@ pub trait Config: 'static + Sized + Send + Sync + UnwindSafe + RefUnwindSafe {
&self,
shared: &SharedServer<Self>,
remote_addr: SocketAddr,
protocol_version: i32,
) -> ServerListPing {
ServerListPing::Ignore
}

View file

@ -391,10 +391,16 @@ pub(crate) mod test {
use super::*;
def_struct! {
TestPacket 0xfff {
TestPacket {
first: String,
second: Vec<u16>,
third: u64
}
}
def_packet_group! {
TestPacketGroup {
TestPacket = 12345,
}
}
}

View file

@ -547,11 +547,13 @@ async fn handle_connection<C: Config>(
// TODO: peek stream for 0xFE legacy ping
match c.dec.read_packet::<Handshake>().await?.next_state {
HandshakeNextState::Status => handle_status(server, &mut c, remote_addr)
let handshake = c.dec.read_packet::<Handshake>().await?;
match handshake.next_state {
HandshakeNextState::Status => handle_status(server, &mut c, remote_addr, handshake)
.await
.context("error during status"),
HandshakeNextState::Login => match handle_login(&server, &mut c, remote_addr)
HandshakeNextState::Login => match handle_login(&server, &mut c, remote_addr, handshake)
.await
.context("error during login")?
{
@ -567,10 +569,16 @@ async fn handle_status<C: Config>(
server: SharedServer<C>,
c: &mut Codec,
remote_addr: SocketAddr,
handshake: Handshake,
) -> anyhow::Result<()> {
c.dec.read_packet::<QueryRequest>().await?;
match server.0.cfg.server_list_ping(&server, remote_addr).await {
match server
.0
.cfg
.server_list_ping(&server, remote_addr, handshake.protocol_version.0)
.await
{
ServerListPing::Respond {
online_players,
max_players,
@ -619,7 +627,13 @@ async fn handle_login<C: Config>(
server: &SharedServer<C>,
c: &mut Codec,
remote_addr: SocketAddr,
handshake: Handshake,
) -> anyhow::Result<Option<NewClientData>> {
if handshake.protocol_version.0 != PROTOCOL_VERSION {
// TODO: send translated disconnect msg?
return Ok(None);
}
let LoginStart {
username: BoundedString(username),
sig_data: _, // TODO