summaryrefslogtreecommitdiff
path: root/backend/decky_loader/injector.py
blob: 2e8846c69b9f5094f0bcb93897b3d7ccc9696b1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# Injector code from https://github.com/SteamDeckHomebrew/steamdeck-ui-inject. More info on how it works there.

from asyncio import sleep
from logging import getLogger
from typing import Any, Callable, List, TypedDict, Dict

from aiohttp import ClientSession
from aiohttp.client_exceptions import ClientConnectorError, ClientOSError
from asyncio.exceptions import TimeoutError
import uuid

BASE_ADDRESS = "http://localhost:8080"

logger = getLogger("Injector")

class _TabResponse(TypedDict):
    title: str
    id: str
    url: str
    webSocketDebuggerUrl: str

class Tab:
    cmd_id = 0

    def __init__(self, res: _TabResponse) -> None:
        self.title: str = res["title"]
        self.id: str = res["id"]
        self.url: str = res["url"]
        self.ws_url: str = res["webSocketDebuggerUrl"]

        self.websocket = None
        self.client = None

    async def open_websocket(self):
        self.client = ClientSession()
        self.websocket = await self.client.ws_connect(self.ws_url) # type: ignore

    async def close_websocket(self):
        if self.websocket:
            await self.websocket.close()
        if self.client:
            await self.client.close()

    async def listen_for_message(self):
        if self.websocket:
            async for message in self.websocket:
                data = message.json()
                yield data
            logger.warn(f"The Tab {self.title} socket has been disconnected while listening for messages.")
            await self.close_websocket()
            
    async def _send_devtools_cmd(self, dc: Dict[str, Any], receive: bool = True):
        if self.websocket:
            self.cmd_id += 1
            dc["id"] = self.cmd_id
            await self.websocket.send_json(dc)
            if receive:
                async for msg in self.listen_for_message():
                    if "id" in msg and msg["id"] == dc["id"]:
                        return msg
            return None
        raise RuntimeError("Websocket not opened")

    async def evaluate_js(self, js: str, run_async: bool | None = False, manage_socket: bool | None = True, get_result: bool = True):
        try:
            if manage_socket:
                await self.open_websocket()

            res = await self._send_devtools_cmd({
                "method": "Runtime.evaluate",
                "params": {
                    "expression": js,
                    "userGesture": True,
                    "awaitPromise": run_async
                }
            }, get_result)

        finally:
            if manage_socket:
                await self.close_websocket()
        return res

    async def has_global_var(self, var_name: str, manage_socket: bool = True):
        res = await self.evaluate_js(f"window['{var_name}'] !== null && window['{var_name}'] !== undefined", False, manage_socket)
        assert res is not None

        if not "result" in res or not "result" in res["result"] or not "value" in res["result"]["result"]:
            return False

        return res["result"]["result"]["value"]

    async def close(self, manage_socket: bool = True):
        try:
            if manage_socket:
                await self.open_websocket()

            res = await self._send_devtools_cmd({
                "method": "Page.close",
            }, False)

        finally:
            if manage_socket:
                await self.close_websocket()
        return res

    async def enable(self):
        """
        Enables page domain notifications.
        """
        await self._send_devtools_cmd({
            "method": "Page.enable",
        }, False)

    async def disable(self):
        """
        Disables page domain notifications.
        """
        await self._send_devtools_cmd({
            "method": "Page.disable",
        }, False)

    async def refresh(self, manage_socket: bool = True):
        try:
            if manage_socket:
                await self.open_websocket()

            await self._send_devtools_cmd({
                "method": "Page.reload",
            }, False)

        finally:
            if manage_socket:
                await self.close_websocket()

        return
    async def reload_and_evaluate(self, js: str, manage_socket: bool = True):
        """
        Reloads the current tab, with JS to run on load via debugger
        """
        try:
            if manage_socket:
                await self.open_websocket()

            await self._send_devtools_cmd({
                "method": "Debugger.enable"
            }, True)

            await self._send_devtools_cmd({
                "method": "Runtime.evaluate",
                "params": {
                    "expression": "location.reload();",
                    "userGesture": True,
                    "awaitPromise": False
                }
            }, False)

            breakpoint_res = await self._send_devtools_cmd({
                "method": "Debugger.setInstrumentationBreakpoint",
                "params": {
                    "instrumentation": "beforeScriptExecution"
                }
            }, True)

            assert breakpoint_res is not None

            logger.info(breakpoint_res)
            
            # Page finishes loading when breakpoint hits

            for _ in range(20):
                # this works around 1/5 of the time, so just send it 8 times.
                # the js accounts for being injected multiple times allowing only one instance to run at a time anyway
                await self._send_devtools_cmd({
                    "method": "Runtime.evaluate",
                    "params": {
                        "expression": js,
                        "userGesture": True,
                        "awaitPromise": False
                    }
                }, False)

            await self._send_devtools_cmd({
                "method": "Debugger.removeBreakpoint",
                "params": {
                    "breakpointId": breakpoint_res["result"]["breakpointId"]
                }
            }, False)

            for _ in range(4):
                await self._send_devtools_cmd({
                    "method": "Debugger.resume"
                }, False)

            await self._send_devtools_cmd({
                "method": "Debugger.disable"
            }, True)

        finally:
            if manage_socket:
                await self.close_websocket()
        return

    async def add_script_to_evaluate_on_new_document(self, js: str, add_dom_wrapper: bool = True, manage_socket: bool = True, get_result: bool = True):
        """
        How the underlying call functions is not particularly clear from the devtools docs, so stealing puppeteer's description:

        Adds a function which would be invoked in one of the following scenarios:
        * whenever the page is navigated
        * whenever the child frame is attached or navigated. In this case, the
          function is invoked in the context of the newly attached frame.

        The function is invoked after the document was created but before any of
        its scripts were run. This is useful to amend the JavaScript environment,
        e.g. to seed `Math.random`.

        Parameters
        ----------
        js : str
            The script to evaluate on new document
        add_dom_wrapper : bool
            True to wrap the script in a wait for the 'DOMContentLoaded' event.
            DOM will usually not exist when this execution happens,
            so it is necessary to delay til DOM is loaded if you are modifying it
        manage_socket : bool
            True to have this function handle opening/closing the websocket for this tab
        get_result : bool
            True to wait for the result of this call

        Returns
        -------
        int or None
            The identifier of the script added, used to remove it later.
            (see remove_script_to_evaluate_on_new_document below)
            None is returned if `get_result` is False
        """
        try:

            wrappedjs = """
            function scriptFunc() {
                {js}
            }
            if (document.readyState === 'loading') {
                addEventListener('DOMContentLoaded', () => {
                scriptFunc();
            });
            } else {
                scriptFunc();
            }
            """.format(js=js) if add_dom_wrapper else js

            if manage_socket:
                await self.open_websocket()

            res = await self._send_devtools_cmd({
                "method": "Page.addScriptToEvaluateOnNewDocument",
                "params": {
                    "source": wrappedjs
                }
            }, get_result)

        finally:
            if manage_socket:
                await self.close_websocket()
        return res

    async def remove_script_to_evaluate_on_new_document(self, script_id: str, manage_socket: bool = True):
        """
        Removes a script from a page that was added with `add_script_to_evaluate_on_new_document`

        Parameters
        ----------
        script_id : int
            The identifier of the script to remove (returned from `add_script_to_evaluate_on_new_document`)
        """

        try:
            if manage_socket:
                await self.open_websocket()

            await self._send_devtools_cmd({
                "method": "Page.removeScriptToEvaluateOnNewDocument",
                "params": {
                    "identifier": script_id
                }
            }, False)

        finally:
            if manage_socket:
                await self.close_websocket()

    async def has_element(self, element_name: str, manage_socket: bool = True):
        res = await self.evaluate_js(f"document.getElementById('{element_name}') != null", False, manage_socket)
        assert res is not None

        if not "result" in res or not "result" in res["result"] or not "value" in res["result"]["result"]:
            return False

        return res["result"]["result"]["value"]

    async def inject_css(self, style: str, manage_socket: bool = True):
        try:
            css_id = str(uuid.uuid4())

            result = await self.evaluate_js(
                f"""
                (function() {{
                    const style = document.createElement('style');
                    style.id = "{css_id}";
                    document.head.append(style);
                    style.textContent = `{style}`;
                }})()
                """, False, manage_socket)

            assert result is not None

            if "exceptionDetails" in result["result"]:
                return {
                    "success": False,
                    "result": result["result"]
                }

            return {
                "success": True,
                "result": css_id
            }
        except Exception as e:
            return {
                "success": False,
                "result": e
            }

    async def remove_css(self, css_id: str, manage_socket: bool = True):
        try:
            result = await self.evaluate_js(
                f"""
                (function() {{
                    let style = document.getElementById("{css_id}");

                    if (style.nodeName.toLowerCase() == 'style')
                        style.parentNode.removeChild(style);
                }})()
                """, False, manage_socket)

            assert result is not None

            if "exceptionDetails" in result["result"]:
                return {
                    "success": False,
                    "result": result
                }

            return {
                "success": True
            }
        except Exception as e:
            return {
                "success": False,
                "result": e
            }

    async def get_steam_resource(self, url: str):
        res = await self.evaluate_js(f'(async function test() {{ return await (await fetch("{url}")).text() }})()', True)
        assert res is not None
        return res["result"]["result"]["value"]

    def __repr__(self):
        return self.title


async def get_tabs() -> List[Tab]:
    res = {}

    na = False
    while True:
        try:
            async with ClientSession() as web:
                res = await web.get(f"{BASE_ADDRESS}/json", timeout=3)
        except ClientConnectorError:
            if not na:
                logger.debug("Steam isn't available yet. Wait for a moment...")
                na = True
            await sleep(5)
        except ClientOSError:
            logger.warn(f"The request to {BASE_ADDRESS}/json was reset")
            await sleep(1)
        except TimeoutError:
            logger.warn(f"The request to {BASE_ADDRESS}/json timed out")
            await sleep(1)
        else:
            break

    if res.status == 200:
        r = await res.json()
        return [Tab(i) for i in r]
    else:
        raise Exception(f"/json did not return 200. {await res.text()}")


async def get_tab(tab_name: str) -> Tab:
    tabs = await get_tabs()
    tab = next((i for i in tabs if i.title == tab_name), None)
    if not tab:
        raise ValueError(f"Tab {tab_name} not found")
    return tab

async def get_tab_lambda(test: Callable[[Tab], bool]) -> Tab:
    tabs = await get_tabs()
    tab = next((i for i in tabs if test(i)), None)
    if not tab:
        raise ValueError(f"Tab not found by lambda")
    return tab

SHARED_CTX_NAMES = ["SharedJSContext", "Steam Shared Context presented by Valve™", "Steam", "SP"]
CLOSEABLE_URLS = ["about:blank", "data:text/html,%3Cbody%3E%3C%2Fbody%3E"] # Closing anything other than these *really* likes to crash Steam
DO_NOT_CLOSE_URLS = ["Valve Steam Gamepad/default", "Valve%20Steam%20Gamepad/default"] # Steam Big Picture Mode tab

def tab_is_gamepadui(t: Tab) -> bool:
    return "https://steamloopback.host/routes/" in t.url and t.title in SHARED_CTX_NAMES

async def get_gamepadui_tab() -> Tab:
    tabs = await get_tabs()
    tab = next((i for i in tabs if tab_is_gamepadui(i)), None)
    if not tab:
        raise ValueError(f"GamepadUI Tab not found")
    return tab

async def inject_to_tab(tab_name: str, js: str, run_async: bool = False):
    tab = await get_tab(tab_name)

    return await tab.evaluate_js(js, run_async)

async def close_old_tabs():
    tabs = await get_tabs()
    for t in tabs:
        if not t.title or (t.title not in SHARED_CTX_NAMES and any(url in t.url for url in CLOSEABLE_URLS) and not any(url in t.url for url in DO_NOT_CLOSE_URLS)):
            logger.debug("Closing tab: " + getattr(t, "title", "Untitled"))
            await t.close()
            await sleep(0.5)