From 78d8516287fff8ef633eaa49d82cf0a5bf7ed301 Mon Sep 17 00:00:00 2001 From: hensm Date: Wed, 26 Feb 2020 16:31:18 +0000 Subject: [PATCH] Add options to switch bridge backup WebSocket server port --- app/src/daemon/index.ts | 16 +++++++++++++- ext/src/_locales/en/messages.json | 9 ++++++-- ext/src/defaultOptions.ts | 1 + ext/src/lib/nativeMessaging.ts | 9 +++++--- ext/src/lib/options.ts | 1 + ext/src/ui/options/Bridge.tsx | 33 +++++++++++++++++------------ ext/src/ui/options/index.tsx | 9 +++++--- ext/src/ui/options/styles/index.css | 7 ++++++ 8 files changed, 62 insertions(+), 23 deletions(-) diff --git a/app/src/daemon/index.ts b/app/src/daemon/index.ts index d84ea0d..53a7479 100644 --- a/app/src/daemon/index.ts +++ b/app/src/daemon/index.ts @@ -3,13 +3,27 @@ import { spawn } from "child_process"; import { Readable } from "stream"; +import minimist from "minimist"; import WebSocket from "ws"; import { DecodeTransform , EncodeTransform } from "../transforms"; -const wss = new WebSocket.Server({ port: 9556 }); +const argv = minimist(process.argv.slice(2), { + string: [ "port" ] + , default: { + port: "9556" + } +}); + +const port = parseInt(argv.port); +if (!port || port < 1025 || port > 65535) { + console.error("Invalid port specified!"); + process.exit(1); +} + +const wss = new WebSocket.Server({ port }); wss.on("connection", socket => { // Stream for incoming WebSocket messages diff --git a/ext/src/_locales/en/messages.json b/ext/src/_locales/en/messages.json index f40a7d5..ce3f68b 100755 --- a/ext/src/_locales/en/messages.json +++ b/ext/src/_locales/en/messages.json @@ -202,8 +202,13 @@ } , "optionsBridgeBackupEnabled": { - "message": "Enable backup daemon connection" - , "description": "Backup daemon checkbox label." + "message": "Enable backup daemon connection on port $numberInput$" + , "description": "Backup daemon checkbox label. An HTML number input is inserted inline at the numberInput substitution." + , "placeholders": { + "numberInput": { + "content": "$1" + } + } } , "optionsBridgeBackupEnabledDescription": { "message": "If the regular bridge connection fails, attempt to connect to a bridge running in daemon mode." diff --git a/ext/src/defaultOptions.ts b/ext/src/defaultOptions.ts index 3ccdcce..504dc1e 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 + , bridgeBackupPort: 9556 , mediaEnabled: true , mediaOverlayEnabled: false , mediaSyncElement: false diff --git a/ext/src/lib/nativeMessaging.ts b/ext/src/lib/nativeMessaging.ts index 79e0118..d78d755 100644 --- a/ext/src/lib/nativeMessaging.ts +++ b/ext/src/lib/nativeMessaging.ts @@ -4,7 +4,7 @@ import logger from "./logger"; import options from "./options"; -const WEBSOCKET_DAEMON_URL = "ws://localhost:9556"; +const WEBSOCKET_DAEMON_URL_PREFIX = "ws://localhost:"; type DisconnectListener = (port: browser.runtime.Port) => void; @@ -114,7 +114,8 @@ function connectNative (application: string) { if (port.error && !isNativeHostStatusKnown) { isNativeHostStatusKnown = true; - socket = new WebSocket(WEBSOCKET_DAEMON_URL); + const port = await options.get("bridgeBackupPort"); + socket = new WebSocket(`${WEBSOCKET_DAEMON_URL_PREFIX}${port}`); socket.addEventListener("open", () => { // Send all messages in queue @@ -171,8 +172,10 @@ async function sendNativeMessage ( 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); + const ws = new WebSocket(`${WEBSOCKET_DAEMON_URL_PREFIX}${port}`); ws.addEventListener("open", () => { ws.send(JSON.stringify(message)); diff --git a/ext/src/lib/options.ts b/ext/src/lib/options.ts index af1cb39..c9e30ea 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; + bridgeBackupPort: number; mediaEnabled: boolean; mediaOverlayEnabled: boolean; mediaSyncElement: boolean; diff --git a/ext/src/ui/options/Bridge.tsx b/ext/src/ui/options/Bridge.tsx index 70ab8f5..81db77c 100644 --- a/ext/src/ui/options/Bridge.tsx +++ b/ext/src/ui/options/Bridge.tsx @@ -4,7 +4,7 @@ import React, { Component } from "react"; import semver from "semver"; -import options from "../../lib/options"; +import options, { Options } from "../../lib/options"; import { BridgeInfo } from "../../lib/bridge"; import { getNextEllipsis } from "../../lib/utils"; @@ -62,6 +62,8 @@ const BridgeStats = (props: BridgeStatsProps) => ( interface BridgeProps { info?: BridgeInfo; loading: boolean; + options?: Options; + onChange: (ev: React.ChangeEvent) => void; } interface BridgeState { @@ -69,7 +71,6 @@ interface BridgeState { isUpdateAvailable: boolean; wasErrorCheckingUpdates: boolean; checkUpdatesEllipsis: string; - bridgeBackupEnabled?: boolean; updateStatus?: string; } @@ -87,11 +88,6 @@ export default class Bridge extends Component { , checkUpdatesEllipsis: "..." }; - options.get("bridgeBackupEnabled") - .then(bridgeBackupEnabled => { - this.setState({ bridgeBackupEnabled }); - }); - this.onCheckUpdates = this.onCheckUpdates.bind(this); this.onCheckUpdatesResponse = this.onCheckUpdatesResponse.bind(this); this.onCheckUpdatesError = this.onCheckUpdatesError.bind(this); @@ -99,6 +95,9 @@ export default class Bridge extends Component { } public render () { + const [ backupMessageStart, backupMessageEnd ] + = _("optionsBridgeBackupEnabled", "\0").split("\0"); + return (
{ this.props.loading @@ -108,20 +107,26 @@ export default class Bridge extends Component {
) : this.renderStatus() } - { !this.props.loading && this.state.bridgeBackupEnabled !== undefined && + { !this.props.loading && this.props.options &&