Feature(Go): get_screenshot over the main-connection preview RPC

COMMAND_SCREEN_PREVIEW_REQ (247) / TOKEN_SCREEN_PREVIEW_RSP (248) is a
request/reply pair on the device's main connection: the client captures,
scales and encodes the frame itself, so no sub-connection is opened and no
screen-session slot is taken. get_screenshot can therefore run while a
human is watching the same machine.

The server keeps one pending request per device in a small table, matches
replies by reqId and drops foreign or late ones, and bounds a round trip
with an 8s timer. A missing CLIENT_CAP_SCREEN_PREVIEW bit is refused with
-32005 before anything is sent; region/window/monitor parameters are
refused with the same code because the v2 CLIENT_CAP_SCREEN_REGION
extension is not implemented in this build. Status 4 maps to -32602
because the caller can fix the target, while capture/encode failures stay
-32000.

Windows clients return JPEG directly. Linux clients have no JPEG encoder
and return top-down BGRX, which the server converts (R/B swap, opaque
alpha) before wrapping the frame as an MCP image item. The image item is
new to the content envelope; text tools keep text-only items.

Verified live against x64\Debug\ghost.exe (JPEG FF D8 FF, decoded size
matches metadata, max_width clamped to 64) and against linux/ghost under
WSL (BGRX path, valid JPEG, black frame from the known Xwayland capture
limitation). gofmt/vet clean, unit and -race tests green.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-12 22:07:07 +01:00
1 parent 808747f9d2
commit 5227d4d78e
6 files changed
+947 -37

No files matched your search

+47 -33
View File
@@ -39,6 +39,7 @@ type MyHandler struct {
signer licensing.Signer // CMD_MASTERSETTING signer (local / remote / noop)
loginLimit *wsauth.RateLimiter // per-IP TOKEN_AUTH throttle
connAuthKey []byte // nil = ConnAuth HMAC verification disabled (legacy always-OK)
mcp *mcp.Server // routes main-connection RPC replies (screen preview, file lists)
}
// OnConnect is called when a client connects
@@ -165,6 +166,14 @@ func (h *MyHandler) OnReceive(ctx *connection.Context, data []byte) {
// overlay learns to render arbitrary BGRA images. Drop silently for
// now; the standard IDC_* index (data[10] of every frame header) is
// what we actually use right now.
case protocol.TokenScreenPreviewRsp:
// get_screenshot is a main-connection RPC: the reply carries only a
// reqId, so MCP matches it against the pending request for this device
// and drops a late or foreign one. No other component reads this token.
if h.mcp != nil && h.mcp.OnPacket(ctx, data) {
return
}
h.log.Info("Unhandled command %d from client %d", cmd, ctx.ID)
case protocol.CmdSockDllLoader:
// SOCKET_DLLLOADER (210): the shellcode / memory-DLL loader asks for the
// full DLL payload before it can load the client. Serving it is the MFC
@@ -1204,6 +1213,43 @@ func main() {
log.Info("Trusting forwarded client IPs from: %s", strings.Join(trustedProxies, ", "))
}
// MCP endpoint: a static bearer token independent of the web login. An
// empty YAMA_MCP_TOKEN leaves /mcp unregistered (404) — there is no
// auto-generated token, because a secret that only appears in a log is not
// a secret. Version/commit come from the link-time buildinfo injection.
//
// Built before the TCP servers so each handler can hand main-connection
// RPC replies (screen preview, file lists) to it. Starting its reaper a
// few statements early is harmless; the HTTP route is mounted later.
mcpReadonly := envBool("YAMA_MCP_READONLY")
var mcpUnsubscribe func()
bindAddr := ""
if *httpPort != 0 {
bindAddr = "0.0.0.0" // web server listens on :port (all interfaces)
}
mcpSrv := mcp.New(mcp.Config{
Token: strings.TrimSpace(os.Getenv("YAMA_MCP_TOKEN")),
Readonly: mcpReadonly,
TermIdle: time.Duration(envInt("YAMA_MCP_TERM_IDLE", 180)) * time.Second,
MaxConcurrency: envInt("YAMA_MCP_MAX_CONCURRENCY", 16),
ReadMax: int64(envInt("YAMA_MCP_READ_MAX", 2<<20)),
Version: buildinfo.Version,
StartedAt: time.Now(),
Log: log.WithPrefix("MCP"),
Hub: deviceHub,
History: deviceHistory,
AuditPath: auditFile,
Port: *httpPort,
Bind: bindAddr,
})
if mcpSrv.Enabled() {
// Terminal tools need the hub's shell events (ready/data/closed).
// Unsubscribed again at shutdown, before the TCP servers stop.
mcpUnsubscribe = deviceHub.Subscribe(mcpSrv)
log.Info("MCP endpoint enabled on /mcp (readonly=%v, commit=%s)",
mcpReadonly, buildinfo.Commit)
}
// Create servers for each port
var servers []*server.Server
for _, port := range ports {
@@ -1229,6 +1275,7 @@ func main() {
signer: signer,
loginLimit: authLoginLimit,
connAuthKey: connAuthKey,
mcp: mcpSrv,
}
srv.SetHandler(handler)
@@ -1303,39 +1350,6 @@ func main() {
SSO: ssoDetector,
}
// MCP endpoint: a static bearer token independent of the web login. An
// empty YAMA_MCP_TOKEN leaves /mcp unregistered (404) — there is no
// auto-generated token, because a secret that only appears in a log is not
// a secret. Version/commit come from the link-time buildinfo injection.
mcpReadonly := envBool("YAMA_MCP_READONLY")
var mcpUnsubscribe func()
bindAddr := ""
if *httpPort != 0 {
bindAddr = "0.0.0.0" // web server listens on :port (all interfaces)
}
mcpSrv := mcp.New(mcp.Config{
Token: strings.TrimSpace(os.Getenv("YAMA_MCP_TOKEN")),
Readonly: mcpReadonly,
TermIdle: time.Duration(envInt("YAMA_MCP_TERM_IDLE", 180)) * time.Second,
MaxConcurrency: envInt("YAMA_MCP_MAX_CONCURRENCY", 16),
ReadMax: int64(envInt("YAMA_MCP_READ_MAX", 2<<20)),
Version: buildinfo.Version,
StartedAt: time.Now(),
Log: log.WithPrefix("MCP"),
Hub: deviceHub,
History: deviceHistory,
AuditPath: auditFile,
Port: *httpPort,
Bind: bindAddr,
})
if mcpSrv.Enabled() {
// Terminal tools need the hub's shell events (ready/data/closed).
// Unsubscribed again at shutdown, before the TCP servers stop.
mcpUnsubscribe = deviceHub.Subscribe(mcpSrv)
log.Info("MCP endpoint enabled on /mcp (readonly=%v, commit=%s)",
mcpReadonly, buildinfo.Commit)
}
// Start HTTP server for web UI. Hub gives it read-only access to the
// device registry; the authenticator owns user accounts and session tokens.
httpSrv := web.New(*httpPort, log.WithPrefix("Web"), deviceHub, webAuth).