From 1e176c43166674daaa398dd31bff8419c0542dca Mon Sep 17 00:00:00 2001 From: GyDi Date: Sun, 12 Dec 2021 18:02:41 +0800 Subject: [PATCH] feat: refactor system proxy config --- src-tauri/src/sysopt.rs | 89 ++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/src-tauri/src/sysopt.rs b/src-tauri/src/sysopt.rs index b21e769..4e42154 100644 --- a/src-tauri/src/sysopt.rs +++ b/src-tauri/src/sysopt.rs @@ -1,46 +1,71 @@ use serde::{Deserialize, Serialize}; use std::io; -#[cfg(target_os = "windows")] -use winreg::enums::*; -#[cfg(target_os = "windows")] -use winreg::RegKey; - #[derive(Debug, Deserialize, Serialize)] -pub struct ProxyConfig { - enable: u32, +pub struct SysProxyConfig { + enable: bool, server: String, bypass: String, } #[cfg(target_os = "windows")] -/// Get the windows system proxy config -pub fn get_proxy_config() -> io::Result { - let hkcu = RegKey::predef(HKEY_CURRENT_USER); - let cur_var = hkcu.open_subkey_with_flags( - "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", - KEY_READ, - )?; +mod win { + use super::*; + use winreg::enums::*; + use winreg::RegKey; - Ok(ProxyConfig { - enable: cur_var.get_value("ProxyEnable")?, - server: cur_var.get_value("ProxyServer")?, - bypass: cur_var.get_value("ProxyOverride")?, - }) + /// Get the windows system proxy config + pub fn get_proxy_config() -> io::Result { + let hkcu = RegKey::predef(HKEY_CURRENT_USER); + let cur_var = hkcu.open_subkey_with_flags( + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + KEY_READ, + )?; + + Ok(SysProxyConfig { + enable: cur_var.get_value::("ProxyEnable")? == 1u32, + server: cur_var.get_value("ProxyServer")?, + bypass: cur_var.get_value("ProxyOverride")?, + }) + } + + /// Set the windows system proxy config + pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> { + let hkcu = RegKey::predef(HKEY_CURRENT_USER); + let cur_var = hkcu.open_subkey_with_flags( + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + KEY_SET_VALUE, + )?; + + let enable: u32 = if config.enable { 1u32 } else { 0u32 }; + + cur_var.set_value("ProxyEnable", &enable)?; + cur_var.set_value("ProxyServer", &config.server)?; + cur_var.set_value("ProxyOverride", &config.bypass)?; + + Ok(()) + } +} + +#[cfg(target_os = "macos")] +mod macos { + use super::*; + + pub fn get_proxy_config() -> io::Result { + Ok(SysProxyConfig { + enable: false, + server: "server".into(), + bypass: "bypass".into(), + }) + } + + pub fn set_proxy_config(config: &SysProxyConfig) -> io::Result<()> { + Ok(()) + } } #[cfg(target_os = "windows")] -/// Set the windows system proxy config -pub fn set_proxy_config(config: &ProxyConfig) -> io::Result<()> { - let hkcu = RegKey::predef(HKEY_CURRENT_USER); - let cur_var = hkcu.open_subkey_with_flags( - "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", - KEY_SET_VALUE, - )?; +pub use win::*; - cur_var.set_value("ProxyEnable", &config.enable)?; - cur_var.set_value("ProxyServer", &config.server)?; - cur_var.set_value("ProxyOverride", &config.bypass)?; - - Ok(()) -} +#[cfg(target_os = "macos")] +pub use macos::*;