@@ -22,12 +22,14 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222SOFTWARE.
2323*/
2424using DnsClient ;
25- using DnsClient . Protocol ;
2625using InternetTest . Commands ;
26+ using InternetTest . Enums ;
27+ using InternetTest . Helpers ;
2728using InternetTest . Models ;
2829using InternetTest . ViewModels . Components ;
2930using Microsoft . Win32 ;
3031using System . Collections . ObjectModel ;
32+ using System . Diagnostics ;
3133using System . IO ;
3234using System . Net ;
3335using 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
161262public class DnsRecordTabBtnViewModel ( string title , DnsToolsPageViewModel dnsToolsPage , QueryType type , bool isChecked = false ) : ViewModelBase
0 commit comments