diff --git a/README.md b/README.md index cafcea4..255ba86 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,11 @@ -Question server +# Question server Install: - npm install express connect-busboy ejs express-layout querystring express -some stuff maybe missing +Requires node.js. -"Client:" https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript \ No newline at end of file +`npm install express connect-busboy ejs express-layout querystring express` + +Should auto generate needed folder structure + +Client diff --git a/server.js b/server.js index f9a7f6a..909d1fb 100755 --- a/server.js +++ b/server.js @@ -18,21 +18,20 @@ ------------------------------------------------------------------------- */ -var express = require('express'); -var bodyParser = require('body-parser'); -var busboy = require('connect-busboy'); -var fs = require('fs'); -var app = express(); -var http = require('http'); -var https = require('https'); +const express = require('express'); +const bodyParser = require('body-parser'); +const busboy = require('connect-busboy'); +const fs = require('fs'); +const app = express(); +const http = require('http'); +const https = require('https'); -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 logger = require('./logger.js'); +const utils = require('./utils.js'); +const actions = require('./actions.js'); +const stat = require('./stat.js'); +const siteUrl = "http://localhost:8080"; // http(s)//asd.basd const recivedFiles = "public/recivedfiles"; const motdFile = "public/motd"; const staticFile = "public/data/static"; @@ -45,10 +44,23 @@ const msgFile = "stats/msgs"; const logFile = "stats/logs"; // https://certbot.eff.org/ -const key = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/privkey.pem", "utf8"); -const cert = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/fullchain.pem", "utf8"); -const ca = fs.readFileSync("/etc/letsencrypt/live/questionmining.tk/chain.pem", "utf8"); -var certs = {key:key, cert:cert, ca:ca}; +const privkeyFile = "/etc/letsencrypt/live/questionmining.tk/privkey.pem"; +const fullchainFile = "/etc/letsencrypt/live/questionmining.tk/fullchain.pem"; +const chainFile = "/etc/letsencrypt/live/questionmining.tk/chain.pem"; + +var certsLoaded = false; +if (utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExistsc(chainFile)) { + const key = fs.readFileSync(privkeyFile, "utf8"); + const cert = fs.readFileSync(fullchainFile, "utf8"); + const ca = fs.readFileSync(chainFile, "utf8"); + var certs = { + key: key, + cert: cert, + ca: ca + }; + certsLoaded = true; +} + const port = 8080; const httpsPort = 8443; @@ -56,8 +68,6 @@ var highlights = ["public", "static", "manual", "isgetting", "postfeedback", "postquestions" ]; -var newMessages = ""; - app.set('view engine', 'ejs'); //app.all('*', function(req, res, next) { // if(req.secure) { @@ -91,7 +101,7 @@ app.use(bodyParser.json({ app.get('/', function(req, res) { res.render('main', { - sdata: utils.ReadFile(staticFile) + siteurl: siteUrl }); res.end(); }); @@ -131,7 +141,6 @@ app.get('/legacy', function(req, res) { 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); }); @@ -158,29 +167,6 @@ app.get('/menuClick', function(req, res) { res.end(); }); -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(); @@ -202,12 +188,41 @@ app.post('*', function(req, res) { // utils.AppendToFile(logger.GetDateString() + ": " + "404 POST", logFile); }); +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")); + + utils.CreatePath(recivedFiles, true); + 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"); + }); + }); +}); + +var msg = "[STRT]: "; stat.Load(); + const httpServer = http.createServer(app); -const httpsServer = https.createServer(certs, app); httpServer.listen(port); -httpsServer.listen(httpsPort); -logger.Log("[STRT]: Server listening on port " + port + " (http), and " + httpsPort + " (https)...", logger.GetColor("yellow")); +msg += "Server listening on port " + port + " (http)"; + +if (certsLoaded) { + const httpsServer = https.createServer(certs, app); + httpsServer.listen(httpsPort); + msg += ", and " + httpsPort + " (https)..."; +} else { + logger.Log("Cert files does not exists, starting http only!" ,logger.GetColor("redbg")); +} +logger.Log(msg ,logger.GetColor("yellow")); function Log(req, toFile, sc) { try { @@ -230,7 +245,7 @@ function Log(req, toFile, sc) { } else { var defLogs = logger.GetDateString() + ": " + logEntry; var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req - .body) + "\n"; + .body) + "\n"; utils.AppendToFile(defLogs, logFile); } diff --git a/todos b/todos deleted file mode 100644 index ea20ea6..0000000 --- a/todos +++ /dev/null @@ -1,8 +0,0 @@ -make init function that - creates directory and file structure -redirects, for more stats (ex. to greasyfork: /script -like - dislike button -static public links above textboxes -public questions allow question deletion -remove public questions -remove greasyfork images from manual diff --git a/utils.js b/utils.js index 965a470..1ae817e 100644 --- a/utils.js +++ b/utils.js @@ -4,7 +4,9 @@ module.exports = { writeFileAsync: WriteFileAsync, AppendToFile: AppendToFile, Beep: Beep, - WriteBackup: WriteBackup + WriteBackup: WriteBackup, + FileExists: FileExists, + CreatePath: CreatePath }; var fs = require('fs'); @@ -18,25 +20,32 @@ const manFile = "public/man.html"; const logFile = "stats/logs"; function ReadFile(name) { - if (!fs.existsSync(name)) + if (!FileExists(name)) throw "No such file: " + name; return fs.readFileSync(name, "utf8"); } -function CreatePath(path) { - if (fs.existsSync(path)) +function FileExists(path) { + return fs.existsSync(path); +} + +function CreatePath(path, onlyPath) { + if (FileExists(path)) return; var p = path.split("/"); var currDir = p[0]; for (var i = 1; i < p.length; i++) { - console.log(currDir); if (!fs.existsSync(currDir)) { fs.mkdirSync(currDir); } currDir += "/" + p[i]; } - fs.writeFileSync(path, ""); + if (onlyPath == undefined || onlyPath == false) + fs.writeFileSync(path, ""); + else + fs.mkdirSync(path); + } function WriteFile(content, path) { diff --git a/views/main.ejs b/views/main.ejs index 1ada181..fe32e25 100755 --- a/views/main.ejs +++ b/views/main.ejs @@ -20,15 +20,15 @@ a {color: #9999ff;} Script greasyforkon | -Manual +Manual | -Szerver repó +Szerver repó | -Userscript repó +Userscript repó | -Összes kérdés (JSON) +Összes kérdés (JSON) | -Összes kérdés (Régi formátum) +Összes kérdés (Régi formátum)