- 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
81 lines
1.7 KiB
Go
81 lines
1.7 KiB
Go
//go:build windows
|
|
|
|
package startup
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
|
"tea.chunkbyte.com/kato/go-worm/lib/helpers"
|
|
)
|
|
|
|
func WatchdogEnabled() bool {
|
|
return taskExists(config.WatchdogTaskName) || taskExists(config.WatchdogTaskNameLegacy)
|
|
}
|
|
|
|
func EnableWatchdog() error {
|
|
exe, err := config.InstalledExe()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := runSchtasksCmdLine(schtasksCreateCmdLine(config.WatchdogTaskName, ensureTaskTR(exe))); err != nil {
|
|
return err
|
|
}
|
|
_ = runSchtasks("/Delete", "/TN", config.WatchdogTaskNameLegacy, "/F")
|
|
return nil
|
|
}
|
|
|
|
func DisableWatchdog() error {
|
|
var first error
|
|
for _, name := range []string{config.WatchdogTaskName, config.WatchdogTaskNameLegacy} {
|
|
err := runSchtasks("/Delete", "/TN", name, "/F")
|
|
if err == nil || taskNotFound(err) {
|
|
continue
|
|
}
|
|
if first == nil {
|
|
first = err
|
|
}
|
|
}
|
|
return first
|
|
}
|
|
|
|
func taskExists(name string) bool {
|
|
return runSchtasks("/Query", "/TN", name) == nil
|
|
}
|
|
|
|
func runSchtasksCmdLine(cmdLine string) error {
|
|
cmd := exec.Command("schtasks")
|
|
attr := helpers.HiddenSysProcAttr()
|
|
attr.CmdLine = cmdLine
|
|
cmd.SysProcAttr = attr
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
msg := strings.TrimSpace(string(out))
|
|
if msg == "" {
|
|
msg = err.Error()
|
|
}
|
|
return fmt.Errorf("schtasks: %s", msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runSchtasks(args ...string) error {
|
|
cmd := exec.Command("schtasks", args...)
|
|
cmd.SysProcAttr = helpers.HiddenSysProcAttr()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
msg := strings.TrimSpace(string(out))
|
|
if msg == "" {
|
|
msg = err.Error()
|
|
}
|
|
return fmt.Errorf("schtasks: %s", msg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func taskNotFound(err error) bool {
|
|
return strings.Contains(strings.ToLower(err.Error()), "cannot find")
|
|
}
|