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.

6.2 KB
Raw Blame
package opensuite.proxy;

import opensuite.core.SuiteContext;
import opensuite.ui.Ui;

import javax.swing.*;
import java.awt.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Opens the browser through the proxy and, when there is no browser on the
 * machine at all, fetches Open Suite's own first: one question, a progress bar,
 * then the browser opens. Nothing is left for the user to install by hand.
 */
public final class BrowserSetup {

    private BrowserSetup() {}

    public static void open(Component parent, SuiteContext ctx, ProxyServer server) {
        boolean relax = ctx.settings().getBool(BrowserFlags.RELAX_KEY, true);
        String err = EmbeddedBrowser.launch(server, "http://opensuite/", relax);
        if (err == null) {
            ctx.alert("proxy", "launched browser through the proxy, " + BrowserFlags.describe(relax));
        } else if (EmbeddedBrowser.NO_BROWSER.equals(err) && BrowserInstaller.supported()) {
            fetchThenOpen(parent, ctx, server);
        } else {
            JOptionPane.showMessageDialog(parent, EmbeddedBrowser.NO_BROWSER.equals(err)
                    ? "No Chromium-based browser was found, and the bundled browser supports " + BrowserInstaller.PLATFORMS + " only." : err,
                    "Open browser", JOptionPane.WARNING_MESSAGE);
        }
    }

    private static void fetchThenOpen(Component parent, SuiteContext ctx, ProxyServer server) {
        Window owner = parent instanceof Window w ? w : SwingUtilities.getWindowAncestor(parent);
        fetchThen(owner, ctx, "Open browser",
                "Open Suite's browser is not installed.\nDownload it now? The download is about 250 MB and is required only once.", () -> {
            String err = EmbeddedBrowser.launch(server, "http://opensuite/", ctx.settings().getBool(BrowserFlags.RELAX_KEY, true));
            if (err != null) JOptionPane.showMessageDialog(owner, err, "Open browser", JOptionPane.WARNING_MESSAGE);
        });
    }

    /**
     * A scan drives the browser: it crawls the pages JavaScript builds. Where
     * the browser is missing the scan does not start weaker without it; the
     * browser is fetched first, and the scan starts when it is there.
     *
     * @return true when the browser is there and the scan can start now
     */
    public static boolean readyOrFetch(Window owner, SuiteContext ctx, Runnable startScan) {
        if (EmbeddedBrowser.readyForScans()) return true;
        if (!BrowserInstaller.supported()) {
            JOptionPane.showMessageDialog(owner, "The scan needs Open Suite's browser, which supports " + BrowserInstaller.PLATFORMS + " only.",
                    "New scan", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        fetchThen(owner, ctx, "New scan",
                "The scan needs Open Suite's browser, which is not installed.\nDownload it now? The download is about 250 MB and is required only once.",
                startScan);
        return false;
    }

    /** Ask, fetch the browser with its progress shown, then carry on with {@code then}. */
    private static void fetchThen(Window owner, SuiteContext ctx, String title, String question, Runnable then) {
        int go = JOptionPane.showOptionDialog(owner, question,
                title, JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                new Object[]{ "Download", "Cancel" }, "Download");
        if (go != 0) return;

        JDialog d = new JDialog(owner, "Downloading browser", Dialog.ModalityType.MODELESS);
        d.setIconImages(opensuite.ui.AppIcon.images());   // its own, set before it is shown
        JLabel step = Ui.label("Starting");
        JProgressBar bar = new JProgressBar(0, 1000);
        bar.setPreferredSize(new Dimension(360, 8));
        JButton cancel = Ui.button("Cancel");
        AtomicBoolean cancelled = new AtomicBoolean();
        cancel.addActionListener(e -> { cancelled.set(true); cancel.setEnabled(false); step.setText("Cancelling"); });
        JPanel p = new JPanel(new BorderLayout(0, 10));
        p.setBorder(Ui.pad(15, 15, 15, 15));
        p.add(step, BorderLayout.NORTH);
        p.add(bar, BorderLayout.CENTER);
        JPanel south = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
        south.add(cancel);
        p.add(south, BorderLayout.SOUTH);
        d.setContentPane(p);
        d.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        d.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override public void windowClosing(java.awt.event.WindowEvent e) { cancel.doClick(); }
        });
        d.pack();
        d.setLocationRelativeTo(owner);
        d.setVisible(true);

        long[] shown = { 0 };
        Thread t = new Thread(() -> {
            String failure = null;
            try {
                BrowserInstaller.install(EmbeddedBrowser.installDir(), (what, done, total) -> {
                    long now = System.currentTimeMillis();
                    if (now - shown[0] < 100 && done != total) return;   // ten updates a second is plenty
                    shown[0] = now;
                    SwingUtilities.invokeLater(() -> {
                        if (cancelled.get()) return;
                        bar.setIndeterminate(total <= 0);
                        if (total > 0) bar.setValue((int) (done * 1000 / total));
                        step.setText(total > 0 ? what + ": " + done / 1_000_000 + " of " + total / 1_000_000 + " MB" : what);
                    });
                }, cancelled::get);
            } catch (Exception ex) {
                failure = String.valueOf(ex.getMessage());
            }
            String result = failure;
            SwingUtilities.invokeLater(() -> {
                d.dispose();
                if (cancelled.get()) return;
                if (result != null) {
                    JOptionPane.showMessageDialog(owner, "The browser could not be downloaded: " + result,
                            title, JOptionPane.WARNING_MESSAGE);
                    return;
                }
                ctx.setBrowserBinary(EmbeddedBrowser.findBinary());
                ctx.alert("proxy", "downloaded Open Suite's browser");
                then.run();
            });
        }, "browser-download");
        t.setDaemon(true);
        t.start();
    }
}