Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Added more options when selecting proxy domain, comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanuk12 committed Jun 16, 2023
1 parent 63d0bd0 commit 41d5859
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -15,21 +15,24 @@ fn pause() {
let _ = stdin.read(&mut [0u8]).unwrap();
}

// Path to the installation root dir
fn patch(path: PathBuf, _proxy: Option<String>) {
/// Performs the entire patching process.
fn patch(path: PathBuf, _proxy: Option<String>) -> 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();
Expand All @@ -41,44 +44,48 @@ fn patch(path: PathBuf, _proxy: Option<String>) {

// 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" => {
Expand All @@ -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();
}

0 comments on commit 41d5859

Please sign in to comment.