Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

2.7 KB
Raw Blame
package opensuite.target;

import java.util.function.Consumer;

/**
 * The switch and the tally for the live passive crawl, shared by the site map
 * (which does the work as traffic arrives) and the Dashboard task that shows
 * and controls it.
 */
public final class PassiveCrawl {
    private PassiveCrawl() {}

    private static volatile boolean enabled = true;
    private static volatile Consumer<String> log = s -> {};
    private static volatile Runnable changed = () -> {};
    private static final java.util.concurrent.atomic.AtomicInteger added = new java.util.concurrent.atomic.AtomicInteger();
    private static final java.util.concurrent.atomic.AtomicInteger processed = new java.util.concurrent.atomic.AtomicInteger();

    public static boolean enabled() { return enabled; }
    public static void setEnabled(boolean on) { enabled = on; }

    /** Where the crawl reports what it added, and how it says its figures moved. */
    public static void listen(Consumer<String> logLine, Runnable onChange) {
        log = logLine == null ? s -> {} : logLine;
        changed = onChange == null ? () -> {} : onChange;
    }

    public static int itemsAdded() { return added.get(); }
    public static int responsesProcessed() { return processed.get(); }
    public static void reset() { added.set(0); processed.set(0); items.clear(); }

    /** What the crawl has added, newest last: host, method, URL, status code, MIME type. */
    private static final java.util.List<String[]> items = new java.util.concurrent.CopyOnWriteArrayList<>();
    public static java.util.List<String[]> items() { return items; }

    private static void record(String host, String method, String path, String status, String mime) {
        if (items.size() < 5000) items.add(new String[] { host, method, path, status, mime });
    }

    static void responseProcessed(opensuite.core.LogEntry e) {
        record(e.service().host(), e.request() == null ? "" : e.request().method(), e.pathAndQuery(),
                e.status() == 0 ? "" : String.valueOf(e.status()), e.mimeType() == null ? "" : e.mimeType());
        responseProcessed(e.url());
    }

    static void linkAdded(String link, String from, String host, String path) {
        record(host, "", path, "", "");
        linkAdded(link, from);
    }

    static void responseProcessed(String url) {
        processed.incrementAndGet();
        added.incrementAndGet();
        log.accept("Adding response " + url + " to site map");
        changed.run();
    }

    static void linkAdded(String link, String from) {
        added.incrementAndGet();
        log.accept("Adding link " + link + " in response " + from + " to site map");
        changed.run();
    }
}