Support static mode and 404 override
This commit is contained in:
parent
c6b2d60821
commit
58f5afdfcb
2 changed files with 156 additions and 157 deletions
206
EUS.js
206
EUS.js
|
@ -12,11 +12,10 @@ let node_modules = {};
|
|||
let eusConfig = {},
|
||||
useUploadKey = true,
|
||||
cacheJSON = "",
|
||||
startupFinished = false,
|
||||
timeSinceLastCache = Date.now();
|
||||
|
||||
class Database {
|
||||
constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName, connectedCallback) {
|
||||
constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName) {
|
||||
this.connectionPool = node_modules["mysql2"].createPool({
|
||||
connectionLimit: 128,
|
||||
host: databaseAddress,
|
||||
|
@ -25,26 +24,6 @@ class Database {
|
|||
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) {
|
||||
|
@ -139,11 +118,9 @@ function init() {
|
|||
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;
|
||||
});
|
||||
if (!eusConfig["noUpload"]) {
|
||||
dbConnection = new Database(eusConfig["database"]["databaseAddress"], eusConfig["database"]["databasePort"], eusConfig["database"]["databaseUsername"], eusConfig["database"]["databasePassword"], eusConfig["database"]["databaseName"]);
|
||||
}
|
||||
|
||||
console.log("[EUS] Finished loading.");
|
||||
}
|
||||
|
@ -184,6 +161,7 @@ function cacheFilesAndSpace() {
|
|||
function validateConfig(json) {
|
||||
let performShutdownAfterValidation = false;
|
||||
// URL Tests
|
||||
if (!json["noUpload"]) {
|
||||
if (json["baseURL"] == null) {
|
||||
console.error("EUS baseURL property does not exist!");
|
||||
performShutdownAfterValidation = true;
|
||||
|
@ -197,21 +175,27 @@ function validateConfig(json) {
|
|||
if (json["baseURL"] != "http://" || json["baseURL"] != "https://") console.warn("EUS baseURL property is possibly invalid!");
|
||||
}
|
||||
}
|
||||
}
|
||||
// acceptedTypes checks
|
||||
if (!json["noUpload"]) {
|
||||
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["noUpload"]) {
|
||||
if (json["uploadKey"] == null) {
|
||||
console.error("EUS uploadKey property does not exist!");
|
||||
performShutdownAfterValidation = true;
|
||||
} else {
|
||||
if (json["uploadKey"] == "") useUploadKey = false;
|
||||
}
|
||||
}
|
||||
// database checks
|
||||
if (!json["noUpload"]) {
|
||||
if (json["database"] == null) {
|
||||
console.error("EUS database properties do not exist!");
|
||||
performShutdownAfterValidation = true;
|
||||
|
@ -242,6 +226,7 @@ function validateConfig(json) {
|
|||
performShutdownAfterValidation = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if server needs to be shutdown
|
||||
if (performShutdownAfterValidation) {
|
||||
|
@ -251,31 +236,48 @@ function validateConfig(json) {
|
|||
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 {
|
||||
function fileExists(file) {
|
||||
return new Promise((resolve) => {
|
||||
fs.access(file, (error) => {
|
||||
resolve(!error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function sendFile(req, res, path, startTime) {
|
||||
// File does exist, send it back to the client.
|
||||
res.sendFile(`${__dirname}${BASE_PATH}/files${req.url}`);
|
||||
res.sendFile(path);
|
||||
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${node_modules.chalk.green("[200]")} ${req.url} ${Date.now() - startTime}ms`);
|
||||
}
|
||||
});
|
||||
|
||||
async function regularFile(req, res, urs = "", startTime) {
|
||||
if (req.url === "/") { urs = "/index.html" } else if (!req.url.includes(".") && !req.url.endsWith("/")) { urs = `${req.url}/index.html` } else { urs = req.url }
|
||||
|
||||
if (await fileExists(`${__dirname}${BASE_PATH}/files${urs}`)) {
|
||||
sendFile(req, res, `${__dirname}${BASE_PATH}/files${decodeURIComponent(urs)}`, startTime);
|
||||
} else {
|
||||
let isSuccess = false;
|
||||
if (!urs.endsWith(".html")) {
|
||||
if (await fileExists(`${__dirname}${BASE_PATH}/files${decodeURIComponent(urs)}.html`)) {
|
||||
isSuccess = true;
|
||||
sendFile(req, res, `${__dirname}${BASE_PATH}/files${decodeURIComponent(urs)}.html`, startTime);
|
||||
}
|
||||
} else if (urs.endsWith("/index.html")) {
|
||||
const path = `${__dirname}${BASE_PATH}/files${decodeURIComponent(req.url)}.html`;
|
||||
if (await fileExists(path)) {
|
||||
isSuccess = true;
|
||||
sendFile(req, res, path, startTime);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isSuccess) {
|
||||
// Doesn't exist, send a 404 to the client.
|
||||
await error404Page(res);
|
||||
global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${node_modules.chalk.red("[404]")} ${req.url} ${Date.now() - startTime}ms`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function imageFile(req, res, file, startTime = 0) {
|
||||
|
@ -283,16 +285,58 @@ function imageFile(req, res, file, startTime = 0) {
|
|||
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${node_modules.chalk.green("[200]")} (ImageReq) ${req.url} ${Date.now() - startTime}ms`);
|
||||
}
|
||||
|
||||
function error404Page(res) {
|
||||
async function doImageLookup(req, res, urs, startTime) {
|
||||
// Check if we even need to query the DB
|
||||
const dbAble = (!/[^0-9A-Za-z]/.test(urs)) && urs != "" && urs != "index" && (req.url.split("/").length == 2);
|
||||
|
||||
if (dbAble) {
|
||||
if (urs in existanceCache) {
|
||||
const cachedFile = existanceCache[urs];
|
||||
imageFile(req, res, `${cachedFile.fileHash}.${cachedFile.fileType}`, startTime);
|
||||
} else {
|
||||
// Try to get what we think is an image's details from the DB
|
||||
const dbEntry = await dbConnection.query(`SELECT hash, imageType FROM images WHERE imageId = ? LIMIT 1`, [urs]);
|
||||
|
||||
// There's an entry in the DB for this, send the file back.
|
||||
if (dbEntry != null) {
|
||||
existanceCache[urs] = {
|
||||
fileHash: dbEntry.hash,
|
||||
fileType: dbEntry.imageType
|
||||
};
|
||||
imageFile(req, res, `${dbEntry.hash}.${dbEntry.imageType}`, startTime);
|
||||
}
|
||||
// There's no entry, so treat this as a regular file.
|
||||
else regularFile(req, res, urs, startTime);
|
||||
}
|
||||
}
|
||||
// We can still serve files if they are not dbable
|
||||
// since we don't need to check the db
|
||||
else regularFile(req, res, urs, startTime);
|
||||
}
|
||||
|
||||
const PATH_404 = `${__dirname}${BASE_PATH}/files/404.html`;
|
||||
async function error404Page(res) {
|
||||
if (await fileExists(PATH_404)) {
|
||||
res.status(404).sendFile(PATH_404);
|
||||
} else {
|
||||
res.status(404).send("404!<hr>EUS");
|
||||
}
|
||||
}
|
||||
|
||||
let getHandler = (req, res, urs, startTime) => res.send("EUS is starting up, please try again in a few seconds.");
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
extras:async function() {
|
||||
if (!eusConfig["noUpload"]) {
|
||||
// Setup express to use busboy
|
||||
global.app.use(node_modules.busboy());
|
||||
startupFinished = true;
|
||||
getHandler = doImageLookup;
|
||||
cacheJSON = JSON.stringify(await cacheFilesAndSpace());
|
||||
cacheIsReady = true;
|
||||
} else {
|
||||
getHandler = regularFile;
|
||||
}
|
||||
},
|
||||
get:async function(req, res) {
|
||||
/*
|
||||
|
@ -311,49 +355,21 @@ module.exports = {
|
|||
|
||||
req.url = decodeURIComponent(req.url.split("?")[0]);
|
||||
|
||||
// The Funny Response
|
||||
// Auto 404 any attempted php / wp access and log it.
|
||||
// TODO: IP ban based on mass access. Very clearly a scan.
|
||||
if (req.url.includes(".php") || req.url.includes("/wp-")) {
|
||||
global.modules.consoleHelper.printWarn(emoji.globe_europe, `${req.method}: SUSSY ${req.headers["cf-connecting-ip"]} ${req.url}`);
|
||||
return res.status(418).send("Unfortunately for you this server does not use PHP or WordPress.<hr>EUS");
|
||||
return await error404Page(res);
|
||||
}
|
||||
|
||||
// Check if returned value is true.
|
||||
if (req.url.includes("/api/")) return handleAPI(req, res);
|
||||
if (req.url.includes("/api/")) {
|
||||
return handleAPI(req, res);
|
||||
}
|
||||
|
||||
// Register the time at the start of the request
|
||||
const startTime = Date.now();
|
||||
|
||||
// Get the requested image
|
||||
const urs = req.url.split("/")[1];
|
||||
// Check if we even need to query the DB
|
||||
const dbAble = (!/[^0-9A-Za-z]/.test(urs)) && urs != "" && urs != "index" && (req.url.split("/").length == 2);
|
||||
|
||||
if (dbAble) {
|
||||
if (urs in existanceCache) {
|
||||
const cachedFile = existanceCache[urs];
|
||||
imageFile(req, res, `${cachedFile.fileHash}.${cachedFile.fileType}`, startTime);
|
||||
} else {
|
||||
if (dbConnection.dbActive) {
|
||||
// Try to get what we think is an image's details from the DB
|
||||
const dbEntry = await dbConnection.query(`SELECT hash, imageType FROM images WHERE imageId = ? LIMIT 1`, [urs]);
|
||||
|
||||
// There's an entry in the DB for this, send the file back.
|
||||
if (dbEntry != null) {
|
||||
existanceCache[urs] = {
|
||||
fileHash: dbEntry.hash,
|
||||
fileType: dbEntry.imageType
|
||||
};
|
||||
imageFile(req, res, `${dbEntry.hash}.${dbEntry.imageType}`, startTime);
|
||||
}
|
||||
// There's no entry, so treat this as a regular file.
|
||||
else regularFile(req, res, urs, startTime);
|
||||
}
|
||||
else res.status(400).end("EUS is restarting, please try again in a few secs.");
|
||||
}
|
||||
}
|
||||
// We can still serve files if they are not dbable
|
||||
// since we don't need to check the db
|
||||
else regularFile(req, res, urs, startTime);
|
||||
getHandler(req, res, urs, startTime);
|
||||
},
|
||||
post:async function(req, res) {
|
||||
/*
|
||||
|
@ -361,21 +377,16 @@ module.exports = {
|
|||
res - Response from server
|
||||
*/
|
||||
|
||||
// Make sure the endpoint is /upload
|
||||
// If it isn't upload send an empty response
|
||||
if (eusConfig["noUpload"]) return res.end("");
|
||||
if (req.url != "/upload") return res.end("");
|
||||
|
||||
// Get time at the start of upload
|
||||
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
|
||||
if (useUploadKey && eusConfig["uploadKey"] != req.header("key")) return res.end("Incorrect key provided for upload");
|
||||
|
||||
const startTime = Date.now();
|
||||
// Pipe the request to busboy
|
||||
req.pipe(req.busboy);
|
||||
req.busboy.on('file', function (fieldname, file, info) {
|
||||
// Make a new file name
|
||||
fileOutName = node_modules.randomstring.generate(14);
|
||||
global.modules.consoleHelper.printInfo(emoji.fast_up, `${req.method}: Upload of ${fileOutName} started.`);
|
||||
// Check the file is within the accepted file types
|
||||
|
@ -389,7 +400,7 @@ module.exports = {
|
|||
thefe = fileType;
|
||||
} else {
|
||||
// File isn't accepted, send response back to client stating so.
|
||||
res.status(403).end("This file type isn't accepted currently.");
|
||||
res.status(403).end("This file type isn't accepted.");
|
||||
return;
|
||||
}
|
||||
// Create a write stream for the file
|
||||
|
@ -449,19 +460,6 @@ module.exports = {
|
|||
});
|
||||
}
|
||||
});
|
||||
|
||||
/*fstream.on('close', async () => {
|
||||
// Add this image to the database
|
||||
await dbConnection.query(`INSERT INTO images (id, imageId, imageType, hash, imageSize) VALUES (NULL, "${fileOutName}", "${thefe}", ${meter.bytes})`);
|
||||
|
||||
// Send URL of the uploaded image to the client
|
||||
res.end(eusConfig.baseURL + fileOutName);
|
||||
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: Upload of ${fileOutName} finished. Took ${Date.now() - startTime}ms`);
|
||||
|
||||
// Update cached files & space
|
||||
cacheJSON = JSON.stringify(await cacheFilesAndSpace());
|
||||
cacheIsReady = true;
|
||||
});*/
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
".mp4"
|
||||
],
|
||||
"uploadKey":"",
|
||||
"noUpload": false,
|
||||
"database": {
|
||||
"databaseAddress": "127.0.0.1",
|
||||
"databasePort": 3306,
|
||||
|
|
Loading…
Reference in a new issue