summaryrefslogtreecommitdiff
path: root/src/main/java/com/it_jaros/networkScanner/Scanner.java
blob: fae18902bde45cdc253bda9b4f8b66c3664a0b68 (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
package com.it_jaros.networkScanner;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.LockSupport;

public class Scanner {
                
    private final Counter counter = new Counter();
    private final ProgressBar progressBar = new ProgressBar();
    private final Semaphore socketCap;
    private final int timeoutInMillis;
    
    public Scanner(int socketLimit, int timeoutInMillis) {
        this.timeoutInMillis = timeoutInMillis;
        socketCap = new Semaphore(socketLimit);
    }

    public Scanner() {
        this(1000, 1000);
    }

    public List<ScanResult> scanPorts(ScanOptions target) {
        return scanPorts(target.targets(), target.delayInMillis());
    }

    public List<ScanResult> scanPorts(List<String> targets, int delayInMillis) {
        List<ScanResult> results = new ArrayList<>();
        long delayInNanos = TimeUnit.MILLISECONDS.toNanos(Math.max(0, delayInMillis));
        try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();) {
            List<Progress> hosts = new ArrayList<>();
            for (String target : targets) {
                Progress progress = new Progress(target, 65535, new AtomicInteger(), new AtomicInteger());
                hosts.add(progress);
                results.add(scanTargetAsync(target, delayInNanos, executor, progress));
            }
            progressBar.start(hosts);
        }

        progressBar.stop();
        System.out.println("Peak concurrent connects: " + counter.max());
        return results;
    }

    private ScanResult scanTargetAsync(String target, long delayInNanos, ExecutorService executor, Progress p) {
        AtomicLong nextStartNanos = new AtomicLong(System.nanoTime());
        Queue<Integer> openPorts = new ConcurrentLinkedQueue<>();
        for (int port = 1; port < 65536; port++) {
            final int currentPort = port;
            executor.submit(() -> {
                try {
                    scanPortsAsync(target, delayInNanos, nextStartNanos, openPorts, currentPort);
                } finally {
                    progressBar.onPortFinished(p);
                }
            });
        }
        return new ScanResult(target, openPorts);
    }

    private void scanPortsAsync(String target, long delayInNanos, AtomicLong nextStartNanos, Queue<Integer> openPorts,
            int currentPort) {
        if (delayInNanos > 0) {
            long mySlot = nextStartNanos.getAndAdd(delayInNanos);
            long wait = mySlot - System.nanoTime();
            if (wait > 0)
                LockSupport.parkNanos(wait);
        }
        socketCap.acquireUninterruptibly();
        counter.inc();
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(target, currentPort), timeoutInMillis);
            openPorts.add(currentPort);
        } catch (ConnectException | SocketTimeoutException ignored) {
        } catch (IOException e) {
            System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage());
        } finally {
            counter.dec();
            socketCap.release();
        }
    }
}