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

@ -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
)
}