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 --- src/main.rs | 148 ----------------------------------------------------- src/plugin_page.js | 82 ----------------------------- 2 files changed, 230 deletions(-) delete mode 100644 src/main.rs delete mode 100644 src/plugin_page.js (limited to 'src') diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 36143ccc..00000000 --- a/src/main.rs +++ /dev/null @@ -1,148 +0,0 @@ -use std::fmt::{Debug, Display, Formatter}; -use std::fs; -use hyper::{Client, Uri}; -use hyper::body::Buf; -use serde::{ Serialize, Deserialize }; -use serde_json; -use tungstenite::Message; - -type TokioResult = std::result::Result>; - -enum AppError { - ContentNotFound -} - -impl Debug for AppError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let message = match self { - AppError::ContentNotFound => "Content not found" - }; - - write!(f, "{}", message) - } -} - -impl Display for AppError { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self) - } -} - -unsafe impl Send for AppError { } -unsafe impl Sync for AppError { } - -impl std::error::Error for AppError { } - -#[allow(non_snake_case)] -#[allow(dead_code)] -#[derive(Deserialize)] -struct WebContent { - description: String, - devtoolsFrontendUrl: String, - id: String, - title: String, - r#type: String, - url: String, - webSocketDebuggerUrl: String -} - -#[allow(non_snake_case)] -#[allow(dead_code)] -#[derive(Serialize)] -struct DebuggerCommandParams { - expression: String, - userGesture: bool -} - -#[allow(non_snake_case)] -#[allow(dead_code)] -#[derive(Serialize)] -struct DebuggerCommand { - id: u32, - method: String, - params: DebuggerCommandParams -} - -/// Downloads all content from a website -async fn get_web_content(url: Uri) -> TokioResult> { - let client = Client::new(); - let response = client.get(url).await?; - let body = hyper::body::aggregate(response).await?; - - let data = String::from(std::str::from_utf8(body.chunk())?); - - Ok(serde_json::from_str(data.as_str())?) -} - -/// Loads plugins -fn load_plugins() -> String { - let paths = fs::read_dir("./plugins"); - if let Ok(paths) = paths { - - let mut result = String::new(); - - for entry in paths { - if let Ok(entry) = entry { - if let Ok(file_type) = entry.file_type() { - if file_type.is_file() { - if let Ok(content) = fs::read_to_string(entry.path()) { - result.push_str(format!("plugins.push(new {});", content).as_str()); - } - } - } - } - } - - result - } else { - String::from("") - } -} - -#[tokio::main] -async fn main() -> TokioResult<()> { - // If CEF Debugging is enabled, it will be accessible through port 8080 - let url = "http://127.0.0.1:8080/json".parse::().unwrap(); - - // Load all available tabs that can be debugged - let contents = get_web_content(url).await?; - - // Find QuickAccess tab (sidebar menu) and get the debugger websocket interface url - let mut quick_access_debug_url: Option = None; - for content in &contents { - if content.title == "QuickAccess" { - quick_access_debug_url = Some(content.webSocketDebuggerUrl.clone()); - } - } - - if let Some(url) = quick_access_debug_url { - - // Connect to debugger websocket - let (mut socket, _) = tungstenite::connect(url)?; - - // Create a inject command to send to the debugger - let command = DebuggerCommand { - id: 1, - method: String::from("Runtime.evaluate"), - params: DebuggerCommandParams { - expression: String::from(include_str!("plugin_page.js").replace("{{ PLUGINS }}", load_plugins().as_str())), - userGesture: true - } - }; - - // Send command to debugger - socket.write_message(Message::Text(serde_json::to_string(&command)?))?; - - // Print response - let response = socket.read_message()?; - println!("{}", response); - - socket.close(None)?; - - } else { - return Err(AppError::ContentNotFound.into()); - } - - - Ok(()) -} diff --git a/src/plugin_page.js b/src/plugin_page.js deleted file mode 100644 index 537f53a3..00000000 --- a/src/plugin_page.js +++ /dev/null @@ -1,82 +0,0 @@ -(function () { - let plugins = []; - - {{ PLUGINS }} - - const PLUGIN_ICON = ` - - - - `; - - function createTitle(text) { - return `
${text}
`; - } - - function createTabGroupPanel(content) { - return `
${content}
`; - } - - function createPanelSelection(content) { - return `
${content}
`; - } - - function createPanelSelectionRow(content) { - return `
${content}
`; - } - - function createButton(text, id) { - return ` -
-
- -
-
- `; - } - - function createPluginList() { - let pages = document.getElementsByClassName("quickaccessmenu_AllTabContents_2yKG4 quickaccessmenu_Down_3rR0o")[0]; - let pluginPage = pages.children[pages.children.length - 1]; - - pluginPage.innerHTML = createTitle("Plugins"); - - let buttons = ""; - for (let i = 0; i < plugins.length; i++) { - buttons += createPanelSelectionRow(createButton(plugins[i].getName(), "plugin_btn_" + i)) - } - - pluginPage.innerHTML += createTabGroupPanel(createPanelSelection(buttons)); - - for (let i = 0; i < plugins.length; i++) { - document.getElementById("plugin_btn_" + i).onclick = (function(plugin, pluginPage) { - return function() { - pluginPage.innerHTML = createButton("Back", "plugin_back") + createTitle(plugin.getName()) + createTabGroupPanel(plugin.getPageContent()); - plugin.runCode(); - - document.getElementById("plugin_back").onclick = (e) => { - createPluginList(); - }; - }; - }(plugins[i], pluginPage)) - } - - } - - function inject() { - let tabs = document.getElementsByClassName("quickaccessmenu_TabContentColumn_2z5NL Panel Focusable")[0]; - tabs.children[tabs.children.length - 1].innerHTML = PLUGIN_ICON; - - createPluginList(); - } - - let injector = setInterval(function () { - if (document.hasFocus()) { - inject(); - clearInterval(injector); - } - }, 100); -})(); \ No newline at end of file -- cgit v1.2.3