Files
MagicMirror/js/server.js
T

120 lines
3.4 KiB
JavaScript
Raw Normal View History

const fs = require("node:fs");
const http = require("node:http");
const https = require("node:https");
const path = require("node:path");
2023-04-04 20:44:32 +02:00
const express = require("express");
2021-01-05 18:37:16 +01:00
const ipfilter = require("express-ipfilter").IpFilter;
const helmet = require("helmet");
2023-04-04 20:44:32 +02:00
const socketio = require("socket.io");
2021-01-05 18:37:16 +01:00
2021-02-18 19:14:53 +01:00
const Log = require("logger");
const { cors, getConfig, getHtml, getVersion, getStartup, getEnvVars } = require("./server_functions");
2017-03-10 18:20:11 -03:00
2021-01-06 13:17:39 +01:00
/**
* Server
* @param {object} config The MM config
* @class
*/
2024-01-01 15:38:08 +01:00
function Server (config) {
2023-01-01 18:09:08 +01:00
const app = express();
2021-01-05 18:37:16 +01:00
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;
2023-01-01 18:09:08 +01:00
/**
* Opens the server for incoming connections
* @returns {Promise} A promise that is resolved when the server listens to connections
*/
this.open = function () {
return new Promise((resolve) => {
if (config.useHttps) {
const options = {
key: fs.readFileSync(config.httpsPrivateKey),
cert: fs.readFileSync(config.httpsCertificate)
};
2023-04-04 20:44:32 +02:00
server = https.Server(options, app);
} else {
2023-04-04 20:44:32 +02:00
server = http.Server(app);
2023-01-01 18:09:08 +01:00
}
2023-04-04 20:44:32 +02:00
const io = socketio(server, {
2023-01-01 18:09:08 +01:00
cors: {
origin: /.*$/,
credentials: true
},
allowEIO3: true
});
server.on("connection", (socket) => {
serverSockets.add(socket);
socket.on("close", () => {
serverSockets.delete(socket);
});
});
Log.log(`Starting server on port ${port} ... `);
server.listen(port, config.address || "localhost");
if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {
2024-01-16 21:54:55 +01:00
Log.warn("You're using a full whitelist configuration to allow for all IPs");
}
2022-01-24 23:45:17 +01:00
2023-01-01 18:09:08 +01:00
app.use(function (req, res, next) {
ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) {
if (err === undefined) {
res.header("Access-Control-Allow-Origin", "*");
return next();
}
Log.log(err.message);
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(config.httpHeaders));
app.use("/js", express.static(__dirname));
let directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations"];
if (process.env.JEST_WORKER_ID !== undefined) {
// add tests directories only when running tests
directories.push("/tests/configs", "/tests/mocks");
}
2023-01-01 18:09:08 +01:00
for (const directory of directories) {
app.use(directory, express.static(path.resolve(global.root_path + directory)));
}
2016-12-29 22:23:08 -03:00
2023-01-01 18:09:08 +01:00
app.get("/cors", async (req, res) => await cors(req, res));
2017-06-29 21:22:00 +02:00
2023-01-01 18:09:08 +01:00
app.get("/version", (req, res) => getVersion(req, res));
2016-10-13 16:42:15 +02:00
2023-01-01 18:09:08 +01:00
app.get("/config", (req, res) => getConfig(req, res));
2023-10-01 20:13:41 +02:00
app.get("/startup", (req, res) => getStartup(req, res));
app.get("/env", (req, res) => getEnvVars(req, res));
2023-01-01 18:09:08 +01:00
app.get("/", (req, res) => getHtml(req, res));
2023-01-01 18:09:08 +01:00
server.on("listening", () => {
resolve({
app,
io
});
});
});
};
2021-09-15 21:09:31 +02:00
2023-01-01 18:09:08 +01:00
/**
* Closes the server and destroys all lingering connections to it.
* @returns {Promise} A promise that resolves when server has successfully shut down
*/
2021-09-15 21:09:31 +02:00
this.close = function () {
2023-01-01 18:09:08 +01:00
return new Promise((resolve) => {
for (const socket of serverSockets.values()) {
socket.destroy();
}
server.close(resolve);
});
2021-09-16 22:36:18 +02:00
};
2021-01-05 18:37:16 +01:00
}
2016-04-03 19:52:13 +02:00
module.exports = Server;