Skip to content

Commit

Permalink
Added the possibility to get DNS Cache (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpeyr committed Sep 16, 2024
1 parent df975b5 commit 48b308f
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
37 changes: 37 additions & 0 deletions InternetTest/InternetTest/Classes/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using Synethia;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.NetworkInformation;
Expand Down Expand Up @@ -635,4 +636,40 @@ public static string GetCurrentWifiSSID()
return null;
}

public static async Task<DnsCacheInfo[]> GetDnsCache()
{
// The PowerShell command to execute
string psCommand = "Get-DnsClientCache | ConvertTo-Json -Depth 4";

// Capture the JSON output asynchronously
string json = await RunPowerShellCommandAsync(psCommand);
return DnsCacheInfo.FromJson(json);
}

public static async Task<string> RunPowerShellCommandAsync(string psCommand)
{
// Create a new process to run PowerShell
ProcessStartInfo processInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-Command \"{psCommand}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

// Start the PowerShell process
using Process process = Process.Start(processInfo);

// Asynchronously read the output from the process
string output = await process.StandardOutput.ReadToEndAsync();

// Wait for the process to complete asynchronously
await process.WaitForExitAsync();

// Return the JSON output
return output;
}

}
84 changes: 83 additions & 1 deletion InternetTest/InternetTest/Pages/DnsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,89 @@
</Grid>
<StackPanel x:Name="RecordDisplayer" Grid.Row="6" />
</Grid>
<Grid x:Name="DnsCacheGrid" Grid.Row="2" />
<Grid
x:Name="DnsCacheGrid"
Grid.Row="2"
Visibility="Collapsed">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Button
x:Name="GetDnsCacheBtn"
Margin="5"
Padding="5 2"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="{DynamicResource Accent}"
BorderThickness="0"
Click="GetDnsCacheBtn_Click"
Content="{x:Static lang:Resources.GetCache}"
Cursor="Hand"
FontWeight="ExtraBold"
Foreground="{DynamicResource WindowButtonsHoverForeground1}"
Style="{DynamicResource PrimaryButton}" />
<Border
Grid.Row="1"
Background="{DynamicResource Background1}"
BorderBrush="{DynamicResource Background2}"
BorderThickness="1"
CornerRadius="10">
<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>


<Border
BorderBrush="{DynamicResource Background2}"
BorderThickness="0 0 0 1">
<Grid Margin="10 15">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock
FontSize="14"
FontWeight="Bold"
Text="{x:Static lang:Resources.Entry}" />
<TextBlock
Grid.Column="1"
FontSize="14"
FontWeight="Bold"
Text="{x:Static lang:Resources.RecordName}" />
<TextBlock
Grid.Column="2"
FontSize="14"
FontWeight="Bold"
Text="{x:Static lang:Resources.Type}" />
<TextBlock
Grid.Column="3"
FontSize="14"
FontWeight="Bold"
Text="{x:Static lang:Resources.Status}" />
<TextBlock
Grid.Column="4"
FontSize="14"
FontWeight="Bold"
Text="{x:Static lang:Resources.Data}" />
</Grid>

</Border>
<StackPanel
x:Name="ItemDisplayer"
Grid.Row="1"
Grid.ColumnSpan="5" />
</Grid>
</Border>


</Grid>
</Grid>

</Page>
15 changes: 14 additions & 1 deletion InternetTest/InternetTest/Pages/DnsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,24 @@ private void DnsInfoBtn_Checked(object sender, RoutedEventArgs e)
{
DnsInfoGrid.Visibility = Visibility.Visible;
DnsCacheGrid.Visibility = Visibility.Collapsed;
}
}

private void DnsCacheBtn_Checked(object sender, RoutedEventArgs e)
{
DnsInfoGrid.Visibility = Visibility.Collapsed;
DnsCacheGrid.Visibility = Visibility.Visible;
}

private async void GetDnsCacheBtn_Click(object sender, RoutedEventArgs e)
{
ItemDisplayer.Children.Clear();
var cache = await Global.GetDnsCache();
if (cache != null)
{
for (int i = 0; i < cache.Length; i++)
{
ItemDisplayer.Children.Add(new DnsCacheItem(cache[i]));
}
}
}
}

0 comments on commit 48b308f

Please # to comment.