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.8 KB
Raw Blame
package opensuite.core;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Settings > Tasks > Schedule tasks. Each row holds a time, whether it repeats
 * and what to do: pause the tasks, resume them, or scan a URL. A time is
 * "HH:mm" (today, or every day) or "yyyy-MM-dd HH:mm" (that day only).
 */
public final class TaskSchedule {

    public static final String KEY = "tasks.schedule";
    public static final String ONCE = "Once", DAILY = "Daily";
    public static final String PAUSE = "Pause tasks", RESUME = "Resume tasks";

    private static final DateTimeFormatter DAY_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm");
    private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("H:mm");

    private TaskSchedule() {}

    /** One row of the table. */
    public record Entry(String time, String repeat, String task) {
        public boolean daily() { return DAILY.equalsIgnoreCase(repeat.strip()); }
    }

    /** The rows as stored: one per line, cells parted by tabs. Rows without a time or a task are dropped. */
    public static List<Entry> parse(String stored) {
        List<Entry> out = new ArrayList<>();
        if (stored == null) return out;
        for (String line : stored.split("\n")) {
            String[] c = line.split("\t", -1);
            if (c.length < 3 || c[0].isBlank() || c[2].isBlank()) continue;
            out.add(new Entry(c[0].strip(), c[1].strip(), c[2].strip()));
        }
        return out;
    }

    public static String format(List<Entry> entries) {
        StringBuilder b = new StringBuilder();
        for (Entry e : entries) {
            if (b.length() > 0) b.append('\n');
            b.append(e.time()).append('\t').append(e.repeat()).append('\t').append(e.task());
        }
        return b.toString();
    }

    /**
     * When the entry falls on the given day, or null when it does not (a dated
     * entry on another day) or its time cannot be read.
     */
    static LocalDateTime on(Entry e, LocalDate day) {
        try {
            if (e.time().indexOf('-') >= 0) {
                LocalDateTime at = LocalDateTime.parse(e.time(), DAY_TIME);
                return e.daily() ? LocalDateTime.of(day, at.toLocalTime()) : at.toLocalDate().equals(day) ? at : null;
            }
            return LocalDateTime.of(day, LocalTime.parse(e.time(), TIME));
        } catch (DateTimeParseException ex) {
            return null;
        }
    }

    /** The entries whose time lies after {@code since} and no later than {@code now}. */
    public static List<Entry> due(List<Entry> entries, LocalDateTime since, LocalDateTime now) {
        List<Entry> out = new ArrayList<>();
        for (Entry e : entries) {
            for (LocalDate d = since.toLocalDate(); !d.isAfter(now.toLocalDate()); d = d.plusDays(1)) {
                LocalDateTime at = on(e, d);
                if (at != null && at.isAfter(since) && !at.isAfter(now)) { out.add(e); break; }
            }
        }
        return out;
    }

    /** What is left once the due entries have run: an entry that does not repeat is spent. */
    public static List<Entry> remaining(List<Entry> entries, List<Entry> ran) {
        List<Entry> out = new ArrayList<>(entries);
        for (Entry e : ran) if (!e.daily()) out.remove(e);
        return out;
    }

    /** Check the schedule every half minute and carry out what has fallen due. */
    public static void start(SuiteContext ctx) {
        LocalDateTime[] last = { LocalDateTime.now() };
        Thread t = new Thread(() -> {
            while (true) {
                try { Thread.sleep(30_000); } catch (InterruptedException e) { return; }
                try {
                    LocalDateTime now = LocalDateTime.now();
                    List<Entry> all = parse(ctx.settings().get(KEY, ""));
                    List<Entry> ran = due(all, last[0], now);
                    last[0] = now;
                    for (Entry e : ran) run(ctx, e);
                    List<Entry> left = remaining(all, ran);
                    if (left.size() != all.size()) ctx.settings().set(KEY, format(left));
                } catch (RuntimeException ex) {
                    ctx.alert("Tasks", "Scheduled task failed: " + ex.getMessage());
                }
            }
        }, "task-schedule");
        t.setDaemon(true);
        t.start();
    }

    private static void run(SuiteContext ctx, Entry e) {
        String task = e.task();
        ctx.alert("Tasks", "Scheduled task: " + task);
        if (task.equalsIgnoreCase(PAUSE)) ctx.pauseAllTasks();
        else if (task.equalsIgnoreCase(RESUME)) ctx.resumeAllTasks();
        else ctx.launchScan(task.matches("(?i)^https?://.*") ? task : "https://" + task, true);
    }
}