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,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) {
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue