Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
feat: 改进了更改DNS功能 (SDUQD-SNA#10)
Browse files Browse the repository at this point in the history
在DNS更改页面增加了以管理员权限重启的按钮
  • Loading branch information
Jenway authored Feb 4, 2024
1 parent 7eee3f5 commit 966b6a8
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
1 change: 1 addition & 0 deletions SduNetCheckTool.Core/SduNetCheckTool.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Compile Include="Tests\SduWebsiteTest.cs" />
<Compile Include="Tests\SystemProxyTest.cs" />
<Compile Include="Tests\TestResult.cs" />
<Compile Include="Utils\Identifier.cs" />
<Compile Include="Utils\HttpUtil.cs" />
<Compile Include="Utils\NetworkInterfaceHelper.cs" />
<Compile Include="Utils\PingTraceRoute.cs" />
Expand Down
60 changes: 60 additions & 0 deletions SduNetCheckTool.Core/Utils/Identifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Diagnostics;
using System.Security.Principal;

namespace SduNetCheckTool.Core.Utils
{
public static class Identifier
{
/// <summary>
/// IsAdministrator
/// </summary>
/// <returns></returns>
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);

}
}
}
26 changes: 25 additions & 1 deletion SduNetCheckTool.GUI/Views/ToolboxTabs/DNSSwitchView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,31 @@
<Button
Margin="5"
Command="{Binding RunDnsSwitchCommand}"
Content="修改DNS" />
Content="修改DNS">
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsDnsSwitchEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button
Margin="5"
Command="{Binding RebootAsAdminCommand}"
Content="以管理员权限重启" >
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsDnsSwitchEnabled}" Value="True">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DockPanel>
<TextBox
Grid.Row="3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public struct DNSSwitchItem
public string NameAndValue => $"{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" },
Expand All @@ -44,27 +49,53 @@ 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;
DNSSwitchItems = DNSList;
SelectedNetworkInterface = NetworkInterfaces.FirstOrDefault();
SelectedDNSSwitchItem = DNSSwitchItems.FirstOrDefault();

if (IsDnsSwitchEnabled)
{
Tips = "已经是管理员权限!\n可以修改DNS设置";
} else
{
Tips = "错误: 没有管理员权限!\n请尝试:点击按钮以管理员权限重启";
}
}

public NetworkInterface[] NetworkInterfaces { get; set; }
Expand Down

0 comments on commit 966b6a8

Please sign in to comment.