From 42bd83f4d3924b1b53a9859d2e20fdda1031accd Mon Sep 17 00:00:00 2001 From: Space Time Date: Tue, 10 Dec 2024 16:06:48 +0800 Subject: [PATCH] =?UTF-8?q?1.1.3=20->=201.1.4=20=E7=AC=AC42=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/NginxStoppedCleaner.cs | 45 ++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Utils/NginxStoppedCleaner.cs b/Utils/NginxStoppedCleaner.cs index 8503040..dc33356 100644 --- a/Utils/NginxStoppedCleaner.cs +++ b/Utils/NginxStoppedCleaner.cs @@ -1,42 +1,43 @@ using System.IO; using System.Security.Cryptography.X509Certificates; +using System.Threading; using Sheas_Cealer.Consts; namespace Sheas_Cealer.Utils { internal static class NginxStoppedCleaner { - private static bool IsCleaning = false; + private static readonly object IsCleaningLock = new(); internal static void Clean() { - if (IsCleaning) + if (!Monitor.TryEnter(IsCleaningLock)) return; - IsCleaning = true; + try + { + string hostsContent = File.ReadAllText(MainConst.HostsConfPath); + int hostsConfStartIndex = hostsContent.IndexOf(MainConst.HostsConfStartMarker); + int hostsConfEndIndex = hostsContent.LastIndexOf(MainConst.HostsConfEndMarker); - 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)); - 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); - 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 { } - foreach (X509Certificate2 storedCert in certStore.Certificates) - if (storedCert.Subject == MainConst.NginxRootCertSubjectName) - while (true) - try - { - certStore.Remove(storedCert); - break; - } - catch { } - - certStore.Close(); - - IsCleaning = false; + certStore.Close(); + } + finally { Monitor.Exit(IsCleaningLock); } } } } \ No newline at end of file