Fix build errors on Unix

This commit is contained in:
Brian Bowman
2022-07-05 05:49:25 -05:00
parent a3e1898d82
commit 4f63e55a28
4 changed files with 65 additions and 30 deletions

11
src-tauri/Cargo.lock generated
View File

@@ -756,6 +756,7 @@ dependencies = [
"rustls-pemfile", "rustls-pemfile",
"serde", "serde",
"serde_json", "serde_json",
"sudo",
"sysinfo", "sysinfo",
"tauri", "tauri",
"tauri-build", "tauri-build",
@@ -3726,6 +3727,16 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" 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]] [[package]]
name = "syn" name = "syn"
version = "1.0.92" version = "1.0.92"

View File

@@ -14,6 +14,13 @@ rust-version = "1.57"
[build-dependencies] [build-dependencies]
tauri-build = { version = "1.0.0-rc.8", features = [] } 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] [dependencies]
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.0-rc.9", features = ["api-all"] } tauri = { version = "1.0.0-rc.9", features = ["api-all"] }
@@ -28,9 +35,6 @@ zip = "0.6.2"
# For creating a "global" downloads list. # For creating a "global" downloads list.
lazy_static = "1.4.0" lazy_static = "1.4.0"
# Access to the Windows Registry.
registry = "1.2.1"
# Program opener. # Program opener.
open = "2.1.2" open = "2.1.2"
duct = "0.13.5" duct = "0.13.5"
@@ -39,7 +43,6 @@ duct = "0.13.5"
serde_json = "1" serde_json = "1"
# System process elevation. # System process elevation.
is_elevated = "0.1.2"
runas = "0.2.1" runas = "0.2.1"
# Dependencies for the HTTP(S) proxy. # Dependencies for the HTTP(S) proxy.

View File

@@ -17,11 +17,12 @@ use hudsucker::{
use std::fs; use std::fs;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::Path; use std::path::Path;
use registry::{Hive, Data, Security};
use rustls_pemfile as pemfile; use rustls_pemfile as pemfile;
use tauri::http::Uri; use tauri::http::Uri;
use crate::system_helpers::run_command;
#[cfg(windows)]
use registry::{Hive, Data, Security};
async fn shutdown_signal() { async fn shutdown_signal() {
tokio::signal::ctrl_c().await tokio::signal::ctrl_c().await
@@ -109,8 +110,8 @@ pub async fn create_proxy(proxy_port: u16, certificate_path: String) {
/** /**
* Connects to the local HTTP(S) proxy server. * Connects to the local HTTP(S) proxy server.
*/ */
#[cfg(windows)]
pub fn connect_to_proxy(proxy_port: u16) { pub fn connect_to_proxy(proxy_port: u16) {
if cfg!(target_os = "windows") {
// Create 'ProxyServer' string. // Create 'ProxyServer' string.
let server_string: String = format!("http=127.0.0.1:{};https=127.0.0.1:{}", proxy_port, proxy_port); let server_string: String = format!("http=127.0.0.1:{};https=127.0.0.1:{}", proxy_port, proxy_port);
@@ -120,26 +121,32 @@ pub fn connect_to_proxy(proxy_port: u16) {
// Set registry values. // Set registry values.
settings.set_value("ProxyServer", &Data::String(server_string.parse().unwrap())).unwrap(); settings.set_value("ProxyServer", &Data::String(server_string.parse().unwrap())).unwrap();
settings.set_value("ProxyEnable", &Data::U32(1)).unwrap(); settings.set_value("ProxyEnable", &Data::U32(1)).unwrap();
}
println!("Connected to the proxy."); 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. * Disconnects from the local HTTP(S) proxy server.
*/ */
#[cfg(windows)]
pub fn disconnect_from_proxy() { pub fn disconnect_from_proxy() {
if cfg!(target_os = "windows") {
// Fetch the 'Internet Settings' registry key. // Fetch the 'Internet Settings' registry key.
let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap(); let settings = Hive::CurrentUser.open(r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", Security::Write).unwrap();
// Set registry values. // Set registry values.
settings.set_value("ProxyEnable", &Data::U32(0)).unwrap(); settings.set_value("ProxyEnable", &Data::U32(0)).unwrap();
}
println!("Disconnected from proxy."); println!("Disconnected from proxy.");
} }
#[cfg(not(windows))]
pub fn disconnect_from_proxy() {}
/* /*
* Generates a private key and certificate used by the certificate authority. * Generates a private key and certificate used by the certificate authority.
* Additionally installs the certificate and private key in the Root CA store. * 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. * Attempts to install the certificate authority's certificate into the Root CA store.
*/ */
#[cfg(windows)]
pub fn install_ca_files(cert_path: &Path) { pub fn install_ca_files(cert_path: &Path) {
if cfg!(target_os = "windows") { crate::system_helpers::run_command("certutil", vec!["-user", "-addstore", "Root", cert_path.to_str().unwrap()]);
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()]);
}
println!("Installed certificate."); println!("Installed certificate.");
} }
#[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.");
}

View File

@@ -76,7 +76,14 @@ pub fn install_location() -> String {
return exe_path.to_str().unwrap().to_string(); return exe_path.to_str().unwrap().to_string();
} }
#[cfg(windows)]
#[tauri::command] #[tauri::command]
pub fn is_elevated() -> bool { pub fn is_elevated() -> bool {
return is_elevated::is_elevated(); is_elevated::is_elevated()
}
#[cfg(unix)]
#[tauri::command]
pub fn is_elevated() -> bool {
sudo::check() == sudo::RunningAs::Root
} }