summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-08-13 16:22:51 +0200
committerGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-08-13 16:22:51 +0200
commit27cb977a8f7ae27388302b4f3343bdfc2cbb9838 (patch)
tree55567125991734a0755f4806438fc4ea9abfea15 /src/main
parent42f8eaaba15ca75b42ab2aa2434f9faefe17444a (diff)
Increased speed 1000x times by replacing host
reachable method with another port scan task. The speed is gained by removing thread pinning method isReachable()
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/it_jaros/jns/scan/engine/ScanHostTask.java24
-rw-r--r--src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java4
2 files changed, 10 insertions, 18 deletions
diff --git a/src/main/java/com/it_jaros/jns/scan/engine/ScanHostTask.java b/src/main/java/com/it_jaros/jns/scan/engine/ScanHostTask.java
index 0fbfef0..ce06287 100644
--- a/src/main/java/com/it_jaros/jns/scan/engine/ScanHostTask.java
+++ b/src/main/java/com/it_jaros/jns/scan/engine/ScanHostTask.java
@@ -3,15 +3,13 @@ package com.it_jaros.jns.scan.engine;
import com.it_jaros.jns.scan.Scan;
import com.it_jaros.jns.scan.domain.PortRange;
import com.it_jaros.jns.scan.domain.PortResult;
+import com.it_jaros.jns.scan.domain.PortState;
import com.it_jaros.jns.scan.domain.ScanResult;
-import java.io.IOException;
-import java.net.InetAddress;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.LockSupport;
public class ScanHostTask implements Callable<ScanResult> {
@@ -21,18 +19,16 @@ public class ScanHostTask implements Callable<ScanResult> {
private final CancelledToken cancelledToken;
private final boolean disableOnlineCheck;
private final int maxWorkersPerHost;
- private final int timeoutInMillis;
- private final long delayInNanos;
+ private final PortResultAccumulator accumulator;
public ScanHostTask(Scan scan, String host, ScanExecutionContext context) {
this.scan = scan;
this.host = host;
this.context = context;
+ this.accumulator = new PortResultAccumulator(host);
this.cancelledToken = context.cancelledToken();
- this.delayInNanos = TimeUnit.MILLISECONDS.toNanos(Math.max(0, context.scanOptions().delayInMillis()));
this.disableOnlineCheck = context.scanOptions().disableOnlineCheck();
this.maxWorkersPerHost = context.scanOptions().maxWorkersPerHost();
- this.timeoutInMillis = context.scanOptions().timeoutInMillis();
}
@Override
@@ -55,10 +51,6 @@ public class ScanHostTask implements Callable<ScanResult> {
// Unreachable host
return ScanResult.empty(host);
}
- // online check also sends packets to the target system.
- // in order not to violate set delay time
- // we wait here too
- LockSupport.parkNanos(delayInNanos);
}
final PortRange portRange = new PortRange(scan.getPorts());
@@ -74,7 +66,6 @@ public class ScanHostTask implements Callable<ScanResult> {
// we run as long as the producer is running OR
// as long as things are in pipeline waiting to be processed
// ONLY exception is when cancelled is set
- final PortResultAccumulator accumulator = new PortResultAccumulator(host);
while (!cancelledToken.isCancelled() && (state.running().get() || state.inPipeline().get() > 0)) {
try {
PollState<PortResult> poll = getPortResult(state);
@@ -94,8 +85,13 @@ public class ScanHostTask implements Callable<ScanResult> {
private boolean checkHostOnline() {
try {
- return InetAddress.getByName(host).isReachable(timeoutInMillis);
- } catch (IOException e) {
+ PortResult result = new ScanPortTask(host, 7, context).call();
+ Exception exception = result.getException();
+ accumulator.add(result);
+ if (exception == null && !result.getState().equals(PortState.FILTERED)) {
+ return true;
+ }
+ } catch (Exception e) {
// we ignore this error because it means that the host is probably not online
}
diff --git a/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java b/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java
index c5591e7..b6c5964 100644
--- a/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java
+++ b/src/main/java/com/it_jaros/jns/scan/engine/ScanPortTask.java
@@ -8,7 +8,6 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
-import java.net.NoRouteToHostException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.Callable;
@@ -89,9 +88,6 @@ public class ScanPortTask implements Callable<PortResult> {
}
} catch (ConnectException ignored) {
result.setState(PortState.CLOSED);
- } catch (NoRouteToHostException ignored) {
- // NoRouteToHostException: this can be safely ignored because the port is closed if a host is unreachable
- // Will happen a lot when scanning for open ports, so not needed
} catch (IOException e) {
result.setException(e);
}