mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 22:36:00 +00:00
feat(addons): add a CurseForge source to the addon browser (#3028)
* feat(addons): add a CurseForge source to the addon browser Search and install CurseForge addons from the Download addons window by driving the installed CurseForge app in the background: it starts minimized on the session's X display with its inspector bound to 127.0.0.1, and the window's own renderer services handle game detection, search, install and shutdown. Sign-in stays inside the CurseForge app. The Install buttons in the addon list now size to their label, so the label is centered and the button matches the other compact buttons. * fix(curseforge): secret inspector URL, hold mode and serialised starts - Start CurseForge with its Node inspector on 127.0.0.1:0 and publish only the UUID websocket URL, kept in the 0700 state directory, so no fixed unauthenticated debug port is exposed. - The agent evaluates through that URL instead of a fixed port. - Add a hold mode that runs as a session program and stops the app on exit, and a lock so hold and start never launch twice. * feat(addons): run CurseForge in a hidden session when none is running - Opening the CurseForge source with no session starts one headlessly (offscreen compositor output, shared start path with SessionActivity via CompositorHost) running the CurseForge hold helper; leaving the dialog stops the app and that session. A session the user already had is reused and left running. - Drop the "start a session" notice from every locale. - Addon browser link: cleanup shares the link's lock off the UI thread, stops the app at once on close, refuses work after close and stops a launch that finishes after close. - CurseForgeApp.start stops the app before every failed return. - instanceFor returns no instance when the populated list has no match. * Harden headless CurseForge session and helper lifecycle Scope the hidden session to its id, roll back partial starts, release the offscreen reader once the session ends, re-acquire the inspector when its endpoint goes away, bound DevTools calls by one deadline, key the installed snapshot by target, track the helper launch by pid start time and directory, fail on lock errors, and update the HDR gate test to its new location. * Map DevTools send failures and recheck the hidden session id * fix(addons): bind the hidden session to the id it claims at launch * Keep the CurseForge helper quiet when no launch is recorded
This commit is contained in:
1 parent
234e40157e
commit
210c127446
48 files changed
+1162
-67
No files matched your search
@@ -432,7 +432,7 @@ class FocusWatcher(threading.Thread):
|
||||
|
||||
class WebSocket:
|
||||
|
||||
def __init__(self, url, timeout):
|
||||
def __init__(self, url, timeout, bounded=False):
|
||||
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):
|
||||
@@ -442,6 +442,7 @@ class WebSocket:
|
||||
if target.query:
|
||||
path += "?" + target.query
|
||||
host = "127.0.0.1" if target.hostname == "localhost" else target.hostname
|
||||
self.deadline = time.monotonic() + timeout if bounded else None
|
||||
self.s = socket.create_connection((host, target.port or 80), timeout=timeout)
|
||||
try:
|
||||
self._handshake(path, hostport)
|
||||
@@ -457,6 +458,7 @@ class WebSocket:
|
||||
).encode())
|
||||
head = b""
|
||||
while b"\r\n\r\n" not in head:
|
||||
self._wait()
|
||||
chunk = self.s.recv(4096)
|
||||
if not chunk:
|
||||
raise OSError("DevTools closed the connection during the handshake")
|
||||
@@ -468,8 +470,17 @@ class WebSocket:
|
||||
raise OSError("DevTools refused the WebSocket: " + status.decode(errors="replace"))
|
||||
self.buffer = head.split(b"\r\n\r\n", 1)[1]
|
||||
|
||||
def _wait(self):
|
||||
if self.deadline is None:
|
||||
return
|
||||
left = self.deadline - time.monotonic()
|
||||
if left <= 0:
|
||||
raise socket.timeout("DevTools did not answer in time")
|
||||
self.s.settimeout(left)
|
||||
|
||||
def _read(self, n):
|
||||
while len(self.buffer) < n:
|
||||
self._wait()
|
||||
chunk = self.s.recv(65536)
|
||||
if not chunk:
|
||||
raise OSError("DevTools closed the connection")
|
||||
@@ -547,22 +558,51 @@ def cdp_targets(agent_dir, timeout=5):
|
||||
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)
|
||||
INSPECTOR_URL = re.compile(r"ws://127\.0\.0\.1:([0-9]{4,5})/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
|
||||
|
||||
|
||||
def inspector_url(url):
|
||||
found = INSPECTOR_URL.fullmatch(url) if isinstance(url, str) else None
|
||||
if not found or not 1024 <= int(found.group(1)) <= 65535:
|
||||
raise AgentError("INVALID_REQUEST", "inspector must be a ws://127.0.0.1 inspector URL with its session id")
|
||||
return url
|
||||
|
||||
|
||||
def cdp_eval(agent_dir, expression, target=None, await_promise=True, timeout=15, inspector=None,
|
||||
command_line_api=False):
|
||||
if inspector is not None:
|
||||
url, name = inspector_url(inspector), "inspector"
|
||||
else:
|
||||
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))
|
||||
url, name = match[0]["webSocketDebuggerUrl"], match[0].get("title")
|
||||
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())
|
||||
ws = WebSocket(url, timeout, bounded=True)
|
||||
except socket.timeout:
|
||||
raise AgentError("TIMEOUT", "No answer from DevTools in %ss" % timeout)
|
||||
except OSError as e:
|
||||
raise AgentError("DEVTOOLS_UNREACHABLE", "Could not reach DevTools: %s" % e)
|
||||
try:
|
||||
try:
|
||||
ws.send(json.dumps({"id": 1, "method": "Runtime.evaluate", "params": {
|
||||
"expression": expression, "returnByValue": True, "awaitPromise": await_promise,
|
||||
"userGesture": True, "includeCommandLineAPI": command_line_api,
|
||||
}}))
|
||||
except socket.timeout:
|
||||
raise AgentError("TIMEOUT", "No answer from DevTools in %ss" % timeout)
|
||||
except OSError as e:
|
||||
raise AgentError("DEVTOOLS_UNREACHABLE", "DevTools went away: %s" % e)
|
||||
while True:
|
||||
try:
|
||||
message = json.loads(ws.recv())
|
||||
except socket.timeout:
|
||||
raise AgentError("TIMEOUT", "No answer from DevTools in %ss" % timeout)
|
||||
except OSError as e:
|
||||
raise AgentError("DEVTOOLS_UNREACHABLE", "DevTools went away: %s" % e)
|
||||
if message.get("id") != 1:
|
||||
continue
|
||||
if "error" in message:
|
||||
@@ -573,9 +613,8 @@ def cdp_eval(agent_dir, expression, target=None, await_promise=True, timeout=15)
|
||||
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"),
|
||||
return {"target": name, "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()
|
||||
|
||||
@@ -1157,7 +1196,7 @@ def handle(agent_dir, req):
|
||||
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))
|
||||
request_timeout(req, 15), req.get("inspector"), req.get("commandLineApi") is True)
|
||||
if kind == "steam-selection":
|
||||
return steam_selection(agent_dir, request_timeout(req, 5))
|
||||
if kind == "quit":
|
||||
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
#!/bin/bash
|
||||
set -u
|
||||
umask 077
|
||||
self=${BASH_SOURCE[0]}
|
||||
proc=${DD_PROC:-/proc}
|
||||
state=${DD_CURSEFORGE_STATE:-${XDG_RUNTIME_DIR:-/tmp}/droiddeck-curseforge}
|
||||
runner=${DD_APPIMAGE_RUN:-${self%/*}/droiddeck-appimage-run}
|
||||
wait_s=${DD_CURSEFORGE_WAIT:-60}
|
||||
cmd=${1:-}
|
||||
dir=${2:-}
|
||||
flag=--inspect-publish-uid=stderr
|
||||
case $cmd in start|hold|stop|display) ;; *) echo "usage: droiddeck-curseforge start|hold|stop|display <imported AppImage directory>" >&2; exit 2 ;; esac
|
||||
if [ "$cmd" != display ] && [ ! -d "$dir/app" ]; then
|
||||
echo "droiddeck-curseforge: $dir is not an imported AppImage" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
x_display() {
|
||||
if [ -n "${DISPLAY:-}" ]; then
|
||||
printf '%s\n%s\n' "$DISPLAY" "${XAUTHORITY:-}"
|
||||
return 0
|
||||
fi
|
||||
local p name a prev display auth
|
||||
for p in "$proc"/[0-9]*; do
|
||||
name=$(tr '\0' '\n' 2>/dev/null < "$p/cmdline" | head -n 1)
|
||||
case ${name##*/} in Xwayland|Xorg|Xvfb) ;; *) continue ;; esac
|
||||
display= auth= prev=
|
||||
while IFS= read -r a; do
|
||||
case $a in :[0-9]*) [ -z "$display" ] && display=$a ;; esac
|
||||
[ "$prev" = -auth ] && auth=$a
|
||||
prev=$a
|
||||
done < <(tr '\0' '\n' 2>/dev/null < "$p/cmdline")
|
||||
if [ -n "$display" ]; then
|
||||
printf '%s\n%s\n' "$display" "$auth"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
running() {
|
||||
local p
|
||||
for p in "$proc"/[0-9]*; do
|
||||
tr '\0' '\n' 2>/dev/null < "$p/cmdline" | grep -qF -- "$dir/app/" && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
started_at() {
|
||||
local stat
|
||||
stat=$(cat "/proc/$1/stat" 2>/dev/null) || return 1
|
||||
stat=${stat##*) }
|
||||
set -- $stat
|
||||
echo "${20}"
|
||||
}
|
||||
|
||||
tracked() {
|
||||
local pid at launched
|
||||
read -r pid at launched 2>/dev/null < "$state/pid" || return 1
|
||||
case $pid in ''|*[!0-9]*) return 1 ;; esac
|
||||
kill -0 "$pid" 2>/dev/null && [ "$(started_at "$pid")" = "$at" ] || return 1
|
||||
printf '%s %s\n' "$pid" "$launched"
|
||||
}
|
||||
|
||||
ours() {
|
||||
local pid launched
|
||||
read -r pid launched <<<"$(tracked)"
|
||||
[ -n "$pid" ] && [ "$launched" = "$dir" ] && echo "$pid"
|
||||
}
|
||||
|
||||
lock() {
|
||||
mkdir -p "$state" && chmod 700 "$state" || return 1
|
||||
exec 9>"$state/lock" || return 1
|
||||
flock 9
|
||||
}
|
||||
|
||||
inspector() {
|
||||
grep -aom1 'ws://127\.0\.0\.1:[0-9]*/[0-9a-f]\{8\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{4\}-[0-9a-f]\{12\}' "$state/log" 2>/dev/null
|
||||
}
|
||||
|
||||
start() {
|
||||
local shown display auth url pid
|
||||
lock || { echo "droiddeck-curseforge: cannot lock $state" >&2; return 5; }
|
||||
if ! ours >/dev/null; then
|
||||
if tracked >/dev/null || running; then
|
||||
exec 9>&-
|
||||
echo busy
|
||||
return 3
|
||||
fi
|
||||
shown=$(x_display) || { exec 9>&-; echo "no X display" >&2; return 4; }
|
||||
display=$(sed -n 1p <<<"$shown")
|
||||
auth=$(sed -n 2p <<<"$shown")
|
||||
env -u DBUS_SESSION_BUS_ADDRESS -u WAYLAND_DISPLAY -u GAMESCOPE_WAYLAND_DISPLAY DISPLAY="$display" ${auth:+XAUTHORITY="$auth"} \
|
||||
setsid "$runner" "$dir" --minimized --inspect=127.0.0.1:0 "$flag" </dev/null >"$state/log" 2>&1 9>&- &
|
||||
pid=$!
|
||||
printf '%s %s %s\n' "$pid" "$(started_at "$pid")" "$dir" > "$state/pid"
|
||||
fi
|
||||
exec 9>&-
|
||||
for _ in $(seq 1 $((wait_s * 2))); do
|
||||
if url=$(inspector); then
|
||||
echo "$url"
|
||||
return 0
|
||||
fi
|
||||
ours >/dev/null || break
|
||||
sleep 0.5
|
||||
done
|
||||
tail -n 20 "$state/log" >&2
|
||||
return 5
|
||||
}
|
||||
|
||||
stop() {
|
||||
local pid p
|
||||
lock || return 1
|
||||
pid=$(ours) || { tracked >/dev/null || rm -f "$state/pid"; exec 9>&-; return 0; }
|
||||
kill -TERM -- "-$pid" 2>/dev/null
|
||||
for _ in $(seq 1 20); do
|
||||
kill -0 -- "-$pid" 2>/dev/null || break
|
||||
sleep 0.5
|
||||
done
|
||||
kill -KILL -- "-$pid" 2>/dev/null
|
||||
for p in "$proc"/[0-9]*; do
|
||||
tr '\0' '\n' 2>/dev/null < "$p/cmdline" | grep -qF -- "$dir/app/" && kill -KILL "${p##*/}" 2>/dev/null
|
||||
done
|
||||
rm -f "$state/pid"
|
||||
exec 9>&-
|
||||
return 0
|
||||
}
|
||||
|
||||
case $cmd in
|
||||
display)
|
||||
x_display || exit 4
|
||||
;;
|
||||
start)
|
||||
start
|
||||
exit $?
|
||||
;;
|
||||
hold)
|
||||
start >/dev/null || exit $?
|
||||
trap 'stop; exit 0' TERM INT HUP
|
||||
while ours >/dev/null; do
|
||||
sleep 1 &
|
||||
wait $!
|
||||
done
|
||||
exit 0
|
||||
;;
|
||||
stop)
|
||||
stop
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
Reference in new issue
Block a user