- 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
78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
//go:build windows
|
|
|
|
package startup
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"golang.org/x/sys/windows/registry"
|
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
|
)
|
|
|
|
func Enabled() bool {
|
|
k, err := registry.OpenKey(registry.CURRENT_USER, config.StartupRunKey, registry.QUERY_VALUE)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
defer k.Close()
|
|
_, _, err = k.GetStringValue(config.StartupValueName)
|
|
return err == nil
|
|
}
|
|
|
|
func Enable() error {
|
|
command, err := installedCommandLine()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
k, _, err := registry.CreateKey(registry.CURRENT_USER, config.StartupRunKey, registry.SET_VALUE)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer k.Close()
|
|
if err := k.SetStringValue(config.StartupValueName, command); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SyncInstalledPath rewrites existing Run/watchdog entries to the AppData path.
|
|
func SyncInstalledPath() error {
|
|
if Enabled() {
|
|
if err := Enable(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if WatchdogEnabled() {
|
|
if err := EnableWatchdog(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Disable() error {
|
|
k, err := registry.OpenKey(registry.CURRENT_USER, config.StartupRunKey, registry.SET_VALUE)
|
|
if err != nil {
|
|
if errors.Is(err, registry.ErrNotExist) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
defer k.Close()
|
|
err = k.DeleteValue(config.StartupValueName)
|
|
if errors.Is(err, registry.ErrNotExist) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func installedCommandLine() (string, error) {
|
|
exe, err := config.InstalledExe()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// Registry Run requires quotes around a path that may contain spaces.
|
|
return `"` + exe + `"`, nil
|
|
}
|