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.
Java application-securityappsecbug-bountyburp-alternativeburp-suitedasthttp-proxyinfosecintercepting-proxyjavaowaspowasp-zappenetration-testingpentestingsecuritysecurity-toolsvulnerability-scannerweb-application-securityweb-security
package opensuite.ui;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
/**
* Interface scaling via Java's real HiDPI mechanism (sun.java2d.uiScale).
*
* That property scales the entire Swing rendering uniformly: fonts, column
* widths, borders and all coordinates, and it stays crisp. It can only be set
* at JVM startup, so changing scale relaunches the app. This is why scaling
* "just works" here instead of leaving fixed pixel sizes behind.
*/
public final class UiScale {
private UiScale() {}
private static final String PREF_NODE = "opensuite.v1";
private static final String PREF_KEY = "ui.scale";
public static final double MIN = 0.75, MAX = 3.0;
/**
* The scale to use: the launch flag, else the one chosen in Settings >
* User interface > Display > Scaling, else 1.0, which sets nothing and
* leaves the display's own setting to Java.
*
* <p>The application does not guess a scale from the size of the screen.
* It used to, and a wide screen that was not a dense one got an interface
* twice the size, with no way to say otherwise from outside.
*/
public static double desired() {
String flag = System.getProperty("opensuite.scale");
if (flag != null) {
try { return clamp(Double.parseDouble(flag.trim())); } catch (Exception ignored) {}
}
try {
String saved = Preferences.userRoot().node(PREF_NODE).get(PREF_KEY, null);
if (saved != null) return clamp(Double.parseDouble(saved));
} catch (Exception ignored) {}
return 1.0;
}
/** Whether a scale was asked for, by the launch flag or in the settings; 1.0 asked for is still a choice. */
public static boolean chosen() { return System.getProperty("opensuite.scale") != null || hasSaved(); }
/** Whether the JVM is already running with the scale applied. */
public static boolean applied() {
return System.getProperty("opensuite.scaled") != null
|| System.getProperty("sun.java2d.uiScale") != null;
}
static double clamp(double v) { return Math.max(MIN, Math.min(MAX, v)); }
/** Whether a scale has been chosen by hand, rather than left to the system's setting. */
public static boolean hasSaved() {
try { return Preferences.userRoot().node(PREF_NODE).get(PREF_KEY, null) != null; } catch (Exception e) { return false; }
}
/** Forget a scale chosen by hand, so the next start follows the system's setting again. */
public static void clearSaved() {
try { Preferences p = Preferences.userRoot().node(PREF_NODE); p.remove(PREF_KEY); p.flush(); } catch (Exception ignored) {}
}
private static void save(double v) {
try { Preferences.userRoot().node(PREF_NODE).put(PREF_KEY, String.valueOf(v)); }
catch (Exception ignored) {}
}
/**
* Relaunch this application in a fresh JVM with the given scale applied,
* then exit the current one. Called at startup (when a non-default scale is
* saved) and whenever the user changes scale from the UI.
*
* @return true if a relaunch was started (caller should return/stop).
*/
/**
* Inside an AppImage the program runs from a mount that is taken away the moment this
* process ends, so a copy started from there dies with it, or lives on without its files.
* The image itself is started again instead: it makes a mount of its own, and its start
* script works out the scale and the memory limit from the settings as on any start.
*/
/**
* The packaged launcher sets this for itself before it loads Java. A launcher that finds it
* already set takes its arguments for Java's own and reads no settings of the package: a copy
* started with it either prints Java's usage and ends, or refuses the first argument.
*/
static final String LAUNCHER_MARK = "_JPACKAGE_LAUNCHER";
private static boolean relaunchAppImage(String[] args) throws java.io.IOException {
String image = System.getenv("APPIMAGE");
if (image == null || image.isBlank() || opensuite.core.Os.WINDOWS || opensuite.core.Os.MAC) return false;
java.io.File file = new java.io.File(image);
if (!file.isFile() || !file.canExecute()) return false;
List<String> cmd = new ArrayList<>();
cmd.add(file.getPath());
if (args != null) java.util.Collections.addAll(cmd, args);
ProcessBuilder pb = new ProcessBuilder(cmd).inheritIO();
java.util.Map<String, String> env = pb.environment();
String opts = withoutOwnScale(env.get("JAVA_TOOL_OPTIONS"));
if (opts.isEmpty()) env.remove("JAVA_TOOL_OPTIONS"); else env.put("JAVA_TOOL_OPTIONS", opts);
// What the first start set for its own mount means nothing to the next one.
for (String k : new String[]{"APPDIR", "ARGV0", "OWD", LAUNCHER_MARK}) env.remove(k);
pb.start();
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 "";
if (!options.contains("-Dopensuite.scaled=")) return options.trim();
return options.replaceAll("-Dsun\\.java2d\\.uiScale=\\S+", "").replaceAll("-Dopensuite\\.scaled=\\S+", "")
.trim().replaceAll("\\s+", " ");
}
public static boolean relaunchWith(double scale, String[] args) {
double s = clamp(scale);
// Re-exec the exact command that launched this process, whether that was
// "java -jar ...", a jpackage launcher, or an AppImage, and inject the
// scale through JAVA_TOOL_OPTIONS, which every JVM reads at start-up no
// matter how it was launched. Reconstructing a "java -cp ..." line (the
// old approach) only worked from a plain jar; inside a self-contained app
// it failed silently and the UI came up unscaled.
try {
if (relaunchAppImage(args)) return true;
ProcessHandle.Info info = ProcessHandle.current().info();
List<String> cmd = new ArrayList<>();
// Windows tells a process its program but not its arguments, which is all a
// packaged launcher needs; a plain java started by hand is rebuilt below.
boolean bareJava = info.arguments().isEmpty() && info.command().map(c -> {
String n = new java.io.File(c).getName().toLowerCase(java.util.Locale.ROOT);
return n.equals("java.exe") || n.equals("javaw.exe") || n.equals("java");
}).orElse(false);
if (info.command().isPresent() && !bareJava) {
cmd.add(info.command().get());
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.
cmd.add(System.getProperty("java.home") + java.io.File.separator
+ "bin" + java.io.File.separator + "java");
cmd.add("-cp");
cmd.add(System.getProperty("java.class.path"));
cmd.add("opensuite.ui.Main");
if (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).
String opts = ((Math.abs(s - 1.0) > 0.001 || chosen()) && !opensuite.core.Os.MAC ? "-Dsun.java2d.uiScale=" + s + " " : "")
+ "-Dopensuite.scaled=" + s;
// The memory limit too, for a launcher that cannot read it itself (the macOS app).
int mb = opensuite.core.Launch.memoryFileMb();
if (mb > 0) opts += " -Xmx" + mb + "m";
String prior = pb.environment().get("JAVA_TOOL_OPTIONS");
pb.environment().put("JAVA_TOOL_OPTIONS",
(prior == null || prior.isBlank() ? "" : prior + " ") + opts);
pb.start();
return true;
} catch (Exception e) {
return false;
}
}
/** Save a new scale and relaunch to apply it. */
public static void changeTo(double scale, String[] args) {
double s = clamp(scale);
save(s);
if (relaunchWith(s, args)) System.exit(0);
}
// Kept for call-site compatibility; the JVM now does all scaling, so these
// are no-ops and fonts stay at their base sizes.
public static void installDefaults() {}
public static void rescaleFonts() {}
public static double factor() { return 1.0; }
public static int px(int base) { return base; }
public static java.awt.Font font(String family, int style, int baseSize) {
return new java.awt.Font(family, style, baseSize);
}
}