From e0272aa38ac1db3155c36af80d6ec429bd770bec Mon Sep 17 00:00:00 2001 From: Benj Date: Wed, 6 Jul 2022 20:21:04 +0800 Subject: [PATCH] Re-encrypt Metadata We in the end game now bois (Time for UI, refactoring, and better functionality) --- src-tauri/src/main.rs | 4 +- .../src/{metadata.rs => metadata_patcher.rs} | 49 ++++++++----------- 2 files changed, 22 insertions(+), 31 deletions(-) rename src-tauri/src/{metadata.rs => metadata_patcher.rs} (80%) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1126ecc..d1edd4e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -19,7 +19,7 @@ mod downloader; mod lang; mod proxy; mod web; -mod metadata; +mod metadata_patcher; lazy_static! { static ref WATCH_GAME_PROCESS: Mutex = { @@ -64,7 +64,7 @@ fn main() { lang::get_lang, lang::get_languages, web::valid_url, - metadata::patch_metadata + metadata_patcher::patch_metadata ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/metadata.rs b/src-tauri/src/metadata_patcher.rs similarity index 80% rename from src-tauri/src/metadata.rs rename to src-tauri/src/metadata_patcher.rs index 2e30e23..fd34eb3 100644 --- a/src-tauri/src/metadata.rs +++ b/src-tauri/src/metadata_patcher.rs @@ -6,10 +6,7 @@ use std::fs::File; use std::io::Read; use std::io::Write; -fn dll_decrypt_global_metadata( - data: *mut u8, - size: u64, -) -> Result<*const c_void, Box> { +fn dll_decrypt_global_metadata(data: *mut u8, size: u64) -> Result<*const c_void, Box> { unsafe { // Load DLL let lib = Library::new("mhycrypto.dll")?; @@ -24,10 +21,7 @@ fn dll_decrypt_global_metadata( } } -fn dll_encrypt_global_metadata( - data: *mut u8, - size: u64, -) -> Result<*const c_void, Box> { +fn dll_encrypt_global_metadata(data: *mut u8, size: u64) -> Result<*const c_void, Box> { unsafe { // Load DLL let lib = Library::new("mhycrypto.dll")?; @@ -46,13 +40,13 @@ fn dll_encrypt_global_metadata( pub fn patch_metadata(metadata_folder: &str) { let metadata_file = &(metadata_folder.to_owned() + "\\global-metadata.dat"); println!("Patching metadata file: {}", metadata_file); - let decrypted: Vec = decrypt_metadata(metadata_file); - + let decrypted = decrypt_metadata(metadata_file); let modified = replace_keys(&decrypted); + let encrypted = encrypt_metadata(&modified); - //write modfied to file - let mut file = File::create(&(metadata_folder.to_owned() + "\\modified-metadata.dat")).unwrap(); - file.write_all(&modified).unwrap(); + //write encrypted to file + let mut file = File::create(&(metadata_folder.to_owned() + "\\encrypted-metadata.dat")).unwrap(); + file.write_all(&encrypted).unwrap(); } fn decrypt_metadata(file_path: &str) -> Vec { @@ -131,19 +125,16 @@ fn replace_rsa_key(old_data: &str, to_replace: &str, file_name: &str) -> String } } -/*let mut file = match OpenOptions::new().write(true).create(true).open(&(file_location.to_owned() + "\\decrypted_metadata.dat")) { - Ok(file) => file, - Err(e) => { - println!("Failed to open file: {}", e); - return; - } -}; -match file.write_all(&data) { - Ok(_) => { - println!("Successfully decrypted metadata"); - } - Err(e) => { - println!("Failed to write to file: {}", e); - return; - } -}*/ +fn encrypt_metadata(old_data: &Vec) -> Vec { + let mut data = old_data.to_vec(); + match dll_encrypt_global_metadata(data.as_mut_ptr(), data.len().try_into().unwrap()) { + Ok(_) => { + println!("Successfully encrypted global-metadata"); + return data; + } + Err(e) => { + println!("Failed to encrypt global-metadata: {}", e); + return Vec::new(); + } + }; +}