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.

4.5 KB
Raw Blame
package opensuite.core;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.prefs.Preferences;

/** String-keyed settings with typed accessors, persisted to java.util.prefs. */
public class Settings implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final String NODE = "opensuite.v1";
    // The macOS preferences backend ignores java.util.prefs.userRoot, so tests
    // need an explicit process-local store rather than touching a user's settings.
    private static final boolean TEST_STORE = Boolean.getBoolean("opensuite.test.inMemorySettings");
    private static final Map<String, String> TEST_VALUES = TEST_STORE ? new java.util.concurrent.ConcurrentHashMap<>() : null;

    private final Map<String, String> values = new LinkedHashMap<>();
    private transient Preferences prefs;
    // Told the key each time a value changes, so a control elsewhere (the proxy
    // server, a banner) can follow a setting without polling it.
    private transient List<Consumer<String>> listeners = new CopyOnWriteArrayList<>();

    public Settings() {
        if (!TEST_STORE) {
            try { prefs = Preferences.userRoot().node(NODE); } catch (Exception ignored) {}
        }
    }

    static boolean usingTestStore() { return TEST_STORE; }

    public synchronized String get(String key, String def) {
        String v = values.get(key);
        if (v != null) return v;
        if (TEST_STORE) {
            v = TEST_VALUES.get(key);
            if (v != null) { values.put(key, v); return v; }
        }
        if (prefs != null) {
            try {
                String p = prefs.get(key, null);
                if (p != null) { values.put(key, p); return p; }
            } catch (Exception ignored) {}
        }
        return def;
    }

    public void set(String key, String value) {
        synchronized (this) {
            values.put(key, value);
            if (TEST_STORE) TEST_VALUES.put(key, value);
            else if (prefs != null) {
                try { prefs.put(key, value); } catch (Exception ignored) {}
            }
        }
        for (Consumer<String> l : listeners()) l.accept(key);
    }

    /** Forget a key so reads fall back to the default again. */
    public void remove(String key) {
        synchronized (this) {
            values.remove(key);
            if (TEST_STORE) TEST_VALUES.remove(key);
            else if (prefs != null) {
                try { prefs.remove(key); } catch (Exception ignored) {}
            }
        }
        for (Consumer<String> l : listeners()) l.accept(key);
    }

    /** Every key currently set, including those only in the preferences store. */
    public synchronized java.util.Set<String> keys() {
        java.util.Set<String> out = new java.util.LinkedHashSet<>(values.keySet());
        if (TEST_STORE) out.addAll(TEST_VALUES.keySet());
        else if (prefs != null) { try { out.addAll(java.util.Arrays.asList(prefs.keys())); } catch (Exception ignored) {} }
        return out;
    }

    public boolean getBool(String k, boolean def) { return Boolean.parseBoolean(get(k, String.valueOf(def))); }
    public void setBool(String k, boolean v)      { set(k, String.valueOf(v)); }

    public int getInt(String k, int def) {
        try { return Integer.parseInt(get(k, String.valueOf(def))); } catch (Exception e) { return def; }
    }
    public void setInt(String k, int v) { set(k, String.valueOf(v)); }

    public synchronized Map<String, String> snapshot() { return new LinkedHashMap<>(values); }

    public void restore(Map<String, String> m) {
        if (m == null) return;
        for (Map.Entry<String, String> e : m.entrySet()) set(e.getKey(), e.getValue());
    }

    /** Be told the key whenever a value is set, on the thread that set it. A listener registered by an extension is not kept raw: a stand-in is kept that offers each key to the extension's own thread. */
    public void addListener(Consumer<String> l) { listeners().add(ExtensionCaller.admit(listeners(), Consumer.class, l)); }

    /** Stop being told. A listener registered by a short-lived component must
     *  be removed when that component goes, or Settings retains it forever. */
    public void removeListener(Consumer<String> l) {
        if (listeners != null) ExtensionCaller.withdraw(listeners(), l);
    }

    private List<Consumer<String>> listeners() {
        if (listeners == null) listeners = new CopyOnWriteArrayList<>();
        return listeners;
    }
}