mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	Auto generating folders, more compatibility running on any machine
This commit is contained in:
		
							
								
								
									
										11
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								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
 | 
			
		||||
`npm install express connect-busboy ejs express-layout querystring express`
 | 
			
		||||
 | 
			
		||||
Should auto generate needed folder structure
 | 
			
		||||
 | 
			
		||||
<a href="https://gitlab.com/YourFriendlyNeighborhoodDealer/moodle-test-userscript">Client</a>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										111
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										111
									
								
								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);
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								todos
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								todos
									
									
									
									
									
								
							@@ -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
 | 
			
		||||
							
								
								
									
										21
									
								
								utils.js
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								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) {
 | 
			
		||||
 
 | 
			
		||||
@@ -20,15 +20,15 @@ a {color: #9999ff;}
 | 
			
		||||
<a href="https://greasyfork.org/en/scripts/38999-moodle-elearning-kmooc-test-help">Script
 | 
			
		||||
greasyforkon</a>
 | 
			
		||||
|
 | 
			
		||||
<a href="http://questionmining.tk/manual">Manual</a>
 | 
			
		||||
<a href="<%= siteurl %>/manual">Manual</a>
 | 
			
		||||
|
 | 
			
		||||
<a href="http://questionmining.tk/servergit">Szerver repó</a>
 | 
			
		||||
<a href="<%= siteurl %>/servergit">Szerver repó</a>
 | 
			
		||||
|
 | 
			
		||||
<a href="http://questionmining.tk/scriptgit">Userscript repó</a>
 | 
			
		||||
<a href="<%= siteurl %>/scriptgit">Userscript repó</a>
 | 
			
		||||
|
 | 
			
		||||
<a href="http://questionmining.tk/data.json">Összes kérdés (JSON)</a>
 | 
			
		||||
<a href="<%= siteurl %>/data.json">Összes kérdés (JSON)</a>
 | 
			
		||||
|
 | 
			
		||||
<a href="http://questionmining.tk/legacy">Összes kérdés (Régi formátum)</a>
 | 
			
		||||
<a href="<%= siteurl %>/legacy">Összes kérdés (Régi formátum)</a>
 | 
			
		||||
</h2>
 | 
			
		||||
 | 
			
		||||
<table style="table-layout:fixed;width:100%">
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user