From 40a818630df8d34baf97cf88ae052770438992f8 Mon Sep 17 00:00:00 2001 From: GyDi Date: Wed, 9 Mar 2022 02:36:03 +0800 Subject: [PATCH] fix: change proxy bypass on mac --- src-tauri/src/utils/sysopt.rs | 69 ++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/src-tauri/src/utils/sysopt.rs b/src-tauri/src/utils/sysopt.rs index a258d6f..9344cc4 100755 --- a/src-tauri/src/utils/sysopt.rs +++ b/src-tauri/src/utils/sysopt.rs @@ -36,8 +36,10 @@ impl SysProxyConfig { bypass: bypass.unwrap_or(DEFAULT_BYPASS.into()), } } +} - #[cfg(target_os = "windows")] +#[cfg(target_os = "windows")] +impl SysProxyConfig { /// Get the windows system proxy config pub fn get_sys() -> io::Result { use winreg::enums::*; @@ -56,7 +58,26 @@ impl SysProxyConfig { }) } - #[cfg(target_os = "macos")] + /// Set the windows system proxy config + pub fn set_sys(&self) -> io::Result<()> { + use winreg::enums::*; + use winreg::RegKey; + 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 self.enable { 1u32 } else { 0u32 }; + + cur_var.set_value("ProxyEnable", &enable)?; + cur_var.set_value("ProxyServer", &self.server)?; + cur_var.set_value("ProxyOverride", &self.bypass) + } +} + +#[cfg(target_os = "macos")] +impl SysProxyConfig { /// Get the macos system proxy config pub fn get_sys() -> io::Result { use std::process::Command; @@ -84,41 +105,39 @@ impl SysProxyConfig { let bypass_output = Command::new("networksetup") .args(["-getproxybypassdomains", MACOS_SERVICE]) .output()?; - let bypass = std::str::from_utf8(&bypass_output.stdout).unwrap_or(DEFAULT_BYPASS); + + // change the format to xxx,xxx + let bypass = std::str::from_utf8(&bypass_output.stdout) + .unwrap_or(DEFAULT_BYPASS) + .to_string() + .split('\n') + .collect::>() + .join(","); Ok(SysProxyConfig { enable, server, - bypass: bypass.into(), + bypass, }) } - #[cfg(target_os = "windows")] - /// Set the windows system proxy config - pub fn set_sys(&self) -> io::Result<()> { - use winreg::enums::*; - use winreg::RegKey; - 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 self.enable { 1u32 } else { 0u32 }; - - cur_var.set_value("ProxyEnable", &enable)?; - cur_var.set_value("ProxyServer", &self.server)?; - cur_var.set_value("ProxyOverride", &self.bypass) - } - - #[cfg(target_os = "macos")] /// Set the macos system proxy config pub fn set_sys(&self) -> io::Result<()> { + use std::process::Command; + let enable = self.enable; let server = self.server.as_str(); + let bypass = self.bypass.clone(); macproxy::set_proxy("-setwebproxy", MACOS_SERVICE, enable, server)?; macproxy::set_proxy("-setsecurewebproxy", MACOS_SERVICE, enable, server)?; - macproxy::set_proxy("-setsocksfirewallproxy", MACOS_SERVICE, enable, server) + macproxy::set_proxy("-setsocksfirewallproxy", MACOS_SERVICE, enable, server)?; + + let domains = bypass.split(",").collect::>(); + Command::new("networksetup") + .args([["-setproxybypassdomains", MACOS_SERVICE].to_vec(), domains].concat()) + .status()?; + + Ok(()) } } @@ -171,7 +190,7 @@ mod macproxy { } /// parse the networksetup output - pub(super) fn parse<'a>(target: &'a str, key: &'a str) -> &'a str { + fn parse<'a>(target: &'a str, key: &'a str) -> &'a str { match target.find(key) { Some(idx) => { let idx = idx + key.len();