summaryrefslogtreecommitdiff
path: root/src/main/java/com/it_jaros/jscanner/scan/Scan.java
diff options
context:
space:
mode:
authorGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-08-12 16:36:28 +0200
committerGravatar Matthias Jaros <jarlucmat@mailbox.org>2026-08-12 16:36:28 +0200
commit1cbb5b6ffd5afdf55ea6727ee23550796a57809b (patch)
tree1c1b31e14966afadf6677d66e5af92b0dea9b2d4 /src/main/java/com/it_jaros/jscanner/scan/Scan.java
parent7fc179ed2d663a04c22e4182cde898301597f74f (diff)
Renamed jscanner package to jns
Diffstat (limited to 'src/main/java/com/it_jaros/jscanner/scan/Scan.java')
-rw-r--r--src/main/java/com/it_jaros/jscanner/scan/Scan.java173
1 files changed, 0 insertions, 173 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/scan/Scan.java b/src/main/java/com/it_jaros/jscanner/scan/Scan.java
deleted file mode 100644
index 0820abc..0000000
--- a/src/main/java/com/it_jaros/jscanner/scan/Scan.java
+++ /dev/null
@@ -1,173 +0,0 @@
-package com.it_jaros.jscanner.scan;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.List;
-import java.util.stream.Stream;
-
-/**
- * Object holder for stateful volatile data during scan
- */
-public class Scan {
- private final Stream<String> hosts;
- private final String ports;
- private final Runnable onDone;
- private final Counter hostCounter;
- private final Counter portCounter;
- private final Counter socketCounter;
- private final Counter threadCounter;
- private long start = 0;
- private long stop = 0;
-
- private Scan(Stream<String> hosts, String ports) {
- this(hosts, ports, () -> {});
- }
-
- private Scan(Stream<String> hosts, String ports, Runnable onDone) {
- this.hosts = hosts;
- this.ports = ports;
- this.onDone = onDone;
- this.hostCounter = new Counter();
- this.portCounter = new Counter();
- this.socketCounter = new Counter();
- this.threadCounter = new Counter();
- }
-
- public static Scan create(ScanOptions options) throws IOException {
- if (options.hostsFile() != null) {
- return create(options.hostsFile(), options.ports());
- }
- return create(options.hostsArgv(), options.ports());
- }
-
- /**
- * Create Scan based on either a source file or stdin
- *
- * @param sourceFile
- * @param ports
- * @return
- * @throws IOException
- */
- public static Scan create(String sourceFile, String ports) {
- Stream<String> hosts;
- if (sourceFile == null || sourceFile.isEmpty()) {
- throw new IllegalArgumentException("Source file is null or empty");
- }
-
- if ("-".equals(sourceFile)) {
- BufferedReader stdinReader = new BufferedReader(new InputStreamReader(System.in));
- hosts = stdinReader.lines();
- return new Scan(hosts, ports);
- }
-
- try {
- BufferedReader reader = Files.newBufferedReader(Path.of(sourceFile));
- return new Scan(reader.lines(), ports, () -> {
- try {
- reader.close();
- } catch (IOException ignore) {
- // we ignore it because at this moment the program is shutting down anyway
- }
- });
- } catch (IOException e) {
- throw new ScanException("Error while trying to open source File", e);
- }
- }
-
- public static Scan create(List<String> hostsArgv, String ports) {
- return new Scan(hostsArgv.stream(), ports);
- }
-
- public Stream<String> getHosts() {
- return hosts;
- }
-
- public String getPorts() {
- return ports;
- }
-
- public void stop() {
- stop = System.currentTimeMillis();
- onDone.run();
- }
-
- public void start() {
- start = System.currentTimeMillis();
- }
-
- private long getDurationMillis() {
- if (stop == 0) {
- if (start == 0) {
- return 0;
- }
- return System.currentTimeMillis() - start;
- }
- return stop - start;
- }
-
- public void portStart() {
- this.portCounter.inc();
- this.threadCounter.inc();
- this.socketCounter.inc();
- }
-
- public void portFinish() {
- this.portCounter.dec();
- this.threadCounter.dec();
- this.socketCounter.dec();
- }
-
- public void hostStart() {
- this.hostCounter.inc();
- this.threadCounter.inc();
- }
-
- public void hostFinish() {
- this.threadCounter.dec();
- this.hostCounter.dec();
- }
-
- public void producerStart() {
- this.threadCounter.inc();
- }
-
- public void producerStop() {
- this.threadCounter.dec();
- }
-
- /**
- * This is not an atomic 100% correct snapshot but
- * more a relative snapshot optimized more for performance
- * than exactness
- */
- public Statistics getStatistics() {
- return new Statistics(
- this.getDurationMillis(),
- this.hostCounter.total(),
- this.hostCounter.current(),
- this.hostCounter.max(),
- this.portCounter.total(),
- this.portCounter.current(),
- this.threadCounter.current(),
- this.threadCounter.max(),
- this.socketCounter.current(),
- this.socketCounter.max()
- );
- }
-
- public record Statistics(
- long durationInMillis,
- int hostTotal,
- int hostCurrent,
- int hostMaxConcurrent,
- int portTotal,
- int portCurrent,
- int threadCurrent,
- int threadMaxConcurrent,
- int socketCurrent,
- int socketMaxConcurrent
- ) {}
-}