commit d4bee287812aa199ced1f54e133ca6fc04ed0eca Author: G2-Games Date: Wed Oct 9 16:33:27 2024 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f4ea9ac --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,330 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "colog" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c426b7af8d5e0ad79de6713996632ce31f0d68ba84068fb0d654b396e519df0" +dependencies = [ + "colored", + "env_logger", + "log", +] + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "minecraft_server_impl" +version = "0.1.0" +dependencies = [ + "byteorder", + "colog", + "log", +] + +[[package]] +name = "regex" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..198f7b7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "minecraft_server_impl" +version = "0.1.0" +edition = "2021" + +[dependencies] +byteorder = "1.5.0" +colog = "1.3.0" +log = "0.4.22" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7ea1faa --- /dev/null +++ b/src/main.rs @@ -0,0 +1,150 @@ +mod utils; +mod to_bytes; + +use std::{io::{self, Write}, net::{TcpListener, TcpStream}}; + +use log::{info, warn}; +use byteorder::{ReadBytesExt, WriteBytesExt, BE}; +use to_bytes::ToBytes; +use utils::{MCString, ReadMCString, WriteMCString}; + + +fn main() { + colog::default_builder() + .filter_level(log::LevelFilter::Debug) + .init(); + + let listener = TcpListener::bind("0.0.0.0:25565").unwrap(); + + for mut connection in listener.incoming().filter_map(|c| c.ok()) { + info!("Connected to client @ {}", connection.peer_addr().unwrap()); + while let Some(cmd) = connection.read_u8().ok() { + let command = Command::try_from(cmd); + info!("COMMAND: {command:?}"); + handle_command(&mut connection, command.unwrap()).unwrap(); + + } + warn!("Lost connection to client"); + } +} + +fn handle_command(connection: &mut TcpStream, command: Command) -> Result<(), io::Error> { + match command { + Command::KeepAlive => todo!(), + Command::Handshake => { + let username = connection.read_mcstring()?; + info!("Handshake: {username}"); + + connection.write_u8(0x02)?; + connection.write_mcstring(&MCString::try_from("-").unwrap())?; + }, + Command::Login => { + info!("---"); + info!("Initiating login"); + let protocol_version = connection.read_u32::()?; + let username = connection.read_mcstring()?; + let _password = connection.read_mcstring()?; + let _map_seed = connection.read_i64::()?; + let _dimension = connection.read_i8()?; + + info!("Protocol Version: {protocol_version}"); + info!("Username: {username}"); + + let login_packet = ServerLoginPacket { + entity_id: 1, + unknown1: MCString::default(), + unknown2: MCString::default(), + map_seed: 1715505462032542147, + dimension: 0, + }; + login_packet.write_into(connection)?; + + info!("Responded to auth request"); + + let prechunk = PreChunk { + x_coord: 0, + z_coord: 0, + mode: true, + }; + + connection.write_u8(Command::PreChunk as u8)?; + connection.write_all(&prechunk.to_bytes())?; + }, + _ => unimplemented!("This command is probably `Server -> Client` only") + } + + Ok(()) +} + +#[derive(Debug, Clone, Copy)] +enum Command { + KeepAlive = 0x00, + Login = 0x01, + Handshake = 0x02, + PreChunk = 0x32, + ChunkData = 0x33, + Kick = 0xFF, +} + +#[derive(Debug, Clone, Copy)] +struct CommandError { + _id: u8, +} + +impl TryFrom for Command { + type Error = CommandError; + + fn try_from(value: u8) -> Result { + Ok(match value { + 0x00 => Self::KeepAlive, + 0x01 => Self::Login, + 0x02 => Self::Handshake, + 0x32 => Self::PreChunk, + 0x33 => Self::ChunkData, + 0xFF => Self::Kick, + v => return Err(CommandError{ + _id: v, + }), + }) + } +} + +struct ServerLoginPacket { + entity_id: i32, + unknown1: MCString, + unknown2: MCString, + map_seed: i64, + dimension: i8, +} + +impl ServerLoginPacket { + fn write_into(&self, stream: &mut W) -> Result<(), io::Error> { + stream.write_i32::(self.entity_id)?; + stream.write_mcstring(&self.unknown1)?; + stream.write_mcstring(&self.unknown2)?; + stream.write_i64::(self.map_seed)?; + stream.write_i8(self.dimension)?; + + Ok(()) + } +} + +#[repr(C)] +struct PreChunk { + x_coord: i32, + z_coord: i32, + mode: bool, // True to load, False to unload +} + +impl ToBytes for PreChunk { + type Bytes = [u8; 9]; + + fn to_bytes(self) -> Self::Bytes { + let mut buffer = Vec::new(); + buffer.write_i32::(self.x_coord).unwrap(); + buffer.write_i32::(self.z_coord).unwrap(); + buffer.write_u8(self.mode as u8).unwrap(); + + buffer.try_into().unwrap() + } +} diff --git a/src/to_bytes.rs b/src/to_bytes.rs new file mode 100644 index 0000000..a0d9205 --- /dev/null +++ b/src/to_bytes.rs @@ -0,0 +1,38 @@ +use std::ops; + +pub trait ToBytes: Sized { + /// A byte array which can store a packed representation of this type. + type Bytes: ByteArray; + + fn to_bytes(self) -> Self::Bytes; +} + +mod private { + pub trait ByteArray {} + + impl ByteArray for [u8; N] {} +} + + +pub trait ByteArray: + private::ByteArray + + ops::IndexMut + + ops::IndexMut, Output = [u8]> + + AsRef<[u8]> + + AsMut<[u8]> +{ + /// The length of this byte array. + const SIZE: usize; + + /// Return the array with all zeros. + /// Cannot use `Default` as it is not implemented for all array sizes. + fn zeroed() -> Self; +} + +impl ByteArray for [u8; N] { + const SIZE: usize = N; + + fn zeroed() -> Self { + [0; N] + } +} diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..d6f01ab --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,61 @@ +use std::{fmt::Display, io::{self, Read, Write}}; + +use byteorder::{ReadBytesExt, WriteBytesExt, BE}; + +#[derive(Debug, Default, Clone)] +pub struct MCString { + len: u16, + chars: Vec, +} + +impl TryFrom<&str> for MCString { + type Error = (); + + fn try_from(value: &str) -> Result { + if value.len() > u16::MAX as usize { + return Err(()) + } + + Ok(Self { + len: value.len() as u16, + chars: value.as_bytes().to_vec(), + }) + } +} + +impl Display for MCString { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", String::from_utf8(self.chars.clone()).unwrap()) + } +} + +pub trait ReadMCString: io::Read { + #[inline] + fn read_mcstring(&mut self) -> Result { + let len = self.read_u16::()?; + let mut string = vec![0; len as usize]; + self.read_exact(&mut string)?; + + Ok(MCString { + len, + chars: string, + }) + } +} + +impl ReadMCString for R {} + +pub trait WriteMCString: io::Write { + #[inline] + fn write_mcstring(&mut self, string: &MCString) -> Result<(), io::Error> { + self.write_u16::(string.len)?; + + if string.len != 0 { + self.write_all(&string.chars)?; + } + + Ok(()) + } +} + +impl WriteMCString for W {}