Handle SSL in setup database temp server

dev
Louis Lam 1 month ago
parent 62bfc38c27
commit cfe342a4c9

@ -1745,7 +1745,8 @@ let needSetup = false;
await server.start();
server.httpServer.listen(port, hostname, async () => {
printServerUrls("server", port, hostname);
printServerUrls("server", port, hostname, config.isSSL);
await startMonitors();
// Put this here. Start background jobs after the db and server is ready to prevent clear up during db migration.

@ -6,6 +6,8 @@ const path = require("path");
const Database = require("./database");
const { allowDevAllOrigin, printServerUrls } = require("./util-server");
const mysql = require("mysql2/promise");
const { isSSL, sslKey, sslCert, sslKeyPassphrase } = require("./config");
const https = require("https");
/**
* Reads a configuration value from an environment variable or a Docker secrets file.
@ -306,9 +308,24 @@ class SetupDatabase {
response.end();
});
tempServer = app.listen(port, hostname, () => {
let server;
if (isSSL) {
server = tempServer = https.createServer(
{
key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert),
passphrase: sslKeyPassphrase,
},
app
);
} else {
server = app;
}
tempServer = server.listen(port, hostname, () => {
log.info("setup-database", "Starting Setup Database");
printServerUrls("setup-database", port, hostname);
printServerUrls("setup-database", port, hostname, isSSL);
log.info("setup-database", "Waiting for user action...");
});
});

@ -19,6 +19,7 @@ const RadiusClient = require("./radius-client");
const oidc = require("openid-client");
const tls = require("tls");
const { exists } = require("fs");
const { networkInterfaces } = require("os");
const {
dictionaries: {
@ -993,26 +994,66 @@ module.exports.commandExists = commandExists;
* @param {string} tag Log tag (e.g. "server", "setup-database")
* @param {number} port Port number
* @param {string} hostname Bound hostname, if any
* @param {boolean} isHTTPS Whether the server is using HTTPS
* @returns {void}
*/
module.exports.printServerUrls = (tag, port, hostname) => {
if (hostname) {
log.info(tag, `Listening on http://${hostname}:${port}`);
return;
}
module.exports.printServerUrls = (tag, port, hostname, isHTTPS = false) => {
try {
// If hostname is specified, just print that one.
if (hostname) {
log.info(tag, `Listening on: `, createURL(isHTTPS, hostname, port));
return;
}
// Since no hostname is specified, which means the server is bound to all interfaces, we need to print all possible URLs.
const nets = networkInterfaces();
const { networkInterfaces } = require("os");
const nets = networkInterfaces();
log.info(tag, "Listening on:");
log.info(tag, `- `, createURL(isHTTPS, "localhost", port));
// Prepare a list of valid address
const addressList = [];
for (const iface of Object.values(nets)) {
for (const addr of iface) {
if (!addr.internal) {
addressList.push(addr);
}
}
}
log.info(tag, "Listening on:");
log.info(tag, ` Local: http://localhost:${port}`);
// Sort IPv4 addresses first
addressList.sort((a, b) => {
if (a.family === "IPv4" && b.family === "IPv6") {
return -1;
} else if (a.family === "IPv6" && b.family === "IPv4") {
return 1;
} else {
return a.address.localeCompare(b.address);
}
});
for (const iface of Object.values(nets)) {
for (const addr of iface) {
if (!addr.internal) {
const host = addr.family === "IPv6" ? `[${addr.address}]` : addr.address;
log.info(tag, ` Network: http://${host}:${port}`);
for (const address of addressList) {
if (!address.internal) {
const host = address.family === "IPv6" ? `[${address.address}]` : address.address;
log.info(tag, `- `, createURL(isHTTPS, host, port));
}
}
} catch (e) {
log.error(tag, "Error printing server URLs: " + e.message);
}
};
/**
* Construct a URL a bit more safely
* @param {boolean} isHTTPS Whether the URL should use HTTPS protocol
* @param {string} hostname The hostname to use in the URL
* @param {number} [port=80] The port
* @returns {string} The constructed URL as a string
*/
function createURL(isHTTPS, hostname, port = 80) {
const url = new URL((isHTTPS ? "https" : "http") + `://` + hostname);
url.port = String(port);
// Prefer origin if available, it doesn't contain the tailing slash
return url.origin || url.toString();
}

Loading…
Cancel
Save