//const http = require("http"); const https = require("https"); const http = require("http"); const url = require("url"); const food = require("./food.js"); const SQL_DBS = require("./database.js"); const DBPARSE = require("./dbparse.js"); const open = require("./Functions/open.js"); const strFuncs = require("./Functions/stringFuncs.js"); const dateFuncs = require("./Functions/dateFuncs.js"); const updateDB = require("./update.js"); async function init() { const build = { "./Cont/index.html": buildMain, "./Cont/index.css": buildDefault, "./Cont/devs/index.html": buildDevs, "./Cont/devs/index.css": buildDefault, "./Cont/404/index.css": buildDefault, "./Cont/non-main.css": buildDefault, "./Cont/Images/help.png": buildImage, "./Cont/Images/back.png": buildImage, "./Cont/Images/favicon.ico": buildImage, }; const errorPath = "./Cont/404/index.html"; const startDate = new Date(); let visitorCount = 0; // await for needed things in async let [dbcredentials, httpsKey, httpsCert] = await Promise.all([ open.file("../dblogin.txt"), open.file("../Certificate/key.pem"), open.file("../Certificate/cert.pem") ]); // https options, you need to get a certificate in the file ../Certificate for the server to work const httpsOpts = { key: httpsKey, cert: httpsCert }; // get the MySQL DB connection const SQLDB = new SQL_DBS.Database(JSON.parse(dbcredentials)); // Update... // ...shifts and classes await updateDB.update(SQLDB, "../Updation/shifts.txt", "../Updation/vanhalops.csv", "../Updation/uusilops.csv"); // ...foods run_at_monday_mornings(() => food.build(SQLDB)); if ((new Date()).getDay() !== 1) // update if it's not monday. if it's monday, it has already been run by the scheduler above. await food.build(SQLDB); // server code async function server(req, res) { // Lightweight analytics. Don't be evil. We just want to know if anyone uses this. visitorCount++; // validate inputs let q = url.parse(req.url, true); let ind = q.query.index; if (typeof ind === "string") ind = validateIndex(q.query.index.substring(0, 20)); else ind = ""; let d = q.query.day; if (typeof d === "string") d = antiXSS(d); else d = ""; q.query = { index: ind, day: d }; let path = "./Cont" + antiXSS(q.pathname); if (isDir(path)) path += ["/index.html", "index.html"][+(path[path.length - 1] === "/")]; // pack the data required by the builders let data; const args = { "path": path, "path404": errorPath, "query": q.query, "sqldb": SQLDB }; // build the page const buildFound = +(typeof build[path] === "function"); res.writeHead([404, 200][buildFound]); data = await [build404, build[path]][buildFound](args); res.write(data); res.end(); } // start servers const httpsServer = https.createServer(httpsOpts, server).listen(443); const httpServer = http.createServer(server).listen(80); console.log("Servers Up And Running!") // stop server async function closeServers() { console.log("Updating stats to DB...") const uptime = Math.ceil((((new Date()).getTime() - startDate.getTime()) / 1000) / (24 * 60 * 60)); const monthOfStart = `${startDate.getMonth() + 1}`.padStart(2, "0"); const monthDayOfStart = `${startDate.getDate()}`.padStart(2, "0"); try { await SQLDB.query("INSERT INTO stats VALUES (?, ?, ?, ?)", [ `${startDate.getFullYear()}-${monthOfStart}-${monthDayOfStart}`, uptime, visitorCount, Math.round(visitorCount / uptime) ]); } catch(e) { console.log(`\nERROR! Probably because updating the statistics several times a day is not supported, at least yet. Here's the error:\n${e}\n`); } console.log("Done. Shutting down..."); await SQLDB.close(); console.log("MySQL connection closed"); httpsServer.close(); httpServer.close(); console.log("Servers shut down"); console.log("Process exiting..."); process.exit(0); } process.on("SIGINT", closeServers); process.on("SIGQUIT", closeServers); process.on("SIGTERM", closeServers); } function validateIndex(sus) { return antiXSS(DBPARSE.cluttered(sus)); } function antiXSS(sus) { if (!(typeof sus === "string")) return ""; return replace(sus, ["<", ">", "(", ")"], ["<", ">", "(", ")"]); } function isDir(path) { return (DBPARSE.getNextChar(path.substring(1), ".") === -1); } function replace(s, from, to) { for (let i = 0; i < from.length; i++) { s = s.replaceAll(from[i], to[i]); } return s; } async function buildMain(args) { // get the passed arguments const path = args["path"]; const query = args["query"]; const index = query.index; const SQLDB = args["sqldb"]; const data = await open.file(path); let data_string = data.toString("utf-8"); // here are the things to replace in the html page let res = {}; // get valid day const d = new Date(); let day = d.getDay(); day = (day + +(day === 0) * 7) - 1; // converts from 0 = sunday to 0 = monday const actualDay = day; day = +(!(day === 5) && !(day === 6)) * day; if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) >= 0) && (parseInt(query.day) < 5)) day = parseInt(query.day); // set the day selected (must be done manually with this replacement system) data_string = data_string.replace(`