From 4f63e55a280c963c80c4b16385f0b47a5fc29320 Mon Sep 17 00:00:00 2001 From: Brian Bowman Date: Tue, 5 Jul 2022 05:49:25 -0500 Subject: [PATCH 1/2] Fix build errors on Unix --- src-tauri/Cargo.lock | 11 ++++++ src-tauri/Cargo.toml | 11 +++--- src-tauri/src/proxy.rs | 62 ++++++++++++++++++++------------- src-tauri/src/system_helpers.rs | 11 ++++-- 4 files changed, 65 insertions(+), 30 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index f2d5257..2063b7f 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -756,6 +756,7 @@ dependencies = [ "rustls-pemfile", "serde", "serde_json", + "sudo", "sysinfo", "tauri", "tauri-build", @@ -3726,6 +3727,16 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" +[[package]] +name = "sudo" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88bd84d4c082e18e37fef52c0088e4407dabcef19d23a607fb4b5ee03b7d5b83" +dependencies = [ + "libc", + "log", +] + [[package]] name = "syn" version = "1.0.92" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 7c90f77..e3ec2a2 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -14,6 +14,13 @@ rust-version = "1.57" [build-dependencies] tauri-build = { version = "1.0.0-rc.8", features = [] } +[target.'cfg(windows)'.dependencies] +is_elevated = "0.1.2" +registry = "1.2.1" + +[target.'cfg(unix)'.dependencies] +sudo = "0.6.0" + [dependencies] serde = { version = "1.0", features = ["derive"] } tauri = { version = "1.0.0-rc.9", features = ["api-all"] } @@ -28,9 +35,6 @@ zip = "0.6.2" # For creating a "global" downloads list. lazy_static = "1.4.0" -# Access to the Windows Registry. -registry = "1.2.1" - # Program opener. open = "2.1.2" duct = "0.13.5" @@ -39,7 +43,6 @@ duct = "0.13.5" serde_json = "1" # System process elevation. -is_elevated = "0.1.2" runas = "0.2.1" # Dependencies for the HTTP(S) proxy. diff --git a/src-tauri/src/proxy.rs b/src-tauri/src/proxy.rs index dc00879..35661b9 100644 --- a/src-tauri/src/proxy.rs +++ b/src-tauri/src/proxy.rs @@ -17,11 +17,12 @@ use hudsucker::{ use std::fs; use std::net::SocketAddr; use std::path::Path; -use registry::{Hive, Data, Security}; use rustls_pemfile as pemfile; use tauri::http::Uri; -use crate::system_helpers::run_command; + +#[cfg(windows)] +use registry::{Hive, Data, Security}; async fn shutdown_signal() { tokio::signal::ctrl_c().await @@ -109,37 +110,43 @@ pub async fn create_proxy(proxy_port: u16, certificate_path: String) { /** * Connects to the local HTTP(S) proxy server. */ +#[cfg(windows)] pub fn connect_to_proxy(proxy_port: u16) { - if cfg!(target_os = "windows") { - // Create 'ProxyServer' string. - let server_string: String = format!("http=127.0.0.1:{};https=127.0.0.1:{}", proxy_port, proxy_port); + // Create 'ProxyServer' string. + let server_string: String = format!("http=127.0.0.1:{};https=127.0.0.1:{}", proxy_port, proxy_port); - // Fetch the 'Internet Settings' registry key. - let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap(); + // Fetch the 'Internet Settings' registry key. + let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap(); - // Set registry values. - settings.set_value("ProxyServer", &Data::String(server_string.parse().unwrap())).unwrap(); - settings.set_value("ProxyEnable", &Data::U32(1)).unwrap(); - } + // Set registry values. + settings.set_value("ProxyServer", &Data::String(server_string.parse().unwrap())).unwrap(); + settings.set_value("ProxyEnable", &Data::U32(1)).unwrap(); println!("Connected to the proxy."); } +#[cfg(not(windows))] +pub fn connect_to_proxy(_proxy_port: u16) { + println!("Connecting to the proxy is not implemented on this platform."); +} + /** * Disconnects from the local HTTP(S) proxy server. */ +#[cfg(windows)] pub fn disconnect_from_proxy() { - if cfg!(target_os = "windows") { - // Fetch the 'Internet Settings' registry key. - let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap(); + // Fetch the 'Internet Settings' registry key. + let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap(); - // Set registry values. - settings.set_value("ProxyEnable", &Data::U32(0)).unwrap(); - } + // Set registry values. + settings.set_value("ProxyEnable", &Data::U32(0)).unwrap(); println!("Disconnected from proxy."); } +#[cfg(not(windows))] +pub fn disconnect_from_proxy() {} + /* * Generates a private key and certificate used by the certificate authority. * Additionally installs the certificate and private key in the Root CA store. @@ -201,12 +208,19 @@ pub fn generate_ca_files(path: &Path) { /* * Attempts to install the certificate authority's certificate into the Root CA store. */ +#[cfg(windows)] pub fn install_ca_files(cert_path: &Path) { - if cfg!(target_os = "windows") { - run_command("certutil", vec!["-user", "-addstore", "Root", cert_path.to_str().unwrap()]); - } else { - run_command("security", vec!["add-trusted-cert", "-d", "-r", "trustRoot", "-k", "/Library/Keychains/System.keychain", cert_path.to_str().unwrap()]); - } - + crate::system_helpers::run_command("certutil", vec!["-user", "-addstore", "Root", cert_path.to_str().unwrap()]); println!("Installed certificate."); -} \ No newline at end of file +} + +#[cfg(target_os = "macos")] +pub fn install_ca_files(cert_path: &Path) { + crate::system_helpers::run_command("security", vec!["add-trusted-cert", "-d", "-r", "trustRoot", "-k", "/Library/Keychains/System.keychain", cert_path.to_str().unwrap()]); + println!("Installed certificate."); +} + +#[cfg(not(any(windows, target_os = "macos")))] +pub fn install_ca_files(_cert_path: &Path) { + println!("Certificate installation is not supported on this platform."); +} diff --git a/src-tauri/src/system_helpers.rs b/src-tauri/src/system_helpers.rs index 329243f..d74794b 100644 --- a/src-tauri/src/system_helpers.rs +++ b/src-tauri/src/system_helpers.rs @@ -76,7 +76,14 @@ pub fn install_location() -> String { return exe_path.to_str().unwrap().to_string(); } +#[cfg(windows)] #[tauri::command] pub fn is_elevated() -> bool { - return is_elevated::is_elevated(); -} \ No newline at end of file + is_elevated::is_elevated() +} + +#[cfg(unix)] +#[tauri::command] +pub fn is_elevated() -> bool { + sudo::check() == sudo::RunningAs::Root +} From bb9a044e05c391b0271a31a10df0f6dc16cad77c Mon Sep 17 00:00:00 2001 From: Brian Bowman Date: Tue, 5 Jul 2022 19:42:46 -0500 Subject: [PATCH 2/2] Fix reading config on Unix --- src/utils/configuration.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/configuration.ts b/src/utils/configuration.ts index 5404718..5ce8863 100644 --- a/src/utils/configuration.ts +++ b/src/utils/configuration.ts @@ -84,7 +84,7 @@ export async function saveConfig(obj: Configuration) { async function readConfigFile() { const local = await dataDir() - if (!configFilePath) configFilePath = local + 'cultivation\\configuration.json' + if (!configFilePath) configFilePath = local + 'cultivation/configuration.json' // Ensure Cultivation dir exists const dirs = await fs.readDir(local) @@ -94,12 +94,12 @@ async function readConfigFile() { await fs.createDir(local + 'cultivation').catch(e => console.log(e)) } - const innerDirs = await fs.readDir(local + '\\cultivation') + const innerDirs = await fs.readDir(local + '/cultivation') // Create grasscutter dir for potential installation if (!innerDirs.find((fileOrDir) => fileOrDir?.name === 'grasscutter')) { // Create dir - await fs.createDir(local + 'cultivation\\grasscutter').catch(e => console.log(e)) + await fs.createDir(local + 'cultivation/grasscutter').catch(e => console.log(e)) } const dataFiles = await fs.readDir(local + 'cultivation')