summaryrefslogtreecommitdiff
path: root/src/main/java/com/it_jaros/jscanner/Scanner.java
blob: 39fd584f95ef35f440b102bf36216007a9fa92e1 (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package com.it_jaros.jscanner;

import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Function;

public class Scanner implements AutoCloseable {

    private static final int READ_BUFFER_SIZE = 1024;

    private volatile boolean cancelled = false;

    private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
    private final Semaphore socketLimit;
    private final boolean bannerRecognition;
    private final boolean disableOnlineCheck;
    private final int maxHostsLimit;
    private final int maxWorkersPerHost;
    private final int timeoutInMillis;
    private final long delayInNanos;

    public Scanner(
            int socketLimit,
            int timeoutInMillis,
            int delayInMillis,
            int maxWorkersPerHost,
            int maxHostsLimit,
            boolean disableOnlineCheck,
            boolean bannerRecognition
    ) {
        this.delayInNanos = TimeUnit.MILLISECONDS.toNanos(Math.max(0, delayInMillis));
        this.bannerRecognition = bannerRecognition;
        this.disableOnlineCheck = disableOnlineCheck;
        this.maxHostsLimit = maxHostsLimit;
        this.maxWorkersPerHost = maxWorkersPerHost;
        this.socketLimit = new Semaphore(socketLimit);
        this.timeoutInMillis = timeoutInMillis;
    }

    public Scanner(ScanOptions options) {
        this(options.socketLimit(), options.timeoutInMillis(), options.delayInMillis(), options.maxWorkersPerHost(), options.maxHostsLimit(), options.disableOnlineCheck(), options.bannerRecognition());
    }

    /**
     * Starts a given scan.
     *
     * @param scan
     */
    public void runScan(final Scan scan, final Consumer<ScanResult> consumer) {
        if (scan == null) {
            throw new IllegalArgumentException("Scan argument cannot be null");
        }

        scan.start();

        // start producer thread
        final ProducerState<ScanResult> state = startProducer(
                scan.getHosts().iterator(),
                maxHostsLimit,
                host -> new ScanHostTask(scan, host),
                scan.getThreadCounter()
        );

        // the main thread is the consumer
        // Let the consumer run as long as the producer runs
        // or if still tasks are pending in pipeline
        // we do not listen to canceled here because we want
        // all results (also partial) collected for the consumer
        // with whatever is there already
        while (state.running().get() || state.inPipeline().get() > 0) {
            try {
                ScanResult result = getHostResult(state);
                if (result == null) {
                    continue;
                }
                consumer.accept(result);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }

        scan.stop();
    }

    private ScanResult getHostResult(ProducerState<ScanResult> state) throws InterruptedException {
        // let's check for results and give add them to our scan data holder object
        Future<ScanResult> finishedHost = state.completionService().poll(10, TimeUnit.MILLISECONDS);
        if (finishedHost == null) {
            return null;
        }

        ScanResult result = null;
        try {
            result = finishedHost.get();
        } catch (ExecutionException e) {
            System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
        } finally {
            // No matter what happens we have to free the resources after getting ScanResult
            state.activeWorkers().release();
            state.inPipeline().decrementAndGet();
        }

        return result;
    }

    /**
     * This method helps to cleanup the code a bit and remove redundancy
     * The producer for providing hosts and the one for providing ports
     * are similar and the small differences can be handled using a function
     *
     * @param queue
     * @param maxWorkers
     * @param taskFactory
     * @param <INPUT>
     * @param <OUTPUT>
     * @return
     */
    private <INPUT, OUTPUT> ProducerState<OUTPUT> startProducer(Iterator<INPUT> queue, int maxWorkers, Function<INPUT, Callable<OUTPUT>> taskFactory, Counter threadCounter) {
        final AtomicInteger inPipeline = new AtomicInteger(0);
        final AtomicBoolean running = new AtomicBoolean(true);
        final Semaphore activeWorkers = new Semaphore(maxWorkers);
        CompletionService<OUTPUT> completionService = new ExecutorCompletionService<>(executor);
        executor.submit(() -> {
            try {
                threadCounter.inc();
                while (!cancelled && queue.hasNext()) {
                    activeWorkers.acquire();

                    // remember if we submitted anything
                    // so we can release the semaphore
                    boolean submitted = false;
                    try {
                        // just in case something changed while waiting
                        // for the semaphore
                        if (cancelled) {
                            break;
                        }

                        // get next item and create callable
                        // using lambda expression
                        final INPUT item = queue.next();
                        Callable<OUTPUT> task = taskFactory.apply(item);
                        inPipeline.incrementAndGet();
                        try {
                            // here we are filling the completion service
                            // host <-> virtual thread
                            completionService.submit(task);
                            submitted = true;
                        } catch (Throwable e) {
                            inPipeline.decrementAndGet();
                            throw e;
                        }
                    } finally {
                        if (!submitted) {
                            activeWorkers.release();
                        }
                    }
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                threadCounter.dec();
                running.set(false);
            }
        });
        return new ProducerState<>(running, inPipeline, activeWorkers, completionService);
    }

    public boolean awaitTermination(Duration duration) throws InterruptedException {
        return executor.awaitTermination(duration.toMillis(), TimeUnit.MILLISECONDS);
    }

    public void cancel() {
        if (!cancelled) {
            cancelled = true;
        }
        executor.shutdown();
    }

    public void cancelNow() {
        if (!cancelled) {
            cancelled = true;
        }
        executor.shutdownNow();
    }

    @Override
    public void close() throws Exception {
        cancel();
    }

    private final class ScanHostTask implements Callable<ScanResult> {
        private final Scan scan;
        private final String host; // input parameter

        ScanHostTask(Scan scan, String host) {
            this.scan = scan;
            this.host = host;
        }

        @Override
        public ScanResult call() {
            try {
                scan.getThreadCounter().inc();
                scan.getHostCounter().inc();
                scan.getHostTotalCounter().inc();
                if (cancelled) {
                    return null;
                }
                return scanHostPorts();
            } finally {
                scan.getThreadCounter().dec();
                scan.getHostCounter().dec();
            }
        }

        /**
         * Scans the ports of a given host
         *
         * @return
         */
        private ScanResult scanHostPorts() {
            if (!disableOnlineCheck && !checkHostOnline(host)) {
                // Unreachable host
                return new ScanResult(
                        host,
                        new BitSet(PortRange.MAX_PORT),
                        new BitSet(PortRange.MAX_PORT),
                        new HashMap<>(),
                        List.of()
                );
            }

            final PortRange portRange = new PortRange(scan.getPorts());
            final AtomicLong portSlotFactory = new AtomicLong(System.nanoTime());
            // producer thread
            ProducerState<PortResult> state = startProducer(
                    portRange.iterator(),
                    maxWorkersPerHost,
                    port -> new ScanPortTask(host, port, scan, portSlotFactory),
                    scan.getThreadCounter()
            );

            // consumer is the main thread
            // we run as long as the producer is running or as long as things are in pipeline to be processed
            // only exception is when cancelled is set
            final PortResultAccumulator accumulator = new PortResultAccumulator(host);
            while (!cancelled && (state.running().get() || state.inPipeline().get() > 0)) {
                try {
                    PortResult portResult = getPortResult(state);
                    if (portResult == null) {
                        continue;
                    }

                    accumulator.add(portResult);
                } catch (InterruptedException ignored) {
                    Thread.currentThread().interrupt();
                }
            }

            return accumulator.build();
        }

        private boolean checkHostOnline(String host) {
            try {
                return InetAddress.getByName(host).isReachable(timeoutInMillis);
            } catch (IOException e) {
                // something went wrong
            }

            return false;
        }

        private PortResult getPortResult(ProducerState<PortResult> state) throws InterruptedException {
            Future<PortResult> portResultFuture = state.completionService().poll(10, TimeUnit.MILLISECONDS);
            if (portResultFuture == null) {
                return null;
            }

            PortResult portResult = null;
            try {
                portResult = portResultFuture.get();
            } catch (ExecutionException e) {
                // something more serious did not work
                System.err.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage());
            } finally {
                state.activeWorkers().release();
                state.inPipeline().decrementAndGet();
            }
            return portResult;
        }
    }

    private final class ScanPortTask implements Callable<PortResult> {
        private final Scan scan;
        private final String host;
        private final int port;
        private final AtomicLong portSlotFactory;

        private ScanPortTask(String host, int port, Scan scan, AtomicLong portSlotFactory) {
            this.scan = scan;
            this.host = host;
            this.port = port;
            this.portSlotFactory = portSlotFactory;
        }

        @Override
        public PortResult call() throws Exception {
            try {
                scan.getThreadCounter().inc();
                waitForSlot(portSlotFactory);
                if (cancelled) {
                    return null;
                }
                return checkPort();
            } finally {
                scan.getThreadCounter().dec();
            }
        }

        private void waitForSlot(AtomicLong scanSlotFactory) throws InterruptedException {
            if (delayInNanos > 0) {
                long slot = scanSlotFactory.getAndAdd(delayInNanos);
                long wait = slot - System.nanoTime();
                if (wait > 0)
                    Thread.sleep(Duration.ofNanos(wait));
            }
        }

        private PortResult checkPort() throws InterruptedException {
            PortResult result = new PortResult();
            result.setPort(port);
            result.setState(PortState.UNKNOWN);

            // don't allow more sockets then specified
            socketLimit.acquire();
            // count how many ports are concurrently checked
            // todo object/data asymmetry
            scan.getSocketCounter().inc();
            try (Socket socket = new Socket()) {
                socket.connect(new InetSocketAddress(host, port), timeoutInMillis);
                result.setState(PortState.OPEN);
                if (bannerRecognition) {
                    result.setBanner(getBanner(socket));
                }
            } catch (SocketTimeoutException ignored) {
                result.setState(PortState.FILTERED);
            } 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);
            } finally {
                scan.getSocketCounter().dec();
                socketLimit.release();
            }

            return result;
        }

        private String getBanner(Socket socket) {
            byte[] buffer = new byte[READ_BUFFER_SIZE];
            try (InputStream input = socket.getInputStream()) {
                socket.setSoTimeout(timeoutInMillis);
                int bytesRead = input.read(buffer);
                if (bytesRead <= 0) {
                    return null;
                }
                return new String(buffer, 0, bytesRead, StandardCharsets.UTF_8).trim();
            } catch (IOException e) {
                // we ignore this failure
            }
            return null;
        }
    }

    private final class PortResultAccumulator {
        private final String host;
        private final BitSet openPorts = new BitSet(PortRange.MAX_PORT);
        private final BitSet filteredPorts = new BitSet(PortRange.MAX_PORT);
        private final Map<Integer, ServiceType> serviceTypes = new HashMap<>();
        private final List<ScanFailure> scanFailures = new ArrayList<>();

        private PortResultAccumulator(String host) {
            this.host = host;
        }

        void add(PortResult portResult) {
            switch (portResult.getState()) {
                case OPEN -> {
                    openPorts.set(portResult.getPort());
                    serviceTypes.put(portResult.getPort(), ServiceDetector.detect(portResult.getBanner()));
                }
                case FILTERED -> {
                    filteredPorts.set(portResult.getPort());
                }
                default -> {
                    // intentional no-op for uncovered port states
                }
            }
            Exception e = portResult.getException();
            if (e != null) {
                scanFailures.add(new ScanFailure(portResult.getPort(), ExceptionInfo.from(e)));
            }
        }

        ScanResult build() {
            return new ScanResult(host, openPorts, filteredPorts, Collections.unmodifiableMap(serviceTypes), Collections.unmodifiableList(scanFailures));
        }
    }
}