diff --git a/Consts/MainConst.cs b/Consts/MainConst.cs index 3983e26..dff3caa 100644 --- a/Consts/MainConst.cs +++ b/Consts/MainConst.cs @@ -9,7 +9,7 @@ namespace Sheas_Cealer.Consts; internal abstract partial class MainConst : MainMultilangConst { internal enum SettingsMode - { BrowserPathMode, UpstreamUrlMode, ExtraArgsMode }; + { BrowserPathMode, UpstreamUrlMode, ExtraArgsMode } public static bool IsAdmin => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator); diff --git a/Convs/MainSettingsBoxTextConv.cs b/Convs/MainSettingsBoxTextConv.cs index ae68526..d06af18 100644 --- a/Convs/MainSettingsBoxTextConv.cs +++ b/Convs/MainSettingsBoxTextConv.cs @@ -1,13 +1,14 @@ using Sheas_Cealer.Consts; using System; using System.Diagnostics; +using System.Globalization; using System.Windows.Data; namespace Sheas_Cealer.Convs; internal class MainSettingsBoxTextConv : IMultiValueConverter { - public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { MainConst.SettingsMode settingsMode = (MainConst.SettingsMode)values[0]; string browserPath = (string)values[1]; @@ -19,9 +20,9 @@ internal class MainSettingsBoxTextConv : IMultiValueConverter MainConst.SettingsMode.BrowserPathMode => browserPath, MainConst.SettingsMode.UpstreamUrlMode => upstreamUrl, MainConst.SettingsMode.ExtraArgsMode => extraArgs, - _ => throw new UnreachableException(), + _ => throw new UnreachableException() }; } - public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) => throw new NotImplementedException(); + public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException(); } \ No newline at end of file diff --git a/Utils/IconRemover.cs b/Utils/IconRemover.cs index 1ab4b1c..023855a 100644 --- a/Utils/IconRemover.cs +++ b/Utils/IconRemover.cs @@ -32,7 +32,7 @@ internal static partial class IconRemover SetWindowPos(hwnd, nint.Zero, 0, 0, 0, 0, SwpNoMove | SwpNoSize | SwpNoZOrder | SwpFrameChanged); - SendMessage(hwnd, WmSetIcon, new nint(1), nint.Zero); + SendMessage(hwnd, WmSetIcon, new(1), nint.Zero); SendMessage(hwnd, WmSetIcon, nint.Zero, nint.Zero); } } \ No newline at end of file diff --git a/Utils/NginxCleaner.cs b/Utils/NginxCleaner.cs index 4b8b1bb..4fa3bf8 100644 --- a/Utils/NginxCleaner.cs +++ b/Utils/NginxCleaner.cs @@ -5,42 +5,41 @@ using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; -namespace Sheas_Cealer.Utils +namespace Sheas_Cealer.Utils; + +internal static class NginxCleaner { - internal static class NginxCleaner + private static readonly SemaphoreSlim IsCleaningSemaphore = new(1); + + internal static async Task Clean() { - private static readonly SemaphoreSlim IsCleaningSemaphore = new(1); + if (!await IsCleaningSemaphore.WaitAsync(0)) + return; - internal static async Task Clean() + try { - if (!await IsCleaningSemaphore.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.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); - foreach (X509Certificate2 storedCert in certStore.Certificates) - if (storedCert.Subject == MainConst.NginxRootCertSubjectName) - while (true) - try - { - certStore.Remove(storedCert); + break; + } + catch { } - break; - } - catch { } - - certStore.Close(); - } - finally { IsCleaningSemaphore.Release(); } + certStore.Close(); } + finally { IsCleaningSemaphore.Release(); } } } \ No newline at end of file diff --git a/Wins/AboutWin.xaml.cs b/Wins/AboutWin.xaml.cs index 5d0b3c8..d9197d1 100644 --- a/Wins/AboutWin.xaml.cs +++ b/Wins/AboutWin.xaml.cs @@ -16,7 +16,7 @@ namespace Sheas_Cealer.Wins; public partial class AboutWin : Window { private readonly AboutPres AboutPres; - private readonly HttpClient AboutClient = new(new HttpClientHandler() { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }); + private readonly HttpClient AboutClient = new(new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }); internal AboutWin() { diff --git a/Wins/MainWin.xaml.cs b/Wins/MainWin.xaml.cs index 18b9c9e..c1a556e 100644 --- a/Wins/MainWin.xaml.cs +++ b/Wins/MainWin.xaml.cs @@ -34,7 +34,7 @@ namespace Sheas_Cealer.Wins; public partial class MainWin : Window { private readonly MainPres MainPres; - private readonly HttpClient MainClient = new(new HttpClientHandler() { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }); + private readonly HttpClient MainClient = new(new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator }); private DispatcherTimer? HoldButtonTimer; private readonly DispatcherTimer ProxyTimer = new() { Interval = TimeSpan.FromSeconds(0.1) }; private readonly FileSystemWatcher CealHostWatcher = new(Path.GetDirectoryName(MainConst.CealHostPath)!, Path.GetFileName(MainConst.CealHostPath)) { EnableRaisingEvents = true, NotifyFilter = NotifyFilters.LastWrite }; @@ -346,7 +346,7 @@ public partial class MainWin : Window if (!MainPres.IsMihomoRunning) { if (string.IsNullOrWhiteSpace(MihomoConfs)) - throw new Exception(MainConst._MihomoConfErrorMsg); + throw new(MainConst._MihomoConfErrorMsg); if (MessageBox.Show(MainConst._LaunchProxyPrompt, string.Empty, MessageBoxButton.YesNo) != MessageBoxResult.Yes) return; @@ -532,7 +532,7 @@ public partial class MainWin : Window Style newButtonStyle = new(typeof(Button), Application.Current.Resources[typeof(Button)] as Style); (Color? newForegroundColor, Color newAccentForegroundColor) = ForegroundGenerator.GetForeground(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); - newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); + newButtonStyle.Setters.Add(new Setter(ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); Application.Current.Resources[typeof(Button)] = newButtonStyle; MainPres.AccentForegroundColor = newAccentForegroundColor; diff --git a/Wins/SettingsWin.xaml.cs b/Wins/SettingsWin.xaml.cs index 2eb2711..da563ce 100644 --- a/Wins/SettingsWin.xaml.cs +++ b/Wins/SettingsWin.xaml.cs @@ -47,7 +47,7 @@ public partial class SettingsWin : Window Style newButtonStyle = new(typeof(Button), Application.Current.Resources[typeof(Button)] as Style); (Color? newForegroundColor, Color newAccentForegroundColor) = ForegroundGenerator.GetForeground(newPrimaryColor.R, newPrimaryColor.G, newPrimaryColor.B); - newButtonStyle.Setters.Add(new Setter(Button.ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); + newButtonStyle.Setters.Add(new Setter(ForegroundProperty, newForegroundColor.HasValue ? new SolidColorBrush(newForegroundColor.Value) : new DynamicResourceExtension("MaterialDesignBackground"))); Application.Current.Resources[typeof(Button)] = newButtonStyle; SettingsPres.AccentForegroundColor = newAccentForegroundColor;