2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2016-03-30 12:20:46 +02:00
|
|
|
* Server
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2016-03-30 12:20:46 +02:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2021-01-05 18:37:16 +01:00
|
|
|
const express = require("express");
|
|
|
|
const path = require("path");
|
|
|
|
const ipfilter = require("express-ipfilter").IpFilter;
|
|
|
|
const fs = require("fs");
|
|
|
|
const helmet = require("helmet");
|
2022-05-27 19:46:28 +02:00
|
|
|
const fetch = require("fetch");
|
2021-01-05 18:37:16 +01:00
|
|
|
|
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");
|
2017-03-10 18:20:11 -03:00
|
|
|
|
2021-01-06 13:17:39 +01:00
|
|
|
/**
|
|
|
|
* Server
|
|
|
|
*
|
|
|
|
* @param {object} config The MM config
|
|
|
|
* @class
|
|
|
|
*/
|
2022-10-19 21:40:43 +02:00
|
|
|
function Server(config) {
|
|
|
|
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;
|
2022-10-19 21:40:43 +02:00
|
|
|
|
|
|
|
this.open = function () {
|
|
|
|
if (config.useHttps) {
|
|
|
|
const options = {
|
|
|
|
key: fs.readFileSync(config.httpsPrivateKey),
|
|
|
|
cert: fs.readFileSync(config.httpsCertificate)
|
|
|
|
};
|
|
|
|
server = require("https").Server(options, app);
|
|
|
|
} else {
|
|
|
|
server = require("http").Server(app);
|
|
|
|
}
|
|
|
|
const io = require("socket.io")(server, {
|
|
|
|
cors: {
|
|
|
|
origin: /.*$/,
|
|
|
|
credentials: true
|
|
|
|
},
|
|
|
|
allowEIO3: true
|
2021-09-24 00:30:00 +02:00
|
|
|
});
|
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
server.on("connection", (socket) => {
|
|
|
|
serverSockets.add(socket);
|
|
|
|
socket.on("close", () => {
|
|
|
|
serverSockets.delete(socket);
|
|
|
|
});
|
|
|
|
});
|
2017-03-10 18:20:11 -03:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
Log.log(`Starting server on port ${port} ... `);
|
|
|
|
server.listen(port, config.address || "localhost");
|
2016-09-29 17:07:22 +02:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {
|
|
|
|
Log.warn(Utils.colors.warn("You're using a full whitelist configuration to allow for all IPs"));
|
|
|
|
}
|
2017-03-10 16:36:17 -03:00
|
|
|
|
2022-10-19 21:40:43 +02: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.");
|
|
|
|
});
|
2016-09-29 17:07:22 +02:00
|
|
|
});
|
2022-10-19 21:40:43 +02:00
|
|
|
|
|
|
|
app.use(helmet(config.httpHeaders));
|
|
|
|
app.use("/js", express.static(__dirname));
|
|
|
|
|
|
|
|
// TODO add tests directory only when running tests?
|
|
|
|
const directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations", "/tests/configs", "/tests/mocks"];
|
|
|
|
for (const directory of directories) {
|
|
|
|
app.use(directory, express.static(path.resolve(global.root_path + directory)));
|
2022-01-25 22:30:16 +01:00
|
|
|
}
|
2022-01-24 23:45:17 +01:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
app.get("/cors", async function (req, res) {
|
|
|
|
// example: http://localhost:8080/cors?url=https://google.de
|
|
|
|
|
|
|
|
try {
|
|
|
|
const reg = "^/cors.+url=(.*)";
|
|
|
|
let url = "";
|
|
|
|
|
|
|
|
let match = new RegExp(reg, "g").exec(req.url);
|
|
|
|
if (!match) {
|
|
|
|
url = "invalid url: " + req.url;
|
|
|
|
Log.error(url);
|
|
|
|
res.send(url);
|
|
|
|
} else {
|
|
|
|
url = match[1];
|
|
|
|
Log.log("cors url: " + url);
|
|
|
|
const response = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 MagicMirror/" + global.version } });
|
|
|
|
const header = response.headers.get("Content-Type");
|
|
|
|
const data = await response.text();
|
|
|
|
if (header) res.set("Content-Type", header);
|
|
|
|
res.send(data);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
Log.error(error);
|
|
|
|
res.send(error);
|
|
|
|
}
|
|
|
|
});
|
2016-12-29 22:23:08 -03:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
app.get("/version", function (req, res) {
|
|
|
|
res.send(global.version);
|
|
|
|
});
|
2017-06-29 21:22:00 +02:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
app.get("/config", function (req, res) {
|
|
|
|
res.send(config);
|
|
|
|
});
|
2016-10-13 16:42:15 +02:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
app.get("/", function (req, res) {
|
|
|
|
let html = fs.readFileSync(path.resolve(`${global.root_path}/index.html`), { encoding: "utf8" });
|
|
|
|
html = html.replace("#VERSION#", global.version);
|
2017-01-28 21:37:12 -03:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
let configFile = "config/config.js";
|
|
|
|
if (typeof global.configuration_file !== "undefined") {
|
|
|
|
configFile = global.configuration_file;
|
|
|
|
}
|
|
|
|
html = html.replace("#CONFIG_FILE#", configFile);
|
|
|
|
|
|
|
|
res.send(html);
|
|
|
|
});
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2022-10-19 21:40:43 +02:00
|
|
|
return {
|
|
|
|
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
|
|
|
}
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2016-04-03 19:52:13 +02:00
|
|
|
module.exports = Server;
|