package com.it_jaros.network_scanner; import java.util.HashSet; import java.util.Set; public class CliParser { public ScanOptions parseArgs(String[] args) { if (args.length < 1 || args[0].trim().equals("")) { throw new IllegalArgumentException("No argument given"); } Set targetHosts = new HashSet<>(); int socketLimit = CliDefaults.SOCKET_LIMIT; int delayInMillis = CliDefaults.DELAY_IN_MILLIS; int timeoutInMillis = CliDefaults.TIMEOUT_IN_MILLIS; int maxWorkersPerHost = CliDefaults.MAX_WORKERS_PER_HOST; int maxHostsLimit = CliDefaults.MAX_HOSTS_LIMIT; String ports = CliDefaults.PORTS; boolean showFilteredPorts = CliDefaults.SHOW_FILTERED_PORTS; boolean disableHostCheck = CliDefaults.DISABLE_HOST_CHECK; String file = null; for (int i = 0; i < args.length; i++) { final String currentArg = args[i]; if (!currentArg.startsWith("-")) { targetHosts.add(currentArg); continue; } switch (currentArg) { case "--help", "-h": usageAndExit(); break; case "--socketLimit", "-sl": socketLimit = parseIntOption(args, ++i, currentArg); break; case "--delay", "-d": delayInMillis = parseIntOption(args, ++i, currentArg); break; case "--timeout", "-t": timeoutInMillis = parseIntOption(args, ++i, currentArg); break; case "--maxWorkersPerHost", "-wh": maxWorkersPerHost = parseIntOption(args, ++i, currentArg); break; case "--maxHostsLimit", "-tl": maxHostsLimit = parseIntOption(args, ++i, currentArg); break; case "--ports", "-p": ports = requireValue(args, ++i, currentArg); break; case "--showFilteredPorts", "-sf": showFilteredPorts = true; break; case "--disableHostCheck", "-dt": disableHostCheck = true; break; case "--file", "-f": file = requireValue(args, ++i, currentArg); break; default: throw new IllegalArgumentException("No such param " + currentArg); } } System.out.printf( "Parsed arguments: targetHosts('%s'), ports(%s), socketLimit(%s), delayInMillis(%s), timeoutInMillis(%s), maxWorkersPerHost(%s), maxTargetsLimit(%s), showFilteredPorts(%s), disableTargetCheck(%s), file(%s) %n", targetHosts, ports, socketLimit, delayInMillis, timeoutInMillis, maxWorkersPerHost, maxHostsLimit, showFilteredPorts, disableHostCheck, file); return new ScanOptions(targetHosts.stream().toList(), socketLimit, delayInMillis, timeoutInMillis, maxWorkersPerHost, maxHostsLimit, ports, showFilteredPorts, disableHostCheck, file); } private String requireValue(String[] args, int index, String option) { if (index >= args.length) { throw new IllegalArgumentException(String.format("Missing argument for option %s", option)); } return args[index]; } private int parseIntOption(String[] args, int index, String option) { String value = requireValue(args, index, option); try { return Integer.parseInt(value); } catch (NumberFormatException e) { throw new IllegalArgumentException(String.format("Invalid argument %s for option %s", value, option)); } } public void usageAndExit() { System.err.println(""" Usage: java -jar scanner.jar [OPTIONS] Options: --delay, -d:\t\tWaiting period in millis for connection attempts for a host (Default: 0) --socketLimit, -sl:\t\tSocket Connection limit for a host (Default: 1000) --timeout, -t:\t\tTimeout per connection attempt, 0 means infinite (Default: 1000) --ports, -p:\t\tPorts to be scanned --maxWorkersPerHost, -wh:\tVirtual Worker Threads run per Host. Higher doesn't mean necessarily faster. But the more you use the more RAM is required (Default: 1000) --maxHostsLimit, -tl:\tMax number of Hosts that are scanned at the same time (Default: 10) --showFilteredPorts, -sf:\tShow also filtered ports --disableHostCheck, -dt:\tDon't check if Host is reachable Examples: java -jar scanner.jar localhost java -jar scanner.jar fd00::1 """); System.exit(1); } }