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.

13.3 KB
Raw Blame
package opensuite.ui.settings;

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

import javax.swing.*;
import java.awt.*;

/** Settings > Suite pages: REST API, Updates, Performance, Temporary files location, Startup behavior, Shutdown behavior. */
public final class SuitePages {
    private SuitePages() {}


    public static SettingsPage restApi(SuiteContext ctx) {
        Settings s = ctx.settings();
        SettingsPage p = new SettingsPage();
        String def = opensuite.core.RestApi.DEFAULT_URL;
        JLabel url = opensuite.ui.Sections.caption("Service URL: " + s.get("rest.url", def));
        JButton change = Ui.button("Change...");
        JCheckBox running = SettingsPage.check(s, "rest.running", false, "Service running");
        JLabel state = opensuite.ui.Sections.caption("");
        state.setForeground(opensuite.ui.Theme.ACCENT);
        // The service follows the box; if it could not start, the reason is shown and the box says so.
        Runnable show = () -> SwingUtilities.invokeLater(() -> {
            String problem = ctx.restApi().problem();
            state.setText(problem.isEmpty() ? "" : "Could not start: " + problem);
        });
        running.addActionListener(e -> show.run());
        change.addActionListener(e -> {
            String v = JOptionPane.showInputDialog(change, "Service URL (the address and port the REST API listens on):", s.get("rest.url", def));
            if (v == null || v.isBlank()) return;
            try {
                java.net.URI u = java.net.URI.create(v.strip());
                if (u.getHost() == null || !"http".equalsIgnoreCase(u.getScheme())) throw new IllegalArgumentException();
            } catch (IllegalArgumentException ex) {
                JOptionPane.showMessageDialog(change, "Enter the service URL as http://address:port, for example " + def, "Service URL", JOptionPane.ERROR_MESSAGE);
                return;
            }
            s.set("rest.url", v.strip());
            url.setText("Service URL: " + v.strip());
            show.run();
        });
        KeyedTable keys = new KeyedTable(s, "rest.keys", new String[]{"Enabled", "Name", "Created", "Key"}, 150, "Add", "Edit", "Remove")
                .labels("New", "Edit", "Delete").hide("Key").lock("Created", "Key")
                .size(378, 150).buttons(79, 29, 5).widths(46, 180, 150);
        keys.adder(() -> {
            String name = JOptionPane.showInputDialog(keys, "Name for the new API key:", "New API key", JOptionPane.PLAIN_MESSAGE);
            if (name == null || name.isBlank()) return null;
            String key = opensuite.core.RestApi.newKey();
            JTextField shown = Ui.field(key);
            shown.setEditable(false);
            shown.setColumns(30);
            JButton copy = Ui.button("Copy to clipboard");
            copy.addActionListener(e -> Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new java.awt.datatransfer.StringSelection(key), null));
            JPanel box = Ui.vbox(Ui.label("The key is displayed only once. Copy it now. It is included in the path of each request:"),
                    Ui.row(shown, copy), Ui.label(s.get("rest.url", def) + "/" + key + "/v0.1/"));
            box.setBorder(Ui.pad(10, 10, 10, 10));
            JOptionPane.showMessageDialog(keys, box, "New API key", JOptionPane.PLAIN_MESSAGE);
            return new String[] { "true", name.strip(), new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm").format(new java.util.Date()), opensuite.core.RestApi.hash(key) };
        });
        JLabel heading = Ui.heading("API Keys");
        JComponent configure = (JComponent) opensuite.ui.Sections.text("Use these settings to configure the REST API.");
        configure.setBorder(Ui.pad(1, 0, 0, 0));
        // The reference's heading row is 4px taller here (it carries a button we have no use for).
        JPanel block = p.block("REST API", SettingsPage.Kind.USER,
                "The REST API can be used by other tools to integrate with Open Suite. The REST API exposes sensitive functionality and data, so "
              + "keep it on the loopback address and require an API key unless you have a reason not to.",
                () -> { SettingsPage.clearPrefix(s, "rest."); running.setSelected(false); },
                configure,
                opensuite.ui.Sections.gap(6),
                opensuite.ui.Sections.text("Once configured, the list of endpoints is at: [Service URL]/[API key]/v0.1/"),
                opensuite.ui.Sections.gap(5),
                SettingsPage.line(22, url, 20, change, 10, state),
                running,
                SettingsPage.check(s, "rest.noKey", false, "Allow access without API key (using this option is not secure)"),
                opensuite.ui.Sections.gap(10), heading, opensuite.ui.Sections.gap(0), keys);
        block.setBorder(BorderFactory.createCompoundBorder(Ui.pad(4, 0, 0, 0), block.getBorder()));
        return p;
    }

    public static SettingsPage updates(SuiteContext ctx) {
        Settings s = ctx.settings();
        SettingsPage p = new SettingsPage();
        JButton check = Ui.primaryButton("Check for updates", "open the project's releases page");
        check.addActionListener(e -> {
            // Stable looks at the latest full release; early adopters see every release, pre-releases too.
            String page = "Stable".equals(s.get("updates.channel", "Stable")) ? "/releases/latest" : "/releases";
            try { Desktop.getDesktop().browse(java.net.URI.create(opensuite.ui.Unbuilt.PROJECT_URL + page)); } catch (Exception ignore) { }
        });
        JComboBox<String> channel = SettingsPage.combo(s, "updates.channel", new String[]{"Stable", "Early Adopter"}, "Stable");
        Ui.sized(channel, 106);
        JLabel channelLabel = opensuite.ui.Sections.caption("Channel:");
        channelLabel.setPreferredSize(new Dimension(61, 18));
        // There is no installer build that updates itself, so as in the reference outside one, the box is off.
        JCheckBox auto = Ui.check("Enable auto-updates (requires restart)", false);
        auto.setEnabled(false);
        p.block("Updates [installer version only]", SettingsPage.Kind.USER,
                "Use this setting to select the update channel that Open Suite should use.",
                () -> SettingsPage.clearPrefix(s, "updates."),
                auto,
                opensuite.ui.Sections.gap(6), OptionsPage.indent(SettingsPage.line(22, channelLabel, channel), 10), opensuite.ui.Sections.gap(11),
                check);
        return p;
    }

    public static SettingsPage performance(SuiteContext ctx) {
        Settings s = ctx.settings();
        SettingsPage p = new SettingsPage();
        String id = s.get("perf.debugId", "");
        if (id.isEmpty()) { id = java.util.UUID.randomUUID().toString(); s.set("perf.debugId", id); }
        JTextField debugId = Ui.field(id);
        debugId.setEditable(false);
        JButton copy = Ui.button("Copy");
        Ui.sized(copy, 73);
        copy.addActionListener(e -> Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new java.awt.datatransfer.StringSelection(debugId.getText()), null));
        JButton email = Ui.button("Email support to report a bug");
        email.addActionListener(e -> opensuite.ui.Unbuilt.openProject(email));
        Ui.sized(debugId, 264);
        JComponent[] logDir = SettingsPage.path(s, "perf.logDir", System.getProperty("user.home") + "/.opensuite/logs", true, "Choose folder ...", 315);
        // Open Suite has nowhere to send feedback to and never sends any: the box is there, and stays off.
        JCheckBox submit = Ui.check("Automatically submit feedback about Open Suite's performance", false);
        submit.setEnabled(false);
        submit.setToolTipText("Open Suite has no feedback service and sends no data. Use the local log below instead.");
        p.block("Performance feedback", SettingsPage.Kind.USER,
                "You can help improve Open Suite by submitting feedback about its performance. Your feedback contains technical information about Open Suite's internal functioning.",
                () -> s.setBool("perf.logLocal", false),
                OptionsPage.indent(submit, 15),
                opensuite.ui.Sections.gap(8),
                opensuite.ui.Sections.text("You can also report a performance problem on the project's issue tracker. To help diagnose a problem specific to your "
                        + "instance, please include your debug ID. This identifies the session in which the problem occurred."),
                opensuite.ui.Sections.gap(5),
                SettingsPage.line(22, opensuite.ui.Sections.caption("Debug ID:"), 5, debugId, 5, copy),
                opensuite.ui.Sections.gap(14), email, opensuite.ui.Sections.gap(15),
                opensuite.ui.Sections.text("You can log performance data to a local directory. Attach the resultant file to your report to help diagnose the problem."),
                opensuite.ui.Sections.gap(5),
                OptionsPage.indent(SettingsPage.line(22, SettingsPage.check(s, "perf.logLocal", false, "Log performance data to a local directory:"), 5, logDir[0], 5, logDir[1]), 17), opensuite.ui.Sections.gap(0));
        long maxMb = Runtime.getRuntime().maxMemory() / (1024 * 1024);
        int half = opensuite.core.Launch.halfOfSystemMemoryMb();
        JRadioButton[] r = SettingsPage.radios(s, "perf.memory.mode", "half", new String[]{"half", "custom"}, "Set to half of system memory", "Set custom value:");
        JTextField custom = SettingsPage.number(s, "perf.memory.mb", half, 8);
        Ui.sized(custom, 93);
        Runnable show = () -> custom.setEnabled(r[1].isSelected());
        for (JRadioButton b : r) b.addItemListener(e -> show.run());
        show.run();
        p.restoreMark().block("Maximum memory usage", SettingsPage.Kind.USER,
                "Set the maximum system memory available to Open Suite. Changes apply at the next start. The limit for this session is " + maxMb + " MB.",
                () -> { r[0].setSelected(true); s.set("perf.memory.mode", "half"); custom.setText(String.valueOf(half)); },
                opensuite.ui.Sections.text("A higher limit improves performance for large projects and long audits, at the expense of other processes on the system.", 1 + Ui.fontGrowth()),
                opensuite.ui.Sections.gap(6 + Ui.fontGrowth()),
                opensuite.ui.Sections.text("Adjust this limit to balance performance with your system's needs:\n"
                        + "    \u2022  Decrease it if Open Suite's memory use is impacting your system performance.\n"
                        + "    \u2022  Increase it to speed up Open Suite when working on large projects or if you have spare RAM."),
                opensuite.ui.Sections.gap(21 + Ui.fontGrowth()),
                r[0], SettingsPage.line(22, r[1], 9, custom, 5, opensuite.ui.Sections.caption("MB")));
        ProjectExtras.memory(p, ctx);
        return p;
    }

    public static SettingsPage tempFiles(SuiteContext ctx) {
        Settings s = ctx.settings();
        SettingsPage p = new SettingsPage();
        JRadioButton[] r = SettingsPage.radios(s, "temp.mode", "default", new String[]{"default", "custom"}, "Use default system temp directory", "Use custom location:");
        JComponent[] dir = SettingsPage.path(s, "temp.dir", "", true, "Choose folder ...", 214);
        p.block("Temporary files location", SettingsPage.Kind.USER,
                "Use this setting to configure where Open Suite should store temporary files. Changes will take effect when you restart Open Suite.",
                () -> { s.set("temp.mode", "default"); s.set("temp.dir", ""); },
                r[0], SettingsPage.line(22, r[1], 7, dir[0], 5, dir[1]));
        return p;
    }

    public static SettingsPage startup(SuiteContext ctx) {
        Settings s = ctx.settings();
        SettingsPage p = new SettingsPage();
        JCheckBox pause = SettingsPage.check(s, "startup.pauseTasks", true, "<html>Select <b>Pause automated tasks</b> by default</html>");
        p.block("Automated tasks on startup", SettingsPage.Kind.USER,
                "Use this setting to control whether the \"Pause automated tasks\" checkbox is selected by default when you open a project at startup.",
                () -> s.setBool("startup.pauseTasks", true),
                pause);
        p.restoreMark().block("Unrecognized project files", SettingsPage.Kind.USER,
                "Use this setting to configure whether a project file from a different installation of Open Suite should be automatically trusted or not.\n"
              + "By opting not to trust these project files, we will open the project with any potentially harmful settings removed.",
                () -> s.setBool(opensuite.ui.StateIO.ALWAYS_TRUST, true),
                SettingsPage.check(s, opensuite.ui.StateIO.ALWAYS_TRUST, true, "Always trust project files"));
        return p;
    }

    public static SettingsPage shutdown(SuiteContext ctx) {
        Settings s = ctx.settings();
        SettingsPage p = new SettingsPage();
        p.block("Open Suite's closing behavior", SettingsPage.Kind.USER,
                "Use this setting to control whether a confirmation alert appears when you close Open Suite.",
                () -> s.setBool("suite.noConfirmClose", false),
                SettingsPage.check(s, "suite.noConfirmClose", false, "Don't confirm before closing Open Suite"));
        return p;
    }
}