summaryrefslogtreecommitdiff
path: root/src/main/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/it_jaros/jscanner/Scanner.java79
1 files changed, 45 insertions, 34 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/Scanner.java b/src/main/java/com/it_jaros/jscanner/Scanner.java
index f656854..e22f18c 100644
--- a/src/main/java/com/it_jaros/jscanner/Scanner.java
+++ b/src/main/java/com/it_jaros/jscanner/Scanner.java
@@ -69,11 +69,13 @@ public class Scanner implements AutoCloseable {
maxHostsLimit,
host -> () -> {
try {
+ scan.getThreadCounter().inc();
scan.getHostCounter().inc();
scan.getHostTotalCounter().inc();
if (cancelled) return null;
return scanHostPorts(host, scan);
} finally {
+ scan.getThreadCounter().dec();
scan.getHostCounter().dec();
}
},
@@ -94,21 +96,23 @@ public class Scanner implements AutoCloseable {
continue;
}
- // No matter what happens we have to free the resources after getting ScanResult
+ ScanResult result = null;
try {
- ScanResult result = finishedHost.get();
- if (result != null) {
- consumer.accept(result);
- }
+ result = finishedHost.get();
+ } catch (ExecutionException e) {
+ System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
} finally {
+ // No matter what happens we have to free the resources after getting ScanResult
state.activeWorkers().release();
state.inPipeline().decrementAndGet();
- scan.getThreadCounter().dec();
+ }
+
+ // release workers first before consuming
+ if (result != null) {
+ consumer.accept(result);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
- } catch (ExecutionException e) {
- System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
}
}
@@ -140,9 +144,14 @@ public class Scanner implements AutoCloseable {
portRange,
maxWorkersPerHost,
port -> () -> {
- waitForSlot(portSlotFactory);
- if (cancelled) return null;
- return checkPort(host, port, scan);
+ try {
+ scan.getThreadCounter().inc();
+ waitForSlot(portSlotFactory);
+ if (cancelled) return null;
+ return checkPort(host, port, scan);
+ } finally {
+ scan.getThreadCounter().dec();
+ }
},
scan.getThreadCounter()
);
@@ -157,35 +166,39 @@ public class Scanner implements AutoCloseable {
continue;
}
+ PortResult portResult = null;
try {
- PortResult portResult = portResultFuture.get();
- if (portResult == null) continue;
- switch (portResult.getState()) {
- case OPEN -> {
- // bitset is not thread-safe, so it is set
- // outside the other virtual threads that update progress
- openPorts.set(portResult.getPort());
- serviceTypes.put(portResult.getPort(), ServiceDetector.detect(portResult.getBanner()));
- }
- case FILTERED -> {
- filteredPorts.set(portResult.getPort());
- }
- default -> {
- // sonarcube glücklich machen
- }
- }
- Exception e = portResult.getException();
- if (e != null) {
- scanFailures.add(new ScanFailure(portResult.getPort(), ExceptionInfo.from(e)));
- }
+ portResult = portResultFuture.get();
} catch (ExecutionException e) {
// something more serious did not work
System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
} finally {
- scan.getThreadCounter().dec();
state.activeWorkers().release();
state.inPipeline().decrementAndGet();
}
+
+ if (portResult == null) {
+ continue;
+ }
+
+ switch (portResult.getState()) {
+ case OPEN -> {
+ // bitset is not thread-safe, so it is set
+ // outside the other virtual threads that update progress
+ openPorts.set(portResult.getPort());
+ serviceTypes.put(portResult.getPort(), ServiceDetector.detect(portResult.getBanner()));
+ }
+ case FILTERED -> {
+ filteredPorts.set(portResult.getPort());
+ }
+ default -> {
+ // sonarcube glücklich machen
+ }
+ }
+ Exception e = portResult.getException();
+ if (e != null) {
+ scanFailures.add(new ScanFailure(portResult.getPort(), ExceptionInfo.from(e)));
+ }
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
@@ -232,7 +245,6 @@ public class Scanner implements AutoCloseable {
final INPUT item = queue.next();
Callable<OUTPUT> task = taskFactory.apply(item);
inPipeline.incrementAndGet();
- threadCounter.inc();
try {
// here we are filling the completion service
// host <-> virtual thread
@@ -240,7 +252,6 @@ public class Scanner implements AutoCloseable {
submitted = true;
} catch (Throwable e) {
inPipeline.decrementAndGet();
- threadCounter.dec();
throw e;
}
} finally {