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
112
|
package com.it_jaros.jscanner;
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 ScanState state;
private final String ports;
private final Counter counter;
private final ProgressBar progressBar;
private Scan(Stream<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.getCopyOfResults();
}
public int getPeakConcurrentConnects() {
return counter.max();
}
public static Scan create(ScanOptions options) throws IOException {
if (options.hostsFile() != null) {
return create(options.hostsFile(), options.ports(), options.quiet());
}
return create(options.hostsArgv(), options.ports(), options.quiet());
}
/**
* Create Scan based on either a source file or stdin
*
* @param sourceFile
* @param ports
* @param quiet
* @return
* @throws IOException
*/
public static Scan create(String sourceFile, String ports, boolean quiet) throws IOException {
Stream<String> hosts;
if ("-".equals(sourceFile)) {
BufferedReader stdinReader = new BufferedReader(new InputStreamReader(System.in));
hosts = stdinReader.lines();
} else {
BufferedReader reader = Files.newBufferedReader(Path.of(sourceFile));
hosts = reader.lines().onClose(() -> {
try {
reader.close();
} catch (IOException e) {
// ignored
}
});
}
return create(hosts, ports, quiet);
}
private static Scan create(Stream<String> lines, String ports, boolean quiet) {
return new Scan(lines, ports, new ScanState(), quiet);
}
public static Scan create(List<String> hostsArgv, String ports, boolean quiet) {
return new Scan(hostsArgv.stream(), ports, new ScanState(), quiet);
}
public Stream<String> getHosts() {
return hosts;
}
public String getPorts() {
return ports;
}
public void start() {
progressBar.start();
}
public void stop() {
progressBar.stop();
hosts.close(); // close stream
}
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);
}
}
|