From f4ce4c40f12cf9e9cc6351f47ceff23e6e77779c Mon Sep 17 00:00:00 2001 From: kkylin-si <> Date: Sat, 7 Nov 2026 15:14:09 +0000 Subject: [PATCH] fix(P3-7): redact DNS addresses from diagnostics --- src-tauri/src/net.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/net.rs b/src-tauri/src/net.rs index 969d0be..42e7c0d 100644 --- a/src-tauri/src/net.rs +++ b/src-tauri/src/net.rs @@ -285,6 +285,20 @@ pub fn err_text(e: &reqwest::Error) -> String { out } +fn dns_summary(addrs: impl IntoIterator) -> Value { + let mut ips: Vec<_> = addrs.into_iter().map(|addr| addr.ip()).collect(); + ips.sort_unstable(); + ips.dedup(); + let has_fake_ip = ips.iter().any(|ip| { + let v4 = match ip { + std::net::IpAddr::V4(v4) => Some(*v4), + std::net::IpAddr::V6(v6) => v6.to_ipv4_mapped(), + }; + v4.is_some_and(|ip| { let [a, b, _, _] = ip.octets(); a == 198 && (b == 18 || b == 19) }) + }); + json!({ "count": ips.len(), "has_fake_ip": has_fake_ip }) +} + /// 网络自检:DNS / HTTP / 代理相关环境变量,用于远程定位「打不开」。 pub async fn diagnose() -> Value { let cur = base(); @@ -299,8 +313,8 @@ pub async fn diagnose() -> Value { tokio::task::spawn_blocking(move || { use std::net::ToSocketAddrs; match (host.as_str(), 443u16).to_socket_addrs() { - Ok(iter) => json!(iter.map(|a| a.ip().to_string()).collect::>()), - Err(e) => json!(format!("解析失败: {e}")), + Ok(iter) => dns_summary(iter), + Err(_) => json!({ "error": "DNS 解析失败" }), } }) .await @@ -497,6 +511,16 @@ pub async fn fetch_bytes(url: &str) -> Result<(Option, Vec), String> mod tests { use super::*; + #[test] + fn dns_diagnostics_only_expose_counts_and_fake_ip_presence() { + let addrs: Vec = ["10.0.0.1:443", "198.18.0.2:443", "[::ffff:198.18.0.3]:443"].into_iter().map(|raw| raw.parse().unwrap()).collect(); + let summary = dns_summary(addrs); + assert_eq!(summary, json!({"count": 3, "has_fake_ip": true})); + assert!(!summary.to_string().contains("10.0.0.1")); + assert!(!summary.to_string().contains("198.18")); + assert_eq!(dns_summary(Vec::::new()), json!({"count":0,"has_fake_ip":false})); + } + #[test] fn pinned_clients_expire_without_extending_the_dns_ttl() { let now = Instant::now();