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.

5.8 KB
Raw Blame
package opensuite.proxy;

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

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

/**
 * Configure upstream proxy chaining: a table of rules routing matching
 * destinations through an HTTP (CONNECT) or SOCKS proxy. Rules are saved to
 * settings as they change, so they persist and travel with the project.
 */
public class UpstreamProxyPanel extends JPanel {

    private final SuiteContext ctx;
    private final UpstreamProxyManager mgr;
    private final Model model = new Model();

    public UpstreamProxyPanel(SuiteContext ctx) {
        super(new BorderLayout());
        this.ctx = ctx;
        this.mgr = ctx.upstreamProxies();

        JTable table = new JTable(model);
        Ui.styleTable(table);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        Ui.exactWidths(table, 60, 149, 150, 60, 60, 61);

        JButton add = Ui.button("Add", "Add an upstream proxy rule");
        JButton edit = Ui.button("Edit");
        JButton del = Ui.button("Remove");
        JButton up = Ui.button("Up", "Try the selected rule earlier");
        JButton down = Ui.button("Down", "Try the selected rule later");
        add.addActionListener(e -> { UpstreamProxy p = new UpstreamProxy(); if (editRule(p)) { mgr.rules().add(p); persist(); } });
        edit.addActionListener(e -> { int i = table.getSelectedRow(); if (i >= 0 && editRule(mgr.rules().get(i))) persist(); });
        del.addActionListener(e -> { int i = table.getSelectedRow(); if (i >= 0) { mgr.rules().remove(i); persist(); } });
        up.addActionListener(e -> move(table, -1));
        down.addActionListener(e -> move(table, 1));

        // Five buttons 32px apart beside a table as tall as they are, as the reference sets them.
        JButton[] buttons = { add, edit, del, up, down };
        JPanel column = new JPanel(new GridLayout(buttons.length, 1, 0, 10));
        column.setOpaque(false);
        for (JButton b : buttons) {
            b.setPreferredSize(new Dimension(87 + 4 * Ui.fontGrowth(), Ui.tall(22)));
            column.add(b);
        }
        JPanel top = new JPanel(new BorderLayout());
        top.setOpaque(false);
        top.add(column, BorderLayout.NORTH);
        int height = buttons.length * Ui.tall(22) + (buttons.length - 1) * 10;
        JPanel row = new JPanel(new BorderLayout(10, 0));
        row.setOpaque(false);
        row.add(top, BorderLayout.WEST);
        row.add(opensuite.ui.Sections.withArrow(Ui.scroll(table), 540, height), BorderLayout.CENTER);

        setOpaque(false);
        add(row, BorderLayout.CENTER);
    }

    /** The first rule that matches is used, so their order matters. */
    private void move(JTable table, int by) {
        int i = table.getSelectedRow(), j = i + by;
        if (i < 0 || j < 0 || j >= mgr.rules().size()) return;
        java.util.Collections.swap(mgr.rules(), i, j);
        persist();
        table.setRowSelectionInterval(j, j);
    }

    private void persist() { mgr.save(); model.fireTableDataChanged(); }

    private boolean editRule(UpstreamProxy p) {
        JComboBox<UpstreamProxy.Type> type = Ui.combo(UpstreamProxy.Type.values());
        type.setSelectedItem(p.type);
        JTextField host = Ui.field(p.host);
        JTextField port = Ui.shortField(String.valueOf(p.port));
        JTextField user = Ui.field(p.user);
        JPasswordField pass = Ui.passwordField(p.pass);
        pass.setFont(Ui.PLAIN);
        JTextField pattern = Ui.field(p.hostPattern);
        JCheckBox enabled = Ui.check("enabled", p.enabled);

        JPanel form = Ui.form(
                "type", type,
                "proxy host", host,
                "proxy port", port,
                "username (HTTP only)", user,
                "password", pass,
                "destination host (blank = all, *.example.com allowed)", pattern,
                "", enabled);
        int ok = JOptionPane.showConfirmDialog(ctx.frame(), form, "upstream proxy",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (ok != JOptionPane.OK_OPTION) return false;
        p.type = (UpstreamProxy.Type) type.getSelectedItem();
        p.host = host.getText().trim();
        try { p.port = Integer.parseInt(port.getText().trim()); } catch (Exception ignored) {}
        p.user = user.getText().trim();
        p.pass = new String(pass.getPassword());
        p.hostPattern = pattern.getText().trim();
        p.enabled = enabled.isSelected();
        return true;
    }

    private final class Model extends AbstractTableModel {
        private final String[] cols = {"Enabled", "Destination host", "Proxy host", "Proxy port", "Auth type", "Username"};
        @Override public int getRowCount() { return mgr.rules().size(); }
        @Override public int getColumnCount() { return cols.length; }
        @Override public String getColumnName(int c) { return cols[c]; }
        @Override public Class<?> getColumnClass(int c) { return c == 0 ? Boolean.class : String.class; }
        @Override public boolean isCellEditable(int r, int c) { return c == 0; }
        @Override public void setValueAt(Object v, int r, int c) {
            if (c == 0) { mgr.rules().get(r).enabled = Boolean.TRUE.equals(v); mgr.save(); fireTableRowsUpdated(r, r); }
        }
        @Override public Object getValueAt(int r, int c) {
            UpstreamProxy p = mgr.rules().get(r);
            return switch (c) {
                case 0 -> p.enabled;
                case 1 -> p.hostPattern.isBlank() ? "*" : p.hostPattern;
                case 2 -> p.host;
                case 3 -> p.port;
                case 4 -> p.type == UpstreamProxy.Type.HTTP ? (p.user.isEmpty() ? "None" : "Basic") : p.type.toString();
                default -> p.user;
            };
        }
    }
}