Refresh https settings

This commit is contained in:
bartbutenaers 2020-05-02 07:52:20 +02:00 committed by GitHub
parent efb9dce92f
commit 00d41c6de2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 1 deletions

View File

@ -143,7 +143,46 @@ if (process.env.NODE_RED_ENABLE_PROJECTS) {
}
if (settings.https) {
server = https.createServer(settings.https,function(req,res) {app(req,res);});
var startupHttps = settings.https;
if (typeof startupHttps === "function") {
// Get the result of the function, because createServer doesn't accept functions as input
startupHttps = startupHttps();
}
server = https.createServer(startupHttps,function(req,res) {app(req,res);});
// Refresh https settings at intervals for NodeJs version 11 and above
if (settings.httpsRefreshInterval) {
if (typeof startupHttps === "function") {
if (server.setSecureContext) {
console.log("Refreshing https settings every " + parseInt(settings.credentialRenewalTime) + " seconds.");
setInterval(function () {
try {
// Get the result of the function, because createServer doesn't accept functions as input
var refreshedHttps = settings.https();
if (!refreshedHttps.key || !refreshedHttps.cert) {
console.log("Cannot refresh the https settings when the https property function doesn't return a 'key' and 'cert'.");
return;
}
// Only update the credentials in the server when key or cert has changed
if(!server.key || !server.cert || !server.key.equals(refreshedHttps.key) || !server.cert.equals(refreshedHttps.cert)) {
server.setSecureContext(refreshedHttps);
console.log("The https settings have been refreshed.");
}
} catch(err) {
console.log("Failed to refresh the https settings: " + err);
}
}, parseInt(settings.credentialRenewalTime) * 1000);
} else {
console.log("Cannot refresh the https settings automatically, because NodeJs version 11 or above is required.");
}
} else {
console.log("Cannot refresh the https settings automatically (at httpsRefreshInterval), because the https property needs to be a function.");
}
}
} else {
server = http.createServer(function(req,res) {app(req,res);});
}