|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
2 | 4 | using System.Linq; |
3 | 5 | using System.Net; |
4 | 6 | using System.Net.Sockets; |
@@ -44,66 +46,69 @@ protected virtual void OnUserHasCanceled() |
44 | 46 | #endregion |
45 | 47 |
|
46 | 48 | #region Methods |
47 | | - public void ScanAsync(IPAddress ipAddress, int[] ports, PortScannerOptions portScannerOptions, CancellationToken cancellationToken) |
| 49 | + public void ScanAsync(List<Tuple<IPAddress, string>> hostData, int[] ports, PortScannerOptions portScannerOptions, CancellationToken cancellationToken) |
48 | 50 | { |
49 | 51 | progressValue = 0; |
50 | 52 |
|
51 | 53 | // Modify the ThreadPool for better performance |
52 | | - |
53 | 54 | ThreadPool.GetMinThreads(out int workerThreads, out int completionPortThreads); |
54 | 55 | ThreadPool.SetMinThreads(workerThreads + portScannerOptions.Threads, completionPortThreads + portScannerOptions.Threads); |
55 | 56 |
|
56 | 57 | Task.Run(() => |
57 | 58 | { |
58 | | - try |
| 59 | + foreach (Tuple<IPAddress, string> host in hostData) |
59 | 60 | { |
60 | | - ParallelOptions parallelOptions = new ParallelOptions() |
61 | | - { |
62 | | - CancellationToken = cancellationToken, |
63 | | - MaxDegreeOfParallelism = portScannerOptions.Threads |
64 | | - }; |
65 | | - |
66 | | - // foreach ip, Parallel.ForEach port... |
67 | | - Parallel.ForEach(ports, parallelOptions, port => |
| 61 | + try |
68 | 62 | { |
69 | | - // Test if port is open |
70 | | - using (TcpClient tcpClient = ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? new TcpClient(AddressFamily.InterNetworkV6) : new TcpClient(AddressFamily.InterNetwork)) |
| 63 | + ParallelOptions parallelOptions = new ParallelOptions() |
71 | 64 | { |
72 | | - IAsyncResult tcpClientConnection = tcpClient.BeginConnect(ipAddress, port, null, null); |
| 65 | + CancellationToken = cancellationToken, |
| 66 | + MaxDegreeOfParallelism = portScannerOptions.Threads |
| 67 | + }; |
73 | 68 |
|
74 | | - if (tcpClientConnection.AsyncWaitHandle.WaitOne(portScannerOptions.Timeout, false)) |
| 69 | + // foreach ip, Parallel.ForEach port... |
| 70 | + Parallel.ForEach(ports, parallelOptions, port => |
| 71 | + { |
| 72 | + // Test if port is open |
| 73 | + using (TcpClient tcpClient = host.Item1.AddressFamily == AddressFamily.InterNetworkV6 ? new TcpClient(AddressFamily.InterNetworkV6) : new TcpClient(AddressFamily.InterNetwork)) |
75 | 74 | { |
76 | | - try |
77 | | - { |
78 | | - tcpClient.EndConnect(tcpClientConnection); |
| 75 | + IAsyncResult tcpClientConnection = tcpClient.BeginConnect(host.Item1, port, null, null); |
79 | 76 |
|
80 | | - OnPortScanned(new PortScannedArgs(ipAddress, port, PortLookup.Lookup(port).FirstOrDefault(x => x.Protocol == PortLookup.Protocol.tcp), PortInfo.PortStatus.Open)); |
| 77 | + if (tcpClientConnection.AsyncWaitHandle.WaitOne(portScannerOptions.Timeout, false)) |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + tcpClient.EndConnect(tcpClientConnection); |
| 82 | + |
| 83 | + OnPortScanned(new PortScannedArgs(host, port, PortLookup.Lookup(port).FirstOrDefault(x => x.Protocol == PortLookup.Protocol.tcp), PortInfo.PortStatus.Open)); |
| 84 | + } |
| 85 | + catch |
| 86 | + { |
| 87 | + if (portScannerOptions.ShowClosed) |
| 88 | + OnPortScanned(new PortScannedArgs(host, port, PortLookup.Lookup(port).FirstOrDefault(x => x.Protocol == PortLookup.Protocol.tcp), PortInfo.PortStatus.Closed)); |
| 89 | + } |
81 | 90 | } |
82 | | - catch |
| 91 | + else |
83 | 92 | { |
84 | 93 | if (portScannerOptions.ShowClosed) |
85 | | - OnPortScanned(new PortScannedArgs(ipAddress, port, PortLookup.Lookup(port).FirstOrDefault(x => x.Protocol == PortLookup.Protocol.tcp), PortInfo.PortStatus.Closed)); |
| 94 | + OnPortScanned(new PortScannedArgs(host, port, PortLookup.Lookup(port).FirstOrDefault(x => x.Protocol == PortLookup.Protocol.tcp), PortInfo.PortStatus.Closed)); |
86 | 95 | } |
87 | 96 | } |
88 | | - else |
89 | | - { |
90 | | - if (portScannerOptions.ShowClosed) |
91 | | - OnPortScanned(new PortScannedArgs(ipAddress, port, PortLookup.Lookup(port).FirstOrDefault(x => x.Protocol == PortLookup.Protocol.tcp), PortInfo.PortStatus.Closed)); |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - // Increase the progress |
96 | | - Interlocked.Increment(ref progressValue); |
97 | | - OnProgressChanged(); |
98 | | - }); |
99 | 97 |
|
100 | | - OnScanComplete(); |
101 | | - } |
102 | | - catch (OperationCanceledException) // If user has canceled |
103 | | - { |
104 | | - OnUserHasCanceled(); |
| 98 | + // Increase the progress |
| 99 | + Interlocked.Increment(ref progressValue); |
| 100 | + OnProgressChanged(); |
| 101 | + }); |
| 102 | + } |
| 103 | + catch (OperationCanceledException) // If user has canceled |
| 104 | + { |
| 105 | + OnUserHasCanceled(); |
| 106 | + break; |
| 107 | + } |
105 | 108 | } |
106 | | - }); |
| 109 | + |
| 110 | + OnScanComplete(); |
| 111 | + }); |
107 | 112 |
|
108 | 113 | // Reset the ThreadPool to defaul |
109 | 114 | ThreadPool.SetMinThreads(workerThreads, completionPortThreads); |
|
0 commit comments