commit a7e5e2346aa1016ec588f22d03515b8212f7722a
parent 0fae24059a8bb1046005386233833858663b7791
Author: Matthias Jaros <jaros@mailbox.org>
Date: Mon, 9 Mar 2026 14:41:39 +0100
Finally refactored structure of virtual threads in a way that makes
sense and feels more natural. Each layer, host target and target port
scan has it's own executor and is responsible for it's own updates.
return statement is called only when everything is finished
Diffstat:
4 files changed, 38 insertions(+), 30 deletions(-)
diff --git a/src/main/java/com/it_jaros/networkScanner/App.java b/src/main/java/com/it_jaros/networkScanner/App.java
@@ -15,7 +15,7 @@ public class App {
System.out.println(e.getMessage());
cliParser.usageAndExit();
} catch (Exception e) {
- System.err.println("Something went wrong");
+ e.printStackTrace();
}
}
diff --git a/src/main/java/com/it_jaros/networkScanner/ProgressBar.java b/src/main/java/com/it_jaros/networkScanner/ProgressBar.java
@@ -74,12 +74,4 @@ public class ProgressBar {
} catch (InterruptedException ignored) {
}
}
-
- void onPortFinished(Progress progress) {
- progress.done().incrementAndGet();
- }
-
- public void onPortOpen(Progress progress) {
- progress.open().incrementAndGet();
- }
}
diff --git a/src/main/java/com/it_jaros/networkScanner/ScanResult.java b/src/main/java/com/it_jaros/networkScanner/ScanResult.java
@@ -2,4 +2,4 @@ package com.it_jaros.networkScanner;
import java.util.Queue;
-public record ScanResult(String target, Queue<Integer> openPorts, Progress progress) {}
+public record ScanResult(String target, Queue<Integer> openPorts) {}
diff --git a/src/main/java/com/it_jaros/networkScanner/Scanner.java b/src/main/java/com/it_jaros/networkScanner/Scanner.java
@@ -9,8 +9,10 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@@ -37,13 +39,22 @@ public class Scanner {
public List<ScanResult> scanTargets(List<String> targets, String ports) {
List<ScanResult> results = new ArrayList<>();
+ List<Future<ScanResult>> futures = new ArrayList<>();
+ progressBar.start();
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();) {
- progressBar.start();
for (String target : targets) {
- ScanResult result = scanTargetPorts(target, ports, executor);
- results.add(result);
- progressBar.submit(result.progress());
+ futures.add(executor.submit(() -> {
+ return scanTargetPorts(target, ports);
+ }));
}
+
+ futures.forEach((f) -> {
+ try {
+ results.add(f.get());
+ } catch (InterruptedException | ExecutionException e) {
+ e.printStackTrace();
+ }
+ });
}
progressBar.stop();
@@ -51,27 +62,32 @@ public class Scanner {
return results;
}
- private ScanResult scanTargetPorts(String target, String ports, ExecutorService executor) {
+ private ScanResult scanTargetPorts(String target, String ports) {
PortRange portRange = new PortRange(ports);
- Progress progress = new Progress(target, portRange.getTotal(), new AtomicInteger(),
- new AtomicInteger());
AtomicLong scanSlotFactory = new AtomicLong(System.nanoTime());
Queue<Integer> openPorts = new ConcurrentLinkedQueue<>();
- while (portRange.hasNext()) {
- final int currentPort = portRange.next();
- executor.submit(() -> {
- try {
- waitForSlot(scanSlotFactory);
- if (isTargetPortOpen(target, currentPort)) {
- openPorts.add(currentPort);
- progressBar.onPortOpen(progress);
+
+ Progress progress = new Progress(target, portRange.getTotal(), new AtomicInteger(), new AtomicInteger());
+ progressBar.submit(progress);
+
+ try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();) {
+ while (portRange.hasNext()) {
+ final int currentPort = portRange.next();
+ executor.submit(() -> {
+ try {
+ waitForSlot(scanSlotFactory);
+ if (isTargetPortOpen(target, currentPort)) {
+ openPorts.add(currentPort);
+ progress.open().incrementAndGet();
+ }
+ } finally {
+ progress.done().incrementAndGet();
}
- } finally {
- progressBar.onPortFinished(progress);
- }
- });
+ });
+ }
}
- return new ScanResult(target, openPorts, progress);
+
+ return new ScanResult(target, openPorts);
}
private void waitForSlot(AtomicLong scanSlotFactory) {