From d3b797f0fb92e0c1336785f9ee5e32431a3ad83a Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 5 May 2025 14:19:34 -0400 Subject: [PATCH] feat(client): add windows system proxies for Matcher --- Cargo.toml | 5 ++++- src/client/proxy/matcher.rs | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 70634885..69ee1453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,9 @@ pnet_datalink = "0.35.0" [target.'cfg(target_os = "macos")'.dependencies] system-configuration = { version = "0.6.1", optional = true } +[target.'cfg(windows)'.dependencies] +windows-registry = { version = "0.4", optional = true } + [features] default = [] @@ -68,7 +71,7 @@ full = [ client = ["hyper/client", "dep:tracing", "dep:futures-channel", "dep:tower-service"] client-legacy = ["client", "dep:socket2", "tokio/sync", "dep:libc"] client-proxy = ["client", "dep:base64", "dep:ipnet", "dep:percent-encoding"] -client-proxy-system = ["dep:system-configuration"] +client-proxy-system = ["dep:system-configuration", "dep:windows-registry"] server = ["hyper/server"] server-auto = ["server", "http1", "http2"] diff --git a/src/client/proxy/matcher.rs b/src/client/proxy/matcher.rs index c2a1cc2c..f7ad8971 100644 --- a/src/client/proxy/matcher.rs +++ b/src/client/proxy/matcher.rs @@ -242,6 +242,9 @@ impl Builder { #[cfg(all(feature = "client-proxy-system", target_os = "macos"))] mac::with_system(&mut builder); + #[cfg(all(feature = "client-proxy-system", windows))] + win::with_system(&mut builder); + builder } @@ -638,6 +641,41 @@ mod mac { } } +#[cfg(feature = "client-proxy-system")] +#[cfg(windows)] +mod win { + pub(super) fn with_system(builder: &mut super::Builder) { + let settings = if let Ok(settings) = windows_registry::CURRENT_USER + .open("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings") + { + settings + } else { + return; + }; + + if settings.get_u32("ProxyEnable").unwrap_or(0) == 0 { + return; + } + + if builder.http.is_empty() { + if let Ok(val) = settings.get_string("ProxyServer") { + builder.http = val; + } + } + + if builder.no.is_empty() { + if let Ok(val) = settings.get_string("ProxyOverride") { + builder.no = val + .split(';') + .map(|s| s.trim()) + .collect::>() + .join(",") + .replace("*.", ""); + } + } + } +} + #[cfg(test)] mod tests { use super::*;