Status Ping Works
This commit is contained in:
parent
2acaea1bd2
commit
59392b3fc4
|
@ -12,11 +12,12 @@ pub struct Handshake {
|
|||
}
|
||||
|
||||
pub async fn read_handshake(stream: &mut OwnedReadHalf) -> Handshake {
|
||||
get_handshake(&mut mc_types::read_packet(stream).await)
|
||||
let mut data = mc_types::read_packet(stream).await;
|
||||
let _packet_id = mc_types::get_var_int(&mut data);
|
||||
get_handshake(&mut data)
|
||||
}
|
||||
|
||||
pub fn get_handshake(data: &mut Vec<u8>) -> Handshake {
|
||||
mc_types::get_var_int(data);
|
||||
Handshake {
|
||||
protocol_version: mc_types::get_var_int(data),
|
||||
server_address: mc_types::get_string(data),
|
||||
|
|
|
@ -23,10 +23,9 @@ pub async fn read_packet(stream: &mut OwnedReadHalf) -> Vec<u8> {
|
|||
buffer
|
||||
}
|
||||
pub async fn write_packet(stream: &mut OwnedWriteHalf, data: &mut Vec<u8>) {
|
||||
let length = data.len() as i32;
|
||||
stream.write_all(&convert_var_int(length))
|
||||
.await.expect("Error writing to stream");
|
||||
stream.write_all(&data)
|
||||
let mut out_data = convert_var_int(data.len() as i32);
|
||||
out_data.append(data);
|
||||
stream.write_all(&out_data)
|
||||
.await.expect("Error writing to stream");
|
||||
}
|
||||
async fn read_var_int_stream(stream: &mut OwnedReadHalf) -> i32 {
|
||||
|
@ -186,9 +185,9 @@ pub fn convert_var_int(mut value: i32) -> Vec<u8> {
|
|||
}
|
||||
|
||||
pub fn get_string(data: &mut Vec<u8>) -> String {
|
||||
let length = get_var_int(data);
|
||||
let mut buffer = vec![0; length as usize];
|
||||
data.append(&mut buffer);
|
||||
let length = get_var_int(data) as usize;
|
||||
let buffer = data[..length].to_vec();
|
||||
for _ in 0..length { data.remove(0); }
|
||||
String::from_utf8_lossy(&buffer).to_string()
|
||||
}
|
||||
pub fn convert_string(s: &str) -> Vec<u8> {
|
||||
|
|
|
@ -23,18 +23,21 @@ pub struct StatusPlayerInfo {
|
|||
pub struct StatusPlayers {
|
||||
pub max: i32,
|
||||
pub online: i32,
|
||||
pub sample: Vec<StatusPlayerInfo>
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub sample: Option<Vec<StatusPlayerInfo>>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct StatusResponse {
|
||||
pub struct StatusResponseData {
|
||||
pub version: StatusVersion,
|
||||
pub description: mc_types::Chat,
|
||||
pub players: StatusPlayers,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub favicon: Option<String>,
|
||||
pub enforcesSecureChat: bool,
|
||||
pub previewsChat: bool,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub enforcesSecureChat: Option<bool>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub previewsChat: Option<bool>,
|
||||
}
|
||||
|
||||
async fn online_players(
|
||||
|
@ -59,15 +62,16 @@ pub async fn respond_status(
|
|||
server_writer: &mut OwnedWriteHalf,
|
||||
) {
|
||||
loop {
|
||||
let packet_id = client_reader.read_u8()
|
||||
.await.expect("Error reading from stream");
|
||||
println!("Status Handling");
|
||||
let mut data = mc_types::read_packet(client_reader).await;
|
||||
let packet_id = mc_types::get_var_int(&mut data);
|
||||
|
||||
let online_players = online_players(server_reader, server_writer).await;
|
||||
println!("Status Packet ID: {}", packet_id);
|
||||
|
||||
if packet_id == 0x00 {
|
||||
println!("Handling Status");
|
||||
let status_response = StatusResponse {
|
||||
let online_players = online_players(server_reader, server_writer).await;
|
||||
let status_response = StatusResponseData {
|
||||
version: StatusVersion {
|
||||
name: mc_types::VERSION_NAME.to_string(),
|
||||
protocol: mc_types::VERSION_PROTOCOL,
|
||||
|
@ -81,18 +85,19 @@ pub async fn respond_status(
|
|||
sample: online_players.sample,
|
||||
},
|
||||
favicon: favicon(),
|
||||
enforcesSecureChat: false,
|
||||
previewsChat: false,
|
||||
enforcesSecureChat: None,
|
||||
previewsChat: None,
|
||||
// enforcesSecureChat: Some(false),
|
||||
// previewsChat: Some(false),
|
||||
};
|
||||
|
||||
let json_result = serde_json::to_string(&status_response);
|
||||
|
||||
match json_result {
|
||||
Ok(json) => {
|
||||
client_writer.write_u8(0)
|
||||
.await.expect("Error writing to stream");
|
||||
client_writer.write_all(&mc_types::convert_string(&json))
|
||||
.await.expect("Error writing to stream");
|
||||
let mut out_data: Vec<u8> = vec![0];
|
||||
out_data.append(&mut mc_types::convert_string(&json));
|
||||
mc_types::write_packet(client_writer, &mut out_data).await;
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("Error serializing to JSON: {}", err);
|
||||
|
@ -101,12 +106,9 @@ pub async fn respond_status(
|
|||
}
|
||||
} else if packet_id == 0x01 {
|
||||
println!("Handling Ping");
|
||||
let payload = client_reader.read_i64()
|
||||
.await.expect("Error reading from stream");
|
||||
client_writer.write_u8(0)
|
||||
.await.expect("Error writing to stream");
|
||||
client_writer.write_i64(payload)
|
||||
.await.expect("Error writing to stream");
|
||||
let mut out_data: Vec<u8> = vec![1];
|
||||
out_data.append(&mut data);
|
||||
mc_types::write_packet(client_writer, &mut out_data).await;
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
|
@ -117,21 +119,25 @@ pub async fn respond_status(
|
|||
pub async fn get_upstream_status(
|
||||
server_reader: &mut OwnedReadHalf,
|
||||
server_writer: &mut OwnedWriteHalf,
|
||||
) -> StatusResponse {
|
||||
) -> StatusResponseData {
|
||||
handshake::write_handshake(server_writer, handshake::Handshake{
|
||||
protocol_version: mc_types::VERSION_PROTOCOL,
|
||||
server_address: "localhost".to_string(),
|
||||
server_port: 25565,
|
||||
next_state: 1,
|
||||
}).await;
|
||||
server_writer.write_u8(0).await.expect("Error writing to stream");
|
||||
|
||||
mc_types::write_packet(server_writer, &mut vec![0]).await;
|
||||
let mut data = mc_types::read_packet(server_reader).await;
|
||||
|
||||
mc_types::get_u8(&mut data);
|
||||
let json = mc_types::get_string(&mut data);
|
||||
let status_response: StatusResponse = serde_json::from_str(&json)
|
||||
let status_response: StatusResponseData = serde_json::from_str(&json)
|
||||
.expect("Error parsing JSON");
|
||||
|
||||
// let mut out_data: Vec<u8> = vec![1];
|
||||
// out_data.append(&mut mc_types::convert_i64(0));
|
||||
// mc_types::write_packet(server_writer, &mut out_data).await;
|
||||
|
||||
status_response
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue