Repository navigation
Expand file tree
/
Copy pathPortScanner.cs
More file actions
154 lines (121 loc) · 4.66 KB
/
Copy pathPortScanner.cs
File metadata and controls
154 lines (121 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using NETworkManager.Models.Lookup;
using NETworkManager.Utilities;
namespace NETworkManager.Models.Network;
public sealed class PortScanner
{
#region Constructor
public PortScanner(PortScannerOptions options)
{
_options = options;
}
#endregion
#region Variables
private int _progressValue;
private readonly InterlockedCounter _portsOpen = new();
private readonly InterlockedCounter _portsClosed = new();
private readonly PortScannerOptions _options;
/// <summary>
/// Gets the number of ports found to be open so far. Thread-safe; may be read from any
/// thread while the scan is running.
/// </summary>
public int PortsOpen => _portsOpen.Value;
/// <summary>
/// Gets the number of ports found to be closed (or timed out) so far. Thread-safe; may be
/// read from any thread while the scan is running.
/// </summary>
public int PortsClosed => _portsClosed.Value;
#endregion
#region Events
public event EventHandler<PortScannerPortScannedArgs> PortScanned;
private void OnPortScanned(PortScannerPortScannedArgs e)
{
PortScanned?.Invoke(this, e);
}
public event EventHandler ScanComplete;
private void OnScanComplete()
{
ScanComplete?.Invoke(this, EventArgs.Empty);
}
public event EventHandler<ProgressChangedArgs> ProgressChanged;
private void OnProgressChanged()
{
ProgressChanged?.Invoke(this, new ProgressChangedArgs(_progressValue));
}
public event EventHandler UserHasCanceled;
private void OnUserHasCanceled()
{
UserHasCanceled?.Invoke(this, EventArgs.Empty);
}
#endregion
#region Methods
public void ScanAsync(IEnumerable<(IPAddress ipAddress, string hostname)> hosts, IEnumerable<int> ports,
CancellationToken cancellationToken)
{
_progressValue = 0;
Task.Run(async () =>
{
try
{
var hostParallelOptions = new ParallelOptions
{
CancellationToken = cancellationToken,
MaxDegreeOfParallelism = _options.MaxHostThreads
};
var portParallelOptions = new ParallelOptions
{
CancellationToken = cancellationToken,
MaxDegreeOfParallelism = _options.MaxPortThreads
};
await Parallel.ForEachAsync(hosts, hostParallelOptions, async (host, hostCt) =>
{
// Resolve Hostname (PTR)
var hostname = string.Empty;
if (_options.ResolveHostname)
{
var dnsResult = await DNSClient.GetInstance().ResolvePtrAsync(host.ipAddress)
.WaitAsync(hostCt).ConfigureAwait(false);
if (!dnsResult.HasError)
hostname = dnsResult.Value;
}
// Check each port
await Parallel.ForEachAsync(ports, portParallelOptions, async (port, portCt) =>
{
var portState = await PortProbe.ProbeAsync(host.ipAddress, port, _options.Timeout, portCt)
.ConfigureAwait(false);
// Count open/closed ports unconditionally, since ShowAllResults (below)
// may prevent closed ports from ever reaching PortScanned
if (portState == PortState.Open)
_portsOpen.Increment();
else
_portsClosed.Increment();
if (_options.ShowAllResults || portState == PortState.Open)
OnPortScanned(new PortScannerPortScannedArgs(
new PortScannerPortInfo(host.ipAddress, hostname, port,
PortLookup.LookupByPortAndProtocol(port), portState)));
IncreaseProgress();
}).ConfigureAwait(false);
}).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
OnUserHasCanceled();
}
finally
{
OnScanComplete();
}
}, cancellationToken);
}
private void IncreaseProgress()
{
// Increase the progress
Interlocked.Increment(ref _progressValue);
OnProgressChanged();
}
#endregion
}