private async void StartScan() { DisplayStatusMessage = false; StatusMessage = string.Empty; IsScanRunning = true; PreparingScan = true; // Measure the time StartTime = DateTime.Now; stopwatch.Start(); dispatcherTimer.Tick += DispatcherTimer_Tick; dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); dispatcherTimer.Start(); EndTime = null; PortScanResult.Clear(); PortsOpen = 0; cancellationTokenSource = new CancellationTokenSource(); string[] hosts = Hostname.Split(';'); List <Tuple <IPAddress, string> > hostData = new List <Tuple <IPAddress, string> >(); for (int i = 0; i < hosts.Length; i++) { string host = hosts[i].Trim(); string hostname = string.Empty; IPAddress.TryParse(host, out IPAddress ipAddress); try { // Resolve DNS // Try to resolve the hostname if (ipAddress == null) { IPHostEntry ipHostEntry = await Dns.GetHostEntryAsync(host); foreach (IPAddress ip in ipHostEntry.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork && SettingsManager.Current.PortScanner_ResolveHostnamePreferIPv4) { ipAddress = ip; continue; } else if (ip.AddressFamily == AddressFamily.InterNetworkV6 && !SettingsManager.Current.PortScanner_ResolveHostnamePreferIPv4) { ipAddress = ip; continue; } } // Fallback --> If we could not resolve our prefered ip protocol if (ipAddress == null) { foreach (IPAddress ip in ipHostEntry.AddressList) { ipAddress = ip; continue; } } hostname = host; } else { try { IPHostEntry ipHostEntry = await Dns.GetHostEntryAsync(ipAddress); hostname = ipHostEntry.HostName; } catch { } } } catch (SocketException) // This will catch DNS resolve errors { if (!string.IsNullOrEmpty(StatusMessage)) { StatusMessage += Environment.NewLine; } StatusMessage += string.Format(Application.Current.Resources["String_CouldNotResolveHostnameFor"] as string, host); DisplayStatusMessage = true; continue; } hostData.Add(Tuple.Create(ipAddress, hostname)); } if (hostData.Count == 0) { StatusMessage += Environment.NewLine + Application.Current.Resources["String_NothingToDoCheckYourInput"] as string; DisplayStatusMessage = true; ScanFinished(); return; } int[] ports = await PortRangeHelper.ConvertPortRangeToIntArrayAsync(Ports); try { PortsToScan = ports.Length * hostData.Count; PortsScanned = 0; PreparingScan = false; HostnameHistory = new List <string>(HistoryListHelper.Modify(HostnameHistory, Hostname, SettingsManager.Current.Application_HistoryListEntries)); PortsHistory = new List <string>(HistoryListHelper.Modify(PortsHistory, Ports, SettingsManager.Current.Application_HistoryListEntries)); PortScannerOptions portScannerOptions = new PortScannerOptions { Threads = SettingsManager.Current.PortScanner_Threads, ShowClosed = SettingsManager.Current.PortScanner_ShowClosed, Timeout = SettingsManager.Current.PortScanner_Timeout }; PortScanner portScanner = new PortScanner(); portScanner.PortScanned += PortScanner_PortScanned; portScanner.ScanComplete += PortScanner_ScanComplete; portScanner.ProgressChanged += PortScanner_ProgressChanged; portScanner.UserHasCanceled += PortScanner_UserHasCanceled; portScanner.ScanAsync(hostData, ports, portScannerOptions, cancellationTokenSource.Token); } catch (Exception ex) // This will catch any exception { StatusMessage = ex.Message; DisplayStatusMessage = true; ScanFinished(); } }
private async void StartScan() { DisplayStatusMessage = false; StatusMessage = string.Empty; IsScanRunning = true; PreparingScan = true; // Measure the time StartTime = DateTime.Now; stopwatch.Start(); dispatcherTimer.Tick += DispatcherTimer_Tick; dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100); dispatcherTimer.Start(); EndTime = null; PortScanResult.Clear(); PortsOpen = 0; // Change the tab title (not nice, but it works) Window window = Application.Current.Windows.OfType <Window>().FirstOrDefault(x => x.IsActive); if (window != null) { foreach (TabablzControl tabablzControl in VisualTreeHelper.FindVisualChildren <TabablzControl>(window)) { tabablzControl.Items.OfType <DragablzTabItem>().First(x => x.ID == _tabId).Header = Host; } } cancellationTokenSource = new CancellationTokenSource(); string[] hosts = Host.Split(';'); List <Tuple <IPAddress, string> > hostData = new List <Tuple <IPAddress, string> >(); for (int i = 0; i < hosts.Length; i++) { string host = hosts[i].Trim(); string hostname = string.Empty; IPAddress.TryParse(host, out IPAddress ipAddress); try { // Resolve DNS // Try to resolve the hostname if (ipAddress == null) { IPHostEntry ipHostEntry = await Dns.GetHostEntryAsync(host); foreach (IPAddress ip in ipHostEntry.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork && SettingsManager.Current.PortScanner_ResolveHostnamePreferIPv4) { ipAddress = ip; continue; } else if (ip.AddressFamily == AddressFamily.InterNetworkV6 && !SettingsManager.Current.PortScanner_ResolveHostnamePreferIPv4) { ipAddress = ip; continue; } } // Fallback --> If we could not resolve our prefered ip protocol if (ipAddress == null) { foreach (IPAddress ip in ipHostEntry.AddressList) { ipAddress = ip; continue; } } hostname = host; } else { try { IPHostEntry ipHostEntry = await Dns.GetHostEntryAsync(ipAddress); hostname = ipHostEntry.HostName; } catch { } } } catch (SocketException) // This will catch DNS resolve errors { if (!string.IsNullOrEmpty(StatusMessage)) { StatusMessage += Environment.NewLine; } StatusMessage += string.Format(LocalizationManager.GetStringByKey("String_CouldNotResolveHostnameFor"), host); DisplayStatusMessage = true; continue; } hostData.Add(Tuple.Create(ipAddress, hostname)); } if (hostData.Count == 0) { StatusMessage += Environment.NewLine + LocalizationManager.GetStringByKey("String_NothingToDoCheckYourInput"); DisplayStatusMessage = true; ScanFinished(); return; } int[] ports = await PortRangeHelper.ConvertPortRangeToIntArrayAsync(Port); try { PortsToScan = ports.Length * hostData.Count; PortsScanned = 0; PreparingScan = false; AddHostToHistory(Host); AddPortToHistory(Port); PortScannerOptions portScannerOptions = new PortScannerOptions { Threads = SettingsManager.Current.PortScanner_Threads, ShowClosed = SettingsManager.Current.PortScanner_ShowClosed, Timeout = SettingsManager.Current.PortScanner_Timeout }; PortScanner portScanner = new PortScanner(); portScanner.PortScanned += PortScanner_PortScanned; portScanner.ScanComplete += PortScanner_ScanComplete; portScanner.ProgressChanged += PortScanner_ProgressChanged; portScanner.UserHasCanceled += PortScanner_UserHasCanceled; portScanner.ScanAsync(hostData, ports, portScannerOptions, cancellationTokenSource.Token); } catch (Exception ex) // This will catch any exception { StatusMessage = ex.Message; DisplayStatusMessage = true; ScanFinished(); } }