Skip to content

Commit 22f5881

Browse files
feat: Some comments
1 parent 80f8548 commit 22f5881

File tree

10 files changed

+17
-18
lines changed

10 files changed

+17
-18
lines changed

src/byte_buf_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ pub fn read_unsigned_short(buf: &mut BytesMut) -> io::Result<u16> {
7979
Ok(buf.get_u16())
8080
}
8181

82+
/*
83+
For some reason, not all Minecraft clients send their UUID correctly,
84+
so you have to return Result, since parsing is not always possible.
85+
*/
8286
pub fn try_get_uuid(buf: &mut BytesMut) -> anyhow::Result<Uuid> {
8387
let len = buf.len();
8488
if len < 16 {

src/handlers/handshake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub async fn handle_handshake(client: &mut MinecraftClient) -> Result<()> {
1515
_ => NextStateEnum::Unknown,
1616
};
1717

18+
// Check client's connecting hostname
1819
client.session.proto_ver = Some(handshake.proto_ver);
1920
if let Some(server_ip) = &client.config.server.server_ip {
2021
if server_ip.ne(&handshake.server_addr) {

src/packets/handshake.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use crate::byte_buf_utils::{read_unsigned_short, read_utf8, read_varint};
12
use bytes::BytesMut;
23
use tokio::io;
34

4-
use crate::byte_buf_utils::{read_unsigned_short, read_utf8, read_varint};
5-
65
#[allow(dead_code)]
76
pub struct HandshakePacket {
87
pub proto_ver: usize,
@@ -17,7 +16,7 @@ impl HandshakePacket {
1716
proto_ver: read_varint(buff)?,
1817
server_addr: read_utf8(buff)?,
1918
port: read_unsigned_short(buff)?,
20-
next_state: read_varint(buff)?
19+
next_state: read_varint(buff)?,
2120
})
2221
}
2322
}

src/packets/login_start.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use crate::byte_buf_utils::{read_utf8, try_get_uuid};
12
use bytes::BytesMut;
23
use uuid::Uuid;
34

4-
use crate::byte_buf_utils::{read_utf8, try_get_uuid};
5-
65
pub struct LoginStartPacket {
76
pub name: String,
87
pub uuid: Option<Uuid>,

src/packets/ping.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1+
use crate::byte_buf_utils::{add_size, write_varint};
12
use anyhow::Result;
23
use bytes::{BufMut, BytesMut};
34

4-
use crate::byte_buf_utils::{add_size, write_varint};
5-
6-
7-
#[allow(dead_code)]
85
pub struct PingPacket {
9-
pub payload: i64
6+
pub payload: i64,
107
}
118

129
impl PingPacket {

src/packets/status.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use crate::byte_buf_utils::{add_size, write_utf8, write_varint};
12
use anyhow::Result;
23
use bytes::BytesMut;
34
use serde::Serialize;
45
use serde_json::Value;
56

6-
use crate::byte_buf_utils::{add_size, write_utf8, write_varint};
7-
87
pub struct StatusPacket {}
98

109
impl StatusPacket {

src/responses/disconnect.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ pub async fn send_disconnect(
1313
}
1414
.build()?;
1515

16+
// Passing a session object allows sending a disconnect packet with or without encryption.
1617
encrypt_packet(&mut disconnect_packet, session);
1718

1819
stream.writable().await?;
1920
stream.write_all(&disconnect_packet).await?;
21+
22+
// Minecraft clients expect that after sending a disconnect packet there should be a disconnect
2023
stream.shutdown().await?;
2124

2225
debug!("Disconnected client with reason: {}", reason);

src/responses/encryption.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use crate::{client::Session, packets::encryption_request::EncryptionRequestPacket};
12
use anyhow::Result;
23
use rsa::pkcs8::EncodePublicKey;
34
use std::sync::Arc;
45
use tokio::{io::AsyncWriteExt, net::TcpStream};
56

6-
use crate::{client::Session, packets::encryption_request::EncryptionRequestPacket};
7-
87
pub async fn send_encryption(
98
stream: &mut TcpStream,
109
keys: Arc<rsa::RsaPrivateKey>,

src/responses/ping.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use anyhow::Result;
22

3+
use crate::packets::ping::PingPacket;
34
use bytes::{Buf, BytesMut};
45
use tokio::{io::AsyncWriteExt, net::TcpStream};
56

6-
use crate::packets::ping::PingPacket;
7-
87
pub async fn handle_ping(stream: &mut TcpStream, buff: &mut BytesMut) -> Result<()> {
98
let payload = buff.get_i64();
109
let packet = PingPacket { payload };

src/responses/status.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use anyhow::Result;
22

3+
use crate::{client::Session, config::get_config, packets::status};
34
use serde_json::json;
45
use tokio::{io::AsyncWriteExt, net::TcpStream};
56

6-
use crate::{client::Session, config::get_config, packets::status};
7-
87
pub async fn send_status(stream: &mut TcpStream, session: &mut Session) -> Result<()> {
98
let config = get_config().await;
109
let proto_ver = if config.server.config.protocol == 0 {

0 commit comments

Comments
 (0)