From 5deb70a0b00e464be5c5b2dda2636a40543d49ac Mon Sep 17 00:00:00 2001 From: ale Date: Sun, 10 Nov 2024 02:36:04 +0100 Subject: [PATCH] refactor server --- lib/fediblock.js | 2 +- server.js | 86 ++++++++++++++++++++++++++---------------------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/lib/fediblock.js b/lib/fediblock.js index 3da5f81..e932dbd 100644 --- a/lib/fediblock.js +++ b/lib/fediblock.js @@ -238,5 +238,5 @@ module.exports = async (client, apex, app) => { job = schedule.scheduleJob('0 ' + constant.schedule + ' * * *', async () => { await util.sendFederatedMessage(constant.nick, null, 'Scanning ' + app.locals.server + ' instance with ' + app.locals.scantotal + ' peers\nScanned ' + app.locals.peers + ' peers from ' + app.locals.instances + ' instances, ' + app.locals.created + ' created, ' + app.locals.updated + ' updated\nhttps://' + constant.apexdomain) }) - await scanReturn() + return await scanReturn() } diff --git a/server.js b/server.js index 0275d13..b5bbe7a 100644 --- a/server.js +++ b/server.js @@ -54,7 +54,48 @@ const apexinstance = require('./lib/apex'), }, itemsPerPage: 200 }), - server = http.createServer(app).listen(4000, () => { + register = async () => { + const admin = await apex.store.getObject(apex.utils.usernameToIRI('admin'), true) + if (!admin) { + const adminaccount = await apex.createActor('admin', 'Fediblock Admin', 'Fediblock Admin - https://' + constant.apexdomain, + { type: 'Image', mediaType: 'image/png', url: constant.icon }, 'Service'), + keys = generateKeyPairSync('rsa', { + modulusLength: 2048, + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs8', + format: 'pem' + } + }) + adminaccount.publicKey[0].publicKeyPem[0] = keys.publicKey + adminaccount._meta.privateKey = keys.privateKey + await apex.store.saveObject(adminaccount) + const bot = await apex.createActor(constant.nick, 'Fediblock Bot', 'Fediblock #Bot - https://' + constant.apexdomain, + { type: 'Image', mediaType: 'image/png', url: constant.icon }, 'Person'), + keysbot = generateKeyPairSync('rsa', { + modulusLength: 2048, + publicKeyEncoding: { + type: 'spki', + format: 'pem' + }, + privateKeyEncoding: { + type: 'pkcs8', + format: 'pem' + } + }) + bot.publicKey[0].publicKeyPem[0] = keysbot.publicKey + bot._meta.privateKey = keysbot.privateKey + await apex.store.saveObject(bot) + apex.systemUser = adminaccount + apex.store.setup(adminaccount) + } else { + apex.systemUser = admin + } + }, + connect = () => { mongoclient.connect() .then(async () => { const exists = await client.indices.exists({ index: constant.index }) @@ -69,45 +110,7 @@ const apexinstance = require('./lib/apex'), }) .then(async () => { try { - const admin = await apex.store.getObject(apex.utils.usernameToIRI('admin'), true) - if (!admin) { - const adminaccount = await apex.createActor('admin', 'Fediblock Admin', 'Fediblock Admin - https://' + constant.apexdomain, - { type: 'Image', mediaType: 'image/png', url: constant.icon }, 'Service'), - keys = generateKeyPairSync('rsa', { - modulusLength: 2048, - publicKeyEncoding: { - type: 'spki', - format: 'pem' - }, - privateKeyEncoding: { - type: 'pkcs8', - format: 'pem' - } - }) - adminaccount.publicKey[0].publicKeyPem[0] = keys.publicKey - adminaccount._meta.privateKey = keys.privateKey - await apex.store.saveObject(adminaccount) - const bot = await apex.createActor(constant.nick, 'Fediblock Bot', 'Fediblock #Bot - https://' + constant.apexdomain, - { type: 'Image', mediaType: 'image/png', url: constant.icon }, 'Person'), - keysbot = generateKeyPairSync('rsa', { - modulusLength: 2048, - publicKeyEncoding: { - type: 'spki', - format: 'pem' - }, - privateKeyEncoding: { - type: 'pkcs8', - format: 'pem' - } - }) - bot.publicKey[0].publicKeyPem[0] = keysbot.publicKey - bot._meta.privateKey = keysbot.privateKey - await apex.store.saveObject(bot) - apex.systemUser = adminaccount - apex.store.setup(adminaccount) - } else { - apex.systemUser = admin - } + await register() taskdeletedup(client) await apex.startDelivery() console.log('Server listening on ' + server.address().address + ':' + server.address().port) @@ -116,6 +119,9 @@ const apexinstance = require('./lib/apex'), console.error(e) } }) + }, + server = http.createServer(app).listen(4000, () => { + connect() }) app.locals.scan = new events.EventEmitter()