Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Magic Mirror
  2. * Server
  3. *
  4. * By Michael Teeuw https://michaelteeuw.nl
  5. * MIT Licensed.
  6. */
  7. const express = require("express");
  8. const app = require("express")();
  9. const path = require("path");
  10. const ipfilter = require("express-ipfilter").IpFilter;
  11. const fs = require("fs");
  12. const helmet = require("helmet");
  13. const Log = require("logger");
  14. const Utils = require("./utils.js");
  15. /**
  16. * Server
  17. *
  18. * @param {object} config The MM config
  19. * @param {Function} callback Function called when done.
  20. * @class
  21. */
  22. function Server(config, callback) {
  23. const port = process.env.MM_PORT || config.port;
  24. const serverSockets = new Set();
  25. let server = null;
  26. if (config.useHttps) {
  27. const options = {
  28. key: fs.readFileSync(config.httpsPrivateKey),
  29. cert: fs.readFileSync(config.httpsCertificate)
  30. };
  31. server = require("https").Server(options, app);
  32. } else {
  33. server = require("http").Server(app);
  34. }
  35. const io = require("socket.io")(server, {
  36. cors: {
  37. origin: /.*$/,
  38. credentials: true
  39. },
  40. allowEIO3: true
  41. });
  42. server.on("connection", (socket) => {
  43. serverSockets.add(socket);
  44. socket.on("close", () => {
  45. serverSockets.delete(socket);
  46. });
  47. });
  48. Log.log(`Starting server on port ${port} ... `);
  49. server.listen(port, config.address || "localhost");
  50. if (config.ipWhitelist instanceof Array && config.ipWhitelist.length === 0) {
  51. Log.warn(Utils.colors.warn("You're using a full whitelist configuration to allow for all IPs"));
  52. }
  53. app.use(function (req, res, next) {
  54. ipfilter(config.ipWhitelist, { mode: config.ipWhitelist.length === 0 ? "deny" : "allow", log: false })(req, res, function (err) {
  55. if (err === undefined) {
  56. return next();
  57. }
  58. Log.log(err.message);
  59. 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.");
  60. });
  61. });
  62. app.use(helmet({ contentSecurityPolicy: false }));
  63. app.use("/js", express.static(__dirname));
  64. const directories = ["/config", "/css", "/fonts", "/modules", "/vendor", "/translations", "/tests/configs"];
  65. for (const directory of directories) {
  66. app.use(directory, express.static(path.resolve(global.root_path + directory)));
  67. }
  68. app.get("/version", function (req, res) {
  69. res.send(global.version);
  70. });
  71. app.get("/config", function (req, res) {
  72. res.send(config);
  73. });
  74. app.get("/", function (req, res) {
  75. let html = fs.readFileSync(path.resolve(`${global.root_path}/index.html`), { encoding: "utf8" });
  76. html = html.replace("#VERSION#", global.version);
  77. let configFile = "config/config.js";
  78. if (typeof global.configuration_file !== "undefined") {
  79. configFile = global.configuration_file;
  80. }
  81. html = html.replace("#CONFIG_FILE#", configFile);
  82. res.send(html);
  83. });
  84. if (typeof callback === "function") {
  85. callback(app, io);
  86. }
  87. this.close = function () {
  88. for (const socket of serverSockets.values()) {
  89. socket.destroy();
  90. }
  91. server.close();
  92. };
  93. }
  94. module.exports = Server;