Support static mode and 404 override
This commit is contained in:
parent
c6b2d60821
commit
58f5afdfcb
2 changed files with 156 additions and 157 deletions
212
EUS.js
212
EUS.js
|
@ -12,11 +12,10 @@ let node_modules = {};
|
||||||
let eusConfig = {},
|
let eusConfig = {},
|
||||||
useUploadKey = true,
|
useUploadKey = true,
|
||||||
cacheJSON = "",
|
cacheJSON = "",
|
||||||
startupFinished = false,
|
|
||||||
timeSinceLastCache = Date.now();
|
timeSinceLastCache = Date.now();
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName, connectedCallback) {
|
constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName) {
|
||||||
this.connectionPool = node_modules["mysql2"].createPool({
|
this.connectionPool = node_modules["mysql2"].createPool({
|
||||||
connectionLimit: 128,
|
connectionLimit: 128,
|
||||||
host: databaseAddress,
|
host: databaseAddress,
|
||||||
|
@ -25,26 +24,6 @@ class Database {
|
||||||
password: databasePassword,
|
password: databasePassword,
|
||||||
database: databaseName
|
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) {
|
dataReceived(resolveCallback, data, limited = false) {
|
||||||
|
@ -139,11 +118,9 @@ function init() {
|
||||||
if (validateConfig(eusConfig)) console.log("[EUS] EUS config passed all checks");
|
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.
|
if (!eusConfig["noUpload"]) {
|
||||||
dbConnection = new Database(eusConfig["database"]["databaseAddress"], eusConfig["database"]["databasePort"], eusConfig["database"]["databaseUsername"], eusConfig["database"]["databasePassword"], eusConfig["database"]["databaseName"], async () => {
|
dbConnection = new Database(eusConfig["database"]["databaseAddress"], eusConfig["database"]["databasePort"], eusConfig["database"]["databaseUsername"], eusConfig["database"]["databasePassword"], eusConfig["database"]["databaseName"]);
|
||||||
cacheJSON = JSON.stringify(await cacheFilesAndSpace());
|
}
|
||||||
cacheIsReady = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log("[EUS] Finished loading.");
|
console.log("[EUS] Finished loading.");
|
||||||
}
|
}
|
||||||
|
@ -184,6 +161,7 @@ function cacheFilesAndSpace() {
|
||||||
function validateConfig(json) {
|
function validateConfig(json) {
|
||||||
let performShutdownAfterValidation = false;
|
let performShutdownAfterValidation = false;
|
||||||
// URL Tests
|
// URL Tests
|
||||||
|
if (!json["noUpload"]) {
|
||||||
if (json["baseURL"] == null) {
|
if (json["baseURL"] == null) {
|
||||||
console.error("EUS baseURL property does not exist!");
|
console.error("EUS baseURL property does not exist!");
|
||||||
performShutdownAfterValidation = true;
|
performShutdownAfterValidation = true;
|
||||||
|
@ -197,21 +175,27 @@ function validateConfig(json) {
|
||||||
if (json["baseURL"] != "http://" || json["baseURL"] != "https://") console.warn("EUS baseURL property is possibly invalid!");
|
if (json["baseURL"] != "http://" || json["baseURL"] != "https://") console.warn("EUS baseURL property is possibly invalid!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// acceptedTypes checks
|
// acceptedTypes checks
|
||||||
|
if (!json["noUpload"]) {
|
||||||
if (json["acceptedTypes"] == null) {
|
if (json["acceptedTypes"] == null) {
|
||||||
console.error("EUS acceptedTypes list does not exist!");
|
console.error("EUS acceptedTypes list does not exist!");
|
||||||
performShutdownAfterValidation = true;
|
performShutdownAfterValidation = true;
|
||||||
} else {
|
} else {
|
||||||
if (json["acceptedTypes"].length < 1) console.warn("EUS acceptedTypes array has no extentions in it, users will not be able to upload images!");
|
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
|
// uploadKey checks
|
||||||
|
if (!json["noUpload"]) {
|
||||||
if (json["uploadKey"] == null) {
|
if (json["uploadKey"] == null) {
|
||||||
console.error("EUS uploadKey property does not exist!");
|
console.error("EUS uploadKey property does not exist!");
|
||||||
performShutdownAfterValidation = true;
|
performShutdownAfterValidation = true;
|
||||||
} else {
|
} else {
|
||||||
if (json["uploadKey"] == "") useUploadKey = false;
|
if (json["uploadKey"] == "") useUploadKey = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// database checks
|
// database checks
|
||||||
|
if (!json["noUpload"]) {
|
||||||
if (json["database"] == null) {
|
if (json["database"] == null) {
|
||||||
console.error("EUS database properties do not exist!");
|
console.error("EUS database properties do not exist!");
|
||||||
performShutdownAfterValidation = true;
|
performShutdownAfterValidation = true;
|
||||||
|
@ -242,6 +226,7 @@ function validateConfig(json) {
|
||||||
performShutdownAfterValidation = true;
|
performShutdownAfterValidation = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if server needs to be shutdown
|
// Check if server needs to be shutdown
|
||||||
if (performShutdownAfterValidation) {
|
if (performShutdownAfterValidation) {
|
||||||
|
@ -251,31 +236,48 @@ function validateConfig(json) {
|
||||||
else return true;
|
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 = {};
|
let existanceCache = {};
|
||||||
|
|
||||||
function regularFile(req, res, urs = "", startTime = 0) {
|
function fileExists(file) {
|
||||||
if (req.url === "/") { urs = "/index.html" } else { urs = req.url }
|
return new Promise((resolve) => {
|
||||||
fs.access(`${__dirname}${BASE_PATH}/files${urs}`, (error) => {
|
fs.access(file, (error) => {
|
||||||
if (error) {
|
resolve(!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 sendFile(req, res, path, startTime) {
|
||||||
|
// File does exist, send it back to the client.
|
||||||
|
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) {
|
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`);
|
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) {
|
||||||
res.status(404).send("404!<hr>EUS");
|
// 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 = {
|
module.exports = {
|
||||||
init: init,
|
init: init,
|
||||||
extras:async function() {
|
extras:async function() {
|
||||||
|
if (!eusConfig["noUpload"]) {
|
||||||
// Setup express to use busboy
|
// Setup express to use busboy
|
||||||
global.app.use(node_modules.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) {
|
get:async function(req, res) {
|
||||||
/*
|
/*
|
||||||
|
@ -311,49 +355,21 @@ module.exports = {
|
||||||
|
|
||||||
req.url = decodeURIComponent(req.url.split("?")[0]);
|
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-")) {
|
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}`);
|
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/")) {
|
||||||
if (req.url.includes("/api/")) return handleAPI(req, res);
|
return handleAPI(req, res);
|
||||||
|
}
|
||||||
|
|
||||||
// Register the time at the start of the request
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
|
||||||
// Get the requested image
|
|
||||||
const urs = req.url.split("/")[1];
|
const urs = req.url.split("/")[1];
|
||||||
// Check if we even need to query the DB
|
getHandler(req, res, urs, startTime);
|
||||||
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);
|
|
||||||
},
|
},
|
||||||
post:async function(req, res) {
|
post:async function(req, res) {
|
||||||
/*
|
/*
|
||||||
|
@ -361,21 +377,16 @@ module.exports = {
|
||||||
res - Response from server
|
res - Response from server
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Make sure the endpoint is /upload
|
if (eusConfig["noUpload"]) return res.end("");
|
||||||
// If it isn't upload send an empty response
|
|
||||||
if (req.url != "/upload") return res.end("");
|
if (req.url != "/upload") return res.end("");
|
||||||
|
|
||||||
// Get time at the start of upload
|
|
||||||
|
|
||||||
res.header("Access-Control-Allow-Origin", "*");
|
res.header("Access-Control-Allow-Origin", "*");
|
||||||
|
|
||||||
if (useUploadKey && eusConfig["uploadKey"] != req.header("key")) return res.end("Incorrect key provided for upload");
|
if (useUploadKey && eusConfig["uploadKey"] != req.header("key")) return res.end("Incorrect key provided for upload");
|
||||||
|
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
// Pipe the request to busboy
|
|
||||||
req.pipe(req.busboy);
|
req.pipe(req.busboy);
|
||||||
req.busboy.on('file', function (fieldname, file, info) {
|
req.busboy.on('file', function (fieldname, file, info) {
|
||||||
// Make a new file name
|
|
||||||
fileOutName = node_modules.randomstring.generate(14);
|
fileOutName = node_modules.randomstring.generate(14);
|
||||||
global.modules.consoleHelper.printInfo(emoji.fast_up, `${req.method}: Upload of ${fileOutName} started.`);
|
global.modules.consoleHelper.printInfo(emoji.fast_up, `${req.method}: Upload of ${fileOutName} started.`);
|
||||||
// Check the file is within the accepted file types
|
// Check the file is within the accepted file types
|
||||||
|
@ -389,7 +400,7 @@ module.exports = {
|
||||||
thefe = fileType;
|
thefe = fileType;
|
||||||
} else {
|
} else {
|
||||||
// File isn't accepted, send response back to client stating so.
|
// 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;
|
return;
|
||||||
}
|
}
|
||||||
// Create a write stream for the file
|
// 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"
|
".mp4"
|
||||||
],
|
],
|
||||||
"uploadKey":"",
|
"uploadKey":"",
|
||||||
|
"noUpload": false,
|
||||||
"database": {
|
"database": {
|
||||||
"databaseAddress": "127.0.0.1",
|
"databaseAddress": "127.0.0.1",
|
||||||
"databasePort": 3306,
|
"databasePort": 3306,
|
||||||
|
|
Loading…
Reference in a new issue