diff --git a/agents/meshinstall-linux.sh b/agents/meshinstall-linux.sh index 932855f1..4b19440a 100644 --- a/agents/meshinstall-linux.sh +++ b/agents/meshinstall-linux.sh @@ -9,6 +9,15 @@ CheckStartupType() { return 0; } +# Add "StartupType=(type)" to .msh file +UpdateMshFile() { + # Remove all lines that start with "StartupType=" + sed '/^StartupType=/ d' < /usr/local/mesh/meshagent.msh >> /usr/local/mesh/meshagent2.msh + # Add the startup type to the file + echo "StartupType=$starttype" >> /usr/local/mesh/meshagent2.msh + mv /usr/local/mesh/meshagent2.msh /usr/local/mesh/meshagent.msh +} + CheckInstallAgent() { # echo "Checking mesh identifier..." if [ -e "/usr/local" ] @@ -70,11 +79,12 @@ DownloadAgent() { if [ $? -eq 0 ] then echo "Mesh agent download." -# TODO: Check meshagent sha256 hash +# TODO: We could check the meshagent sha256 hash, but best to authenticate the server. chmod 755 /usr/local/mesh/meshagent wget $url/meshsettings?id=$meshid -q --no-check-certificate -O /usr/local/mesh/meshagent.msh if [ $? -eq 0 ] then + UpdateMshFile if [ $starttype -eq 1 ] then echo -e "[Unit]\nDescription=MeshCentral Agent\n[Service]\nExecStart=/usr/local/mesh/meshagent\nStandardOutput=null\n[Install]\nWantedBy=multi-user.target\nAlias=meshcentral.service\n" > /lib/systemd/system/meshcentral.service diff --git a/meshcentral.js b/meshcentral.js index bf5aebf9..53b20ce8 100644 --- a/meshcentral.js +++ b/meshcentral.js @@ -118,16 +118,21 @@ function CreateMeshCentralServer() { obj.launchChildServer = function (startLine) { var child_process = require('child_process'); var xprocess = child_process.exec(startLine + ' --launch', function (error, stdout, stderr) { - if (xprocess.xrestart == true) { - setTimeout(function () { obj.launchChildServer(startLine); }, 500); // If exit with restart requested, restart the server. + console.log(xprocess.xrestart); + if (xprocess.xrestart == 1) { + setTimeout(function () { obj.launchChildServer(startLine); }, 500); // This is an expected restart. + } else if (xprocess.xrestart == 2) { + console.log('Expected exit...'); + process.exit(); // User CTRL-C exit. } else { if (error != null) { + // This is an un-expected restart console.log('ERROR: MeshCentral failed with critical error, restarting...'); setTimeout(function () { obj.launchChildServer(startLine); }, 1000); } } }); - xprocess.stdout.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } if (data.indexOf('Updating settings folder...') >= 0) { xprocess.xrestart = true; } console.log(data); }); + xprocess.stdout.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } if (data.indexOf('Updating settings folder...') >= 0) { xprocess.xrestart = 1; } else if (data.indexOf('Server Ctrl-C exit...') >= 0) { xprocess.xrestart = 2; } console.log(data); }); xprocess.stderr.on('data', function (data) { if (data[data.length - 1] == '\n') { data = data.substring(0, data.length - 1); } obj.fs.appendFileSync('mesherrors.txt', '-------- ' + new Date().toLocaleString() + ' --------\r\n\r\n' + data + '\r\n\r\n\r\n'); }); xprocess.on('close', function (code) { if ((code != 0) && (code != 123)) { /* console.log("Exited with code " + code); */ } }); } @@ -650,7 +655,7 @@ function InstallModule(modulename, func, tag1, tag2) { } // Detect CTRL-C on Linux and stop nicely -process.on('SIGINT', function () { if (meshserver != null) { meshserver.Stop(); meshserver = null; } }); +process.on('SIGINT', function () { if (meshserver != null) { meshserver.Stop(); meshserver = null; } console.log('Server Ctrl-C exit...'); process.exit(); }); // Build the list of required modules var modules = ['nedb', 'https', 'unzip', 'xmldom', 'express', 'mongojs', 'archiver', 'minimist', 'multiparty', 'node-forge', 'express-ws', 'compression', 'body-parser', 'connect-redis', 'express-session', 'express-handlebars']; diff --git a/redirserver.js b/redirserver.js index 1390b45e..5d5f7975 100644 --- a/redirserver.js +++ b/redirserver.js @@ -22,7 +22,7 @@ module.exports.CreateRedirServer = function (parent, db, args, certificates) { // Perform an HTTP to HTTPS redirection function performRedirection(req, res) { var host = certificates.CommonName; - if (certificates.CommonName == 'sample.org') { host = req.headers.host; } + if ((certificates.CommonName == 'sample.org') || (certificates.CommonName == 'un-configured')) { host = req.headers.host; } if (req.headers && req.headers.host && (req.headers.host.split(':')[0].toLowerCase() == 'localhost')) { res.redirect('https://localhost:' + args.port + req.url); } else { res.redirect('https://' + host + ':' + args.port + req.url); } }