mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
feat: agent bridge wayland and gamescope coverage (#406)
* feat: run a guest agent in every session for commands, focus and the Steam client's DevTools * feat: give the agent bridge game, input, UI and experiment commands behind a Setup toggle * feat: droiddeckctl commands for the new bridge, a scenario runner and contract tests * feat: run cleanup steps after every scenario run so a failure cannot leak into the next * feat: restart a session that died between scenario runs with start --reuse * fix: give droiddeckctl stop its own, longer wait for the session's logs * fix: harden debugging tools access and output
This commit is contained in:
1 parent
37e89c5b4d
commit
d231564f8e
42 files changed
+3787
-168
No files matched your search
Executable
+245
@@ -0,0 +1,245 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run a device scenario through droiddeckctl, repeatedly, and report each run.
|
||||
|
||||
A scenario is JSON (stdlib only, no YAML):
|
||||
|
||||
{
|
||||
"name": "skyrim-focus",
|
||||
"vars": {"appId": 489830},
|
||||
"repeat": 6,
|
||||
"setup": [["wake"]],
|
||||
"before": [["start", "steam", "--wait", "--reuse", "--timeout", "180"]],
|
||||
"steps": [["launch", "${appId}", "--wait", "--timeout", "240"],
|
||||
{"assert": "session.focus.focusedApp", "equals": "${appId}"},
|
||||
{"screenshot": "in-game"},
|
||||
["quit", "${appId}"]],
|
||||
"after": [["quit", "${appId}"]],
|
||||
"teardown": [["stop"]]
|
||||
}
|
||||
|
||||
A list step is a droiddeckctl command line; its JSON answer becomes "last". An assert step checks
|
||||
a dotted path in the last answer ("equals", "in", "contains", "exists", "not"); {"sleep": s},
|
||||
{"screenshot": "name"} and {"record": "name", "seconds": s} do what they say. ${var} is replaced
|
||||
from "vars" and --var KEY=VALUE. setup and teardown run once; steps run "repeat" times (or
|
||||
--repeat), each preceded by "before" (a failure there fails the run) and followed by "after"
|
||||
whether it passed or not, so a failed run cannot leave a game running into the next and a session
|
||||
that died in one run is started again for the next. A failing run gets a screenshot, the focus state and the session's logs in
|
||||
its folder.
|
||||
|
||||
tools/droiddeck-scenario tools/scenarios/steam-game-focus.json --var appId=489830 --out ./runs
|
||||
|
||||
Exit status: 0 when every run passed, 1 when any failed, 2 for a bad scenario or a failed setup.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
sys.path.insert(0, str(HERE))
|
||||
_loader = importlib.machinery.SourceFileLoader("droiddeckctl", str(HERE / "droiddeckctl"))
|
||||
_spec = importlib.util.spec_from_loader("droiddeckctl", _loader)
|
||||
ctl = importlib.util.module_from_spec(_spec)
|
||||
_loader.exec_module(ctl)
|
||||
|
||||
|
||||
class StepFailed(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def write_json(path: Path, value) -> None:
|
||||
with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600), "w") as stream:
|
||||
os.fchmod(stream.fileno(), 0o600)
|
||||
json.dump(value, stream, indent=2, sort_keys=True)
|
||||
|
||||
|
||||
def substitute(value, variables: dict):
|
||||
if isinstance(value, str):
|
||||
whole = re.fullmatch(r"\$\{(\w+)\}", value)
|
||||
if whole and whole.group(1) in variables:
|
||||
return variables[whole.group(1)]
|
||||
return re.sub(r"\$\{(\w+)\}", lambda m: str(variables.get(m.group(1), m.group(0))), value)
|
||||
if isinstance(value, list):
|
||||
return [substitute(v, variables) for v in value]
|
||||
if isinstance(value, dict):
|
||||
return {k: substitute(v, variables) for k, v in value.items()}
|
||||
return value
|
||||
|
||||
|
||||
def lookup(data, path: str):
|
||||
for part in path.split("."):
|
||||
if isinstance(data, list) and part.isdigit():
|
||||
data = data[int(part)] if int(part) < len(data) else None
|
||||
elif isinstance(data, dict):
|
||||
data = data.get(part)
|
||||
else:
|
||||
return None
|
||||
return data
|
||||
|
||||
|
||||
def check(step: dict, last: dict) -> None:
|
||||
path = step["assert"]
|
||||
value = lookup(last, path)
|
||||
if "equals" in step and value != step["equals"]:
|
||||
raise StepFailed(f"{path} is {value!r}, expected {step['equals']!r}")
|
||||
if "not" in step and value == step["not"]:
|
||||
raise StepFailed(f"{path} is {value!r}")
|
||||
if "in" in step and value not in step["in"]:
|
||||
raise StepFailed(f"{path} is {value!r}, expected one of {step['in']!r}")
|
||||
if "contains" in step and (not isinstance(value, (list, str)) or step["contains"] not in value):
|
||||
raise StepFailed(f"{path} is {value!r}, expected it to contain {step['contains']!r}")
|
||||
if step.get("exists") is True and value is None:
|
||||
raise StepFailed(f"{path} is missing")
|
||||
|
||||
|
||||
class Runner:
|
||||
def __init__(self, adb: str, serial: str, out: Path, global_args: list[str]):
|
||||
self.adb = adb
|
||||
self.serial = serial
|
||||
self.out = out
|
||||
self.global_args = global_args
|
||||
self.parser = ctl.build_parser()
|
||||
|
||||
def ctl(self, words: list) -> dict:
|
||||
args = self.parser.parse_args(self.global_args + [str(w) for w in words])
|
||||
try:
|
||||
return ctl.execute(args, self.adb, self.serial)
|
||||
except ctl.ControlError as exc:
|
||||
raise StepFailed(f"droiddeckctl {' '.join(map(str, words))}: {exc.code}: {exc}") from exc
|
||||
|
||||
def run_steps(self, steps: list, folder: Path, log: list) -> None:
|
||||
last: dict = {}
|
||||
for index, step in enumerate(steps):
|
||||
started = time.monotonic()
|
||||
entry = {"step": index, "what": step}
|
||||
try:
|
||||
if isinstance(step, list):
|
||||
last = self.ctl(step)
|
||||
entry["result"] = last
|
||||
elif "assert" in step:
|
||||
check(step, last)
|
||||
elif "sleep" in step:
|
||||
time.sleep(float(step["sleep"]))
|
||||
elif "screenshot" in step:
|
||||
entry["result"] = ctl.screenshot(self.adb, self.serial, str(folder / f"{step['screenshot']}.png"),
|
||||
step.get("display"))
|
||||
elif "record" in step:
|
||||
entry["result"] = ctl.record(self.adb, self.serial, str(folder / f"{step['record']}.mp4"),
|
||||
float(step.get("seconds", 10)), step.get("display"))
|
||||
else:
|
||||
raise StepFailed(f"Unknown step {step!r}")
|
||||
entry["ok"] = True
|
||||
except (StepFailed, ctl.ControlError) as exc:
|
||||
entry["ok"] = False
|
||||
entry["error"] = str(exc)
|
||||
raise StepFailed(f"step {index} {json.dumps(step)}: {exc}") from exc
|
||||
finally:
|
||||
entry["seconds"] = round(time.monotonic() - started, 2)
|
||||
log.append(entry)
|
||||
|
||||
def evidence(self, folder: Path) -> dict:
|
||||
"""What a failed run leaves behind: the screen, gamescope's focus, the session's logs."""
|
||||
found = {}
|
||||
for name, action in (
|
||||
("screenshot", lambda: ctl.screenshot(self.adb, self.serial, str(folder / "failure.png"), None)),
|
||||
("focus", lambda: self.ctl(["focus"])),
|
||||
("logs", lambda: self.ctl(["logs", "latest", str(folder / "session")])),
|
||||
):
|
||||
try:
|
||||
found[name] = action()
|
||||
except Exception as exc: # evidence is best effort
|
||||
found[name] = {"ok": False, "error": str(exc)}
|
||||
write_json(folder / "evidence.json", found)
|
||||
return {k: v.get("ok", True) if isinstance(v, dict) else True for k, v in found.items()}
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("scenario")
|
||||
parser.add_argument("--var", action="append", default=[], metavar="KEY=VALUE")
|
||||
parser.add_argument("--repeat", type=int)
|
||||
parser.add_argument("--out", default="scenario-runs")
|
||||
parser.add_argument("--adb")
|
||||
parser.add_argument("--serial")
|
||||
parser.add_argument("--keep-going", action="store_true", help="run every repeat even after a failure")
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
try:
|
||||
scenario = json.loads(Path(args.scenario).read_text())
|
||||
except (OSError, ValueError) as exc:
|
||||
print(json.dumps({"ok": False, "error": f"Cannot read scenario: {exc}"}))
|
||||
return 2
|
||||
variables = dict(scenario.get("vars", {}))
|
||||
for item in args.var:
|
||||
key, _, value = item.partition("=")
|
||||
variables[key] = int(value) if value.lstrip("-").isdigit() else value
|
||||
scenario = substitute(scenario, variables)
|
||||
repeat = args.repeat or int(scenario.get("repeat", 1))
|
||||
|
||||
adb = ctl.adb_path(args.adb)
|
||||
serial = ctl.resolve_serial(adb, args.serial, log=True)
|
||||
out = Path(args.out).expanduser().resolve() / f"{scenario.get('name', 'scenario')}-{time.strftime('%Y%m%d-%H%M%S')}"
|
||||
out.mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||
global_args = (["--adb", adb] if args.adb else []) + ["--serial", serial]
|
||||
runner = Runner(adb, serial, out, global_args)
|
||||
report = {"name": scenario.get("name"), "serial": serial, "vars": variables, "repeat": repeat,
|
||||
"out": str(out), "runs": []}
|
||||
|
||||
status = 0
|
||||
setup_log: list = []
|
||||
try:
|
||||
runner.run_steps(scenario.get("setup", []), out, setup_log)
|
||||
for run in range(1, repeat + 1):
|
||||
folder = out / f"run-{run:02d}"
|
||||
folder.mkdir(mode=0o700)
|
||||
log: list = []
|
||||
started = time.monotonic()
|
||||
result = {"run": run, "folder": str(folder)}
|
||||
try:
|
||||
runner.run_steps(scenario.get("before", []) + scenario.get("steps", []), folder, log)
|
||||
result["ok"] = True
|
||||
except StepFailed as exc:
|
||||
result["ok"] = False
|
||||
result["error"] = str(exc)
|
||||
result["evidence"] = runner.evidence(folder)
|
||||
status = 1
|
||||
after: list = []
|
||||
try:
|
||||
runner.run_steps(scenario.get("after", []), folder, after)
|
||||
except StepFailed as exc:
|
||||
result["afterError"] = str(exc)
|
||||
log.extend({**entry, "after": True} for entry in after)
|
||||
result["seconds"] = round(time.monotonic() - started, 2)
|
||||
write_json(folder / "steps.json", log)
|
||||
report["runs"].append(result)
|
||||
print(f"run {run}/{repeat}: {'PASS' if result['ok'] else 'FAIL ' + result['error']}", file=sys.stderr)
|
||||
if not result["ok"] and not args.keep_going:
|
||||
break
|
||||
except StepFailed as exc:
|
||||
report["setupError"] = str(exc)
|
||||
status = 2
|
||||
finally:
|
||||
teardown_log: list = []
|
||||
try:
|
||||
runner.run_steps(scenario.get("teardown", []), out, teardown_log)
|
||||
except StepFailed as exc:
|
||||
report["teardownError"] = str(exc)
|
||||
write_json(out / "setup.json", setup_log + teardown_log)
|
||||
|
||||
passed = sum(1 for r in report["runs"] if r["ok"])
|
||||
report.update({"ok": status == 0, "passed": passed, "failed": len(report["runs"]) - passed})
|
||||
write_json(out / "report.json", report)
|
||||
print(json.dumps(report, indent=2, sort_keys=True))
|
||||
return status
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+470
-96
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Control a debug DroidDeck build over ADB."""
|
||||
"""Control a DroidDeck build over ADB (docs/agent-control.md)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -7,16 +7,21 @@ import argparse
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from adb_device import DeviceResolutionError, resolve_serial
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
||||
from adb_device import DeviceResolutionError, resolve_serial # noqa: E402
|
||||
|
||||
|
||||
AUTHORITY = "content://com.droiddeck.launcher.agent"
|
||||
PACKAGE = "com.droiddeck.launcher"
|
||||
STEAM_CLIENT_APP = 769
|
||||
REQUEST_CAP = 256 * 1024
|
||||
|
||||
|
||||
class ControlError(RuntimeError):
|
||||
@@ -46,7 +51,7 @@ def adb_path(configured: str | None) -> str:
|
||||
raise ControlError("adb not found; set ADB, ANDROID_HOME, or ANDROID_SDK_ROOT", "ADB_NOT_FOUND")
|
||||
|
||||
|
||||
def run_adb(adb: str, serial: str, *args: str, timeout: int = 30) -> subprocess.CompletedProcess[bytes]:
|
||||
def run_adb(adb: str, serial: str, *args: str, timeout: float = 30) -> subprocess.CompletedProcess[bytes]:
|
||||
try:
|
||||
return subprocess.run(
|
||||
[adb, "-s", serial, *args], capture_output=True, check=False, timeout=timeout
|
||||
@@ -59,7 +64,7 @@ def _content_result(output: str) -> dict:
|
||||
marker = "json="
|
||||
start = output.find(marker)
|
||||
if start < 0:
|
||||
detail = output.strip() or "The debug agent provider returned no result"
|
||||
detail = output.strip() or "The agent provider returned no result"
|
||||
raise ControlError(detail, "INVALID_PROVIDER_RESPONSE")
|
||||
raw = output[start + len(marker):].lstrip()
|
||||
try:
|
||||
@@ -71,14 +76,22 @@ def _content_result(output: str) -> dict:
|
||||
raise ControlError("Could not decode the agent provider response", "INVALID_PROVIDER_RESPONSE") from exc
|
||||
|
||||
|
||||
def provider_call(adb: str, serial: str, method: str, arg: str | None = None, request: dict | None = None) -> dict:
|
||||
def encode_request(request: dict) -> str:
|
||||
encoded = base64.b64encode(json.dumps(request, separators=(",", ":")).encode()).decode()
|
||||
if len(encoded) > REQUEST_CAP:
|
||||
raise ControlError("The encoded request exceeds 256 KiB", "INVALID_REQUEST")
|
||||
return encoded
|
||||
|
||||
|
||||
def provider_call(adb: str, serial: str, method: str, arg: str | None = None, request: dict | None = None,
|
||||
timeout: float = 30) -> dict:
|
||||
command = ["shell", "content", "call", "--uri", AUTHORITY, "--method", method]
|
||||
if arg is not None:
|
||||
command.extend(["--arg", arg])
|
||||
if request is not None:
|
||||
encoded = base64.b64encode(json.dumps(request, separators=(",", ":")).encode()).decode()
|
||||
encoded = encode_request(request)
|
||||
command.extend(["--extra", f"request:s:{encoded}"])
|
||||
result = run_adb(adb, serial, *command)
|
||||
result = run_adb(adb, serial, *command, timeout=timeout)
|
||||
output = (result.stdout + result.stderr).decode(errors="replace")
|
||||
response = _content_result(output)
|
||||
if result.returncode != 0 and response.get("ok") is not False:
|
||||
@@ -86,8 +99,16 @@ def provider_call(adb: str, serial: str, method: str, arg: str | None = None, re
|
||||
return response
|
||||
|
||||
|
||||
def checked(response: dict, default_code: str) -> dict:
|
||||
"""A provider or guest answer, raised as a ControlError when it carries ok: false."""
|
||||
if response.get("ok") is False:
|
||||
error = response.get("error", {})
|
||||
raise ControlError(error.get("message", default_code), error.get("code", default_code))
|
||||
return response
|
||||
|
||||
|
||||
def start_agent_activity(adb: str, serial: str, request: dict) -> None:
|
||||
encoded = base64.b64encode(json.dumps(request, separators=(",", ":")).encode()).decode()
|
||||
encoded = encode_request(request)
|
||||
result = run_adb(
|
||||
adb,
|
||||
serial,
|
||||
@@ -96,7 +117,7 @@ def start_agent_activity(adb: str, serial: str, request: dict) -> None:
|
||||
"start",
|
||||
"-W",
|
||||
"-n",
|
||||
"com.droiddeck.launcher/.agent.AgentStartActivity",
|
||||
f"{PACKAGE}/.agent.AgentStartActivity",
|
||||
"--es",
|
||||
"request",
|
||||
encoded,
|
||||
@@ -126,23 +147,18 @@ def confirm_start(adb: str, serial: str, request: dict, previous: dict) -> dict:
|
||||
|
||||
|
||||
def get_state(adb: str, serial: str) -> dict:
|
||||
response = provider_call(adb, serial, "state")
|
||||
if response.get("ok") is False:
|
||||
error = response.get("error", {})
|
||||
raise ControlError(error.get("message", "state failed"), error.get("code", "STATE_FAILED"))
|
||||
return response
|
||||
return checked(provider_call(adb, serial, "state"), "STATE_FAILED")
|
||||
|
||||
|
||||
def wait_for_phase(adb: str, serial: str, wanted: str, timeout: float) -> dict:
|
||||
def wait_until(adb: str, serial: str, describe: str, done, timeout: float, poll: float = 1.0) -> dict:
|
||||
"""Poll state until done(state) is true. A failed session ends the wait at once."""
|
||||
deadline = time.monotonic() + timeout
|
||||
last_state: dict | None = None
|
||||
while True:
|
||||
last_state = get_state(adb, serial)
|
||||
session = last_state.get("session", {})
|
||||
phase = session.get("phase")
|
||||
if phase == wanted:
|
||||
return last_state
|
||||
if phase == "FAILED" and wanted != "FAILED":
|
||||
state = get_state(adb, serial)
|
||||
session = state.get("session", {})
|
||||
if done(state):
|
||||
return state
|
||||
if session.get("phase") == "FAILED":
|
||||
failure = session.get("failure") or {}
|
||||
raise ControlError(
|
||||
f"Session failed ({failure.get('code', 'FAILED')}): {failure.get('message', 'unknown error')}",
|
||||
@@ -150,10 +166,35 @@ def wait_for_phase(adb: str, serial: str, wanted: str, timeout: float) -> dict:
|
||||
)
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
focus = session.get("focus") or {}
|
||||
raise ControlError(
|
||||
f"Timed out waiting for {wanted}; last phase was {phase}", "TIMEOUT"
|
||||
f"Timed out waiting for {describe}; phase {session.get('phase')}, focused app {focus.get('focusedApp')}",
|
||||
"TIMEOUT",
|
||||
)
|
||||
time.sleep(min(1.0, remaining))
|
||||
time.sleep(min(poll, remaining))
|
||||
|
||||
|
||||
def wait_for_phase(adb: str, serial: str, wanted: str, timeout: float) -> dict:
|
||||
if wanted == "FAILED":
|
||||
deadline = time.monotonic() + timeout
|
||||
while True:
|
||||
state = get_state(adb, serial)
|
||||
if state.get("session", {}).get("phase") == "FAILED":
|
||||
return state
|
||||
if time.monotonic() > deadline:
|
||||
raise ControlError("Timed out waiting for FAILED", "TIMEOUT")
|
||||
time.sleep(1)
|
||||
return wait_until(adb, serial, wanted, lambda s: s.get("session", {}).get("phase") == wanted, timeout)
|
||||
|
||||
|
||||
def focused(state: dict, app_id: int | None) -> bool:
|
||||
"""The game has focus the way a player sees it: gamescope focuses it and it is in the list the
|
||||
Steam client orders the base layer from. app_id None means any game (not the client)."""
|
||||
focus = state.get("session", {}).get("focus") or {}
|
||||
current = focus.get("focusedApp")
|
||||
if app_id is None:
|
||||
return current not in (None, 0, STEAM_CLIENT_APP)
|
||||
return current == app_id and app_id in (focus.get("focusableApps") or [])
|
||||
|
||||
|
||||
def wait_for_artifacts(adb: str, serial: str, state: dict, timeout: float) -> dict:
|
||||
@@ -174,14 +215,123 @@ def add_serial(response: dict, serial: str) -> dict:
|
||||
return response
|
||||
|
||||
|
||||
# --- displays ------------------------------------------------------------------------------------
|
||||
|
||||
def displays(adb: str, serial: str) -> list[dict]:
|
||||
"""Logical displays with the SurfaceFlinger (physical) id screencap and screenrecord want."""
|
||||
result = run_adb(adb, serial, "shell", "dumpsys", "display", timeout=30)
|
||||
text = result.stdout.decode(errors="replace")
|
||||
devices = dict(re.findall(r'DisplayDeviceInfo\{"([^"]+)": uniqueId="local:(\d+)"', text))
|
||||
out = []
|
||||
for block in re.split(r"\n Display (?=\d+:)", text)[1:]:
|
||||
logical = int(block.split(":", 1)[0])
|
||||
device = re.search(r"mPrimaryDisplayDevice=(.+)", block)
|
||||
size = re.search(r"real (\d+) x (\d+)", block)
|
||||
name = device.group(1).strip() if device else None
|
||||
out.append({
|
||||
"display": logical,
|
||||
"name": name,
|
||||
"physicalId": devices.get(name) if name else None,
|
||||
"size": [int(size.group(1)), int(size.group(2))] if size else None,
|
||||
})
|
||||
return out
|
||||
|
||||
|
||||
def physical_display(adb: str, serial: str, logical: int | None) -> str | None:
|
||||
if logical is None:
|
||||
return None
|
||||
for d in displays(adb, serial):
|
||||
if d["display"] == logical:
|
||||
if not d["physicalId"]:
|
||||
raise ControlError(f"Display {logical} has no physical id to capture", "NO_SUCH_DISPLAY")
|
||||
return d["physicalId"]
|
||||
raise ControlError(f"No display {logical}; see `droiddeckctl displays`", "NO_SUCH_DISPLAY")
|
||||
|
||||
|
||||
def screenshot(adb: str, serial: str, path: str, display: int | None) -> dict:
|
||||
destination = Path(path).expanduser().resolve()
|
||||
physical = physical_display(adb, serial, display)
|
||||
command = ["exec-out", "screencap", "-p"] + (["-d", physical] if physical else [])
|
||||
result = run_adb(adb, serial, *command, timeout=30)
|
||||
if result.returncode != 0 or not result.stdout.startswith(b"\x89PNG"):
|
||||
detail = result.stderr.decode(errors="replace").strip()
|
||||
raise ControlError(detail or "screencap failed", "SCREENSHOT_FAILED")
|
||||
try:
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_bytes(result.stdout)
|
||||
except OSError as exc:
|
||||
raise ControlError(str(exc), "FILE_IO") from exc
|
||||
return {"ok": True, "command": "screenshot", "path": str(destination), "display": display}
|
||||
|
||||
|
||||
def record(adb: str, serial: str, path: str, seconds: float, display: int | None) -> dict:
|
||||
destination = Path(path).expanduser().resolve()
|
||||
remote = "/data/local/tmp/droiddeckctl-record.mp4"
|
||||
physical = physical_display(adb, serial, display)
|
||||
command = ["shell", "screenrecord", "--time-limit", str(max(1, min(180, int(seconds))))]
|
||||
if physical:
|
||||
command += ["--display-id", physical]
|
||||
result = run_adb(adb, serial, *command, remote, timeout=seconds + 30)
|
||||
if result.returncode != 0:
|
||||
raise ControlError(result.stderr.decode(errors="replace").strip() or "screenrecord failed", "RECORD_FAILED")
|
||||
try:
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
except OSError as exc:
|
||||
raise ControlError(str(exc), "FILE_IO") from exc
|
||||
pulled = run_adb(adb, serial, "pull", remote, str(destination), timeout=120)
|
||||
run_adb(adb, serial, "shell", "rm", "-f", remote)
|
||||
if pulled.returncode != 0:
|
||||
raise ControlError(pulled.stderr.decode(errors="replace").strip() or "adb pull failed", "RECORD_FAILED")
|
||||
return {"ok": True, "command": "record", "path": str(destination), "seconds": seconds, "display": display}
|
||||
|
||||
|
||||
# --- commands ------------------------------------------------------------------------------------
|
||||
|
||||
def start_session(adb: str, serial: str, request: dict) -> dict:
|
||||
if request.get("mode") == "run":
|
||||
require_commands(adb, serial)
|
||||
current = get_state(adb, serial)
|
||||
if current["session"].get("phase") not in {"IDLE", "FAILED"} or current["session"].get("running"):
|
||||
raise ControlError("A session is already starting or running", "SESSION_ACTIVE")
|
||||
start_agent_activity(adb, serial, request)
|
||||
return confirm_start(adb, serial, request, current["session"])
|
||||
|
||||
|
||||
def require_commands(adb: str, serial: str) -> dict:
|
||||
access = checked(provider_call(adb, serial, "access"), "ACCESS_FAILED")
|
||||
if access.get("commands") is not True:
|
||||
raise ControlError("Debugging tools are off: turn on Setup > Session > Debugging tools on the device",
|
||||
"AGENT_COMMANDS_DISABLED")
|
||||
return access
|
||||
|
||||
|
||||
def input_request(args: argparse.Namespace) -> dict:
|
||||
kind = args.input_type
|
||||
if kind == "key":
|
||||
return {"type": "key", "key": args.key, "action": args.action, "modifiers": args.mod or [], "holdMs": args.hold}
|
||||
if kind == "text":
|
||||
return {"type": "text", "text": args.text, "commit": args.commit}
|
||||
if kind == "tap":
|
||||
return {"type": "tap", "x": args.x, "y": args.y, "space": args.space, "holdMs": args.hold}
|
||||
if kind == "swipe":
|
||||
return {"type": "swipe", "x": args.x, "y": args.y, "x2": args.x2, "y2": args.y2,
|
||||
"space": args.space, "durationMs": args.duration}
|
||||
if kind == "pad":
|
||||
return {"type": "pad", "button": args.button, "action": args.action, "holdMs": args.hold}
|
||||
if kind == "stick":
|
||||
return {"type": "stick", "stick": args.stick, "x": args.x, "y": args.y, "holdMs": args.hold}
|
||||
if kind == "trigger":
|
||||
return {"type": "trigger", "trigger": args.trigger, "value": args.value, "holdMs": args.hold}
|
||||
raise ControlError(f"Unknown input type {kind}", "INVALID_INPUT")
|
||||
|
||||
|
||||
def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
|
||||
if args.command == "state":
|
||||
command = args.command
|
||||
|
||||
if command == "state":
|
||||
return add_serial(get_state(adb, serial), serial)
|
||||
|
||||
if args.command == "start":
|
||||
current = get_state(adb, serial)
|
||||
if current["session"].get("phase") not in {"IDLE", "FAILED"} or current["session"].get("running"):
|
||||
raise ControlError("A session is already starting or running", "SESSION_ACTIVE")
|
||||
if command == "start":
|
||||
request = {"mode": args.mode}
|
||||
if args.mode == "steam":
|
||||
if args.ui == "desktop":
|
||||
@@ -190,59 +340,152 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
|
||||
if not args.url.startswith("steam://"):
|
||||
raise ControlError("Steam URL must start with steam://", "INVALID_STEAM_URL")
|
||||
request["steamUrl"] = args.url
|
||||
start_agent_activity(adb, serial, request)
|
||||
started = confirm_start(adb, serial, request, current["session"])
|
||||
if args.reuse:
|
||||
# A scenario's per-run "before": keep a session of this mode that is already up, so only
|
||||
# a run after one that ended (a crash, the low-memory killer) starts a new one.
|
||||
current = get_state(adb, serial)["session"]
|
||||
wanted = {"steam": "steam", "desktop": "lxqt"}[args.mode]
|
||||
if current.get("running") and current.get("mode") == wanted and current.get("phase") not in {"STOPPING", "FAILED"}:
|
||||
state = wait_for_phase(adb, serial, "READY", args.timeout) if args.wait else get_state(adb, serial)
|
||||
return add_serial(dict(state, reused=True), serial)
|
||||
started = start_session(adb, serial, request)
|
||||
if args.wait:
|
||||
return add_serial(wait_for_phase(adb, serial, "READY", args.timeout), serial)
|
||||
session = started["session"]
|
||||
return add_serial({
|
||||
"ok": True,
|
||||
"command": "start",
|
||||
"mode": args.mode,
|
||||
"sessionId": session.get("id"),
|
||||
"phase": session.get("phase"),
|
||||
}, serial)
|
||||
return add_serial({"ok": True, "command": "start", "mode": args.mode,
|
||||
"sessionId": session.get("id"), "phase": session.get("phase")}, serial)
|
||||
|
||||
if args.command == "run":
|
||||
current = get_state(adb, serial)
|
||||
if current["session"].get("phase") not in {"IDLE", "FAILED"} or current["session"].get("running"):
|
||||
raise ControlError("A session is already starting or running", "SESSION_ACTIVE")
|
||||
if command == "run":
|
||||
program_args = args.arguments[1:] if args.arguments[:1] == ["--"] else args.arguments
|
||||
request = {"mode": "run", "program": args.program, "programArgs": program_args}
|
||||
start_agent_activity(adb, serial, request)
|
||||
started = confirm_start(adb, serial, request, current["session"])
|
||||
started = start_session(adb, serial, {"mode": "run", "program": args.program, "programArgs": program_args})
|
||||
session = started["session"]
|
||||
return add_serial({
|
||||
"ok": True,
|
||||
"command": "start",
|
||||
"mode": "run",
|
||||
"sessionId": session.get("id"),
|
||||
"phase": session.get("phase"),
|
||||
}, serial)
|
||||
return add_serial({"ok": True, "command": "start", "mode": "run",
|
||||
"sessionId": session.get("id"), "phase": session.get("phase")}, serial)
|
||||
|
||||
if args.command == "wait":
|
||||
if command == "wait":
|
||||
if args.phase == "focused":
|
||||
if args.app_id is None:
|
||||
raise ControlError("wait focused needs an app id", "INVALID_ARGUMENTS")
|
||||
return add_serial(wait_until(adb, serial, f"app {args.app_id} to have focus",
|
||||
lambda s: focused(s, args.app_id), args.timeout, 0.5), serial)
|
||||
if args.phase == "game":
|
||||
return add_serial(wait_until(adb, serial, "a game to have focus",
|
||||
lambda s: focused(s, None), args.timeout, 0.5), serial)
|
||||
phases = {"ready": "READY", "idle": "IDLE", "failed": "FAILED"}
|
||||
return add_serial(wait_for_phase(adb, serial, phases[args.phase], args.timeout), serial)
|
||||
|
||||
if args.command == "stop":
|
||||
if command == "stop":
|
||||
current = get_state(adb, serial)
|
||||
if current["session"].get("phase") in {"IDLE", "FAILED"}:
|
||||
return add_serial(wait_for_artifacts(adb, serial, current, args.timeout), serial)
|
||||
response = provider_call(adb, serial, "stop")
|
||||
if response.get("ok") is False:
|
||||
error = response.get("error", {})
|
||||
raise ControlError(error.get("message", "stop failed"), error.get("code", "STOP_FAILED"))
|
||||
return add_serial(wait_for_artifacts(adb, serial, current, args.artifact_timeout), serial)
|
||||
checked(provider_call(adb, serial, "stop"), "STOP_FAILED")
|
||||
state = wait_for_phase(adb, serial, "IDLE", args.timeout)
|
||||
return add_serial(wait_for_artifacts(adb, serial, state, args.timeout), serial)
|
||||
return add_serial(wait_for_artifacts(adb, serial, state, args.artifact_timeout), serial)
|
||||
|
||||
if args.command == "resume":
|
||||
response = provider_call(adb, serial, "resume")
|
||||
if response.get("ok") is False:
|
||||
error = response.get("error", {})
|
||||
raise ControlError(error.get("message", "resume failed"), error.get("code", "RESUME_FAILED"))
|
||||
if command == "resume":
|
||||
return add_serial(checked(provider_call(adb, serial, "resume"), "RESUME_FAILED"), serial)
|
||||
|
||||
if command == "launch":
|
||||
response = checked(provider_call(adb, serial, "launch", request={"appId": str(args.app_id)}), "LAUNCH_FAILED")
|
||||
if args.wait:
|
||||
state = wait_until(adb, serial, f"app {args.app_id} to have focus",
|
||||
lambda s: focused(s, args.app_id), args.timeout, 0.5)
|
||||
response["focus"] = state["session"].get("focus")
|
||||
return add_serial(response, serial)
|
||||
|
||||
if args.command == "logs":
|
||||
if command == "quit":
|
||||
response = provider_call(adb, serial, "quit", request={"appId": args.app_id, "timeout": args.timeout},
|
||||
timeout=args.timeout + 40)
|
||||
return add_serial(checked(response, "QUIT_FAILED"), serial)
|
||||
|
||||
if command == "focus":
|
||||
return add_serial(checked(provider_call(adb, serial, "focus", timeout=40), "FOCUS_FAILED"), serial)
|
||||
|
||||
if command == "guest":
|
||||
argv = args.argv[1:] if args.argv[:1] == ["--"] else args.argv
|
||||
if not argv:
|
||||
raise ControlError("guest needs a command after --", "INVALID_ARGUMENTS")
|
||||
env = {}
|
||||
for item in args.env or []:
|
||||
key, sep, value = item.partition("=")
|
||||
if not sep:
|
||||
raise ControlError(f"--env takes KEY=VALUE, not {item}", "INVALID_ARGUMENTS")
|
||||
env[key] = value
|
||||
request = {"argv": argv, "timeout": args.timeout, "env": env}
|
||||
if args.cwd:
|
||||
request["cwd"] = args.cwd
|
||||
if args.stdin is not None:
|
||||
request["stdin"] = args.stdin
|
||||
response = checked(provider_call(adb, serial, "guest", request=request, timeout=args.timeout + 40), "GUEST_FAILED")
|
||||
if args.check and response.get("exitCode") != 0:
|
||||
raise ControlError(f"{argv[0]} exited with {response.get('exitCode')}: "
|
||||
f"{(response.get('stderr') or {}).get('text', '')[-400:]}", "GUEST_EXIT")
|
||||
return add_serial(response, serial)
|
||||
|
||||
if command == "cdp":
|
||||
if args.cdp_op == "targets":
|
||||
request = {"op": "targets"}
|
||||
else:
|
||||
request = {"op": "eval", "expression": args.expression, "awaitPromise": not args.no_await,
|
||||
"timeout": args.timeout}
|
||||
if args.target:
|
||||
request["target"] = args.target
|
||||
timeout = getattr(args, "timeout", 15)
|
||||
return add_serial(checked(provider_call(adb, serial, "cdp", request=request, timeout=timeout + 40),
|
||||
"CDP_FAILED"), serial)
|
||||
|
||||
if command == "input":
|
||||
return add_serial(checked(provider_call(adb, serial, "input", request=input_request(args), timeout=60),
|
||||
"INPUT_FAILED"), serial)
|
||||
|
||||
if command == "ui":
|
||||
if args.ui_op == "dump":
|
||||
request = {"op": "dump", "all": args.all}
|
||||
else:
|
||||
request = {"op": args.ui_op, "index": args.index}
|
||||
if args.match:
|
||||
request["match"] = args.match
|
||||
else:
|
||||
request["tag"] = args.tag
|
||||
if args.ui_op == "set-text":
|
||||
request["text"] = args.text
|
||||
return add_serial(checked(provider_call(adb, serial, "ui", request=request), "UI_FAILED"), serial)
|
||||
|
||||
if command == "env":
|
||||
if args.env_op == "set":
|
||||
request = {"op": "set", "lines": args.lines, "persistent": args.persistent}
|
||||
elif args.env_op == "clear":
|
||||
request = {"op": "clear", "scope": args.scope}
|
||||
else:
|
||||
request = {"op": "list"}
|
||||
return add_serial(checked(provider_call(adb, serial, "env", request=request), "ENV_FAILED"), serial)
|
||||
|
||||
if command == "override":
|
||||
access = require_commands(adb, serial)
|
||||
if not re.fullmatch(r"[A-Za-z0-9._-]{1,64}", args.name) or args.name in {".", ".."}:
|
||||
raise ControlError("Override names must be simple filenames", "INVALID_NAME")
|
||||
source = Path(args.path).expanduser()
|
||||
if not source.is_file():
|
||||
raise ControlError(f"No such file: {source}", "FILE_IO")
|
||||
inbox = access["inbox"]
|
||||
run_adb(adb, serial, "shell", "mkdir", "-p", inbox)
|
||||
pushed = run_adb(adb, serial, "push", str(source), f"{inbox}/{args.name}", timeout=300)
|
||||
if pushed.returncode != 0:
|
||||
raise ControlError(pushed.stderr.decode(errors="replace").strip() or "adb push failed", "PUSH_FAILED")
|
||||
return add_serial(checked(provider_call(adb, serial, "override", request={"name": args.name}),
|
||||
"OVERRIDE_FAILED"), serial)
|
||||
|
||||
if command == "prefs":
|
||||
if args.prefs_op == "set":
|
||||
request = {"op": "set", "file": args.file, "key": args.key, "type": args.type, "value": args.value}
|
||||
else:
|
||||
request = {"op": "get", "file": args.file}
|
||||
return add_serial(checked(provider_call(adb, serial, "prefs", request=request), "PREFS_FAILED"), serial)
|
||||
|
||||
if command == "access":
|
||||
return add_serial(checked(provider_call(adb, serial, "access"), "ACCESS_FAILED"), serial)
|
||||
|
||||
if command == "logs":
|
||||
state = get_state(adb, serial)
|
||||
session = state.get("session", {})
|
||||
if not session.get("running") and session.get("artifactsAvailable") and not session.get("artifactsComplete"):
|
||||
@@ -258,20 +501,24 @@ def execute(args: argparse.Namespace, adb: str, serial: str) -> dict:
|
||||
raise ControlError(detail or "adb pull failed", "LOG_PULL_FAILED")
|
||||
return add_serial({"ok": True, "command": "logs", "sessionId": session["id"], "path": str(destination)}, serial)
|
||||
|
||||
if args.command == "screenshot":
|
||||
destination = Path(args.path).expanduser().resolve()
|
||||
result = run_adb(adb, serial, "exec-out", "screencap", "-p", timeout=30)
|
||||
if result.returncode != 0:
|
||||
detail = result.stderr.decode(errors="replace").strip()
|
||||
raise ControlError(detail or "screencap failed", "SCREENSHOT_FAILED")
|
||||
try:
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_bytes(result.stdout)
|
||||
except OSError as exc:
|
||||
raise ControlError(str(exc), "FILE_IO") from exc
|
||||
return add_serial({"ok": True, "command": "screenshot", "path": str(destination)}, serial)
|
||||
if command == "screenshot":
|
||||
return add_serial(screenshot(adb, serial, args.path, args.display), serial)
|
||||
|
||||
raise ControlError(f"Unknown command: {args.command}", "UNKNOWN_COMMAND")
|
||||
if command == "record":
|
||||
return add_serial(record(adb, serial, args.path, args.seconds, args.display), serial)
|
||||
|
||||
if command == "wake":
|
||||
# A sleeping device stops the session and hides the app's windows from ui dump.
|
||||
run_adb(adb, serial, "shell", "input", "keyevent", "KEYCODE_WAKEUP")
|
||||
run_adb(adb, serial, "shell", "wm", "dismiss-keyguard")
|
||||
power = run_adb(adb, serial, "shell", "dumpsys", "power").stdout.decode(errors="replace")
|
||||
awake = re.search(r"mWakefulness=(\w+)", power)
|
||||
return add_serial({"ok": True, "command": "wake", "wakefulness": awake.group(1) if awake else None}, serial)
|
||||
|
||||
if command == "displays":
|
||||
return add_serial({"ok": True, "command": "displays", "displays": displays(adb, serial)}, serial)
|
||||
|
||||
raise ControlError(f"Unknown command: {command}", "UNKNOWN_COMMAND")
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
@@ -288,49 +535,172 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
start.add_argument("--ui", choices=["bigpicture", "desktop"], default="bigpicture")
|
||||
start.add_argument("--url", help="steam:// URL to pass to the client")
|
||||
start.add_argument("--wait", action="store_true", help="wait for READY after starting")
|
||||
start.add_argument("--reuse", action="store_true", help="keep a running session of this mode instead of failing")
|
||||
start.add_argument("--timeout", type=float, default=90)
|
||||
|
||||
run = commands.add_parser("run", help="run a guest program under Gamescope")
|
||||
run.add_argument("program")
|
||||
run.add_argument("arguments", nargs=argparse.REMAINDER)
|
||||
|
||||
wait = commands.add_parser("wait", help="wait for a session phase")
|
||||
wait.add_argument("phase", choices=["ready", "idle", "failed"])
|
||||
wait = commands.add_parser("wait", help="wait for a session phase, or for a game to have focus")
|
||||
wait.add_argument("phase", choices=["ready", "idle", "failed", "focused", "game"])
|
||||
wait.add_argument("app_id", nargs="?", type=int, help="with focused: the Steam app id")
|
||||
wait.add_argument("--timeout", type=float, default=90)
|
||||
|
||||
stop = commands.add_parser("stop", help="stop the session and wait for IDLE")
|
||||
stop.add_argument("--timeout", type=float, default=90)
|
||||
# Collecting the client's logs and redacting them took about two minutes after a Skyrim session.
|
||||
stop.add_argument("--artifact-timeout", type=float, default=300)
|
||||
commands.add_parser("resume", help="resume a suspended session")
|
||||
|
||||
launch = commands.add_parser("launch", help="launch a Steam game in the running Steam session")
|
||||
launch.add_argument("app_id", type=int)
|
||||
launch.add_argument("--wait", action="store_true", help="wait until the game has focus")
|
||||
launch.add_argument("--timeout", type=float, default=180)
|
||||
|
||||
quit_ = commands.add_parser("quit", help="quit a running game (through the client, then signals)")
|
||||
quit_.add_argument("app_id", type=int)
|
||||
quit_.add_argument("--timeout", type=float, default=15)
|
||||
|
||||
commands.add_parser("focus", help="gamescope's focus and every Xwayland server's windows, fresh")
|
||||
|
||||
guest = commands.add_parser("guest", help="run a command inside the session (debugging tools)")
|
||||
guest.add_argument("--timeout", type=float, default=30)
|
||||
guest.add_argument("--env", action="append", metavar="KEY=VALUE")
|
||||
guest.add_argument("--cwd")
|
||||
guest.add_argument("--stdin")
|
||||
guest.add_argument("--check", action="store_true", help="fail unless the command exits 0")
|
||||
guest.add_argument("argv", nargs=argparse.REMAINDER)
|
||||
|
||||
cdp = commands.add_parser("cdp", help="the Steam client's DevTools (debugging tools)")
|
||||
cdp_ops = cdp.add_subparsers(dest="cdp_op", required=True)
|
||||
cdp_ops.add_parser("targets", help="list DevTools targets")
|
||||
evaluate = cdp_ops.add_parser("eval", help="evaluate JavaScript (default target SharedJSContext)")
|
||||
evaluate.add_argument("expression")
|
||||
evaluate.add_argument("--target", help="title or URL substring of the target")
|
||||
evaluate.add_argument("--no-await", action="store_true", help="do not await a returned promise")
|
||||
evaluate.add_argument("--timeout", type=float, default=15)
|
||||
|
||||
inp = commands.add_parser("input", help="keys, text, touch and pad input into the session")
|
||||
kinds = inp.add_subparsers(dest="input_type", required=True)
|
||||
key = kinds.add_parser("key", help="a key by name (ENTER, ESC, A, F1...) or evdev code")
|
||||
key.add_argument("key")
|
||||
key.add_argument("--action", choices=["tap", "down", "up"], default="tap")
|
||||
key.add_argument("--mod", action="append", help="modifier held around a tap (CTRL, SHIFT, ALT, META)")
|
||||
key.add_argument("--hold", type=int, default=40, help="ms between down and up")
|
||||
text = kinds.add_parser("text", help="type text (US layout keys; --commit for text-input)")
|
||||
text.add_argument("text")
|
||||
text.add_argument("--commit", action="store_true")
|
||||
tap = kinds.add_parser("tap", help="touch at a point of the session's output")
|
||||
tap.add_argument("x", type=float)
|
||||
tap.add_argument("y", type=float)
|
||||
tap.add_argument("--space", choices=["output", "fraction"], default="output")
|
||||
tap.add_argument("--hold", type=int, default=60)
|
||||
swipe = kinds.add_parser("swipe", help="drag a touch from one point to another")
|
||||
for name in ("x", "y", "x2", "y2"):
|
||||
swipe.add_argument(name, type=float)
|
||||
swipe.add_argument("--space", choices=["output", "fraction"], default="output")
|
||||
swipe.add_argument("--duration", type=int, default=300)
|
||||
pad = kinds.add_parser("pad", help="a virtual pad button (a, b, x, y, lb, rb, start, select, guide, qam, up...)")
|
||||
pad.add_argument("button")
|
||||
pad.add_argument("--action", choices=["tap", "down", "up"], default="tap")
|
||||
pad.add_argument("--hold", type=int, default=120)
|
||||
stick = kinds.add_parser("stick", help="hold a stick at x, y (-1..1)")
|
||||
stick.add_argument("stick", choices=["left", "right"])
|
||||
stick.add_argument("x", type=float)
|
||||
stick.add_argument("y", type=float)
|
||||
stick.add_argument("--hold", type=int, default=300)
|
||||
trigger = kinds.add_parser("trigger", help="hold a trigger (0..1)")
|
||||
trigger.add_argument("trigger", choices=["left", "right"])
|
||||
trigger.add_argument("value", type=float, nargs="?", default=1.0)
|
||||
trigger.add_argument("--hold", type=int, default=200)
|
||||
|
||||
ui = commands.add_parser("ui", help="the app's own UI: dump nodes, click or type by test tag")
|
||||
ui_ops = ui.add_subparsers(dest="ui_op", required=True)
|
||||
dump = ui_ops.add_parser("dump", help="tagged, labelled and clickable nodes of every app window")
|
||||
dump.add_argument("--all", action="store_true", help="every node")
|
||||
for name in ("click", "set-text"):
|
||||
op = ui_ops.add_parser(name)
|
||||
op.add_argument("tag", nargs="?", help="test tag, e.g. rail-steam")
|
||||
op.add_argument("--match", help="instead of a tag: text or description containing this")
|
||||
op.add_argument("--index", type=int, default=0, help="which match, in tree order")
|
||||
if name == "set-text":
|
||||
op.add_argument("--text", required=True)
|
||||
|
||||
env = commands.add_parser("env", help="session environment lines (debugging tools)")
|
||||
env_ops = env.add_subparsers(dest="env_op", required=True)
|
||||
env_ops.add_parser("list")
|
||||
env_set = env_ops.add_parser("set", help="KEY=VALUE lines for the next session")
|
||||
env_set.add_argument("lines", nargs="+")
|
||||
env_set.add_argument("--persistent", action="store_true", help="every session until cleared")
|
||||
env_clear = env_ops.add_parser("clear")
|
||||
env_clear.add_argument("scope", nargs="?", choices=["next", "persistent", "all"], default="next")
|
||||
|
||||
override = commands.add_parser("override", help="put a binary first on the next session's PATH (debugging tools)")
|
||||
override.add_argument("name", help="the program name, e.g. gamescope")
|
||||
override.add_argument("path", help="the local file to push")
|
||||
|
||||
prefs = commands.add_parser("prefs", help="the app's preferences (debugging tools)")
|
||||
prefs_ops = prefs.add_subparsers(dest="prefs_op", required=True)
|
||||
prefs_get = prefs_ops.add_parser("get")
|
||||
prefs_get.add_argument("file", help="preferences file, e.g. session")
|
||||
prefs_set = prefs_ops.add_parser("set")
|
||||
prefs_set.add_argument("file")
|
||||
prefs_set.add_argument("key")
|
||||
prefs_set.add_argument("value", nargs="?")
|
||||
prefs_set.add_argument("--type", choices=["bool", "int", "long", "float", "string", "remove"], default="string")
|
||||
|
||||
commands.add_parser("access", help="whether debugging tools are enabled on the device")
|
||||
|
||||
logs = commands.add_parser("logs", help="pull the latest session artifact directory")
|
||||
logs.add_argument("which", choices=["latest"])
|
||||
logs.add_argument("destination", nargs="?")
|
||||
logs.add_argument("--timeout", type=float, default=90)
|
||||
|
||||
screenshot = commands.add_parser("screenshot", help="save a device screenshot")
|
||||
screenshot.add_argument("path")
|
||||
shot = commands.add_parser("screenshot", help="save a device screenshot")
|
||||
shot.add_argument("path")
|
||||
shot.add_argument("--display", type=int, help="logical display id (see displays); default the default display")
|
||||
|
||||
rec = commands.add_parser("record", help="record the screen to an mp4")
|
||||
rec.add_argument("path")
|
||||
rec.add_argument("--seconds", type=float, default=10)
|
||||
rec.add_argument("--display", type=int)
|
||||
|
||||
commands.add_parser("displays", help="the device's displays and their capture ids")
|
||||
commands.add_parser("wake", help="wake the device and dismiss the keyguard")
|
||||
return parser
|
||||
|
||||
|
||||
EXIT_ADB = {"ADB_NOT_FOUND", "ADB_FAILED", "INVALID_PROVIDER_RESPONSE"}
|
||||
EXIT_SESSION = {
|
||||
"SESSION_ACTIVE", "NO_ACTIVE_SESSION", "SESSION_NOT_SUSPENDED", "SESSION_FAILED", "GUEST_EXIT",
|
||||
"GUEST_START_FAILED", "RUNTIME_INSTALL_FAILED", "DESKTOP_INSTALL_FAILED", "ACTIVITY_START_FAILED",
|
||||
"NO_STEAM_SESSION", "SESSION_NOT_READY", "AGENT_UNAVAILABLE", "LAUNCH_REJECTED", "NO_PAD",
|
||||
}
|
||||
EXIT_TIMEOUT = {"TIMEOUT", "ARTIFACT_TIMEOUT"}
|
||||
EXIT_FILE = {"NO_SESSION_LOGS", "LOG_PULL_FAILED", "SCREENSHOT_FAILED", "FILE_IO", "RECORD_FAILED", "PUSH_FAILED"}
|
||||
EXIT_DENIED = {"AGENT_COMMANDS_DISABLED", "PROTECTED_PREFS", "PROTECTED_CONTROL"}
|
||||
|
||||
|
||||
def exit_code(exc: Exception) -> int:
|
||||
if isinstance(exc, DeviceResolutionError):
|
||||
return 3
|
||||
code = getattr(exc, "code", "")
|
||||
if code in {"ADB_NOT_FOUND", "ADB_FAILED", "INVALID_PROVIDER_RESPONSE"}:
|
||||
if code in EXIT_ADB:
|
||||
return 3
|
||||
if code in {"SESSION_ACTIVE", "NO_ACTIVE_SESSION", "SESSION_NOT_SUSPENDED", "SESSION_FAILED",
|
||||
"GUEST_EXIT", "GUEST_START_FAILED", "RUNTIME_INSTALL_FAILED", "DESKTOP_INSTALL_FAILED",
|
||||
"ACTIVITY_START_FAILED"}:
|
||||
if code in EXIT_SESSION:
|
||||
return 4
|
||||
if code in {"TIMEOUT", "ARTIFACT_TIMEOUT"}:
|
||||
if code in EXIT_TIMEOUT:
|
||||
return 5
|
||||
if code in {"NO_SESSION_LOGS", "LOG_PULL_FAILED", "SCREENSHOT_FAILED", "FILE_IO"}:
|
||||
if code in EXIT_FILE:
|
||||
return 6
|
||||
if code in EXIT_DENIED:
|
||||
return 7
|
||||
return 2
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = build_parser().parse_args()
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
args = build_parser().parse_args(argv)
|
||||
serial = None
|
||||
try:
|
||||
adb = adb_path(args.adb)
|
||||
@@ -340,11 +710,15 @@ def main() -> int:
|
||||
return 0
|
||||
except (ControlError, DeviceResolutionError) as exc:
|
||||
error = {"code": getattr(exc, "code", "DEVICE_RESOLUTION_FAILED"), "message": str(exc)}
|
||||
response = {"ok": False, "error": error}
|
||||
if serial:
|
||||
response["deviceSerial"] = serial
|
||||
print(json.dumps(response, indent=2, sort_keys=True))
|
||||
return exit_code(exc)
|
||||
code = exit_code(exc)
|
||||
except Exception as exc: # the contract is JSON on stdout, even for a bug in this tool
|
||||
error = {"code": "INTERNAL_ERROR", "message": f"{type(exc).__name__}: {exc}"}
|
||||
code = 2
|
||||
response = {"ok": False, "error": error}
|
||||
if serial:
|
||||
response["deviceSerial"] = serial
|
||||
print(json.dumps(response, indent=2, sort_keys=True))
|
||||
return code
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
+687
@@ -0,0 +1,687 @@
|
||||
#!/usr/bin/python3
|
||||
"""The guest side of the agent bridge (docs/agent-control.md).
|
||||
|
||||
Started by droiddeck-session inside the session, so whatever it runs has the session's own
|
||||
environment: DISPLAY, the games' Xwayland, XDG_RUNTIME_DIR, and the preloads every guest process
|
||||
gets - which is what lets it reach the Steam client's DevTools port, open to guest processes only.
|
||||
|
||||
The app talks to it through files under $BL_LAUNCH_DIR/agent (the session root, the same path on
|
||||
both sides):
|
||||
|
||||
req/<id>.json a request the app wrote (tmp + rename)
|
||||
resp/<id>.json the answer, written the same way
|
||||
hello.json this daemon is up: pid, version, what it can do
|
||||
focus.json gamescope's focus and every server's top-level windows, rewritten on change
|
||||
cdp-port the client's DevTools port, written by droiddeck-session
|
||||
|
||||
Requests: ping, exec, focus, cdp, quit. Each answer is {"ok": true, ...} or
|
||||
{"ok": false, "error": {"code", "message"}}.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import selectors
|
||||
import signal
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.request
|
||||
import urllib.parse
|
||||
from pathlib import Path
|
||||
|
||||
VERSION = 1
|
||||
OUTPUT_CAP = 256 * 1024
|
||||
REQUEST_CAP = 256 * 1024
|
||||
RESPONSE_CAP = 1024 * 1024
|
||||
FOCUS_PROPS = (
|
||||
"GAMESCOPE_FOCUSED_APP", "GAMESCOPE_FOCUSED_WINDOW", "GAMESCOPE_FOCUSABLE_APPS",
|
||||
"GAMESCOPE_FOCUSABLE_WINDOWS", "GAMESCOPECTRL_BASELAYER_APPID",
|
||||
)
|
||||
WINDOW_PROPS = (
|
||||
"STEAM_GAME", "WM_STATE", "_NET_WM_PID", "WM_CLASS", "_NET_WM_WINDOW_OPACITY", "STEAM_OVERLAY",
|
||||
"GAMESCOPE_EXTERNAL_OVERLAY", "STEAM_INPUT_FOCUS", "_NET_WM_STATE",
|
||||
)
|
||||
MAP_STATE = ("unmapped", "unviewable", "viewable")
|
||||
WM_STATES = {0: "withdrawn", 1: "normal", 3: "iconic"}
|
||||
|
||||
|
||||
def log(message):
|
||||
print("== agent: " + message, flush=True)
|
||||
|
||||
|
||||
def write_json(path, value):
|
||||
tmp = path.with_name(path.name + ".tmp")
|
||||
with os.fdopen(os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600), "w") as stream:
|
||||
os.fchmod(stream.fileno(), 0o600)
|
||||
json.dump(value, stream, separators=(",", ":"))
|
||||
os.replace(tmp, path)
|
||||
|
||||
|
||||
class AgentError(Exception):
|
||||
def __init__(self, code, message):
|
||||
super().__init__(message)
|
||||
self.code = code
|
||||
|
||||
|
||||
# --- X11: just enough of the protocol for root properties, the window tree and events ---------
|
||||
|
||||
class X:
|
||||
"""One connection to an Xwayland server. The rootfs has xprop but no python X library."""
|
||||
|
||||
def __init__(self, display):
|
||||
num = display.lstrip(":").split(".")[0]
|
||||
last = None
|
||||
for address in ("\0/tmp/.X11-unix/X" + num, "/tmp/.X11-unix/X" + num):
|
||||
try:
|
||||
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
self.s.settimeout(3)
|
||||
self.s.connect(address)
|
||||
break
|
||||
except OSError as e:
|
||||
last = e
|
||||
self.s.close()
|
||||
else:
|
||||
raise last
|
||||
self.s.sendall(struct.pack("<BxHHHHxx", 0x6C, 11, 0, 0, 0))
|
||||
head = self._read(8)
|
||||
if head[0] != 1:
|
||||
raise OSError("X connection refused on " + display)
|
||||
setup = self._read(struct.unpack_from("<H", head, 6)[0] * 4)
|
||||
vendor = struct.unpack_from("<H", setup, 16)[0]
|
||||
formats = setup[21]
|
||||
self.root = struct.unpack_from("<I", setup, 32 + (vendor + 3) // 4 * 4 + formats * 8)[0]
|
||||
self.events = []
|
||||
self._atoms = {}
|
||||
self._names = {}
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
self.s.close()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def _read(self, n):
|
||||
buf = b""
|
||||
while len(buf) < n:
|
||||
chunk = self.s.recv(n - len(buf))
|
||||
if not chunk:
|
||||
raise OSError("X connection closed")
|
||||
buf += chunk
|
||||
return buf
|
||||
|
||||
def request(self, data):
|
||||
"""Send one request and return its reply, or None for an X error (a window that went)."""
|
||||
self.s.sendall(data)
|
||||
while True:
|
||||
head = self._read(32)
|
||||
if head[0] == 0:
|
||||
return None
|
||||
if head[0] == 1:
|
||||
return head + self._read(struct.unpack_from("<I", head, 4)[0] * 4)
|
||||
self.events.append(head)
|
||||
|
||||
def next_event(self):
|
||||
if self.events:
|
||||
return self.events.pop(0)
|
||||
while True:
|
||||
head = self._read(32)
|
||||
if head[0] > 1:
|
||||
return head
|
||||
|
||||
def atom(self, name):
|
||||
if name not in self._atoms:
|
||||
n = name.encode()
|
||||
pad = (4 - len(n) % 4) % 4
|
||||
r = self.request(struct.pack("<BBHHxx", 16, 0, 2 + (len(n) + pad) // 4, len(n)) + n + b"\0" * pad)
|
||||
self._atoms[name] = struct.unpack_from("<I", r, 8)[0] if r else 0
|
||||
return self._atoms[name]
|
||||
|
||||
def atom_name(self, atom):
|
||||
if atom not in self._names:
|
||||
r = self.request(struct.pack("<BxHI", 17, 2, atom))
|
||||
self._names[atom] = r[32:32 + struct.unpack_from("<H", r, 8)[0]].decode(errors="replace") if r else str(atom)
|
||||
return self._names[atom]
|
||||
|
||||
def select(self, window, mask):
|
||||
self.s.sendall(struct.pack("<BxHIII", 2, 4, window, 0x800, mask))
|
||||
|
||||
def children(self, window):
|
||||
r = self.request(struct.pack("<BxHI", 15, 2, window))
|
||||
if not r:
|
||||
return []
|
||||
n = struct.unpack_from("<H", r, 16)[0]
|
||||
return list(struct.unpack_from("<%dI" % n, r, 32))
|
||||
|
||||
def attributes(self, window):
|
||||
r = self.request(struct.pack("<BxHI", 3, 2, window))
|
||||
return (r[26], r[27], struct.unpack_from("<H", r, 12)[0]) if r else None
|
||||
|
||||
def geometry(self, window):
|
||||
r = self.request(struct.pack("<BxHI", 14, 2, window))
|
||||
return struct.unpack_from("<hhHH", r, 12) if r else None
|
||||
|
||||
def prop(self, window, name, length=1024):
|
||||
atom = self.atom(name)
|
||||
if not atom:
|
||||
return None
|
||||
r = self.request(struct.pack("<BBHIIIII", 20, 0, 6, window, atom, 0, 0, length))
|
||||
if not r or r[1] == 0:
|
||||
return None
|
||||
fmt, n = r[1], struct.unpack_from("<I", r, 16)[0]
|
||||
data = r[32:32 + n * fmt // 8]
|
||||
if fmt == 32:
|
||||
return list(struct.unpack("<%dI" % n, data))
|
||||
return data
|
||||
|
||||
|
||||
def windows(x):
|
||||
"""Top-level windows that are mapped or carry an app id: what gamescope picks focus from."""
|
||||
out = []
|
||||
for win in x.children(x.root):
|
||||
attrs = x.attributes(win)
|
||||
if not attrs:
|
||||
continue
|
||||
props = {name: x.prop(win, name) for name in WINDOW_PROPS}
|
||||
if attrs[0] != 2 and not props["STEAM_GAME"]:
|
||||
continue
|
||||
geo = x.geometry(win) or (0, 0, 0, 0)
|
||||
cls = props["WM_CLASS"]
|
||||
state = props["WM_STATE"]
|
||||
entry = {
|
||||
"window": win,
|
||||
"map": MAP_STATE[attrs[0]] if attrs[0] < 3 else attrs[0],
|
||||
"overrideRedirect": bool(attrs[1]),
|
||||
"inputOnly": attrs[2] == 2,
|
||||
"geometry": [geo[0], geo[1], geo[2], geo[3]],
|
||||
"appId": props["STEAM_GAME"][0] if props["STEAM_GAME"] else None,
|
||||
"wmState": WM_STATES.get(state[0], state[0]) if state else None,
|
||||
"pid": props["_NET_WM_PID"][0] if props["_NET_WM_PID"] else None,
|
||||
"class": cls.split(b"\0")[-2].decode(errors="replace") if cls and b"\0" in cls else None,
|
||||
}
|
||||
if props["_NET_WM_WINDOW_OPACITY"]:
|
||||
entry["opacity"] = props["_NET_WM_WINDOW_OPACITY"][0]
|
||||
for key, name in (("overlay", "STEAM_OVERLAY"), ("externalOverlay", "GAMESCOPE_EXTERNAL_OVERLAY"),
|
||||
("inputFocus", "STEAM_INPUT_FOCUS")):
|
||||
if props[name]:
|
||||
entry[key] = props[name][0]
|
||||
if props["_NET_WM_STATE"]:
|
||||
entry["netState"] = [x.atom_name(a).replace("_NET_WM_STATE_", "") for a in props["_NET_WM_STATE"]]
|
||||
out.append(entry)
|
||||
return out
|
||||
|
||||
|
||||
def displays():
|
||||
seen = []
|
||||
for d in (os.environ.get("DISPLAY"), os.environ.get("STEAM_GAME_DISPLAY_0")):
|
||||
if d and d not in seen:
|
||||
seen.append(d)
|
||||
return seen
|
||||
|
||||
|
||||
def focus_snapshot():
|
||||
"""gamescope's focus as Steam sees it (root properties on the client's server) and every
|
||||
server's windows. GAMESCOPE_FOCUSABLE_WINDOWS is the list the client orders the base layer
|
||||
from: a game missing there stays behind the client however healthy its window looks."""
|
||||
shown = displays()
|
||||
if not shown:
|
||||
raise AgentError("NO_DISPLAY", "This session has no X display")
|
||||
snap = {"t": int(time.time() * 1000), "displays": {}}
|
||||
for i, d in enumerate(shown):
|
||||
x = X(d)
|
||||
try:
|
||||
if i == 0:
|
||||
values = {name: x.prop(x.root, name) for name in FOCUS_PROPS}
|
||||
trip = values["GAMESCOPE_FOCUSABLE_WINDOWS"] or []
|
||||
snap["focusedApp"] = (values["GAMESCOPE_FOCUSED_APP"] or [None])[0]
|
||||
snap["focusedWindow"] = (values["GAMESCOPE_FOCUSED_WINDOW"] or [None])[0]
|
||||
snap["focusableApps"] = values["GAMESCOPE_FOCUSABLE_APPS"] or []
|
||||
snap["focusableWindows"] = [
|
||||
{"window": trip[k], "appId": trip[k + 1], "pid": trip[k + 2]} for k in range(0, len(trip) - 2, 3)
|
||||
]
|
||||
snap["baselayerAppIds"] = values["GAMESCOPECTRL_BASELAYER_APPID"] or []
|
||||
snap["displays"][d] = windows(x)
|
||||
finally:
|
||||
x.close()
|
||||
return snap
|
||||
|
||||
|
||||
class FocusWatcher(threading.Thread):
|
||||
"""Rewrites focus.json when gamescope's focus properties change, and every few seconds when
|
||||
the windows do (a window can change without any root property moving)."""
|
||||
|
||||
def __init__(self, path):
|
||||
super().__init__(daemon=True, name="focus")
|
||||
self.path = path
|
||||
self.changed = threading.Event()
|
||||
self.last = None
|
||||
self.seq = 0
|
||||
|
||||
def run(self):
|
||||
threading.Thread(target=self.listen, daemon=True, name="focus-events").start()
|
||||
while True:
|
||||
self.changed.wait(timeout=5)
|
||||
self.changed.clear()
|
||||
time.sleep(0.2) # let a burst of property changes settle
|
||||
try:
|
||||
snap = focus_snapshot()
|
||||
except (OSError, AgentError) as e:
|
||||
log("focus snapshot failed: %s" % e)
|
||||
time.sleep(2)
|
||||
continue
|
||||
body = {k: v for k, v in snap.items() if k != "t"}
|
||||
if body == self.last:
|
||||
continue
|
||||
self.last = body
|
||||
self.seq += 1
|
||||
snap["seq"] = self.seq
|
||||
write_json(self.path, snap)
|
||||
|
||||
def listen(self):
|
||||
while True:
|
||||
try:
|
||||
x = X(os.environ["DISPLAY"])
|
||||
wanted = {x.atom(name) for name in FOCUS_PROPS}
|
||||
x.select(x.root, 0x400000) # PropertyChangeMask
|
||||
self.changed.set()
|
||||
while True:
|
||||
event = x.next_event()
|
||||
if event[0] & 0x7F == 28 and struct.unpack_from("<I", event, 8)[0] in wanted:
|
||||
self.changed.set()
|
||||
except OSError as e:
|
||||
log("focus events: %s; reconnecting" % e)
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
# --- Steam client DevTools (CDP) -----------------------------------------------------------------
|
||||
|
||||
class WebSocket:
|
||||
"""A minimal RFC 6455 client: text frames out, text frames in. Enough for CDP."""
|
||||
|
||||
def __init__(self, url, timeout):
|
||||
target = urllib.parse.urlsplit(url)
|
||||
if (target.scheme != "ws" or target.hostname not in ("127.0.0.1", "::1", "localhost")
|
||||
or target.username is not None or target.password is not None or target.fragment):
|
||||
raise AgentError("INVALID_TARGET", "DevTools targets must use a loopback WebSocket")
|
||||
hostport = target.netloc
|
||||
path = target.path or "/"
|
||||
if target.query:
|
||||
path += "?" + target.query
|
||||
host = "127.0.0.1" if target.hostname == "localhost" else target.hostname
|
||||
self.s = socket.create_connection((host, target.port or 80), timeout=timeout)
|
||||
try:
|
||||
self._handshake(path, hostport)
|
||||
except Exception:
|
||||
self.s.close()
|
||||
raise
|
||||
|
||||
def _handshake(self, path, hostport):
|
||||
key = base64.b64encode(os.urandom(16)).decode()
|
||||
self.s.sendall((
|
||||
"GET %s HTTP/1.1\r\nHost: %s\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Key: %s\r\nSec-WebSocket-Version: 13\r\n\r\n" % (path, hostport, key)
|
||||
).encode())
|
||||
head = b""
|
||||
while b"\r\n\r\n" not in head:
|
||||
chunk = self.s.recv(4096)
|
||||
if not chunk:
|
||||
raise OSError("DevTools closed the connection during the handshake")
|
||||
head += chunk
|
||||
if len(head) > 16 * 1024:
|
||||
raise AgentError("RESPONSE_TOO_LARGE", "The DevTools handshake exceeds 16 KiB")
|
||||
status = head.split(b"\r\n", 1)[0]
|
||||
if b" 101 " not in status:
|
||||
raise OSError("DevTools refused the WebSocket: " + status.decode(errors="replace"))
|
||||
self.buffer = head.split(b"\r\n\r\n", 1)[1]
|
||||
|
||||
def _read(self, n):
|
||||
while len(self.buffer) < n:
|
||||
chunk = self.s.recv(65536)
|
||||
if not chunk:
|
||||
raise OSError("DevTools closed the connection")
|
||||
self.buffer += chunk
|
||||
data, self.buffer = self.buffer[:n], self.buffer[n:]
|
||||
return data
|
||||
|
||||
def send(self, text):
|
||||
payload = text.encode()
|
||||
header = bytearray([0x81])
|
||||
n = len(payload)
|
||||
if n < 126:
|
||||
header.append(0x80 | n)
|
||||
elif n < 65536:
|
||||
header.append(0x80 | 126)
|
||||
header += struct.pack(">H", n)
|
||||
else:
|
||||
header.append(0x80 | 127)
|
||||
header += struct.pack(">Q", n)
|
||||
mask = os.urandom(4)
|
||||
header += mask
|
||||
self.s.sendall(bytes(header) + bytes(b ^ mask[i % 4] for i, b in enumerate(payload)))
|
||||
|
||||
def recv(self):
|
||||
message = b""
|
||||
while True:
|
||||
b0, b1 = self._read(2)
|
||||
n = b1 & 0x7F
|
||||
if n == 126:
|
||||
n = struct.unpack(">H", self._read(2))[0]
|
||||
elif n == 127:
|
||||
n = struct.unpack(">Q", self._read(8))[0]
|
||||
if n > RESPONSE_CAP or len(message) + n > RESPONSE_CAP:
|
||||
raise AgentError("RESPONSE_TOO_LARGE", "The DevTools response exceeds 1 MiB")
|
||||
mask = self._read(4) if b1 & 0x80 else None
|
||||
data = self._read(n)
|
||||
if mask:
|
||||
data = bytes(b ^ mask[i % 4] for i, b in enumerate(data))
|
||||
op = b0 & 0x0F
|
||||
if op == 8:
|
||||
raise OSError("DevTools closed the WebSocket")
|
||||
if op == 9: # ping
|
||||
continue
|
||||
if op in (0, 1, 2):
|
||||
message += data
|
||||
if b0 & 0x80:
|
||||
return message.decode(errors="replace")
|
||||
|
||||
def close(self):
|
||||
try:
|
||||
self.s.close()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def cdp_port(agent_dir):
|
||||
try:
|
||||
port = (agent_dir / "cdp-port").read_text().strip()
|
||||
except OSError:
|
||||
port = os.environ.get("BL_CDP_PORT", "")
|
||||
if not port.isdigit():
|
||||
raise AgentError("NO_DEVTOOLS", "The Steam client's DevTools port is not known (no Steam client in this session yet)")
|
||||
return int(port)
|
||||
|
||||
|
||||
def cdp_targets(agent_dir, timeout=5):
|
||||
url = "http://127.0.0.1:%d/json" % cdp_port(agent_dir)
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=timeout) as r:
|
||||
data = r.read(RESPONSE_CAP + 1)
|
||||
if len(data) > RESPONSE_CAP:
|
||||
raise AgentError("RESPONSE_TOO_LARGE", "The DevTools response exceeds 1 MiB")
|
||||
return json.loads(data.decode())
|
||||
except OSError as e:
|
||||
raise AgentError("DEVTOOLS_UNREACHABLE", "Could not reach the client's DevTools at %s: %s" % (url, e))
|
||||
|
||||
|
||||
def cdp_eval(agent_dir, expression, target=None, await_promise=True, timeout=15):
|
||||
targets = [t for t in cdp_targets(agent_dir) if t.get("webSocketDebuggerUrl")]
|
||||
want = target or "SharedJSContext"
|
||||
match = [t for t in targets if want in (t.get("title") or "") or want in (t.get("url") or "")]
|
||||
if not match:
|
||||
names = ", ".join(sorted({t.get("title") or t.get("url") or "?" for t in targets}))
|
||||
raise AgentError("NO_TARGET", "No DevTools target matches '%s' (have: %s)" % (want, names))
|
||||
ws = WebSocket(match[0]["webSocketDebuggerUrl"], timeout)
|
||||
try:
|
||||
ws.send(json.dumps({"id": 1, "method": "Runtime.evaluate", "params": {
|
||||
"expression": expression, "returnByValue": True, "awaitPromise": await_promise,
|
||||
"userGesture": True,
|
||||
}}))
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
message = json.loads(ws.recv())
|
||||
if message.get("id") != 1:
|
||||
continue
|
||||
if "error" in message:
|
||||
raise AgentError("CDP_ERROR", json.dumps(message["error"]))
|
||||
result = message.get("result", {})
|
||||
if result.get("exceptionDetails"):
|
||||
details = result["exceptionDetails"]
|
||||
text = (details.get("exception") or {}).get("description") or details.get("text") or "exception"
|
||||
raise AgentError("JS_EXCEPTION", text)
|
||||
value = result.get("result", {})
|
||||
return {"target": match[0].get("title"), "type": value.get("type"),
|
||||
"value": value.get("value"), "description": value.get("description")}
|
||||
raise AgentError("TIMEOUT", "No answer from DevTools in %ss" % timeout)
|
||||
finally:
|
||||
ws.close()
|
||||
|
||||
|
||||
# --- Game processes ------------------------------------------------------------------------------
|
||||
|
||||
def game_pids(app_id):
|
||||
"""Our processes started for this Steam app (Steam sets SteamAppId / SteamGameId in their
|
||||
environment, and Proton's wine processes inherit it)."""
|
||||
wanted = [("SteamAppId=%s" % app_id).encode(), ("SteamGameId=%s" % app_id).encode()]
|
||||
me = os.getpid()
|
||||
pids = []
|
||||
for entry in os.listdir("/proc"):
|
||||
if not entry.isdigit() or int(entry) == me:
|
||||
continue
|
||||
try:
|
||||
with open("/proc/%s/environ" % entry, "rb") as f:
|
||||
env = f.read().split(b"\0")
|
||||
except OSError:
|
||||
continue
|
||||
if any(w in env for w in wanted):
|
||||
pids.append(int(entry))
|
||||
return pids
|
||||
|
||||
|
||||
def quit_game(agent_dir, app_id, timeout):
|
||||
if not str(app_id).isdigit():
|
||||
raise AgentError("INVALID_APP_ID", "appId must be a decimal Steam app id")
|
||||
steps = []
|
||||
if game_pids(app_id):
|
||||
try:
|
||||
cdp_eval(agent_dir, "SteamClient.Apps.TerminateApp(%s, false)" % json.dumps(str(app_id)), timeout=5)
|
||||
steps.append("client")
|
||||
except AgentError as e:
|
||||
steps.append("client unavailable: %s" % e)
|
||||
deadline = time.monotonic() + timeout
|
||||
while game_pids(app_id) and time.monotonic() < deadline:
|
||||
time.sleep(0.5)
|
||||
left = game_pids(app_id)
|
||||
if left:
|
||||
steps.append("SIGTERM %s" % left)
|
||||
for pid in left:
|
||||
try:
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
except OSError:
|
||||
pass
|
||||
end = time.monotonic() + 5
|
||||
while game_pids(app_id) and time.monotonic() < end:
|
||||
time.sleep(0.5)
|
||||
left = game_pids(app_id)
|
||||
if left:
|
||||
steps.append("SIGKILL %s" % left)
|
||||
for pid in left:
|
||||
try:
|
||||
os.kill(pid, signal.SIGKILL)
|
||||
except OSError:
|
||||
pass
|
||||
time.sleep(1)
|
||||
return {"appId": int(app_id), "steps": steps, "remaining": game_pids(app_id)}
|
||||
|
||||
|
||||
# --- Requests ------------------------------------------------------------------------------------
|
||||
|
||||
def run_exec(req):
|
||||
argv = req.get("argv")
|
||||
if not isinstance(argv, list) or not argv or not all(isinstance(a, str) and "\0" not in a for a in argv):
|
||||
raise AgentError("INVALID_REQUEST", "exec needs argv, a non-empty list of strings")
|
||||
timeout = request_timeout(req, 30)
|
||||
env = dict(os.environ)
|
||||
extra_env = req.get("env") if req.get("env") is not None else {}
|
||||
if not isinstance(extra_env, dict) or any("\0" in str(k) or "=" in str(k) or "\0" in str(v) for k, v in extra_env.items()):
|
||||
raise AgentError("INVALID_REQUEST", "env must be an object with valid environment entries")
|
||||
env.update({str(k): str(v) for k, v in extra_env.items()})
|
||||
stdin = req.get("stdin")
|
||||
if stdin is not None and not isinstance(stdin, str):
|
||||
raise AgentError("INVALID_REQUEST", "stdin must be a string")
|
||||
started = time.monotonic()
|
||||
try:
|
||||
proc = subprocess.Popen(
|
||||
argv, cwd=req.get("cwd") or None, env=env, start_new_session=True,
|
||||
stdin=subprocess.PIPE if stdin is not None else subprocess.DEVNULL,
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||
)
|
||||
except OSError as e:
|
||||
raise AgentError("EXEC_FAILED", "%s: %s" % (argv[0], e))
|
||||
captured = {"stdout": bytearray(), "stderr": bytearray()}
|
||||
truncated = {"stdout": False, "stderr": False}
|
||||
pending = memoryview(stdin.encode()) if stdin is not None else memoryview(b"")
|
||||
deadline = started + timeout
|
||||
timed_out = False
|
||||
try:
|
||||
with selectors.DefaultSelector() as selector:
|
||||
for stream, name in ((proc.stdout, "stdout"), (proc.stderr, "stderr")):
|
||||
os.set_blocking(stream.fileno(), False)
|
||||
selector.register(stream, selectors.EVENT_READ, name)
|
||||
if proc.stdin is not None:
|
||||
os.set_blocking(proc.stdin.fileno(), False)
|
||||
if pending:
|
||||
selector.register(proc.stdin, selectors.EVENT_WRITE, "stdin")
|
||||
else:
|
||||
proc.stdin.close()
|
||||
while selector.get_map() or proc.poll() is None:
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
timed_out = True
|
||||
break
|
||||
for key, _ in selector.select(min(0.1, remaining)):
|
||||
stream, name = key.fileobj, key.data
|
||||
if name == "stdin":
|
||||
try:
|
||||
pending = pending[os.write(stream.fileno(), pending[:65536]):]
|
||||
except BrokenPipeError:
|
||||
pending = pending[len(pending):]
|
||||
if not pending:
|
||||
selector.unregister(stream)
|
||||
stream.close()
|
||||
else:
|
||||
data = os.read(stream.fileno(), 65536)
|
||||
if not data:
|
||||
selector.unregister(stream)
|
||||
stream.close()
|
||||
continue
|
||||
available = OUTPUT_CAP - len(captured[name])
|
||||
captured[name].extend(data[:available])
|
||||
truncated[name] |= len(data) > available
|
||||
if timed_out:
|
||||
try:
|
||||
os.killpg(proc.pid, signal.SIGKILL)
|
||||
except ProcessLookupError:
|
||||
pass
|
||||
proc.wait(timeout=5)
|
||||
finally:
|
||||
if proc.poll() is None:
|
||||
try:
|
||||
os.killpg(proc.pid, signal.SIGKILL)
|
||||
except ProcessLookupError:
|
||||
pass
|
||||
proc.wait(timeout=5)
|
||||
for stream in (proc.stdin, proc.stdout, proc.stderr):
|
||||
if stream is not None:
|
||||
stream.close()
|
||||
|
||||
def text(name):
|
||||
return {"text": captured[name].decode(errors="replace"), "truncated": truncated[name]}
|
||||
|
||||
return {"exitCode": proc.returncode, "timedOut": timed_out,
|
||||
"seconds": round(time.monotonic() - started, 3), "stdout": text("stdout"), "stderr": text("stderr")}
|
||||
|
||||
|
||||
def request_timeout(req, default):
|
||||
try:
|
||||
timeout = float(req.get("timeout", default))
|
||||
except (TypeError, ValueError):
|
||||
raise AgentError("INVALID_REQUEST", "timeout must be a positive number no greater than 600 seconds")
|
||||
if not math.isfinite(timeout) or not 0 < timeout <= 600:
|
||||
raise AgentError("INVALID_REQUEST", "timeout must be a positive number no greater than 600 seconds")
|
||||
return timeout
|
||||
|
||||
|
||||
def handle(agent_dir, req):
|
||||
kind = req.get("kind")
|
||||
if kind == "ping":
|
||||
return {"pid": os.getpid(), "version": VERSION, "displays": displays()}
|
||||
if kind == "exec":
|
||||
return run_exec(req)
|
||||
if kind == "focus":
|
||||
return focus_snapshot()
|
||||
if kind == "cdp":
|
||||
if req.get("op") == "targets":
|
||||
return {"targets": [{k: t.get(k) for k in ("title", "url", "type")} for t in cdp_targets(agent_dir)]}
|
||||
expression = req.get("expression")
|
||||
if not isinstance(expression, str) or not expression:
|
||||
raise AgentError("INVALID_REQUEST", "cdp eval needs an expression")
|
||||
return cdp_eval(agent_dir, expression, req.get("target"), bool(req.get("awaitPromise", True)),
|
||||
request_timeout(req, 15))
|
||||
if kind == "quit":
|
||||
return quit_game(agent_dir, req.get("appId"), request_timeout(req, 15))
|
||||
raise AgentError("UNKNOWN_REQUEST", "Unknown agent request '%s'" % kind)
|
||||
|
||||
|
||||
def answer(agent_dir, path):
|
||||
rid = path.stem
|
||||
try:
|
||||
with path.open("rb") as stream:
|
||||
data = stream.read(REQUEST_CAP + 1)
|
||||
if len(data) > REQUEST_CAP:
|
||||
raise ValueError("The request exceeds 256 KiB")
|
||||
req = json.loads(data)
|
||||
if not isinstance(req, dict):
|
||||
raise ValueError("The request must be a JSON object")
|
||||
except (OSError, ValueError) as e:
|
||||
req = None
|
||||
result = {"ok": False, "error": {"code": "INVALID_REQUEST", "message": str(e)}}
|
||||
finally:
|
||||
try:
|
||||
path.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
if req is not None:
|
||||
try:
|
||||
result = dict(handle(agent_dir, req), ok=True)
|
||||
except AgentError as e:
|
||||
result = {"ok": False, "error": {"code": e.code, "message": str(e)}}
|
||||
except Exception as e: # an answer of some kind always goes back
|
||||
result = {"ok": False, "error": {"code": "AGENT_FAILED", "message": "%s: %s" % (type(e).__name__, e)}}
|
||||
write_json(agent_dir / "resp" / (rid + ".json"), result)
|
||||
|
||||
|
||||
def serve(agent_dir, focus=True, poll=0.1):
|
||||
(agent_dir / "req").mkdir(parents=True, exist_ok=True)
|
||||
(agent_dir / "resp").mkdir(parents=True, exist_ok=True)
|
||||
can_focus = focus and bool(os.environ.get("DISPLAY"))
|
||||
if can_focus:
|
||||
FocusWatcher(agent_dir / "focus.json").start()
|
||||
write_json(agent_dir / "hello.json", {"pid": os.getpid(), "version": VERSION, "started": int(time.time() * 1000),
|
||||
"displays": displays(), "focus": can_focus})
|
||||
log("serving %s (focus %s)" % (agent_dir, "on" if can_focus else "off"))
|
||||
taken = set()
|
||||
while True:
|
||||
for path in sorted((agent_dir / "req").glob("*.json")):
|
||||
if path.name in taken:
|
||||
continue
|
||||
taken.add(path.name)
|
||||
threading.Thread(target=lambda p=path: (answer(agent_dir, p), taken.discard(p.name)),
|
||||
daemon=True, name="req").start()
|
||||
time.sleep(poll)
|
||||
|
||||
|
||||
def main(argv):
|
||||
base = os.environ.get("BL_LAUNCH_DIR")
|
||||
if not base:
|
||||
print("BL_LAUNCH_DIR is not set", file=sys.stderr)
|
||||
return 2
|
||||
serve(Path(base) / "agent", focus="--no-focus" not in argv)
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv[1:]))
|
||||
@@ -97,6 +97,14 @@ export GAMESCOPE_FORCE_VULKAN_REALTIME=${BL_GAMESCOPE_REALTIME:-0}
|
||||
# A sleep that starts no process. Under proot every exec is a traced execve plus the loader's
|
||||
# opens and maps, and the watchers below poll for the whole session: `sleep 1` in a loop was 60
|
||||
# execs a minute each. read -t on a pipe nothing ever writes to waits in bash itself.
|
||||
# The agent bridge's guest side (docs/agent-control.md): runs what the app's agent provider asks
|
||||
# for inside this session and publishes gamescope's focus. Its files live under the session root
|
||||
# ($BL_LAUNCH_DIR/agent), which the app empties before each session.
|
||||
start_agent() {
|
||||
[ -n "${BL_LAUNCH_DIR:-}" ] && [ -x /usr/local/bin/droiddeck-agent ] || return 0
|
||||
mkdir -p "$BL_LAUNCH_DIR/agent"
|
||||
/usr/local/bin/droiddeck-agent "$@" 2>&1 &
|
||||
}
|
||||
nap() {
|
||||
[ -n "${bl_nap_fd:-}" ] || exec {bl_nap_fd}<> <(:)
|
||||
read -r -t "$1" -u "$bl_nap_fd" _ 2>/dev/null || :
|
||||
@@ -166,6 +174,7 @@ if [ -z "${BL_INSIDE:-}" ]; then
|
||||
# into the app's: no gamescope in between.
|
||||
if [ "$mode" = lxqt ]; then
|
||||
step "starting the desktop"
|
||||
start_agent --no-focus
|
||||
[ -x /usr/local/bin/droiddeck-desktop ] || { echo "== the desktop package is not installed"; exit 66; }
|
||||
cores=$(program_cores)
|
||||
if [ -n "$cores" ]; then
|
||||
@@ -284,6 +293,8 @@ if [ -n "${BL_ANDROID_CLIPBOARD:-}" ] && [ -x /usr/local/bin/droiddeck-clipboard
|
||||
/usr/local/bin/droiddeck-clipboard "$BL_ANDROID_CLIPBOARD" &
|
||||
fi
|
||||
|
||||
start_agent
|
||||
|
||||
case $mode in
|
||||
desktop)
|
||||
exec pcmanfm
|
||||
@@ -710,6 +721,8 @@ BUSCONF
|
||||
export BL_CDP_GUARD=$BL_CDP_PORT
|
||||
echo "== steam: devtools port $BL_CDP_PORT, guest processes only"
|
||||
fi
|
||||
# The agent bridge drives the client through it (droiddeck-agent's cdp requests).
|
||||
[ -n "${BL_LAUNCH_DIR:-}" ] && mkdir -p "$BL_LAUNCH_DIR/agent" && echo "$BL_CDP_PORT" > "$BL_LAUNCH_DIR/agent/cdp-port"
|
||||
droiddeck-steam-compat "$steam_root" 2>&1 &
|
||||
# The client exits 42 when it has updated itself and wants a fresh start (steam.sh does this).
|
||||
# BL_STEAM_UI=desktop = the client's desktop UI (a window, the library as a list), which the
|
||||
|
||||
+65
-22
@@ -41,6 +41,19 @@ static const char *net_dir(void) {
|
||||
return dir;
|
||||
}
|
||||
|
||||
static int loopback_family(const struct sockaddr *address, socklen_t length) {
|
||||
if (address && address->sa_family == AF_INET && length >= sizeof(struct sockaddr_in)) {
|
||||
return ((const struct sockaddr_in *)address)->sin_addr.s_addr == htonl(INADDR_LOOPBACK) ? AF_INET : 0;
|
||||
}
|
||||
if (address && address->sa_family == AF_INET6 && length >= sizeof(struct sockaddr_in6)) {
|
||||
const struct in6_addr *ip = &((const struct sockaddr_in6 *)address)->sin6_addr;
|
||||
if (IN6_IS_ADDR_LOOPBACK(ip)) return AF_INET6;
|
||||
if (IN6_IS_ADDR_V4MAPPED(ip) && ip->s6_addr[12] == 127 && ip->s6_addr[13] == 0 &&
|
||||
ip->s6_addr[14] == 0 && ip->s6_addr[15] == 1) return AF_INET;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Record that this process owns a loopback socket on its local port; peer is the far end's port,
|
||||
* 0 for a listening socket. */
|
||||
static void record_port(int fd, unsigned peer) {
|
||||
@@ -49,12 +62,11 @@ static void record_port(int fd, unsigned peer) {
|
||||
if (getsockname(fd, (struct sockaddr *)&ss, &len) != 0) {
|
||||
return;
|
||||
}
|
||||
int family = loopback_family((struct sockaddr *)&ss, len);
|
||||
if (!family) return;
|
||||
unsigned port;
|
||||
if (ss.ss_family == AF_INET) {
|
||||
struct sockaddr_in *in = (struct sockaddr_in *)&ss;
|
||||
if (in->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
|
||||
return;
|
||||
}
|
||||
port = ntohs(in->sin_port);
|
||||
} else if (ss.ss_family == AF_INET6) {
|
||||
port = ntohs(((struct sockaddr_in6 *)&ss)->sin6_port);
|
||||
@@ -64,14 +76,21 @@ static void record_port(int fd, unsigned peer) {
|
||||
if (port == 0) {
|
||||
return;
|
||||
}
|
||||
struct stat socket_stat;
|
||||
if (fstat(fd, &socket_stat) != 0) return;
|
||||
char path[300];
|
||||
snprintf(path, sizeof(path), "%s/p%u", net_dir(), port);
|
||||
char line[64];
|
||||
int n = snprintf(line, sizeof(line), "%d %d %d %u\n", (int)getpid(), (int)getppid(), (int)getuid(), peer);
|
||||
int out = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
char staged[360];
|
||||
snprintf(staged, sizeof(staged), "%s.tmp.%d.%d", path, (int)getpid(), fd);
|
||||
char line[128];
|
||||
int n = snprintf(line, sizeof(line), "%d %d %d %u %d %llu %d\n", (int)getpid(), (int)getppid(),
|
||||
(int)getuid(), peer, fd, (unsigned long long)socket_stat.st_ino, family);
|
||||
int out = open(staged, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600);
|
||||
if (out >= 0) {
|
||||
if (write(out, line, n) != n) { /* best effort */ }
|
||||
int complete = write(out, line, n) == n;
|
||||
close(out);
|
||||
if (complete) rename(staged, path);
|
||||
unlink(staged);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +117,7 @@ typedef int (*bind_fn)(int, const struct sockaddr *, socklen_t);
|
||||
typedef int (*listen_fn)(int, int);
|
||||
typedef int (*connect_fn)(int, const struct sockaddr *, socklen_t);
|
||||
typedef int (*accept4_fn)(int, struct sockaddr *, socklen_t *, int);
|
||||
static unsigned guarded_port(void);
|
||||
|
||||
/* udevmon.c: a stand-in for the netlink socket the sandbox refuses, bound here without the kernel. */
|
||||
__attribute__((visibility("hidden"))) int bl_udevmon_stand_in(int fd);
|
||||
@@ -112,6 +132,18 @@ int bind(int fd, const struct sockaddr *addr, socklen_t len) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
struct sockaddr_storage local;
|
||||
unsigned guarded = guarded_port();
|
||||
if (guarded && addr_port(addr, len) == guarded && len <= sizeof(local)) {
|
||||
memcpy(&local, addr, len);
|
||||
if (addr->sa_family == AF_INET) {
|
||||
((struct sockaddr_in *)&local)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
addr = (struct sockaddr *)&local;
|
||||
} else if (addr->sa_family == AF_INET6) {
|
||||
((struct sockaddr_in6 *)&local)->sin6_addr = in6addr_loopback;
|
||||
addr = (struct sockaddr *)&local;
|
||||
}
|
||||
}
|
||||
int ret = real(fd, addr, len);
|
||||
if (ret == 0) record_port(fd, 0);
|
||||
return ret;
|
||||
@@ -135,22 +167,20 @@ static unsigned local_port(int fd) {
|
||||
}
|
||||
|
||||
static void bind_before_loopback_connect(int fd, const struct sockaddr *addr, socklen_t len) {
|
||||
if (!addr || addr->sa_family != AF_INET || len < sizeof(struct sockaddr_in) ||
|
||||
((const struct sockaddr_in *)addr)->sin_addr.s_addr != htonl(INADDR_LOOPBACK)) {
|
||||
return;
|
||||
}
|
||||
if (!loopback_family(addr, len)) return;
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t slen = sizeof(ss);
|
||||
if (getsockname(fd, (struct sockaddr *)&ss, &slen) != 0 || ss.ss_family != AF_INET ||
|
||||
((struct sockaddr_in *)&ss)->sin_port != 0) {
|
||||
if (getsockname(fd, (struct sockaddr *)&ss, &slen) != 0 || ss.ss_family != addr->sa_family ||
|
||||
addr_port((struct sockaddr *)&ss, slen) != 0) {
|
||||
return;
|
||||
}
|
||||
struct sockaddr_in local;
|
||||
memset(&local, 0, sizeof(local));
|
||||
local.sin_family = AF_INET;
|
||||
local.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
struct sockaddr_storage local;
|
||||
if (len > sizeof(local)) return;
|
||||
memcpy(&local, addr, len);
|
||||
if (addr->sa_family == AF_INET) ((struct sockaddr_in *)&local)->sin_port = 0;
|
||||
else ((struct sockaddr_in6 *)&local)->sin6_port = 0;
|
||||
int saved = errno;
|
||||
bind(fd, (struct sockaddr *)&local, sizeof(local));
|
||||
bind(fd, (struct sockaddr *)&local, len);
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
@@ -165,6 +195,11 @@ static unsigned guarded_port(void) {
|
||||
}
|
||||
|
||||
static int guest_peer(int fd) {
|
||||
struct sockaddr_storage address;
|
||||
socklen_t length = sizeof(address);
|
||||
if (getpeername(fd, (struct sockaddr *)&address, &length) != 0) return 0;
|
||||
int family = loopback_family((struct sockaddr *)&address, length);
|
||||
if (!family) return 0;
|
||||
unsigned peer = peer_port(fd);
|
||||
if (peer == 0) {
|
||||
return 0;
|
||||
@@ -179,26 +214,34 @@ static int guest_peer(int fd) {
|
||||
if (in < 0) {
|
||||
return 0;
|
||||
}
|
||||
char buf[64];
|
||||
char buf[128];
|
||||
int n = read(in, buf, sizeof(buf) - 1);
|
||||
close(in);
|
||||
if (n <= 0) {
|
||||
return 0;
|
||||
}
|
||||
buf[n] = '\0';
|
||||
int pid = atoi(buf);
|
||||
return pid > 0 && (kill(pid, 0) == 0 || errno != ESRCH);
|
||||
int pid, parent, uid, descriptor, recorded_family;
|
||||
unsigned destination;
|
||||
unsigned long long inode;
|
||||
if (sscanf(buf, "%d %d %d %u %d %llu %d", &pid, &parent, &uid, &destination, &descriptor,
|
||||
&inode, &recorded_family) != 7 || pid <= 0 || descriptor < 0 || destination != guarded_port() ||
|
||||
recorded_family != family) return 0;
|
||||
snprintf(path, sizeof(path), "/proc/%d/fd/%d", pid, descriptor);
|
||||
return stat(path, &st) == 0 && S_ISSOCK(st.st_mode) && (unsigned long long)st.st_ino == inode;
|
||||
}
|
||||
|
||||
int connect(int fd, const struct sockaddr *addr, socklen_t len) {
|
||||
static connect_fn real;
|
||||
if (!real) real = (connect_fn)dlsym(RTLD_NEXT, "connect");
|
||||
bind_before_loopback_connect(fd, addr, len);
|
||||
unsigned peer = loopback_family(addr, len) ? addr_port(addr, len) : 0;
|
||||
if (peer) record_port(fd, peer);
|
||||
int ret = real(fd, addr, len);
|
||||
if (ret == 0 || errno == EINPROGRESS) {
|
||||
/* The caller goes on to test for EINPROGRESS, which the bookkeeping must not disturb. */
|
||||
int saved = errno;
|
||||
record_port(fd, addr_port(addr, len));
|
||||
record_port(fd, peer);
|
||||
errno = saved;
|
||||
}
|
||||
return ret;
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
{
|
||||
"name": "skyrim-launcher-focus",
|
||||
"vars": {
|
||||
"appId": 489830
|
||||
},
|
||||
"repeat": 6,
|
||||
"setup": [
|
||||
[
|
||||
"wake"
|
||||
]
|
||||
],
|
||||
"before": [
|
||||
[
|
||||
"start",
|
||||
"steam",
|
||||
"--wait",
|
||||
"--reuse",
|
||||
"--timeout",
|
||||
"240"
|
||||
]
|
||||
],
|
||||
"steps": [
|
||||
[
|
||||
"launch",
|
||||
"${appId}",
|
||||
"--wait",
|
||||
"--timeout",
|
||||
"240"
|
||||
],
|
||||
{
|
||||
"sleep": 8
|
||||
},
|
||||
[
|
||||
"input",
|
||||
"tap",
|
||||
"0.927",
|
||||
"0.111",
|
||||
"--space",
|
||||
"fraction"
|
||||
],
|
||||
{
|
||||
"sleep": 30
|
||||
},
|
||||
[
|
||||
"wait",
|
||||
"focused",
|
||||
"${appId}",
|
||||
"--timeout",
|
||||
"60"
|
||||
],
|
||||
{
|
||||
"screenshot": "in-game"
|
||||
},
|
||||
[
|
||||
"quit",
|
||||
"${appId}",
|
||||
"--timeout",
|
||||
"20"
|
||||
],
|
||||
{
|
||||
"assert": "remaining",
|
||||
"equals": []
|
||||
},
|
||||
{
|
||||
"sleep": 5
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
[
|
||||
"quit",
|
||||
"${appId}",
|
||||
"--timeout",
|
||||
"20"
|
||||
]
|
||||
],
|
||||
"teardown": [
|
||||
[
|
||||
"stop",
|
||||
"--timeout",
|
||||
"120"
|
||||
]
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"name": "steam-game-focus",
|
||||
"vars": {
|
||||
"appId": 489830
|
||||
},
|
||||
"repeat": 6,
|
||||
"setup": [
|
||||
[
|
||||
"wake"
|
||||
]
|
||||
],
|
||||
"before": [
|
||||
[
|
||||
"start",
|
||||
"steam",
|
||||
"--wait",
|
||||
"--reuse",
|
||||
"--timeout",
|
||||
"240"
|
||||
]
|
||||
],
|
||||
"steps": [
|
||||
[
|
||||
"launch",
|
||||
"${appId}",
|
||||
"--wait",
|
||||
"--timeout",
|
||||
"240"
|
||||
],
|
||||
{
|
||||
"assert": "focus.focusedApp",
|
||||
"equals": "${appId}"
|
||||
},
|
||||
{
|
||||
"assert": "focus.focusableApps",
|
||||
"contains": "${appId}"
|
||||
},
|
||||
{
|
||||
"screenshot": "focused"
|
||||
},
|
||||
[
|
||||
"quit",
|
||||
"${appId}",
|
||||
"--timeout",
|
||||
"20"
|
||||
],
|
||||
{
|
||||
"assert": "remaining",
|
||||
"equals": []
|
||||
},
|
||||
{
|
||||
"sleep": 5
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
[
|
||||
"quit",
|
||||
"${appId}",
|
||||
"--timeout",
|
||||
"20"
|
||||
]
|
||||
],
|
||||
"teardown": [
|
||||
[
|
||||
"stop",
|
||||
"--timeout",
|
||||
"120"
|
||||
]
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
SOURCE = Path(__file__).resolve().parents[1] / "linuxfs/preload/net.c"
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform.startswith("linux") and shutil.which("cc"), "needs Linux and a C compiler")
|
||||
class CdpGuardTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.build = tempfile.TemporaryDirectory()
|
||||
cls.library = Path(cls.build.name) / "net.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", str(SOURCE), "-x", "c", "-", "-ldl", "-o", str(cls.library)],
|
||||
input=b"int bl_udevmon_stand_in(int fd) { return 0; }", check=True, capture_output=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.build.cleanup()
|
||||
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.registry = Path(self.temp.name) / "net"
|
||||
self.registry.mkdir()
|
||||
|
||||
def port(self, ipv6=False):
|
||||
with socket.socket(socket.AF_INET6 if ipv6 else socket.AF_INET) as s:
|
||||
s.bind(("::1" if ipv6 else "127.0.0.1", 0))
|
||||
return s.getsockname()[1]
|
||||
|
||||
def env(self, port):
|
||||
return dict(os.environ, LD_PRELOAD=str(self.library), BL_CDP_GUARD=str(port), BL_SYSV_DIR=self.temp.name)
|
||||
|
||||
def test_guarded_ipv4_bind_is_loopback_only(self):
|
||||
port = self.port()
|
||||
program = "import socket; s=socket.socket(); s.bind(('0.0.0.0', %d)); print(s.getsockname()[0])" % port
|
||||
result = subprocess.run([sys.executable, "-c", program], env=self.env(port), check=True, capture_output=True, text=True)
|
||||
self.assertEqual("127.0.0.1", result.stdout.strip())
|
||||
|
||||
def test_guarded_ipv6_bind_is_loopback_only(self):
|
||||
if not socket.has_ipv6:
|
||||
self.skipTest("needs IPv6")
|
||||
port = self.port(True)
|
||||
program = "import socket; s=socket.socket(socket.AF_INET6); s.bind(('::', %d)); print(s.getsockname()[0])" % port
|
||||
result = subprocess.run([sys.executable, "-c", program], env=self.env(port), check=True, capture_output=True, text=True)
|
||||
self.assertEqual("::1", result.stdout.strip())
|
||||
|
||||
def test_other_listeners_are_unchanged(self):
|
||||
port = self.port()
|
||||
program = "import socket; s=socket.socket(); s.bind(('0.0.0.0', %d)); print(s.getsockname()[0])" % port
|
||||
result = subprocess.run([sys.executable, "-c", program], env=self.env(0), check=True, capture_output=True, text=True)
|
||||
self.assertEqual("0.0.0.0", result.stdout.strip())
|
||||
|
||||
def connect(self, address, registered, stale=False, wrong_destination=False):
|
||||
port = self.port()
|
||||
program = "import socket; s=socket.socket(); s.bind(('0.0.0.0', %d)); s.listen(); print('ready', flush=True); c,_=s.accept(); c.sendall(b'accepted')" % port
|
||||
server = subprocess.Popen([sys.executable, "-u", "-c", program], env=self.env(port), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
try:
|
||||
self.assertEqual(b"ready\n", server.stdout.readline())
|
||||
with socket.socket() as client:
|
||||
client.settimeout(3)
|
||||
client.bind((address, 0))
|
||||
if registered:
|
||||
peer = client.getsockname()[1]
|
||||
inode = os.fstat(client.fileno()).st_ino + (1 if stale else 0)
|
||||
destination = port + (1 if wrong_destination else 0)
|
||||
(self.registry / ("p%d" % peer)).write_text("%d 1 %d %d %d %d %d\n" %
|
||||
(os.getpid(), os.getuid(), destination, client.fileno(), inode, socket.AF_INET))
|
||||
client.connect(("127.0.0.1", port))
|
||||
try:
|
||||
return client.recv(8)
|
||||
except ConnectionResetError:
|
||||
return b""
|
||||
finally:
|
||||
server.kill()
|
||||
server.wait()
|
||||
server.stdout.close()
|
||||
server.stderr.close()
|
||||
|
||||
def test_registered_guest_loopback_peer_is_accepted(self):
|
||||
self.assertEqual(b"accepted", self.connect("127.0.0.1", True))
|
||||
|
||||
def test_unregistered_local_peer_is_refused(self):
|
||||
self.assertEqual(b"", self.connect("127.0.0.1", False))
|
||||
|
||||
def test_matching_port_record_cannot_authorize_another_address(self):
|
||||
self.assertEqual(b"", self.connect("127.0.0.2", True))
|
||||
|
||||
def test_stale_socket_record_does_not_authorize_a_reused_port(self):
|
||||
self.assertEqual(b"", self.connect("127.0.0.1", True, stale=True))
|
||||
|
||||
def test_record_for_another_destination_does_not_authorize_cdp(self):
|
||||
self.assertEqual(b"", self.connect("127.0.0.1", True, wrong_destination=True))
|
||||
|
||||
def test_preloaded_guest_connect_still_works(self):
|
||||
port = self.port()
|
||||
program = "import socket; s=socket.socket(); s.bind(('0.0.0.0', %d)); s.listen(); print('ready', flush=True); c,_=s.accept(); c.sendall(b'accepted')" % port
|
||||
server = subprocess.Popen([sys.executable, "-u", "-c", program], env=self.env(port), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
try:
|
||||
self.assertEqual(b"ready\n", server.stdout.readline())
|
||||
client = "import socket; s=socket.create_connection(('127.0.0.1', %d), timeout=3); print(s.recv(8).decode())" % port
|
||||
result = subprocess.run([sys.executable, "-c", client], env=self.env(port), check=True, capture_output=True, text=True, timeout=5)
|
||||
self.assertEqual("accepted", result.stdout.strip())
|
||||
finally:
|
||||
server.kill()
|
||||
server.wait()
|
||||
server.stdout.close()
|
||||
server.stderr.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,250 @@
|
||||
"""The guest agent: its request files, exec, errors and the DevTools client, without a session."""
|
||||
import base64
|
||||
import hashlib
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import struct
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import tracemalloc
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin/droiddeck-agent"
|
||||
loader = importlib.machinery.SourceFileLoader("droiddeck_agent", str(SCRIPT))
|
||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
||||
agent = importlib.util.module_from_spec(spec)
|
||||
loader.exec_module(agent)
|
||||
|
||||
|
||||
class FakeDevTools(threading.Thread):
|
||||
"""GET /json lists one target; its WebSocket answers Runtime.evaluate the way Chromium does."""
|
||||
|
||||
def __init__(self, answer):
|
||||
super().__init__(daemon=True)
|
||||
self.answer = answer
|
||||
self.server = socket.socket()
|
||||
self.server.bind(("127.0.0.1", 0))
|
||||
self.server.listen(4)
|
||||
self.port = self.server.getsockname()[1]
|
||||
self.seen = []
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
try:
|
||||
conn, _ = self.server.accept()
|
||||
except OSError:
|
||||
return
|
||||
threading.Thread(target=self.serve, args=(conn,), daemon=True).start()
|
||||
|
||||
def serve(self, conn):
|
||||
head = b""
|
||||
while b"\r\n\r\n" not in head:
|
||||
head += conn.recv(4096)
|
||||
request = head.decode()
|
||||
if request.startswith("GET /json"):
|
||||
body = json.dumps([{"title": "SharedJSContext", "url": "https://steamloopback.host/", "type": "page",
|
||||
"webSocketDebuggerUrl": "ws://127.0.0.1:%d/devtools/page/1" % self.port}]).encode()
|
||||
conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n" % len(body) + body)
|
||||
conn.close()
|
||||
return
|
||||
key = [l.split(":", 1)[1].strip() for l in request.split("\r\n") if l.lower().startswith("sec-websocket-key")][0]
|
||||
accept = base64.b64encode(hashlib.sha1((key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode()).digest()).decode()
|
||||
conn.sendall(("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n"
|
||||
"Sec-WebSocket-Accept: %s\r\n\r\n" % accept).encode())
|
||||
b0, b1 = conn.recv(2)
|
||||
n = b1 & 0x7F
|
||||
if n == 126:
|
||||
n = struct.unpack(">H", conn.recv(2))[0]
|
||||
mask = conn.recv(4)
|
||||
data = b""
|
||||
while len(data) < n:
|
||||
data += conn.recv(n - len(data))
|
||||
message = json.loads(bytes(b ^ mask[i % 4] for i, b in enumerate(data)))
|
||||
self.seen.append(message)
|
||||
for reply in ({"method": "Runtime.consoleAPICalled", "params": {}}, dict(self.answer, id=message["id"])):
|
||||
payload = json.dumps(reply).encode()
|
||||
header = bytes([0x81, 126]) + struct.pack(">H", len(payload)) if len(payload) > 125 else bytes([0x81, len(payload)])
|
||||
conn.sendall(header + payload)
|
||||
time.sleep(0.2)
|
||||
conn.close()
|
||||
|
||||
|
||||
class AgentTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.dir = Path(self.temp.name)
|
||||
(self.dir / "resp").mkdir()
|
||||
|
||||
def ask(self, request):
|
||||
path = self.dir / "req.json"
|
||||
path.write_text(json.dumps(request))
|
||||
agent.answer(self.dir, path)
|
||||
self.assertFalse(path.exists(), "the request file is consumed")
|
||||
return json.loads((self.dir / "resp" / "req.json").read_text())
|
||||
|
||||
def test_exec_output_and_status(self):
|
||||
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "echo out; echo err >&2; exit 3"], "env": {"X": "1"}})
|
||||
self.assertTrue(answer["ok"])
|
||||
self.assertEqual(3, answer["exitCode"])
|
||||
self.assertEqual("out\n", answer["stdout"]["text"])
|
||||
self.assertEqual("err\n", answer["stderr"]["text"])
|
||||
self.assertFalse(answer["timedOut"])
|
||||
|
||||
def test_exec_environment_stdin_and_cap(self):
|
||||
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "printf %s \"$X\"; cat"], "env": {"X": "a"}, "stdin": "b"})
|
||||
self.assertEqual("ab", answer["stdout"]["text"])
|
||||
big = self.ask({"kind": "exec", "argv": ["sh", "-c", "head -c 300000 /dev/zero"]})
|
||||
self.assertTrue(big["stdout"]["truncated"])
|
||||
self.assertEqual(agent.OUTPUT_CAP, len(big["stdout"]["text"]))
|
||||
|
||||
def test_exec_timeout_kills_the_process_group(self):
|
||||
started = time.monotonic()
|
||||
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "sleep 30 & sleep 30"], "timeout": 0.5})
|
||||
self.assertTrue(answer["timedOut"])
|
||||
self.assertLess(time.monotonic() - started, 10)
|
||||
|
||||
def test_exec_memory_is_bounded_while_draining_large_output(self):
|
||||
tracemalloc.start()
|
||||
try:
|
||||
answer = agent.run_exec({"argv": [sys.executable, "-c", "import os; [os.write(1, b'x' * 65536) for _ in range(1024)]"]})
|
||||
_, peak = tracemalloc.get_traced_memory()
|
||||
finally:
|
||||
tracemalloc.stop()
|
||||
self.assertEqual(0, answer["exitCode"])
|
||||
self.assertEqual(agent.OUTPUT_CAP, len(answer["stdout"]["text"]))
|
||||
self.assertTrue(answer["stdout"]["truncated"])
|
||||
self.assertLess(peak, 6 * 1024 * 1024)
|
||||
|
||||
def test_exec_drains_both_streams_while_writing_stdin(self):
|
||||
program = "import os, sys; os.write(1, b'x' * 300000); print(len(sys.stdin.read()), file=sys.stderr)"
|
||||
answer = self.ask({"kind": "exec", "argv": [sys.executable, "-c", program], "stdin": "y" * 100000})
|
||||
self.assertFalse(answer["timedOut"])
|
||||
self.assertTrue(answer["stdout"]["truncated"])
|
||||
self.assertEqual("100000\n", answer["stderr"]["text"])
|
||||
|
||||
def test_flooding_both_streams_still_times_out(self):
|
||||
program = "import os\nwhile True:\n os.write(1, b'o' * 65536)\n os.write(2, b'e' * 65536)"
|
||||
answer = self.ask({"kind": "exec", "argv": [sys.executable, "-c", program], "timeout": 0.5})
|
||||
self.assertTrue(answer["timedOut"])
|
||||
for name in ("stdout", "stderr"):
|
||||
self.assertEqual(agent.OUTPUT_CAP, len(answer[name]["text"]))
|
||||
self.assertTrue(answer[name]["truncated"])
|
||||
|
||||
def test_timeout_applies_when_parent_exits_with_inherited_pipes(self):
|
||||
started = time.monotonic()
|
||||
answer = self.ask({"kind": "exec", "argv": ["sh", "-c", "sleep 30 & exit 0"], "timeout": 0.5})
|
||||
self.assertTrue(answer["timedOut"])
|
||||
self.assertLess(time.monotonic() - started, 5)
|
||||
|
||||
def test_invalid_timeout_and_input_are_refused_before_exec(self):
|
||||
for value in (0, -1, 601, "NaN", "Infinity", "invalid"):
|
||||
answer = self.ask({"kind": "exec", "argv": ["true"], "timeout": value})
|
||||
self.assertEqual("INVALID_REQUEST", answer["error"]["code"])
|
||||
for fields in ({"stdin": 1}, {"env": []}, {"env": {"BAD=KEY": "value"}}, {"argv": ["true\0"]}):
|
||||
answer = self.ask({"kind": "exec", "argv": ["true"], **fields})
|
||||
self.assertEqual("INVALID_REQUEST", answer["error"]["code"])
|
||||
|
||||
def test_errors_are_answers(self):
|
||||
self.assertEqual("INVALID_REQUEST", self.ask({"kind": "exec", "argv": []})["error"]["code"])
|
||||
self.assertEqual("EXEC_FAILED", self.ask({"kind": "exec", "argv": ["/no/such/program"]})["error"]["code"])
|
||||
self.assertEqual("UNKNOWN_REQUEST", self.ask({"kind": "nope"})["error"]["code"])
|
||||
self.assertEqual("INVALID_APP_ID", self.ask({"kind": "quit", "appId": "x"})["error"]["code"])
|
||||
(self.dir / "req.json").write_text("{not json")
|
||||
agent.answer(self.dir, self.dir / "req.json")
|
||||
self.assertEqual("INVALID_REQUEST", json.loads((self.dir / "resp" / "req.json").read_text())["error"]["code"])
|
||||
|
||||
def test_request_shape_and_size_are_bounded(self):
|
||||
self.assertEqual("INVALID_REQUEST", self.ask([])["error"]["code"])
|
||||
self.assertEqual("INVALID_REQUEST", self.ask({"kind": "ping", "unused": "x" * agent.REQUEST_CAP})["error"]["code"])
|
||||
|
||||
def test_replies_are_owner_only_even_when_replacing_a_public_file(self):
|
||||
path = self.dir / "private.json"
|
||||
path.write_text("old")
|
||||
path.chmod(0o644)
|
||||
agent.write_json(path, {"ok": True})
|
||||
self.assertEqual(0o600, path.stat().st_mode & 0o777)
|
||||
|
||||
def test_oversized_websocket_frames_are_refused_before_reading_payload(self):
|
||||
ws = agent.WebSocket.__new__(agent.WebSocket)
|
||||
ws.buffer = bytes([0x81, 127]) + struct.pack(">Q", agent.RESPONSE_CAP + 1)
|
||||
with self.assertRaises(agent.AgentError) as caught:
|
||||
ws.recv()
|
||||
self.assertEqual("RESPONSE_TOO_LARGE", caught.exception.code)
|
||||
|
||||
def test_devtools_targets_cannot_connect_to_remote_hosts(self):
|
||||
for url in ("ws://example.com/devtools", "ws://127.0.0.1@example.com/devtools", "wss://127.0.0.1/devtools"):
|
||||
with patch.object(agent.socket, "create_connection") as connect:
|
||||
with self.assertRaises(agent.AgentError) as caught:
|
||||
agent.WebSocket(url, 1)
|
||||
self.assertEqual("INVALID_TARGET", caught.exception.code)
|
||||
connect.assert_not_called()
|
||||
|
||||
def test_cdp_without_a_port(self):
|
||||
os.environ.pop("BL_CDP_PORT", None)
|
||||
self.assertEqual("NO_DEVTOOLS", self.ask({"kind": "cdp", "expression": "1"})["error"]["code"])
|
||||
|
||||
def test_cdp_eval(self):
|
||||
server = FakeDevTools({"result": {"result": {"type": "number", "value": 42}}})
|
||||
server.start()
|
||||
self.addCleanup(server.server.close)
|
||||
(self.dir / "cdp-port").write_text("%d\n" % server.port)
|
||||
answer = self.ask({"kind": "cdp", "expression": "6 * 7"})
|
||||
self.assertTrue(answer["ok"], answer)
|
||||
self.assertEqual(42, answer["value"])
|
||||
self.assertEqual("SharedJSContext", answer["target"])
|
||||
self.assertEqual("Runtime.evaluate", server.seen[0]["method"])
|
||||
self.assertEqual("6 * 7", server.seen[0]["params"]["expression"])
|
||||
targets = self.ask({"kind": "cdp", "op": "targets"})
|
||||
self.assertEqual("SharedJSContext", targets["targets"][0]["title"])
|
||||
missing = self.ask({"kind": "cdp", "expression": "1", "target": "Nope"})
|
||||
self.assertEqual("NO_TARGET", missing["error"]["code"])
|
||||
|
||||
def test_cdp_exception(self):
|
||||
server = FakeDevTools({"result": {"result": {"type": "object"}, "exceptionDetails": {
|
||||
"text": "Uncaught", "exception": {"description": "ReferenceError: SteamClient is not defined"}}}})
|
||||
server.start()
|
||||
self.addCleanup(server.server.close)
|
||||
(self.dir / "cdp-port").write_text(str(server.port))
|
||||
answer = self.ask({"kind": "cdp", "expression": "SteamClient"})
|
||||
self.assertEqual("JS_EXCEPTION", answer["error"]["code"])
|
||||
self.assertIn("ReferenceError", answer["error"]["message"])
|
||||
|
||||
def test_serve_answers_request_files(self):
|
||||
os.environ.pop("DISPLAY", None)
|
||||
thread = threading.Thread(target=agent.serve, args=(self.dir, False, 0.02), daemon=True)
|
||||
thread.start()
|
||||
for _ in range(100):
|
||||
if (self.dir / "hello.json").exists():
|
||||
break
|
||||
time.sleep(0.02)
|
||||
hello = json.loads((self.dir / "hello.json").read_text())
|
||||
self.assertEqual(os.getpid(), hello["pid"])
|
||||
self.assertFalse(hello["focus"])
|
||||
(self.dir / "req" / "a.json.tmp").write_text(json.dumps({"kind": "ping"}))
|
||||
os.replace(self.dir / "req" / "a.json.tmp", self.dir / "req" / "a.json")
|
||||
for _ in range(200):
|
||||
if (self.dir / "resp" / "a.json").exists():
|
||||
break
|
||||
time.sleep(0.02)
|
||||
self.assertEqual(agent.VERSION, json.loads((self.dir / "resp" / "a.json").read_text())["version"])
|
||||
|
||||
def test_game_pids_reads_the_steam_environment(self):
|
||||
import subprocess
|
||||
if not Path("/proc").is_dir():
|
||||
self.skipTest("needs /proc")
|
||||
child = subprocess.Popen(["sleep", "30"], env=dict(os.environ, SteamAppId="424242"))
|
||||
self.addCleanup(lambda: (child.kill(), child.wait()))
|
||||
time.sleep(0.2)
|
||||
self.assertIn(child.pid, agent.game_pids(424242))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,353 @@
|
||||
"""droiddeckctl and droiddeck-scenario against a fake adb: the JSON contract and exit codes."""
|
||||
import base64
|
||||
import contextlib
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import stat
|
||||
import sys
|
||||
import tempfile
|
||||
import textwrap
|
||||
import unittest
|
||||
|
||||
TOOLS = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(TOOLS))
|
||||
|
||||
|
||||
def load(name):
|
||||
loader = importlib.machinery.SourceFileLoader(name.replace("-", "_"), str(TOOLS / name))
|
||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
ctl = load("droiddeckctl")
|
||||
scenario = load("droiddeck-scenario")
|
||||
|
||||
# A stand-in for adb: answers the provider's content calls from a JSON config (a list answers
|
||||
# successive calls), records every invocation, and fakes the few shell commands droiddeckctl uses.
|
||||
FAKE_ADB = textwrap.dedent('''\
|
||||
#!/usr/bin/env python3
|
||||
import base64, json, os, sys
|
||||
config_path = os.environ["FAKE_ADB_CONFIG"]
|
||||
config = json.load(open(config_path))
|
||||
args = sys.argv[1:]
|
||||
if args[:1] == ["-s"]:
|
||||
args = args[2:]
|
||||
entry = {"args": args}
|
||||
if args[:1] == ["get-state"]:
|
||||
print("device"); sys.exit(0)
|
||||
if args[:3] == ["shell", "content", "call"]:
|
||||
method = args[args.index("--method") + 1]
|
||||
request = None
|
||||
if "--extra" in args:
|
||||
request = json.loads(base64.b64decode(args[args.index("--extra") + 1].split(":", 2)[2]))
|
||||
entry.update(method=method, request=request)
|
||||
answers = config["provider"].get(method, {"ok": False, "error": {"code": "UNKNOWN_COMMAND", "message": method}})
|
||||
if isinstance(answers, list):
|
||||
count = config.setdefault("counts", {}).get(method, 0)
|
||||
config["counts"][method] = count + 1
|
||||
answers = answers[min(count, len(answers) - 1)]
|
||||
json.dump(config, open(config_path, "w"))
|
||||
print("Result: Bundle[{json=" + json.dumps(answers) + "}]")
|
||||
elif args[:3] == ["shell", "dumpsys", "display"]:
|
||||
print(config.get("dumpsys_display", ""))
|
||||
elif args[:3] == ["shell", "dumpsys", "power"]:
|
||||
print(" mWakefulness=Awake")
|
||||
elif args[:2] == ["exec-out", "screencap"]:
|
||||
sys.stdout.buffer.write(b"\\x89PNG fake")
|
||||
with open(config["log"], "a") as log:
|
||||
log.write(json.dumps(entry) + "\\n")
|
||||
''')
|
||||
|
||||
DUMPSYS_DISPLAY = textwrap.dedent('''\
|
||||
Display Devices: size=2
|
||||
DisplayDeviceInfo{"Built-in Screen": uniqueId="local:111", 1080 x 1920}
|
||||
DisplayDeviceInfo{"Screen-2": uniqueId="local:222", 1080 x 1240}
|
||||
Logical Displays: size=2
|
||||
Display 0:
|
||||
mDisplayId=0
|
||||
mPrimaryDisplayDevice=Built-in Screen
|
||||
mBaseDisplayInfo=DisplayInfo{"Built-in Screen", displayId 0", real 1080 x 1920, x}
|
||||
Display 4:
|
||||
mDisplayId=4
|
||||
mPrimaryDisplayDevice=Screen-2
|
||||
mBaseDisplayInfo=DisplayInfo{"Screen-2", displayId 4", real 1080 x 1240, x}
|
||||
''')
|
||||
|
||||
|
||||
def state(phase="READY", focused=None, focusable=()):
|
||||
focus = None if focused is None else {"focusedApp": focused, "focusableApps": list(focusable),
|
||||
"baselayerAppIds": [], "seq": 1, "t": 0}
|
||||
return {"ok": True, "schema": 2, "session": {"id": "s1", "phase": phase, "running": phase == "READY",
|
||||
"focus": focus, "failure": None}}
|
||||
|
||||
|
||||
class FakeAdbTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.dir = Path(self.temp.name)
|
||||
self.adb = self.dir / "adb"
|
||||
self.adb.write_text(FAKE_ADB)
|
||||
self.adb.chmod(self.adb.stat().st_mode | stat.S_IEXEC)
|
||||
self.config = self.dir / "config.json"
|
||||
self.log = self.dir / "log.jsonl"
|
||||
self.configure({})
|
||||
os.environ["FAKE_ADB_CONFIG"] = str(self.config)
|
||||
self.addCleanup(os.environ.pop, "FAKE_ADB_CONFIG", None)
|
||||
|
||||
def configure(self, provider, **extra):
|
||||
self.config.write_text(json.dumps(dict({"provider": provider, "log": str(self.log)}, **extra)))
|
||||
|
||||
def run_ctl(self, *argv):
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(io.StringIO()):
|
||||
code = ctl.main(["--adb", str(self.adb), "--serial", "fake-1", *argv])
|
||||
return code, json.loads(out.getvalue())
|
||||
|
||||
def calls(self, method=None):
|
||||
if not self.log.exists():
|
||||
return []
|
||||
entries = [json.loads(line) for line in self.log.read_text().splitlines()]
|
||||
return [e for e in entries if method is None or e.get("method") == method]
|
||||
|
||||
|
||||
class DroiddeckctlTest(FakeAdbTest):
|
||||
def test_oversized_requests_are_refused_before_adb(self):
|
||||
with self.assertRaises(ctl.ControlError) as caught:
|
||||
ctl.provider_call(str(self.adb), "fake-1", "guest", request={"stdin": "x" * ctl.REQUEST_CAP})
|
||||
self.assertEqual("INVALID_REQUEST", caught.exception.code)
|
||||
self.assertFalse(self.calls())
|
||||
|
||||
def test_state_passes_through_with_the_serial(self):
|
||||
self.configure({"state": state()})
|
||||
code, out = self.run_ctl("state")
|
||||
self.assertEqual(0, code)
|
||||
self.assertEqual("READY", out["session"]["phase"])
|
||||
self.assertEqual("fake-1", out["deviceSerial"])
|
||||
|
||||
def test_launch_sends_the_app_id_and_waits_for_focus(self):
|
||||
self.configure({
|
||||
"launch": {"ok": True, "command": "launch", "appId": "489830"},
|
||||
"state": [state(focused=769, focusable=[769]), state(focused=489830, focusable=[769])],
|
||||
})
|
||||
# Focus alone is not enough: the game must also be in the list Steam orders from.
|
||||
code, out = self.run_ctl("launch", "489830", "--wait", "--timeout", "1")
|
||||
self.assertEqual(5, code)
|
||||
self.assertEqual("TIMEOUT", out["error"]["code"])
|
||||
self.assertIn("focused app 489830", out["error"]["message"])
|
||||
self.assertEqual({"appId": "489830"}, self.calls("launch")[0]["request"])
|
||||
|
||||
self.configure({
|
||||
"launch": {"ok": True, "command": "launch", "appId": "489830"},
|
||||
"state": [state(focused=769, focusable=[769]), state(focused=489830, focusable=[489830, 769])],
|
||||
})
|
||||
code, out = self.run_ctl("launch", "489830", "--wait", "--timeout", "5")
|
||||
self.assertEqual(0, code)
|
||||
self.assertEqual(489830, out["focus"]["focusedApp"])
|
||||
|
||||
def test_start_reuse_keeps_a_running_session(self):
|
||||
running = state(focused=769)
|
||||
running["session"]["mode"] = "steam"
|
||||
self.configure({"state": running})
|
||||
code, out = self.run_ctl("start", "steam", "--reuse", "--wait")
|
||||
self.assertEqual(0, code)
|
||||
self.assertTrue(out["reused"])
|
||||
self.assertFalse([c for c in self.calls() if c["args"][:2] == ["shell", "am"]])
|
||||
|
||||
def test_start_without_reuse_refuses_a_running_session(self):
|
||||
running = state()
|
||||
running["session"]["mode"] = "steam"
|
||||
self.configure({"state": running})
|
||||
code, out = self.run_ctl("start", "steam")
|
||||
self.assertEqual(4, code)
|
||||
self.assertEqual("SESSION_ACTIVE", out["error"]["code"])
|
||||
|
||||
def test_wait_game_ignores_the_client(self):
|
||||
self.configure({"state": [state(focused=769), state(focused=752590, focusable=[752590])]})
|
||||
code, out = self.run_ctl("wait", "game", "--timeout", "5")
|
||||
self.assertEqual(0, code)
|
||||
self.assertEqual(752590, out["session"]["focus"]["focusedApp"])
|
||||
|
||||
def test_a_failed_session_ends_a_wait(self):
|
||||
failed = state(phase="FAILED")
|
||||
failed["session"]["failure"] = {"code": "GUEST_EXIT", "message": "boom"}
|
||||
self.configure({"state": failed})
|
||||
code, out = self.run_ctl("wait", "focused", "1", "--timeout", "5")
|
||||
self.assertEqual(4, code)
|
||||
self.assertEqual("GUEST_EXIT", out["error"]["code"])
|
||||
|
||||
def test_disabled_agent_commands_exit_7(self):
|
||||
self.configure({"guest": {"ok": False, "error": {"code": "AGENT_COMMANDS_DISABLED", "message": "off"}}})
|
||||
code, out = self.run_ctl("guest", "--", "true")
|
||||
self.assertEqual(7, code)
|
||||
self.assertEqual("AGENT_COMMANDS_DISABLED", out["error"]["code"])
|
||||
|
||||
def test_run_checks_access_before_launching_an_activity(self):
|
||||
self.configure({"access": {"ok": True, "commands": False}})
|
||||
code, out = self.run_ctl("run", "/usr/bin/sh", "--", "-c", "true")
|
||||
self.assertEqual(7, code)
|
||||
self.assertEqual("AGENT_COMMANDS_DISABLED", out["error"]["code"])
|
||||
self.assertIn("Debugging tools", out["error"]["message"])
|
||||
self.assertEqual(["access"], [c.get("method") for c in self.calls()])
|
||||
|
||||
def test_override_checks_names_before_pushing_any_file(self):
|
||||
self.configure({"access": {"ok": True, "commands": True, "inbox": "/tmp/inbox"}})
|
||||
source = self.dir / "binary"
|
||||
source.write_bytes(b"test")
|
||||
code, out = self.run_ctl("override", "../outside", str(source))
|
||||
self.assertEqual(2, code)
|
||||
self.assertEqual("INVALID_NAME", out["error"]["code"])
|
||||
self.assertEqual(["access"], [c.get("method") for c in self.calls()])
|
||||
|
||||
def test_guest_request_and_check(self):
|
||||
self.configure({"guest": {"ok": True, "exitCode": 3, "stdout": {"text": ""}, "stderr": {"text": "nope"}}})
|
||||
code, out = self.run_ctl("guest", "--timeout", "7", "--env", "A=1", "--check", "--", "xprop", "-root")
|
||||
self.assertEqual(4, code)
|
||||
self.assertEqual("GUEST_EXIT", out["error"]["code"])
|
||||
self.assertEqual({"argv": ["xprop", "-root"], "timeout": 7.0, "env": {"A": "1"}},
|
||||
self.calls("guest")[0]["request"])
|
||||
|
||||
def test_screenshot_resolves_the_logical_display(self):
|
||||
self.configure({}, dumpsys_display=DUMPSYS_DISPLAY)
|
||||
target = self.dir / "shot.png"
|
||||
code, out = self.run_ctl("screenshot", str(target), "--display", "4")
|
||||
self.assertEqual(0, code)
|
||||
self.assertTrue(target.read_bytes().startswith(b"\x89PNG"))
|
||||
shot = [c for c in self.calls() if c["args"][:2] == ["exec-out", "screencap"]][0]
|
||||
self.assertEqual(["exec-out", "screencap", "-p", "-d", "222"], shot["args"])
|
||||
|
||||
def test_displays(self):
|
||||
self.configure({}, dumpsys_display=DUMPSYS_DISPLAY)
|
||||
code, out = self.run_ctl("displays")
|
||||
self.assertEqual(0, code)
|
||||
self.assertEqual([0, 4], [d["display"] for d in out["displays"]])
|
||||
self.assertEqual("222", out["displays"][1]["physicalId"])
|
||||
self.assertEqual([1080, 1240], out["displays"][1]["size"])
|
||||
|
||||
def test_unknown_display_is_an_error(self):
|
||||
self.configure({}, dumpsys_display=DUMPSYS_DISPLAY)
|
||||
code, out = self.run_ctl("screenshot", str(self.dir / "x.png"), "--display", "9")
|
||||
self.assertEqual(2, code)
|
||||
self.assertEqual("NO_SUCH_DISPLAY", out["error"]["code"])
|
||||
|
||||
def test_input_requests(self):
|
||||
self.configure({"input": {"ok": True, "command": "input"}})
|
||||
self.run_ctl("input", "tap", "0.5", "0.25", "--space", "fraction")
|
||||
self.run_ctl("input", "key", "enter", "--mod", "CTRL")
|
||||
self.run_ctl("input", "pad", "a", "--hold", "50")
|
||||
requests = [c["request"] for c in self.calls("input")]
|
||||
self.assertEqual({"type": "tap", "x": 0.5, "y": 0.25, "space": "fraction", "holdMs": 60}, requests[0])
|
||||
self.assertEqual({"type": "key", "key": "enter", "action": "tap", "modifiers": ["CTRL"], "holdMs": 40}, requests[1])
|
||||
self.assertEqual({"type": "pad", "button": "a", "action": "tap", "holdMs": 50}, requests[2])
|
||||
|
||||
def test_ui_click_by_tag_or_match(self):
|
||||
self.configure({"ui": {"ok": True, "command": "ui"}})
|
||||
self.run_ctl("ui", "click", "rail-steam")
|
||||
self.run_ctl("ui", "click", "--match", "Play", "--index", "1")
|
||||
requests = [c["request"] for c in self.calls("ui")]
|
||||
self.assertEqual({"op": "click", "index": 0, "tag": "rail-steam"}, requests[0])
|
||||
self.assertEqual({"op": "click", "index": 1, "match": "Play"}, requests[1])
|
||||
|
||||
def test_cdp_targets_and_eval_requests(self):
|
||||
self.configure({"cdp": {"ok": True, "targets": []}})
|
||||
self.assertEqual(0, self.run_ctl("cdp", "targets")[0])
|
||||
self.assertEqual(0, self.run_ctl("cdp", "eval", "1+1", "--target", "Big Picture")[0])
|
||||
requests = [c["request"] for c in self.calls("cdp")]
|
||||
self.assertEqual({"op": "targets"}, requests[0])
|
||||
self.assertEqual({"op": "eval", "expression": "1+1", "awaitPromise": True, "timeout": 15.0,
|
||||
"target": "Big Picture"}, requests[1])
|
||||
|
||||
def test_a_bug_still_answers_json(self):
|
||||
self.configure({"state": {"ok": True, "session": "not an object"}})
|
||||
code, out = self.run_ctl("wait", "game", "--timeout", "1")
|
||||
self.assertEqual(2, code)
|
||||
self.assertEqual("INTERNAL_ERROR", out["error"]["code"])
|
||||
|
||||
def test_wake(self):
|
||||
code, out = self.run_ctl("wake")
|
||||
self.assertEqual(0, code)
|
||||
self.assertEqual("Awake", out["wakefulness"])
|
||||
self.assertIn(["shell", "input", "keyevent", "KEYCODE_WAKEUP"], [c["args"] for c in self.calls()])
|
||||
|
||||
def test_override_pushes_to_the_inbox_first(self):
|
||||
self.configure({
|
||||
"access": {"ok": True, "commands": True, "inbox": "/sdcard/Android/data/x/files/agent-inbox"},
|
||||
"override": {"ok": True, "installed": "/opt/droiddeck-agent/bin/gamescope"},
|
||||
})
|
||||
binary = self.dir / "gamescope"
|
||||
binary.write_bytes(b"\x7fELF")
|
||||
code, out = self.run_ctl("override", "gamescope", str(binary))
|
||||
self.assertEqual(0, code)
|
||||
push = [c for c in self.calls() if c["args"][:1] == ["push"]][0]
|
||||
self.assertEqual("/sdcard/Android/data/x/files/agent-inbox/gamescope", push["args"][2])
|
||||
self.assertEqual({"name": "gamescope"}, self.calls("override")[0]["request"])
|
||||
|
||||
def test_override_refused_before_pushing_when_commands_are_off(self):
|
||||
self.configure({"access": {"ok": True, "commands": False, "inbox": "/x"}})
|
||||
binary = self.dir / "gamescope"
|
||||
binary.write_bytes(b"x")
|
||||
code, out = self.run_ctl("override", "gamescope", str(binary))
|
||||
self.assertEqual(7, code)
|
||||
self.assertFalse([c for c in self.calls() if c["args"][:1] == ["push"]])
|
||||
|
||||
|
||||
class ScenarioTest(FakeAdbTest):
|
||||
def write(self, body):
|
||||
path = self.dir / "scenario.json"
|
||||
path.write_text(json.dumps(body))
|
||||
return path
|
||||
|
||||
def run_scenario(self, path, *extra):
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(io.StringIO()):
|
||||
code = scenario.main([str(path), "--adb", str(self.adb), "--serial", "fake-1",
|
||||
"--out", str(self.dir / "runs"), *extra])
|
||||
return code, json.loads(out.getvalue())
|
||||
|
||||
def test_variables_asserts_and_repeats(self):
|
||||
self.configure({"state": state(focused=489830, focusable=[489830])})
|
||||
path = self.write({
|
||||
"name": "t", "vars": {"appId": 1}, "repeat": 2,
|
||||
"steps": [["state"], {"assert": "session.focus.focusedApp", "equals": "${appId}"}],
|
||||
})
|
||||
code, report = self.run_scenario(path, "--var", "appId=489830")
|
||||
self.assertEqual(0, code)
|
||||
self.assertEqual(2, report["passed"])
|
||||
output = Path(report["out"])
|
||||
self.assertEqual(0o700, output.stat().st_mode & 0o777)
|
||||
for artifact in output.rglob("*.json"):
|
||||
self.assertEqual(0o600, artifact.stat().st_mode & 0o777, str(artifact))
|
||||
for run in report["runs"]:
|
||||
self.assertEqual(0o700, Path(run["folder"]).stat().st_mode & 0o777)
|
||||
|
||||
def test_a_failed_assert_fails_the_run_and_collects_evidence(self):
|
||||
self.configure({"state": state(focused=769, focusable=[769]), "focus": {"ok": True, "focusedApp": 769}},
|
||||
dumpsys_display=DUMPSYS_DISPLAY)
|
||||
path = self.write({"name": "t", "repeat": 3,
|
||||
"steps": [["state"], {"assert": "session.focus.focusedApp", "equals": 489830}]})
|
||||
code, report = self.run_scenario(path)
|
||||
self.assertEqual(1, code)
|
||||
self.assertEqual(1, len(report["runs"])) # stops at the first failure without --keep-going
|
||||
self.assertIn("is 769", report["runs"][0]["error"])
|
||||
evidence = json.loads((Path(report["runs"][0]["folder"]) / "evidence.json").read_text())
|
||||
self.assertEqual(769, evidence["focus"]["focusedApp"])
|
||||
|
||||
def test_after_steps_run_after_a_failure(self):
|
||||
self.configure({"state": state(focused=769), "quit": {"ok": True, "remaining": []}})
|
||||
path = self.write({"name": "t", "repeat": 2, "vars": {"appId": 7},
|
||||
"steps": [["state"], {"assert": "session.focus.focusedApp", "equals": 7}],
|
||||
"after": [["quit", "${appId}"]]})
|
||||
code, report = self.run_scenario(path, "--keep-going")
|
||||
self.assertEqual(1, code)
|
||||
self.assertEqual(2, len(report["runs"]))
|
||||
self.assertEqual([{"appId": 7, "timeout": 15.0}] * 2, [c["request"] for c in self.calls("quit")])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user