package com.it_jaros.jscanner; import java.util.HashSet; import java.util.Set; public class CliParser { public ScanOptions parseArgs(String[] args) { if (args.length < 1 || args[0].trim().isEmpty()) { throw new IllegalArgumentException("No argument given"); } Set hostsArgv = new HashSet<>(); String hostsFile = null; String ports = CliDefaults.PORTS; boolean bannerRecognition = CliDefaults.BANNER_RECOGNITION; boolean disableOnlineCheck = CliDefaults.DISABLE_HOST_CHECK; boolean quiet = CliDefaults.QUIET; boolean showFilteredPorts = CliDefaults.SHOW_FILTERED_PORTS; int delayInMillis = CliDefaults.DELAY_IN_MILLIS; int maxHostsLimit = CliDefaults.MAX_HOSTS_LIMIT; int maxWorkersPerHost = CliDefaults.MAX_WORKERS_PER_HOST; int socketLimit = CliDefaults.SOCKET_LIMIT; int timeoutInMillis = CliDefaults.TIMEOUT_IN_MILLIS; for (int i = 0; i < args.length; i++) { final String currentArg = args[i]; if (!currentArg.startsWith("-")) { hostsArgv.add(currentArg); continue; } switch (currentArg) { case "--bannerRecognition", "-b": bannerRecognition = true; break; case "--delay", "-d": delayInMillis = parseIntOption(args, ++i, currentArg); break; case "--disableOnlineCheck", "-do": disableOnlineCheck = true; break; case "--help", "-h": printUsageHelp(); throw new HelpRequested(); case "--input", "-i": hostsFile = getArgument(args, ++i, currentArg); break; case "--maxHostsLimit", "-m": maxHostsLimit = parseIntOption(args, ++i, currentArg); break; case "--maxWorkersPerHost", "-mh": maxWorkersPerHost = parseIntOption(args, ++i, currentArg); break; case "--ports", "-p": ports = getArgument(args, ++i, currentArg); break; case "--quiet", "-q": quiet = true; break; case "--showFilteredPorts", "-sf": showFilteredPorts = true; break; case "--socketLimit", "-sl": socketLimit = parseIntOption(args, ++i, currentArg); break; case "--timeout", "-t": timeoutInMillis = parseIntOption(args, ++i, currentArg); break; default: throw new IllegalArgumentException("No such param " + currentArg); } } if (hostsArgv.isEmpty() && hostsFile == null) { throw new IllegalArgumentException("No input provided. Check with --help for available options."); } return new ScanOptions( hostsArgv.stream().toList(), hostsFile, ports, bannerRecognition, disableOnlineCheck, quiet, showFilteredPorts, delayInMillis, maxHostsLimit, maxWorkersPerHost, socketLimit, timeoutInMillis ); } private String getArgument(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 = getArgument(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 printUsageHelp() { StringBuilder usage = new StringBuilder(); usage.append(""" Usage: java -jar jscanner.jar [OPTIONS] [,...] Options: """); usage.append(""" --bannerRecognition, -b:\tEnable banner recognition to find out which service is running behind port (Default :%b) --delay, -d:\t\tWaiting period in millis for connection attempts for a host (Default: %d) --disableOnlineCheck, -do:\tDon't check if Host is reachable (Default: %b) --input, -i:\t\tRead file to get host targets to scan. If set to "-" than Stdin is read (Default: null) --maxHostsLimit, -m:\tMax number of hosts that are scanned at the same time (Default: %d) --maxWorkersPerHost, -mh:\tVirtual Worker Threads run per Host. Higher doesn't mean necessarily faster. But the more you use the more RAM is required (Default: %d) --ports, -p:\t\tDefine ports to be scanned (Default: %s) --quiet, -q:\t\tDo not output progress bar (Default: %b) --showFilteredPorts, -sf:\tShow also filtered ports (Default: %b) --socketLimit, -sl:\t\tSocket limit in total. You can not scan more ports than allowed here (Default: %d) --timeout, -t:\t\tTimeout in millis per connection attempt, 0 means infinite (Default: %d) """.formatted(CliDefaults.BANNER_RECOGNITION, CliDefaults.DELAY_IN_MILLIS, CliDefaults.DISABLE_HOST_CHECK, CliDefaults.MAX_HOSTS_LIMIT, CliDefaults.MAX_WORKERS_PER_HOST, CliDefaults.PORTS, CliDefaults.QUIET, CliDefaults.SHOW_FILTERED_PORTS, CliDefaults.SOCKET_LIMIT, CliDefaults.TIMEOUT_IN_MILLIS) ); usage.append(""" Examples: java -jar jscanner.jar localhost java -jar jscanner.jar fd00::1 """); System.err.println(usage); } }