From 41d58592d43831f70cba387d8c367d1f125abc18 Mon Sep 17 00:00:00 2001 From: Stefanuk12 <42220813+Stefanuk12@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:26:18 +0100 Subject: [PATCH] Added more options when selecting proxy domain, comments. --- src/main.rs | 62 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 209bf34..4320fe6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::{fs, io::{stdin, stdout, Write, Read}, path::PathBuf}; use platform_dirs::AppDirs; -// Pauses the application until userinput +/// Pauses the application until the user presses enter. fn pause() { let mut stdin = stdin(); let mut stdout = stdout(); @@ -15,21 +15,24 @@ fn pause() { let _ = stdin.read(&mut [0u8]).unwrap(); } -// Path to the installation root dir -fn patch(path: PathBuf, _proxy: Option) { +/// Performs the entire patching process. +fn patch(path: PathBuf, _proxy: Option) -> String { // Ask for the proxy url let mut proxy = _proxy.unwrap_or("N/A".to_owned()); if proxy == "N/A" { // Ask for the proxy domain, default it to ours proxy = String::new(); - print!("Please enter the proxy domain (ropro-proxy.deno.dev): "); + print!("Please enter the proxy domain (either a number or a custom input):\n1. ropro-proxy.deno.dev (default)\n2. ropro.darkhub.cloud\n> "); stdout().flush().unwrap(); stdin().read_line(&mut proxy).ok().expect("Failed to get user input"); - // Check if input was blank - if proxy.trim().len() == 0 { - proxy = "ropro-proxy.deno.dev".to_string(); - } + // Set + proxy = match proxy.trim() { + "" => "ropro-proxy.deno.dev".to_owned(), + "1" => "ropro-proxy.deno.dev".to_owned(), + "2" => "ropro.darkhub.cloud".to_owned(), + _ => proxy + }; // Trim proxy = proxy.trim().to_string(); @@ -41,44 +44,48 @@ fn patch(path: PathBuf, _proxy: Option) { // Patching the background file let background = path.join("background.js"); - let mut background_contents = fs::read_to_string(&background).expect("Unable to open file (background.js)"); - background_contents.insert_str(0, "$.ajaxSetup({xhrFields: { withCredentials: true}})"); - background_contents = re.replace_all(&background_contents, &rep).to_string(); - fs::write(&background, background_contents).expect("Unable to write file contents (background.js)"); + let background_contents = fs::read_to_string(&background).expect("Unable to open file (background.js)"); + let new_background_contents = re.replace_all(&background_contents, &rep).to_string(); + fs::write(&background, new_background_contents.clone()).expect("Unable to write file contents (background.js)"); + + // Checking if they changed + if background_contents == new_background_contents { + println!("warning: nothing changed while patching `background.js` (and possibly others within js/page) - already patched?"); + } // Patching each file in js/page let jspage = path.clone().join("js/page"); for dir_entry in fs::read_dir(jspage).unwrap() { + // Get the file path let file = dir_entry.unwrap(); let file_name = format!("js/page/{}", file.file_name().to_str().unwrap()); let file_path = file.path(); - let mut file_data = fs::read_to_string(file_path.clone()).expect(&format!("Unable to open file ({})", file_name)); - file_data = re.replace_all(&file_data, &rep).to_string(); - fs::write(file_path, file_data).expect(&format!("Unable to write file contents ({})", file_name)); + // Patch the file + let file_data = fs::read_to_string(file_path.clone()).expect(&format!("Unable to open file ({})", file_name)); + let new_file_data = re.replace_all(&file_data, &rep).to_string(); + fs::write(file_path.clone(), new_file_data.clone()).expect(&format!("Unable to write file contents ({})", file_name)); } + + // Done + proxy } -// Main +/// Entrypoint fn main() { // Grab the input directory let mut input_dir = String::new(); - print!("Thanks for using Stefanuk12's RoPro Patcher.\n\nPlease select an option:\n\n1. Opera GX\n2. Custom Path\n> "); + print!("Thanks for using Stefanuk12's RoPro Patcher.\n\nPlease select an option:\n1. Opera GX\n2. Custom Path\n> "); stdout().flush().unwrap(); stdin().read_line(&mut input_dir).ok().expect("Failed to get user input"); - // All of the options + // Grab the path we want to use let path: PathBuf; match input_dir.trim() { // Opera GX "1" => { // Grab path path = fs::read_dir(AppDirs::new(Some(r"Opera Software\Opera GX Stable\Extensions\adbacgifemdbhdkfppmeilbgppmhaobf"), false).unwrap().config_dir).expect("Unable to grab Opera GX extension.").next().unwrap().unwrap().path(); - - // Patch - patch(path, None); - println!("Patched!"); - pause(); } // Custom Path "2" => { @@ -87,12 +94,13 @@ fn main() { stdout().flush().unwrap(); stdin().read_line(&mut input_dir).ok().expect("Failed to get user input"); path = PathBuf::from(input_dir.trim().to_string()); - - patch(path, None); - println!("Patched!"); - pause(); } // Neither of above _ => panic!("Invalid option") } + + // Patching... + let proxy = patch(path.clone(), None); + println!("Patched with the following configuration:\n-> Path: {}\n-> Proxy: {}", path.display(), proxy); + pause(); } \ No newline at end of file