From 2a89d484e7408df95b5ebbdac4b0c82ad8d1e6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=8C=AF=E4=BC=9F?= Date: Wed, 3 Jan 2024 09:45:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=20DNS=20=E7=9A=84=E5=8A=9F=E8=83=BD=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * demo :修改 DNS 服务器设置 * 基本完成了 DNS switch 的工作 * 增加了管理员权限请求和输入框 --- .../CustomInputTest/DNSSwitch.cs | 48 ++++ .../SduNetCheckTool.Core.csproj | 2 + .../Utils/NetworkInterfaceHelper.cs | 58 +++++ .../Common/CustomUserPerformdTask.cs | 78 +++++++ .../Common/TaskTemplateSelector.cs | 22 ++ .../Common/UserPerformedTask.cs | 14 +- SduNetCheckTool.GUI/Properties/app.manifest | 73 ++++++ .../SduNetCheckTool.GUI.csproj | 14 +- .../ViewModels/ToolBoxViewModel.cs | 4 +- SduNetCheckTool.GUI/Views/ToolBoxView.xaml | 217 +++++++++++++----- 10 files changed, 458 insertions(+), 72 deletions(-) create mode 100644 SduNetCheckTool.Core/CustomInputTest/DNSSwitch.cs create mode 100644 SduNetCheckTool.Core/Utils/NetworkInterfaceHelper.cs create mode 100644 SduNetCheckTool.GUI/Common/CustomUserPerformdTask.cs create mode 100644 SduNetCheckTool.GUI/Common/TaskTemplateSelector.cs create mode 100644 SduNetCheckTool.GUI/Properties/app.manifest diff --git a/SduNetCheckTool.Core/CustomInputTest/DNSSwitch.cs b/SduNetCheckTool.Core/CustomInputTest/DNSSwitch.cs new file mode 100644 index 0000000..303daec --- /dev/null +++ b/SduNetCheckTool.Core/CustomInputTest/DNSSwitch.cs @@ -0,0 +1,48 @@ +using System.Linq; +using System.Net.NetworkInformation; +using System.Management; +using System.Text; +using SduNetCheckTool.Core.Utils; + +namespace SduNetCheckTool.Core.CustomInputTest +{ + public class DNSSwitch : ICustomInputTest + { + + public DNSSwitch() + { } + public string Test(string input) + { + // do nothing + return ""; + } + + public string Switch(NetworkInterface[] netInterfaces, string dnsServer) + { + var data = new StringBuilder(); + + data.AppendLine("脚本执行结果:"); + + foreach (var netInterface in netInterfaces) + { + // 修改 DNS 为 dnsServer or DHCP + if (dnsServer == "DHCP") + { + NetworkInterfaceHelper.SetDnsServers(netInterface, []); + data.AppendLine($"已将 {netInterface.Name} 的 DNS 设置为 DHCP"); + } + else + { + NetworkInterfaceHelper.SetDnsServers(netInterface, [dnsServer]); + data.AppendLine($"已将 {netInterface.Name} 的 DNS 设置为 {dnsServer}"); + } + } + + data.AppendLine("结束"); + + return data.ToString(); + } + + + } +} diff --git a/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj b/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj index b5ec8af..f59fcd4 100644 --- a/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj +++ b/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj @@ -80,6 +80,7 @@ + @@ -95,6 +96,7 @@ + diff --git a/SduNetCheckTool.Core/Utils/NetworkInterfaceHelper.cs b/SduNetCheckTool.Core/Utils/NetworkInterfaceHelper.cs new file mode 100644 index 0000000..250e9d8 --- /dev/null +++ b/SduNetCheckTool.Core/Utils/NetworkInterfaceHelper.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Management; +using System.Net.NetworkInformation; + +namespace SduNetCheckTool.Core.Utils +{ + public static class NetworkInterfaceHelper + { + public static NetworkInterface[] GetAvailableNetworkInterfaces => NetworkInterface + .GetAllNetworkInterfaces() + .Where(n => n.OperationalStatus == OperationalStatus.Up) + .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback) + .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Tunnel) + .ToArray(); + + + public static void SetDnsServers(NetworkInterface netInterface, string[] dnsServers) + { + var managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration"); + var managementObjects = managementClass.GetInstances(); + + foreach (var managementObject in managementObjects.Cast() + .Where(mo => (bool)mo["IPEnabled"] && mo["Description"].ToString() == netInterface.Description)) + { + var inParams = managementObject.GetMethodParameters("SetDNSServerSearchOrder"); + inParams["DNSServerSearchOrder"] = dnsServers; + managementObject.InvokeMethod("SetDNSServerSearchOrder", inParams, null); + } + } + + public static void SetDnsServersUsingNetsh(NetworkInterface netInterface, string[] dnsServers) + { + string dnsServersString = string.Join(" ", dnsServers); + string command; + if (dnsServers.Length == 0) + { + command = $"interface ipv4 set dns \"{netInterface.Name}\" source=dhcp"; + } + else + { + command = $"interface ipv4 set dns \"{netInterface.Name}\" static {dnsServersString}"; + } + // netsh interface ipv4 set dns name=3 static 8.8.8.8 + + Process process = new(); + process.StartInfo.FileName = "netsh"; + process.StartInfo.Arguments = command; + process.StartInfo.CreateNoWindow = false; + process.StartInfo.UseShellExecute = false; + process.Start(); + process.WaitForExit(); + + } + + } +} diff --git a/SduNetCheckTool.GUI/Common/CustomUserPerformdTask.cs b/SduNetCheckTool.GUI/Common/CustomUserPerformdTask.cs new file mode 100644 index 0000000..81b5bf4 --- /dev/null +++ b/SduNetCheckTool.GUI/Common/CustomUserPerformdTask.cs @@ -0,0 +1,78 @@ +using CommunityToolkit.Mvvm.Input; +using SduNetCheckTool.Core.CustomInputTest; +using SduNetCheckTool.Core.Utils; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Threading.Tasks; + +namespace SduNetCheckTool.GUI.Common +{ + public class CustomUserPerformdTask : UserPerformedTask + { + public class DNSSwitchItem + { + public string Name { get; set; } + public string Value { get; set; } + + public string NameAndValue => $"{Name} ({Value})"; + } + public readonly DNSSwitchItem[] DNSList = new[] + { + new DNSSwitchItem { Name = "阿里 DNS", Value = "223.5.5.5" }, + new DNSSwitchItem { Name = "腾讯 DNS", Value = "119.29.29.29" }, + new DNSSwitchItem { Name = "114 DNS", Value = "114.114.114.114" }, + new DNSSwitchItem { Name = "谷歌 DNS", Value = "8.8.8.8" }, + new DNSSwitchItem { Name = "自动分配DNS", Value = "DHCP" }, + // 添加更多的项,如果需要 + new DNSSwitchItem { Name = "自定义", Value = string.Empty } + }; + private new readonly DNSSwitch _test; + public CustomUserPerformdTask(DNSSwitch test, string name) : base(test, name) + { + InitializeProperties(); + _test = test; + RunCommand = new AsyncRelayCommand(RunTask); + } + + new public Task RunTask(string input) + { + return Task.Run(() => + { + TaskStatusEnum = TaskStatusEnum.Running; + NetworkInterface[] interfaces = new NetworkInterface[] { SelectedNetworkInterface }; + switch (SelectedDNSSwitchItem.Name) + { + case "自定义": + Tips = _test.Switch(interfaces, CustomDNS); + break; + default: + Tips = _test.Switch(interfaces, SelectedDNSSwitchItem.Value); + break; + } + TaskStatusEnum = TaskStatusEnum.Completed; + }); + } + + private void InitializeProperties() + { + NetworkInterfaces = NetworkInterfaceHelper.GetAvailableNetworkInterfaces; + DNSSwitchItems = DNSList; + SelectedNetworkInterface = NetworkInterfaces.FirstOrDefault(); + SelectedDNSSwitchItem = DNSSwitchItems.FirstOrDefault(); + + } + + public NetworkInterface[] NetworkInterfaces { get; set; } + public NetworkInterface SelectedNetworkInterface { get; set; } + + public DNSSwitchItem[] DNSSwitchItems { get; set; } + + public DNSSwitchItem SelectedDNSSwitchItem { get; set; } + + public string CustomDNS { get; set; } + + } + + +} diff --git a/SduNetCheckTool.GUI/Common/TaskTemplateSelector.cs b/SduNetCheckTool.GUI/Common/TaskTemplateSelector.cs new file mode 100644 index 0000000..77eae4c --- /dev/null +++ b/SduNetCheckTool.GUI/Common/TaskTemplateSelector.cs @@ -0,0 +1,22 @@ +using SduNetCheckTool.Core.CustomInputTest; +using System.Windows; +using System.Windows.Controls; + +namespace SduNetCheckTool.GUI.Common +{ + public class TaskTemplateSelector : DataTemplateSelector + { + public DataTemplate DefaultTemplate { get; set; } + public DataTemplate InternetTestTemplate { get; set; } + public DataTemplate DNSSwitchTemplate { get; set; } + + public override DataTemplate SelectTemplate(object item, DependencyObject container) + { + if (item is CustomUserPerformdTask) { + return DNSSwitchTemplate; + } + + return DefaultTemplate; + } + } +} diff --git a/SduNetCheckTool.GUI/Common/UserPerformedTask.cs b/SduNetCheckTool.GUI/Common/UserPerformedTask.cs index 45ef0f1..eaf9faf 100644 --- a/SduNetCheckTool.GUI/Common/UserPerformedTask.cs +++ b/SduNetCheckTool.GUI/Common/UserPerformedTask.cs @@ -15,7 +15,7 @@ public UserPerformedTask(ICustomInputTest test, string name) RunCommand = new AsyncRelayCommand(RunTask); } - public Task RunTask(string input) + public virtual Task RunTask(string input) { return Task.Run(() => { @@ -25,17 +25,17 @@ public Task RunTask(string input) }); } - public ICommand RunCommand { get; private set; } + public ICommand RunCommand { get; protected set; } /// /// 测试类 /// - private readonly ICustomInputTest _test; + protected ICustomInputTest _test; /// /// 任务名字 /// - private string _name; + protected string _name; public string Name { @@ -46,7 +46,7 @@ public string Name /// /// 完成状态 /// - private TaskStatusEnum _taskStatusEnum = TaskStatusEnum.Waiting; + protected TaskStatusEnum _taskStatusEnum = TaskStatusEnum.Waiting; public TaskStatusEnum TaskStatusEnum { @@ -57,7 +57,7 @@ public TaskStatusEnum TaskStatusEnum /// /// 返回的提示 /// - private string _tips = "任务未完成"; + protected string _tips = "任务未完成"; public string Tips { @@ -71,6 +71,6 @@ public string InputText set => SetProperty(ref _inputText, value); } - private string _inputText = string.Empty; + protected string _inputText = string.Empty; } } diff --git a/SduNetCheckTool.GUI/Properties/app.manifest b/SduNetCheckTool.GUI/Properties/app.manifest new file mode 100644 index 0000000..5a95963 --- /dev/null +++ b/SduNetCheckTool.GUI/Properties/app.manifest @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SduNetCheckTool.GUI/SduNetCheckTool.GUI.csproj b/SduNetCheckTool.GUI/SduNetCheckTool.GUI.csproj index 8ec33a1..98e16c3 100644 --- a/SduNetCheckTool.GUI/SduNetCheckTool.GUI.csproj +++ b/SduNetCheckTool.GUI/SduNetCheckTool.GUI.csproj @@ -1,6 +1,7 @@  - + Debug AnyCPU @@ -57,7 +58,7 @@ SduNetCheckTool.GUI_TemporaryKey.pfx - true + false false @@ -83,6 +84,12 @@ prompt true + + Properties\app.manifest + + + LocalIntranet + @@ -104,6 +111,8 @@ MSBuild:Compile Designer + + @@ -173,6 +182,7 @@ Resources.Designer.cs + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/SduNetCheckTool.GUI/ViewModels/ToolBoxViewModel.cs b/SduNetCheckTool.GUI/ViewModels/ToolBoxViewModel.cs index 8176ed2..6165f19 100644 --- a/SduNetCheckTool.GUI/ViewModels/ToolBoxViewModel.cs +++ b/SduNetCheckTool.GUI/ViewModels/ToolBoxViewModel.cs @@ -2,6 +2,7 @@ using SduNetCheckTool.Core.CustomInputTest; using SduNetCheckTool.GUI.Common; using System.Collections.ObjectModel; +using System.Net.NetworkInformation; namespace SduNetCheckTool.GUI.ViewModels { @@ -16,7 +17,8 @@ private void Init() { Tasks = new ObservableCollection() { - new UserPerformedTask(new InternetTest(),"指定网站Ping && 路由测试") + new UserPerformedTask(new InternetTest(),"指定网站Ping && 路由测试"), + new CustomUserPerformdTask(new DNSSwitch(),"DNS切换"), }; } diff --git a/SduNetCheckTool.GUI/Views/ToolBoxView.xaml b/SduNetCheckTool.GUI/Views/ToolBoxView.xaml index 06e7a45..8bc6275 100644 --- a/SduNetCheckTool.GUI/Views/ToolBoxView.xaml +++ b/SduNetCheckTool.GUI/Views/ToolBoxView.xaml @@ -16,71 +16,164 @@ - - - - - - - - - - - - - - - - - - - - - - - -