const config = require("../config/config.json"), crypto = require("crypto"), emoji = require("../misc/emoji_list.json"), fs = require("fs"); // Defines the function of this module const MODULE_FUNCTION = "handle_requests", // Base path for module folder creation and navigation BASE_PATH = "/EUS", API_CACHE_LIFESPAN = 3600000; let node_modules = {}; let eusConfig = {}, useUploadKey = true, cacheJSON = "", startupFinished = false, timeSinceLastCache = Date.now(); class Database { constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName, connectedCallback) { this.connectionPool = node_modules["mysql2"].createPool({ connectionLimit: 128, host: databaseAddress, port: databasePort, user: databaseUsername, password: databasePassword, database: databaseName }); const classCreationTime = Date.now(); this.dbActive = false; if (connectedCallback == null) { this.dbActive = true; } else { const connectionCheckInterval = setInterval(() => { this.query("SELECT id FROM images LIMIT 1") .then(data => { global.modules.consoleHelper.printInfo(emoji.globe_europe, `Connected to database. Took ${Date.now() - classCreationTime}ms`); this.dbActive = true; clearInterval(connectionCheckInterval); connectedCallback(); }) .catch(err => { console.error(err); }); }, 167); // Roughly 6 times per sec } } dataReceived(resolveCallback, data, limited = false) { if (limited) resolveCallback(data[0]); else resolveCallback(data); } query(query = "", data) { const limited = query.includes("LIMIT 1"); return new Promise((resolve, reject) => { this.connectionPool.getConnection((err, connection) => { if (err) { reject(err); try { connection.release();} catch (e) { console.error("Failed to release mysql connection", err); } } else { // Use old query if (data == null) { connection.query(query, (err, data) => { if (err) { reject(err); connection.release(); } else { this.dataReceived(resolve, data, limited); connection.release(); } }); } // Use new prepared statements w/ placeholders else { connection.execute(query, data, (err, data) => { if (err) { reject(err); connection.release(); } else { this.dataReceived(resolve, data, limited); connection.release(); } }); } } }); }); } } let dbConnection; function init() { // Require node modules node_modules["chalk"] = require("chalk"); node_modules["busboy"] = require("connect-busboy"); node_modules["randomstring"] = require("randomstring"); node_modules["streamMeter"] = require("stream-meter"); node_modules["mysql2"] = require("mysql2"); // Only ran on startup so using sync functions is fine // Makes the folder for files of the module if (!fs.existsSync(__dirname + BASE_PATH)) { fs.mkdirSync(__dirname + BASE_PATH); console.log(`[EUS] Made EUS module folder`); } // Makes the folder for frontend files if (!fs.existsSync(__dirname + BASE_PATH + "/files")) { fs.mkdirSync(__dirname + BASE_PATH + "/files"); console.log(`[EUS] Made EUS web files folder`); } // Makes the folder for images if (!fs.existsSync(__dirname + BASE_PATH + "/i")) { fs.mkdirSync(__dirname + BASE_PATH + "/i"); console.log(`[EUS] Made EUS images folder`); } if (!fs.existsSync("/tmp/EUS_UPLOADS")) { fs.mkdirSync("/tmp/EUS_UPLOADS"); console.log("[EUS] Made EUS temp upload folder"); } // Makes the config file if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) { // Config doesn't exist, make it. fs.writeFileSync(`${__dirname}${BASE_PATH}/config.json`, '{\n\t"baseURL":"http://example.com/",\n\t"acceptedTypes": [\n\t\t".png",\n\t\t".jpg",\n\t\t".jpeg",\n\t\t".gif"\n\t],\n\t"uploadKey": "",\n\t"database": {\n\t\t"databaseAddress": "127.0.0.1",\n\t\t"databasePort": 3306,\n\t\t"databaseUsername": "root",\n\t\t"databasePassword": "password",\n\t\t"databaseName": "EUS"\n\t}\n}'); console.log("[EUS] Made EUS config File!"); console.log("[EUS] Please edit the EUS Config file before restarting."); // Config has been made, close framework. process.exit(0); } else { eusConfig = require(`${__dirname}${BASE_PATH}/config.json`); if (validateConfig(eusConfig)) console.log("[EUS] EUS config passed all checks"); } // This is using a callback but that's fine, the server will just react properly to the db not being ready yet. dbConnection = new Database(eusConfig["database"]["databaseAddress"], eusConfig["database"]["databasePort"], eusConfig["database"]["databaseUsername"], eusConfig["database"]["databasePassword"], eusConfig["database"]["databaseName"], async () => { cacheJSON = JSON.stringify(await cacheFilesAndSpace()); cacheIsReady = true; }); console.log("[EUS] Finished loading."); } // Cache for the file count and space usage, this takes a while to do so it's best to cache the result let cacheIsReady = false; function cacheFilesAndSpace() { timeSinceLastCache = Date.now(); return new Promise(async (resolve, reject) => { const startCacheTime = Date.now(); cacheIsReady = false; let cachedFilesAndSpace = { fileCounts: {}, }; const dbData = await dbConnection.query(`SELECT imageType, COUNT(imageType) AS "count" FROM images GROUP BY imageType`); let totalFiles = 0; dbData.forEach(fileType => { cachedFilesAndSpace["fileCounts"][fileType.imageType] = fileType.count; totalFiles += fileType.count; }); cachedFilesAndSpace["filesTotal"] = totalFiles; cachedFilesAndSpace["size"] = {}; const dbSize = (await dbConnection.query(`SELECT SUM(fileSize) FROM images LIMIT 1`))["SUM(fileSize)"]; const totalSizeBytes = dbSize == null ? 0 : dbSize; const mbSize = totalSizeBytes / 1024 / 1024; cachedFilesAndSpace["size"]["mb"] = parseFloat(mbSize.toFixed(4)); cachedFilesAndSpace["size"]["gb"] = parseFloat((mbSize / 1024).toFixed(4)); cachedFilesAndSpace["size"]["string"] = await spaceToLowest(totalSizeBytes, true); resolve(cachedFilesAndSpace); global.modules.consoleHelper.printInfo(emoji.folder, `Stats api cache took ${Date.now() - startCacheTime}ms`); }); } function validateConfig(json) { let performShutdownAfterValidation = false; // URL Tests if (json["baseURL"] == null) { console.error("EUS baseURL property does not exist!"); performShutdownAfterValidation = true; } else { if (json["baseURL"] == "") console.warn("EUS baseURL property is blank"); const bURL = `${json["baseURL"]}`.split(""); if (bURL.length > 1) { if (bURL[bURL.length-1] != "/") console.warn("EUS baseURL property doesn't have a / at the end, this can lead to unpredictable results!"); } else { if (json["baseURL"] != "http://" || json["baseURL"] != "https://") console.warn("EUS baseURL property is possibly invalid!"); } } // acceptedTypes checks if (json["acceptedTypes"] == null) { console.error("EUS acceptedTypes list does not exist!"); performShutdownAfterValidation = true; } else { if (json["acceptedTypes"].length < 1) console.warn("EUS acceptedTypes array has no extentions in it, users will not be able to upload images!"); } // uploadKey checks if (json["uploadKey"] == null) { console.error("EUS uploadKey property does not exist!"); performShutdownAfterValidation = true; } else { if (json["uploadKey"] == "") useUploadKey = false; } // database checks if (json["database"] == null) { console.error("EUS database properties do not exist!"); performShutdownAfterValidation = true; } else { // databaseAddress if (json["database"]["databaseAddress"] == null) { console.error("EUS database.databaseAddress property does not exist!"); performShutdownAfterValidation = true; } // databasePort if (json["database"]["databasePort"] == null) { console.error("EUS database.databasePort property does not exist!"); performShutdownAfterValidation = true; } // databaseUsername if (json["database"]["databaseUsername"] == null) { console.error("EUS database.databaseUsername property does not exist!"); performShutdownAfterValidation = true; } // databasePassword if (json["database"]["databasePassword"] == null) { console.error("EUS database.databasePassword property does not exist!"); performShutdownAfterValidation = true; } // databaseName if (json["database"]["databaseName"] == null) { console.error("EUS database.databaseName property does not exist!"); performShutdownAfterValidation = true; } } // Check if server needs to be shutdown if (performShutdownAfterValidation) { console.error("EUS config properties are missing, refer to example config on GitHub (https://github.com/tgpholly/EUS)"); process.exit(1); } else return true; } function cleanURL(url = "") { return url.split("%20").join(" ").split("%22").join("\"").split("%23").join("#").split("%24").join("$").split("%25").join("%").split("%26").join("&").split("%27").join("'").split("%2B").join("+").split("%2C").join(",").split("%2F").join("/") .split("%3A").join(":").split("%3B").join(";").split("%3C").join("<").split("%3D").join("=").split("%3E").join(">").split("%3F").join("?") .split("%40").join("@") .split("%5B").join("[").split("%5C").join("\\").split("%5D").join("]").split("%5E").join("^") .split("%60").join("`") .split("%7B").join("{").split("%7C").join("|").split("%7D").join("}").split("%7E").join("~") .split("%CE%A9").join("Ω"); } let existanceCache = {}; function regularFile(req, res, urs = "", startTime = 0) { if (req.url === "/") { urs = "/index.html" } else { urs = req.url } fs.access(`${__dirname}${BASE_PATH}/files${urs}`, (error) => { if (error) { // Doesn't exist, send a 404 to the client. error404Page(res); global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${node_modules.chalk.red("[404]")} ${req.url} ${Date.now() - startTime}ms`); } else { // File does exist, send it back to the client. res.sendFile(`${__dirname}${BASE_PATH}/files${req.url}`); global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${node_modules.chalk.green("[200]")} ${req.url} ${Date.now() - startTime}ms`); } }); } function imageFile(req, res, file, startTime = 0) { res.sendFile(`${__dirname}${BASE_PATH}/i/${file}`); global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${node_modules.chalk.green("[200]")} (ImageReq) ${req.url} ${Date.now() - startTime}ms`); } function error404Page(res) { res.status(404).send("404!