commit c8d7c63071ce4d3ae0bb767e7972c4879a44cf62
parent 7643482a657160517b7994915cbb902f73eeeac4
Author: Matthias Jaros <jaros@mailbox.org>
Date: Mon, 9 Mar 2026 16:19:16 +0100
Further broke down structure and fixed bug that not all ports got
scanned due to early executor shutdown. now handling everything nice in
a future that gives return port if it is open
Diffstat:
2 files changed, 31 insertions(+), 20 deletions(-)
diff --git a/src/main/java/com/it_jaros/networkScanner/ProgressBar.java b/src/main/java/com/it_jaros/networkScanner/ProgressBar.java
@@ -14,10 +14,8 @@ public class ProgressBar {
private ReentrantLock lock = new ReentrantLock();
public void start() {
- System.out.println("Progress:");
this.hosts = new ArrayList<>();
- // move cursor down depending on how many hosts have to be scanned
- System.out.print("\n");
+ System.out.print("Scanning targets...\n\n");
ui.scheduleAtFixedRate(() -> {
lock.lock();
drawProgress(false);
@@ -31,9 +29,10 @@ public class ProgressBar {
drawBar(progress);
}
- // move cursor up again
- if (!last)
+ if (!last) {
+ // move cursor up again
System.out.print("\u001b[" + hosts.size() + "A");
+ }
}
public void submit(Progress progress) {
diff --git a/src/main/java/com/it_jaros/networkScanner/Scanner.java b/src/main/java/com/it_jaros/networkScanner/Scanner.java
@@ -43,9 +43,7 @@ public class Scanner {
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();) {
List<Future<ScanResult>> futures = new ArrayList<>();
for (String target : targets) {
- futures.add(executor.submit(() -> {
- return scanTarget(target, ports);
- }));
+ futures.add(executor.submit(() -> scanTarget(target, ports)));
}
futures.forEach((f) -> {
@@ -64,32 +62,46 @@ public class Scanner {
private ScanResult scanTarget(String target, String ports) {
PortRange portRange = new PortRange(ports);
- AtomicLong scanSlotFactory = new AtomicLong(System.nanoTime());
+ AtomicLong portSlotFactory = new AtomicLong(System.nanoTime());
Queue<Integer> openPorts = new ConcurrentLinkedQueue<>();
Progress progress = new Progress(target, portRange.getTotal(), new AtomicInteger(), new AtomicInteger());
progressBar.submit(progress);
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();) {
+ List<Future<Integer>> futures = new ArrayList<>();
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();
- }
- });
+ futures.add(executor.submit(() -> scanPort(target, portSlotFactory, currentPort)));
}
+
+ futures.forEach(f -> {
+ try {
+ int port = f.get();
+ if (port != -1) {
+ openPorts.add(port);
+ progress.open().incrementAndGet();
+ }
+ } catch (InterruptedException | ExecutionException e) {
+ e.printStackTrace();
+ } finally {
+ progress.done().incrementAndGet();
+ }
+ });
}
return new ScanResult(target, openPorts);
}
+ private int scanPort(String target, AtomicLong scanSlotFactory, final int currentPort) {
+ waitForSlot(scanSlotFactory);
+ if (isTargetPortOpen(target, currentPort)) {
+ return currentPort;
+ }
+
+ return -1;
+ }
+
private void waitForSlot(AtomicLong scanSlotFactory) {
if (delayInNanos > 0) {
long slot = scanSlotFactory.getAndAdd(delayInNanos);