mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
272 lines
7.4 KiB
JavaScript
Executable file
272 lines
7.4 KiB
JavaScript
Executable file
/* ----------------------------------------------------------------------------
|
|
|
|
Question Server
|
|
GitLab: <https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server>
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
var express = require('express');
|
|
var bodyParser = require('body-parser');
|
|
var busboy = require('connect-busboy');
|
|
var fs = require('fs');
|
|
var app = express();
|
|
|
|
var logger = require('./logger.js');
|
|
var utils = require('./utils.js');
|
|
var actions = require('./actions.js');
|
|
var stat = require('./stat.js');
|
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
const recivedFiles = "public/recivedfiles";
|
|
const publicFile = "public/data/public";
|
|
const staticFile = "public/data/static";
|
|
const countFile = "public/data/count";
|
|
const manFile = "public/man.html";
|
|
const simpOutFile = "public/simplified";
|
|
const inputFile = "stats/inputs";
|
|
const msgFile = "stats/msgs";
|
|
const logFile = "stats/logs";
|
|
const port = 8080;
|
|
|
|
var highlights = ["public", "static", "manual", "isgetting", "postfeedback",
|
|
"postquestions"
|
|
];
|
|
|
|
try {
|
|
var stdin = process.openStdin();
|
|
stdin.addListener("data", function(d) {
|
|
var input = d.toString().trim();
|
|
|
|
if (input == "hello")
|
|
console.log("hello, messages: \n" + newMessages);
|
|
if (input == "clear" || input == "c")
|
|
console.clear();
|
|
|
|
});
|
|
} catch (e) {
|
|
console.log("Failed to open STDIN");
|
|
console.log(e);
|
|
}
|
|
|
|
var newMessages = "";
|
|
|
|
app.set('view engine', 'ejs');
|
|
app.use(function(req, res, next) {
|
|
res.on('finish', function() {
|
|
if (res.statusCode != 404 && req.connection.remoteAddress != "::1"){
|
|
// Log(req);
|
|
}
|
|
else
|
|
Log(req, true);
|
|
});
|
|
stat.Inc(req.url);
|
|
next();
|
|
});
|
|
app.use(express.static('public'));
|
|
app.use(busboy({
|
|
limits: {
|
|
fileSize: 10 * 1024 * 1024
|
|
}
|
|
}));
|
|
app.use(bodyParser.json());
|
|
app.use(bodyParser.urlencoded({
|
|
limit: '5mb',
|
|
extended: true
|
|
}));
|
|
app.use(bodyParser.json({
|
|
limit: '5mb'
|
|
}));
|
|
|
|
app.get('/', function(req, res) {
|
|
res.render('main', {
|
|
pdata: utils.ReadFile(publicFile),
|
|
sdata: utils.ReadFile(staticFile)
|
|
});
|
|
res.end();
|
|
});
|
|
|
|
app.get('/manual', function(req, res) {
|
|
res.write(utils.ReadFile(manFile));
|
|
res.end();
|
|
});
|
|
|
|
app.get('/public', function(req, res) {
|
|
res.write(utils.ReadFile(publicFile));
|
|
res.end();
|
|
});
|
|
|
|
app.get('/static', function(req, res) {
|
|
res.write(utils.ReadFile(staticFile));
|
|
res.end();
|
|
});
|
|
|
|
app.post('/postquestions', function(req, res) {
|
|
var log = "Questions posted! ";
|
|
if (req.body.data_field == undefined) {
|
|
res.render('submitted', {
|
|
log: [],
|
|
msg: "Nincsen beküldött adat!"
|
|
});
|
|
log += "Sent data is undefined! ";
|
|
|
|
} else {
|
|
try {
|
|
utils.AppendToFile("\n" + logger.GetDateString() + "\n" + req.body.data_field, inputFile);
|
|
} catch (e) {
|
|
console.log(e);
|
|
}
|
|
var r = actions.CheckData(req.body.data_field);
|
|
if (r.count > 0) {
|
|
var c = utils.ReadFile(countFile);
|
|
if (parseInt(c) >= r.count) {
|
|
res.render('submitted', {
|
|
log: r.log,
|
|
msg: "Amit beküldtél kevesebb, vagy ugyanannyi kérdést tartalmaz! Az új kérdéseidet az eredeti végére másold! " +
|
|
"Új: " + r.count + " vs régi: " + c
|
|
});
|
|
log += "Sent data contains less questions: " + r.count + ", ";
|
|
|
|
} else {
|
|
res.render('submitted', {
|
|
log: r.log,
|
|
msg: "Sikeres frissítés! + " + (r.count - parseInt(c)) +
|
|
" új kérdés beküldve. néhány üzenet:"
|
|
});
|
|
utils.WriteFile(req.body.data_field, publicFile);
|
|
utils.WriteFile(r.count.toString(), countFile);
|
|
utils.WriteBackup();
|
|
log += "Wrote to file. ";
|
|
}
|
|
log += "Prev count : " + parseInt(c) + ", ";
|
|
|
|
} else {
|
|
res.render('submitted', {
|
|
log: r.log,
|
|
msg: "A fordító nem bírta értelmezni az elküldött adatokat! Ellenőrizd a kimenetet, és az elküldött adatokat."
|
|
});
|
|
log += "Could't parse data! ";
|
|
}
|
|
log += "NLOAD result: " + r.count;
|
|
}
|
|
logger.Log(log, logger.GetColor("blue"));
|
|
});
|
|
|
|
app.get('/postquestions', function(req, res) {
|
|
res.render('main', {
|
|
pdata: utils.ReadFile(publicFile),
|
|
sdata: utils.ReadFile(staticFile)
|
|
});
|
|
});
|
|
|
|
app.post('/postfeedback', function(req, res) {
|
|
res.redirect('back');
|
|
newMessages += "\n" + req.body.message_field;
|
|
logger.Log("[UMSG]: " + req.body.message_field, logger.GetColor("bluebg"), true);
|
|
utils.AppendToFile(logger.GetDateString() + ": " + req.body.message_field, msgFile);
|
|
});
|
|
|
|
app.get('/postfeedback', function(req, res) {
|
|
res.render('main', {
|
|
pdata: utils.ReadFile(publicFile),
|
|
sdata: utils.ReadFile(staticFile)
|
|
});
|
|
});
|
|
|
|
app.post('/isAdding', function(req, res) {
|
|
res.end('OK');
|
|
actions.ProcessIncomingRequest(req.body.datatoadd);
|
|
utils.WriteBackup();
|
|
});
|
|
|
|
app.get('/sanityCheck', function(req, res) {
|
|
res.end('Hello guys');
|
|
});
|
|
|
|
app.route('/badtestsender').post(function(req, res, next) {
|
|
var fstream;
|
|
req.pipe(req.busboy);
|
|
req.busboy.on('file', function(fieldname, file, filename) {
|
|
logger.Log("[UPL]: Uploading: " + filename, logger.GetColor("blue"));
|
|
|
|
fstream = fs.createWriteStream(recivedFiles + "/" + filename + " | " + Date().toString());
|
|
file.pipe(fstream);
|
|
fstream.on('close', function() {
|
|
logger.Log("[UPL]: Upload Finished of " + filename, logger.GetColor("blue"));
|
|
res.render("uploaded");
|
|
});
|
|
fstream.on('error', function(err) {
|
|
console.log("ERROR:" + err);
|
|
res.end("file uploaded");
|
|
});
|
|
});
|
|
});
|
|
|
|
app.get('/sanityCheck', function(req, res) {
|
|
res.end('Uploaded :) go back now');
|
|
});
|
|
|
|
app.get('/scriptgit', function(req, res) {
|
|
res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript");
|
|
res.end();
|
|
});
|
|
|
|
app.get('/servergit', function(req, res) {
|
|
res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server");
|
|
res.end();
|
|
});
|
|
|
|
app.get('*', function(req, res) {
|
|
res.render('404');
|
|
res.status(404);
|
|
utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile);
|
|
});
|
|
|
|
app.post('*', function(req, res) {
|
|
res.status(404);
|
|
utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile);
|
|
Log(req, true);
|
|
});
|
|
|
|
stat.Load();
|
|
app.listen(port);
|
|
logger.Log("[START]: Server listening on port " + port + "...");
|
|
|
|
function Log(req, toFile) {
|
|
try {
|
|
var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress;
|
|
var logEntry = "[RSND]: " + ip + ", " + req.headers['user-agent'] +
|
|
" " + req.method + " " + req.url;
|
|
var color = logger.GetColor("green");
|
|
for (var i = 0; i < highlights.length; i++)
|
|
if (req.url.toLowerCase().includes(highlights[i])) {
|
|
color = logger.GetColor("blue");
|
|
break;
|
|
}
|
|
if (req.url.toLowerCase().includes("isadding"))
|
|
color = logger.GetColor("yellow");
|
|
if (!toFile) {
|
|
logger.Log(logEntry, color);
|
|
} else {
|
|
utils.AppendToFile(logger.GetDateString() + ": " + logEntry + "\n\t" + JSON.stringify(req.headers) +
|
|
"\n\t" + JSON.stringify(req
|
|
.body) + "\n", logFile);
|
|
}
|
|
|
|
} catch (e) {
|
|
logger.Log("Error at logging lol", logger.GetColor("redbg"), true);
|
|
}
|
|
}
|