From ff0847441bd1ac58bd1c9a5f167b32d42d9ed3d7 Mon Sep 17 00:00:00 2001 From: hensm Date: Thu, 26 Mar 2020 20:12:23 +0000 Subject: [PATCH] Add daemon host extension option --- ext/src/_locales/en/messages.json | 6 +++--- ext/src/defaultOptions.ts | 1 + ext/src/lib/nativeMessaging.ts | 21 ++++++++++++++------- ext/src/lib/options.ts | 1 + ext/src/ui/options/Bridge.tsx | 7 +++++++ ext/src/ui/options/styles/index.css | 10 +++++++++- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/ext/src/_locales/en/messages.json b/ext/src/_locales/en/messages.json index ce3f68b..732330d 100755 --- a/ext/src/_locales/en/messages.json +++ b/ext/src/_locales/en/messages.json @@ -202,10 +202,10 @@ } , "optionsBridgeBackupEnabled": { - "message": "Enable backup daemon connection on port $numberInput$" - , "description": "Backup daemon checkbox label. An HTML number input is inserted inline at the numberInput substitution." + "message": "Enable backup daemon connection on $hostPort$" + , "description": "Backup daemon checkbox label. Host/port inputs are inserted inline at the hostPort substitution." , "placeholders": { - "numberInput": { + "hostPort": { "content": "$1" } } diff --git a/ext/src/defaultOptions.ts b/ext/src/defaultOptions.ts index 504dc1e..be96ba2 100644 --- a/ext/src/defaultOptions.ts +++ b/ext/src/defaultOptions.ts @@ -7,6 +7,7 @@ import { Options } from "./lib/options"; export default { bridgeApplicationName: APPLICATION_NAME , bridgeBackupEnabled: false + , bridgeBackupHost: "localhost" , bridgeBackupPort: 9556 , mediaEnabled: true , mediaOverlayEnabled: false diff --git a/ext/src/lib/nativeMessaging.ts b/ext/src/lib/nativeMessaging.ts index d78d755..4a4c094 100644 --- a/ext/src/lib/nativeMessaging.ts +++ b/ext/src/lib/nativeMessaging.ts @@ -4,8 +4,6 @@ import logger from "./logger"; import options from "./options"; -const WEBSOCKET_DAEMON_URL_PREFIX = "ws://localhost:"; - type DisconnectListener = (port: browser.runtime.Port) => void; type MessageListener = (message: any) => void; @@ -99,7 +97,11 @@ function connectNative (application: string) { port.onDisconnect.addListener(async () => { - if (!(await options.get("bridgeBackupEnabled"))) { + const { bridgeBackupEnabled + , bridgeBackupHost + , bridgeBackupPort } = await options.getAll(); + + if (!bridgeBackupEnabled) { portObject.error = { message: "" }; @@ -114,8 +116,8 @@ function connectNative (application: string) { if (port.error && !isNativeHostStatusKnown) { isNativeHostStatusKnown = true; - const port = await options.get("bridgeBackupPort"); - socket = new WebSocket(`${WEBSOCKET_DAEMON_URL_PREFIX}${port}`); + socket = new WebSocket( + `ws://${bridgeBackupHost}:${bridgeBackupPort}`); socket.addEventListener("open", () => { // Send all messages in queue @@ -168,14 +170,19 @@ async function sendNativeMessage ( try { return await browser.runtime.sendNativeMessage(application, message); } catch { - if (!(await options.get("bridgeBackupEnabled"))) { + const { bridgeBackupEnabled + , bridgeBackupHost + , bridgeBackupPort } = await options.getAll(); + + if (!bridgeBackupEnabled) { throw logger.error("Bridge connection failed and backup not enabled."); } const port = await options.get("bridgeBackupPort"); return await new Promise((resolve, reject) => { - const ws = new WebSocket(`${WEBSOCKET_DAEMON_URL_PREFIX}${port}`); + const ws = new WebSocket( + `ws://${bridgeBackupHost}:${bridgeBackupPort}`); ws.addEventListener("open", () => { ws.send(JSON.stringify(message)); diff --git a/ext/src/lib/options.ts b/ext/src/lib/options.ts index c9e30ea..31f20ca 100644 --- a/ext/src/lib/options.ts +++ b/ext/src/lib/options.ts @@ -16,6 +16,7 @@ const storageArea = new TypedStorageArea<{ export interface Options { bridgeApplicationName: string; bridgeBackupEnabled: boolean; + bridgeBackupHost: string; bridgeBackupPort: number; mediaEnabled: boolean; mediaOverlayEnabled: boolean; diff --git a/ext/src/ui/options/Bridge.tsx b/ext/src/ui/options/Bridge.tsx index 6484eee..badaabc 100644 --- a/ext/src/ui/options/Bridge.tsx +++ b/ext/src/ui/options/Bridge.tsx @@ -118,6 +118,13 @@ export default class Bridge extends Component {
{ backupMessageStart } + + :