Skip to content

Commit

Permalink
Use libproxy on Windows (#320)
Browse files Browse the repository at this point in the history
* proxy(Windows): use libproxy handle Windows proxy settings

Signed-off-by: tsukinaha <[email protected]>

* Fix windows target feature (again)

Signed-off-by: tsukinaha <[email protected]>

* libproxy: try to parse direct

Signed-off-by: tsukinaha <[email protected]>

---------

Signed-off-by: tsukinaha <[email protected]>
  • Loading branch information
tsukinaha authored Jan 23, 2025
1 parent 130dbc1 commit b98edad
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ xattr ={ version = "1.4.0" }

[target.'cfg(target_os = "windows")'.dependencies]
gdk4-win32 = { version = "0.9" }
libproxy = { version = "0.1.1" }

[package.metadata.deb]
maintainer = "Inaha <[email protected]>"
Expand Down
6 changes: 4 additions & 2 deletions src/client/emby_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ fn generate_hash(s: &str) -> String {
format!("{:x}", hasher.finish())
}

impl EmbyClient {
pub fn default() -> Self {
impl Default for EmbyClient {
fn default() -> Self {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert("Accept-Encoding", HeaderValue::from_static("gzip"));
headers.insert(
Expand All @@ -159,7 +159,9 @@ impl EmbyClient {
server_name_hash: Mutex::new(String::new()),
}
}
}

impl EmbyClient {
pub async fn init(&self, account: &Account) -> Result<(), Box<dyn std::error::Error>> {
self.header_change_url(&account.server, &account.port)
.await?;
Expand Down
37 changes: 16 additions & 21 deletions src/client/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,23 @@ use windows::{
};
#[cfg(target_os = "windows")]
pub fn get_proxy_settings() -> Option<String> {
unsafe {
let mut proxy_config = WINHTTP_CURRENT_USER_IE_PROXY_CONFIG::default();
if WinHttpGetIEProxyConfigForCurrentUser(&mut proxy_config).is_err() {
return None;
}
// FIXME: proxy should be a dynamic constructor
//
// This is only a temporary solution to get the proxy settings on Windows.
// ProxyFactory::get_proxies() is a blocking method, PAC may be a stream.
const EXAMPLE_PROXY: &str = "http://example.com";

if !proxy_config.lpszProxy.is_null() {
return PCWSTR(proxy_config.lpszProxy.0).to_string().ok();
}

if !proxy_config.lpszAutoConfigUrl.is_null() {
return PCWSTR(proxy_config.lpszAutoConfigUrl.0)
.to_string()
.map(|proxy_url| {
proxy_url.split('/').collect::<Vec<_>>()[..3]
.join("/")
.to_string()
})
.ok();
}
}
None
// FIEXME: user:password@ is not supported
//
// libproxy will return "direct://", if no proxy is found.
// protocol://[user:password@]proxyhost[:port], but reqwest cant parse [user:password@]
use libproxy::ProxyFactory;
ProxyFactory::new()?
.get_proxies(EXAMPLE_PROXY)
.ok()?
.first()
.filter(|&proxy| proxy != "direct://")
.cloned()
}

#[cfg(target_os = "linux")]
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use std::{
};

mod arg;
mod client;
mod config;
mod gstl;
mod macros;
mod ui;
mod utils;

pub mod client;

#[cfg(target_os = "windows")]
pub use client::windows_compat::theme::is_system_dark_mode_enabled;

Expand Down

0 comments on commit b98edad

Please sign in to comment.