2016-03-30 12:20:46 +02:00
|
|
|
/* Magic Mirror
|
|
|
|
* Server
|
|
|
|
*
|
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
var express = require("express");
|
|
|
|
var app = require("express")();
|
|
|
|
var server = require("http").Server(app);
|
|
|
|
var io = require("socket.io")(server);
|
|
|
|
var path = require("path");
|
2016-09-29 17:26:32 +02:00
|
|
|
var ipfilter = require("express-ipfilter").IpFilter;
|
2016-10-13 16:42:15 +02:00
|
|
|
var fs = require("fs");
|
2016-11-17 09:34:11 -05:00
|
|
|
var helmet = require("helmet");
|
2016-03-30 12:20:46 +02:00
|
|
|
|
|
|
|
var Server = function(config, callback) {
|
|
|
|
console.log("Starting server op port " + config.port + " ... ");
|
|
|
|
|
2016-11-25 20:14:34 -03:00
|
|
|
server.listen(config.port, config.address ? config.address : null);
|
2016-09-29 17:07:22 +02:00
|
|
|
|
|
|
|
app.use(function(req, res, next) {
|
2016-09-29 17:26:32 +02:00
|
|
|
var result = ipfilter(config.ipWhitelist, {mode: "allow", log: false})(req, res, function(err) {
|
2016-09-29 17:07:22 +02:00
|
|
|
if (err === undefined) {
|
|
|
|
return next();
|
|
|
|
}
|
2016-09-29 17:55:32 +02:00
|
|
|
console.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.");
|
|
|
|
});
|
|
|
|
});
|
2016-11-17 09:34:11 -05:00
|
|
|
app.use(helmet());
|
2016-09-29 17:07:22 +02:00
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
app.use("/js", express.static(__dirname));
|
2016-12-29 19:06:47 -03:00
|
|
|
app.use("/config", express.static(path.resolve(global.root_path + "/config")));
|
|
|
|
app.use("/css", express.static(path.resolve(global.root_path + "/css")));
|
|
|
|
app.use("/fonts", express.static(path.resolve(global.root_path + "/fonts")));
|
|
|
|
app.use("/modules", express.static(path.resolve(global.root_path + "/modules")));
|
|
|
|
app.use("/vendor", express.static(path.resolve(global.root_path + "/vendor")));
|
|
|
|
app.use("/translations", express.static(path.resolve(global.root_path + "/translations")));
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
app.get("/version", function(req,res) {
|
|
|
|
res.send(global.version);
|
|
|
|
});
|
2016-12-29 22:23:08 -03:00
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
app.get("/", function(req, res) {
|
2016-12-29 19:06:47 -03:00
|
|
|
var html = fs.readFileSync(path.resolve(global.root_path + "/index.html"), {encoding: "utf8"});
|
2016-10-13 16:42:15 +02:00
|
|
|
html = html.replace("#VERSION#", global.version);
|
|
|
|
|
2017-01-28 21:37:12 -03:00
|
|
|
configFile = "config/config.js";
|
|
|
|
if (typeof(global.configuration_file) !== "undefined") {
|
|
|
|
configFile = global.configuration_file;
|
|
|
|
}
|
|
|
|
html = html.replace("#CONFIG_FILE#", configFile);
|
|
|
|
|
2016-10-13 16:42:15 +02:00
|
|
|
res.send(html);
|
2016-03-30 12:20:46 +02:00
|
|
|
});
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
if (typeof callback === "function") {
|
2016-04-05 14:22:34 +02:00
|
|
|
callback(app, io);
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-03 19:52:13 +02:00
|
|
|
module.exports = Server;
|