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, &self,
_server: &SharedServer<Self>, _server: &SharedServer<Self>,
_remote_addr: SocketAddr, _remote_addr: SocketAddr,
_protocol_version: i32,
) -> ServerListPing { ) -> ServerListPing {
ServerListPing::Respond { ServerListPing::Respond {
online_players: self.player_count.load(Ordering::SeqCst) as i32, online_players: self.player_count.load(Ordering::SeqCst) as i32,

View file

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

View file

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

View file

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

View file

@ -69,6 +69,7 @@ impl Config for Game {
&self, &self,
_server: &SharedServer<Self>, _server: &SharedServer<Self>,
_remote_addr: SocketAddr, _remote_addr: SocketAddr,
_protocol_version: i32,
) -> ServerListPing { ) -> ServerListPing {
ServerListPing::Respond { ServerListPing::Respond {
online_players: self.player_count.load(Ordering::SeqCst) as i32, 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, &self,
shared: &SharedServer<Self>, shared: &SharedServer<Self>,
remote_addr: SocketAddr, remote_addr: SocketAddr,
protocol_version: i32,
) -> ServerListPing { ) -> ServerListPing {
ServerListPing::Ignore ServerListPing::Ignore
} }

View file

@ -391,10 +391,16 @@ pub(crate) mod test {
use super::*; use super::*;
def_struct! { def_struct! {
TestPacket 0xfff { TestPacket {
first: String, first: String,
second: Vec<u16>, second: Vec<u16>,
third: u64 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 // TODO: peek stream for 0xFE legacy ping
match c.dec.read_packet::<Handshake>().await?.next_state { let handshake = c.dec.read_packet::<Handshake>().await?;
HandshakeNextState::Status => handle_status(server, &mut c, remote_addr)
match handshake.next_state {
HandshakeNextState::Status => handle_status(server, &mut c, remote_addr, handshake)
.await .await
.context("error during status"), .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 .await
.context("error during login")? .context("error during login")?
{ {
@ -567,10 +569,16 @@ async fn handle_status<C: Config>(
server: SharedServer<C>, server: SharedServer<C>,
c: &mut Codec, c: &mut Codec,
remote_addr: SocketAddr, remote_addr: SocketAddr,
handshake: Handshake,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
c.dec.read_packet::<QueryRequest>().await?; 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 { ServerListPing::Respond {
online_players, online_players,
max_players, max_players,
@ -619,7 +627,13 @@ async fn handle_login<C: Config>(
server: &SharedServer<C>, server: &SharedServer<C>,
c: &mut Codec, c: &mut Codec,
remote_addr: SocketAddr, remote_addr: SocketAddr,
handshake: Handshake,
) -> anyhow::Result<Option<NewClientData>> { ) -> anyhow::Result<Option<NewClientData>> {
if handshake.protocol_version.0 != PROTOCOL_VERSION {
// TODO: send translated disconnect msg?
return Ok(None);
}
let LoginStart { let LoginStart {
username: BoundedString(username), username: BoundedString(username),
sig_data: _, // TODO sig_data: _, // TODO