2024-01-08 17:45:54 +01:00
|
|
|
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 helmet = require("helmet");
|
2023-04-04 20:44:32 +02:00
|
|
|
const socketio = require("socket.io");
|
2021-02-18 19:14:53 +01:00
|
|
|
const Log = require("logger");
|
2017-03-10 18:20:11 -03:00
|
|
|
|
2026-06-01 17:26:16 +02:00
|
|
|
const { ipAccessControl, socketIpAccessControl } = require("./ip_access_control");
|
2026-03-12 10:35:42 +01:00
|
|
|
|
|
|
|
|
const vendor = require("./vendor");
|
2025-10-18 19:56:55 +02:00
|
|
|
|
2026-03-12 10:35:42 +01:00
|
|
|
const { getHtml, getVersion, getEnvVars, cors } = require("#server_functions");
|
2025-06-01 17:03:11 +02:00
|
|
|
|
2021-01-06 13:17:39 +01:00
|
|
|
/**
|
|
|
|
|
* Server
|
2026-02-06 00:21:35 +01:00
|
|
|
* @param {object} configObj The MM config full and redacted
|
2021-01-06 13:17:39 +01:00
|
|
|
* @class
|
|
|
|
|
*/
|
2026-02-06 00:21:35 +01:00
|
|
|
function Server (configObj) {
|
|
|
|
|
const config = configObj.fullConf;
|
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);
|
2022-01-25 22:30:16 +01:00
|
|
|
} 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, {
|
2026-06-01 17:26:16 +02:00
|
|
|
allowRequest: socketIpAccessControl(config.ipWhitelist),
|
2023-01-01 18:09:08 +01:00
|
|
|
cors: {
|
|
|
|
|
origin: /.*$/,
|
|
|
|
|
credentials: true
|
|
|
|
|
},
|
2025-08-28 11:02:21 -05:00
|
|
|
allowEIO3: true,
|
|
|
|
|
pingInterval: 120000, // server → client ping every 2 mins
|
|
|
|
|
pingTimeout: 120000 // wait up to 2 mins for client pong
|
2023-01-01 18:09:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.on("connection", (socket) => {
|
|
|
|
|
serverSockets.add(socket);
|
|
|
|
|
socket.on("close", () => {
|
|
|
|
|
serverSockets.delete(socket);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Log.log(`Starting server on port ${port} ... `);
|
2025-09-13 13:01:55 +02:00
|
|
|
|
|
|
|
|
// Add explicit error handling BEFORE calling listen so we can give user-friendly feedback
|
|
|
|
|
server.once("error", (err) => {
|
|
|
|
|
if (err && err.code === "EADDRINUSE") {
|
|
|
|
|
const bindAddr = config.address || "localhost";
|
|
|
|
|
const portInUseMessage = [
|
|
|
|
|
"",
|
|
|
|
|
"────────────────────────────────────────────────────────────────",
|
|
|
|
|
` PORT IN USE: ${bindAddr}:${port}`,
|
|
|
|
|
"",
|
|
|
|
|
" Another process (most likely another MagicMirror instance)",
|
|
|
|
|
" is already using this port.",
|
|
|
|
|
"",
|
|
|
|
|
" Stop the other process (free the port) or use a different port.",
|
|
|
|
|
"────────────────────────────────────────────────────────────────"
|
|
|
|
|
].join("\n");
|
|
|
|
|
Log.error(portInUseMessage);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.error("Failed to start server:", err);
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-01 18:09:08 +01:00
|
|
|
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-25 22:30:16 +01:00
|
|
|
}
|
2022-01-24 23:45:17 +01:00
|
|
|
|
2025-10-18 19:56:55 +02:00
|
|
|
app.use(ipAccessControl(config.ipWhitelist));
|
2023-01-01 18:09:08 +01:00
|
|
|
app.use(helmet(config.httpHeaders));
|
|
|
|
|
app.use("/js", express.static(__dirname));
|
|
|
|
|
|
2026-02-06 00:21:35 +01:00
|
|
|
if (config.hideConfigSecrets) {
|
|
|
|
|
app.get("/config/config.env", (req, res) => {
|
|
|
|
|
res.status(404).send("<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot GET /config/config.env</pre>\n</body>\n</html>");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 08:37:52 +01:00
|
|
|
let directories = ["/config", "/css", "/favicon.svg", "/defaultmodules", "/modules", "/node_modules/animate.css", "/node_modules/@fontsource", "/node_modules/@fortawesome", "/translations", "/tests/configs", "/tests/mocks"];
|
2026-04-02 08:56:27 +02:00
|
|
|
for (const value of Object.values(vendor)) {
|
2025-06-01 17:03:11 +02:00
|
|
|
const dirArr = value.split("/");
|
|
|
|
|
if (dirArr[0] === "node_modules") directories.push(`/${dirArr[0]}/${dirArr[1]}`);
|
|
|
|
|
}
|
|
|
|
|
const uniqDirs = [...new Set(directories)];
|
|
|
|
|
for (const directory of uniqDirs) {
|
2023-01-01 18:09:08 +01:00
|
|
|
app.use(directory, express.static(path.resolve(global.root_path + directory)));
|
|
|
|
|
}
|
2016-12-29 22:23:08 -03:00
|
|
|
|
2026-02-23 10:27:29 +01:00
|
|
|
const startUp = new Date();
|
|
|
|
|
const getStartup = (req, res) => res.send(startUp);
|
|
|
|
|
|
2026-02-06 00:21:35 +01:00
|
|
|
const getConfig = (req, res) => {
|
2026-04-12 22:50:00 +02:00
|
|
|
const obj = config.hideConfigSecrets ? configObj.redactedConf : configObj.fullConf;
|
|
|
|
|
// Functions can't survive JSON.stringify, so we wrap them in a
|
|
|
|
|
// tagged object { __mmFunction: "<source>" }. The client-side
|
|
|
|
|
// JSON reviver in main.js recognises this tag and reconstructs
|
|
|
|
|
// the live function from the source string.
|
|
|
|
|
const jsonString = JSON.stringify(obj, (key, value) => {
|
|
|
|
|
if (typeof value === "function") {
|
|
|
|
|
return { __mmFunction: value.toString() };
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
});
|
|
|
|
|
res.set("Content-Type", "application/json");
|
|
|
|
|
res.send(jsonString);
|
2026-02-06 00:21:35 +01:00
|
|
|
};
|
2026-04-12 22:50:00 +02:00
|
|
|
|
2026-02-23 10:27:29 +01:00
|
|
|
app.get("/config", (req, res) => getConfig(req, res));
|
2026-02-06 00:21:35 +01: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-10-01 20:13:41 +02:00
|
|
|
app.get("/startup", (req, res) => getStartup(req, res));
|
|
|
|
|
|
2024-09-18 19:10:46 +02:00
|
|
|
app.get("/env", (req, res) => getEnvVars(req, res));
|
|
|
|
|
|
2023-01-01 18:09:08 +01:00
|
|
|
app.get("/", (req, res) => getHtml(req, res));
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2025-10-28 19:14:51 +01:00
|
|
|
// Reload endpoint for watch mode - triggers browser reload
|
|
|
|
|
app.get("/reload", (req, res) => {
|
|
|
|
|
Log.info("Reload request received, notifying all clients");
|
|
|
|
|
io.emit("RELOAD");
|
|
|
|
|
res.status(200).send("OK");
|
|
|
|
|
});
|
|
|
|
|
|
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-03-30 12:20:46 +02:00
|
|
|
|
2016-04-03 19:52:13 +02:00
|
|
|
module.exports = Server;
|