From 56f6e10bbd40ef26a69718f4f02446f4de26686d Mon Sep 17 00:00:00 2001 From: alterwain Date: Sun, 16 Mar 2025 22:48:22 +0300 Subject: [PATCH] modified: Cargo.lock modified: Cargo.toml modified: src/main.rs --- Cargo.lock | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 11 +++++- src/main.rs | 75 ++++++++++++++++++++++++++++++++------ 3 files changed, 176 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c199abe..9dae956 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -325,6 +325,8 @@ dependencies = [ name = "nicotine" version = "0.1.0" dependencies = [ + "walkdir", + "zip", "zip-extract", ] @@ -419,6 +421,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "serde" version = "1.0.219" @@ -556,12 +567,104 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[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.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[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.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[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.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + [[package]] name = "zerocopy" version = "0.8.23" diff --git a/Cargo.toml b/Cargo.toml index b052e76..62ee436 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,14 @@ [package] name = "nicotine" -version = "0.1.0" +version = "0.1.2" edition = "2024" +description = "A small rust crate for patching mojang's Authlib" +authors = ["alterwain"] + +[lib] +crate-type = ["staticlib", "cdylib", "lib"] [dependencies] -zip-extract = "0.2.1" \ No newline at end of file +zip-extract = "0.2.1" +zip = "2.2.3" +walkdir = "2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 92ef2e8..ba6e900 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use std::{io::Cursor, path::PathBuf}; +use std::{error::Error, fs::File, io::{Cursor, Read, Seek, Write}, path::{Path, PathBuf}}; + +use zip::{write::SimpleFileOptions, CompressionMethod, ZipWriter}; fn find_subsequence(haystack: &[u8], needle: &[u8]) -> Vec { haystack.windows(needle.len()) @@ -25,23 +27,74 @@ fn recursively_find_classes(path: PathBuf) -> Vec { classes } -fn main() { +// patch_jar(r#"D:\Documents\RustroverProjects\XCraft\xcraft\libraries\com\mojang\authlib\1.5.22\authlib-1.5.22__.jar"#, "authlib_patched.jar", "http://localhost:8999/api/") + +pub fn patch_jar(input_jar: &str, output_jar: &str, endpoint: &str) -> Result<(), Box> { let mut target_dir = PathBuf::new(); target_dir.push("out"); - //let archive = std::fs::read(r#"D:\Documents\RustroverProjects\XCraft\xcraft\libraries\com\mojang\authlib\1.5.22\authlib-1.5.22.jar"#).unwrap(); + let archive = std::fs::read(input_jar)?; - //zip_extract::extract(Cursor::new(archive), &target_dir, true); + zip_extract::extract(Cursor::new(archive), &target_dir, true)?; + + let needle = b"https://sessionserver.mojang.com/session/minecraft/"; + let replacement = endpoint.as_bytes(); for path in recursively_find_classes(PathBuf::from(".\\out")) { - let haystack = std::fs::read(&path).unwrap(); - let v = find_subsequence(&haystack, b"mojang.com"); - if !v.is_empty() { - println!("File {:?}: {:#?}",path, v); + let mut haystack = std::fs::read(&path).unwrap(); + let mut v = find_subsequence(&haystack, needle); + if v.is_empty() { continue; } + while let Some(g) = v.first() { + let (a,b) = haystack.split_at(*g); + let l = a[a.len()-1]; + let mut a = a[..a.len()-1].to_vec(); + a.push( if l as usize > needle.len() { (replacement.len() + (l as usize - needle.len())) as u8 } else { replacement.len() as u8 }); + a.append(&mut replacement.to_vec()); + a.append(&mut b[needle.len()..].to_vec()); + haystack = a; + v = find_subsequence(&haystack, needle); } + + std::fs::write(path, haystack)?; } - //let haystack = std::fs::read(r#".\out\com\mojang\authlib\yggdrasil\YggdrasilMinecraftSessionService.class"#).unwrap(); + let mut jar = File::create(output_jar)?; - // println!("Result: {:#?}", find_subsequence(&haystack, b"yggdrasil_session_pubkey.der")); -} \ No newline at end of file + zip_dir(&target_dir, &target_dir, jar)?; + + std::fs::remove_dir_all(&target_dir)?; + + Ok(()) +} + + +fn zip_dir( + src_dir: &Path, + prefix: &Path, + writer: T, +) -> zip::result::ZipResult<()> { + let mut zip = ZipWriter::new(writer); + let options = SimpleFileOptions::default() + .compression_method(CompressionMethod::Stored); + + let mut buffer = Vec::new(); + + for entry in walkdir::WalkDir::new(src_dir) { + let entry = entry.unwrap(); + let path = entry.path(); + let name = path.strip_prefix(prefix).unwrap(); + + if path.is_file() { + let mut f = File::open(path)?; + f.read_to_end(&mut buffer)?; + + zip.start_file(name.to_string_lossy(), options)?; + zip.write_all(&buffer)?; + buffer.clear(); + } else if path.is_dir() { + zip.add_directory(name.to_string_lossy(), options)?; + } + } + zip.finish()?; + Ok(()) +}