diff --git a/src-tauri/src/lang.rs b/src-tauri/src/lang.rs index 8a7f53d..cff655f 100644 --- a/src-tauri/src/lang.rs +++ b/src-tauri/src/lang.rs @@ -1,3 +1,4 @@ +use std::path::{Path, PathBuf}; use crate::system_helpers::*; #[tauri::command] @@ -5,7 +6,8 @@ pub async fn get_lang(window: tauri::Window, lang: String) -> String { let lang = lang.to_lowercase(); // Send contents of language file back - let contents = match std::fs::read_to_string(format!("{}/lang/{}.json", install_location(), lang)) { + let lang_path: PathBuf = [&install_location(), "lang", &format!("{}.json", lang)].iter().collect(); + let contents = match std::fs::read_to_string(&lang_path) { Ok(x) => x, Err(e) => { emit_lang_err(window, format!("Failed to read language file: {}", e)); @@ -21,14 +23,14 @@ pub async fn get_languages() -> std::collections::HashMap { // for each lang file, set the key as the filename and the value as the lang_name contained in the file let mut languages = std::collections::HashMap::new(); - let mut lang_files = std::fs::read_dir(format!("{}/lang", install_location())).unwrap(); + let mut lang_files = std::fs::read_dir(Path::new(&install_location()).join("lang")).unwrap(); while let Some(entry) = lang_files.next() { let entry = entry.unwrap(); let path = entry.path(); let filename = path.file_name().unwrap().to_str().unwrap(); - let content = match std::fs::read_to_string(path.to_str().unwrap()) { + let content = match std::fs::read_to_string(&path) { Ok(x) => x, Err(e) => { println!("Failed to read language file: {}", e); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index fcd6c9d..f163d1c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,6 +5,7 @@ windows_subsystem = "windows" use lazy_static::lazy_static; use std::{sync::Mutex, collections::HashMap}; +use std::path::PathBuf; use std::thread; use sysinfo::{System, SystemExt}; @@ -31,10 +32,8 @@ fn main() { process_watcher(); // Make BG folder if it doesn't exist. - let bg_folder = format!("{}/bg", system_helpers::install_location()); - if !std::path::Path::new(&bg_folder).exists() { - std::fs::create_dir_all(&bg_folder).unwrap(); - } + let bg_folder: PathBuf = [&system_helpers::install_location(), "bg"].iter().collect(); + std::fs::create_dir_all(&bg_folder).unwrap(); tauri::Builder::default() .invoke_handler(tauri::generate_handler![ diff --git a/src-tauri/src/proxy.rs b/src-tauri/src/proxy.rs index 36d0dac..dc00879 100644 --- a/src-tauri/src/proxy.rs +++ b/src-tauri/src/proxy.rs @@ -16,6 +16,7 @@ use hudsucker::{ use std::fs; use std::net::SocketAddr; +use std::path::Path; use registry::{Hive, Data, Security}; use rustls_pemfile as pemfile; @@ -145,7 +146,7 @@ pub fn disconnect_from_proxy() { * Source: https://github.com/zu1k/good-mitm/raw/master/src/ca/gen.rs */ #[tauri::command] -pub fn generate_ca_files(path: &str) { +pub fn generate_ca_files(path: &Path) { let mut params = CertificateParams::default(); let mut details = DistinguishedName::new(); @@ -166,14 +167,13 @@ pub fn generate_ca_files(path: &str) { ]; // Create certificate. - let cert_path = format!("{}\\ca", path); - let cert = Certificate::from_params(params).unwrap(); let cert_crt = cert.serialize_pem().unwrap(); let private_key = cert.serialize_private_key_pem(); // Make certificate directory. - match fs::create_dir(&cert_path) { + let cert_dir = path.join("ca"); + match fs::create_dir(&cert_dir) { Ok(_) => {}, Err(e) => { println!("{}", e); @@ -181,29 +181,31 @@ pub fn generate_ca_files(path: &str) { }; // Write the certificate to a file. - match fs::write(format!("{}\\cert.crt", &cert_path), cert_crt) { - Ok(_) => println!("Wrote certificate to {}", &cert_path), - Err(e) => println!("Error writing certificate to {}: {}", &cert_path, e), + let cert_path = cert_dir.join("cert.crt"); + match fs::write(&cert_path, cert_crt) { + Ok(_) => println!("Wrote certificate to {}", cert_path.to_str().unwrap()), + Err(e) => println!("Error writing certificate to {}: {}", cert_path.to_str().unwrap(), e), } // Write the private key to a file. - match fs::write(format!("{}\\private.key", &cert_path), private_key) { - Ok(_) => println!("Wrote private key to {}", &cert_path), - Err(e) => println!("Error writing private key to {}: {}", &cert_path, e), + let private_key_path = cert_dir.join("private.key"); + match fs::write(&private_key_path, private_key) { + Ok(_) => println!("Wrote private key to {}", private_key_path.to_str().unwrap()), + Err(e) => println!("Error writing private key to {}: {}", private_key_path.to_str().unwrap(), e), } // Install certificate into the system's Root CA store. - install_ca_files(path); + install_ca_files(&cert_path); } /* * Attempts to install the certificate authority's certificate into the Root CA store. */ -pub fn install_ca_files(path: &str) { +pub fn install_ca_files(cert_path: &Path) { if cfg!(target_os = "windows") { - run_command(format!("certutil -user -addstore \"Root\" {}\\ca\\cert.crt", path).to_string()); + run_command("certutil", vec!["-user", "-addstore", "Root", cert_path.to_str().unwrap()]); } else { - run_command(format!("security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain {}/ca/cert.crt", path).to_string()); + run_command("security", vec!["add-trusted-cert", "-d", "-r", "trustRoot", "-k", "/Library/Keychains/System.keychain", cert_path.to_str().unwrap()]); } println!("Installed certificate."); diff --git a/src-tauri/src/system_helpers.rs b/src-tauri/src/system_helpers.rs index 10b68e0..329243f 100644 --- a/src-tauri/src/system_helpers.rs +++ b/src-tauri/src/system_helpers.rs @@ -17,9 +17,8 @@ pub fn run_program(path: String) { } #[tauri::command] -pub fn run_command(command: String) { - // Run the specified command. - cmd!(command).run() +pub fn run_command(program: &str, args: Vec<&str>) { + cmd(program, args).run() .expect("Failed to run command"); }