From 5e9c12bac838730d4e216b3779227a9a94447e40 Mon Sep 17 00:00:00 2001 From: marios Date: Sun, 3 Apr 2022 23:50:26 +0300 Subject: Python rewrite (#6) * Initial commit. Untested * various fixes Core functionality confirmed working: - Iframe injection into steam client - Plugin fetching from the iframe - Plugin opening * Added function to fetch resources from steam * Improved injector module, added server-js communication - Injector module now has methods for better lower-level manipulation of the tab debug websocket. - Our "front-end" can now communicate with the manager (2-way), completely bypassing the chromium sandbox. This works via a dirty debug console trick, whoever wants to know how it works can take a look at the code. - Added utility methods file, along with an implementation of the aiohttp client that our "front-end" can access, via the system described above. - Added js implementations of the communication system described above, which can be imported by plugins. * Added steam_resource endpoint * Added basic installer script * retry logic bug fix * fixed library injection, event propagation, websocket handling - library is injected directly into the plugins as well as the plugin list - resolveMethodCall is implemented in the plugin_list.js file, which in turns calls window.sendMessage on the iframe to propagate the event - websocket method calls are processed in their own tasks now, so as not to block on long-running calls. Co-authored-by: tza Co-authored-by: WerWolv --- plugin_loader/loader.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 plugin_loader/loader.py (limited to 'plugin_loader/loader.py') diff --git a/plugin_loader/loader.py b/plugin_loader/loader.py new file mode 100644 index 00000000..0ed58b39 --- /dev/null +++ b/plugin_loader/loader.py @@ -0,0 +1,60 @@ +from aiohttp import web +from aiohttp_jinja2 import template + +from os import path, listdir +from importlib.util import spec_from_file_location, module_from_spec +from logging import getLogger + +import injector + +class Loader: + def __init__(self, server_instance, plugin_path) -> None: + self.logger = getLogger("Loader") + self.plugin_path = plugin_path + self.plugins = self.import_plugins() + + server_instance.add_routes([ + web.get("/plugins/iframe", self.plugin_iframe_route), + web.get("/plugins/reload", self.reload_plugins), + web.post("/plugins/method_call", self.handle_plugin_method_call), + web.get("/plugins/load/{name}", self.load_plugin), + web.get("/steam_resource/{path:.+}", self.get_steam_resource) + ]) + + def import_plugins(self): + files = [i for i in listdir(self.plugin_path) if i.endswith(".py")] + dc = {} + for file in files: + try: + spec = spec_from_file_location("_", path.join(self.plugin_path, file)) + module = module_from_spec(spec) + spec.loader.exec_module(module) + dc[module.Plugin.name] = module.Plugin + self.logger.info("Loaded {}".format(module.Plugin.name)) + except Exception as e: + self.logger.error("Could not load {}. {}".format(file, e)) + return dc + + async def reload_plugins(self, request=None): + self.logger.info("Re-importing all plugins.") + self.plugins = self.import_plugins() + + async def handle_plugin_method_call(self, plugin_name, method_name, **kwargs): + return await getattr(self.plugins[plugin_name], method_name)(**kwargs) + + async def get_steam_resource(self, request): + tab = (await injector.get_tabs())[0] + return web.Response(text=await tab.get_steam_resource(f"https://steamloopback.host/{request.match_info['path']}"), content_type="text/html") + + async def load_plugin(self, request): + plugin = self.plugins[request.match_info["name"]] + ret = """ + + + {} + """.format(plugin.name, plugin.main_view_html) + return web.Response(text=ret, content_type="text/html") + + @template('plugin_view.html') + async def plugin_iframe_route(self, request): + return {"plugins": self.plugins.values()} -- cgit v1.2.3