mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
server.js format
This commit is contained in:
parent
e9c2df8e9b
commit
6a71982677
1 changed files with 245 additions and 261 deletions
364
server.js
364
server.js
|
@ -18,330 +18,314 @@
|
||||||
|
|
||||||
------------------------------------------------------------------------- */
|
------------------------------------------------------------------------- */
|
||||||
|
|
||||||
const startHTTPS = true;
|
const startHTTPS = true
|
||||||
const siteUrl = "https://qmining.tk"; // http(s)//asd.basd
|
const siteUrl = 'https://qmining.tk' // http(s)//asd.basd
|
||||||
const ircURL = "https://kiwiirc.com/nextclient/irc.sub.fm/#qmining";
|
const ircURL = 'https://kiwiirc.com/nextclient/irc.sub.fm/#qmining'
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express')
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser')
|
||||||
const busboy = require('connect-busboy');
|
const busboy = require('connect-busboy')
|
||||||
const fs = require('fs');
|
const fs = require('fs')
|
||||||
const app = express();
|
const app = express()
|
||||||
const http = require('http');
|
const http = require('http')
|
||||||
const https = require('https');
|
const https = require('https')
|
||||||
|
|
||||||
const logger = require('./logger.js');
|
const logger = require('./logger.js')
|
||||||
const utils = require('./utils.js');
|
const utils = require('./utils.js')
|
||||||
const actions = require('./actions.js');
|
const actions = require('./actions.js')
|
||||||
const stat = require('./stat.js');
|
const stat = require('./stat.js')
|
||||||
|
|
||||||
const listedFiles = "./public/files";
|
const listedFiles = './public/files'
|
||||||
const recivedFiles = "public/recivedfiles";
|
const recivedFiles = 'public/recivedfiles'
|
||||||
const uloadFiles = "public/f";
|
const uloadFiles = 'public/f'
|
||||||
const staticFile = "public/data/static";
|
const staticFile = 'public/data/static'
|
||||||
const dataFile = "public/data.json";
|
const dataFile = 'public/data.json'
|
||||||
const manFile = "public/man.html";
|
const msgFile = 'stats/msgs'
|
||||||
const simpOutFile = "public/simplified";
|
|
||||||
const inputFile = "stats/inputs";
|
|
||||||
const msgFile = "stats/msgs";
|
|
||||||
const logFile = "stats/logs";
|
|
||||||
const allLogFile = "/nlogs/log";
|
|
||||||
|
|
||||||
// https://certbot.eff.org/
|
// https://certbot.eff.org/
|
||||||
const privkeyFile = "/etc/letsencrypt/live/qmining.tk/privkey.pem";
|
const privkeyFile = '/etc/letsencrypt/live/qmining.tk/privkey.pem'
|
||||||
const fullchainFile = "/etc/letsencrypt/live/qmining.tk/fullchain.pem";
|
const fullchainFile = '/etc/letsencrypt/live/qmining.tk/fullchain.pem'
|
||||||
const chainFile = "/etc/letsencrypt/live/qmining.tk/chain.pem";
|
const chainFile = '/etc/letsencrypt/live/qmining.tk/chain.pem'
|
||||||
|
|
||||||
var certsLoaded = false;
|
var certsLoaded = false
|
||||||
if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists(
|
if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists(
|
||||||
chainFile)) {
|
chainFile)) {
|
||||||
try {
|
try {
|
||||||
const key = fs.readFileSync(privkeyFile, "utf8");
|
const key = fs.readFileSync(privkeyFile, 'utf8')
|
||||||
const cert = fs.readFileSync(fullchainFile, "utf8");
|
const cert = fs.readFileSync(fullchainFile, 'utf8')
|
||||||
const ca = fs.readFileSync(chainFile, "utf8");
|
const ca = fs.readFileSync(chainFile, 'utf8')
|
||||||
var certs = {
|
var certs = {
|
||||||
key: key,
|
key: key,
|
||||||
cert: cert,
|
cert: cert,
|
||||||
ca: ca
|
ca: ca
|
||||||
};
|
}
|
||||||
certsLoaded = true;
|
certsLoaded = true
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log("Error loading cert files!", logger.GetColor("redbg"));
|
logger.Log('Error loading cert files!', logger.GetColor('redbg'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const port = 8080;
|
const port = 8080
|
||||||
const httpsPort = 8443;
|
const httpsPort = 8443
|
||||||
|
|
||||||
app.set('view engine', 'ejs');
|
app.set('view engine', 'ejs')
|
||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
res.on('finish', function () {
|
res.on('finish', function () {
|
||||||
logger.LogReq(req, true, res.statusCode);
|
logger.LogReq(req, true, res.statusCode)
|
||||||
if (res.statusCode != 404)
|
if (res.statusCode !== 404) { stat.LogStat(req.url) }
|
||||||
stat.LogStat(req.url);
|
})
|
||||||
});
|
next()
|
||||||
next();
|
})
|
||||||
});
|
app.use(express.static('public'))
|
||||||
app.use(express.static('public'));
|
|
||||||
app.use(busboy({
|
app.use(busboy({
|
||||||
limits: {
|
limits: {
|
||||||
fileSize: 10000 * 1024 * 1024
|
fileSize: 10000 * 1024 * 1024
|
||||||
}
|
}
|
||||||
}));
|
}))
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json())
|
||||||
app.use(bodyParser.urlencoded({
|
app.use(bodyParser.urlencoded({
|
||||||
limit: '5mb',
|
limit: '5mb',
|
||||||
extended: true
|
extended: true
|
||||||
}));
|
}))
|
||||||
app.use(bodyParser.json({
|
app.use(bodyParser.json({
|
||||||
limit: '5mb'
|
limit: '5mb'
|
||||||
}));
|
}))
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
|
|
||||||
app.get('/', function (req, res) {
|
app.get('/', function (req, res) {
|
||||||
|
// req.hostname
|
||||||
|
|
||||||
res.render('main', {
|
res.render('main', {
|
||||||
siteurl: siteUrl,
|
siteurl: siteUrl,
|
||||||
qa: actions.ProcessQA()
|
qa: actions.ProcessQA()
|
||||||
});
|
})
|
||||||
res.end();
|
res.end()
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/sio', function (req, res) {
|
app.get('/sio', function (req, res) {
|
||||||
res.render('uload');
|
res.render('uload')
|
||||||
res.end();
|
res.end()
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/manual', function (req, res) {
|
app.get('/manual', function (req, res) {
|
||||||
res.render('man');
|
res.render('man')
|
||||||
res.end();
|
res.end()
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/public', function (req, res) {
|
app.get('/public', function (req, res) {
|
||||||
var response = '@Plz update :)';
|
var response = '@Plz update :)'
|
||||||
response += utils.ReadFile(staticFile);
|
response += utils.ReadFile(staticFile)
|
||||||
res.write(response);
|
res.write(response)
|
||||||
res.end();
|
res.end()
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/static', function (req, res) {
|
app.get('/static', function (req, res) {
|
||||||
var response = '@Plz update :)';
|
var response = '@Plz update :)'
|
||||||
response += utils.ReadFile(staticFile);
|
response += utils.ReadFile(staticFile)
|
||||||
res.write(response);
|
res.write(response)
|
||||||
res.end();
|
res.end()
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/legacy', function (req, res) {
|
app.get('/legacy', function (req, res) {
|
||||||
|
var f = utils.ReadFile(dataFile)
|
||||||
var f = utils.ReadFile(dataFile);
|
var d = actions.LoadJSON(f)
|
||||||
var d = actions.LoadJSON(f);
|
let qcount = 0
|
||||||
let qcount = 0;
|
for (let i = 0; i < d.length; i++) { qcount += d.Subjects[i].length }
|
||||||
for (let i = 0; i < d.length; i++)
|
let scount = d.length
|
||||||
qcount += d.Subjects[i].length;
|
|
||||||
let scount = d.length;
|
|
||||||
|
|
||||||
res.render('alldata', {
|
res.render('alldata', {
|
||||||
data: d,
|
data: d,
|
||||||
scount: scount,
|
scount: scount,
|
||||||
qcount: qcount,
|
qcount: qcount,
|
||||||
siteurl: siteUrl
|
siteurl: siteUrl
|
||||||
});
|
})
|
||||||
|
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.post('/postfeedback', function (req, res) {
|
app.post('/postfeedback', function (req, res) {
|
||||||
res.redirect('back');
|
res.redirect('back')
|
||||||
logger.Log("New feedback message", logger.GetColor("bluebg"), true);
|
logger.Log('New feedback message', logger.GetColor('bluebg'), true)
|
||||||
utils.AppendToFile("\n\n" + logger.GetDateString() + ": " + req.body.message_field, msgFile);
|
utils.AppendToFile('\n\n' + logger.GetDateString() + ': ' + req.body.message_field, msgFile)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/postfeedback', function (req, res) {
|
app.get('/postfeedback', function (req, res) {
|
||||||
// TODO: res.redirect("/"); or if needs this anyways, becouse /postfeedback post handler already
|
// TODO: res.redirect("/"); or if needs this anyways, becouse /postfeedback post handler already
|
||||||
// redirects
|
// redirects
|
||||||
res.render('main', {
|
res.render('main', {
|
||||||
sdata: utils.ReadFile(staticFile)
|
sdata: utils.ReadFile(staticFile)
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
app.post('/isAdding', function (req, res) {
|
app.post('/isAdding', function (req, res) {
|
||||||
res.end('OK');
|
res.end('OK')
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
actions.ProcessIncomingRequest(req.body.datatoadd);
|
actions.ProcessIncomingRequest(req.body.datatoadd)
|
||||||
utils.WriteBackup();
|
utils.WriteBackup()
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/lred', function (req, res) {
|
app.get('/lred', function (req, res) {
|
||||||
res.redirect("/legacy");
|
res.redirect('/legacy')
|
||||||
res.end();
|
res.end()
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/menuClick', function (req, res) {
|
app.get('/menuClick', function (req, res) {
|
||||||
res.redirect("/");
|
res.redirect('/')
|
||||||
res.end();
|
res.end()
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/irc', function (req, res) {
|
app.get('/irc', function (req, res) {
|
||||||
res.redirect(ircURL);
|
res.redirect(ircURL)
|
||||||
res.end();
|
res.end()
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
// all questions readable
|
// all questions readable
|
||||||
app.get('/allqr', function (req, res) {
|
app.get('/allqr', function (req, res) {
|
||||||
var f = utils.ReadFile(dataFile);
|
var f = utils.ReadFile(dataFile)
|
||||||
var d = actions.LoadJSON(f);
|
var d = actions.LoadJSON(f)
|
||||||
|
|
||||||
res.render('allqr', {
|
res.render('allqr', {
|
||||||
d: d.toString().split('\n')
|
d: d.toString().split('\n')
|
||||||
});
|
})
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/greasy', function (req, res) {
|
app.get('/greasy', function (req, res) {
|
||||||
res.redirect("https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help");
|
res.redirect('https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help')
|
||||||
res.end();
|
res.end()
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/scriptgit', function (req, res) {
|
app.get('/scriptgit', function (req, res) {
|
||||||
res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript");
|
res.redirect('https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript')
|
||||||
res.end();
|
res.end()
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
app.get('/servergit', function (req, res) {
|
app.get('/servergit', function (req, res) {
|
||||||
res.redirect("https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server");
|
res.redirect('https://gitlab.com/YourFriendlyNeighborhoodDealer/question-node-server')
|
||||||
res.end();
|
res.end()
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
function UploadFile (req, res, path, next) {
|
function UploadFile (req, res, path, next) {
|
||||||
var fstream;
|
var fstream
|
||||||
req.pipe(req.busboy);
|
req.pipe(req.busboy)
|
||||||
req.busboy.on('file', function (fieldname, file, filename) {
|
req.busboy.on('file', function (fieldname, file, filename) {
|
||||||
logger.Log("Uploading: " + filename, logger.GetColor("blue"));
|
logger.Log('Uploading: ' + filename, logger.GetColor('blue'))
|
||||||
|
|
||||||
utils.CreatePath(path, true);
|
utils.CreatePath(path, true)
|
||||||
let d = new Date();
|
let d = new Date()
|
||||||
let fn = d.getHours() + "" + d.getMinutes() + "" + d.getSeconds() + "_" + filename;
|
let fn = d.getHours() + '' + d.getMinutes() + '' + d.getSeconds() + '_' + filename
|
||||||
|
|
||||||
fstream = fs.createWriteStream(path + "/" + fn);
|
fstream = fs.createWriteStream(path + '/' + fn)
|
||||||
file.pipe(fstream);
|
file.pipe(fstream)
|
||||||
fstream.on('close', function () {
|
fstream.on('close', function () {
|
||||||
logger.Log("Upload Finished of " + path + "/" + fn, logger.GetColor("blue"));
|
logger.Log('Upload Finished of ' + path + '/' + fn, logger.GetColor('blue'))
|
||||||
next(fn);
|
next(fn)
|
||||||
});
|
})
|
||||||
fstream.on('error', function (err) {
|
fstream.on('error', function (err) {
|
||||||
console.log(err);
|
console.log(err)
|
||||||
res.end("something bad happened :s");
|
res.end('something bad happened :s')
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
app.route('/fosuploader').post(function (req, res, next) {
|
app.route('/fosuploader').post(function (req, res, next) {
|
||||||
UploadFile(req, res, uloadFiles, (fn) => {
|
UploadFile(req, res, uloadFiles, (fn) => {
|
||||||
res.redirect("/f/" + fn);
|
res.redirect('/f/' + fn)
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
|
|
||||||
app.route('/badtestsender').post(function (req, res, next) {
|
app.route('/badtestsender').post(function (req, res, next) {
|
||||||
UploadFile(req, res, recivedFiles, (fn) => {
|
UploadFile(req, res, recivedFiles, (fn) => {
|
||||||
res.render("uploaded");
|
res.render('uploaded')
|
||||||
});
|
})
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
});
|
})
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
app.get('/stuff*', function (req, res) {
|
app.get('/stuff*', function (req, res) {
|
||||||
|
let curr = listedFiles + '/' + req.url.substring('/stuff/'.length, req.url.length).split('?')[0]
|
||||||
|
let relPath = curr.substring('./public/files'.length, curr.length)
|
||||||
|
|
||||||
let curr = listedFiles + "/" + req.url.substring("/stuff/".length, req.url.length).split('?')[0];
|
if (relPath[relPath.length - 1] !== '/') { relPath += '/' }
|
||||||
let relPath = curr.substring("./public/files".length, curr.length);
|
|
||||||
|
|
||||||
if (relPath[relPath.length - 1] != "/")
|
|
||||||
relPath += "/";
|
|
||||||
|
|
||||||
let t = relPath.split("/");
|
|
||||||
let prevDir = "";
|
|
||||||
for (let i = 0; i < t.length - 2; i++)
|
|
||||||
prevDir += t[i] + "/";
|
|
||||||
|
|
||||||
|
let t = relPath.split('/')
|
||||||
|
let prevDir = ''
|
||||||
|
for (let i = 0; i < t.length - 2; i++) { prevDir += t[i] + '/' }
|
||||||
|
|
||||||
// curr = curr.replace(/\//g, "/");
|
// curr = curr.replace(/\//g, "/");
|
||||||
// relPath = relPath.replace(/\//g, "/");
|
// relPath = relPath.replace(/\//g, "/");
|
||||||
|
|
||||||
logger.LogReq(req);
|
logger.LogReq(req)
|
||||||
|
|
||||||
if (fs.lstatSync(curr).isDirectory()) {
|
if (fs.lstatSync(curr).isDirectory()) {
|
||||||
if (curr[curr.length - 1] != "/")
|
if (curr[curr.length - 1] !== '/') { curr += '/' }
|
||||||
curr += "/";
|
|
||||||
|
|
||||||
let f = [];
|
let f = []
|
||||||
|
|
||||||
fs.readdirSync(curr).forEach((item) => {
|
fs.readdirSync(curr).forEach((item) => {
|
||||||
if (item[0] !== '.') {
|
if (item[0] !== '.') {
|
||||||
let res = {name:item};
|
let res = { name: item }
|
||||||
let stats = fs.statSync(curr + "/" + item);
|
let stats = fs.statSync(curr + '/' + item)
|
||||||
|
|
||||||
let fileSizeInBytes = stats["size"];
|
let fileSizeInBytes = stats['size']
|
||||||
res.size = Math.round(fileSizeInBytes / 1000000);
|
res.size = Math.round(fileSizeInBytes / 1000000)
|
||||||
|
|
||||||
res.path = relPath;
|
res.path = relPath
|
||||||
if (res.path[res.path.length - 1] != "/")
|
if (res.path[res.path.length - 1] !== '/') { res.path += '/' }
|
||||||
res.path += "/";
|
res.path += item
|
||||||
res.path += item;
|
|
||||||
|
|
||||||
res.mtime = stats["mtime"].toLocaleString();
|
res.mtime = stats['mtime'].toLocaleString()
|
||||||
|
|
||||||
f.push(res);
|
f.push(res)
|
||||||
}
|
}
|
||||||
});;
|
})
|
||||||
|
|
||||||
res.render('folders', {
|
res.render('folders', {
|
||||||
folders: f,
|
folders: f,
|
||||||
dirname: relPath,
|
dirname: relPath,
|
||||||
prevDir
|
prevDir
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
let fileStream = fs.createReadStream(curr);
|
let fileStream = fs.createReadStream(curr)
|
||||||
fileStream.pipe(res);
|
fileStream.pipe(res)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
return;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
app.get('*', function (req, res) {
|
app.get('*', function (req, res) {
|
||||||
res.render('404');
|
res.render('404')
|
||||||
res.status(404);
|
res.status(404)
|
||||||
// utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile);
|
// utils.AppendToFile(logger.GetDateString() + ": " + "404 GET", logFile);
|
||||||
});
|
})
|
||||||
|
|
||||||
app.post('*', function (req, res) {
|
app.post('*', function (req, res) {
|
||||||
res.status(404);
|
res.status(404)
|
||||||
// utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile);
|
// utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile);
|
||||||
});
|
})
|
||||||
|
|
||||||
var msg = "";
|
var msg = ''
|
||||||
stat.Load();
|
stat.Load()
|
||||||
|
|
||||||
const httpServer = http.createServer(app);
|
const httpServer = http.createServer(app)
|
||||||
httpServer.listen(port);
|
httpServer.listen(port)
|
||||||
msg += "Server listening on port " + port + " (http)";
|
msg += 'Server listening on port ' + port + ' (http)'
|
||||||
|
|
||||||
if (startHTTPS && certsLoaded) {
|
if (startHTTPS && certsLoaded) {
|
||||||
const httpsServer = https.createServer(certs, app);
|
const httpsServer = https.createServer(certs, app)
|
||||||
httpsServer.listen(httpsPort);
|
httpsServer.listen(httpsPort)
|
||||||
msg += ", and " + httpsPort + " (https)...";
|
msg += ', and ' + httpsPort + ' (https)...'
|
||||||
} else {
|
} else {
|
||||||
logger.Log("Cert files does not exists, starting http only!", logger.GetColor("redbg"));
|
logger.Log('Cert files does not exists, starting http only!', logger.GetColor('redbg'))
|
||||||
}
|
}
|
||||||
logger.Log(msg, logger.GetColor("yellow"));
|
logger.Log(msg, logger.GetColor('yellow'))
|
||||||
logger.Log("Node version: " + process.version);
|
logger.Log('Node version: ' + process.version)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue