MagicMirror/js/server.js

113 lines
3.0 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.
*/
2021-01-05 18:37:16 +01:00
const express = require("express");
const app = require("express")();
const path = require("path");
const ipfilter = require("express-ipfilter").IpFilter;
const fs = require("fs");
const helmet = require("helmet");
2021-02-18 19:14:53 +01:00
const Log = require("logger");
2021-01-05 18:37:16 +01:00
const Utils = require("./utils.js");
2021-01-06 13:17:39 +01:00
/**
* Server
*
* @param {object} config The MM config
* @param {Function} callback Function called when done.
* @class
*/
2021-01-05 18:37:16 +01:00
function Server(config, callback) {
const port = process.env.MM_PORT || config.port;
2021-09-24 00:30:00 +02:00
const serverSockets = new Set();
2021-01-05 18:37:16 +01:00
let server = null;
if (config.useHttps) {
2021-01-05 18:37:16 +01:00
const options = {
2020-02-01 20:46:26 +08:00
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);
}
const io = require("socket.io")(server, {
cors: {
origin: /.*$/,
credentials: true
},
allowEIO3: true
});
2020-02-13 08:35:09 +01:00
2021-09-24 00:30:00 +02:00
server.on("connection", (socket) => {
serverSockets.add(socket);
socket.on("close", () => {
serverSockets.delete(socket);
});
});
2021-01-05 18:37:16 +01:00
Log.log(`Starting server on port ${port} ... `);
2021-01-05 18:37:16 +01:00
server.listen(port, config.address || "localhost");
2016-09-29 17:07:22 +02:00
if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {
2020-06-20 12:07:04 +02:00
Log.warn(Utils.colors.warn("You're using a full whitelist configuration to allow for all IPs"));
}
app.use(function (req, res, next) {
2021-01-05 18:37:16 +01:00
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.");
});
});
2022-01-14 00:05:08 +01:00
app.use(helmet({ contentSecurityPolicy: false, crossOriginOpenerPolicy: false, crossOriginEmbedderPolicy: false, crossOriginResourcePolicy: false, originAgentCluster: false }));
2016-09-29 17:07:22 +02:00
2016-04-05 14:35:11 -04:00
app.use("/js", express.static(__dirname));
2021-01-05 18:37:16 +01:00
const directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations", "/tests/configs"];
for (const directory of directories) {
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) {
2021-01-05 18:37:16 +01:00
let html = fs.readFileSync(path.resolve(`${global.root_path}/index.html`), { encoding: "utf8" });
html = html.replace("#VERSION#", global.version);
2021-01-05 18:37:16 +01:00
let 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);
}
2021-09-15 21:09:31 +02:00
this.close = function () {
2021-09-24 00:30:00 +02:00
for (const socket of serverSockets.values()) {
socket.destroy();
}
2021-09-15 21:09:31 +02:00
server.close();
2021-09-16 22:36:18 +02:00
};
2021-01-05 18:37:16 +01:00
}
module.exports = Server;