1.1.3 -> 1.1.4 第42次更新

This commit is contained in:
Space Time 2024-12-10 16:06:48 +08:00
parent 4009e8ce63
commit 42bd83f4d3

View File

@ -1,42 +1,43 @@
using System.IO; using System.IO;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading;
using Sheas_Cealer.Consts; using Sheas_Cealer.Consts;
namespace Sheas_Cealer.Utils namespace Sheas_Cealer.Utils
{ {
internal static class NginxStoppedCleaner internal static class NginxStoppedCleaner
{ {
private static bool IsCleaning = false; private static readonly object IsCleaningLock = new();
internal static void Clean() internal static void Clean()
{ {
if (IsCleaning) if (!Monitor.TryEnter(IsCleaningLock))
return; 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); if (hostsConfStartIndex != -1 && hostsConfEndIndex != -1)
int hostsConfStartIndex = hostsContent.IndexOf(MainConst.HostsConfStartMarker); File.WriteAllText(MainConst.HostsConfPath, hostsContent.Remove(hostsConfStartIndex, hostsConfEndIndex - hostsConfStartIndex + MainConst.HostsConfEndMarker.Length));
int hostsConfEndIndex = hostsContent.LastIndexOf(MainConst.HostsConfEndMarker);
if (hostsConfStartIndex != -1 && hostsConfEndIndex != -1) using X509Store certStore = new(StoreName.Root, StoreLocation.CurrentUser, OpenFlags.ReadWrite);
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 { }
foreach (X509Certificate2 storedCert in certStore.Certificates) certStore.Close();
if (storedCert.Subject == MainConst.NginxRootCertSubjectName) }
while (true) finally { Monitor.Exit(IsCleaningLock); }
try
{
certStore.Remove(storedCert);
break;
}
catch { }
certStore.Close();
IsCleaning = false;
} }
} }
} }