From 69ad3f92de31da7b4868057063af89606cedfbec Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 13 Apr 2025 03:14:16 +0200 Subject: [PATCH] encryption example --- Cargo.lock | 67 +++++++++++++++++++++++++++ Cargo.toml | 2 + src/main.rs | 128 ++++++++++++++++++++++++++++------------------------ 3 files changed, 139 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae56909..8575c2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,8 +60,10 @@ dependencies = [ name = "auraclient-api" version = "0.1.0" dependencies = [ + "base64 0.22.1", "minreq", "mongodb", + "openssl", "serde", ] @@ -393,6 +395,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -1071,6 +1088,44 @@ version = "1.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" +[[package]] +name = "openssl" +version = "0.10.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fedfea7d58a1f73118430a55da6a286e7b044961736ce96a16a17068ea25e5da" +dependencies = [ + "bitflags 2.9.0", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.99", +] + +[[package]] +name = "openssl-sys" +version = "0.9.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8288979acd84749c744a9014b4382d42b8f7b2592847b5afb2ed29e5d16ede07" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "parking_lot" version = "0.12.3" @@ -1121,6 +1176,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + [[package]] name = "powerfmt" version = "0.2.0" @@ -1784,6 +1845,12 @@ dependencies = [ "serde", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index 7b3b142..f58683c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] +base64 = "0.22.1" minreq = { version = "2.13.2", features = ["https", "urlencoding", "json-using-serde"] } mongodb = { version = "3.2.1", features = ["sync"] } +openssl = "0.10.72" serde = { version ="1.0.218", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index a5b1c30..605474e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,12 +5,17 @@ use std::{ time::{Duration, Instant}, }; +use openssl::rsa::Rsa; + pub mod database; +pub mod encryption; pub mod methods; pub mod parser; pub mod response; fn main() -> std::io::Result<()> { + let rsa = Rsa::generate(2048).expect("Failed to generate RSA keys"); + let client = mongodb::sync::Client::with_uri_str("mongodb://admin:admin@localhost/").unwrap(); let database = Arc::new(database::Database::new(&client)); @@ -19,70 +24,77 @@ fn main() -> std::io::Result<()> { for stream in listener.incoming() { println!("New connection"); let database = Arc::clone(&database); + let rsa = rsa.clone(); std::thread::spawn({ move || { - let mut stream = stream.unwrap(); - let mut reader = BufReader::new(stream.try_clone().unwrap()); - let mut last_activity = Instant::now(); + let mut stream = encryption::handshake(stream.unwrap(), rsa); - match methods::Session::new(&mut reader, database) { - Ok((session, res)) => { - writeln!(stream, "{res}").unwrap(); - stream.flush().unwrap(); + println!("{:?}", stream.read()); - loop { - let mut request_string = String::new(); - match reader.read_line(&mut request_string) { - Ok(0) => { - // Client disconnected - println!("[MOJANG] {} disconnected", session.local_player.name); - methods::player::logout(&session).unwrap(); - println!("[MOJANG] {} went offline", session.local_player.name); - break; - } - Ok(_) => { - last_activity = Instant::now(); - let (method, params) = parser::parse(&request_string).unwrap(); - let response = session.handle_request(&method, ¶ms); - writeln!( - stream, - "{}", - match response { - Ok(response) => response.to_string(), - Err(e) => format!("!{e}"), - } - ) - .unwrap(); - } - Err(e) => { - println!( - "[ERROR] Read error for {}: {}", - session.local_player.name, e - ); - break; - } - }; + stream.send("Daddy OMG!!!"); - // Check for inactivity - if last_activity.elapsed() > Duration::from_secs(60) { - println!( - "[MOJANG] {} inactive for too long", - session.local_player.name - ); - methods::player::logout(&session).unwrap(); - println!("[MOJANG] {} went offline", session.local_player.name); - break; - } - } - } - Err(e) => { - stream.write_all(format!("!{e}").as_bytes()).unwrap(); - stream - .shutdown(std::net::Shutdown::Both) - .expect("shutdown call failed"); - } - } + println!("OMG sent") + // let mut reader = BufReader::new(stream.try_clone().unwrap()); + // let mut last_activity = Instant::now(); + + // match methods::Session::new(&mut reader, database) { + // Ok((session, res)) => { + // writeln!(stream, "{res}").unwrap(); + // stream.flush().unwrap(); + + // loop { + // let mut request_string = String::new(); + // match reader.read_line(&mut request_string) { + // Ok(0) => { + // // Client disconnected + // println!("[MOJANG] {} disconnected", session.local_player.name); + // methods::player::logout(&session).unwrap(); + // println!("[MOJANG] {} went offline", session.local_player.name); + // break; + // } + // Ok(_) => { + // last_activity = Instant::now(); + // let (method, params) = parser::parse(&request_string).unwrap(); + // let response = session.handle_request(&method, ¶ms); + // writeln!( + // stream, + // "{}", + // match response { + // Ok(response) => response.to_string(), + // Err(e) => format!("!{e}"), + // } + // ) + // .unwrap(); + // } + // Err(e) => { + // println!( + // "[ERROR] Read error for {}: {}", + // session.local_player.name, e + // ); + // break; + // } + // }; + + // // Check for inactivity + // if last_activity.elapsed() > Duration::from_secs(60) { + // println!( + // "[MOJANG] {} inactive for too long", + // session.local_player.name + // ); + // methods::player::logout(&session).unwrap(); + // println!("[MOJANG] {} went offline", session.local_player.name); + // break; + // } + // } + // } + // Err(e) => { + // stream.write_all(format!("!{e}").as_bytes()).unwrap(); + // stream + // .shutdown(std::net::Shutdown::Both) + // .expect("shutdown call failed"); + // } + // } } }); }