Skip to content

Commit c85e5fe

Browse files
committed
Added DNS Cache section (#679)
1 parent 27645ef commit c85e5fe

File tree

5 files changed

+373
-1
lines changed

5 files changed

+373
-1
lines changed

InternetTest/InternetTest/Helpers/Context.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
*/
2424
using InternetTest.Enums;
2525
using PeyrSharp.Env;
26+
using System.Diagnostics;
2627

2728
namespace InternetTest.Helpers;
2829

@@ -48,4 +49,32 @@ public static void ChangeLanguage(Language language)
4849
_ => Thread.CurrentThread.CurrentUICulture
4950
};
5051
}
52+
53+
54+
public static async Task<string> RunPowerShellCommandAsync(string psCommand)
55+
{
56+
// Create a new process to run PowerShell
57+
ProcessStartInfo processInfo = new()
58+
{
59+
FileName = "powershell.exe",
60+
Arguments = $"-Command \"{psCommand}\"",
61+
RedirectStandardOutput = true,
62+
RedirectStandardError = true,
63+
UseShellExecute = false,
64+
CreateNoWindow = true
65+
};
66+
67+
// Start the PowerShell process
68+
using Process? process = Process.Start(processInfo);
69+
70+
// Asynchronously read the output from the process
71+
if (process == null) return string.Empty;
72+
string output = await process.StandardOutput.ReadToEndAsync();
73+
74+
// Wait for the process to complete asynchronously
75+
await process.WaitForExitAsync();
76+
77+
// Return the JSON output
78+
return output;
79+
}
5180
}

InternetTest/InternetTest/Themes/Dark.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:local="clr-namespace:InternetTest.Themes">
55

66
<SolidColorBrush x:Key="Background1" Color="#0A0A1E" />
7+
<SolidColorBrush x:Key="HoverBackground" Color="#0A0A1E" />
78
<SolidColorBrush x:Key="CardBackground" Color="#FF141428" />
89
<SolidColorBrush x:Key="SidebarBackground" Color="#3F141428" />
910
<SolidColorBrush x:Key="Foreground1" Color="#FFFFFF" />

InternetTest/InternetTest/Themes/Light.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:local="clr-namespace:InternetTest.Themes">
55

66
<SolidColorBrush x:Key="Background1" Color="#ffffff" />
7+
<SolidColorBrush x:Key="HoverBackground" Color="#e6e6e6" />
78
<SolidColorBrush x:Key="CardBackground" Color="#ffffff" />
89
<SolidColorBrush x:Key="SidebarBackground" Color="#ccffffff" />
910
<SolidColorBrush x:Key="Foreground1" Color="#000000" />

InternetTest/InternetTest/ViewModels/DnsToolsPageViewModel.cs

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
*/
2424
using DnsClient;
25-
using DnsClient.Protocol;
2625
using InternetTest.Commands;
26+
using InternetTest.Enums;
27+
using InternetTest.Helpers;
2728
using InternetTest.Models;
2829
using InternetTest.ViewModels.Components;
2930
using Microsoft.Win32;
3031
using System.Collections.ObjectModel;
32+
using System.Diagnostics;
3133
using System.IO;
3234
using System.Net;
3335
using System.Windows;
@@ -43,6 +45,9 @@ public class DnsToolsPageViewModel : ViewModelBase
4345
private ObservableCollection<DnsItemViewModel> _dnsRecordsItems = [];
4446
public ObservableCollection<DnsItemViewModel> DnsRecordsItems { get => _dnsRecordsItems; set { _dnsRecordsItems = value; OnPropertyChanged(nameof(DnsRecordsItems)); } }
4547

48+
private ObservableCollection<DnsCacheItemViewModel> _dnsCacheItems = [];
49+
public ObservableCollection<DnsCacheItemViewModel> DnsCacheItems { get => _dnsCacheItems; set { _dnsCacheItems = value; OnPropertyChanged(nameof(DnsCacheItems)); } }
50+
4651
private string _query = string.Empty;
4752
public string Query { get => _query; set { _query = value; OnPropertyChanged(nameof(Query)); } }
4853

@@ -64,7 +69,89 @@ public class DnsToolsPageViewModel : ViewModelBase
6469
private string _status = string.Empty;
6570
public string Status { get => _status; set { _status = value; OnPropertyChanged(nameof(Status)); } }
6671

72+
private string _success = string.Empty;
73+
public string Success { get => _success; set { _success = value; OnPropertyChanged(nameof(Success)); } }
74+
75+
private string _error = string.Empty;
76+
public string Error { get => _error; set { _error = value; OnPropertyChanged(nameof(Error)); } }
77+
78+
private string _info = string.Empty;
79+
public string Info { get => _info; set { _info = value; OnPropertyChanged(nameof(Info)); } }
80+
81+
private string _tooltipContent = string.Empty;
82+
public string TooltipContent { get => _tooltipContent; set { _tooltipContent = value; OnPropertyChanged(nameof(TooltipContent)); } }
83+
6784
private string _csv = string.Empty;
85+
86+
public ICommand GetDnsCacheCommand => new RelayCommand(async o =>
87+
{
88+
try
89+
{
90+
var cache = await GetDnsCache();
91+
DnsCacheItems = [.. cache.Select(x => new DnsCacheItemViewModel(x))];
92+
93+
var status = new Dictionary<Status, int>();
94+
if (cache != null)
95+
{
96+
for (int i = 0; i < cache.Length; i++)
97+
{
98+
status[(Status)cache[i].Status] = status.ContainsKey((Status)cache[i].Status) ? status[(Status)cache[i].Status] + 1 : 1;
99+
}
100+
}
101+
102+
Success = status.TryGetValue(Enums.Status.Success, out int value) ? value.ToString() : "0";
103+
Error = (cache?.Length - value ?? 0).ToString();
104+
Info = cache?.Length.ToString() ?? "0";
105+
106+
foreach (var pair in status)
107+
{
108+
TooltipContent += $"{pair.Key}: {pair.Value}\n";
109+
}
110+
TooltipContent = TooltipContent.TrimEnd('\n');
111+
112+
CacheLoaded = true;
113+
}
114+
catch (Exception ex)
115+
{
116+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
117+
CacheLoaded = false;
118+
}
119+
});
120+
121+
public ICommand FlushCommand => new RelayCommand(o =>
122+
{
123+
if (MessageBox.Show(Properties.Resources.FlushDNSMessage, Properties.Resources.FlushDNS, MessageBoxButton.YesNoCancel, MessageBoxImage.Question) != MessageBoxResult.Yes)
124+
return;
125+
try
126+
{
127+
ProcessStartInfo processInfo = new()
128+
{
129+
FileName = "ipconfig",
130+
Arguments = "/flushdns",
131+
RedirectStandardOutput = true,
132+
RedirectStandardError = true,
133+
UseShellExecute = false,
134+
CreateNoWindow = true
135+
};
136+
137+
using Process? process = Process.Start(processInfo);
138+
if (process is null) return;
139+
// Read the output from the command
140+
string output = process.StandardOutput.ReadToEnd();
141+
string error = process.StandardError.ReadToEnd();
142+
143+
// Wait for the process to exit
144+
process.WaitForExit();
145+
MessageBox.Show(Properties.Resources.FlushDNSSuccess, Properties.Resources.FlushDNS, MessageBoxButton.OK, MessageBoxImage.Information);
146+
147+
}
148+
catch (Exception ex)
149+
{
150+
MessageBox.Show(ex.Message, Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
151+
}
152+
CacheLoaded = false;
153+
});
154+
68155
public ICommand GetDnsInfoCommand => new RelayCommand(async o =>
69156
{
70157
try
@@ -106,6 +193,9 @@ public class DnsToolsPageViewModel : ViewModelBase
106193

107194
public bool PlaceholderVis => !IsRefreshing && !HasInfo;
108195

196+
private bool _cacheLoaded = false;
197+
public bool CacheLoaded { get => _cacheLoaded; set { _cacheLoaded = value; OnPropertyChanged(nameof(CacheLoaded)); } }
198+
109199
public List<DnsRecord> DnsRecords { get; set; } = [];
110200
public DnsToolsPageViewModel()
111201
{
@@ -156,6 +246,17 @@ private async Task GetDnsRecords()
156246

157247
DnsRecordsItems = [.. DnsRecords.Select(x => new DnsItemViewModel(x))];
158248
}
249+
250+
public static async Task<DnsCacheInfo[]> GetDnsCache()
251+
{
252+
// The PowerShell command to execute
253+
string psCommand = "Get-DnsClientCache | ConvertTo-Json -Depth 4";
254+
255+
// Capture the JSON output asynchronously
256+
string json = await Context.RunPowerShellCommandAsync(psCommand);
257+
return DnsCacheInfo.FromJson(json);
258+
}
259+
159260
}
160261

161262
public class DnsRecordTabBtnViewModel(string title, DnsToolsPageViewModel dnsToolsPage, QueryType type, bool isChecked = false) : ViewModelBase

0 commit comments

Comments
 (0)