MagicMirror/js/server.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

/* Magic Mirror
* Server
*
2020-04-28 23:05:28 +02:00
* By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
*/
2016-04-05 14:35:11 -04:00
var express = require("express");
var app = require("express")();
var path = require("path");
2016-09-29 17:26:32 +02:00
var ipfilter = require("express-ipfilter").IpFilter;
var fs = require("fs");
var helmet = require("helmet");
2020-05-11 07:25:42 +02:00
var Log = require("./logger.js");
var Utils = require("./utils.js");
var Server = function (config, callback) {
var port = config.port;
if (process.env.MM_PORT) {
port = process.env.MM_PORT;
}
2020-02-01 20:46:26 +08:00
var server = null;
if (config.useHttps) {
2020-02-01 20:46:26 +08:00
var options = {
key: fs.readFileSync(config.httpsPrivateKey),
cert: fs.readFileSync(config.httpsCertificate)
2020-02-13 08:35:09 +01:00
};
2020-02-01 20:46:26 +08:00
server = require("https").Server(options, app);
} else {
2020-02-01 20:46:26 +08:00
server = require("http").Server(app);
}
var io = require("socket.io")(server);
2020-02-13 08:35:09 +01:00
2020-05-11 07:25:42 +02:00
Log.log("Starting server on port " + port + " ... ");
server.listen(port, config.address ? config.address : "localhost");
2016-09-29 17:07:22 +02:00
if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {
2020-05-11 07:25:42 +02:00
Log.info(Utils.colors.warn("You're using a full whitelist configuration to allow for all IPs"));
}
app.use(function (req, res, next) {
var result = ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) {
2016-09-29 17:07:22 +02:00
if (err === undefined) {
return next();
}
2020-05-11 07:25:42 +02:00
Log.log(err.message);
2016-09-29 17:07:22 +02:00
res.status(403).send("This device is not allowed to access your mirror. <br> Please check your config.js or config.js.sample to change this.");
});
});
app.use(helmet({ contentSecurityPolicy: false }));
2016-09-29 17:07:22 +02:00
2016-04-05 14:35:11 -04:00
app.use("/js", express.static(__dirname));
var directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations", "/tests/configs"];
var directory;
for (var i in directories) {
directory = directories[i];
app.use(directory, express.static(path.resolve(global.root_path + directory)));
}
app.get("/version", function (req, res) {
res.send(global.version);
});
app.get("/config", function (req, res) {
res.send(config);
});
app.get("/", function (req, res) {
var html = fs.readFileSync(path.resolve(global.root_path + "/index.html"), { encoding: "utf8" });
html = html.replace("#VERSION#", global.version);
2020-05-02 10:39:09 +02:00
var configFile = "config/config.js";
if (typeof global.configuration_file !== "undefined") {
2020-04-20 22:16:23 +02:00
configFile = global.configuration_file;
}
html = html.replace("#CONFIG_FILE#", configFile);
res.send(html);
});
2016-04-05 14:35:11 -04:00
if (typeof callback === "function") {
callback(app, io);
}
};
module.exports = Server;