diff --git a/Utils/NginxCleaner.cs b/Utils/NginxCleaner.cs index 06eb996..09ea2bc 100644 --- a/Utils/NginxCleaner.cs +++ b/Utils/NginxCleaner.cs @@ -2,44 +2,34 @@ using System; using System.IO; using System.Security.Cryptography.X509Certificates; -using System.Threading; using System.Threading.Tasks; namespace Sheas_Cealer.Utils; internal static class NginxCleaner { - internal static readonly SemaphoreSlim IsNginxCleaningSemaphore = new(1); - internal static async Task Clean() { - if (!await IsNginxCleaningSemaphore.WaitAsync(0)) - return; + string hostsContent = await File.ReadAllTextAsync(MainConst.HostsConfPath); + int hostsConfStartIndex = hostsContent.IndexOf(MainConst.HostsConfStartMarker, StringComparison.Ordinal); + int hostsConfEndIndex = hostsContent.LastIndexOf(MainConst.HostsConfEndMarker, StringComparison.Ordinal); - try - { - string hostsContent = await File.ReadAllTextAsync(MainConst.HostsConfPath); - int hostsConfStartIndex = hostsContent.IndexOf(MainConst.HostsConfStartMarker, StringComparison.Ordinal); - int hostsConfEndIndex = hostsContent.LastIndexOf(MainConst.HostsConfEndMarker, StringComparison.Ordinal); + if (hostsConfStartIndex != -1 && hostsConfEndIndex != -1) + await File.WriteAllTextAsync(MainConst.HostsConfPath, hostsContent.Remove(hostsConfStartIndex, hostsConfEndIndex - hostsConfStartIndex + MainConst.HostsConfEndMarker.Length)); - if (hostsConfStartIndex != -1 && hostsConfEndIndex != -1) - await File.WriteAllTextAsync(MainConst.HostsConfPath, hostsContent.Remove(hostsConfStartIndex, hostsConfEndIndex - hostsConfStartIndex + MainConst.HostsConfEndMarker.Length)); + using X509Store certStore = new(StoreName.Root, StoreLocation.LocalMachine, 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); - foreach (X509Certificate2 storedCert in certStore.Certificates) - if (storedCert.Subject == MainConst.NginxRootCertSubjectName) - while (true) - try - { - certStore.Remove(storedCert); + break; + } + catch { } - break; - } - catch { } - - certStore.Close(); - } - finally { IsNginxCleaningSemaphore.Release(); } + certStore.Close(); } } \ No newline at end of file diff --git a/Wins/MainWin.xaml.cs b/Wins/MainWin.xaml.cs index f04b63d..5913a3d 100644 --- a/Wins/MainWin.xaml.cs +++ b/Wins/MainWin.xaml.cs @@ -206,9 +206,6 @@ public partial class MainWin : Window if (!MainPres.IsConginxRunning && !MainPres.IsNginxRunning) { - if (NginxCleaner.IsNginxCleaningSemaphore.CurrentCount == 0 || !await IsNginxLaunchingSemaphore.WaitAsync(0)) - return; - try { if ((!MainPres.IsConginxExist && !MainPres.IsNginxExist) || @@ -238,7 +235,7 @@ public partial class MainWin : Window rootCertRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(true, false, 0, false)); using X509Certificate2 rootCert = rootCertRequest.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddYears(100)); - using X509Store certStore = new(StoreName.Root, StoreLocation.CurrentUser, OpenFlags.ReadWrite); + using X509Store certStore = new(StoreName.Root, StoreLocation.LocalMachine, OpenFlags.ReadWrite); certStore.Add(rootCert); certStore.Close();