mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-06-01 09:42:41 +00:00
Remove errors details as they were quite useless
This commit is contained in:
parent
e8619a7df1
commit
c263b17651
13 changed files with 103 additions and 116 deletions
|
|
@ -4,8 +4,9 @@ This file has code that has been documented by the NodeJS GameDig library (MIT)
|
|||
https://github.com/gamedig/node-gamedig/blob/master/protocols/minecraftbedrock.js
|
||||
*/
|
||||
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::GDResult;
|
||||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::GDError::{PacketBad, TypeParse};
|
||||
use crate::protocols::minecraft::{BedrockResponse, GameMode, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::socket::{Socket, UdpSocket};
|
||||
|
|
@ -45,12 +46,12 @@ impl Bedrock {
|
|||
let mut buffer = Bufferer::new_with_data(Endianess::Little, &self.socket.receive(None)?);
|
||||
|
||||
if buffer.get_u8()? != 0x1c {
|
||||
return Err(GDError::PacketBad("Invalid message id.".to_string()));
|
||||
return Err(PacketBad);
|
||||
}
|
||||
|
||||
// Checking for our nonce directly from a u64 (as the nonce is 8 bytes).
|
||||
if buffer.get_u64()? != 9833440827789222417 {
|
||||
return Err(GDError::PacketBad("Invalid nonce.".to_string()));
|
||||
return Err(PacketBad);
|
||||
}
|
||||
|
||||
// These 8 bytes are identical to the serverId string we receive in decimal below
|
||||
|
|
@ -58,11 +59,11 @@ impl Bedrock {
|
|||
|
||||
// Verifying the magic value (as we need 16 bytes, cast to two u64 values)
|
||||
if buffer.get_u64()? != 18374403896610127616 {
|
||||
return Err(GDError::PacketBad("Invalid magic (part 1).".to_string()));
|
||||
return Err(PacketBad);
|
||||
}
|
||||
|
||||
if buffer.get_u64()? != 8671175388723805693 {
|
||||
return Err(GDError::PacketBad("Invalid magic (part 2).".to_string()));
|
||||
return Err(PacketBad);
|
||||
}
|
||||
|
||||
let remaining_length = buffer.as_endianess(Endianess::Big).get_u16()? as usize;
|
||||
|
|
@ -74,7 +75,7 @@ impl Bedrock {
|
|||
|
||||
// We must have at least 6 values
|
||||
if status.len() < 6 {
|
||||
return Err(GDError::PacketBad("Not enough status parts.".to_string()));
|
||||
return Err(PacketBad);
|
||||
}
|
||||
|
||||
Ok(BedrockResponse {
|
||||
|
|
@ -82,8 +83,8 @@ impl Bedrock {
|
|||
name: status[1].to_string(),
|
||||
version_name: status[3].to_string(),
|
||||
version_protocol: status[2].to_string(),
|
||||
max_players: status[5].parse().map_err(|_| GDError::TypeParse("couldn't parse.".to_string()))?,
|
||||
online_players: status[4].parse().map_err(|_| GDError::TypeParse("couldn't parse.".to_string()))?,
|
||||
max_players: status[5].parse().map_err(|_| TypeParse)?,
|
||||
online_players: status[4].parse().map_err(|_| TypeParse)?,
|
||||
id: status.get(6).and_then(|v| Some(v.to_string())),
|
||||
map: status.get(7).and_then(|v| Some(v.to_string())),
|
||||
game_mode: match status.get(8) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use serde_json::Value;
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::GDResult;
|
||||
use crate::GDError::{JsonParse, PacketBad};
|
||||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::protocols::minecraft::{as_varint, get_string, get_varint, Player, Response, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
|
|
@ -72,33 +73,33 @@ impl Java {
|
|||
let mut buffer = self.receive()?;
|
||||
|
||||
if get_varint(&mut buffer)? != 0 { //first var int is the packet id
|
||||
return Err(GDError::PacketBad("Bad receive packet id.".to_string()));
|
||||
return Err(PacketBad);
|
||||
}
|
||||
|
||||
let json_response = get_string(&mut buffer)?;
|
||||
let value_response: Value = serde_json::from_str(&json_response)
|
||||
.map_err(|e| GDError::JsonParse(e.to_string()))?;
|
||||
.map_err(|_|JsonParse)?;
|
||||
|
||||
let version_name = value_response["version"]["name"].as_str()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string();
|
||||
.ok_or(PacketBad)?.to_string();
|
||||
let version_protocol = value_response["version"]["protocol"].as_i64()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as i32;
|
||||
.ok_or(PacketBad)? as i32;
|
||||
|
||||
let max_players = value_response["players"]["max"].as_u64()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as u32;
|
||||
.ok_or(PacketBad)? as u32;
|
||||
let online_players = value_response["players"]["online"].as_u64()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected number.".to_string()))? as u32;
|
||||
.ok_or(PacketBad)? as u32;
|
||||
let sample_players: Option<Vec<Player>> = match value_response["players"]["sample"].is_null() {
|
||||
true => None,
|
||||
false => Some({
|
||||
let players_values = value_response["players"]["sample"].as_array()
|
||||
.ok_or(GDError::PacketBad("Couldn't get expected array.".to_string()))?;
|
||||
.ok_or(PacketBad)?;
|
||||
|
||||
let mut players = Vec::with_capacity(players_values.len());
|
||||
for player in players_values {
|
||||
players.push(Player {
|
||||
name: player["name"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string(),
|
||||
id: player["id"].as_str().ok_or(GDError::PacketBad("Couldn't get expected string.".to_string()))?.to_string()
|
||||
name: player["name"].as_str().ok_or(PacketBad)?.to_string(),
|
||||
id: player["id"].as_str().ok_or(PacketBad)?.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::GDResult;
|
||||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::GDError::{PacketBad, ProtocolFormat};
|
||||
use crate::protocols::minecraft::{LegacyGroup, Response, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
use crate::socket::{Socket, TcpSocket};
|
||||
|
|
@ -30,7 +31,7 @@ impl LegacyBV1_8 {
|
|||
let mut buffer = Bufferer::new_with_data(Endianess::Big, &self.socket.receive(None)?);
|
||||
|
||||
if buffer.get_u8()? != 0xFF {
|
||||
return Err(GDError::ProtocolFormat("Expected 0xFF at the begin of the packet.".to_string()));
|
||||
return Err(ProtocolFormat);
|
||||
}
|
||||
|
||||
let length = buffer.get_u16()? * 2;
|
||||
|
|
@ -43,9 +44,9 @@ impl LegacyBV1_8 {
|
|||
|
||||
let description = split[0].to_string();
|
||||
let online_players = split[1].parse()
|
||||
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
|
||||
.map_err(|_| PacketBad)?;
|
||||
let max_players = split[2].parse()
|
||||
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
|
||||
.map_err(|_| PacketBad)?;
|
||||
|
||||
Ok(Response {
|
||||
version_name: "Beta 1.8+".to_string(),
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::GDResult;
|
||||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::GDError::{PacketBad, ProtocolFormat};
|
||||
use crate::protocols::minecraft::{LegacyGroup, Response, Server};
|
||||
use crate::protocols::minecraft::protocol::legacy_v1_6::LegacyV1_6;
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
|
|
@ -31,7 +32,7 @@ impl LegacyV1_4 {
|
|||
let mut buffer = Bufferer::new_with_data(Endianess::Big, &self.socket.receive(None)?);
|
||||
|
||||
if buffer.get_u8()? != 0xFF {
|
||||
return Err(GDError::ProtocolFormat("Expected 0xFF at the begin of the packet.".to_string()));
|
||||
return Err(ProtocolFormat);
|
||||
}
|
||||
|
||||
let length = buffer.get_u16()? * 2;
|
||||
|
|
@ -48,9 +49,9 @@ impl LegacyV1_4 {
|
|||
|
||||
let description = split[0].to_string();
|
||||
let online_players = split[1].parse()
|
||||
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
|
||||
.map_err(|_| PacketBad)?;
|
||||
let max_players = split[2].parse()
|
||||
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
|
||||
.map_err(|_| PacketBad)?;
|
||||
|
||||
Ok(Response {
|
||||
version_name: "1.4+".to_string(),
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{GDError, GDResult};
|
||||
use crate::GDResult;
|
||||
use crate::GDError::{PacketBad, ProtocolFormat};
|
||||
use crate::bufferer::{Bufferer, Endianess};
|
||||
use crate::protocols::minecraft::{LegacyGroup, Response, Server};
|
||||
use crate::protocols::types::TimeoutSettings;
|
||||
|
|
@ -52,13 +53,13 @@ impl LegacyV1_6 {
|
|||
error_by_expected_size(5, split.len())?;
|
||||
|
||||
let version_protocol = split[0].parse()
|
||||
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
|
||||
.map_err(|_| PacketBad)?;
|
||||
let version_name = split[1].to_string();
|
||||
let description = split[2].to_string();
|
||||
let online_players = split[3].parse()
|
||||
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
|
||||
.map_err(|_| PacketBad)?;
|
||||
let max_players = split[4].parse()
|
||||
.map_err(|_| GDError::PacketBad("Failed to parse to expected int.".to_string()))?;
|
||||
.map_err(|_| PacketBad)?;
|
||||
|
||||
Ok(Response {
|
||||
version_name,
|
||||
|
|
@ -80,14 +81,14 @@ impl LegacyV1_6 {
|
|||
let mut buffer = Bufferer::new_with_data(Endianess::Big, &self.socket.receive(None)?);
|
||||
|
||||
if buffer.get_u8()? != 0xFF {
|
||||
return Err(GDError::ProtocolFormat("Expected a certain byte (0xFF) at the begin of the packet.".to_string()));
|
||||
return Err(ProtocolFormat);
|
||||
}
|
||||
|
||||
let length = buffer.get_u16()? * 2;
|
||||
error_by_expected_size((length + 3) as usize, buffer.data_length())?;
|
||||
|
||||
if !LegacyV1_6::is_protocol(&mut buffer)? {
|
||||
return Err(GDError::ProtocolFormat("Expected certain bytes at the beginning of the packet.".to_string()));
|
||||
return Err(ProtocolFormat);
|
||||
}
|
||||
|
||||
LegacyV1_6::get_response(&mut buffer)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{GDError, GDResult};
|
||||
use crate::GDError::AutoQuery;
|
||||
use crate::GDResult;
|
||||
use crate::protocols::minecraft::{BedrockResponse, LegacyGroup, Response};
|
||||
use crate::protocols::minecraft::protocol::bedrock::Bedrock;
|
||||
use crate::protocols::minecraft::protocol::java::Java;
|
||||
|
|
@ -27,7 +28,7 @@ pub fn query(address: &str, port: u16, timeout_settings: Option<TimeoutSettings>
|
|||
return Ok(response);
|
||||
}
|
||||
|
||||
Err(GDError::AutoQuery)
|
||||
Err(AutoQuery)
|
||||
}
|
||||
|
||||
/// Query a Java Server.
|
||||
|
|
@ -49,7 +50,7 @@ pub fn query_legacy(address: &str, port: u16, timeout_settings: Option<TimeoutSe
|
|||
return Ok(response);
|
||||
}
|
||||
|
||||
Err(GDError::AutoQuery)
|
||||
Err(AutoQuery)
|
||||
}
|
||||
|
||||
/// Query a specific (Java) Legacy Server.
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ by Jaiden Bernard (2021-2022 - MIT) from
|
|||
https://github.com/thisjaiden/golden_apple/blob/master/src/lib.rs
|
||||
*/
|
||||
|
||||
use crate::{GDError, GDResult};
|
||||
use crate::GDResult;
|
||||
use crate::bufferer::Bufferer;
|
||||
use crate::GDError::{PacketBad, UnknownEnumCast};
|
||||
|
||||
/// The type of Minecraft Server you want to query.
|
||||
#[derive(Debug)]
|
||||
|
|
@ -118,7 +119,7 @@ impl GameMode {
|
|||
"Hardcore" => Ok(GameMode::Hardcore),
|
||||
"Spectator" => Ok(GameMode::Spectator),
|
||||
"Adventure" => Ok(GameMode::Adventure),
|
||||
_ => Err(GDError::UnknownEnumCast)
|
||||
_ => Err(UnknownEnumCast)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -136,7 +137,7 @@ pub(crate) fn get_varint(buffer: &mut Bufferer) -> GDResult<i32> {
|
|||
|
||||
// The 5th byte is only allowed to have the 4 smallest bits set
|
||||
if i == 4 && (current_byte & 0xf0 != 0) {
|
||||
return Err(GDError::PacketBad("Couldn't parse to VarInt: Overflow.".to_string()))
|
||||
return Err(PacketBad)
|
||||
}
|
||||
|
||||
if (current_byte & msb) == 0 {
|
||||
|
|
@ -180,7 +181,7 @@ pub(crate) fn get_string(buffer: &mut Bufferer) -> GDResult<String> {
|
|||
}
|
||||
|
||||
Ok(String::from_utf8(text)
|
||||
.map_err(|_| GDError::PacketBad("Couldn't parse to a Minecraft String.".to_string()))?)
|
||||
.map_err(|_| PacketBad)?)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue