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.

3.8 KB
Raw Blame
package opensuite.proxy;

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

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/** Edits the TLS pass-through list under the proxy settings. */
class TlsPassThroughPanel extends JPanel {

    private final SuiteContext ctx;
    private final DefaultTableModel model = new DefaultTableModel(new Object[]{"Host / port"}, 0) {
        @Override public boolean isCellEditable(int r, int c) { return false; }
    };
    private final JTable table = new JTable(model);

    TlsPassThroughPanel(SuiteContext ctx) {
        super(new BorderLayout());
        this.ctx = ctx;
        setOpaque(false);

        Ui.styleTable(table);
        JScrollPane sp = Ui.scroll(table);

        JButton add = Ui.button("Add");
        JButton edit = Ui.button("Edit");
        JButton remove = Ui.button("Remove");
        JButton paste = Ui.button("Paste", "Add one entry per line from the clipboard");
        add.addActionListener(e -> prompt(-1));
        edit.addActionListener(e -> prompt(table.getSelectedRow()));
        remove.addActionListener(e -> {
            List<String> cur = rows();
            int[] sel = table.getSelectedRows();
            for (int i = sel.length - 1; i >= 0; i--) cur.remove(sel[i]);
            TlsPassThrough.setEntries(ctx.settings(), cur);
        });
        paste.addActionListener(e -> {
            try {
                String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard()
                        .getData(java.awt.datatransfer.DataFlavor.stringFlavor);
                if (data == null) return;
                for (String line : data.split("\r\n|\r|\n")) TlsPassThrough.add(ctx.settings(), line);
            } catch (Exception ignored) {}
        });

        JCheckBox auto = Ui.check("Automatically add entries on client TLS negotiation failure",
                TlsPassThrough.autoAdd(ctx.settings()));
        auto.addActionListener(e -> ctx.settings().setBool(TlsPassThrough.AUTO_ADD_KEY, auto.isSelected()));

        Ui.sizedAlike(87, add, edit, remove, paste);
        auto.setBorder(Ui.pad(0, 2, 0, 0));
        auto.setPreferredSize(new Dimension(auto.getPreferredSize().width + 2, 20));
        add(opensuite.ui.Sections.column(0,
                opensuite.ui.Sections.besideTable(Ui.buttonColumn(add, edit, remove, paste), opensuite.ui.Sections.withArrow(sp, 516, 150)),
                opensuite.ui.Sections.gap(5), auto), BorderLayout.CENTER);

        ctx.settings().addListener(key -> {
            if (TlsPassThrough.LIST_KEY.equals(key)) SwingUtilities.invokeLater(this::reload);
            if (TlsPassThrough.AUTO_ADD_KEY.equals(key))
                SwingUtilities.invokeLater(() -> auto.setSelected(TlsPassThrough.autoAdd(ctx.settings())));
        });
        reload();
    }

    private List<String> rows() {
        return new ArrayList<>(TlsPassThrough.entries(ctx.settings()));
    }

    private void reload() {
        model.setRowCount(0);
        for (String e : TlsPassThrough.entries(ctx.settings())) model.addRow(new Object[]{e});
    }

    private void prompt(int row) {
        List<String> cur = rows();
        if (row >= cur.size()) return;
        String v = (String) JOptionPane.showInputDialog(this,
                "Host, with an optional port (for example *.example.com or api.example.com:8443):",
                row < 0 ? "Add TLS pass-through target" : "Edit TLS pass-through target",
                JOptionPane.PLAIN_MESSAGE, null, null, row < 0 ? "" : cur.get(row));
        if (v == null) return;
        v = TlsPassThrough.normalise(v);
        if (v.isEmpty()) return;
        if (row < 0) { TlsPassThrough.add(ctx.settings(), v); return; }
        cur.set(row, v);
        TlsPassThrough.setEntries(ctx.settings(), cur);
    }
}