package com.it_jaros.network_scanner; import java.util.BitSet; import java.util.List; import java.util.stream.Collectors; public class App { public static void main(String[] args) { CliParser cliParser = new CliParser(); try { ScanOptions options = cliParser.parseArgs(args); Scanner scanner = new Scanner(options.openSocketLimit(), options.timeoutInMillis(), options.delayInMillis(), options.maxWorkersPerHost(), options.maxTargetsLimit()); printResults(scanner.scan(options), options.showBlockedPorts()); } catch (IllegalArgumentException e) { System.out.printf("Error while parsing arguments: %s", e.getMessage()); cliParser.usageAndExit(); } catch (Exception e) { System.out.printf("ERROR: %s", e.getMessage()); } } public static void printResults(List results, boolean showBlockedPorts) { results.forEach((ScanResult r) -> printResult(r, showBlockedPorts)); } private static void printResult(ScanResult result, boolean showBlockedPorts) { if (result.openPorts().isEmpty() && (!showBlockedPorts || result.blockedPorts().isEmpty())) { return; } System.out.println(result.target()); if (!result.openPorts().isEmpty()) { System.out.println("\topen:\t" + map(result.openPorts())); } if (showBlockedPorts && !result.blockedPorts().isEmpty()) { System.out.println("\tblocked:\t" + map(result.blockedPorts())); } System.out.println(); } private static String map(BitSet ports) { return ports.stream().mapToObj(String::valueOf).collect(Collectors.joining(",")); } }