From 08f4126ee6a99777e3e586be272f5839d873558e Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Fri, 8 Sep 2023 19:59:41 +0200 Subject: [PATCH] Store global DevSessions --- .../main/java/org/matrix/chromext/Chrome.kt | 18 ++++-------------- .../main/java/org/matrix/chromext/Listener.kt | 8 +++----- .../org/matrix/chromext/devtools/Inspect.kt | 18 ++++++++++++++++++ .../chromext/devtools/WebSocketClient.kt | 9 +++++++++ .../org/matrix/chromext/proxy/UserScript.kt | 3 ++- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/matrix/chromext/Chrome.kt b/app/src/main/java/org/matrix/chromext/Chrome.kt index e0ecb888..f496b721 100644 --- a/app/src/main/java/org/matrix/chromext/Chrome.kt +++ b/app/src/main/java/org/matrix/chromext/Chrome.kt @@ -8,6 +8,7 @@ import java.io.File import java.lang.ref.WeakReference import java.util.concurrent.Executors import org.json.JSONObject +import org.matrix.chromext.devtools.DevSessions import org.matrix.chromext.devtools.DevToolClient import org.matrix.chromext.devtools.getInspectPages import org.matrix.chromext.devtools.hitDevTools @@ -31,7 +32,6 @@ object Chrome { var isVivaldi = false val IO = Executors.newCachedThreadPool() - val cspBypassed = mutableSetOf() fun init(ctx: Context, packageName: String? = null) { val initialized = mContext != null @@ -116,25 +116,15 @@ object Chrome { private fun evaluateJavascriptDevTools(codes: List, tabId: String, bypassCSP: Boolean) { wakeUpDevTools() - val lastClient = cspBypassed.find { it.tabId == tabId } - var client = lastClient ?: DevToolClient(tabId) + var client = DevSessions.get(tabId) ?: DevToolClient(tabId) if (client.isClosed()) { - cspBypassed.remove(client) hitDevTools().close() client = DevToolClient(tabId) } codes.forEach { client.evaluateJavascript(it) } // Bypass CSP is only effective after reloading + client.bypassCSP(bypassCSP) - if ((lastClient == client) != bypassCSP) { - client.command(null, "Page.enable", JSONObject()) - client.command(null, "Page.setBypassCSP", JSONObject().put("enabled", bypassCSP)) - if (bypassCSP) { - cspBypassed.add(client) - } else { - cspBypassed.remove(client) - } - } if (!bypassCSP) client.close() } @@ -145,7 +135,7 @@ object Chrome { bypassCSP: Boolean = false, ) { if (forceDevTools) { - this.IO.submit { + IO.submit { val tabId = if (WebViewHook.isInit) { val url = getUrl(currentTab) diff --git a/app/src/main/java/org/matrix/chromext/Listener.kt b/app/src/main/java/org/matrix/chromext/Listener.kt index 2ce34a33..e182595f 100644 --- a/app/src/main/java/org/matrix/chromext/Listener.kt +++ b/app/src/main/java/org/matrix/chromext/Listener.kt @@ -9,6 +9,7 @@ import java.net.HttpURLConnection import java.net.URL import org.json.JSONArray import org.json.JSONObject +import org.matrix.chromext.devtools.DevSessions import org.matrix.chromext.devtools.DevToolClient import org.matrix.chromext.devtools.getInspectPages import org.matrix.chromext.devtools.hitDevTools @@ -31,7 +32,6 @@ import org.matrix.chromext.utils.parseOrigin object Listener { val xmlhttpRequests = mutableMapOf() - val devSessions = mutableSetOf() val allowedActions = mapOf( "front-end" to listOf("inspect_pages", "userscript", "extension"), @@ -275,7 +275,7 @@ object Listener { "websocket" -> { val detail = JSONObject(payload) val targetTabId = detail.getString("targetTabId") - var target = devSessions.find { it.tabId == targetTabId } + var target = DevSessions.get(targetTabId) if (detail.has("message")) { val message = JSONObject(detail.getString("message")) target?.command( @@ -285,17 +285,15 @@ object Listener { if (Chrome.checkTab(currentTab)) { Chrome.evaluateJavascript(listOf("ChromeXt.post('websocket', ${res})"), currentTab) } else { - Log.d("Tab closed") target?.close() } } Chrome.IO.submit { target?.close() - devSessions.remove(target) hitDevTools().close() target = DevToolClient(targetTabId) if (!target!!.isClosed()) { - devSessions.add(target!!) + DevSessions.add(target!!) response(JSONObject(mapOf("open" to true))) target!!.listen { response(JSONObject(mapOf("message" to it))) } } diff --git a/app/src/main/java/org/matrix/chromext/devtools/Inspect.kt b/app/src/main/java/org/matrix/chromext/devtools/Inspect.kt index 9ea5b652..d8772b5f 100644 --- a/app/src/main/java/org/matrix/chromext/devtools/Inspect.kt +++ b/app/src/main/java/org/matrix/chromext/devtools/Inspect.kt @@ -40,3 +40,21 @@ fun hitDevTools(): LocalSocket { client.outputStream.write("GET /json HTTP/1.1\r\n\r\n".toByteArray()) return client } + +object DevSessions { + private val clients = mutableSetOf() + fun get(tabId: String): DevToolClient? { + var cached = clients.find { it.tabId == tabId } + if (cached?.isClosed() == true) { + clients.remove(cached) + cached = null + } + return cached + } + fun add(client: DevToolClient?) { + if (client == null) return + val cached = clients.find { it.tabId == client.tabId } + if (cached != null) clients.remove(cached) + clients.add(client) + } +} diff --git a/app/src/main/java/org/matrix/chromext/devtools/WebSocketClient.kt b/app/src/main/java/org/matrix/chromext/devtools/WebSocketClient.kt index 3b2b28c1..7d6f75dc 100644 --- a/app/src/main/java/org/matrix/chromext/devtools/WebSocketClient.kt +++ b/app/src/main/java/org/matrix/chromext/devtools/WebSocketClient.kt @@ -16,6 +16,7 @@ import org.matrix.chromext.utils.Log class DevToolClient(tabId: String) : LocalSocket() { val tabId = tabId + private var cspBypassed = false private var id = 1 private var mClosed = false @@ -65,6 +66,14 @@ class DevToolClient(tabId: String) : LocalSocket() { WebSocketFrame(msg.toString()).write(outputStream) } + fun bypassCSP(bypass: Boolean) { + if (cspBypassed == bypass) return + command(null, "Page.enable", JSONObject()) + command(null, "Page.setBypassCSP", JSONObject().put("enabled", bypass)) + cspBypassed = bypass + if (bypass) DevSessions.add(this) + } + fun evaluateJavascript(script: String) { command(this.id, "Runtime.evaluate", JSONObject(mapOf("expression" to script))) this.id += 1 diff --git a/app/src/main/java/org/matrix/chromext/proxy/UserScript.kt b/app/src/main/java/org/matrix/chromext/proxy/UserScript.kt index 8891eb33..d6c9b205 100644 --- a/app/src/main/java/org/matrix/chromext/proxy/UserScript.kt +++ b/app/src/main/java/org/matrix/chromext/proxy/UserScript.kt @@ -65,7 +65,8 @@ object UserScriptProxy { val kMaxURLChars = 2097152 private fun loadUrl(url: String, tab: Any? = Chrome.getTab()) { - runCatching { loadUrl.invoke(tab, newLoadUrlParams(url)) }.onFailure { Log.ex(it) } + if (!Chrome.checkTab(tab)) return + loadUrl.invoke(tab, newLoadUrlParams(url)) } fun newLoadUrlParams(url: String): Any {