1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Asynchronous https support

This commit is contained in:
bartbutenaers 2020-05-11 23:33:54 +02:00 committed by GitHub
parent 15f97bbf26
commit bfa5f39b6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -142,6 +142,7 @@ if (process.env.NODE_RED_ENABLE_PROJECTS) {
settings.editorTheme.projects.enabled = !/^false$/i.test(process.env.NODE_RED_ENABLE_PROJECTS); settings.editorTheme.projects.enabled = !/^false$/i.test(process.env.NODE_RED_ENABLE_PROJECTS);
} }
var httpsPromise;
if (settings.https) { if (settings.https) {
var startupHttps = settings.https; var startupHttps = settings.https;
@ -150,11 +151,26 @@ if (settings.https) {
startupHttps = startupHttps(); startupHttps = startupHttps();
} }
if (startupHttps.hasOwnProperty('then') && typeof startupHttps.then === 'function') {
// A promise was returned
httpsPromise = startupHttps;
} else {
// An object was returned - wrap in a promise
httpsPromise = Promise.resolve(startupHttps);
}
}
else {
// No https is enable - wrap null
httpsPromise = Promise.resolve(null);
}
httpsPromise.then(function(startupHttps) {
if (startupHttps) {
server = https.createServer(startupHttps,function(req,res) {app(req,res);}); server = https.createServer(startupHttps,function(req,res) {app(req,res);});
// Refresh https settings at intervals for NodeJs version 11 and above // Refresh https settings at intervals for NodeJs version 11 and above
if (settings.httpsRefreshInterval) { if (settings.httpsRefreshInterval) {
if (typeof startupHttps === "function") { if (typeof settings.https === "function") {
if (server.setSecureContext) { if (server.setSecureContext) {
console.log("Refreshing https settings every " + parseInt(settings.httpsRefreshInterval) + " seconds."); console.log("Refreshing https settings every " + parseInt(settings.httpsRefreshInterval) + " seconds.");
setInterval(function () { setInterval(function () {
@ -162,6 +178,17 @@ if (settings.https) {
// Get the result of the function, because createServer doesn't accept functions as input // Get the result of the function, because createServer doesn't accept functions as input
var refreshedHttps = settings.https(); var refreshedHttps = settings.https();
var httpsPromise;
if (refreshedHttps.hasOwnProperty('then') && typeof refreshedHttps.then === 'function') {
// A promise was returned
httpsPromise = refreshedHttps;
} else {
// An object was returned - wrap in a promise
httpsPromise = Promise.resolve(refreshedHttps);
}
httpsPromise.then(function(refreshedHttps) {
// Use the refreshed https settings
if (!refreshedHttps.key || !refreshedHttps.cert) { if (!refreshedHttps.key || !refreshedHttps.cert) {
console.log("Cannot refresh the https settings when the https property function doesn't return a 'key' and 'cert'."); console.log("Cannot refresh the https settings when the https property function doesn't return a 'key' and 'cert'.");
return; return;
@ -172,8 +199,11 @@ if (settings.https) {
server.setSecureContext(refreshedHttps); server.setSecureContext(refreshedHttps);
console.log("The https settings have been refreshed."); console.log("The https settings have been refreshed.");
} }
}).catch(function(err) {
console.log("Failed to apply the refreshed https settings: " + err);
});
} catch(err) { } catch(err) {
console.log("Failed to refresh the https settings: " + err); console.log("Failed to get the refreshed https settings: " + err);
} }
}, parseInt(settings.httpsRefreshInterval)*1000); }, parseInt(settings.httpsRefreshInterval)*1000);
} else { } else {
@ -183,12 +213,12 @@ if (settings.https) {
console.log("Cannot refresh the https settings automatically (at httpsRefreshInterval), because the https property needs to be a function."); console.log("Cannot refresh the https settings automatically (at httpsRefreshInterval), because the https property needs to be a function.");
} }
} }
} else { } else {
server = http.createServer(function(req,res) {app(req,res);}); server = http.createServer(function(req,res) {app(req,res);});
} }
server.setMaxListeners(0); server.setMaxListeners(0);
function formatRoot(root) { function formatRoot(root) {
if (root[0] != "/") { if (root[0] != "/") {
root = "/" + root; root = "/" + root;
} }
@ -196,50 +226,50 @@ function formatRoot(root) {
root = root + "/"; root = root + "/";
} }
return root; return root;
} }
if (settings.httpRoot === false) { if (settings.httpRoot === false) {
settings.httpAdminRoot = false; settings.httpAdminRoot = false;
settings.httpNodeRoot = false; settings.httpNodeRoot = false;
} else { } else {
settings.httpRoot = settings.httpRoot||"/"; settings.httpRoot = settings.httpRoot||"/";
settings.disableEditor = settings.disableEditor||false; settings.disableEditor = settings.disableEditor||false;
} }
if (settings.httpAdminRoot !== false) { if (settings.httpAdminRoot !== false) {
settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/"); settings.httpAdminRoot = formatRoot(settings.httpAdminRoot || settings.httpRoot || "/");
settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth; settings.httpAdminAuth = settings.httpAdminAuth || settings.httpAuth;
} else { } else {
settings.disableEditor = true; settings.disableEditor = true;
} }
if (settings.httpNodeRoot !== false) { if (settings.httpNodeRoot !== false) {
settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/"); settings.httpNodeRoot = formatRoot(settings.httpNodeRoot || settings.httpRoot || "/");
settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth; settings.httpNodeAuth = settings.httpNodeAuth || settings.httpAuth;
} }
// if we got a port from command line, use it (even if 0) // if we got a port from command line, use it (even if 0)
// replicate (settings.uiPort = parsedArgs.port||settings.uiPort||1880;) but allow zero // replicate (settings.uiPort = parsedArgs.port||settings.uiPort||1880;) but allow zero
if (parsedArgs.port !== undefined){ if (parsedArgs.port !== undefined){
settings.uiPort = parsedArgs.port; settings.uiPort = parsedArgs.port;
} else { } else {
if (settings.uiPort === undefined){ if (settings.uiPort === undefined){
settings.uiPort = 1880; settings.uiPort = 1880;
} }
} }
settings.uiHost = settings.uiHost||"0.0.0.0"; settings.uiHost = settings.uiHost||"0.0.0.0";
if (flowFile) { if (flowFile) {
settings.flowFile = flowFile; settings.flowFile = flowFile;
} }
if (parsedArgs.userDir) { if (parsedArgs.userDir) {
settings.userDir = parsedArgs.userDir; settings.userDir = parsedArgs.userDir;
} }
try { try {
RED.init(server,settings); RED.init(server,settings);
} catch(err) { } catch(err) {
if (err.code == "unsupported_version") { if (err.code == "unsupported_version") {
console.log("Unsupported version of Node.js:",process.version); console.log("Unsupported version of Node.js:",process.version);
console.log("Node-RED requires Node.js v8.9.0 or later"); console.log("Node-RED requires Node.js v8.9.0 or later");
@ -252,9 +282,9 @@ try {
} }
} }
process.exit(1); process.exit(1);
} }
function basicAuthMiddleware(user,pass) { function basicAuthMiddleware(user,pass) {
var basicAuth = require('basic-auth'); var basicAuth = require('basic-auth');
var checkPassword; var checkPassword;
var localCachedPassword; var localCachedPassword;
@ -294,31 +324,31 @@ function basicAuthMiddleware(user,pass) {
} }
next(); next();
} }
} }
if (settings.httpAdminRoot !== false && settings.httpAdminAuth) { if (settings.httpAdminRoot !== false && settings.httpAdminAuth) {
RED.log.warn(RED.log._("server.httpadminauth-deprecated")); RED.log.warn(RED.log._("server.httpadminauth-deprecated"));
app.use(settings.httpAdminRoot, basicAuthMiddleware(settings.httpAdminAuth.user,settings.httpAdminAuth.pass)); app.use(settings.httpAdminRoot, basicAuthMiddleware(settings.httpAdminAuth.user,settings.httpAdminAuth.pass));
} }
if (settings.httpAdminRoot !== false) { if (settings.httpAdminRoot !== false) {
app.use(settings.httpAdminRoot,RED.httpAdmin); app.use(settings.httpAdminRoot,RED.httpAdmin);
} }
if (settings.httpNodeRoot !== false && settings.httpNodeAuth) { if (settings.httpNodeRoot !== false && settings.httpNodeAuth) {
app.use(settings.httpNodeRoot,basicAuthMiddleware(settings.httpNodeAuth.user,settings.httpNodeAuth.pass)); app.use(settings.httpNodeRoot,basicAuthMiddleware(settings.httpNodeAuth.user,settings.httpNodeAuth.pass));
} }
if (settings.httpNodeRoot !== false) { if (settings.httpNodeRoot !== false) {
app.use(settings.httpNodeRoot,RED.httpNode); app.use(settings.httpNodeRoot,RED.httpNode);
} }
if (settings.httpStatic) { if (settings.httpStatic) {
settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth; settings.httpStaticAuth = settings.httpStaticAuth || settings.httpAuth;
if (settings.httpStaticAuth) { if (settings.httpStaticAuth) {
app.use("/",basicAuthMiddleware(settings.httpStaticAuth.user,settings.httpStaticAuth.pass)); app.use("/",basicAuthMiddleware(settings.httpStaticAuth.user,settings.httpStaticAuth.pass));
} }
app.use("/",express.static(settings.httpStatic)); app.use("/",express.static(settings.httpStatic));
} }
function getListenPath() { function getListenPath() {
var port = settings.serverPort; var port = settings.serverPort;
if (port === undefined){ if (port === undefined){
port = settings.uiPort; port = settings.uiPort;
@ -333,9 +363,9 @@ function getListenPath() {
listenPath += "/"; listenPath += "/";
} }
return listenPath; return listenPath;
} }
RED.start().then(function() { RED.start().then(function() {
if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) { if (settings.httpAdminRoot !== false || settings.httpNodeRoot !== false || settings.httpStatic) {
server.on('error', function(err) { server.on('error', function(err) {
if (err.errno === "EADDRINUSE") { if (err.errno === "EADDRINUSE") {
@ -362,16 +392,16 @@ RED.start().then(function() {
} else { } else {
RED.log.info(RED.log._("server.headless-mode")); RED.log.info(RED.log._("server.headless-mode"));
} }
}).otherwise(function(err) { }).otherwise(function(err) {
RED.log.error(RED.log._("server.failed-to-start")); RED.log.error(RED.log._("server.failed-to-start"));
if (err.stack) { if (err.stack) {
RED.log.error(err.stack); RED.log.error(err.stack);
} else { } else {
RED.log.error(err); RED.log.error(err);
} }
}); });
process.on('uncaughtException',function(err) { process.on('uncaughtException',function(err) {
util.log('[red] Uncaught Exception:'); util.log('[red] Uncaught Exception:');
if (err.stack) { if (err.stack) {
util.log(err.stack); util.log(err.stack);
@ -379,10 +409,11 @@ process.on('uncaughtException',function(err) {
util.log(err); util.log(err);
} }
process.exit(1); process.exit(1);
}); });
process.on('SIGINT', function () { process.on('SIGINT', function () {
RED.stop().then(function() { RED.stop().then(function() {
process.exit(); process.exit();
}); });
});
}); });