diff --git a/.gitmodules b/.gitmodules index 86cc493..76e63d4 100755 --- a/.gitmodules +++ b/.gitmodules @@ -2,7 +2,7 @@ path = submodules/qmining-page url = git@gitlab.com:MrFry/qmining-page.git [submodule "modules/dataEditor/qmining-data-editor"] - path = submodules/dataEditor/qmining-data-editor + path = submodules/qmining-data-editor url = git@gitlab.com:MrFry/qmining-data-editor.git [submodule "qminingPublic/moodle-test-userscript"] path = submodules/moodle-test-userscript diff --git a/src/middlewares/auth.middleware.js b/src/middlewares/auth.middleware.js index 1912e77..dbdf4c7 100644 --- a/src/middlewares/auth.middleware.js +++ b/src/middlewares/auth.middleware.js @@ -2,7 +2,7 @@ const logger = require('../utils/logger.js') const utils = require('../utils/utils.js') const dbtools = require('../utils/dbtools.js') -module.exports = function (options) { +module.exports = function(options) { const { userDB, jsonResponse, exceptions } = options const renderLogin = (req, res) => { @@ -10,16 +10,16 @@ module.exports = function (options) { if (jsonResponse) { res.json({ result: 'nouser', - msg: 'You are not logged in' + msg: 'You are not logged in', }) } else { res.render('login', { - devel: process.env.NS_DEVEL + devel: process.env.NS_DEVEL, }) } } - return function (req, res, next) { + return function(req, res, next) { const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const sessionID = req.cookies.sessionID const isException = exceptions.some((exc) => { @@ -29,17 +29,20 @@ module.exports = function (options) { if (process.env.NS_NOUSER) { req.session = { user: { - id: 21323 + id: 21323, }, sessionID: sessionID || 111111111111111111, - isException: false + isException: false, } next() return } // FIXME Allowing all urls with _next in it, but not in params - if (req.url.split('?')[0].includes('_next') || req.url.split('?')[0].includes('well-known/acme-challenge')) { + if ( + req.url.split('?')[0].includes('_next') || + req.url.split('?')[0].includes('well-known/acme-challenge') + ) { req.session = { isException: true } next() return @@ -74,34 +77,44 @@ module.exports = function (options) { req.session = { user: user, sessionID: sessionID, - isException: isException + isException: isException, } logger.DebugLog(`ID #${user.id}: ${req.url}`, 'auth', 1) UpdateAccess(userDB, user, ip, sessionID) - dbtools.Update(userDB, 'sessions', { - lastAccess: utils.GetDateString() - }, { - id: sessionID - }) + dbtools.Update( + userDB, + 'sessions', + { + lastAccess: utils.GetDateString(), + }, + { + id: sessionID, + } + ) - dbtools.Update(userDB, 'users', { - lastIP: ip, - lastAccess: utils.GetDateString() - }, { - id: user.id - }) + dbtools.Update( + userDB, + 'users', + { + lastIP: ip, + lastAccess: utils.GetDateString(), + }, + { + id: user.id, + } + ) next() } } -function UpdateAccess (db, user, ip, sessionID) { +function UpdateAccess(db, user, ip, sessionID) { const accesses = dbtools.Select(db, 'accesses', { userId: user.id, - ip: ip + ip: ip, }) if (accesses.length === 0) { @@ -109,16 +122,16 @@ function UpdateAccess (db, user, ip, sessionID) { userID: user.id, ip: ip, sessionID: sessionID, - date: utils.GetDateString() + date: utils.GetDateString(), }) } } -function GetUserBySessionID (db, sessionID, req) { +function GetUserBySessionID(db, sessionID, req) { logger.DebugLog(`Getting user from db`, 'auth', 2) const session = dbtools.Select(db, 'sessions', { - id: sessionID + id: sessionID, })[0] if (!session) { @@ -126,7 +139,7 @@ function GetUserBySessionID (db, sessionID, req) { } const user = dbtools.Select(db, 'users', { - id: session.userID + id: session.userID, })[0] if (user) { diff --git a/src/middlewares/reqlogger.middleware.js b/src/middlewares/reqlogger.middleware.js index 52e2574..22f714f 100644 --- a/src/middlewares/reqlogger.middleware.js +++ b/src/middlewares/reqlogger.middleware.js @@ -1,11 +1,11 @@ const logger = require('../utils/logger.js') -module.exports = function (options) { +module.exports = function(options) { const loggableKeywords = options ? options.loggableKeywords : undefined const loggableModules = options ? options.loggableModules : undefined - return function (req, res, next) { - res.on('finish', function () { + return function(req, res, next) { + res.on('finish', function() { if (req.url.includes('_next/static')) { return } @@ -22,18 +22,25 @@ module.exports = function (options) { } // fixme: regexp includes checking - const hasLoggableKeyword = loggableKeywords && loggableKeywords.some((x) => { - return req.url.includes(x) - }) - const hasLoggableModule = loggableModules && loggableModules.some((x) => { - return hostname.includes(x) - }) + const hasLoggableKeyword = + loggableKeywords && + loggableKeywords.some((x) => { + return req.url.includes(x) + }) + const hasLoggableModule = + loggableModules && + loggableModules.some((x) => { + return hostname.includes(x) + }) const toLog = hasLoggableModule || hasLoggableKeyword logger.LogReq(req, true, res.statusCode) - if (toLog) { logger.LogReq(req) } + if (toLog) { + logger.LogReq(req) + } if (res.statusCode !== 404) { - logger.LogStat(req.url, + logger.LogStat( + req.url, ip, hostname, req.session && req.session.user ? req.session.user.id : 'NOUSER' diff --git a/src/server.js b/src/server.js index 81462a1..9eda8e3 100755 --- a/src/server.js +++ b/src/server.js @@ -53,7 +53,7 @@ try { const extraModules = JSON.parse(utils.ReadFile(extraModulesFile)) modules = { ...extraModules, - ...modules + ...modules, } } } catch (e) { @@ -66,7 +66,7 @@ try { process.on('SIGINT', () => exit('SIGINT')) process.on('SIGTERM', () => exit('SIGTERM')) -function exit (reason) { +function exit(reason) { console.log() logger.Log(`Exiting, reason: ${reason}`) Object.keys(modules).forEach((k, i) => { @@ -75,7 +75,10 @@ function exit (reason) { try { x.cleanup() } catch (e) { - logger.Log(`Error in ${k} cleanup! Details in STDERR`, logger.GetColor('redbg')) + logger.Log( + `Error in ${k} cleanup! Details in STDERR`, + logger.GetColor('redbg') + ) console.error(e) } } @@ -90,11 +93,17 @@ function exit (reason) { const app = express() if (!process.env.NS_DEVEL) { - app.use(function (req, res, next) { + app.use(function(req, res, next) { if (req.secure) { next() } else { - logger.DebugLog(`HTTPS ${req.method} redirect to: ${'https://' + req.headers.host + req.url}`, 'https', 1) + logger.DebugLog( + `HTTPS ${req.method} redirect to: ${'https://' + + req.headers.host + + req.url}`, + 'https', + 1 + ) if (req.method === 'POST') { res.redirect(307, 'https://' + req.headers.host + req.url) } else { @@ -104,23 +113,23 @@ if (!process.env.NS_DEVEL) { }) } // https://github.com/expressjs/cors#configuration-options -app.use(cors({ - credentials: true, - origin: true - // origin: [ /\.frylabs\.net$/ ] -})) +app.use( + cors({ + credentials: true, + origin: true, + // origin: [ /\.frylabs\.net$/ ] + }) +) const cookieSecret = uuidv4() app.use(cookieParser(cookieSecret)) -app.use(reqlogger({ - loggableKeywords: [ - 'stable.user.js' - ], - loggableModules: [ - 'dataeditor' - ] -})) +app.use( + reqlogger({ + loggableKeywords: ['stable.user.js'], + loggableModules: ['dataeditor'], + }) +) -Object.keys(modules).forEach(function (k, i) { +Object.keys(modules).forEach(function(k, i) { let x = modules[k] try { let mod = require(x.path) @@ -135,7 +144,7 @@ Object.keys(modules).forEach(function (k, i) { url: 'https://' + x.urls[0], userDB: userDB, publicdirs: x.publicdirs, - nextdir: x.nextdir + nextdir: x.nextdir, }) } @@ -161,8 +170,12 @@ const fullchainFile = '/etc/letsencrypt/live/frylabs.net/fullchain.pem' const chainFile = '/etc/letsencrypt/live/frylabs.net/chain.pem' var certsLoaded = false -if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFile) && utils.FileExists( - chainFile)) { +if ( + startHTTPS && + utils.FileExists(privkeyFile) && + utils.FileExists(fullchainFile) && + utils.FileExists(chainFile) +) { try { const key = utils.ReadFile(privkeyFile) const cert = utils.ReadFile(fullchainFile) @@ -170,7 +183,7 @@ if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFil var certs = { key: key, cert: cert, - ca: ca + ca: ca, } certsLoaded = true } catch (e) { @@ -180,7 +193,7 @@ if (startHTTPS && utils.FileExists(privkeyFile) && utils.FileExists(fullchainFil } setLogTimer() -function setLogTimer () { +function setLogTimer() { const now = new Date() const night = new Date( now.getFullYear(), @@ -196,17 +209,20 @@ function setLogTimer () { logger.DebugLog(`Seconds To Midnight: ${msToMidnight / 1000}`, 'daily', 1) if (msToMidnight < 0) { - logger.Log(`Error setting up Log Timer, msToMidnight is negative! (${msToMidnight})`, logger.GetColor('redbg')) + logger.Log( + `Error setting up Log Timer, msToMidnight is negative! (${msToMidnight})`, + logger.GetColor('redbg') + ) return } - setTimeout(function () { + setTimeout(function() { LogTimerAction() setLogTimer() }, msToMidnight) } -function LogTimerAction () { +function LogTimerAction() { logger.DebugLog(`Running Log Timer Action`, 'daily', 1) Object.keys(modules).forEach((k, i) => { const x = modules[k] @@ -216,13 +232,17 @@ function LogTimerAction () { logger.Log(`Running daily action of ${k}`) x.dailyAction() } catch (e) { - logger.Log(`Error in ${k} daily action! Details in STDERR`, logger.GetColor('redbg')) + logger.Log( + `Error in ${k} daily action! Details in STDERR`, + logger.GetColor('redbg') + ) console.err(e) } } }) - const line = '===================================================================================================================================================' + const line = + '===================================================================================================================================================' logger.Log(line) utils.AppendToFile(line, locLogFile) utils.AppendToFile(line, allLogFile) diff --git a/submodules/dataEditor/dataEditor.js b/submodules/dataEditor/dataEditor.js deleted file mode 100644 index b046e01..0000000 --- a/submodules/dataEditor/dataEditor.js +++ /dev/null @@ -1,116 +0,0 @@ -/* ---------------------------------------------------------------------------- - - Question Server - GitLab: - - 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 . - - ------------------------------------------------------------------------- */ - -// package requires -const express = require('express') -const bodyParser = require('body-parser') -const busboy = require('connect-busboy') -const app = express() - -// other requires -const utils = require('../../utils/utils.js') -const logger = require('../../utils/logger.js') -const auth = require('../../middlewares/auth.middleware.js') - -// stuff gotten from server.js -let userDB -let publicdirs = [] -let nextdir = '' - -function GetApp () { - app.use(bodyParser.urlencoded({ - limit: '5mb', - extended: true - })) - app.use(bodyParser.json({ - limit: '5mb' - })) - app.set('view engine', 'ejs') - app.set('views', [ - './modules/dataEditor/views', - './sharedViews' - ]) - app.use(auth({ - userDB: userDB, - jsonResponse: false, - exceptions: [ - '/favicon.ico', - '/getVeteranPw' - ] - })) - publicdirs.forEach((pdir) => { - logger.Log(`Using public dir: ${pdir}`) - app.use(express.static(pdir)) - }) - app.use(express.static(nextdir)) - app.use(busboy({ - limits: { - fileSize: 10000 * 1024 * 1024 - } - })) - - // -------------------------------------------------------------- - - function AddHtmlRoutes (files) { - const routes = files.reduce((acc, f) => { - if (f.includes('html')) { - acc.push(f.split('.')[0]) - return acc - } - return acc - }, []) - - routes.forEach((route) => { - logger.DebugLog(`Added route /${route}`, 'DataEditor routes', 1) - app.get(`/${route}`, function (req, res) { - logger.LogReq(req) - res.redirect(`${route}.html`) - }) - }) - } - AddHtmlRoutes(utils.ReadDir(nextdir)) - - // -------------------------------------------------------------- - - app.get('/', function (req, res) { - res.end('hai') - logger.LogReq(req) - }) - - app.get('*', function (req, res) { - res.status(404).render('404') - }) - - app.post('*', function (req, res) { - res.status(404).render('404') - }) - - return { - app: app - } -} - -exports.name = 'Data editor' -exports.getApp = GetApp -exports.setup = (data) => { - userDB = data.userDB - publicdirs = data.publicdirs - nextdir = data.nextdir -} diff --git a/submodules/dataEditor/qmining-data-editor b/submodules/qmining-data-editor similarity index 100% rename from submodules/dataEditor/qmining-data-editor rename to submodules/qmining-data-editor