From 90de7e926b92d5d231623ca6950b2eed485eebdf Mon Sep 17 00:00:00 2001 From: MrFry Date: Thu, 24 Oct 2019 15:05:08 +0200 Subject: [PATCH] Folders view tidy up, fix path with spaces, update url added to qmining --- modules/qmining.js | 6 + modules/stuff.js | 3 +- public/moodle-test-userscript | 2 +- utils/logger.js | 244 +++++++++++++++++----------------- views/stuff/folders.ejs | 40 ++++-- 5 files changed, 160 insertions(+), 135 deletions(-) diff --git a/modules/qmining.js b/modules/qmining.js index fa8b83d..081cc39 100644 --- a/modules/qmining.js +++ b/modules/qmining.js @@ -153,6 +153,12 @@ app.get('/greasy', function (req, res) { logger.LogReq(req) }) +app.get('/update', function (req, res) { + res.redirect('http://qmining.frylabs.net/moodle-test-userscript/stable.user.js') + res.end() + logger.LogReq(req) +}) + app.get('/install', function (req, res) { res.redirect('http://qmining.frylabs.net/moodle-test-userscript/stable.user.js') res.end() diff --git a/modules/stuff.js b/modules/stuff.js index 3a09682..8ffd512 100644 --- a/modules/stuff.js +++ b/modules/stuff.js @@ -49,7 +49,8 @@ app.use(bodyParser.json({ // -------------------------------------------------------------- app.get('/*', function (req, res) { - let curr = listedFiles + '/' + req.url.substring('/'.length, req.url.length).split('?')[0] + let parsedUrl = req.url.replace(/%20/g, " ") + let curr = listedFiles + '/' + parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0] let relPath = curr.substring('./public/files'.length, curr.length) if (relPath[relPath.length - 1] !== '/') { relPath += '/' } diff --git a/public/moodle-test-userscript b/public/moodle-test-userscript index c935c9e..fe74436 160000 --- a/public/moodle-test-userscript +++ b/public/moodle-test-userscript @@ -1 +1 @@ -Subproject commit c935c9e078303b98d61d2d12bca3ab86e2695bdc +Subproject commit fe74436ab119a31fdb35f7a3bbae50a572b0fb21 diff --git a/utils/logger.js b/utils/logger.js index a22b74d..c3e7a25 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -1,122 +1,122 @@ -/* ---------------------------------------------------------------------------- - - 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 . - - ------------------------------------------------------------------------- */ - -module.exports = { - GetDateString: GetDateString, - Log: Log, - GetColor: GetColor, - LogReq: LogReq -} - -const DELIM = C('green') + '|' + C() - -const utils = require('../utils/utils.js') -const locLogFile = './stats/logs' -const logFile = '/nlogs/nlogs' -const allLogFile = '/nlogs/log' - -const colors = [ - 'red', - 'green', - 'yellow', - 'blue', - 'magenta', - 'cyan' -] - -function GetDateString () { - const m = new Date() - const d = m.getFullYear() + '/' + - ('0' + (m.getMonth() + 1)).slice(-2) + '/' + - ('0' + m.getDate()).slice(-2) + ' ' + - ('0' + m.getHours()).slice(-2) + ':' + - ('0' + m.getMinutes()).slice(-2) + ':' + - ('0' + m.getSeconds()).slice(-2) - return GetRandomColor(m.getHours().toString()) + d + C() -} - -function Log (s, c) { - let dl = DELIM + C(c) - let log = C(c) + GetDateString() + dl + s - - console.log(log) - utils.AppendToFile(log, logFile) -} - -function LogReq (req, toFile, sc) { - try { - let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress - - let logEntry = GetRandomColor(ip) + ip + C() - let dl = DELIM - if (req.url.includes('lred')) { - dl += C('red') - } - - const hostname = req.hostname.replace('www.', '').split('.')[0] - logEntry += dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl - - logEntry += GetRandomColor(req.url.split('?')[0]) + req.url - - if (sc !== undefined && sc === 404) { logEntry += dl + sc } - - logEntry += C() - if (!toFile) { - Log(logEntry) - } else { - let defLogs = GetDateString() + dl + logEntry - - utils.AppendToFile(defLogs, locLogFile) - utils.AppendToFile(defLogs, allLogFile) - } - } catch (e) { - console.log(e) - Log('Error at logging lol', GetColor('redbg'), true) - } -} - -function GetRandomColor (ip) { - if (!ip) { - return 'red' - } - - let res = ip.split('').reduce((res, x) => { - return res + x.charCodeAt(0) - }, 0) - return C(colors[res % colors.length]) -} - -function GetColor (c) { - return c -} - -function C (c) { - if (c !== undefined) { c = c.toLowerCase() } - - if (c === 'redbg') { return '\x1b[41m' } - if (c === 'bluebg') { return '\x1b[44m' } - if (c === 'red') { return '\x1b[31m' } - if (c === 'green') { return '\x1b[32m' } - if (c === 'yellow') { return '\x1b[33m' } - if (c === 'blue') { return '\x1b[34m' } - if (c === 'magenta') { return '\x1b[35m' } - if (c === 'cyan') { return '\x1b[36m' } - return '\x1b[0m' -} +/* ---------------------------------------------------------------------------- + + 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 . + + ------------------------------------------------------------------------- */ + +module.exports = { + GetDateString: GetDateString, + Log: Log, + GetColor: GetColor, + LogReq: LogReq +} + +const DELIM = C('green') + '|' + C() + +const utils = require('../utils/utils.js') +const locLogFile = './stats/logs' +const logFile = '/nlogs/nlogs' +const allLogFile = '/nlogs/log' + +const colors = [ + 'green', + 'red', + 'yellow', + 'blue', + 'magenta', + 'cyan' +] + +function GetDateString () { + const m = new Date() + const d = m.getFullYear() + '/' + + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + + ('0' + m.getDate()).slice(-2) + ' ' + + ('0' + m.getHours()).slice(-2) + ':' + + ('0' + m.getMinutes()).slice(-2) + ':' + + ('0' + m.getSeconds()).slice(-2) + return GetRandomColor(m.getHours().toString()) + d + C() +} + +function Log (s, c) { + let dl = DELIM + C(c) + let log = C(c) + GetDateString() + dl + s + + console.log(log) + utils.AppendToFile(log, logFile) +} + +function LogReq (req, toFile, sc) { + try { + let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress + + let logEntry = GetRandomColor(ip) + ip + C() + let dl = DELIM + if (req.url.includes('lred')) { + dl += C('red') + } + + const hostname = req.hostname.replace('www.', '').split('.')[0] + logEntry += dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl + + logEntry += GetRandomColor(req.url.split('?')[0]) + req.url + + if (sc !== undefined && sc === 404) { logEntry += dl + sc } + + logEntry += C() + if (!toFile) { + Log(logEntry) + } else { + let defLogs = GetDateString() + dl + logEntry + + utils.AppendToFile(defLogs, locLogFile) + utils.AppendToFile(defLogs, allLogFile) + } + } catch (e) { + console.log(e) + Log('Error at logging lol', GetColor('redbg'), true) + } +} + +function GetRandomColor (ip) { + if (!ip) { + return 'red' + } + + let res = ip.split('').reduce((res, x) => { + return res + x.charCodeAt(0) + }, 0) + return C(colors[res % colors.length]) +} + +function GetColor (c) { + return c +} + +function C (c) { + if (c !== undefined) { c = c.toLowerCase() } + + if (c === 'redbg') { return '\x1b[41m' } + if (c === 'bluebg') { return '\x1b[44m' } + if (c === 'green') { return '\x1b[32m' } + if (c === 'red') { return '\x1b[31m' } + if (c === 'yellow') { return '\x1b[33m' } + if (c === 'blue') { return '\x1b[34m' } + if (c === 'magenta') { return '\x1b[35m' } + if (c === 'cyan') { return '\x1b[36m' } + return '\x1b[0m' +} diff --git a/views/stuff/folders.ejs b/views/stuff/folders.ejs index fd69db0..e9ae97a 100644 --- a/views/stuff/folders.ejs +++ b/views/stuff/folders.ejs @@ -5,6 +5,7 @@ <%=dirname%> +
@@ -72,13 +71,16 @@ <% for (var i = 0; i < folders.length; i++) { %> -