package com.it_jaros.network_scanner; import java.io.IOException; import java.net.*; import java.util.ArrayList; import java.util.BitSet; import java.util.List; import java.util.concurrent.*; 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 socketLimit; private final int timeoutInMillis; private final long delayInNanos; private final int maxWorkersPerHost; private final int maxTargetsLimit; public Scanner(int socketLimit, int timeoutInMillis, int delayInMillis, int maxWorkersPerHost, int maxTargetsLimit) { this.timeoutInMillis = timeoutInMillis; this.delayInNanos = TimeUnit.MILLISECONDS.toNanos(Math.max(0, delayInMillis)); this.socketLimit = new Semaphore(socketLimit); this.maxWorkersPerHost = maxWorkersPerHost; this.maxTargetsLimit = maxTargetsLimit; } public List scan(ScanOptions target) { return scanTargets(target.targets(), target.ports()); } public List scanTargets(List targets, String ports) { List results = new ArrayList<>(); progressBar.start(); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { List> futures = new ArrayList<>(); Semaphore maxTargets = new Semaphore(maxTargetsLimit); for (String target : targets) { maxTargets.acquireUninterruptibly(); futures.add(executor.submit(() -> { try { return scanTarget(target, ports); } finally { maxTargets.release(); } })); } futures.forEach((Future f) -> { try { results.add(f.get()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { System.out.printf("%s -> %s%n", e.getClass().getSimpleName(), e.getMessage()); } }); } progressBar.stop(); System.out.println("--------------------"); System.out.println("Finished scan!"); System.out.println("Stats:"); System.out.println("Peak concurrent connects: " + counter.max()); System.out.println("--------------------"); return results; } private ScanResult scanTarget(String target, String ports) { PortRange portRange = new PortRange(ports); AtomicLong portSlotFactory = new AtomicLong(System.nanoTime()); Semaphore maxWorkers = new Semaphore(maxWorkersPerHost); BitSet openPorts = new BitSet(PortRange.MAX_PORT); BitSet filteredPorts = new BitSet(PortRange.MAX_PORT); Progress progress = new Progress(target, portRange.getTotal(), new AtomicInteger(), new AtomicInteger(), new AtomicInteger() ); progressBar.submit(progress); try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { List> futures = new ArrayList<>(); while (portRange.hasNext()) { maxWorkers.acquireUninterruptibly(); final int currentPort = portRange.next(); futures.add(executor.submit(() -> { try { waitForSlot(portSlotFactory); PortState state = getPortState(target, currentPort); if (state.equals(PortState.OPEN)) { progress.open().incrementAndGet(); } else if (state.equals(PortState.FILTERED)) { progress.filtered().incrementAndGet(); } return new PortResult(currentPort, state); } finally { maxWorkers.release(); progress.done().incrementAndGet(); } })); } List errors = new ArrayList<>(); for (Future f : futures) { try { // bitset is not thread-safe, so it is set // outside the other virtual threads that update progress PortResult portResult = f.get(); if (portResult.state().equals(PortState.OPEN)) { openPorts.set(portResult.port()); } else if (portResult.state().equals(PortState.FILTERED)) { filteredPorts.set(portResult.port()); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (ExecutionException e) { errors.add(e); } } if (!errors.isEmpty()) { System.out.printf("Errors happened during scan of target %s%nErrors:%s -> %s", target, errors.size(), errors); } } return new ScanResult(target, openPorts, filteredPorts); } private void waitForSlot(AtomicLong scanSlotFactory) { if (delayInNanos > 0) { long slot = scanSlotFactory.getAndAdd(delayInNanos); long wait = slot - System.nanoTime(); if (wait > 0) LockSupport.parkNanos(wait); } } private PortState getPortState(String target, int port) { socketLimit.acquireUninterruptibly(); counter.inc(); try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(target, port), timeoutInMillis); return PortState.OPEN; } catch (NoRouteToHostException ignored) { // this can be safely ignored because the port is closed if a host is unreachable } catch (SocketTimeoutException ignored) { return PortState.FILTERED; } catch (ConnectException ignored) { return PortState.CLOSED; } catch (IOException ignored) { // Will happen a lot when scanning for open ports, so not needed } finally { counter.dec(); socketLimit.release(); } return PortState.CLOSED; } }