From 68e07b350dc2b84a8c56047ea7132fe74af6abc1 Mon Sep 17 00:00:00 2001 From: YoVinchen Date: Mon, 12 Jan 2026 17:44:12 +0800 Subject: [PATCH] fix(proxy): fix double encoding issue in proxy auth and add debug logs - Remove encodeURIComponent in mergeAuth() since URL object's username/password setters already do percent-encoding automatically - Add GP-010 debug log for database read operations - Add GP-011 debug log to track incoming URL info (length, has_auth) - Fix username.trim() in fallback branch for consistent behavior --- src-tauri/src/commands/global_proxy.rs | 18 +++++++++++++++++- .../settings/GlobalProxySettings.tsx | 12 +++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/commands/global_proxy.rs b/src-tauri/src/commands/global_proxy.rs index e8a994df6..dcec13e5c 100644 --- a/src-tauri/src/commands/global_proxy.rs +++ b/src-tauri/src/commands/global_proxy.rs @@ -13,7 +13,15 @@ use std::time::{Duration, Instant}; /// 返回当前配置的代理 URL,null 表示直连。 #[tauri::command] pub fn get_global_proxy_url(state: tauri::State<'_, AppState>) -> Result, String> { - state.db.get_global_proxy_url().map_err(|e| e.to_string()) + let result = state.db.get_global_proxy_url().map_err(|e| e.to_string())?; + log::debug!( + "[GlobalProxy] [GP-010] Read from database: {}", + result + .as_ref() + .map(|u| http_client::mask_url(u)) + .unwrap_or_else(|| "None".to_string()) + ); + Ok(result) } /// 设置全局代理 URL @@ -25,6 +33,14 @@ pub fn get_global_proxy_url(state: tauri::State<'_, AppState>) -> Result, url: String) -> Result<(), String> { + // 调试:显示接收到的 URL 信息(不包含敏感内容) + let has_auth = url.contains('@') && (url.starts_with("http://") || url.starts_with("socks")); + log::debug!( + "[GlobalProxy] [GP-011] Received URL: length={}, has_auth={}", + url.len(), + has_auth + ); + let url_opt = if url.trim().is_empty() { None } else { diff --git a/src/components/settings/GlobalProxySettings.tsx b/src/components/settings/GlobalProxySettings.tsx index 5d0caa24f..089abd104 100644 --- a/src/components/settings/GlobalProxySettings.tsx +++ b/src/components/settings/GlobalProxySettings.tsx @@ -49,18 +49,20 @@ function mergeAuth( try { const parsed = new URL(baseUrl); - parsed.username = encodeURIComponent(username.trim()); + // URL 对象的 username/password setter 会自动进行 percent-encoding + // 不要使用 encodeURIComponent,否则会导致双重编码 + parsed.username = username.trim(); if (password) { - parsed.password = encodeURIComponent(password); + parsed.password = password; } return parsed.toString(); } catch { - // URL 解析失败,尝试手动插入 + // URL 解析失败,尝试手动插入(此时需要手动编码) const match = baseUrl.match(/^(\w+:\/\/)(.+)$/); if (match) { const auth = password - ? `${encodeURIComponent(username)}:${encodeURIComponent(password)}@` - : `${encodeURIComponent(username)}@`; + ? `${encodeURIComponent(username.trim())}:${encodeURIComponent(password)}@` + : `${encodeURIComponent(username.trim())}@`; return `${match[1]}${auth}${match[2]}`; } return baseUrl;