Skip to content

Commit

Permalink
fix: compatible with AppImage (#14)
Browse files Browse the repository at this point in the history
* fix: clippy lint

* fix(linux): try to solve appimage issue

* chore: bump deps

chore: bump deps
  • Loading branch information
greenhat616 authored Aug 27, 2024
1 parent bd912f9 commit 45fffb0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ description = "A library for set/get system proxy. Supports Windows, macOS and l
[dependencies]
log = "0.4"
thiserror = "1"
iptools = { version = "0.2.4", optional = true }
iptools = { version = "0.3.0", optional = true }

[target.'cfg(target_os = "linux")'.dependencies]
xdg = "^2.5"

[target.'cfg(target_os = "macos")'.dependencies]
interfaces = "0.0.8"
interfaces = "0.0.9"

[target.'cfg(target_os = "windows")'.dependencies]
winreg = { version = "0.52", features = ["transactions"] }
windows = { version = "0.52", features = [
windows = { version = "0.58", features = [
"Win32_Networking_WinInet",
"Win32_NetworkManagement_Rras",
"Win32_Foundation",
Expand Down
37 changes: 26 additions & 11 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::{Autoproxy, Error, Result, Sysproxy};
use std::{env, process::Command, str::from_utf8};
use xdg;
use std::{env, process::Command, str::from_utf8, sync::LazyLock};

const CMD_KEY: &str = "org.gnome.system.proxy";

static IS_APPIMAGE: LazyLock<bool> = LazyLock::new(|| {
std::env::var("APPIMAGE").is_ok()
});

impl Sysproxy {
pub fn get_system_proxy() -> Result<Sysproxy> {
let enable = Sysproxy::get_enable()?;
Expand All @@ -12,12 +15,12 @@ impl Sysproxy {
let https = get_proxy("https")?;
let http = get_proxy("http")?;

if socks.host.len() == 0 {
if http.host.len() > 0 {
if socks.host.is_empty() {
if !http.host.is_empty() {
socks.host = http.host;
socks.port = http.port;
}
if https.host.len() > 0 {
if !https.host.is_empty() {
socks.host = https.host;
socks.port = https.port;
}
Expand Down Expand Up @@ -195,7 +198,7 @@ impl Sysproxy {
host = String::from("'") + &host;
}
if !host.ends_with('\'') && !host.ends_with('"') {
host = host + "'";
host += "'";
}
host
})
Expand Down Expand Up @@ -226,23 +229,35 @@ impl Sysproxy {
}

fn gsettings() -> Command {
Command::new("gsettings")
let mut command = Command::new("gsettings");
if *IS_APPIMAGE {
command.env_remove("LD_LIBRARY_PATH");
}
command
}

fn kreadconfig() -> Command {
let command = match env::var("KDE_SESSION_VERSION").unwrap_or_default().as_str() {
"6" => "kreadconfig6",
_ => "kreadconfig5",
};
Command::new(command)
let mut command = Command::new(command);
if *IS_APPIMAGE {
command.env_remove("LD_LIBRARY_PATH");
}
command
}

fn kwriteconfig() -> Command {
let command = match env::var("KDE_SESSION_VERSION").unwrap_or_default().as_str() {
"6" => "kwriteconfig6",
_ => "kwriteconfig5",
};
Command::new(command)
let mut command = Command::new(command);
if *IS_APPIMAGE {
command.env_remove("LD_LIBRARY_PATH");
}
command
}

fn set_proxy(proxy: &Sysproxy, service: &str) -> Result<()> {
Expand All @@ -260,7 +275,7 @@ fn set_proxy(proxy: &Sysproxy, service: &str) -> Result<()> {
_ => "http",
};

let host = format!("{}", proxy.host);
let host = proxy.host.to_string();
let host = host.as_str();
let port = format!("{}", proxy.port);
let port = port.as_str();
Expand Down Expand Up @@ -358,7 +373,7 @@ fn get_proxy(service: &str) -> Result<Sysproxy> {
}
}

fn strip_str<'a>(text: &'a str) -> &'a str {
fn strip_str(text: &str) -> &str {
text.strip_prefix('\'')
.unwrap_or(text)
.strip_suffix('\'')
Expand Down
6 changes: 3 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Error, Result};
use iptools::iprange::{IpRange, IpVer};
use iptools::iprange::{IPv4, IpRange, IpVer};

/// Convert ipv4 cidr to wildcard
///
Expand All @@ -9,9 +9,9 @@ use iptools::iprange::{IpRange, IpVer};
/// assert_eq!(ipv4_cidr_to_wildcard("127.0.0.1/8").unwrap(), vec!["127.*".to_string()]);
/// ```
pub fn ipv4_cidr_to_wildcard(cidr: &str) -> Result<Vec<String>> {
let ip = IpRange::new(cidr, "").or(Err(Error::ParseStr(cidr.into())))?;
let ip = IpRange::<IPv4>::new(cidr, "").or(Err(Error::ParseStr(cidr.into())))?;

if ip.get_version() != IpVer::IPV4 {
if ip.get_version() != IpVer::V4 {
return Err(Error::ParseStr(cidr.into()));
}

Expand Down

0 comments on commit 45fffb0

Please sign in to comment.