mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Seperated api module to submodules
This commit is contained in:
parent
2a0926aa7e
commit
c64c189ce3
11 changed files with 2072 additions and 1895 deletions
File diff suppressed because it is too large
Load diff
102
src/modules/api/submodules/feedback.ts
Normal file
102
src/modules/api/submodules/feedback.ts
Normal file
|
@ -0,0 +1,102 @@
|
|||
import fs from 'fs'
|
||||
|
||||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
|
||||
const msgFile = 'stats/msgs'
|
||||
const uloadFiles = 'data/f'
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
|
||||
|
||||
app.post('/postfeedbackfile', function(req: Request, res: any) {
|
||||
UploadFile(req, res, uloadFiles, () => {
|
||||
res.json({ success: true })
|
||||
})
|
||||
|
||||
logger.LogReq(req)
|
||||
logger.Log('New feedback file', logger.GetColor('bluebg'))
|
||||
})
|
||||
|
||||
app.post('/postfeedback', function(req: Request, res: any) {
|
||||
logger.LogReq(req)
|
||||
if (req.body.fromLogin) {
|
||||
logger.Log(
|
||||
'New feedback message from Login page',
|
||||
logger.GetColor('bluebg')
|
||||
)
|
||||
} else {
|
||||
logger.Log(
|
||||
'New feedback message from feedback page',
|
||||
logger.GetColor('bluebg')
|
||||
)
|
||||
}
|
||||
|
||||
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
|
||||
const user: User = req.session.user
|
||||
|
||||
utils.AppendToFile(
|
||||
utils.GetDateString() +
|
||||
':\n' +
|
||||
JSON.stringify(
|
||||
{
|
||||
...req.body,
|
||||
userID: user ? user.id : 'no user',
|
||||
ip: ip,
|
||||
},
|
||||
null,
|
||||
2
|
||||
),
|
||||
msgFile
|
||||
)
|
||||
res.json({ success: true })
|
||||
})
|
||||
|
||||
function UploadFile(req: Request, res: any, path: string, next) {
|
||||
try {
|
||||
req.pipe(req.busboy)
|
||||
req.busboy.on('file', function(fieldname, file, filename) {
|
||||
logger.Log('Uploading: ' + filename, logger.GetColor('blue'))
|
||||
|
||||
utils.CreatePath(path, true)
|
||||
const date = new Date()
|
||||
const fn =
|
||||
date.getHours() +
|
||||
'' +
|
||||
date.getMinutes() +
|
||||
'' +
|
||||
date.getSeconds() +
|
||||
'_' +
|
||||
filename
|
||||
|
||||
const fstream = fs.createWriteStream(path + '/' + fn)
|
||||
file.pipe(fstream)
|
||||
fstream.on('close', function() {
|
||||
logger.Log(
|
||||
'Upload Finished of ' + path + '/' + fn,
|
||||
logger.GetColor('blue')
|
||||
)
|
||||
next(fn)
|
||||
})
|
||||
fstream.on('error', function(err) {
|
||||
console.error(err)
|
||||
res.end('something bad happened :s')
|
||||
})
|
||||
})
|
||||
} catch (err) {
|
||||
logger.Log(`Unable to upload file!`, logger.GetColor('redbg'))
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
app.route('/fosuploader').post(function(req: Request, res: any) {
|
||||
UploadFile(req, res, uloadFiles, (fn) => {
|
||||
res.redirect('/f/' + fn)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
220
src/modules/api/submodules/forum.ts
Normal file
220
src/modules/api/submodules/forum.ts
Normal file
|
@ -0,0 +1,220 @@
|
|||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
|
||||
const adminUsersFile = 'data/admins.json'
|
||||
|
||||
function addComment(obj, path, comment) {
|
||||
if (path.length === 0) {
|
||||
obj.push(comment)
|
||||
} else {
|
||||
const i = path.pop()
|
||||
if (!obj[i].subComments) {
|
||||
obj[i].subComments = []
|
||||
}
|
||||
addComment(obj[i].subComments, path, comment)
|
||||
}
|
||||
}
|
||||
|
||||
function deleteComment(obj: any, path: Array<number>, userid: number): boolean {
|
||||
if (path.length === 1) {
|
||||
if (obj[path[0]].user === userid) {
|
||||
obj.splice(path[0], 1)
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
const i = path.pop()
|
||||
deleteComment(obj[i].subComments, path, userid)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
function addReaction(obj, path, { reaction, isDelete, uid }) {
|
||||
if (path.length === 1) {
|
||||
const index = path[0]
|
||||
if (!obj[index].reacts) {
|
||||
obj[index].reacts = {}
|
||||
}
|
||||
if (isDelete) {
|
||||
if (obj[index].reacts[reaction]) {
|
||||
obj[index].reacts[reaction] = obj[index].reacts[reaction].filter(
|
||||
(uid) => {
|
||||
return uid !== uid
|
||||
}
|
||||
)
|
||||
if (obj[index].reacts[reaction].length === 0) {
|
||||
delete obj[index].reacts[reaction]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!obj[index].reacts[reaction]) {
|
||||
obj[index].reacts[reaction] = [uid]
|
||||
} else {
|
||||
if (!obj[index].reacts[reaction].includes(uid)) {
|
||||
obj[index].reacts[reaction].push(uid)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const i = path.pop()
|
||||
addReaction(obj[i].subComments, path, {
|
||||
reaction: reaction,
|
||||
isDelete: isDelete,
|
||||
uid: uid,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app, /* userDB, url, */ publicdirs /*, moduleSpecificData */ } = data
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
|
||||
const newsFile = publicDir + 'news.json'
|
||||
|
||||
app.post('/react', (req: Request, res) => {
|
||||
logger.LogReq(req)
|
||||
|
||||
const user: User = req.session.user
|
||||
const news: any = utils.ReadJSON(newsFile)
|
||||
|
||||
const { newsKey, path, reaction, isDelete } = req.body
|
||||
if (!newsKey || !reaction) {
|
||||
res.json({ status: 'fail', msg: 'no newskey or reaction' })
|
||||
return
|
||||
}
|
||||
if (!path || path.length === 0) {
|
||||
if (news[newsKey]) {
|
||||
if (isDelete) {
|
||||
if (news[newsKey].reacts) {
|
||||
news[newsKey].reacts[reaction] = news[newsKey].reacts[
|
||||
reaction
|
||||
].filter((uid) => {
|
||||
return uid !== user.id
|
||||
})
|
||||
if (news[newsKey].reacts[reaction].length === 0) {
|
||||
delete news[newsKey].reacts[reaction]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!news[newsKey].reacts) {
|
||||
news[newsKey].reacts = { [reaction]: [user.id] }
|
||||
} else {
|
||||
if (Array.isArray(news[newsKey].reacts[reaction])) {
|
||||
if (!news[newsKey].reacts[reaction].includes(user.id)) {
|
||||
news[newsKey].reacts[reaction].push(user.id)
|
||||
}
|
||||
} else {
|
||||
news[newsKey].reacts[reaction] = [user.id]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
addReaction(news[newsKey].comments, path, {
|
||||
reaction: reaction,
|
||||
isDelete: isDelete,
|
||||
uid: user.id,
|
||||
})
|
||||
}
|
||||
|
||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
||||
res.json({ status: 'ok', news: news })
|
||||
})
|
||||
|
||||
app.post('/comment', (req: Request, res) => {
|
||||
logger.LogReq(req)
|
||||
|
||||
const user: User = req.session.user
|
||||
const news: any = utils.ReadJSON(newsFile)
|
||||
const admins: any = utils.FileExists(adminUsersFile)
|
||||
? utils.ReadJSON(adminUsersFile)
|
||||
: []
|
||||
const { type, path, newsKey } = req.body
|
||||
if (!type || !path || !newsKey) {
|
||||
res.json({ status: 'fail', msg: ' type or path or newsKey is undefined' })
|
||||
return
|
||||
}
|
||||
|
||||
if (type === 'add') {
|
||||
const { content } = req.body
|
||||
const comment = {
|
||||
date: utils.GetDateString(),
|
||||
user: user.id,
|
||||
content: content,
|
||||
admin: admins.includes(user.id),
|
||||
}
|
||||
if (!news[newsKey].comments) {
|
||||
news[newsKey].comments = []
|
||||
}
|
||||
addComment(news[newsKey].comments, path, comment)
|
||||
} else if (type === 'delete') {
|
||||
if (news[newsKey].comments) {
|
||||
const success = deleteComment(news[newsKey].comments, path, user.id)
|
||||
if (!success) {
|
||||
res.json({
|
||||
status: 'fail',
|
||||
msg: 'you cant delete other users comments',
|
||||
news: news,
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res.json({ status: 'fail', msg: 'no such type', news: news })
|
||||
return
|
||||
}
|
||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
||||
res.json({ status: 'ok', news: news })
|
||||
})
|
||||
|
||||
app.post('/rmPost', (req: Request, res) => {
|
||||
logger.LogReq(req)
|
||||
const user: User = req.session.user
|
||||
const news: any = utils.ReadJSON(newsFile)
|
||||
const { newsKey } = req.body
|
||||
|
||||
if (news[newsKey].user === user.id) {
|
||||
delete news[newsKey]
|
||||
} else {
|
||||
res.json({
|
||||
status: 'fail',
|
||||
msg: 'u cant delete other users posts!',
|
||||
news: news,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
||||
res.json({ status: 'ok', news: news })
|
||||
})
|
||||
|
||||
app.post('/addPost', (req: Request, res) => {
|
||||
logger.LogReq(req)
|
||||
const user: User = req.session.user
|
||||
const news: any = utils.ReadJSON(newsFile)
|
||||
const admins: any = utils.FileExists(adminUsersFile)
|
||||
? utils.ReadJSON(adminUsersFile)
|
||||
: []
|
||||
const { title, content } = req.body
|
||||
|
||||
news[uuidv4()] = {
|
||||
date: utils.GetDateString(),
|
||||
user: user.id,
|
||||
title: title,
|
||||
content: content,
|
||||
admin: admins.includes(user.id),
|
||||
}
|
||||
|
||||
utils.WriteFile(JSON.stringify(news, null, 2), newsFile)
|
||||
res.json({ status: 'ok', news: news })
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
1147
src/modules/api/submodules/qmining.ts
Normal file
1147
src/modules/api/submodules/qmining.ts
Normal file
File diff suppressed because it is too large
Load diff
90
src/modules/api/submodules/quickvote.ts
Normal file
90
src/modules/api/submodules/quickvote.ts
Normal file
|
@ -0,0 +1,90 @@
|
|||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
|
||||
const quickVoteResultsDir = 'stats/qvote'
|
||||
const quickVotes = 'stats/qvote/votes.json'
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
|
||||
|
||||
app.get('/quickvote', (req: Request, res: any) => {
|
||||
const key = req.query.key
|
||||
const val: any = req.query.val
|
||||
const user: User = req.session.user
|
||||
|
||||
if (!key || !val) {
|
||||
res.render('votethank', {
|
||||
results: 'error',
|
||||
msg: 'no key or val query param!',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// FIXME: check vote type in file
|
||||
let votes: any = {}
|
||||
if (utils.FileExists(quickVotes)) {
|
||||
votes = utils.ReadJSON(quickVotes)
|
||||
} else {
|
||||
logger.Log(
|
||||
`No such vote "${key}", and quickVotes.json is missing ( #${user.id}: ${key}-${val} )`,
|
||||
logger.GetColor('blue')
|
||||
)
|
||||
res.render('votethank', {
|
||||
result: 'no such pool',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if (!votes.voteNames.includes(key)) {
|
||||
logger.Log(
|
||||
`No such vote "${key}" ( #${user.id}: ${key}-${val} )`,
|
||||
logger.GetColor('blue')
|
||||
)
|
||||
res.render('votethank', {
|
||||
result: 'no such pool',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const voteFile = quickVoteResultsDir + '/' + key + '.json'
|
||||
|
||||
let voteData = {
|
||||
votes: {},
|
||||
sum: {},
|
||||
}
|
||||
|
||||
if (utils.FileExists(voteFile)) {
|
||||
voteData = utils.ReadJSON(voteFile)
|
||||
} else {
|
||||
utils.CreatePath(quickVoteResultsDir)
|
||||
}
|
||||
|
||||
const prevVote = voteData.votes[user.id]
|
||||
|
||||
voteData.votes[user.id] = val
|
||||
if (voteData.sum[val]) {
|
||||
voteData.sum[val]++
|
||||
} else {
|
||||
voteData.sum[val] = 1
|
||||
}
|
||||
if (prevVote) {
|
||||
if (voteData.sum[prevVote]) {
|
||||
voteData.sum[prevVote] -= 1
|
||||
}
|
||||
}
|
||||
|
||||
logger.Log(`Vote from #${user.id}: ${key}: ${val}`, logger.GetColor('blue'))
|
||||
res.render('votethank', {
|
||||
result: prevVote ? 'already voted' : 'success',
|
||||
prevVote: prevVote,
|
||||
msg: 'vote added',
|
||||
})
|
||||
|
||||
utils.WriteFile(JSON.stringify(voteData), voteFile)
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
104
src/modules/api/submodules/ranklist.ts
Normal file
104
src/modules/api/submodules/ranklist.ts
Normal file
|
@ -0,0 +1,104 @@
|
|||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
|
||||
const idStatFile = 'stats/idstats'
|
||||
const idvStatFile = 'stats/idvstats'
|
||||
|
||||
function mergeObjSum(a, b) {
|
||||
const res = { ...b }
|
||||
Object.keys(a).forEach((key) => {
|
||||
if (res[key]) {
|
||||
res[key] += a[key]
|
||||
} else {
|
||||
res[key] = a[key]
|
||||
}
|
||||
})
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
|
||||
|
||||
app.get('/ranklist', (req: Request, res: any) => {
|
||||
logger.LogReq(req)
|
||||
let result
|
||||
const querySince: any = req.query.since
|
||||
const user: User = req.session.user
|
||||
|
||||
if (!querySince) {
|
||||
result = utils.ReadJSON(idStatFile)
|
||||
} else {
|
||||
try {
|
||||
const since = new Date(querySince)
|
||||
if (!(since instanceof Date) || isNaN(since.getTime())) {
|
||||
throw new Error('Not a date')
|
||||
}
|
||||
const data = utils.ReadJSON(idvStatFile)
|
||||
result = {}
|
||||
|
||||
Object.keys(data).forEach((key) => {
|
||||
const dailyStat = data[key]
|
||||
|
||||
if (new Date(key) > since) {
|
||||
Object.keys(dailyStat).forEach((userId) => {
|
||||
const userStat = dailyStat[userId]
|
||||
const uidRes = result[userId]
|
||||
|
||||
if (!uidRes) {
|
||||
result[userId] = userStat
|
||||
} else {
|
||||
result[userId] = {
|
||||
count: uidRes.count + userStat.count,
|
||||
newQuestions: uidRes.newQuestions + userStat.newQuestions,
|
||||
allQuestions: uidRes.allQuestions + userStat.allQuestions,
|
||||
subjs: mergeObjSum(uidRes.subjs, userStat.subjs),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
res.json({
|
||||
msg: 'invalid date format, or other error occured',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const list = []
|
||||
const sum = {
|
||||
count: 0,
|
||||
newQuestions: 0,
|
||||
allQuestions: 0,
|
||||
}
|
||||
Object.keys(result).forEach((key) => {
|
||||
list.push({
|
||||
userId: parseInt(key),
|
||||
...result[key],
|
||||
})
|
||||
|
||||
sum.count = sum.count + result[key].count
|
||||
sum.newQuestions = sum.newQuestions + result[key].newQuestions
|
||||
sum.allQuestions = sum.allQuestions + result[key].allQuestions
|
||||
})
|
||||
|
||||
if (list.length === 0) {
|
||||
res.json({
|
||||
msg: 'There are no users in the stats db :c',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
res.json({
|
||||
since: querySince,
|
||||
sum: sum,
|
||||
list: list,
|
||||
selfuserId: user.id,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
65
src/modules/api/submodules/todos.ts
Normal file
65
src/modules/api/submodules/todos.ts
Normal file
|
@ -0,0 +1,65 @@
|
|||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData } from '../../../types/basicTypes'
|
||||
|
||||
const todosFile = 'data/todos.json'
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
|
||||
|
||||
app.get('/voteTodo', (req: Request, res: any) => {
|
||||
logger.LogReq(req)
|
||||
const userId = req.session.user.id
|
||||
const id: any = req.query.id
|
||||
const todos = utils.ReadJSON(todosFile)
|
||||
|
||||
if (!id) {
|
||||
res.json({
|
||||
msg: 'id query undefined',
|
||||
result: 'not ok',
|
||||
})
|
||||
}
|
||||
|
||||
const cardIndex = todos.cards.findIndex((currcard) => {
|
||||
return currcard.id === parseInt(id)
|
||||
})
|
||||
if (cardIndex === -1) {
|
||||
res.json({
|
||||
msg: 'card not found',
|
||||
result: 'not ok',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const ind = todos.cards[cardIndex].votes.indexOf(userId)
|
||||
if (ind === -1) {
|
||||
todos.cards[cardIndex].votes.push(userId)
|
||||
} else {
|
||||
todos.cards[cardIndex].votes.splice(ind, 1)
|
||||
}
|
||||
|
||||
utils.WriteFile(JSON.stringify(todos, null, 2), todosFile)
|
||||
res.json({
|
||||
todos: todos,
|
||||
userId: userId,
|
||||
msg: 'updated',
|
||||
result: 'ok',
|
||||
})
|
||||
})
|
||||
|
||||
app.get('/todos', (req: Request, res: any) => {
|
||||
logger.LogReq(req)
|
||||
const userId = req.session.user.id
|
||||
const todos = utils.ReadJSON(todosFile)
|
||||
|
||||
res.json({
|
||||
todos: todos,
|
||||
userId: userId,
|
||||
result: 'ok',
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
27
src/modules/api/submodules/userFiles.ts
Normal file
27
src/modules/api/submodules/userFiles.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData } from '../../../types/basicTypes'
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app, /* userDB, url, */ publicdirs /* moduleSpecificData */ } = data
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
|
||||
const userFilesDir = publicDir + 'userFiles'
|
||||
|
||||
app.get('/listUserFiles', (req: Request, res) => {
|
||||
logger.LogReq(req)
|
||||
|
||||
if (!utils.FileExists(userFilesDir)) {
|
||||
utils.CreatePath(userFilesDir, true)
|
||||
}
|
||||
|
||||
res.json({
|
||||
files: utils.ReadDir(userFilesDir),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
297
src/modules/api/submodules/userManagement.ts
Normal file
297
src/modules/api/submodules/userManagement.ts
Normal file
|
@ -0,0 +1,297 @@
|
|||
import { v4 as uuidv4 } from 'uuid'
|
||||
|
||||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
import dbtools from '../../../utils/dbtools'
|
||||
|
||||
const minimumAlowwedSessions = 2 // how many sessions are allowed for a user
|
||||
const addPWPerDay = 3 // every x day a user can give a pw
|
||||
const maxPWCount = 6 // maximum pw give opportunities a user can have at once
|
||||
const addPWCount = 1 // how many pw gen opportunities to add each time
|
||||
const daysAfterUserGetsPWs = 5 // days after user gets pw-s
|
||||
const usersDbBackupPath = 'data/dbs/backup'
|
||||
|
||||
function BackupDB(usersDbBackupPath, userDB) {
|
||||
logger.Log('Backing up auth DB ...')
|
||||
utils.CreatePath(usersDbBackupPath, true)
|
||||
userDB
|
||||
.backup(
|
||||
`${usersDbBackupPath}/users.${utils
|
||||
.GetDateString()
|
||||
.replace(/ /g, '_')}.db`
|
||||
)
|
||||
.then(() => {
|
||||
logger.Log('Auth DB backup complete!')
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.Log('Auth DB backup failed!', logger.GetColor('redbg'))
|
||||
console.error(err)
|
||||
})
|
||||
}
|
||||
|
||||
function setup(data: SubmoduleData): any {
|
||||
const { app, userDB, url /* publicdirs, moduleSpecificData */ } = data
|
||||
let domain: any = url.split('.') // [ "https://api", "frylabs", "net" ]
|
||||
domain.shift() // [ "frylabs", "net" ]
|
||||
domain = domain.join('.') // "frylabs.net"
|
||||
logger.DebugLog(`Cookie domain: ${domain}`, 'cookie', 1)
|
||||
|
||||
app.get('/avaiblePWS', (req: Request, res: any) => {
|
||||
logger.LogReq(req)
|
||||
|
||||
const user: User = req.session.user
|
||||
|
||||
res.json({
|
||||
result: 'success',
|
||||
userCreated: user.created,
|
||||
avaiblePWS: user.avaiblePWRequests,
|
||||
requestedPWS: user.pwRequestCount,
|
||||
maxPWCount: maxPWCount,
|
||||
daysAfterUserGetsPWs: daysAfterUserGetsPWs,
|
||||
addPWPerDay: addPWPerDay,
|
||||
addPWCount: addPWCount,
|
||||
dayDiff: getDayDiff(user.created),
|
||||
})
|
||||
})
|
||||
|
||||
app.post('/getpw', function(req: Request, res: any) {
|
||||
logger.LogReq(req)
|
||||
|
||||
const requestingUser = req.session.user
|
||||
|
||||
if (requestingUser.avaiblePWRequests <= 0) {
|
||||
res.json({
|
||||
result: 'error',
|
||||
msg:
|
||||
'Too many passwords requested or cant request password yet, try later',
|
||||
})
|
||||
logger.Log(
|
||||
`User #${requestingUser.id} requested too much passwords`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'users',
|
||||
{
|
||||
avaiblePWRequests: requestingUser.avaiblePWRequests - 1,
|
||||
pwRequestCount: requestingUser.pwRequestCount + 1,
|
||||
},
|
||||
{
|
||||
id: requestingUser.id,
|
||||
}
|
||||
)
|
||||
|
||||
const pw = uuidv4()
|
||||
const insertRes = dbtools.Insert(userDB, 'users', {
|
||||
pw: pw,
|
||||
avaiblePWRequests: 0,
|
||||
created: utils.GetDateString(),
|
||||
})
|
||||
|
||||
logger.Log(
|
||||
`User #${requestingUser.id} created new user #${insertRes.lastInsertRowid}`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
|
||||
res.json({
|
||||
result: 'success',
|
||||
pw: pw,
|
||||
requestedPWS: requestingUser.pwRequestCount + 1,
|
||||
remaining: requestingUser.avaiblePWRequests - 1,
|
||||
})
|
||||
})
|
||||
|
||||
app.post('/login', (req: Request, res: any) => {
|
||||
logger.LogReq(req)
|
||||
const pw = req.body.pw
|
||||
? req.body.pw
|
||||
.replace(/'/g, '')
|
||||
.replace(/"/g, '')
|
||||
.replace(/;/g, '')
|
||||
: false
|
||||
const isScript = req.body.script
|
||||
const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
|
||||
const user: User = dbtools.Select(userDB, 'users', {
|
||||
pw: pw,
|
||||
})[0]
|
||||
|
||||
if (user) {
|
||||
const sessionID = uuidv4()
|
||||
|
||||
const existingSessions = dbtools
|
||||
.Select(userDB, 'sessions', {
|
||||
userID: user.id,
|
||||
isScript: isScript ? 1 : 0,
|
||||
})
|
||||
.sort((a, b) => {
|
||||
return new Date(a).getTime() - new Date(b).getTime()
|
||||
})
|
||||
|
||||
const diff = existingSessions.length - minimumAlowwedSessions
|
||||
if (diff > 0) {
|
||||
logger.Log(
|
||||
`Multiple ${isScript ? 'script' : 'website'} sessions ( ${
|
||||
existingSessions.length
|
||||
} ) for #${user.id}, deleting olds`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
for (let i = 0; i < diff; i++) {
|
||||
const id = existingSessions[i].id
|
||||
dbtools.Delete(userDB, 'sessions', {
|
||||
id: id,
|
||||
isScript: isScript ? 1 : 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'users',
|
||||
{
|
||||
loginCount: user.loginCount + 1,
|
||||
lastIP: ip,
|
||||
lastLogin: utils.GetDateString(),
|
||||
},
|
||||
{
|
||||
id: user.id,
|
||||
}
|
||||
)
|
||||
|
||||
dbtools.Insert(userDB, 'sessions', {
|
||||
id: sessionID,
|
||||
ip: ip,
|
||||
userID: user.id,
|
||||
isScript: isScript ? 1 : 0,
|
||||
createDate: utils.GetDateString(),
|
||||
})
|
||||
|
||||
// https://www.npmjs.com/package/cookie
|
||||
// FIXME: cookies are not configured coorectly
|
||||
res.cookie('sessionID', sessionID, {
|
||||
domain: domain,
|
||||
expires: new Date(
|
||||
new Date().getTime() + 10 * 365 * 24 * 60 * 60 * 1000
|
||||
),
|
||||
sameSite: 'none',
|
||||
secure: true,
|
||||
})
|
||||
res.cookie('sessionID', sessionID, {
|
||||
expires: new Date(
|
||||
new Date().getTime() + 10 * 365 * 24 * 60 * 60 * 1000
|
||||
),
|
||||
sameSite: 'none',
|
||||
secure: true,
|
||||
})
|
||||
|
||||
res.json({
|
||||
result: 'success',
|
||||
msg: 'you are now logged in',
|
||||
})
|
||||
logger.Log(
|
||||
`Successfull login to ${
|
||||
isScript ? 'script' : 'website'
|
||||
} with user ID: #${user.id}`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
} else {
|
||||
logger.Log(
|
||||
`Login attempt with invalid pw: ${pw} to ${
|
||||
isScript ? 'script' : 'website'
|
||||
}`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
res.json({
|
||||
result: 'error',
|
||||
msg: 'Invalid password',
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/logout', (req: Request, res: any) => {
|
||||
logger.LogReq(req)
|
||||
const sessionID = req.cookies.sessionID
|
||||
const user: User = req.session.user
|
||||
|
||||
if (!user) {
|
||||
res.json({
|
||||
msg: 'You are not logged in',
|
||||
success: false,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
logger.Log(
|
||||
`Successfull logout with user ID: #${user.id}`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
|
||||
// removing session from db
|
||||
dbtools.Delete(userDB, 'sessions', {
|
||||
id: sessionID,
|
||||
})
|
||||
res.clearCookie('sessionID').json({
|
||||
msg: 'Successfull logout',
|
||||
result: 'success',
|
||||
})
|
||||
})
|
||||
|
||||
function getDayDiff(dateString) {
|
||||
const msdiff = new Date().getTime() - new Date(dateString).getTime()
|
||||
return Math.floor(msdiff / (1000 * 3600 * 24))
|
||||
}
|
||||
|
||||
function IncrementAvaiblePWs() {
|
||||
// FIXME: check this if this is legit and works
|
||||
logger.Log('Incrementing avaible PW-s ...')
|
||||
const users: Array<User> = dbtools.SelectAll(userDB, 'users')
|
||||
|
||||
users.forEach((user) => {
|
||||
if (user.avaiblePWRequests >= maxPWCount) {
|
||||
return
|
||||
}
|
||||
|
||||
const dayDiff = getDayDiff(user.created)
|
||||
if (dayDiff < daysAfterUserGetsPWs) {
|
||||
logger.Log(
|
||||
`User #${user.id} is not registered long enough to get password ( ${dayDiff} days, ${daysAfterUserGetsPWs} needed)`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
if (dayDiff % addPWPerDay === 0) {
|
||||
logger.Log(
|
||||
`Incrementing avaible PW-s for user #${user.id}: ${
|
||||
user.avaiblePWRequests
|
||||
} -> ${user.avaiblePWRequests + addPWCount}`,
|
||||
logger.GetColor('cyan')
|
||||
)
|
||||
dbtools.Update(
|
||||
userDB,
|
||||
'users',
|
||||
{
|
||||
avaiblePWRequests: user.avaiblePWRequests + addPWCount,
|
||||
},
|
||||
{
|
||||
id: user.id,
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
dailyAction: () => {
|
||||
BackupDB(usersDbBackupPath, userDB)
|
||||
IncrementAvaiblePWs()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
|
@ -94,6 +94,8 @@ if (stat.isDirectory()) {
|
|||
// possible answers duplicate removing
|
||||
// ---------------------------------------------------------------------------------
|
||||
|
||||
// TODO: dont check every file, only check per directorires
|
||||
// only compare questions of same subjects
|
||||
function removePossibleAnswersDuplicates(path) {
|
||||
let count = 0
|
||||
let currIndex = 1
|
||||
|
|
|
@ -80,4 +80,5 @@ export interface SubmoduleData {
|
|||
publicdirs: Array<string>
|
||||
userDB?: any
|
||||
nextdir?: string
|
||||
moduleSpecificData?: any
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue