diff options
Diffstat (limited to 'src/main/java/com/it_jaros/jscanner/Scan.java')
| -rw-r--r-- | src/main/java/com/it_jaros/jscanner/Scan.java | 80 |
1 files changed, 77 insertions, 3 deletions
diff --git a/src/main/java/com/it_jaros/jscanner/Scan.java b/src/main/java/com/it_jaros/jscanner/Scan.java index f680326..7167264 100644 --- a/src/main/java/com/it_jaros/jscanner/Scan.java +++ b/src/main/java/com/it_jaros/jscanner/Scan.java @@ -1,14 +1,88 @@ package com.it_jaros.jscanner; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; import java.util.List; -public record Scan(List<String> hosts, String ports, ScanState state) { +public class Scan { + private final List<String> hosts; + private final ScanState state; + private final String ports; + private final Counter counter; + private final ProgressBar progressBar; + + private Scan(List<String> hosts, String ports, ScanState state, boolean quiet) { + this.hosts = hosts; + this.ports = ports; + this.state = state; + this.counter = new Counter(); + this.progressBar = new ProgressBar(quiet); + } public List<ScanResult> getResults() { - return this.state.getSnapshotOfScanResults(); + return this.state.getCopyOfResults(); } public int getPeakConcurrentConnects() { - return this.state.getCounter().max(); + return counter.max(); + } + + public static Scan create(ScanOptions options) { + if (options.hostsFile() != null) { + return create(options.hostsFile(), options.ports(), options.quiet()); + } + return create(options.hostsArgv(), options.ports(), options.quiet()); + } + + public static Scan create(String sourceFile, String ports, boolean quiet) { + try (BufferedReader reader = Files.newBufferedReader(Path.of(sourceFile))) { + return create(reader.lines().toList(), ports, quiet); + } catch (FileNotFoundException | NoSuchFileException e) { + System.out.printf("Could not open file %s%n", sourceFile); + } catch (IOException e) { + System.out.printf("Error while trying to read hosts: %s -> %s %n", e.getClass().getSimpleName(), e.getMessage()); + } + + return null; + } + + public static Scan create(List<String> hostsArgv, String ports, boolean quiet) { + return new Scan(hostsArgv, ports, new ScanState(), quiet); + } + + public List<String> getHosts() { + return hosts; + } + + public String getPorts() { + return ports; + } + + public void start() { + progressBar.start(); + } + + public void stop() { + progressBar.stop(); + } + + public void addHostProgress(Progress progress) { + progressBar.add(progress); + } + + public void incSocketCounter() { + counter.inc(); + } + + public void decSocketCounter() { + counter.dec(); + } + + public void addScanResult(ScanResult scanResult) { + state.addScanResult(scanResult); } } |
