Add daemon host option and default to localhost instead of all addresses

This commit is contained in:
hensm
2022-08-16 00:16:17 +01:00
parent cb6e9226de
commit 170b80283d
3 changed files with 41 additions and 17 deletions

View File

@@ -8,7 +8,13 @@ import { Readable } from "stream";
import { DecodeTransform, EncodeTransform } from "./transforms"; import { DecodeTransform, EncodeTransform } from "./transforms";
export function init(port: number, serverPassword?: string) { interface DaemonOpts {
host: string;
port: number;
password: string;
}
export function init(opts: DaemonOpts) {
const server = http.createServer(); const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true }); const wss = new WebSocket.Server({ noServer: true });
@@ -65,14 +71,14 @@ export function init(port: number, serverPassword?: string) {
* server password specified in launch options. * server password specified in launch options.
*/ */
function authenticate(req: IncomingMessage) { function authenticate(req: IncomingMessage) {
if (!serverPassword) return true; if (!opts.password) return true;
const password = new URL( const password = new URL(
req.url!, req.url!,
`http://${req.headers.host}` `http://${req.headers.host}`
).searchParams.get("password"); ).searchParams.get("password");
return password === serverPassword; return password === opts.password;
} }
server.on("upgrade", (req, socket, head) => { server.on("upgrade", (req, socket, head) => {
@@ -113,5 +119,5 @@ export function init(port: number, serverPassword?: string) {
res.end(); res.end();
}); });
server.listen(port); server.listen({ port: opts.port, host: opts.host });
} }

View File

@@ -6,16 +6,19 @@ import { __applicationVersion } from "../package.json";
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
boolean: ["daemon", "help", "version"], boolean: ["daemon", "help", "version"],
string: ["__name", "port", "password"], string: ["__name", "host", "port", "password"],
alias: { alias: {
d: "daemon", d: "daemon",
h: "help", h: "help",
v: "version", v: "version",
p: "port" n: "host",
p: "port",
P: "password"
}, },
default: { default: {
__name: path.basename(process.argv[0]), __name: path.basename(process.argv[0]),
daemon: false, daemon: false,
host: "localhost",
port: "9556" port: "9556"
} }
}); });
@@ -34,10 +37,12 @@ Options:
-d, --daemon Launch in daemon mode. This starts a WebSocket server that -d, --daemon Launch in daemon mode. This starts a WebSocket server that
the extension can be configured to connect to under bridge the extension can be configured to connect to under bridge
options. options.
-p, --port Set port number for WebSocket server. This must match the -n, --host Host for daemon WebSocket server. This must match the host
set in the extension options.
-p, --port Port number for daemon WebSocket server. This must match the
port set in the extension options. port set in the extension options.
--password Set an optional password for the WebSocket server. This must -P, --password Set an optional password for the daemon WebSocket server.
match the password set in the extension options. This must match the password set in the extension options.
WARNING: This password is intended only as a basic access WARNING: This password is intended only as a basic access
control measure and is transmitted in plain text even over control measure and is transmitted in plain text even over
remote connections! remote connections!
@@ -51,7 +56,11 @@ Options:
} }
import("./daemon").then(daemon => { import("./daemon").then(daemon => {
daemon.init(port, argv.password); daemon.init({
host: argv.host,
port,
password: argv.password
});
}); });
} else { } else {
import("./bridge"); import("./bridge");

View File

@@ -15,7 +15,16 @@ async function getBackupServerUrl() {
const { bridgeBackupHost, bridgeBackupPort, bridgeBackupPassword } = const { bridgeBackupHost, bridgeBackupPort, bridgeBackupPassword } =
await options.getAll(); await options.getAll();
const url = new URL(`ws://${bridgeBackupHost}:${bridgeBackupPort}`); const url = new URL(
`ws://${
// Handle IPv6 address formatting
bridgeBackupHost.includes(":")
? `[${bridgeBackupHost}]`
: bridgeBackupHost
}`
);
url.port = bridgeBackupPort.toString();
if (bridgeBackupPassword) { if (bridgeBackupPassword) {
url.searchParams.append("password", bridgeBackupPassword); url.searchParams.append("password", bridgeBackupPassword);
} }