summaryrefslogtreecommitdiff
path: root/src/main/java/com/it_jaros/network_scanner/CliParser.java
blob: 89b9c65192b4d00eba71ec6d4f6ca2e6dfc9e572 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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<String> 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] <host>

                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);
    }
}