summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/com/it_jaros/jscanner/Scanner.java119
1 files changed, 66 insertions, 53 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/Scanner.java b/src/main/java/com/it_jaros/jscanner/Scanner.java
index d22e254..a0a8c00 100644
--- a/src/main/java/com/it_jaros/jscanner/Scanner.java
+++ b/src/main/java/com/it_jaros/jscanner/Scanner.java
@@ -92,27 +92,11 @@ public class Scanner implements AutoCloseable {
// with whatever is there already
while (state.running().get() || state.inPipeline().get() > 0) {
try {
- // let's check for results and give add them to our scan data holder object
- Future<ScanResult> finishedHost = state.completionService().poll(10, TimeUnit.MILLISECONDS);
- if (finishedHost == null) {
+ ScanResult result = getHostResult(state);
+ if (result == null) {
continue;
}
-
- ScanResult result = null;
- try {
- 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();
- }
-
- // release workers first before consuming
- if (result != null) {
- consumer.accept(result);
- }
+ consumer.accept(result);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
@@ -121,6 +105,27 @@ public class Scanner implements AutoCloseable {
scan.stop();
}
+ private ScanResult getHostResult(ProducerState<ScanResult> state) throws InterruptedException {
+ // let's check for results and give add them to our scan data holder object
+ Future<ScanResult> finishedHost = state.completionService().poll(10, TimeUnit.MILLISECONDS);
+ if (finishedHost == null) {
+ return null;
+ }
+
+ ScanResult result = null;
+ try {
+ 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();
+ }
+
+ return result;
+ }
+
/**
* Scans the ports of a given host
*
@@ -165,44 +170,12 @@ public class Scanner implements AutoCloseable {
// only exception is when cancelled is set
while (!cancelled && (state.running().get() || state.inPipeline().get() > 0)) {
try {
- Future<PortResult> portResultFuture = state.completionService().poll(10, TimeUnit.MILLISECONDS);
- if (portResultFuture == null) {
- continue;
- }
-
- PortResult portResult = null;
- try {
- portResult = portResultFuture.get();
- } catch (ExecutionException e) {
- // something more serious did not work
- System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
- } finally {
- state.activeWorkers().release();
- state.inPipeline().decrementAndGet();
- }
-
+ PortResult portResult = getPortResult(state);
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)));
- }
+ handlePortResult(portResult, openPorts, serviceTypes, filteredPorts, scanFailures);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
@@ -211,6 +184,46 @@ public class Scanner implements AutoCloseable {
return new ScanResult(host, openPorts, filteredPorts, serviceTypes, scanFailures);
}
+ private PortResult getPortResult(ProducerState<PortResult> state) throws InterruptedException {
+ Future<PortResult> portResultFuture = state.completionService().poll(10, TimeUnit.MILLISECONDS);
+ if (portResultFuture == null) {
+ return null;
+ }
+
+ PortResult portResult = null;
+ try {
+ portResult = portResultFuture.get();
+ } catch (ExecutionException e) {
+ // something more serious did not work
+ System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
+ } finally {
+ state.activeWorkers().release();
+ state.inPipeline().decrementAndGet();
+ }
+ return portResult;
+ }
+
+ private void handlePortResult(PortResult portResult, BitSet openPorts, HashMap<Integer, ServiceType> serviceTypes, BitSet filteredPorts, List<ScanFailure> scanFailures) {
+ 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)));
+ }
+ }
+
/**
* This method helps to cleanup the code a bit and remove redundancy
* The producer for providing hosts and the one for providing ports