From 3ea51b908ef949267264a027e2462b220900aa45 Mon Sep 17 00:00:00 2001 From: Matthias Jaros Date: Mon, 16 Mar 2026 15:57:09 +0100 Subject: Replaced printStackTrace with proper message, handling interrupts now and also moved workerthreads to target level instead of global level --- .../java/com/it_jaros/networkScanner/Scanner.java | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/main/java/com/it_jaros/networkScanner/Scanner.java') 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 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 errors = new ArrayList<>(); + for (Future 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(); -- cgit v1.3.1