2019-11-03 11:33:59 +00:00
|
|
|
var fuzIsOpen = false;
|
2019-11-03 11:45:01 +00:00
|
|
|
var lastSeen = new Date("1970-01-01");
|
2019-11-03 19:31:00 +00:00
|
|
|
var lastNofified = new Date("1970-01-01");
|
|
|
|
var lastClosed = new Date("1970-01-01");
|
2019-11-03 11:45:01 +00:00
|
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
const db = "./.data/data.json";
|
|
|
|
|
|
|
|
try {
|
|
|
|
var content = fs.readFileSync(db, "utf8");
|
2019-11-03 11:56:59 +00:00
|
|
|
fuzIsOpen = JSON.parse(content)["fuzIsOpen"];
|
|
|
|
lastSeen = new Date(JSON.parse(content)["lastSeen"]);
|
2019-11-03 19:31:00 +00:00
|
|
|
lastNofified = new Date(JSON.parse(content)["lastNofified"]);
|
|
|
|
lastClosed = new Date(JSON.parse(content)["lastClosed"]);
|
2019-11-03 11:45:01 +00:00
|
|
|
} catch (err) {}
|
2019-11-03 11:33:59 +00:00
|
|
|
|
2019-09-30 21:05:06 +00:00
|
|
|
const express = require("express");
|
2018-10-15 15:28:02 +00:00
|
|
|
const app = express();
|
2018-03-13 20:25:39 +00:00
|
|
|
|
2019-09-30 21:05:06 +00:00
|
|
|
app.use(express.static("public"));
|
2018-03-13 20:25:39 +00:00
|
|
|
|
2019-11-03 11:33:59 +00:00
|
|
|
app.get("/", (req, res) => {
|
|
|
|
res.sendFile(__dirname + "/views/index.html");
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/img", (req, res) => {
|
2019-11-03 15:01:00 +00:00
|
|
|
if (fuzIsOpen && new Date() - 2 * 60 * 1000 < lastSeen) {
|
2019-11-03 15:12:01 +00:00
|
|
|
return res.sendFile(__dirname + "/views/open.svg"); // https://www.flaticon.com/free-icon/open_1234189
|
2019-11-03 11:45:01 +00:00
|
|
|
}
|
2019-11-03 15:12:01 +00:00
|
|
|
res.sendFile(__dirname + "/views/closed.svg"); // https://www.flaticon.com/free-icon/closed_1234190
|
2018-03-13 20:25:39 +00:00
|
|
|
});
|
2019-11-03 11:56:59 +00:00
|
|
|
app.get("/api", (req, res) => {
|
2019-11-03 15:01:00 +00:00
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header(
|
|
|
|
"Access-Control-Allow-Headers",
|
|
|
|
"Origin, X-Requested-With, Content-Type, Accept"
|
|
|
|
);
|
2019-11-03 15:12:01 +00:00
|
|
|
res.send({ fuzIsOpen, lastSeen });
|
2019-11-03 11:56:59 +00:00
|
|
|
});
|
2018-03-13 20:25:39 +00:00
|
|
|
|
2019-11-03 11:33:59 +00:00
|
|
|
app.get("/status", (req, res) => {
|
2019-11-03 15:24:00 +00:00
|
|
|
// http basic auth handling without 3rd-party lib https://stackoverflow.com/a/33905671
|
|
|
|
const auth = {
|
|
|
|
login: process.env.MATRIXUSERNAME,
|
|
|
|
password: process.env.MATRIXPASSWORD
|
2019-11-03 15:35:01 +00:00
|
|
|
};
|
2019-11-03 15:12:01 +00:00
|
|
|
|
|
|
|
// parse login and password from headers
|
2019-11-03 15:24:00 +00:00
|
|
|
const b64auth = (req.headers.authorization || "").split(" ")[1] || "";
|
2019-11-03 16:10:01 +00:00
|
|
|
const [_, login, password] =
|
|
|
|
new Buffer(b64auth, "base64").toString().match(/(.*):(.*)/) || []; // slightly modified as we use : in username
|
2019-11-03 15:47:00 +00:00
|
|
|
|
2019-11-03 15:35:01 +00:00
|
|
|
if (
|
|
|
|
!login ||
|
|
|
|
!password ||
|
|
|
|
login !== auth.login ||
|
|
|
|
password !== auth.password
|
|
|
|
) {
|
2019-11-03 16:10:01 +00:00
|
|
|
console.log(login, password);
|
2019-11-03 15:35:01 +00:00
|
|
|
res.set("WWW-Authenticate", 'Basic realm="Authentication required"');
|
|
|
|
return res.status(401).send("Authentication required.");
|
2019-11-03 11:33:59 +00:00
|
|
|
}
|
2019-11-03 17:57:00 +00:00
|
|
|
fuzIsOpen = req.query.fuzisopen === "1";
|
2019-11-03 14:37:00 +00:00
|
|
|
lastSeen = new Date();
|
2019-11-03 11:56:59 +00:00
|
|
|
try {
|
|
|
|
fs.writeFileSync(db, JSON.stringify({ fuzIsOpen, lastSeen }));
|
|
|
|
} catch (err) {}
|
|
|
|
|
2019-11-03 11:33:59 +00:00
|
|
|
res.sendStatus(200);
|
2019-11-03 11:22:00 +00:00
|
|
|
});
|
|
|
|
|
2018-10-15 15:38:54 +00:00
|
|
|
const listener = app.listen(process.env.PORT, function() {
|
2019-09-30 21:05:06 +00:00
|
|
|
console.log("Your app is listening on port " + listener.address().port);
|
2018-03-13 20:25:39 +00:00
|
|
|
});
|
2019-11-03 11:22:00 +00:00
|
|
|
|
2019-11-03 19:19:00 +00:00
|
|
|
setTimeout(() => {
|
2019-11-03 19:31:00 +00:00
|
|
|
if (!fuzIsOpen || lastSeen < new Date() - 2 * 60 * 1000) {
|
|
|
|
|
|
|
|
}
|
2019-11-03 19:19:00 +00:00
|
|
|
}, 10 * 1000)
|
|
|
|
|
2019-11-03 17:45:01 +00:00
|
|
|
if (process.env.PROJECT_DOMAIN != "") {
|
|
|
|
process.on("SIGTERM", function() {
|
|
|
|
console.log("SIGTERM received, sending SOS to Resurrect...");
|
|
|
|
require("https").get(
|
|
|
|
"https://resurrect.glitch.me/" + process.env.PROJECT_DOMAIN + "",
|
|
|
|
process.exit
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|