mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Prettied some stuff, and moved submodules around again
This commit is contained in:
parent
d8ef7cdcb0
commit
3f081d8dff
6 changed files with 106 additions and 182 deletions
2
.gitmodules
vendored
2
.gitmodules
vendored
|
@ -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
|
||||
|
|
|
@ -10,11 +10,11 @@ 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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -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,25 +77,35 @@ 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', {
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'users',
|
||||
{
|
||||
lastIP: ip,
|
||||
lastAccess: utils.GetDateString()
|
||||
}, {
|
||||
id: user.id
|
||||
})
|
||||
lastAccess: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: user.id,
|
||||
}
|
||||
)
|
||||
|
||||
next()
|
||||
}
|
||||
|
@ -101,7 +114,7 @@ module.exports = function (options) {
|
|||
function UpdateAccess(db, user, ip, sessionID) {
|
||||
const accesses = dbtools.Select(db, 'accesses', {
|
||||
userId: user.id,
|
||||
ip: ip
|
||||
ip: ip,
|
||||
})
|
||||
|
||||
if (accesses.length === 0) {
|
||||
|
@ -109,7 +122,7 @@ function UpdateAccess (db, user, ip, sessionID) {
|
|||
userID: user.id,
|
||||
ip: ip,
|
||||
sessionID: sessionID,
|
||||
date: utils.GetDateString()
|
||||
date: utils.GetDateString(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -118,7 +131,7 @@ 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) {
|
||||
|
|
|
@ -22,18 +22,25 @@ module.exports = function (options) {
|
|||
}
|
||||
|
||||
// fixme: regexp includes checking
|
||||
const hasLoggableKeyword = loggableKeywords && loggableKeywords.some((x) => {
|
||||
const hasLoggableKeyword =
|
||||
loggableKeywords &&
|
||||
loggableKeywords.some((x) => {
|
||||
return req.url.includes(x)
|
||||
})
|
||||
const hasLoggableModule = loggableModules && loggableModules.some((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'
|
||||
|
|
|
@ -53,7 +53,7 @@ try {
|
|||
const extraModules = JSON.parse(utils.ReadFile(extraModulesFile))
|
||||
modules = {
|
||||
...extraModules,
|
||||
...modules
|
||||
...modules,
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -94,7 +97,13 @@ if (!process.env.NS_DEVEL) {
|
|||
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,21 +113,21 @@ if (!process.env.NS_DEVEL) {
|
|||
})
|
||||
}
|
||||
// https://github.com/expressjs/cors#configuration-options
|
||||
app.use(cors({
|
||||
app.use(
|
||||
cors({
|
||||
credentials: true,
|
||||
origin: 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) {
|
||||
let x = modules[k]
|
||||
|
@ -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) {
|
||||
|
@ -196,7 +209,10 @@ 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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
|
|
@ -1,116 +0,0 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
|
||||
Question Server
|
||||
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
// 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue