commit ab71c5ad14512f2a33e1c91f510c4c00c8026ff7
parent b38ac9bda18b619e9177db1a71b1370552a8a0c3
Author: Matthias Jaros <jaros@mailbox.org>
Date: Fri, 3 Jul 2026 12:21:01 +0200
Added comments and minor refactoring
Diffstat:
2 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/src/main/java/com/it_jaros/jscanner/Scan.java b/src/main/java/com/it_jaros/jscanner/Scan.java
@@ -8,6 +8,9 @@ import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.List;
+/**
+ * Object holder for stateful volatile data during scan
+ */
public class Scan {
private final List<String> hosts;
private final ScanState state;
diff --git a/src/main/java/com/it_jaros/jscanner/Scanner.java b/src/main/java/com/it_jaros/jscanner/Scanner.java
@@ -49,6 +49,11 @@ public class Scanner implements AutoCloseable {
}
+ /**
+ * Starts a given scan.
+ * @param scan
+ * @return
+ */
public List<ScanResult> runScan(Scan scan) {
if (scan == null) {
throw new IllegalArgumentException("Scan argument cannot be null");
@@ -61,18 +66,20 @@ public class Scanner implements AutoCloseable {
int hostsToProcess = hostsInQueue.size();
int activeHostWorkers = 0;
while ((hostsToProcess > 0 && !cancelled) || activeHostWorkers > 0) {
- // process queue
while (
!hostsInQueue.isEmpty() // continue as long as we have hosts to process
&& activeHostWorkers < maxHostsLimit // limit is not reached
&& !cancelled // and scanner not closed
) {
- activeHostWorkers++;
+ // here we are filling the completion service
+ // host <-> virtual thread
final String host = hostsInQueue.poll();
+ activeHostWorkers++;
completionService.submit(() -> scanHostPorts(host, scan.getPorts(), scan));
}
try {
+ // let's check for results and give add them to our scan data holder object
Future<ScanResult> finishedHost = completionService.poll(10, TimeUnit.MILLISECONDS);
if (finishedHost == null) {
continue;
@@ -118,8 +125,10 @@ public class Scanner implements AutoCloseable {
&& activePerHostWorkers < maxWorkersPerHost // and we have not reached our limit
&& !cancelled // and the scanner is not about to close
) {
- activePerHostWorkers++;
+ // Here we are filling the completion service
+ // port <-> virtual thread
final int currentPort = portRange.next();
+ activePerHostWorkers++;
completionService.submit(() -> {
waitForSlot(portSlotFactory);
return checkPort(host, currentPort, scan);
@@ -133,7 +142,6 @@ public class Scanner implements AutoCloseable {
}
PortResult portResult = portResultFuture.get();
-
activePerHostWorkers--;
portsToProcess--;
progress.done().incrementAndGet();