const chalk = require('chalk');
const mime = require('mime');
const FileType = require('file-type');
module.exports = (req, res, post, files) => {
try {
res.setHeader("X-Electrode-WorkerID", cluster.worker.id)
res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.setHeader('Expires', '-1');
res.setHeader('Pragma', 'no-cache');
if (req.url.includes('../')) {
console.log(chalk.gray(cluster.worker.id + " ") + chalk.blue("warn:") + " working around exploit");
res.writeHead(301, { 'Location': '/index.php' });
res.end();
} else if (req.url.trim() == '/' || req.url.trim() == '//' || req.url.trim() == '') {
console.log(chalk.gray(cluster.worker.id + " ") + chalk.blue("warn:") + " working around redirection trap");
res.writeHead(301, { 'Location': '/index.php' });
res.end();
} else {
console.log(chalk.gray(cluster.worker.id + " ") + chalk.green("verb:") + " " + req.method + " " + req.url + " - HTTP/" + req.httpVersion + " - " + req.connection.remoteAddress);
filename = core.finder(req.url);
if (filename == null) {
res.writeHead(404, { 'Content-Type': 'text/html' });
console.log(chalk.gray(cluster.worker.id + " ") + chalk.yellow("warn:") + " not found: " + req.url);
res.write('
ENOTFOUND - File not foundENOTFOUND
This file couldn\'t be found on the server
' + config.product.name + ' version ' + version + '');
res.end();
} else {
if (require('fs').lstatSync(filename).isDirectory()) {
res.writeHead(403, { 'Content-Type': 'text/html' });
console.log(chalk.gray(cluster.worker.id + " ") + chalk.yellow("warn:") + " is directory: " + req.url);
res.write('EACCES - Permission deniedEACCES
Permission to access this file was denied by the server configuration
' + config.product.name + ' version ' + version + '');
res.end();
} else {
if (core.access(filename)) {
if (!filename.endsWith(".php")) {
res.setHeader("Cache-Control", "no-cache")
require('fs').readFile(filename, (error, file) => {
if (error) {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.write('' + error.code + ' - Internal error' + error.code + '
An internal server error ocurred while trying to give back the file
' + config.product.name + ' version ' + version + '');
res.end();
console.log(chalk.gray(cluster.worker.id + " ") + chalk.red("error:") + " while loading file: " + error.message);
} else {
FileType.fromFile(filename).then((type) => {
res.writeHead(200, { 'Content-Type': type + "", 'Content-Size': file.toString().length });
res.end(file);
});
}
})
} else {
php.runtime(php.cache(req, res, filename, post, files)).then((phpc) => {
if (phpc.error == null) {
if (require('fs').existsSync("./cache/HAD_" + phpc.id + ".json")) {
try {
headers = JSON.parse(require('fs').readFileSync("./cache/HAD_" + phpc.id + ".json"));
hlist = {};
headers.forEach(h => {
p = h.split(":");
n = p[0];
p.shift();
v = p.join(":");
hlist[n.toLowerCase()] = v;
})
if (typeof hlist["content-type"] == "undefined") {
hlist["content-type"] = "text/html";
}
if (typeof hlist["location"] != "undefined") {
res.writeHead(301, hlist);
} else {
res.writeHead(200, hlist);
}
} catch (e) {
console.log(chalk.gray(cluster.worker.id + " ") + chalk.red("error:") + " while loading php headers: " + e.message);
console.log(e.stack);
res.writeHead(500, { 'Content-Type': 'text/html' });
res.write('' + e.name + ' - post-PHP error' + e.name + '
Unable to process PHP headers
' + config.product.name + ' version ' + version + '');
res.end();
}
}
res.write(phpc.content);
res.end();
core.headClean();
} else {
res.writeHead(500, { 'Content-Type': 'text/html' });
console.log(chalk.gray(cluster.worker.id + " ") + chalk.red("error:") + " while running php: " + phpc.error.message);
console.log(phpc.error.stack);
res.write('' + phpc.error.name + ' - PHP error' + phpc.error.name + '
The PHP integration didn\'t fulfill the request correctly:
' + phpc.stderr + '
' + phpc.error.message.split("\n").join("
") + '
' + config.product.name + ' version ' + version + '');
res.end();
core.headClean();
}
});
}
} else {
res.writeHead(403, { 'Content-Type': 'text/html' });
console.log(chalk.gray(cluster.worker.id + " ") + chalk.yellow("warn:") + " denied by .htaccess: " + req.url);
res.write('EACCES - Permission deniedEACCES
Permission to access this file was denied by the server configuration
' + config.product.name + ' version ' + version + '');
res.end();
}
}
}
}
} catch (error) {
res.writeHead(500, { 'Content-Type': 'text/html' });
res.write('' + error.name + ' - Internal error' + error.name + '
An internal server error ocurred while trying to give back the file
' + config.product.name + ' version ' + version + '');
console.log(chalk.gray(cluster.worker.id + " ") + chalk.red("error:") + " " + error.name + ": " + error.message);
console.log(error.stack);
res.end();
}
}