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

import opensuite.core.Debug;
import opensuite.core.Settings;
import opensuite.core.SuiteContext;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

/**
 * Settings > Project > Automatic backup: a copy of the project file,
 * written beside it on a timer, so a damaged project loses at most that many
 * minutes of work. Only a project on disk has anything to back up.
 */
public final class AutoBackup {

    public static final String AUTO = "backup.auto", MINUTES = "backup.minutes", IN_SCOPE = "backup.inScopeOnly",
            PROGRESS = "backup.progress", DELETE_ON_EXIT = "backup.deleteOnExit";

    private AutoBackup() { }

    /** Where a project's backup goes: beside it, with ".bak" added. */
    public static File fileFor(File project) {
        return new File(project.getParentFile(), project.getName() + ".bak");
    }

    private static Thread timer;
    private static volatile long lastRun = System.currentTimeMillis();

    /** Start the timer; it does nothing while the setting is off or the project is temporary. */
    public static synchronized void start(SuiteContext ctx) {
        if (timer != null) return;
        timer = new Thread(() -> {
            while (true) {
                try { Thread.sleep(20_000); } catch (InterruptedException e) { return; }
                try {
                    if (!due(ctx.settings(), ctx.projectFile() != null, lastRun, System.currentTimeMillis())) continue;
                    lastRun = System.currentTimeMillis();
                    backUp(ctx);
                } catch (Exception e) {
                    Debug.error("automatic backup failed", e);
                    ctx.alert("suite", "Automatic backup failed: " + e.getMessage());
                }
            }
        }, "auto-backup");
        timer.setDaemon(true);
        timer.start();
    }

    /** Whether a backup should be written now. */
    static boolean due(Settings s, boolean onDisk, long last, long now) {
        if (!onDisk || !s.getBool(AUTO, false)) return false;
        int minutes = Math.max(1, s.getInt(MINUTES, 30));
        return now - last >= minutes * 60_000L;
    }

    /** Write the backup now; returns the file, or null for a temporary project. */
    public static File backUp(SuiteContext ctx) throws IOException {
        File project = ctx.projectFile();
        if (project == null) return null;
        File target = fileFor(project);
        StateIO.SaveOptions o = StateIO.SaveOptions.fromSettings(ctx);
        o.inScopeOnly = ctx.settings().getBool(IN_SCOPE, false);
        JDialog[] shown = new JDialog[1];
        if (ctx.settings().getBool(PROGRESS, false) && !java.awt.GraphicsEnvironment.isHeadless()) {
            try {
                SwingUtilities.invokeAndWait(() -> {
                    JDialog d = new JDialog(ctx.frame(), "Automatic backup", false);
                    JProgressBar bar = new JProgressBar();
                    bar.setIndeterminate(true);
                    JPanel body = new JPanel(new java.awt.BorderLayout(0, 8));
                    body.setBorder(Ui.pad(16, 20, 16, 20));
                    body.add(Ui.label("Backing up the project to " + target.getName() + "..."), java.awt.BorderLayout.NORTH);
                    body.add(bar, java.awt.BorderLayout.CENTER);
                    d.setContentPane(body);
                    d.pack();
                    d.setLocationRelativeTo(ctx.frame());
                    d.setVisible(true);
                    shown[0] = d;
                });
            } catch (Exception ignored) { }
        }
        try {
            StateIO.save(ctx, target, o);
        } finally {
            JDialog d = shown[0];
            if (d != null) SwingUtilities.invokeLater(d::dispose);
        }
        return target;
    }

    /** On a clean exit the project itself has just been saved, so the backup may go. */
    public static void onCleanExit(SuiteContext ctx) {
        File project = ctx.projectFile();
        if (project != null && ctx.settings().getBool(DELETE_ON_EXIT, false)) fileFor(project).delete();
    }
}