From 8c44b0b82a1881382b368a55e3a967343111823a Mon Sep 17 00:00:00 2001 From: Space Time Date: Sat, 7 Dec 2024 13:06:49 +0800 Subject: [PATCH] =?UTF-8?q?1.1.3=20->=201.1.4=20=E7=AC=AC32=E6=AC=A1?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utils/GlobalCealCleaner.cs | 42 ++++++++++++++++++++++++++++++++++++++ Utils/NginxProc.cs | 26 +---------------------- Wins/MainWin.xaml.cs | 4 ++++ 3 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 Utils/GlobalCealCleaner.cs diff --git a/Utils/GlobalCealCleaner.cs b/Utils/GlobalCealCleaner.cs new file mode 100644 index 0000000..321a2db --- /dev/null +++ b/Utils/GlobalCealCleaner.cs @@ -0,0 +1,42 @@ +using System.IO; +using System.Security.Cryptography.X509Certificates; +using Sheas_Cealer.Consts; + +namespace Sheas_Cealer.Utils +{ + internal static class GlobalCealCleaner + { + private static bool IsCleaning = false; + + internal static void Clean() + { + if (IsCleaning) + return; + + IsCleaning = true; + + string hostsContent = File.ReadAllText(MainConst.HostsConfPath); + int hostsConfStartIndex = hostsContent.IndexOf(MainConst.HostsConfStartMarker); + int hostsConfEndIndex = hostsContent.LastIndexOf(MainConst.HostsConfEndMarker); + + if (hostsConfStartIndex != -1 && hostsConfEndIndex != -1) + File.WriteAllText(MainConst.HostsConfPath, hostsContent.Remove(hostsConfStartIndex, hostsConfEndIndex - hostsConfStartIndex + MainConst.HostsConfEndMarker.Length)); + + using X509Store certStore = new(StoreName.Root, StoreLocation.CurrentUser, OpenFlags.ReadWrite); + + foreach (X509Certificate2 storedCert in certStore.Certificates) + if (storedCert.Subject == MainConst.NginxRootCertSubjectName) + while (true) + try + { + certStore.Remove(storedCert); + break; + } + catch { } + + certStore.Close(); + + IsCleaning = false; + } + } +} diff --git a/Utils/NginxProc.cs b/Utils/NginxProc.cs index ace3432..63025c6 100644 --- a/Utils/NginxProc.cs +++ b/Utils/NginxProc.cs @@ -1,6 +1,4 @@ using System; -using System.IO; -using System.Security.Cryptography.X509Certificates; using Sheas_Cealer.Consts; using SheasCore; @@ -10,27 +8,5 @@ internal class NginxProc : Proc { internal NginxProc() : base(MainConst.NginxPath) { } - public override void Process_Exited(object sender, EventArgs e) - { - string hostsContent = File.ReadAllText(MainConst.HostsConfPath); - int hostsConfStartIndex = hostsContent.IndexOf(MainConst.HostsConfStartMarker); - int hostsConfEndIndex = hostsContent.LastIndexOf(MainConst.HostsConfEndMarker); - - if (hostsConfStartIndex != -1 && hostsConfEndIndex != -1) - File.WriteAllText(MainConst.HostsConfPath, hostsContent.Remove(hostsConfStartIndex, hostsConfEndIndex - hostsConfStartIndex + MainConst.HostsConfEndMarker.Length)); - - using X509Store certStore = new(StoreName.Root, StoreLocation.CurrentUser, OpenFlags.ReadWrite); - - foreach (X509Certificate2 storedCert in certStore.Certificates) - if (storedCert.Subject == MainConst.NginxRootCertSubjectName) - while (true) - try - { - certStore.Remove(storedCert); - break; - } - catch { } - - certStore.Close(); - } + public override void Process_Exited(object sender, EventArgs e) => GlobalCealCleaner.Clean(); } \ No newline at end of file diff --git a/Wins/MainWin.xaml.cs b/Wins/MainWin.xaml.cs index 4998396..3f0ecaa 100644 --- a/Wins/MainWin.xaml.cs +++ b/Wins/MainWin.xaml.cs @@ -285,11 +285,15 @@ public partial class MainWin : Window MainPres.IsNginxIniting = false; } else + { foreach (Process nginxProcess in Process.GetProcessesByName(Path.GetFileNameWithoutExtension(MainConst.NginxPath))) { nginxProcess.Kill(); await nginxProcess.WaitForExitAsync(); } + + GlobalCealCleaner.Clean(); + } } private void MihomoButton_Click(object sender, RoutedEventArgs e) {