mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
fix bad compression on sending long packets
THANKS JAM \SHARP
This commit is contained in:
parent
37b0cbf058
commit
edc37cfd4b
4 changed files with 48 additions and 15 deletions
|
@ -79,14 +79,18 @@ impl Display for ServerAddress {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Cursor;
|
||||
use std::{
|
||||
io::Cursor,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
packets::login::{
|
||||
serverbound_hello_packet::ServerboundHelloPacket, ServerboundLoginPacket,
|
||||
packets::{
|
||||
game::serverbound_chat_packet::{LastSeenMessagesUpdate, ServerboundChatPacket},
|
||||
login::{serverbound_hello_packet::ServerboundHelloPacket, ServerboundLoginPacket},
|
||||
},
|
||||
read::read_packet,
|
||||
write::write_packet,
|
||||
read::{compression_decoder, read_packet},
|
||||
write::{compression_encoder, packet_encoder, write_packet},
|
||||
};
|
||||
use bytes::BytesMut;
|
||||
use uuid::Uuid;
|
||||
|
@ -140,4 +144,29 @@ mod tests {
|
|||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_long_compressed_chat() {
|
||||
let compression_threshold = 256;
|
||||
|
||||
let buf = packet_encoder(
|
||||
&ServerboundChatPacket {
|
||||
message: "a".repeat(256),
|
||||
timestamp: 0,
|
||||
salt: 0,
|
||||
signature: None,
|
||||
last_seen_messages: LastSeenMessagesUpdate::default(),
|
||||
}
|
||||
.get(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let buf = compression_encoder(&buf, compression_threshold)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("{:?}", buf);
|
||||
|
||||
compression_decoder(&mut Cursor::new(&buf), compression_threshold).unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -163,7 +163,7 @@ pub enum DecompressionError {
|
|||
|
||||
/// Get the decompressed bytes from a packet. It must have been decrypted
|
||||
/// first.
|
||||
fn compression_decoder(
|
||||
pub fn compression_decoder(
|
||||
stream: &mut Cursor<&[u8]>,
|
||||
compression_threshold: u32,
|
||||
) -> Result<Vec<u8>, DecompressionError> {
|
||||
|
@ -264,8 +264,7 @@ mod tests {
|
|||
use crate::packets::game::ClientboundGamePacket;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_read_packet() {
|
||||
fn test_read_packet() {
|
||||
let mut buf: Cursor<&[u8]> = Cursor::new(&[
|
||||
56, 64, 85, 58, 141, 138, 71, 146, 193, 64, 88, 0, 0, 0, 0, 0, 0, 64, 60, 224, 105, 34,
|
||||
119, 8, 228, 67, 50, 51, 68, 194, 177, 230, 101, 0, 17, 0,
|
||||
|
|
|
@ -29,7 +29,7 @@ pub enum PacketEncodeError {
|
|||
},
|
||||
}
|
||||
|
||||
fn packet_encoder<P: ProtocolPacket + std::fmt::Debug>(
|
||||
pub fn packet_encoder<P: ProtocolPacket + std::fmt::Debug>(
|
||||
packet: &P,
|
||||
) -> Result<Vec<u8>, PacketEncodeError> {
|
||||
let mut buf = Vec::new();
|
||||
|
@ -51,7 +51,7 @@ pub enum PacketCompressError {
|
|||
Io(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
async fn compression_encoder(
|
||||
pub async fn compression_encoder(
|
||||
data: &[u8],
|
||||
compression_threshold: u32,
|
||||
) -> Result<Vec<u8>, PacketCompressError> {
|
||||
|
@ -60,15 +60,18 @@ async fn compression_encoder(
|
|||
if n < compression_threshold as usize {
|
||||
let mut buf = Vec::new();
|
||||
0.var_write_into(&mut buf)?;
|
||||
buf.write_all(data).await?;
|
||||
std::io::Write::write_all(&mut buf, data)?;
|
||||
Ok(buf)
|
||||
} else {
|
||||
// otherwise, compress
|
||||
let mut deflater = ZlibEncoder::new(data);
|
||||
// write deflated data to buf
|
||||
let mut buf = Vec::new();
|
||||
deflater.read_to_end(&mut buf).await?;
|
||||
Ok(buf)
|
||||
let mut data = Vec::new();
|
||||
deflater.read_to_end(&mut data).await?;
|
||||
let mut len_prepended_buf = Vec::new();
|
||||
(len_prepended_buf.len() as u32).var_write_into(&mut len_prepended_buf)?;
|
||||
len_prepended_buf.append(&mut data);
|
||||
Ok(len_prepended_buf)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,7 +85,7 @@ where
|
|||
P: ProtocolPacket + Debug,
|
||||
W: AsyncWrite + Unpin + Send,
|
||||
{
|
||||
trace!("Sending packet: {:?}", packet);
|
||||
trace!("Sending packet: {:?}", packet,);
|
||||
let mut buf = packet_encoder(packet).unwrap();
|
||||
if let Some(threshold) = compression_threshold {
|
||||
buf = compression_encoder(&buf, threshold).await.unwrap();
|
||||
|
|
|
@ -4,6 +4,8 @@ use azalea::prelude::*;
|
|||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let account = Account::offline("bot");
|
||||
// or let account = Account::microsoft("email").await;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue