From debc1653268d003cc72b2f0a0cbbb543166033e3 Mon Sep 17 00:00:00 2001 From: RiceCake <1250839773a@gmail.com> Date: Tue, 30 Jul 2024 00:01:20 +0800 Subject: [PATCH] delete bloated registry items (#200) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * delete bloated registry items --------- Co-authored-by: 荣耀的捍卫者 <1250839773@qq.com> --- easytier-gui/src-tauri/Cargo.toml | 1 + easytier-gui/src-tauri/src/lib.rs | 3 +- easytier/Cargo.toml | 1 + easytier/src/instance/virtual_nic.rs | 80 ++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/easytier-gui/src-tauri/Cargo.toml b/easytier-gui/src-tauri/Cargo.toml index 7f8cc7c..6260d8e 100644 --- a/easytier-gui/src-tauri/Cargo.toml +++ b/easytier-gui/src-tauri/Cargo.toml @@ -41,6 +41,7 @@ tauri-plugin-positioner = { version = "2.0.0-beta", features = ["tray-icon"] } tauri-plugin-vpnservice = { path = "../../tauri-plugin-vpnservice" } tauri-plugin-os = "2.0.0-beta.7" + [features] # This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!! custom-protocol = ["tauri/custom-protocol"] diff --git a/easytier-gui/src-tauri/src/lib.rs b/easytier-gui/src-tauri/src/lib.rs index 7083867..9a0bf57 100644 --- a/easytier-gui/src-tauri/src/lib.rs +++ b/easytier-gui/src-tauri/src/lib.rs @@ -19,6 +19,7 @@ use serde::{Deserialize, Serialize}; use tauri::Manager as _; + #[derive(Deserialize, Serialize, PartialEq, Debug)] enum NetworkingMethod { PublicServer, @@ -336,6 +337,7 @@ pub fn init_launch(_app_handle: &tauri::AppHandle, enable: bool) -> Result, ifcfg: Box, } +#[cfg(target_os = "windows")] +pub fn checkreg() -> io::Result<()> { + use winreg::{enums::HKEY_LOCAL_MACHINE, RegKey,enums::KEY_ALL_ACCESS}; + // 打开根键 + let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); + // 打开指定的子键 + let profiles_key = hklm.open_subkey_with_flags( + "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Profiles", + KEY_ALL_ACCESS, + )?; + let unmanaged_key = hklm.open_subkey_with_flags( + "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkList\\Signatures\\Unmanaged", + KEY_ALL_ACCESS, + )?; + // 收集要删除的子键名称 + let mut keys_to_delete = Vec::new(); + let mut keys_to_delete_unmanaged = Vec::new(); + for subkey_name in profiles_key.enum_keys().filter_map(Result::ok) { + let subkey = profiles_key.open_subkey(&subkey_name)?; + // 尝试读取 ProfileName 值 + match subkey.get_value::("ProfileName") { + Ok(profile_name) => { + // 检查 ProfileName 是否包含 "et" + if profile_name.contains("et_") { + keys_to_delete.push(subkey_name); + } + } + Err(e) => { + // 打印错误信息 + tracing::error!( + "Failed to read ProfileName for subkey {}: {}", + subkey_name, + e + ); + } + } + } + for subkey_name in unmanaged_key.enum_keys().filter_map(Result::ok) { + let subkey = unmanaged_key.open_subkey(&subkey_name)?; + // 尝试读取 ProfileName 值 + match subkey.get_value::("Description") { + Ok(profile_name) => { + // 检查 ProfileName 是否包含 "et" + if profile_name.contains("et_") { + keys_to_delete_unmanaged.push(subkey_name); + } + } + Err(e) => { + // 打印错误信息 + tracing::error!( + "Failed to read ProfileName for subkey {}: {}", + subkey_name, + e + ); + } + } + } + //删除收集到的子键 + if !keys_to_delete.is_empty() { + for subkey_name in keys_to_delete { + match profiles_key.delete_subkey_all(&subkey_name) { + Ok(_) => tracing::trace!("Successfully deleted subkey: {}", subkey_name), + Err(e) => tracing::error!("Failed to delete subkey {}: {}", subkey_name, e), + } + } + } + if !keys_to_delete_unmanaged.is_empty() { + for subkey_name in keys_to_delete_unmanaged { + match unmanaged_key.delete_subkey_all(&subkey_name) { + Ok(_) => tracing::trace!("Successfully deleted subkey: {}", subkey_name), + Err(e) => tracing::error!("Failed to delete subkey {}: {}", subkey_name, e), + } + } + } + Ok(()) +} impl VirtualNic { pub fn new(global_ctx: ArcGlobalCtx) -> Self { @@ -285,6 +361,10 @@ impl VirtualNic { #[cfg(target_os = "windows")] { + match checkreg(){ + Ok(_) => tracing::trace!("delete successful!"), + Err(e) => tracing::error!("An error occurred: {}", e), + } use rand::distributions::Distribution as _; use std::net::IpAddr; let c = crate::arch::windows::interface_count()?;