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.

Ask before a restart, and open the project again after it

scottsturrock7b4aa123c86a
diff --git a/src/opensuite/ui/ExitPrompt.java b/src/opensuite/ui/ExitPrompt.java
index 6dac44e..3785910 100644
--- a/src/opensuite/ui/ExitPrompt.java
+++ b/src/opensuite/ui/ExitPrompt.java
@@ -33,9 +33,19 @@ final class ExitPrompt extends JPanel {
/** Ask in {@code frame} whether to exit. A second request while one is showing changes nothing. */
static void ask(JFrame frame, String risk, Runnable onExit) {
+ ask(frame, "Exit Open Suite?", risk, "Exit", "Close Open Suite", "Keep Open Suite open", onExit, null);
+ }
+
+ /**
+ * Ask a question of the same kind in any window: {@code onYes} runs when it is agreed to,
+ * {@code onNo} (which may be null) when it is not.
+ */
+ static void ask(RootPaneContainer frame, String heading, String risk, String yes, String yesTip, String noTip,
+ Runnable onYes, Runnable onNo) {
+ Runnable onExit = onYes;
JLayeredPane layers = frame.getLayeredPane();
if (layers.getClientProperty(KEY) != null) return;
- ExitPrompt p = new ExitPrompt(risk);
+ ExitPrompt p = new ExitPrompt(heading, risk, yes, yesTip, noTip);
layers.putClientProperty(KEY, p);
Runnable close = () -> {
layers.remove(p);
@@ -43,9 +53,10 @@ final class ExitPrompt extends JPanel {
layers.revalidate();
layers.repaint();
};
- p.cancel.addActionListener(e -> close.run());
+ Runnable refuse = () -> { close.run(); if (onNo != null) onNo.run(); };
+ p.cancel.addActionListener(e -> refuse.run());
p.exit.addActionListener(e -> { close.run(); onExit.run(); });
- p.registerKeyboardAction(e -> close.run(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW);
+ p.registerKeyboardAction(e -> refuse.run(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), WHEN_IN_FOCUSED_WINDOW);
p.setBounds(0, 0, layers.getWidth(), layers.getHeight());
layers.add(p, JLayeredPane.MODAL_LAYER);
// The panel covers the window whatever size it is taken to while the question stands.
@@ -60,11 +71,13 @@ final class ExitPrompt extends JPanel {
p.cancel.requestFocusInWindow();
}
- private final JButton cancel = Ui.button("Cancel", "Keep Open Suite open");
- private final JButton exit = Ui.button("Exit", "Close Open Suite");
+ private final JButton cancel;
+ private final JButton exit;
- private ExitPrompt(String risk) {
+ private ExitPrompt(String heading, String risk, String yes, String yesTip, String noTip) {
super(new GridBagLayout());
+ cancel = Ui.button("Cancel", noTip);
+ exit = Ui.button(yes, yesTip);
setOpaque(false);
// Nothing underneath takes a click or the pointer while the question stands.
addMouseListener(new MouseAdapter() { });
@@ -72,7 +85,7 @@ final class ExitPrompt extends JPanel {
addMouseWheelListener(e -> { });
setFocusCycleRoot(true);
- JLabel title = Ui.label("Exit Open Suite?");
+ JLabel title = Ui.label(heading);
title.setFont(Ui.BOLD.deriveFont(Ui.BOLD.getSize2D() + 2f));
JLabel what = Ui.label("<html><div style='width:" + Ui.em(300) + "px'>" + risk + "</div></html>");
Theme.primary(exit);
diff --git a/src/opensuite/ui/Restart.java b/src/opensuite/ui/Restart.java
new file mode 100644
index 0000000..e1c5b07
--- /dev/null
+++ b/src/opensuite/ui/Restart.java
@@ -0,0 +1,65 @@
+package opensuite.ui;
+
+import opensuite.core.SuiteContext;
+
+import java.awt.Component;
+import java.awt.Window;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import javax.swing.RootPaneContainer;
+import javax.swing.SwingUtilities;
+
+/**
+ * A change that needs the application started again: the text size, the scale, the theme.
+ *
+ * <p>The restart is never a surprise. It is asked for first, in the window the change was made
+ * in, and the question says what it costs: a project in a file is saved and opened again, a
+ * temporary one is lost, a running scan is stopped. Refused, the setting stays as it was.
+ */
+public final class Restart {
+ private Restart() {}
+
+ /** What the restart means for the work that is open, as the sentences of the question. */
+ static String cost(String change, boolean temporary, boolean hasContents, boolean scanning) {
+ StringBuilder b = new StringBuilder("Open Suite restarts to apply ").append(change).append(".");
+ if (!temporary) b.append(" The project is saved first and opened again.");
+ else if (hasContents) b.append(" This project is temporary and not saved to a file, so its contents will be lost.");
+ else b.append(" It comes back with a new temporary project.");
+ if (scanning) b.append(" A scan is running and will be stopped.");
+ return b.toString();
+ }
+
+ /** The arguments that start the application with {@code project} open: the flags it was started with, and that file. */
+ static String[] arguments(String[] launched, File project) {
+ List<String> out = new ArrayList<>();
+ if (launched != null)
+ for (String a : launched) if (a != null && !StateIO.isProjectName(a)) out.add(a);
+ if (project != null) out.add(project.getAbsolutePath());
+ return out.toArray(new String[0]);
+ }
+
+ /** The arguments for a restart of the application as it stands. */
+ public static String[] arguments(SuiteContext ctx) {
+ return arguments(Main.launchArgs, ctx.projectFile());
+ }
+
+ /**
+ * Ask whether to restart for {@code change} ("the new text size"). Agreed to, the project is
+ * saved and {@code apply} runs, which stores the setting and starts the application again;
+ * refused, {@code refused} runs, to put a control back to what it showed.
+ */
+ public static void ask(Component from, SuiteContext ctx, String change, Runnable apply, Runnable refused) {
+ boolean scanning = false;
+ for (opensuite.core.ScanTask t : ctx.tasks())
+ if (t.sites().size() > 0 && t.state() != opensuite.core.ScanTask.State.FINISHED) scanning = true;
+ boolean temporary = ctx.projectFile() == null;
+ boolean contents = !ctx.store().all().isEmpty() || !ctx.issues().isEmpty();
+ String cost = cost(change, temporary, contents, scanning);
+ Runnable go = () -> { StateIO.saveBeforeRestart(ctx); apply.run(); };
+ Window w = from instanceof Window ? (Window) from : SwingUtilities.getWindowAncestor(from);
+ if (!(w instanceof RootPaneContainer)) { go.run(); return; }
+ ExitPrompt.ask((RootPaneContainer) w, "Restart Open Suite?", cost, "Restart", "Apply the change and start Open Suite again",
+ "Leave the setting as it is", go, refused);
+ }
+}
diff --git a/src/opensuite/ui/SuiteFrame.java b/src/opensuite/ui/SuiteFrame.java
index a1136c4..ab23018 100644
--- a/src/opensuite/ui/SuiteFrame.java
+++ b/src/opensuite/ui/SuiteFrame.java
@@ -815,8 +815,8 @@ public class SuiteFrame extends JFrame implements ToolHost {
/** Apply a new scale: save the project first, then relaunch to apply it. */
private void relaunchScaled(double scale) {
- StateIO.saveBeforeRestart(ctx);
- UiScale.changeTo(scale, Main.launchArgs);
+ if (Math.abs(UiScale.clamp(scale) - currentScale()) < 0.001) return; // already that size: nothing to restart for
+ Restart.ask(this, ctx, "the new text size", () -> UiScale.changeTo(scale, Restart.arguments(ctx)), null);
}
/** A frame without the desktop's title bar must be told not to cover the desktop's panels. */
diff --git a/src/opensuite/ui/SuiteOptionsPanel.java b/src/opensuite/ui/SuiteOptionsPanel.java
index 5696c73..53bfdf1 100644
--- a/src/opensuite/ui/SuiteOptionsPanel.java
+++ b/src/opensuite/ui/SuiteOptionsPanel.java
@@ -36,11 +36,10 @@ public class SuiteOptionsPanel extends JPanel {
scaleCombo.setSelectedItem(cur + "%");
scaleCombo.addActionListener(e -> {
String v = String.valueOf(scaleCombo.getSelectedItem()).replace("%", "");
- try {
- StateIO.saveBeforeRestart(ctx);
- UiScale.changeTo(Double.parseDouble(v) / 100.0, Main.launchArgs);
- }
- catch (Exception ex) { Debug.log("scale change: " + ex); }
+ if (v.equals(String.valueOf(cur))) return;
+ Restart.ask(scaleCombo, ctx, "the new scale",
+ () -> UiScale.changeTo(Double.parseDouble(v) / 100.0, Restart.arguments(ctx)),
+ () -> scaleCombo.setSelectedItem(cur + "%"));
});
JTextArea scaleNote = Ui.note(
"Scales the whole interface using real HiDPI rendering, so fonts, columns and "
@@ -56,9 +55,10 @@ public class SuiteOptionsPanel extends JPanel {
themeCombo.addActionListener(e -> {
boolean wantDark = "dark".equals(themeCombo.getSelectedItem());
if (wantDark == Theme.dark) return;
- ctx.settings().set("ui.theme", wantDark ? "dark" : "light");
- StateIO.saveBeforeRestart(ctx);
- if (UiScale.relaunchWith(UiScale.desired(), Main.launchArgs)) System.exit(0);
+ Restart.ask(themeCombo, ctx, "the " + (wantDark ? "dark" : "light") + " theme", () -> {
+ ctx.settings().set("ui.theme", wantDark ? "dark" : "light");
+ if (UiScale.relaunchWith(UiScale.desired(), Restart.arguments(ctx))) System.exit(0);
+ }, () -> themeCombo.setSelectedItem(Theme.dark ? "dark" : "light"));
});
JTextArea themeNote = Ui.note("Switches between the light and dark palette. "
+ "Changing it restarts the app so every panel comes up in the new theme.");
diff --git a/src/opensuite/ui/UiScale.java b/src/opensuite/ui/UiScale.java
index edb0665..c58bac0 100644
--- a/src/opensuite/ui/UiScale.java
+++ b/src/opensuite/ui/UiScale.java
@@ -104,6 +104,27 @@ public final class UiScale {
return true;
}
+ /**
+ * The arguments for the copy that takes over: those this process was given, with the
+ * application's own at their end replaced by the new ones, so that Java's options and the
+ * main class of a copy started by hand stay as they were. Windows tells a process none of
+ * its arguments, and a packaged launcher needs none but the application's.
+ */
+ static List<String> argumentsFor(String[] given, String[] launched, String[] wanted) {
+ List<String> out = new ArrayList<>(java.util.Arrays.asList(given));
+ if (given.length == 0) { // none to be seen (Windows), or none given: the application's own are all there is
+ if (wanted != null) java.util.Collections.addAll(out, wanted);
+ return out;
+ }
+ String[] old = launched == null ? new String[0] : launched;
+ boolean tail = out.size() >= old.length
+ && out.subList(out.size() - old.length, out.size()).equals(java.util.Arrays.asList(old));
+ if (!tail) return out; // not a command line this knows how to change: start it as it was started
+ out.subList(out.size() - old.length, out.size()).clear();
+ if (wanted != null) java.util.Collections.addAll(out, wanted);
+ return out;
+ }
+
/** The options without the scale the start script put there, so that the next start chooses it afresh. */
static String withoutOwnScale(String options) {
if (options == null) return "";
@@ -132,7 +153,7 @@ public final class UiScale {
}).orElse(false);
if (info.command().isPresent() && !bareJava) {
cmd.add(info.command().get());
- info.arguments().ifPresent(a -> java.util.Collections.addAll(cmd, a));
+ cmd.addAll(argumentsFor(info.arguments().orElse(new String[0]), Main.launchArgs, args));
} else {
// Fallback for platforms that hide the command line: rebuild a
// java invocation from the classpath.
@@ -143,8 +164,6 @@ public final class UiScale {
cmd.add("opensuite.ui.Main");
if (args != null) java.util.Collections.addAll(cmd, args);
}
- // The packaged Windows launcher is handed the project it was opened with again.
- if (opensuite.core.Os.WINDOWS && info.arguments().isEmpty() && !bareJava && args != null) java.util.Collections.addAll(cmd, args);
ProcessBuilder pb = new ProcessBuilder(cmd).inheritIO();
pb.environment().remove(LAUNCHER_MARK);
// On a Mac the scale is applied to the font size instead (Theme.baseFontSize).
diff --git a/src/opensuite/ui/settings/InterfacePages.java b/src/opensuite/ui/settings/InterfacePages.java
index b5307b0..f83d1fe 100644
--- a/src/opensuite/ui/settings/InterfacePages.java
+++ b/src/opensuite/ui/settings/InterfacePages.java
@@ -222,10 +222,12 @@ public final class InterfacePages {
ButtonGroup g = new ButtonGroup(); g.add(light); g.add(dark);
java.awt.event.ItemListener theme = e -> {
boolean wantDark = dark.isSelected();
- if (wantDark == Theme.dark) return;
- s.set("ui.theme", wantDark ? "dark" : "light");
- try { java.util.prefs.Preferences.userRoot().node("opensuite.v1").put("ui.theme", wantDark ? "dark" : "light"); } catch (Exception ignore) { }
- if (opensuite.ui.UiScale.relaunchWith(opensuite.ui.UiScale.desired(), opensuite.ui.Main.launchArgs)) System.exit(0);
+ if (wantDark == Theme.dark || e.getStateChange() != java.awt.event.ItemEvent.SELECTED) return;
+ opensuite.ui.Restart.ask(dark, ctx, "the " + (wantDark ? "dark" : "light") + " theme", () -> {
+ s.set("ui.theme", wantDark ? "dark" : "light");
+ try { java.util.prefs.Preferences.userRoot().node("opensuite.v1").put("ui.theme", wantDark ? "dark" : "light"); } catch (Exception ignore) { }
+ if (opensuite.ui.UiScale.relaunchWith(opensuite.ui.UiScale.desired(), opensuite.ui.Restart.arguments(ctx))) System.exit(0);
+ }, () -> (Theme.dark ? dark : light).setSelected(true));
};
light.addItemListener(theme); dark.addItemListener(theme);
Ui.sized(fontSize, 72);
@@ -267,9 +269,16 @@ public final class InterfacePages {
if (systemDpi.isSelected()) opensuite.ui.UiScale.clearSaved();
enable.run();
});
+ boolean[] puttingBack = {false};
factor.addActionListener(e -> {
- if (!factor.isEnabled()) return;
- try { opensuite.ui.UiScale.changeTo(Double.parseDouble(String.valueOf(factor.getSelectedItem())), opensuite.ui.Main.launchArgs); } catch (Exception ignore) { }
+ if (!factor.isEnabled() || puttingBack[0]) return;
+ double want;
+ try { want = Double.parseDouble(String.valueOf(factor.getSelectedItem())); } catch (Exception ignore) { return; }
+ double now = opensuite.ui.UiScale.desired();
+ if (Math.abs(want - now) < 0.001 && opensuite.ui.UiScale.hasSaved()) return;
+ opensuite.ui.Restart.ask(factor, ctx, "the new scale",
+ () -> opensuite.ui.UiScale.changeTo(want, opensuite.ui.Restart.arguments(ctx)),
+ () -> { puttingBack[0] = true; try { factor.setSelectedItem(String.valueOf(now)); } finally { puttingBack[0] = false; } });
});
JPanel factorRow = SettingsPage.line(22, factorLabel, 5, factor);
JComponent dpiRow = OptionsPage.indent(systemDpi, 32), factorIndented = OptionsPage.indent(factorRow, 30);
diff --git a/tests/RunTests.java b/tests/RunTests.java
index 86262c0..b647384 100644
--- a/tests/RunTests.java
+++ b/tests/RunTests.java
@@ -68,6 +68,7 @@ public final class RunTests {
new opensuite.ui.SendToComparerTest().run();
new opensuite.ui.ThemePaletteTest().run();
new opensuite.ui.RestartOptionsTest().run();
+ new opensuite.ui.RestartTest().run();
new opensuite.ui.EditorKeysTest().run();
new opensuite.ui.PrettyPrintTest().run();
new opensuite.target.LinkFinderTest().run();
diff --git a/tests/opensuite/ui/RestartTest.java b/tests/opensuite/ui/RestartTest.java
new file mode 100644
index 0000000..e3d857e
--- /dev/null
+++ b/tests/opensuite/ui/RestartTest.java
@@ -0,0 +1,41 @@
+package opensuite.ui;
+
+import opensuite.testkit.Expect;
+import opensuite.testkit.Harness;
+
+import java.io.File;
+import java.util.List;
+
+/**
+ * A change of text size, scale or theme starts the application again. It asks first and says
+ * what that costs, and a project in a file is opened again afterwards.
+ */
+public final class RestartTest {
+ public void run() {
+ Harness.suite("restart");
+
+ Harness.test("the question says what the restart costs", () -> {
+ String temp = Restart.cost("the new text size", true, true, false);
+ Expect.isTrue(temp.contains("the new text size") && temp.contains("will be lost"), "a temporary project with work in it is lost: " + temp);
+ Expect.isTrue(!Restart.cost("x", true, false, false).contains("lost"), "an empty temporary project loses nothing");
+ String disk = Restart.cost("the dark theme", false, true, false);
+ Expect.isTrue(disk.contains("saved first and opened again") && !disk.contains("lost"), "a project in a file: " + disk);
+ Expect.isTrue(Restart.cost("x", false, true, true).contains("scan is running"), "a running scan is named");
+ });
+
+ Harness.test("the project in a file is handed to the next start", () -> {
+ File p = new File("work.osuite").getAbsoluteFile();
+ Expect.equal(List.of(Restart.arguments(new String[]{"--debug"}, p)), List.of("--debug", p.getPath()), "flags kept, the project added");
+ Expect.equal(List.of(Restart.arguments(new String[]{"old.osuite", "--debug"}, p)), List.of("--debug", p.getPath()), "the project it was started with gives way to the open one");
+ Expect.equal(List.of(Restart.arguments(new String[]{"old.osuite"}, null)), List.<String>of(), "a temporary project: none is named");
+ });
+
+ Harness.test("only the application's own arguments are replaced", () -> {
+ Expect.equal(UiScale.argumentsFor(new String[]{"-Xmx1g", "-cp", "a:b", "opensuite.ui.Main", "--debug"}, new String[]{"--debug"}, new String[]{"--debug", "/p.osuite"}),
+ List.of("-Xmx1g", "-cp", "a:b", "opensuite.ui.Main", "--debug", "/p.osuite"), "a copy started by hand keeps Java's options");
+ Expect.equal(UiScale.argumentsFor(new String[0], new String[0], new String[]{"/p.osuite"}), List.of("/p.osuite"), "a packaged launcher gets just the project");
+ Expect.equal(UiScale.argumentsFor(new String[0], new String[]{"old.osuite"}, new String[]{"/p.osuite"}), List.of("/p.osuite"), "Windows shows a process none of its arguments: the new ones are all it gets");
+ Expect.equal(UiScale.argumentsFor(new String[]{"--strange"}, new String[]{"old.osuite"}, new String[]{"/p.osuite"}), List.of("--strange"), "a command line it does not know is started as it was");
+ });
+ }
+}