summaryrefslogtreecommitdiff
path: root/src/main/java/com/it_jaros/networkScanner/Scanner.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/it_jaros/networkScanner/Scanner.java')
-rw-r--r--src/main/java/com/it_jaros/networkScanner/Scanner.java28
1 files changed, 19 insertions, 9 deletions
diff --git a/src/main/java/com/it_jaros/networkScanner/Scanner.java b/src/main/java/com/it_jaros/networkScanner/Scanner.java
index 661369b..d219307 100644
--- a/src/main/java/com/it_jaros/networkScanner/Scanner.java
+++ b/src/main/java/com/it_jaros/networkScanner/Scanner.java
@@ -22,7 +22,6 @@ public class Scanner {
private final Counter counter = new Counter();
private final ProgressBar progressBar = new ProgressBar();
- private final Semaphore maxWorkers = new Semaphore(1000);
private final Semaphore socketLimit;
private final int timeoutInMillis;
private final long delayInNanos;
@@ -46,11 +45,13 @@ public class Scanner {
futures.add(executor.submit(() -> scanTarget(target, ports)));
}
- futures.forEach((f) -> {
+ futures.forEach((Future<ScanResult> f) -> {
try {
results.add(f.get());
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ } catch (ExecutionException e) {
+ System.out.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
}
});
}
@@ -63,6 +64,7 @@ public class Scanner {
private ScanResult scanTarget(String target, String ports) {
PortRange portRange = new PortRange(ports);
AtomicLong portSlotFactory = new AtomicLong(System.nanoTime());
+ Semaphore maxWorkers = new Semaphore(1000);
BitSet openPorts = new BitSet(PortRange.MAX_PORT);
Progress progress = new Progress(target, portRange.getTotal(), new AtomicInteger(), new AtomicInteger());
@@ -88,17 +90,24 @@ public class Scanner {
}));
}
- futures.forEach(f -> {
+ List<Throwable> errors = new ArrayList<>();
+ for (Future<Integer> f : futures) {
try {
int port = f.get();
if (port != -1) {
// openPorts is not threadsafe, so we do not update it outside the worker threads
openPorts.set(port);
}
- } catch (InterruptedException | ExecutionException e) {
- e.printStackTrace();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ } catch (ExecutionException e) {
+ errors.add(e);
}
- });
+ }
+
+ if (!errors.isEmpty()) {
+ System.out.printf("Errors happened during scan of target %s%nErrors:%s -> %s", target, errors.size(), errors);
+ }
}
return new ScanResult(target, openPorts);
@@ -128,8 +137,9 @@ public class Scanner {
socket.connect(new InetSocketAddress(target, port), timeoutInMillis);
return true;
} catch (SocketException | SocketTimeoutException ignored) {
+ // Will happen a lot when scanning for open ports, so not needed
} catch (IOException e) {
- System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
+ System.out.printf("%s -> %s", e.getClass().getSimpleName(), e.getMessage());
} finally {
counter.dec();
socketLimit.release();