[Protocol] Fix Minecraft Java query not being able to specify a hostname (#91)

* Make initial fix

* Fix imports

* Rename query_address to hostname and add this to the mc example

* Fix master_querant example not compiling

* Add extra safety on converting strings to Minecraft Varint strings

* Add docs to RequestSettings

* Fix formatting
This commit is contained in:
CosminPerRam 2023-09-01 22:21:08 +03:00 committed by GitHub
parent 211cd5fd5f
commit a56ca45de6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 118 additions and 28 deletions

View file

@ -11,33 +11,28 @@ use crate::{
use std::net::SocketAddr;
use crate::protocols::minecraft::{as_string, RequestSettings};
use byteorder::LittleEndian;
use serde_json::Value;
#[rustfmt::skip]
const PAYLOAD: [u8; 17] = [
//Packet ID (0)
0x00,
//Protocol Version (-1 to determine version)
0xFF, 0xFF, 0xFF, 0xFF, 0x0F,
//Server address (can be anything)
0x07, 0x47, 0x61, 0x6D, 0x65, 0x44, 0x69, 0x67,
//Server port (can be anything)
0x00, 0x00,
//Next state (1 for status)
0x01
];
pub struct Java {
socket: TcpSocket,
request_settings: RequestSettings,
}
impl Java {
fn new(address: &SocketAddr, timeout_settings: Option<TimeoutSettings>) -> GDResult<Self> {
fn new(
address: &SocketAddr,
timeout_settings: Option<TimeoutSettings>,
request_settings: Option<RequestSettings>,
) -> GDResult<Self> {
let socket = TcpSocket::new(address)?;
socket.apply_timeout(timeout_settings)?;
Ok(Self { socket })
Ok(Self {
socket,
request_settings: request_settings.unwrap_or_default(),
})
}
fn send(&mut self, data: Vec<u8>) -> GDResult<()> {
@ -57,7 +52,24 @@ impl Java {
}
fn send_handshake(&mut self) -> GDResult<()> {
self.send(PAYLOAD.to_vec())?;
let handshake_payload = [
&[
// Packet ID (0)
0x00,
], // Protocol Version (-1 to determine version)
as_varint(self.request_settings.protocol_version).as_slice(),
// Server address (can be anything)
as_string(&self.request_settings.hostname)?.as_slice(),
// Server port (can be anything)
&self.socket.port().to_le_bytes(),
&[
// Next state (1 for status)
0x01,
],
]
.concat();
self.send(handshake_payload)?;
Ok(())
}
@ -143,7 +155,11 @@ impl Java {
})
}
pub fn query(address: &SocketAddr, timeout_settings: Option<TimeoutSettings>) -> GDResult<JavaResponse> {
Self::new(address, timeout_settings)?.get_info()
pub fn query(
address: &SocketAddr,
timeout_settings: Option<TimeoutSettings>,
request_settings: Option<RequestSettings>,
) -> GDResult<JavaResponse> {
Self::new(address, timeout_settings, request_settings)?.get_info()
}
}