jscanner

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit e66637a019cd2581fea9d5971c25ae7cf7ec6d56
parent a7777d6b52a31d2045801cadfe1a61c196d9f6bd
Author: Matthias Jaros <jaros@mailbox.org>
Date:   Mon, 16 Mar 2026 15:57:09 +0100

Replaced printStackTrace with proper message, handling interrupts now
and also moved workerthreads to target level instead of global level

Diffstat:
Msrc/main/java/com/it_jaros/networkScanner/App.java | 6+++---
Msrc/main/java/com/it_jaros/networkScanner/Scanner.java | 28+++++++++++++++++++---------
2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/src/main/java/com/it_jaros/networkScanner/App.java b/src/main/java/com/it_jaros/networkScanner/App.java @@ -12,15 +12,15 @@ public class App { options.delayInMillis()); printResults(scanner.scan(options)); } catch (IllegalArgumentException e) { - System.out.println(e.getMessage()); + System.out.printf("Error while parsing arguments: %s", e.getMessage()); cliParser.usageAndExit(); } catch (Exception e) { - e.printStackTrace(); + System.out.printf("ERROR %s", e.getMessage()); } } public static void printResults(List<ScanResult> results) { - results.forEach(r -> printResult(r)); + results.forEach(App::printResult); } private static void printResult(ScanResult result) { diff --git 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();