Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support PAC for Linux #10

Merged
merged 3 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ windows = { version = "0.52", features = [
"Win32_Foundation",
] }

[dev-dependencies]
serial_test = "3.1.1"

[features]
default = ["iptools"]
104 changes: 101 additions & 3 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -232,15 +232,15 @@ 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)
}

fn kwriteconfig() -> Command {
let command = match env::var("KDE_SESSION_VERSION").unwrap_or_default().as_str() {
"6" => "kwriteconfig6",
_ => "kwriteconfig5"
_ => "kwriteconfig5",
};
Command::new(command)
}
Expand Down Expand Up @@ -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<Autoproxy> {
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(())
}
}
3 changes: 3 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(test)]
mod tests {
use serial_test::serial;
use sysproxy::{Autoproxy, Sysproxy};

#[test]
Expand All @@ -23,6 +24,7 @@ mod tests {
}

#[test]
#[serial]
fn test_system_enable() {
let mut sysproxy = Sysproxy {
enable: true,
Expand All @@ -47,6 +49,7 @@ mod tests {
}

#[test]
#[serial]
fn test_auto_enable() {
let mut autoproxy = Autoproxy {
enable: true,
Expand Down
Loading