fix(proxy): add robust system proxy fallback and log redaction

- Fallback to reading Windows Registry for ProxyServer if sysproxy crate fails parsing.
- Fallback to reading HTTP_PROXY and related environment variables.
- Redact user home directory in tauri_plugin_log to protect privacy.
- Improves yt-dlp compatibility for domains blocked by local ISPs.

Resolves #5
This commit is contained in:
NimBold
2026-07-05 20:31:34 +03:30
parent 757e313f71
commit 3da73c623f
2 changed files with 107 additions and 1 deletions
+24
View File
@@ -5303,6 +5303,30 @@ pub fn run() {
.max_file_size(10_000_000)
.rotation_strategy(tauri_plugin_log::RotationStrategy::KeepSome(3))
.timezone_strategy(tauri_plugin_log::TimezoneStrategy::UseLocal)
.format({
let home_dir = std::env::var("USERPROFILE").or_else(|_| std::env::var("HOME")).unwrap_or_default();
let home_dir = if home_dir.len() > 3 { home_dir } else { String::new() };
let escaped_home_dir = home_dir.replace("\\", "\\\\");
move |out, message, record| {
let msg = message.to_string();
let redacted = if !home_dir.is_empty() {
let mut s = msg.replace(&home_dir, "<HOME>");
if !escaped_home_dir.is_empty() && escaped_home_dir != home_dir {
s = s.replace(&escaped_home_dir, "<HOME>");
}
s
} else {
msg
};
out.finish(format_args!(
"[{}][{}][{}] {}",
chrono::Local::now().format("%Y-%m-%d][%H:%M:%S"),
record.level(),
record.target(),
redacted
))
}
})
.build(),
)
.plugin(tauri_plugin_dialog::init())
+83 -1
View File
@@ -18,10 +18,92 @@ pub async fn get_system_proxy() -> Result<Option<String>, String> {
Ok(None)
}
}
Err(error) => Err(format!("failed to read system proxy settings: {error}")),
Err(error) => {
#[cfg(target_os = "windows")]
if let Ok(Some(proxy)) = fallback_windows_proxy() {
return Ok(Some(proxy));
}
if let Ok(proxy) = std::env::var("HTTP_PROXY")
.or_else(|_| std::env::var("http_proxy"))
.or_else(|_| std::env::var("HTTPS_PROXY"))
.or_else(|_| std::env::var("https_proxy"))
.or_else(|_| std::env::var("ALL_PROXY"))
.or_else(|_| std::env::var("all_proxy"))
{
if !proxy.is_empty() {
let protocol = if proxy.contains("://") {
""
} else {
"http://"
};
return Ok(Some(format!("{}{}", protocol, proxy)));
}
}
Err(format!("failed to read system proxy settings: {error}"))
}
}
}
#[cfg(target_os = "windows")]
fn fallback_windows_proxy() -> Result<Option<String>, ()> {
use std::os::windows::process::CommandExt;
use std::process::Command;
const CREATE_NO_WINDOW: u32 = 0x08000000;
let output = Command::new("reg")
.args(&[
"query",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v",
"ProxyEnable",
])
.creation_flags(CREATE_NO_WINDOW)
.output()
.map_err(|_| ())?;
let stdout = String::from_utf8_lossy(&output.stdout);
if !stdout.contains("0x1") {
return Ok(None);
}
let output = Command::new("reg")
.args(&[
"query",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
"/v",
"ProxyServer",
])
.creation_flags(CREATE_NO_WINDOW)
.output()
.map_err(|_| ())?;
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if line.contains("ProxyServer") {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 3 {
let proxy_val = parts[2];
if proxy_val.contains('=') {
for part in proxy_val.split(';') {
if part.starts_with("http=") || part.starts_with("https=") || part.starts_with("socks=") {
let addr = part.split('=').nth(1).unwrap_or("");
if !addr.is_empty() {
let protocol = if part.starts_with("socks") { "socks5://" } else { "http://" };
return Ok(Some(format!("{}{}", protocol, addr)));
}
}
}
} else {
return Ok(Some(format!("http://{}", proxy_val)));
}
}
}
}
Ok(None)
}
#[tauri::command]
pub fn get_file_category(filename: String) -> DownloadCategory {
let ext = std::path::Path::new(&filename)