commit 5034a90eb2711ff1bbe0a5817ed54004ef78fa60
parent 74694c42bfb9c0c45013c4e2dfa4aa2917c000a3
Author: Matthias Jaros <jaros@mailbox.org>
Date: Mon, 23 Mar 2026 16:49:49 +0100
Consistent naming of host instead of target
Diffstat:
5 files changed, 42 insertions(+), 42 deletions(-)
diff --git a/src/main/java/com/it_jaros/network_scanner/App.java b/src/main/java/com/it_jaros/network_scanner/App.java
@@ -10,7 +10,7 @@ public class App {
try {
ScanOptions options = cliParser.parseArgs(args);
Scanner scanner = new Scanner(options.openSocketLimit(), options.timeoutInMillis(),
- options.delayInMillis(), options.maxWorkersPerHost(), options.maxTargetsLimit(), options.disableTargetCheck());
+ options.delayInMillis(), options.maxWorkersPerHost(), options.maxHostsLimit(), options.disableHostCheck());
printResults(scanner.scan(options), options.showFilteredPorts());
} catch (IllegalArgumentException e) {
System.out.printf("Error while parsing arguments: %s", e.getMessage());
@@ -29,7 +29,7 @@ public class App {
return;
}
- System.out.println(result.target());
+ System.out.println(result.host());
if (!result.openPorts().isEmpty()) {
System.out.printf("\t (%d) open:\t%s%n", result.openPorts().cardinality(), map(result.openPorts()));
}
diff --git a/src/main/java/com/it_jaros/network_scanner/CliParser.java b/src/main/java/com/it_jaros/network_scanner/CliParser.java
@@ -10,20 +10,20 @@ public class CliParser {
throw new IllegalArgumentException("No argument given");
}
- Set<String> targets = new HashSet<>();
+ 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 maxTargetsLimit = CliDefaults.MAX_TARGETS_LIMIT;
+ int maxHostsLimit = CliDefaults.MAX_TARGETS_LIMIT;
String ports = CliDefaults.PORTS;
boolean showFilteredPorts = CliDefaults.SHOW_FILTERED_PORTS;
- boolean disableTargetCheck = CliDefaults.DISABLE_TARGET_CHECK;
+ boolean disableHostCheck = CliDefaults.DISABLE_TARGET_CHECK;
for (int i = 0; i < args.length; i++) {
final String currentArg = args[i];
if (!currentArg.startsWith("-")) {
- targets.add(currentArg);
+ targetHosts.add(currentArg);
continue;
}
@@ -44,8 +44,8 @@ public class CliParser {
case "--maxWorkersPerHost", "-wh":
maxWorkersPerHost = Integer.parseInt(args[++i]);
break;
- case "--maxTargetsLimit", "-tl":
- maxTargetsLimit = Integer.parseInt(args[++i]);
+ case "--maxHostsLimit", "-tl":
+ maxHostsLimit = Integer.parseInt(args[++i]);
break;
case "--ports", "-p":
ports = args[++i];
@@ -53,8 +53,8 @@ public class CliParser {
case "--showFilteredPorts", "-sf":
showFilteredPorts = true;
break;
- case "--disableTargetCheck", "-dt":
- disableTargetCheck = true;
+ case "--disableHostCheck", "-dt":
+ disableHostCheck = true;
break;
default:
throw new IllegalArgumentException("No such param " + currentArg);
@@ -66,10 +66,10 @@ public class CliParser {
}
System.out.printf(
- "Parsed arguments: targets('%s'), ports(%s), socketLimit(%s), delayInMillis(%s), timeoutInMillis(%s), maxWorkersPerHost(%s), maxTargetsLimit(%s), showFilteredPorts(%s), disableTargetCheck(%s) %n",
- targets, ports, socketLimit, delayInMillis, timeoutInMillis, maxWorkersPerHost, maxTargetsLimit, showFilteredPorts, disableTargetCheck);
+ "Parsed arguments: targetHosts('%s'), ports(%s), socketLimit(%s), delayInMillis(%s), timeoutInMillis(%s), maxWorkersPerHost(%s), maxTargetsLimit(%s), showFilteredPorts(%s), disableTargetCheck(%s) %n",
+ targetHosts, ports, socketLimit, delayInMillis, timeoutInMillis, maxWorkersPerHost, maxHostsLimit, showFilteredPorts, disableHostCheck);
- return new ScanOptions(targets.stream().toList(), socketLimit, delayInMillis, timeoutInMillis, maxWorkersPerHost, maxTargetsLimit, ports, showFilteredPorts, disableTargetCheck);
+ return new ScanOptions(targetHosts.stream().toList(), socketLimit, delayInMillis, timeoutInMillis, maxWorkersPerHost, maxHostsLimit, ports, showFilteredPorts, disableHostCheck);
}
public void usageAndExit() {
@@ -82,10 +82,10 @@ public class CliParser {
--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 target. Higher doesn't mean necessarily faster. But the more you use the more RAM is required (Default: 1000)
- --maxTargetsLimit, -tl:\\tMax number of targets that are scanned at the same time (Default: 10)
+ --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
- --disableTargetCheck, -dt:\\\\\\\\tDon't check if target is reachable
+ --disableHostCheck, -dt:\\\\\\\\tDon't check if Host is reachable
Examples:
java -jar scanner.jar localhost
diff --git a/src/main/java/com/it_jaros/network_scanner/ScanOptions.java b/src/main/java/com/it_jaros/network_scanner/ScanOptions.java
@@ -2,4 +2,4 @@ package com.it_jaros.network_scanner;
import java.util.List;
-public record ScanOptions(List<String> targets, int openSocketLimit, int delayInMillis, int timeoutInMillis, int maxWorkersPerHost, int maxTargetsLimit, String ports, boolean showFilteredPorts, boolean disableTargetCheck) {}
+public record ScanOptions(List<String> targetHosts, int openSocketLimit, int delayInMillis, int timeoutInMillis, int maxWorkersPerHost, int maxHostsLimit, String ports, boolean showFilteredPorts, boolean disableHostCheck) {}
diff --git a/src/main/java/com/it_jaros/network_scanner/ScanResult.java b/src/main/java/com/it_jaros/network_scanner/ScanResult.java
@@ -2,4 +2,4 @@ package com.it_jaros.network_scanner;
import java.util.BitSet;
-public record ScanResult(String target, BitSet openPorts, BitSet filteredPorts) {}
+public record ScanResult(String host, BitSet openPorts, BitSet filteredPorts) {}
diff --git a/src/main/java/com/it_jaros/network_scanner/Scanner.java b/src/main/java/com/it_jaros/network_scanner/Scanner.java
@@ -18,35 +18,35 @@ public class Scanner {
private final int timeoutInMillis;
private final long delayInNanos;
private final int maxWorkersPerHost;
- private final int maxTargetsLimit;
- private final boolean disableTargetCheck;
+ private final int maxHostsLimit;
+ private final boolean disableHostCheck;
- public Scanner(int socketLimit, int timeoutInMillis, int delayInMillis, int maxWorkersPerHost, int maxTargetsLimit, boolean disableTargetCheck) {
+ public Scanner(int socketLimit, int timeoutInMillis, int delayInMillis, int maxWorkersPerHost, int maxHostsLimit, boolean disableHostCheck) {
this.timeoutInMillis = timeoutInMillis;
this.delayInNanos = TimeUnit.MILLISECONDS.toNanos(Math.max(0, delayInMillis));
this.socketLimit = new Semaphore(socketLimit);
this.maxWorkersPerHost = maxWorkersPerHost;
- this.maxTargetsLimit = maxTargetsLimit;
- this.disableTargetCheck = disableTargetCheck;
+ this.maxHostsLimit = maxHostsLimit;
+ this.disableHostCheck = disableHostCheck;
}
- public List<ScanResult> scan(ScanOptions target) {
- return scanTargets(target.targets(), target.ports());
+ public List<ScanResult> scan(ScanOptions options) {
+ return scanHosts(options.targetHosts(), options.ports());
}
- public List<ScanResult> scanTargets(List<String> targets, String ports) {
+ public List<ScanResult> scanHosts(List<String> targetHosts, String ports) {
List<ScanResult> results = new ArrayList<>();
progressBar.start();
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
List<Future<ScanResult>> futures = new ArrayList<>();
- Semaphore maxTargets = new Semaphore(maxTargetsLimit);
- for (String target : targets) {
- maxTargets.acquireUninterruptibly();
+ Semaphore maxHosts = new Semaphore(maxHostsLimit);
+ for (String host : targetHosts) {
+ maxHosts.acquireUninterruptibly();
futures.add(executor.submit(() -> {
try {
- return scanTarget(target, ports);
+ return scanHost(host, ports);
} finally {
- maxTargets.release();
+ maxHosts.release();
}
}));
}
@@ -71,20 +71,20 @@ public class Scanner {
return results;
}
- private ScanResult scanTarget(String target, String ports) {
+ private ScanResult scanHost(String host, String ports) {
PortRange portRange = new PortRange(ports);
AtomicLong portSlotFactory = new AtomicLong(System.nanoTime());
Semaphore maxWorkers = new Semaphore(maxWorkersPerHost);
BitSet openPorts = new BitSet(PortRange.MAX_PORT);
BitSet filteredPorts = new BitSet(PortRange.MAX_PORT);
- Progress progress = new Progress(target, portRange.getTotal(), new AtomicInteger(), new AtomicInteger(), new AtomicInteger());
+ Progress progress = new Progress(host, portRange.getTotal(), new AtomicInteger(), new AtomicInteger(), new AtomicInteger());
progressBar.submit(progress);
- if(!disableTargetCheck && !checkHostOnline(target)) {
+ if(!disableHostCheck && !checkHostOnline(host)) {
// Visually show that this host is basically done
progress.done().set(progress.total());
- return new ScanResult(target, openPorts, filteredPorts);
+ return new ScanResult(host, openPorts, filteredPorts);
}
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
@@ -95,7 +95,7 @@ public class Scanner {
futures.add(executor.submit(() -> {
try {
waitForSlot(portSlotFactory);
- PortState state = getPortState(target, currentPort);
+ PortState state = getPortState(host, currentPort);
if (state.equals(PortState.OPEN)) {
progress.open().incrementAndGet();
} else if (state.equals(PortState.FILTERED)) {
@@ -128,16 +128,16 @@ public class Scanner {
}
if (!errors.isEmpty()) {
- System.out.printf("Errors happened during scan of target %s%nErrors:%s -> %s", target, errors.size(), errors);
+ System.out.printf("Errors happened during scan of host %s%nErrors:%s -> %s", host, errors.size(), errors);
}
}
- return new ScanResult(target, openPorts, filteredPorts);
+ return new ScanResult(host, openPorts, filteredPorts);
}
- private boolean checkHostOnline(String target) {
+ private boolean checkHostOnline(String host) {
try {
- return InetAddress.getByName(target).isReachable(timeoutInMillis);
+ return InetAddress.getByName(host).isReachable(timeoutInMillis);
} catch (IOException e) {
// something went wrong
}
@@ -154,11 +154,11 @@ public class Scanner {
}
}
- private PortState getPortState(String target, int port) {
+ private PortState getPortState(String host, int port) {
socketLimit.acquireUninterruptibly();
counter.inc();
try (Socket socket = new Socket()) {
- socket.connect(new InetSocketAddress(target, port), timeoutInMillis);
+ socket.connect(new InetSocketAddress(host, port), timeoutInMillis);
return PortState.OPEN;
} catch (NoRouteToHostException ignored) {
// this can be safely ignored because the port is closed if a host is unreachable