2019-11-23 21:02:50 +00:00
|
|
|
const fs = require("fs"),
|
2020-03-26 15:58:25 +00:00
|
|
|
config = require("../config/config.json"),
|
|
|
|
chalk = require("chalk"),
|
|
|
|
busboy = require("connect-busboy"),
|
|
|
|
randomstring = require("randomstring"),
|
|
|
|
getSize = require('get-folder-size'),
|
|
|
|
emoji = require("../misc/emoji_list.json");
|
2019-11-23 21:02:50 +00:00
|
|
|
|
|
|
|
// Defines the function of this module
|
|
|
|
const MODULE_FUNCTION = "handle_requests",
|
|
|
|
|
2020-03-26 15:58:25 +00:00
|
|
|
// Base path for module folder creation and navigation
|
|
|
|
BASE_PATH = "/EUS";
|
2019-11-23 21:02:50 +00:00
|
|
|
|
2019-12-22 07:51:04 +00:00
|
|
|
let eusConfig = {},
|
2020-03-26 15:58:25 +00:00
|
|
|
image_json = {},
|
|
|
|
d = new Date(),
|
|
|
|
startTime,
|
2020-05-21 15:10:43 +01:00
|
|
|
endTime,
|
|
|
|
useUploadKey = true;
|
2019-11-23 21:02:50 +00:00
|
|
|
|
|
|
|
// Only ran on startup so using sync functions is fine
|
2019-12-22 07:51:04 +00:00
|
|
|
// Makes the folder for files of the module
|
2019-11-23 21:02:50 +00:00
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH)) {
|
|
|
|
fs.mkdirSync(__dirname + BASE_PATH);
|
|
|
|
console.log(`[EUS] Made EUS module folder`);
|
|
|
|
}
|
2019-12-22 07:51:04 +00:00
|
|
|
// Makes the folder for frontend files
|
2019-11-23 21:02:50 +00:00
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH + "/files")) {
|
|
|
|
fs.mkdirSync(__dirname + BASE_PATH + "/files");
|
|
|
|
console.log(`[EUS] Made EUS web files folder`);
|
|
|
|
}
|
2019-12-22 07:51:04 +00:00
|
|
|
// Makes the folder for images
|
2019-11-23 21:02:50 +00:00
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH + "/i")) {
|
|
|
|
fs.mkdirSync(__dirname + BASE_PATH + "/i");
|
|
|
|
console.log(`[EUS] Made EUS images folder`);
|
|
|
|
}
|
2019-12-22 07:51:04 +00:00
|
|
|
// Makes the image-type file
|
2020-06-15 12:04:17 +01:00
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH + "/image-type.json")) {
|
2020-06-15 08:16:30 +01:00
|
|
|
// Doesn't exist, create it.
|
2020-06-15 08:30:16 +01:00
|
|
|
fs.writeFileSync(`${__dirname}${BASE_PATH}/image-type.json`, '{}');
|
2020-06-15 12:04:17 +01:00
|
|
|
console.log("[EUS] Made EUS image-type File!");
|
2020-06-15 08:30:16 +01:00
|
|
|
// File has been created, load it.
|
|
|
|
image_json = require(`${__dirname}${BASE_PATH}/image-type.json`);
|
2020-06-15 08:16:30 +01:00
|
|
|
} else {
|
|
|
|
// File already exists, load it.
|
|
|
|
image_json = require(`${__dirname}${BASE_PATH}/image-type.json`);
|
|
|
|
}
|
|
|
|
|
2019-12-22 07:51:04 +00:00
|
|
|
// Makes the config file
|
2020-06-15 12:04:17 +01:00
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) {
|
2020-06-15 08:16:30 +01:00
|
|
|
// Config doesn't exist, make it.
|
2020-06-15 08:30:16 +01:00
|
|
|
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}');
|
2020-06-15 12:04:17 +01:00
|
|
|
console.log("[EUS] Made EUS config File!");
|
2020-06-15 08:30:16 +01:00
|
|
|
console.log("[EUS] Please edit the EUS Config file before restarting.");
|
|
|
|
// Config has been made, close framework.
|
|
|
|
process.exit(0);
|
2020-06-15 08:16:30 +01:00
|
|
|
} else {
|
|
|
|
eusConfig = require(`${__dirname}${BASE_PATH}/config.json`);
|
|
|
|
if (validateConfig(eusConfig)) console.log("[EUS] EUS config passed all checks");
|
|
|
|
}
|
2019-12-22 07:51:04 +00:00
|
|
|
|
2020-05-21 15:10:43 +01:00
|
|
|
function validateConfig(json) {
|
|
|
|
let performShutdownAfterValidation = false;
|
|
|
|
// URL Tests
|
|
|
|
if (json["baseURL"] == null) {
|
2020-06-15 08:16:30 +01:00
|
|
|
console.error("EUS baseURL property does not exist!");
|
2020-05-21 15:10:43 +01:00
|
|
|
performShutdownAfterValidation = true;
|
|
|
|
} else {
|
|
|
|
if (json["baseURL"] == "")
|
2020-06-15 08:16:30 +01:00
|
|
|
console.warn("EUS baseURL property is blank");
|
2020-05-21 15:10:43 +01:00
|
|
|
const bURL = `${json["baseURL"]}`.split("");
|
|
|
|
if (bURL.length > 1) {
|
2020-06-15 08:16:30 +01:00
|
|
|
if (bURL[bURL.length-1] != "/") console.warn("EUS baseURL property doesn't have a / at the end, this can lead to unpredictable results!");
|
2020-05-21 15:10:43 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-06-15 08:16:30 +01:00
|
|
|
if (json["baseURL"] != "http://" || json["baseURL"] != "https://") console.warn("EUS baseURL property is possibly invalid!");
|
2020-05-21 15:10:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// acceptedTypes checks
|
|
|
|
if (json["acceptedTypes"] == null) {
|
2020-06-15 08:16:30 +01:00
|
|
|
console.error("EUS acceptedTypes array does not exist!");
|
2020-05-21 15:10:43 +01:00
|
|
|
performShutdownAfterValidation = true;
|
|
|
|
} else {
|
2020-06-15 08:16:30 +01:00
|
|
|
if (json["acceptedTypes"].length < 1) console.warn("EUS acceptedTypes array has no extentions in it, users will not be able to upload images!");
|
2020-05-21 15:10:43 +01:00
|
|
|
}
|
|
|
|
// uploadKey checks
|
|
|
|
if (json["uploadKey"] == null) {
|
2020-06-15 08:16:30 +01:00
|
|
|
console.error("EUS uploadKey property does not exist!");
|
2020-05-21 15:10:43 +01:00
|
|
|
performShutdownAfterValidation = true;
|
|
|
|
} else {
|
|
|
|
if (json["uploadKey"] == "") useUploadKey = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if server needs to be shutdown
|
2020-06-15 12:04:17 +01:00
|
|
|
if (performShutdownAfterValidation) {
|
|
|
|
console.error("EUS config properties are missing, refer to docs for more details (https://docs.ethanus.ml)");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-05-21 15:10:43 +01:00
|
|
|
else return true;
|
|
|
|
}
|
|
|
|
|
2019-11-23 21:02:50 +00:00
|
|
|
module.exports = {
|
|
|
|
extras:function() {
|
2020-01-04 10:44:20 +00:00
|
|
|
// Setup express to use busboy
|
2019-11-23 21:02:50 +00:00
|
|
|
global.app.use(busboy());
|
|
|
|
},
|
|
|
|
get:function(req, res) {
|
|
|
|
/*
|
|
|
|
req - Request from client
|
|
|
|
res - Response from server
|
|
|
|
*/
|
|
|
|
|
2020-05-05 17:40:37 +01:00
|
|
|
// Set some headers
|
|
|
|
res.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
|
|
|
|
res.set("X-XSS-Protection", "1; mode=block");
|
|
|
|
res.set("Feature-Policy", "fullscreen 'none'");
|
|
|
|
res.set("Referrer-Policy", "strict-origin-when-cross-origin");
|
|
|
|
res.set("Content-Security-Policy", "block-all-mixed-content;frame-ancestors 'self'");
|
|
|
|
res.set("X-Frame-Options", "SAMEORIGIN");
|
|
|
|
res.set("X-Content-Type-Options", "nosniff");
|
|
|
|
|
2020-03-26 15:40:38 +00:00
|
|
|
// Check if returned value is true.
|
2020-05-21 13:15:39 +01:00
|
|
|
if (req.url.includes("/api/")) return handleAPI(req, res);
|
|
|
|
|
|
|
|
// Register the time at the start of the request
|
|
|
|
d = new Date();
|
|
|
|
startTime = d.getTime();
|
|
|
|
// Get the requested image
|
|
|
|
let urs = ""+req.url; urs = urs.split("/")[1];
|
|
|
|
// Get the file type of the image from image_json and make sure it exists
|
|
|
|
fs.access(__dirname + BASE_PATH + "/i/"+urs+image_json[urs], error => {
|
|
|
|
if (error) {
|
|
|
|
// Doesn't exist, handle request normaly
|
2020-06-15 11:48:03 +01:00
|
|
|
if (req.url === "/") { urs = "/index.html" } else { urs = req.url }
|
2020-05-21 13:15:39 +01:00
|
|
|
fs.access(__dirname + BASE_PATH + "/files"+urs, error => {
|
|
|
|
if (error) {
|
|
|
|
// Doesn't exist, send a 404 to the client.
|
|
|
|
res.status(404).end("404!");
|
|
|
|
d = new Date();
|
|
|
|
endTime = d.getTime();
|
|
|
|
global.modules.consoleHelper.printInfo(emoji.cross, `${req.method}: ${chalk.red("[404]")} ${req.url} ${endTime - startTime}ms`);
|
|
|
|
} else {
|
|
|
|
// File does exist, send it back to the client.
|
|
|
|
res.sendFile(__dirname + BASE_PATH + "/files"+req.url);
|
|
|
|
d = new Date();
|
|
|
|
endTime = d.getTime();
|
|
|
|
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Image does exist, send it back.
|
|
|
|
res.sendFile(__dirname + BASE_PATH + "/i/"+urs+image_json[urs]);
|
|
|
|
d = new Date();
|
|
|
|
endTime = d.getTime();
|
|
|
|
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: ${chalk.green("[200]")} ${req.url} ${endTime - startTime}ms`);
|
|
|
|
}
|
|
|
|
});
|
2019-11-23 21:02:50 +00:00
|
|
|
},
|
|
|
|
post:function(req, res) {
|
|
|
|
/*
|
|
|
|
req - Request from client
|
|
|
|
res - Response from server
|
|
|
|
*/
|
|
|
|
|
2020-01-04 10:44:20 +00:00
|
|
|
// Make sure the endpoint is /upload
|
|
|
|
// If it isn't upload send an empty response
|
2020-03-30 11:52:44 +01:00
|
|
|
if (req.url != "/upload") return res.end("");
|
2019-11-23 21:02:50 +00:00
|
|
|
|
2020-01-04 10:44:20 +00:00
|
|
|
// Get time at the start of upload
|
2020-05-21 15:23:57 +01:00
|
|
|
|
2020-06-15 12:04:17 +01:00
|
|
|
if (useUploadKey && eusConfig["uploadKey"] != req.header("key")) return res.end("Incorrect key provided for upload");
|
2020-05-21 15:23:57 +01:00
|
|
|
|
2019-11-23 21:02:50 +00:00
|
|
|
d = new Date(); startTime = d.getTime();
|
|
|
|
var fstream;
|
|
|
|
var thefe;
|
2020-01-04 10:44:20 +00:00
|
|
|
// Pipe the request to busboy
|
2019-11-23 21:02:50 +00:00
|
|
|
req.pipe(req.busboy);
|
|
|
|
req.busboy.on('file', function (fieldname, file, filename) {
|
2020-01-04 10:44:20 +00:00
|
|
|
// Get the image-type json
|
2019-11-23 21:02:50 +00:00
|
|
|
image_json = require(`${__dirname}${BASE_PATH}/image-type.json`);
|
2020-01-04 10:44:20 +00:00
|
|
|
// Make a new file name
|
2019-11-23 21:02:50 +00:00
|
|
|
fileOutName = randomstring.generate(14);
|
2020-01-04 10:44:20 +00:00
|
|
|
global.modules.consoleHelper.printInfo(emoji.fast_up, `${req.method}: Upload of ${fileOutName} started.`);
|
|
|
|
// Check the file is within the accepted file types
|
2019-12-22 07:51:04 +00:00
|
|
|
if (eusConfig.acceptedTypes.includes(`.${filename.split(".")[filename.split(".").length-1]}`)) {
|
2020-01-04 10:44:20 +00:00
|
|
|
// File is accepted, set the extention of the file in thefe for later use.
|
2019-11-23 21:02:50 +00:00
|
|
|
thefe = `.${filename.split(".")[filename.split(".").length-1]}`;
|
|
|
|
} else {
|
2020-01-04 10:44:20 +00:00
|
|
|
// File isn't accepted, send response back to client stating so.
|
2019-11-23 21:02:50 +00:00
|
|
|
res.end("This file type isn't accepted currently.");
|
|
|
|
return;
|
|
|
|
}
|
2020-01-04 10:44:20 +00:00
|
|
|
// Create a write stream for the file
|
2019-11-23 21:02:50 +00:00
|
|
|
fstream = fs.createWriteStream(__dirname + BASE_PATH + "/i/" + fileOutName + thefe);
|
|
|
|
file.pipe(fstream);
|
|
|
|
fstream.on('close', function () {
|
2020-01-04 10:44:20 +00:00
|
|
|
// Get the time at the end of the upload
|
|
|
|
d = new Date(); endTime = d.getTime();
|
|
|
|
// Add image file type to the image_json array
|
2019-11-23 21:02:50 +00:00
|
|
|
image_json[fileOutName] = `.${filename.split(".")[filename.split(".").length-1]}`;
|
2020-01-04 10:44:20 +00:00
|
|
|
// Save image_json array to the file
|
2019-11-23 21:02:50 +00:00
|
|
|
fs.writeFile(`${__dirname}${BASE_PATH}/image-type.json`, JSON.stringify(image_json), function(err) {
|
|
|
|
if (err) throw err;
|
2020-01-04 10:44:20 +00:00
|
|
|
global.modules.consoleHelper.printInfo(emoji.heavy_check, `${req.method}: Upload of ${fileOutName} finished. Took ${endTime - startTime}ms`);
|
|
|
|
// Send URL of the uploaded image to the client
|
2020-02-20 07:41:04 +00:00
|
|
|
res.end(eusConfig.baseURL+""+fileOutName);
|
2019-11-23 21:02:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:40:38 +00:00
|
|
|
function handleAPI(req, res) {
|
2020-06-15 12:10:06 +01:00
|
|
|
let jsonaa = {}, filesaa = 0, spaceaa = 0;
|
2020-06-15 12:04:17 +01:00
|
|
|
switch (req.url.split("?")[0]) {
|
|
|
|
// Status check to see the onlint status of EUS
|
|
|
|
// Used by ESL to make sure EUS is online
|
|
|
|
case "/api/get-server-status":
|
|
|
|
return res.end('{ "status":1, "version":"'+global.internals.version+'" }');
|
|
|
|
|
|
|
|
/* Stats api endpoint
|
|
|
|
Query inputs
|
|
|
|
f : Values [0,1]
|
|
|
|
s : Values [0,1]
|
|
|
|
*/
|
|
|
|
case "/api/get-stats":
|
2020-06-15 12:10:06 +01:00
|
|
|
filesaa = req.query["f"];
|
2020-06-15 12:04:17 +01:00
|
|
|
spaceaa = req.query["s"];
|
2020-06-15 12:10:06 +01:00
|
|
|
jsonaa = {};
|
2020-06-15 12:04:17 +01:00
|
|
|
// If total files is asked for
|
|
|
|
if (filesaa == 1) {
|
|
|
|
let total = 0;
|
|
|
|
jsonaa["files"] = {};
|
|
|
|
// Add each accepted file type to the json
|
|
|
|
for (var i2 = 0; i2 < eusConfig.acceptedTypes.length; i2++) {
|
|
|
|
jsonaa["files"][`${eusConfig.acceptedTypes[i2]}`.replace(".", "")] = 0;
|
|
|
|
}
|
|
|
|
// Read all files from the images directory
|
|
|
|
fs.readdir(__dirname + BASE_PATH + "/i", (err, files) => {
|
|
|
|
if (err) throw err;
|
|
|
|
// Loop through all files
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
// Loop through all accepted file types to check for a match
|
|
|
|
for (var i1 = 0; i1 < eusConfig.acceptedTypes.length; i1++) {
|
|
|
|
const jsudfg = files[i].split(".");
|
|
|
|
if (`.${jsudfg[jsudfg.length-1]}` == eusConfig.acceptedTypes[i1]) {
|
|
|
|
// There is a match! Add it to the json
|
|
|
|
jsonaa["files"][eusConfig.acceptedTypes[i1].replace(".", "")]++;
|
|
|
|
// Also increase the total
|
|
|
|
total++;
|
|
|
|
}
|
2020-03-26 15:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-15 12:04:17 +01:00
|
|
|
// Set the total in the json to the calculated total value
|
|
|
|
jsonaa["files"]["total"] = total;
|
|
|
|
|
|
|
|
// If getting the space used on the server isn't required send the json
|
|
|
|
if (spaceaa != 1) return res.end(JSON.stringify(jsonaa));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Getting space is required
|
|
|
|
if (spaceaa == 1) {
|
|
|
|
jsonaa["space"] = {};
|
|
|
|
// Get the space used on the disk
|
|
|
|
getSize(__dirname + BASE_PATH + "/i", (err, size) => {
|
|
|
|
if (err) throw err;
|
|
|
|
// Calculate in different units the space taken up on disk
|
|
|
|
let sizeOfFolder = (size / 1024 / 1024);
|
|
|
|
jsonaa["space"]["mb"] = sizeOfFolder;
|
|
|
|
sizeOfFolder = (size / 1024 / 1024 / 1024);
|
|
|
|
jsonaa["space"]["gb"] = sizeOfFolder;
|
|
|
|
sizeOfFolder = (size / 1024 / 1024 / 1024).toFixed(2);
|
|
|
|
jsonaa["space"]["string"] = `${sizeOfFolder} GB`;
|
|
|
|
// Send the json to the requesting client
|
|
|
|
return res.end(JSON.stringify(jsonaa));
|
|
|
|
});
|
|
|
|
}
|
2020-03-26 15:58:25 +00:00
|
|
|
|
2020-06-15 12:04:17 +01:00
|
|
|
if (filesaa != 1 && spaceaa != 1) return res.end("Please add f and or s to your queries to get the files and space");
|
|
|
|
break;
|
2020-03-26 15:40:38 +00:00
|
|
|
|
2020-06-15 12:04:17 +01:00
|
|
|
// Information API
|
|
|
|
case "/api/get-info":
|
2020-06-15 12:10:06 +01:00
|
|
|
return res.end(JSON.stringify({
|
2020-06-15 12:04:17 +01:00
|
|
|
version: global.internals.version,
|
|
|
|
instance: config["server"]["instance_type"]
|
2020-06-15 12:10:06 +01:00
|
|
|
}));
|
2020-03-26 15:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 21:02:50 +00:00
|
|
|
module.exports.MOD_FUNC = MODULE_FUNCTION;
|