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.

2.2 KB
Raw Blame
package opensuite.core;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.prefs.Preferences;

/**
 * Carries a user's data over from the product's former name. The settings
 * folder and the saved preferences were both kept under that name; the first
 * start under the new one moves the folder and copies the preferences, so
 * nothing a user had set up (certificate, projects list, settings) is lost.
 */
public final class Migration {

    /** The former name, kept only here so old data can be found. */
    static final String OLD = "open" + "trap";

    private Migration() {}

    /** Run once at startup, before anything reads a setting. */
    public static void run() {
        try {
            Path home = Path.of(System.getProperty("user.home", "."));
            moveFolder(home.resolve("." + OLD), home.resolve(".opensuite"));
        } catch (Exception ignored) {}
        try {
            Preferences root = Preferences.userRoot();
            if (root.nodeExists(OLD + ".v1")) copyPreferences(root.node(OLD + ".v1"), root.node("opensuite.v1"));
        } catch (Exception ignored) {}
    }

    /** Move the old folder to the new place, unless the new one is already there. Returns whether it moved. */
    static boolean moveFolder(Path from, Path to) throws IOException {
        if (!Files.isDirectory(from) || Files.exists(to)) return false;
        Files.move(from, to);
        return true;
    }

    /** Copy every key and child node, unless the new node already holds settings. Returns whether it copied. */
    static boolean copyPreferences(Preferences from, Preferences to) throws java.util.prefs.BackingStoreException {
        if (to.keys().length > 0 || to.childrenNames().length > 0) return false;
        copyNode(from, to);
        to.flush();
        return true;
    }

    private static void copyNode(Preferences from, Preferences to) throws java.util.prefs.BackingStoreException {
        for (String k : from.keys()) {
            String v = from.get(k, null);
            if (v != null) to.put(k, v);
        }
        for (String child : from.childrenNames()) copyNode(from.node(child), to.node(child));
    }
}