Add option to use a secure connection for the daemon

This commit is contained in:
hensm
2022-09-02 09:19:23 +01:00
parent 8ecd3320f7
commit cde15cfd91
7 changed files with 131 additions and 41 deletions

View File

@@ -1,35 +1,31 @@
import http, { IncomingMessage } from "http";
import WebSocket from "ws";
import http from "http";
import https from "https";
import { spawn } from "child_process";
import { Readable } from "stream";
import WebSocket from "ws";
import { DecodeTransform, EncodeTransform } from "./transforms.js";
interface DaemonOpts {
export interface DaemonOpts {
host: string;
port: number;
password?: string;
secure?: boolean;
key?: Buffer;
cert?: Buffer;
}
export function init(opts: DaemonOpts) {
const server = http.createServer();
const server = !opts.secure
? http.createServer()
: https.createServer({
key: opts.key,
cert: opts.cert
});
const wss = new WebSocket.Server({ noServer: true });
process.stdout.write(
`Starting WebSocket server at ws://${
opts.host.includes(":") ? `[${opts.host}]` : opts.host
}:${opts.port}... `
);
server.on("listening", () => {
process.stdout.write("Done!\n");
});
server.on("error", err => {
console.error("Failed!");
console.error(err.message);
});
wss.on("connection", socket => {
// Stream for incoming WebSocket messages
const messageStream = new Readable({ objectMode: true });
@@ -72,7 +68,7 @@ export function init(opts: DaemonOpts) {
* Authenticates requests by checking password URL param against
* server password specified in launch options.
*/
function authenticate(req: IncomingMessage) {
function authenticate(req: http.IncomingMessage) {
if (!opts.password) return true;
const password = new URL(
@@ -121,5 +117,17 @@ export function init(opts: DaemonOpts) {
res.end();
});
server.listen({ port: opts.port, host: opts.host });
process.stdout.write(
`Starting WebSocket server at ${opts.secure ? "wss" : "ws"}://${
opts.host.includes(":") ? `[${opts.host}]` : opts.host
}:${opts.port}... `
);
server.listen({ port: opts.port, host: opts.host }, () => {
process.stdout.write("Done!\n");
});
server.on("error", err => {
console.error("Failed!");
console.error(err.message);
});
}