added bit more advanced file existance checking, removed vhosts in favor or routes

This commit is contained in:
mrfry 2023-03-29 19:10:44 +02:00
parent ba89f4a342
commit 113a114821
24 changed files with 2720 additions and 2474 deletions

View file

@ -40,11 +40,9 @@ import {
} from '../../types/basicTypes'
import { loadJSON } from '../../utils/actions'
import { initWorkerPool } from '../../utils/workerPool'
import { paths } from '../../utils/files'
// files
const rootRedirectToFile = 'data/apiRootRedirectTo'
// other constants
// other paths
const moduleName = 'API'
// stuff gotten from server.js
@ -106,14 +104,14 @@ function GetApp(): ModuleType {
let rootRedirectURL = ''
function reloadRootRedirectURL() {
if (utils.FileExists(rootRedirectToFile)) {
rootRedirectURL = utils.ReadFile(rootRedirectToFile)
if (utils.FileExists(paths.rootRedirectToFile)) {
rootRedirectURL = utils.ReadFile(paths.rootRedirectToFile)
}
}
const filesToWatch = [
{
fname: rootRedirectToFile,
fname: paths.rootRedirectToFile,
logMsg: 'Root redirect URL changed',
action: reloadRootRedirectURL,
},
@ -142,7 +140,7 @@ function GetApp(): ModuleType {
app.get('/', function (req: Request, res: any) {
logger.LogReq(req)
if (reloadRootRedirectURL) {
if (rootRedirectURL) {
res.redirect(rootRedirectURL)
} else {
res.json({ msg: 'hi c:' })
@ -154,6 +152,9 @@ function GetApp(): ModuleType {
const dbsFile = publicDir + 'questionDbs.json'
// FIXME: is dataFiles only a temp variable? does this cause any problems?
if (!utils.FileExists(dbsFile)) {
utils.WriteFile('[]', dbsFile)
}
const dataFiles: Array<DataFile> = utils.ReadJSON(dbsFile)
let questionDbs: Array<QuestionDb> = loadJSON(dataFiles, publicDir)
initWorkerPool(() => questionDbs)
@ -176,14 +177,6 @@ function GetApp(): ModuleType {
// -------------------------------------------------------------------------------------------
app.get('*', function (_req: Request, res: any) {
res.status(404).render('404')
})
app.post('*', function (_req: Request, res: any) {
res.status(404).render('404')
})
function DailyAction() {
submoduleDatas.forEach((data) => {
if (data.dailyAction) {

View file

@ -51,4 +51,4 @@ const DbStruct = {
},
}
exports = DbStruct
exports.default = DbStruct

View file

@ -55,11 +55,11 @@ import {
removeCacheFromQuestion,
} from '../../../utils/qdbUtils'
import {
isJsonValidAndLogError,
PeersInfoSchema,
SelfInfoSchema,
validateJSON,
} from '../../../types/typeSchemas'
import constants from '../../../constants.json'
import { paths } from '../../../utils/files'
// TODO: remove FINALIZE-s and TOTEST-s
@ -301,7 +301,7 @@ function writeNewData(
function updateLastSync(selfInfo: PeerInfo, newDate: number) {
utils.WriteFile(
JSON.stringify({ ...selfInfo, lastSync: newDate }, null, 2),
selfInfoFile
paths.selfInfoFile
)
}
@ -321,14 +321,6 @@ function setupQuestionsForMerge(qdb: QuestionDb, peer: PeerInfo) {
}
}
// files
const peersPath = 'data/p2p/'
const peersFile = peersPath + 'peers.json'
// writes it)
const selfInfoFile = peersPath + 'selfInfo.json'
const thirdPartyPeersFile = peersPath + 'thirdPartyPeers.json'
const keyFile = peersPath + 'key' // key.pub key.priv
function setup(data: SubmoduleData): Submodule {
const {
app,
@ -344,37 +336,32 @@ function setup(data: SubmoduleData): Submodule {
// SETUP
// ---------------------------------------------------------------------------------------
if (!utils.FileExists(peersFile)) {
if (!utils.FileExists(paths.peersFile)) {
logger.Log(
`Warning: peers file was missing, so it was created`,
'yellowbg'
)
utils.CreatePath(peersPath)
utils.WriteFile('[]', peersFile)
utils.CreatePath(paths.peersPath)
utils.WriteFile('[]', paths.peersFile)
}
if (!utils.FileExists(selfInfoFile)) {
logger.Log(
'Self info file for p2p does not exist! P2P functionality will not be loaded',
'redbg'
)
logger.Log(
`File should be at: ${selfInfoFile} with the interface 'PeerInfo'`
)
throw new Error('p2p error')
if (!utils.FileExists(paths.selfInfoFile)) {
const msg = `Self info file for p2p does not exist! (${paths.selfInfoFile}) P2P functionality will not be loaded`
logger.Log(msg, 'redbg')
return {}
}
let publicKey: string
let privateKey: string
if (
!utils.FileExists(keyFile + '.priv') ||
!utils.FileExists(keyFile + '.pub')
!utils.FileExists(paths.keyFile + '.priv') ||
!utils.FileExists(paths.keyFile + '.pub')
) {
createKeyPair().then(({ publicKey: pubk, privateKey: privk }) => {
// at first start there won't be a keypair available until this finishes
utils.WriteFile(pubk, keyFile + '.pub')
utils.WriteFile(privk, keyFile + '.priv')
utils.WriteFile(pubk, paths.keyFile + '.pub')
utils.WriteFile(privk, paths.keyFile + '.priv')
publicKey = pubk
privateKey = privk
@ -384,35 +371,21 @@ function setup(data: SubmoduleData): Submodule {
'yellowbg'
)
} else {
publicKey = utils.ReadFile(keyFile + '.pub')
privateKey = utils.ReadFile(keyFile + '.priv')
publicKey = utils.ReadFile(paths.keyFile + '.pub')
privateKey = utils.ReadFile(paths.keyFile + '.priv')
// checking only here, because if it got generated in the other branch then it must be good
if (!isKeypairValid(publicKey, privateKey)) {
logger.Log('Loaded keypair is not valid!', 'redbg')
}
}
let peers: PeerInfo[] = utils.ReadJSON(peersFile)
let selfInfo: PeerInfo = utils.ReadJSON(selfInfoFile)
const { isValid: isPeersValid, errorMsg: peersErrorMsg } = validateJSON(
peers,
PeersInfoSchema
)
if (!isPeersValid) {
logger.Log(`Peers file (${peersFile}) has invalid contents!`, 'redbg')
peersErrorMsg.forEach((x) => logger.Log(x, 'red'))
let peers: PeerInfo[] = utils.ReadJSON(paths.peersFile)
let selfInfo: PeerInfo = utils.ReadJSON(paths.selfInfoFile)
if (!isJsonValidAndLogError(peers, PeersInfoSchema, paths.peersFile)) {
throw new Error('Invalid peers file')
}
const { isValid: isSelfInfoValid, errorMsg: selfInfoErrorMsg } =
validateJSON(selfInfo, SelfInfoSchema)
if (!isSelfInfoValid) {
logger.Log(
`Self info file (${selfInfoFile}) has invalid contents!`,
'redbg'
)
selfInfoErrorMsg.forEach((x) => logger.Log(x, 'red'))
if (!isJsonValidAndLogError(selfInfo, SelfInfoSchema, paths.selfInfoFile)) {
throw new Error('Invalid peers file')
}
// self info file is not required to have the publicKey, as it is always added on init
@ -420,17 +393,17 @@ function setup(data: SubmoduleData): Submodule {
const filesToWatch = [
{
fname: peersFile,
fname: paths.peersFile,
logMsg: 'Peers file updated',
action: () => {
peers = utils.ReadJSON(peersFile)
peers = utils.ReadJSON(paths.peersFile)
},
},
{
fname: selfInfoFile,
fname: paths.selfInfoFile,
logMsg: 'P2P self info file changed',
action: () => {
selfInfo = utils.ReadJSON(selfInfoFile)
selfInfo = utils.ReadJSON(paths.selfInfoFile)
selfInfo.publicKey = publicKey
},
},
@ -466,22 +439,20 @@ function setup(data: SubmoduleData): Submodule {
}
result.serverRevision = utils.getGitRevision(__dirname)
result.scriptRevision = utils.getGitRevision(
constants.moodleTestUserscriptDir
)
result.qminingPageRevision = utils.getGitRevision(
constants.qminingPageDir
paths.moodleTestUserscriptDir
)
result.qminingPageRevision = utils.getGitRevision(paths.qminingPageDir)
result.dataEditorRevision = utils.getGitRevision(
constants.dataEditorPageDir
paths.dataEditorPageDir
)
result.qminingPageBuildTime = utils
.statFile(constants.qminingIndexPath)
.statFile(paths.qminingIndexPath)
?.mtime.getTime()
result.serverBuildTime = utils
.statFile(constants.serverPath)
.statFile(paths.serverPath)
?.mtime.getTime()
result.dataEditorBuildTime = utils
.statFile(constants.dataEditorIndexPath)
.statFile(paths.dataEditorIndexPath)
?.mtime.getTime()
result.scriptVersion = utils.getScriptVersion()
@ -556,7 +527,7 @@ function setup(data: SubmoduleData): Submodule {
async function syncData() {
if (peers.length === 0) {
logger.Log(
`There are no peers specified in ${peersFile}, aborting sync`,
`There are no peers specified in ${paths.peersFile}, aborting sync`,
'yellowbg'
)
return {
@ -687,14 +658,14 @@ function setup(data: SubmoduleData): Submodule {
if (thirdPartyPeers.length > 0) {
utils.WriteFile(
JSON.stringify(thirdPartyPeers, null, 2),
thirdPartyPeersFile
paths.thirdPartyPeersFile
)
logger.Log(
`\tPeers reported ${logger.C('green')}${
thirdPartyPeers.length
}${logger.C()} third party peer(s) not connected to this server. See ${logger.C(
'blue'
)}${thirdPartyPeersFile}${logger.C()} for details`
)}${paths.thirdPartyPeersFile}${logger.C()} for details`
)
}
@ -830,7 +801,7 @@ function setup(data: SubmoduleData): Submodule {
utils.WriteFile(
JSON.stringify(updatedPeersFile, null, 2),
peersFile
paths.peersFile
)
}

View file

@ -59,7 +59,12 @@ import {
getSubjNameWithoutYear,
SearchResultQuestion,
} from '../../../utils/qdbUtils'
import { paths } from '../../../utils/files'
import constants from '../../../constants.json'
import {
isJsonValidAndLogError,
TestUsersSchema,
} from '../../../types/typeSchemas'
interface SavedQuestionData {
fname: string
@ -78,14 +83,6 @@ interface SavedQuestionData {
// }
const line = '====================================================' // lol
const registeredScriptsFile = 'stats/registeredScripts.json'
const testUsersFile = 'data/testUsers.json'
const askedQuestionFile = 'stats/askedQuestions'
const recievedQuestionFile = 'stats/recievedQuestions'
const savedQuestionsFileName = 'savedQuestions.json'
const oldMotdFile = 'publicDirs/qminingPublic/oldMotd'
const dailyDataCountFile = 'stats/dailyDataCount'
const dataEditsLog = 'stats/dataEdits'
function getSubjCount(qdbs: QuestionDb[]): number {
return qdbs.reduce((acc, qdb) => {
@ -114,7 +111,7 @@ function ExportDailyDataCount(questionDbs: QuestionDb[], userDB: Database) {
questionDbsCount: questionDbs.length,
userCount: dbtools.TableInfo(userDB, 'users').dataCount,
}),
dailyDataCountFile
paths.dailyDataCountFile
)
}
@ -157,17 +154,6 @@ function getDetailedRes(questionDbs: QuestionDb[]) {
})
}
function getMotd(version: string, motd: string) {
if (version) {
if (version.startsWith('2.0.')) {
if (utils.FileExists(oldMotdFile)) {
return utils.ReadFile(oldMotdFile)
}
}
}
return motd
}
function searchInDbs(
question: Question,
subj: string,
@ -268,7 +254,7 @@ function writeAskData(body: QuestionFromScript) {
towrite += JSON.stringify(body)
towrite +=
'\n------------------------------------------------------------------------------\n'
utils.AppendToFile(towrite, askedQuestionFile)
utils.AppendToFile(towrite, paths.askedQuestionFile)
} catch (err) {
logger.Log('Error writing revieved /ask POST data')
console.error(err)
@ -283,7 +269,7 @@ function writeIsAddingData(body: RecievedData) {
towrite += JSON.stringify(body)
towrite +=
'\n------------------------------------------------------------------------------\n'
utils.AppendToFile(towrite, recievedQuestionFile)
utils.AppendToFile(towrite, paths.recievedQuestionFile)
} catch (err) {
logger.Log('Error writing revieved /ask POST data')
console.error(err)
@ -312,7 +298,7 @@ function saveQuestion(
const fname = `${utils.GetDateString()}_${userid}_${testUrl}.json`
const subject = getSubjNameWithoutYear(subj).replace(/\//g, '-')
const subjPath = `${savedQuestionsDir}/${subject}`
const savedSubjQuestionsFilePath = `${subjPath}/${savedQuestionsFileName}`
const savedSubjQuestionsFilePath = `${subjPath}/${constants.savedQuestionsFileName}`
utils.CreatePath(subjPath, true)
if (!utils.FileExists(savedSubjQuestionsFilePath)) {
@ -358,9 +344,7 @@ function saveQuestion(
}
function loadSupportedSites() {
const script = utils
.ReadFile(constants.moodleTestUserscriptPath)
.split('\n')
const script = utils.ReadFile(paths.moodleTestUserscriptPath).split('\n')
let i = 0
let stayIn = true
@ -393,11 +377,18 @@ function LoadMOTD(motdFile: string) {
}
function LoadTestUsers() {
let testUsers = utils.ReadJSON(testUsersFile)
if (testUsers) {
testUsers = testUsers.userIds
if (!utils.FileExists(paths.testUsersFile)) {
utils.WriteFile('{}', paths.testUsersFile)
}
const testUsers = utils.ReadJSON<{ userIds: number[] }>(paths.testUsersFile)
if (
!isJsonValidAndLogError(testUsers, TestUsersSchema, paths.testUsersFile)
) {
return []
} else {
return testUsers.userIds
}
return testUsers
}
function getNewQdb(
@ -481,14 +472,14 @@ function setup(data: SubmoduleData): Submodule {
},
},
{
fname: testUsersFile,
fname: paths.testUsersFile,
logMsg: 'Test Users file changed',
action: () => {
testUsers = LoadTestUsers()
},
},
{
fname: constants.moodleTestUserscriptPath,
fname: paths.moodleTestUserscriptPath,
logMsg: 'User script file changed',
action: () => {
version = utils.getScriptVersion()
@ -754,7 +745,7 @@ function setup(data: SubmoduleData): Submodule {
result.version = version
}
if (req.query.motd) {
result.motd = getMotd(req.query.cversion, motd)
result.motd = motd
}
res.json(result)
})
@ -762,13 +753,13 @@ function setup(data: SubmoduleData): Submodule {
app.post('/registerscript', function (req: Request, res) {
logger.LogReq(req)
if (!utils.FileExists(registeredScriptsFile)) {
utils.WriteFile('[]', registeredScriptsFile)
if (!utils.FileExists(paths.registeredScriptsFile)) {
utils.WriteFile('[]', paths.registeredScriptsFile)
}
const ua: string = req.headers['user-agent']
const registeredScripts: RegisteredUserEntry[] = utils.ReadJSON(
registeredScriptsFile
paths.registeredScriptsFile
)
const { cid, uid, version, installSource, date } = req.body
@ -810,7 +801,7 @@ function setup(data: SubmoduleData): Submodule {
utils.WriteFile(
JSON.stringify(registeredScripts, null, 2),
registeredScriptsFile
paths.registeredScriptsFile
)
res.json({ msg: 'done' })
@ -828,7 +819,7 @@ function setup(data: SubmoduleData): Submodule {
})
res.json({
savedQuestionsFileName: savedQuestionsFileName,
savedQuestionsFileName: constants.savedQuestionsFileName,
subjects: files.map((subj) => {
return {
name: subj,
@ -844,7 +835,7 @@ function setup(data: SubmoduleData): Submodule {
const subj = req.body.subj
const file = req.body.file
const savedQuestionsPath = `${savedQuestionsDir}/${subj}/${savedQuestionsFileName}`
const savedQuestionsPath = `${savedQuestionsDir}/${subj}/${constants.savedQuestionsFileName}`
const savedQuestions: SavedQuestionData[] =
utils.ReadJSON(savedQuestionsPath)
let path = `${savedQuestionsDir}/${subj}/${file}`
@ -943,11 +934,11 @@ function setup(data: SubmoduleData): Submodule {
)
utils.AppendToFile(
`${date}: User ${user.id} deleted a question from '${subjName}' (index: ${index})`,
dataEditsLog
paths.dataEditsLog
)
utils.AppendToFile(
JSON.stringify(deletedQuestion, null, 2),
dataEditsLog
paths.dataEditsLog
)
}
@ -959,7 +950,7 @@ function setup(data: SubmoduleData): Submodule {
)
utils.AppendToFile(
`${date}: User ${user.id} edited a question in '${subjName}' (index: ${index})`,
dataEditsLog
paths.dataEditsLog
)
utils.AppendToFile(
JSON.stringify(
@ -970,7 +961,7 @@ function setup(data: SubmoduleData): Submodule {
null,
2
),
dataEditsLog
paths.dataEditsLog
)
}
@ -982,7 +973,7 @@ function setup(data: SubmoduleData): Submodule {
)
utils.AppendToFile(
`${date} User #${user.id} modified '${subjName}'. Edited: ${deletedQuestions.length}, deleted: ${deletedQuestions.length}`,
dataEditsLog
paths.dataEditsLog
)
utils.AppendToFile(
JSON.stringify(
@ -993,7 +984,7 @@ function setup(data: SubmoduleData): Submodule {
null,
2
),
dataEditsLog
paths.dataEditsLog
)
}
// ------------------

View file

@ -63,6 +63,19 @@ function BackupDB(usersDbBackupPath: string, userDB: Database) {
})
}
function createDefaultUser(userDb: Database) {
logger.Log('The user DB is empty, creating user #1', 'yellowbg')
const pw = uuidv4()
const insertRes = dbtools.Insert(userDb, 'users', {
pw: pw,
avaiblePWRequests: 0,
created: utils.GetDateString(),
})
logger.Log('ID and PW for user #1: ', 'yellowbg')
console.log(`ID: #${insertRes.lastInsertRowid}, PW: "${pw}"`)
logger.Log('It can be also viewed from the user db file.')
}
// TODO: figure out if this is needed
// const validationTokenNameFile = 'data/validationTokenName'
// function readValidationTokenName() {
@ -83,12 +96,13 @@ function setup(data: SubmoduleData): Submodule {
domain = domain.join('.') // "frylabs.net"
logger.DebugLog(`Cookie domain: ${domain}`, 'cookie', 1)
logger.Log(
`User count: ${dbtools
.TableInfo(userDB, 'users')
.dataCount.toLocaleString()} users`,
'blue'
)
const userCount = dbtools
.TableInfo(userDB, 'users')
.dataCount.toLocaleString()
logger.Log(`User count: ${userCount} users`, 'blue')
if (+userCount === 0) {
createDefaultUser(userDB)
}
app.get('/avaiblePWS', (req: Request, res: any) => {
logger.LogReq(req)

View file

@ -97,4 +97,4 @@ const DbStruct = {
},
}
exports = DbStruct
exports.default = DbStruct