diff --git a/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj b/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj index f7fb2d1..4e10a6a 100644 --- a/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj +++ b/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj @@ -94,6 +94,7 @@ + diff --git a/SduNetCheckTool.Core/Utils/Identifier.cs b/SduNetCheckTool.Core/Utils/Identifier.cs new file mode 100644 index 0000000..7293e46 --- /dev/null +++ b/SduNetCheckTool.Core/Utils/Identifier.cs @@ -0,0 +1,60 @@ +using System; +using System.Diagnostics; +using System.Security.Principal; + +namespace SduNetCheckTool.Core.Utils +{ + public static class Identifier + { + /// + /// IsAdministrator + /// + /// + public static bool IsAdministrator() + { + try + { + WindowsIdentity current = WindowsIdentity.GetCurrent(); + WindowsPrincipal windowsPrincipal = new(current); + //WindowsBuiltInRole可以枚举出很多权限,例如系统用户、User、Guest等等 + return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); + } + catch (Exception) + { + // log + return false; + } + } + + public static string ExePath => Process.GetCurrentProcess().MainModule.FileName ?? string.Empty; + public static string StartupPath => AppDomain.CurrentDomain.BaseDirectory; + + public static string AppendQuotes(string value) => string.IsNullOrEmpty(value) ? string.Empty : $"\"{value}\""; + + + public static void RebootAsAdmin() + { + ProcessStartInfo startInfo = new() + { + UseShellExecute = true, + Arguments = "rebootas", + WorkingDirectory = StartupPath, + FileName = AppendQuotes(ExePath), + Verb = "runas", + }; + try + { + Process.Start(startInfo); + Exit(); + } + catch { } + } + + public static void Exit() + { + //Application.Current.Shutdown(); + Environment.Exit(0); + + } + } +} diff --git a/SduNetCheckTool.GUI/Views/ToolboxTabs/DNSSwitchView.xaml b/SduNetCheckTool.GUI/Views/ToolboxTabs/DNSSwitchView.xaml index 7c3fd21..9940a14 100644 --- a/SduNetCheckTool.GUI/Views/ToolboxTabs/DNSSwitchView.xaml +++ b/SduNetCheckTool.GUI/Views/ToolboxTabs/DNSSwitchView.xaml @@ -45,7 +45,31 @@ + $"{Name} ({DNSIPAddress})"; } + public bool IsDnsSwitchEnabled {get { return _isDnsSwitchEnabled; }} + public readonly bool _isDnsSwitchEnabled = Identifier.IsAdministrator(); + + + private readonly DNSSwitchItem[] DNSList = [ new DNSSwitchItem { Name = "阿里 DNS", DNSIPAddress = "223.5.5.5" }, @@ -44,20 +49,39 @@ public Task RunDnsSwitch() { SetStatus(TaskStatusEnum.Running); + Tips = "脚本执行中"; NetworkInterface[] interfaces = [SelectedNetworkInterface]; - - Tips = SelectedDNSSwitchItem.Name switch - { + if(Identifier.IsAdministrator()) + { + Tips = SelectedDNSSwitchItem.Name switch + { "自定义" => DNSSwitch.Switch(interfaces, CustomDNS), _ => DNSSwitch.Switch(interfaces, SelectedDNSSwitchItem.DNSIPAddress), - }; - + }; + SetStatus(TaskStatusEnum.Completed); + } else + { + Tips = "错误: 没有管理员权限!\n请尝试:点击按钮以管理员权限重启"; + SetStatus(TaskStatusEnum.Error); + } - SetStatus(TaskStatusEnum.Completed); }); } + [RelayCommand] + public void RebootAsAdmin() + { + if (Identifier.IsAdministrator()) + { + Tips = "已经是管理员权限!\n可以修改DNS设置"; + } else + { + Identifier.RebootAsAdmin(); + } + + } + private void InitializeProperties() { NetworkInterfaces = NetworkInterfaceHelper.GetAvailableNetworkInterfaces; @@ -65,6 +89,13 @@ private void InitializeProperties() SelectedNetworkInterface = NetworkInterfaces.FirstOrDefault(); SelectedDNSSwitchItem = DNSSwitchItems.FirstOrDefault(); + if (IsDnsSwitchEnabled) + { + Tips = "已经是管理员权限!\n可以修改DNS设置"; + } else + { + Tips = "错误: 没有管理员权限!\n请尝试:点击按钮以管理员权限重启"; + } } public NetworkInterface[] NetworkInterfaces { get; set; }