2017-08-28 12:27:45 -04:00
|
|
|
/**
|
|
|
|
* @description Meshcentral web server
|
|
|
|
* @author Ylian Saint-Hilaire
|
2018-01-04 15:15:21 -05:00
|
|
|
* @copyright Intel Corporation 2018
|
|
|
|
* @license Apache-2.0
|
2018-08-29 20:40:30 -04:00
|
|
|
* @version v0.0.2
|
2017-08-28 12:27:45 -04:00
|
|
|
*/
|
|
|
|
|
2018-08-29 20:40:30 -04:00
|
|
|
/*jslint node: true */
|
|
|
|
/*jshint node: true */
|
|
|
|
/*jshint strict:false */
|
|
|
|
/*jshint -W097 */
|
|
|
|
/*jshint esversion: 6 */
|
|
|
|
"use strict";
|
2018-08-27 15:24:15 -04:00
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
// ExpressJS login sample
|
|
|
|
// https://github.com/expressjs/express/blob/master/examples/auth/index.js
|
|
|
|
|
|
|
|
// Construct a HTTP redirection web server object
|
2018-01-15 00:01:06 -05:00
|
|
|
module.exports.CreateRedirServer = function (parent, db, args, func) {
|
2017-08-28 12:27:45 -04:00
|
|
|
var obj = {};
|
|
|
|
obj.parent = parent;
|
|
|
|
obj.db = db;
|
|
|
|
obj.args = args;
|
2018-01-15 00:01:06 -05:00
|
|
|
obj.certificates = null;
|
2018-08-29 20:40:30 -04:00
|
|
|
obj.express = require("express");
|
|
|
|
obj.net = require("net");
|
2017-08-28 12:27:45 -04:00
|
|
|
obj.app = obj.express();
|
2018-08-29 20:40:30 -04:00
|
|
|
obj.tcpServer = null;
|
2018-01-15 00:01:06 -05:00
|
|
|
obj.port = null;
|
2018-08-29 20:40:30 -04:00
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
// Perform an HTTP to HTTPS redirection
|
|
|
|
function performRedirection(req, res) {
|
2018-01-15 00:01:06 -05:00
|
|
|
var host = req.headers.host;
|
|
|
|
if (obj.certificates != null) {
|
|
|
|
host = obj.certificates.CommonName;
|
2018-08-29 20:40:30 -04:00
|
|
|
if ((obj.certificates.CommonName == "sample.org") || (obj.certificates.CommonName == "un-configured")) { host = req.headers.host; }
|
2018-01-15 00:01:06 -05:00
|
|
|
}
|
2018-03-08 20:58:22 -05:00
|
|
|
var httpsPort = ((obj.args.aliasport == null) ? obj.args.port : obj.args.aliasport); // Use HTTPS alias port is specified
|
2018-08-29 20:40:30 -04:00
|
|
|
if (req.headers && req.headers.host && (req.headers.host.split(":")[0].toLowerCase() == "localhost")) { res.redirect("https://localhost:" + httpsPort + req.url); } else { res.redirect("https://" + host + ":" + httpsPort + req.url); }
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
2018-08-29 20:40:30 -04:00
|
|
|
|
|
|
|
/*
|
2017-08-28 12:27:45 -04:00
|
|
|
// Return the current domain of the request
|
|
|
|
function getDomain(req) {
|
2018-08-29 20:40:30 -04:00
|
|
|
var x = req.url.split("/");
|
|
|
|
if (x.length < 2) { return parent.config.domains[""]; }
|
|
|
|
if (parent.config.domains[x[1].toLowerCase()]) { return parent.config.domains[x[1].toLowerCase()]; }
|
|
|
|
return parent.config.domains[""];
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
2018-08-29 20:40:30 -04:00
|
|
|
*/
|
2017-08-28 12:27:45 -04:00
|
|
|
|
|
|
|
// Renter the terms of service.
|
2018-08-29 20:40:30 -04:00
|
|
|
obj.app.get("/MeshServerRootCert.cer", function (req, res) {
|
|
|
|
res.set({ "Cache-Control": "no-cache, no-store, must-revalidate", "Pragma": "no-cache", "Expires": "0", "Content-Type": "application/octet-stream", "Content-Disposition": "attachment; filename=" + obj.certificates.RootName + ".cer" });
|
2017-08-28 12:27:45 -04:00
|
|
|
var rootcert = obj.certificates.root.cert;
|
|
|
|
var i = rootcert.indexOf("-----BEGIN CERTIFICATE-----\r\n");
|
|
|
|
if (i >= 0) { rootcert = rootcert.substring(i + 29); }
|
|
|
|
i = rootcert.indexOf("-----END CERTIFICATE-----");
|
|
|
|
if (i >= 0) { rootcert = rootcert.substring(i, 0); }
|
2018-08-29 20:40:30 -04:00
|
|
|
res.send(new Buffer(rootcert, "base64"));
|
2017-08-28 12:27:45 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Add HTTP security headers to all responses
|
|
|
|
obj.app.use(function (req, res, next) {
|
|
|
|
res.removeHeader("X-Powered-By");
|
2018-08-29 20:40:30 -04:00
|
|
|
res.set({ "strict-transport-security": "max-age=60000; includeSubDomains", "Referrer-Policy": "no-referrer", "x-frame-options": "SAMEORIGIN", "X-XSS-Protection": "1; mode=block", "X-Content-Type-Options": "nosniff", "Content-Security-Policy": "default-src http: ws: \"self\" \"unsafe-inline\"" });
|
2017-08-28 12:27:45 -04:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2018-01-15 00:01:06 -05:00
|
|
|
// Once the main web server is started, call this to hookup additional handlers
|
|
|
|
obj.hookMainWebServer = function (certs) {
|
|
|
|
obj.certificates = certs;
|
2018-08-29 21:47:22 -04:00
|
|
|
for (var i in parent.config.domains) {
|
2018-01-15 00:01:06 -05:00
|
|
|
if (parent.config.domains[i].dns != null) { continue; }
|
|
|
|
var url = parent.config.domains[i].url;
|
2018-08-29 20:40:30 -04:00
|
|
|
obj.app.post(url + "amtevents.ashx", obj.parent.webserver.handleAmtEventRequest);
|
|
|
|
obj.app.get(url + "meshsettings", obj.parent.webserver.handleMeshSettingsRequest);
|
|
|
|
obj.app.get(url + "meshagents", obj.parent.webserver.handleMeshAgentRequest);
|
2018-01-15 00:01:06 -05:00
|
|
|
}
|
2018-08-29 20:40:30 -04:00
|
|
|
};
|
2018-01-15 00:01:06 -05:00
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
// Setup all HTTP redirection handlers
|
2018-08-29 20:40:30 -04:00
|
|
|
//obj.app.set("etag", false);
|
2018-08-29 21:47:22 -04:00
|
|
|
for (var i in parent.config.domains) {
|
2018-01-15 00:01:06 -05:00
|
|
|
if (parent.config.domains[i].dns != null) { continue; }
|
2017-08-28 12:27:45 -04:00
|
|
|
var url = parent.config.domains[i].url;
|
|
|
|
obj.app.get(url, performRedirection);
|
2018-08-29 20:40:30 -04:00
|
|
|
obj.app.use(url + "clickonce", obj.express.static(obj.parent.path.join(__dirname, "public/clickonce"))); // Indicates the clickonce folder is public
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
2018-08-29 20:40:30 -04:00
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
// Find a free port starting with the specified one and going up.
|
|
|
|
function CheckListenPort(port, func) {
|
|
|
|
var s = obj.net.createServer(function (socket) { });
|
2018-08-29 20:40:30 -04:00
|
|
|
obj.tcpServer = s.listen(port, function () { s.close(function () { if (func) { func(port); } }); }).on("error", function (err) {
|
|
|
|
if (args.exactports) { console.error("ERROR: MeshCentral HTTP web server port " + port + " not available."); process.exit(); }
|
2017-08-28 12:27:45 -04:00
|
|
|
else { if (port < 65535) { CheckListenPort(port + 1, func); } else { if (func) { func(0); } } }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the ExpressJS web server, if the port is busy try the next one.
|
|
|
|
function StartRedirServer(port) {
|
2018-08-29 20:40:30 -04:00
|
|
|
if (port == 0 || port == 65535) { return; }
|
2018-01-15 00:01:06 -05:00
|
|
|
obj.tcpServer = obj.app.listen(port, function () {
|
|
|
|
obj.port = port;
|
2018-08-29 20:40:30 -04:00
|
|
|
console.log("MeshCentral HTTP redirection web server running on port " + port + ".");
|
|
|
|
obj.parent.updateServerState("redirect-port", port);
|
2018-01-15 00:01:06 -05:00
|
|
|
func(obj.port);
|
2018-08-29 20:40:30 -04:00
|
|
|
}).on("error", function (err) {
|
|
|
|
if ((err.code == "EACCES") && (port < 65535)) { StartRedirServer(port + 1); } else { console.log(err); func(obj.port); }
|
2018-01-15 00:01:06 -05:00
|
|
|
});
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
2018-08-29 20:40:30 -04:00
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
CheckListenPort(args.redirport, StartRedirServer);
|
|
|
|
|
|
|
|
return obj;
|
2018-08-29 20:40:30 -04:00
|
|
|
};
|