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.2 KB
Raw Blame
package opensuite.ui;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * The list of recently used project files, shown in the startup screen's "Open
 * existing project" table. Persisted as a small tab-separated file under
 * {@code ~/.opensuite}, most-recent first, so reopening a project is one click.
 * A backslash, tab, carriage return or newline inside a name or a path is
 * written escaped, so a path holding one stays one field on one line.
 */
public final class RecentProjects {

    /** One remembered project: the name the user gave it, its file, and when it was last opened. */
    public record Entry(String name, File file, long lastAccessed) {}

    private static final int MAX = 20;

    private RecentProjects() {}

    private static Path store() {
        return Path.of(System.getProperty("user.home"), ".opensuite", "recent-projects.tsv");
    }

    public static List<Entry> load() {
        List<Entry> out = new ArrayList<>();
        Path p = store();
        if (!Files.exists(p)) return out;
        try {
            for (String line : Files.readAllLines(p, StandardCharsets.UTF_8)) {
                String[] f = line.split("\t", 3);
                if (f.length < 3) continue;
                try {
                    File file = new File(unescape(f[1]));
                    // Skip projects whose file has been deleted or moved. The
                    // name shown is always the file's own name, whatever an
                    // older entry recorded.
                    if (!file.isFile()) continue;
                    out.add(new Entry(file.getName(), file, Long.parseLong(f[2])));
                } catch (NumberFormatException ignored) {}
            }
        } catch (Exception ignored) {}
        return out;
    }

    /**
     * Record a project as the most recently used, de-duplicating by file path.
     * A project's name is its file name: the two are never allowed to drift,
     * so the name passed in is only a fallback for a file with no name.
     */
    public static void record(String name, File file) {
        if (file == null) return;
        List<Entry> list = load();
        list.removeIf(e -> e.file().getAbsolutePath().equals(file.getAbsolutePath()));
        String shown = file.getName().isBlank() ? (name == null ? "" : name) : file.getName();
        list.add(0, new Entry(shown, file, System.currentTimeMillis()));
        while (list.size() > MAX) list.remove(list.size() - 1);
        try {
            Path p = store();
            Files.createDirectories(p.getParent());
            StringBuilder b = new StringBuilder();
            for (Entry e : list) {
                b.append(escape(e.name())).append('\t')
                 .append(escape(e.file().getAbsolutePath())).append('\t')
                 .append(e.lastAccessed()).append('\n');
            }
            Files.writeString(p, b.toString(), StandardCharsets.UTF_8);
        } catch (Exception ignored) {}
    }

    static String escape(String s) {
        StringBuilder b = new StringBuilder(s.length() + 8);
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '\\' -> b.append("\\\\");
                case '\t' -> b.append("\\t");
                case '\r' -> b.append("\\r");
                case '\n' -> b.append("\\n");
                default -> b.append(c);
            }
        }
        return b.toString();
    }

    static String unescape(String s) {
        StringBuilder b = new StringBuilder(s.length());
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c != '\\' || i + 1 == s.length()) { b.append(c); continue; }
            char n = s.charAt(++i);
            switch (n) {
                case '\\' -> b.append('\\');
                case 't' -> b.append('\t');
                case 'r' -> b.append('\r');
                case 'n' -> b.append('\n');
                default -> b.append(c).append(n);
            }
        }
        return b.toString();
    }

    public static String baseName(File f) {
        String n = f.getName();
        int dot = n.lastIndexOf('.');
        return dot > 0 ? n.substring(0, dot) : n;
    }
}