diff --git a/Cargo.toml b/Cargo.toml index 571a06a..f8fc135 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ windows = { version = "0.52", features = [ "Win32_Foundation", ] } +[dev-dependencies] +serial_test = "3.1.1" [features] default = ["iptools"] diff --git a/src/linux.rs b/src/linux.rs index fcc8425..17f9958 100644 --- a/src/linux.rs +++ b/src/linux.rs @@ -1,4 +1,4 @@ -use crate::{Error, Result, Sysproxy}; +use crate::{Autoproxy, Error, Result, Sysproxy}; use std::{env, process::Command, str::from_utf8}; use xdg; @@ -232,7 +232,7 @@ fn gsettings() -> Command { fn kreadconfig() -> Command { let command = match env::var("KDE_SESSION_VERSION").unwrap_or_default().as_str() { "6" => "kreadconfig6", - _ => "kreadconfig5" + _ => "kreadconfig5", }; Command::new(command) } @@ -240,7 +240,7 @@ fn kreadconfig() -> Command { fn kwriteconfig() -> Command { let command = match env::var("KDE_SESSION_VERSION").unwrap_or_default().as_str() { "6" => "kwriteconfig6", - _ => "kwriteconfig5" + _ => "kwriteconfig5", }; Command::new(command) } @@ -364,3 +364,101 @@ fn strip_str<'a>(text: &'a str) -> &'a str { .strip_suffix('\'') .unwrap_or(text) } + +impl Autoproxy { + pub fn get_auto_proxy() -> Result { + let (enable, url) = match env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().as_str() { + "KDE" => { + let xdg_dir = xdg::BaseDirectories::new()?; + let config = xdg_dir.get_config_file("kioslaverc"); + let config = config.to_str().ok_or(Error::ParseStr("config".into()))?; + + let mode = kreadconfig() + .args([ + "--file", + config, + "--group", + "Proxy Settings", + "--key", + "ProxyType", + ]) + .output()?; + let mode = from_utf8(&mode.stdout) + .or(Err(Error::ParseStr("mode".into())))? + .trim(); + let url = kreadconfig() + .args([ + "--file", + config, + "--group", + "Proxy Settings", + "--key", + "Proxy Config Script", + ]) + .output()?; + let url = from_utf8(&url.stdout) + .or(Err(Error::ParseStr("url".into())))? + .trim(); + (mode == "2", url.to_string()) + } + _ => { + let mode = gsettings().args(["get", CMD_KEY, "mode"]).output()?; + let mode = from_utf8(&mode.stdout) + .or(Err(Error::ParseStr("mode".into())))? + .trim(); + let url = gsettings() + .args(["get", CMD_KEY, "autoconfig-url"]) + .output()?; + let url: &str = from_utf8(&url.stdout) + .or(Err(Error::ParseStr("url".into())))? + .trim(); + let url = strip_str(url); + (mode == "'auto'", url.to_string()) + } + }; + + Ok(Autoproxy { enable, url }) + } + + pub fn set_auto_proxy(&self) -> Result<()> { + match env::var("XDG_CURRENT_DESKTOP").unwrap_or_default().as_str() { + "KDE" => { + let xdg_dir = xdg::BaseDirectories::new()?; + let config = xdg_dir.get_config_file("kioslaverc"); + let config = config.to_str().ok_or(Error::ParseStr("config".into()))?; + let mode = if self.enable { "2" } else { "0" }; + kwriteconfig() + .args([ + "--file", + config, + "--group", + "Proxy Settings", + "--key", + "ProxyType", + mode, + ]) + .status()?; + kwriteconfig() + .args([ + "--file", + config, + "--group", + "Proxy Settings", + "--key", + "Proxy Config Script", + &self.url, + ]) + .status()?; + } + _ => { + let mode = if self.enable { "'auto'" } else { "'none'" }; + gsettings().args(["set", CMD_KEY, "mode", mode]).status()?; + gsettings() + .args(["set", CMD_KEY, "autoconfig-url", &self.url]) + .status()?; + } + } + + Ok(()) + } +} diff --git a/tests/test.rs b/tests/test.rs index b0e960b..a91cd80 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -1,5 +1,6 @@ #[cfg(test)] mod tests { + use serial_test::serial; use sysproxy::{Autoproxy, Sysproxy}; #[test] @@ -23,6 +24,7 @@ mod tests { } #[test] + #[serial] fn test_system_enable() { let mut sysproxy = Sysproxy { enable: true, @@ -47,6 +49,7 @@ mod tests { } #[test] + #[serial] fn test_auto_enable() { let mut autoproxy = Autoproxy { enable: true,