Skip to content

Commit 8d47092

Browse files
dearmbclaude
andcommitted
feat: support paired host:port scanning in Port Scanner
A host can now carry its own ports (e.g. 10.0.0.1:80,443-445 or 10.0.0.1:80-90) and is scanned only for those ports, while regular hosts keep scanning the ports of the Ports field. Both can be mixed in a single scan; the Ports field can be left empty when every host carries its own ports. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 67aef5c commit 8d47092

7 files changed

Lines changed: 80 additions & 10 deletions

File tree

‎Source/NETworkManager.Models/Network/PortScanner.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void OnUserHasCanceled()
6161

6262
#region Methods
6363

64-
public void ScanAsync(IEnumerable<(IPAddress ipAddress, string hostname)> hosts, IEnumerable<int> ports,
64+
public void ScanAsync(IEnumerable<(IPAddress ipAddress, string hostname, int[] ports)> hosts,
6565
CancellationToken cancellationToken)
6666
{
6767
_progressValue = 0;
@@ -97,7 +97,7 @@ await Parallel.ForEachAsync(hosts, hostParallelOptions, async (host, hostCt) =>
9797
}
9898

9999
// Check each port
100-
await Parallel.ForEachAsync(ports, portParallelOptions, async (port, portCt) =>
100+
await Parallel.ForEachAsync(host.ports, portParallelOptions, async (port, portCt) =>
101101
{
102102
var portState = await PortProbe.ProbeAsync(host.ipAddress, port, _options.Timeout, portCt)
103103
.ConfigureAwait(false);

‎Source/NETworkManager.Utilities/RegexHelper.cs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,25 @@ public static partial class RegexHelper
6767
[GeneratedRegex($"^{IPv4AddressShortRangeValues}$")]
6868
public static partial Regex IPv4AddressShortRangeRegex();
6969

70+
/// <summary>
71+
/// Represents a regular expression pattern that matches an IPv4 address followed by one or more ports like
72+
/// "192.168.178.1:80" (single port), "192.168.178.1:80-90" (port range) or "192.168.178.1:80,443-445"
73+
/// (multiple ports / ranges). Used by the Port Scanner to scan a specific host only for its own ports.
74+
/// </summary>
75+
private const string IPv4AddressWithPortValues =
76+
$@"{IPv4AddressValues}:({PortValues})(-{PortValues})?(,({PortValues})(-{PortValues})?)*";
77+
78+
/// <summary>
79+
/// Provides a compiled regular expression that matches an IPv4 address with ports like
80+
/// "192.168.178.1:80", "192.168.178.1:80-90" or "192.168.178.1:80,443-445".
81+
/// </summary>
82+
/// <returns>A <see cref="Regex"/> instance that matches IPv4 addresses with one or more ports.</returns>
83+
[GeneratedRegex($"^{IPv4AddressWithPortValues}$")]
84+
public static partial Regex IPv4AddressWithPortRegex();
85+
7086
/// <summary>
7187
/// Provides a compiled regular expression that matches valid IPv4 subnet mask like "255.255.0.0".
72-
/// </summary>
88+
/// </summary>
7389
/// <returns>A <see cref="Regex"/> instance that matches strings representing valid IPv4 subnet masks.</returns>
7490
[GeneratedRegex($"^{SubnetmaskValues}$")]
7591
public static partial Regex SubnetmaskRegex();

‎Source/NETworkManager.Validators/MultipleHostsRangeValidator.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
8686
continue;
8787
}
8888

89+
// 192.168.0.1:80 or 192.168.0.1:80-90 or 192.168.0.1:80,443-445
90+
// (Port Scanner: scan this host only for its own ports)
91+
if (RegexHelper.IPv4AddressWithPortRegex().IsMatch(ipHostOrRange))
92+
continue;
93+
8994
// 2001:db8:85a3::8a2e:370:7334
9095
if (Regex.IsMatch(ipHostOrRange, RegexHelper.IPv6AddressRegex))
9196
continue;

‎Source/NETworkManager/ViewModels/PortScannerViewModel.cs‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,19 @@ private async Task Start()
415415
// Resolve hostnames
416416
(List<(IPAddress ipAddress, string hostname)> hosts, List<string> hostnamesNotResolved) hosts;
417417

418+
// Separate paired hosts (e.g. "192.168.0.1:80,443") from regular hosts. Paired hosts are scanned only
419+
// for their own ports, regular hosts for the ports entered in the "Ports" field.
420+
var hostInputs = HostRangeHelper.CreateListFromInput(Host);
421+
422+
var pairedHostInputs = hostInputs
423+
.Where(x => RegexHelper.IPv4AddressWithPortRegex().IsMatch(x))
424+
.ToList();
425+
426+
var regularHostInputs = hostInputs.Except(pairedHostInputs).ToList();
427+
418428
try
419429
{
420-
hosts = await HostRangeHelper.ResolveAsync(HostRangeHelper.CreateListFromInput(Host),
430+
hosts = await HostRangeHelper.ResolveAsync(regularHostInputs,
421431
SettingsManager.Current.Network_ResolveHostnamePreferIPv4, _cancellationTokenSource.Token);
422432
}
423433
catch (OperationCanceledException)
@@ -434,18 +444,46 @@ private async Task Start()
434444
IsStatusMessageDisplayed = true;
435445
}
436446

437-
// Convert ports to int array
447+
// Convert ports to int array (used for all regular hosts)
438448
var ports = await PortRangeHelper.ConvertPortRangeToIntArrayAsync(Ports);
439449

440-
PortsToScan = ports.Length * hosts.hosts.Count;
450+
// If there are regular hosts, the "Ports" field is required
451+
if (hosts.hosts.Count > 0 && ports.Length == 0)
452+
{
453+
StatusMessage = Strings.FieldCannotBeEmpty;
454+
IsStatusMessageDisplayed = true;
455+
456+
IsRunning = false;
457+
PreparingScan = false;
458+
459+
return;
460+
}
461+
462+
// Build the scan list: regular hosts scan the ports of the "Ports" field,
463+
// paired hosts (e.g. "192.168.0.1:80,443") scan only their own ports.
464+
var scanHosts = new List<(IPAddress ipAddress, string hostname, int[] ports)>();
465+
scanHosts.AddRange(hosts.hosts.Select(x => (x.ipAddress, x.hostname, ports)));
466+
467+
foreach (var pairedHost in pairedHostInputs)
468+
{
469+
var separatorIndex = pairedHost.IndexOf(':');
470+
var pairedPorts = await PortRangeHelper.ConvertPortRangeToIntArrayAsync(
471+
pairedHost[(separatorIndex + 1)..].Replace(',', ';'));
472+
473+
scanHosts.Add((IPAddress.Parse(pairedHost[..separatorIndex]), string.Empty, pairedPorts));
474+
}
475+
476+
PortsToScan = scanHosts.Sum(x => x.ports.Length);
441477
PortsScanned = 0;
442478
Volatile.Write(ref _latestPortsScanned, 0);
443479

444480
PreparingScan = false;
445481

446482
// Add host(s) to the history
447483
AddHostToHistory(Host);
448-
AddPortToHistory(Ports);
484+
485+
if (!string.IsNullOrWhiteSpace(Ports))
486+
AddPortToHistory(Ports);
449487

450488
var portScanner = new PortScanner(new PortScannerOptions(
451489
SettingsManager.Current.PortScanner_MaxHostThreads,
@@ -471,7 +509,7 @@ private async Task Start()
471509
};
472510
_resultsFlushTimer.Start();
473511

474-
portScanner.ScanAsync(hosts.hosts, ports, _cancellationTokenSource.Token);
512+
portScanner.ScanAsync(scanHosts, _cancellationTokenSource.Token);
475513
}
476514

477515
private void Stop()

‎Source/NETworkManager/Views/PortScannerView.xaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@
8080
ItemsSource="{Binding PortsHistoryView}"
8181
mah:TextBoxHelper.Watermark="{x:Static localization:StaticStrings.ExamplePortScanRange}"
8282
IsReadOnly="{Binding Path=IsRunning}"
83+
controls:ComboBoxPasteBehavior.ConvertMultilineToSemicolon="True"
8384
Style="{StaticResource HistoryComboBox}">
8485
<ComboBox.Text>
8586
<Binding Path="Ports" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
8687
<Binding.ValidationRules>
87-
<validators:EmptyValidator ValidatesOnTargetUpdated="True" />
88-
<validators:PortRangeValidator ValidatesOnTargetUpdated="True" />
88+
<validators:EmptyOrPortRangeValidator ValidatesOnTargetUpdated="True" />
8989
</Binding.ValidationRules>
9090
</Binding>
9191
</ComboBox.Text>

‎Website/docs/application/port-scanner.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ Example: `10.0.0.0/24; 10.0.[10-20]1` or `1-1024; 8080; 8443`
5151

5252
Shorthand ranges like `192.168.0.1-100` (192.168.0.1 to 192.168.0.100) are also supported.
5353

54+
**Paired scanning** — add ports directly to a host to scan that host only for its own ports:
55+
56+
| Host | Description |
57+
| ----------------------- | -------------------------------------------------------------------------- |
58+
| `10.0.0.1:80` | Scan `10.0.0.1` only for port `80` |
59+
| `10.0.0.1:80-90` | Scan `10.0.0.1` only for the ports `80` to `90` |
60+
| `10.0.0.1:80,443-445` | Scan `10.0.0.1` only for the ports `80`, `443` to `445` |
61+
62+
Such hosts are scanned **only** for the given ports, while regular hosts are scanned for the ports in the [Ports](#ports) field. You can mix both in a single scan, e.g. `10.0.0.1:80; 10.0.0.2; 10.0.0.3:22,3389` scans `10.0.0.1` for port `80`, `10.0.0.2` for the ports of the Ports field and `10.0.0.3` for ports `22` and `3389`. If all hosts use paired ports, the [Ports](#ports) field can be left empty.
63+
5464
:::
5565

5666
### Toolbar

‎Website/docs/changelog/next-release.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Release date: **xx.xx.2026**
4545

4646
**Port Scanner**
4747

48+
- Added **paired scanning**: a host can carry its own ports (e.g. `10.0.0.1:80,443-445`) and is then scanned only for those ports, while regular hosts are scanned for the ports of the **Ports** field. Both can be mixed in a single scan, and the **Ports** field can be left empty if every host carries its own ports. [#3568](https://github.com/BornToBeRoot/NETworkManager/pull/3568)
4849
- Reduced the default **Max. concurrent host threads** from `5` to `4`. [#3564](https://github.com/BornToBeRoot/NETworkManager/pull/3564)
4950
- Reduced the default **Max. concurrent port threads** from `256` to `64`, a more conservative default that puts less simultaneous load on the scanned host. [#3564](https://github.com/BornToBeRoot/NETworkManager/pull/3564)
5051
- Added a new **Well-known ports** (`1-1024`) default port profile. [#3564](https://github.com/BornToBeRoot/NETworkManager/pull/3564)

0 commit comments

Comments
 (0)