diff --git a/modules/main.js b/modules/main.js index 2c508de..b7e35cf 100644 --- a/modules/main.js +++ b/modules/main.js @@ -18,7 +18,7 @@ ------------------------------------------------------------------------- */ -const siteUrl = 'https://qmining.frylabs.net' // http(s)//asd.basd +let url = '' // http(s)//asd.basd const express = require('express') const bodyParser = require('body-parser') @@ -49,7 +49,7 @@ app.use(bodyParser.json({ app.get('/', function (req, res) { res.render('main/main', { - siteurl: siteUrl + siteurl: url }) }) @@ -65,5 +65,8 @@ app.post('*', function (req, res) { }) exports.app = app +exports.setup = (x) => { + url = x.url +} logger.Log('Main module started', logger.GetColor('yellow')) diff --git a/modules/old.js b/modules/old.js index f54b7c1..680d3bc 100644 --- a/modules/old.js +++ b/modules/old.js @@ -18,6 +18,7 @@ ------------------------------------------------------------------------- */ +let url = '' const express = require('express') const app = express() @@ -26,17 +27,20 @@ const logger = require('../utils/logger.js') // -------------------------------------------------------------- app.get('/', function (req, res) { - res.redirect('https://qmining.frylabs.net' + req.url) + res.redirect(url + req.url) }) app.get('*', function (req, res) { - res.redirect('https://qmining.frylabs.net' + req.url) + res.redirect(url + req.url) }) app.post('*', function (req, res) { - res.redirect('https://qmining.frylabs.net' + req.url) + res.redirect(url + req.url) }) exports.app = app +exports.setup = (x) => { + url = x.url +} logger.Log('Old module started', logger.GetColor('yellow')) diff --git a/modules/qmining.js b/modules/qmining.js index 536755a..22f208c 100644 --- a/modules/qmining.js +++ b/modules/qmining.js @@ -18,7 +18,7 @@ ------------------------------------------------------------------------- */ -const siteUrl = 'https://qmining.frylabs.net' // http(s)//asd.basd +let url = '' const express = require('express') const bodyParser = require('body-parser') @@ -59,7 +59,7 @@ app.get('/', function (req, res) { // req.hostname res.render('qmining/main', { - siteurl: siteUrl, + siteurl: url, qa: actions.ProcessQA() }) res.end() @@ -82,7 +82,7 @@ app.get('/legacy', function (req, res) { data: d, scount: scount, qcount: qcount, - siteurl: siteUrl + siteurl: url }) logger.LogReq(req) @@ -135,7 +135,13 @@ app.get('/greasy', function (req, res) { }) app.get('/install', function (req, res) { - res.redirect(siteUrl + '/moodle-test-userscript/stable.user.js?install') + res.redirect(url + '/moodle-test-userscript/stable.user.js?install') + res.end() + logger.LogReq(req) +}) + +app.get('/install', function (req, res) { + res.redirect(url + '/moodle-test-userscript/stable.user.js') res.end() logger.LogReq(req) }) @@ -206,5 +212,8 @@ app.post('*', function (req, res) { }) exports.app = app +exports.setup = (x) => { + url = x.url +} logger.Log('Qmining module started', logger.GetColor('yellow')) diff --git a/modules/stuff.js b/modules/stuff.js index 3a09682..4e174eb 100644 --- a/modules/stuff.js +++ b/modules/stuff.js @@ -23,6 +23,7 @@ const bodyParser = require('body-parser') const busboy = require('connect-busboy') const fs = require('fs') const app = express() +let url = '' const logger = require('../utils/logger.js') // const utils = require('../utils/utils.js') @@ -49,7 +50,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 += '/' } @@ -63,37 +65,45 @@ app.get('/*', function (req, res) { logger.LogReq(req) - if (fs.lstatSync(curr).isDirectory()) { - if (curr[curr.length - 1] !== '/') { curr += '/' } + try { + if (fs.lstatSync(curr).isDirectory()) { + if (curr[curr.length - 1] !== '/') { curr += '/' } - let f = [] + let f = [] - fs.readdirSync(curr).forEach((item) => { - if (item[0] !== '.') { - let res = { name: item } - let stats = fs.statSync(curr + '/' + item) + fs.readdirSync(curr).forEach((item) => { + if (item[0] !== '.') { + let res = { name: item } + let stats = fs.statSync(curr + '/' + item) - let fileSizeInBytes = stats['size'] - res.size = Math.round(fileSizeInBytes / 1000000) + let fileSizeInBytes = stats['size'] + res.size = Math.round(fileSizeInBytes / 1000000) - res.path = relPath - if (res.path[res.path.length - 1] !== '/') { res.path += '/' } - res.path += item + res.path = relPath + if (res.path[res.path.length - 1] !== '/') { res.path += '/' } + res.path += item - res.mtime = stats['mtime'].toLocaleString() + res.mtime = stats['mtime'].toLocaleString() - f.push(res) - } + f.push(res) + } + }) + + res.render('stuff/folders', { + folders: f, + dirname: relPath, + prevDir, + url + }) + } else { + let fileStream = fs.createReadStream(curr) + fileStream.pipe(res) + } + } catch (e) { + res.render('stuff/nofile', { + missingFile: curr, + url }) - - res.render('stuff/folders', { - folders: f, - dirname: relPath, - prevDir - }) - } else { - let fileStream = fs.createReadStream(curr) - fileStream.pipe(res) } }) @@ -111,5 +121,8 @@ app.post('*', function (req, res) { }) exports.app = app +exports.setup = (x) => { + url = x.url +} logger.Log('Stuff module started', logger.GetColor('yellow')) diff --git a/server.js b/server.js index eef8795..26fc07d 100644 --- a/server.js +++ b/server.js @@ -32,11 +32,69 @@ const https = require('https') const stat = require('./utils/stat.js') stat.Load() -const qmining = require('./modules/qmining.js').app -const main = require('./modules/main.js').app -const sio = require('./modules/sio.js').app -const stuff = require('./modules/stuff.js').app -const old = require('./modules/old.js').app +const loggableKeywords = [ + 'user.js' +] +let modules = { + qmining: { + path: './modules/qmining.js', + name: 'qmining', + urls: [ 'qmining.frylabs.net', 'localhost' ] + }, + main: { + path: './modules/main.js', + name: 'main', + urls: [ 'frylabs.net', 'www.frylabs.net' ] + }, + sio: { + path: './modules/sio.js', + name: 'sio', + urls: [ 'sio.frylabs.net' ] + }, + stuff: { + path: './modules/stuff.js', + name: 'stuff', + urls: [ 'stuff.frylabs.net' ] + }, + old: { + path: './modules/old.js', + name: 'old', + urls: [ 'qmining.tk', 'www.qmining.tk' ] + } +} + +const app = express() + +app.use(function (req, res, next) { + res.on('finish', function () { + logger.LogReq(req, true, res.statusCode) + let toLog = loggableKeywords.some((x) => { + return req.url.includes(x) + }) + if (toLog) { logger.LogReq(req) } + if (res.statusCode !== 404) { stat.LogStat(req.url) } + }) + next() +}) + + +Object.keys(modules).forEach(function (k, i) { + let x = modules[k] + try { + let mod = require(x.path) + if (mod.setup) { + mod.setup({ + url: 'http://' + x.urls[0] // TODO http https or neither + }) + } + x.app = mod.app + x.urls.forEach((url) => { + app.use(vhost(url, x.app)) + }) + } catch (e) { + console.log(e) + } +}) const locLogFile = './stats/logs' const allLogFile = '/nlogs/log' @@ -93,35 +151,6 @@ function setLogTimer () { setLogTimer() -const app = express() - -const loggableKeywords = [ - 'user.js' -] - -app.use(function (req, res, next) { - res.on('finish', function () { - logger.LogReq(req, true, res.statusCode) - let toLog = loggableKeywords.some((x) => { - return req.url.includes(x) - }) - if (toLog) { logger.LogReq(req) } - if (res.statusCode !== 404) { stat.LogStat(req.url) } - }) - next() -}) - -app.use(vhost('qmining.frylabs.net', qmining)) -app.use(vhost('sio.frylabs.net', sio)) -app.use(vhost('stuff.frylabs.net', stuff)) -app.use(vhost('frylabs.net', main)) -app.use(vhost('www.frylabs.net', main)) - -app.use(vhost('qmining.tk', old)) -app.use(vhost('www.qmining.tk', old)) - -app.use(vhost('localhost', qmining)) - logger.Log('Node version: ' + process.version) logger.Log('Listening on port: ' + port) diff --git a/utils/logger.js b/utils/logger.js index 8439733..45e224b 100644 --- a/utils/logger.js +++ b/utils/logger.js @@ -1,121 +1,134 @@ -/* ---------------------------------------------------------------------------- - - 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') - } - - logEntry += dl + req.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' +] + +let noLogips = utils.ReadFile('./nolog').split('\n') +setInterval(() => { + noLogips = utils.ReadFile('./nolog').split('\n') +}, 1000 * 60 * 30) + +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 nolog = noLogips.some((x) => { + return x.includes(ip) + }) + if (nolog) { + return + } + + 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/main/main.ejs b/views/main/main.ejs index 4ba198d..11d8f43 100644 --- a/views/main/main.ejs +++ b/views/main/main.ejs @@ -5,6 +5,7 @@ FryLabs.net +
@@ -65,27 +65,32 @@ <%=dirname%>
- > Up one level + Up one level

<% for (var i = 0; i < folders.length; i++) { %> @@ -93,4 +98,20 @@ <% } %>
-
+ diff --git a/views/stuff/nofile.ejs b/views/stuff/nofile.ejs new file mode 100644 index 0000000..08b74da --- /dev/null +++ b/views/stuff/nofile.ejs @@ -0,0 +1,72 @@ + + + + + + No such file/folder + + + +
+

+ No such file / folder: +
+ <%= missingFile %> +
+ Back to root +

+
+ +