try catch
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
ale 2024-09-21 03:14:28 +02:00
commit 83a28e3ad3
4 changed files with 8 additions and 109 deletions

View File

@ -1,4 +1,4 @@
# Fediblock Instance [![Build Status](https://drone.manalejandro.com/api/badges/ale/fediblock-instance/status.svg)](https://drone.manalejandro.com/ale/fediblock-instance) # Fediblock Instance [![Build Status](https://drone.manalejandro.com/api/badges/ale/fediblock-instance/status.svg)](https://drone.manalejandro.com/ale/fediblock-instance) [![Quality Gate Status](https://sonar.manalejandro.com/api/project_badges/measure?project=ale%3Afediblock-instance&metric=alert_status&token=sqb_b85f22162c314a3c5d781cf356e90ce304ce6a13)](https://sonar.manalejandro.com/dashboard?id=ale%3Afediblock-instance)
## Another instance to search public blocks and do stats and more, like one FBA but with NodeJS powers. ## Another instance to search public blocks and do stats and more, like one FBA but with NodeJS powers.

View File

@ -148,8 +148,10 @@ module.exports = async (client, apex, app) => {
console.error(e) console.error(e)
} }
} }
global.gc()
yield 0 yield 0
} catch (e) { } catch (e) {
global.gc()
console.error(e) console.error(e)
yield 1 yield 1
} }
@ -238,7 +240,7 @@ module.exports = async (client, apex, app) => {
instancessorted = instancesall.sort((a, b) => a < b ? -1 : a > b ? 1 : 0), instancessorted = instancesall.sort((a, b) => a < b ? -1 : a > b ? 1 : 0),
parts = [], parts = [],
split = workers, split = workers,
chunkSize = Math.floor(instancessorted.length / split) chunkSize = Math.ceil(instancessorted.length / split)
await scanIndex(server, instancesall) await scanIndex(server, instancesall)
if (instancesall.length > 0) { if (instancesall.length > 0) {
if (!servers || servers.length === 0) { if (!servers || servers.length === 0) {

View File

@ -1,106 +0,0 @@
module.exports = async (client, apex, app) => {
util = require('./util')(apex),
constant = require('./constant'),
{ parentPort } = require('worker_threads'),
scanInstance = async instance => {
try {
const json = await util.requestPart(`https://${instance}/api/v1/instance/domain_blocks`)
if (Array.isArray(json) && json.length > 0) {
const result = await client.search({
index: constant.index,
size: 1,
query: {
term: {
instance: instance
}
}
}),
instancelocated = result.hits && result.hits.hits ? result.hits.hits : [],
blocks = json.map(block => {
if (block.comment && block.comment.length > 8190) {
block.comment = block.comment.slice(0, 8190)
}
return block
}),
[api, nodeinfo, peers] = await Promise.all([
util.requestPart(`https://${instance}/api/v1/instance`),
util.requestPart(`https://${instance}/nodeinfo/2.0`),
util.requestPart(`https://${instance}/api/v1/instance/peers`)
])
if (instancelocated.length === 0) {
await client.index({
index: constant.index,
body: {
instance,
api,
nodeinfo,
blocks,
peers,
last: new Date()
}
})
app.locals.created++
return await util.sendFederatedMessage(constant.nick, 'New Fediblock Instance', `Fediblock Instance ${instance} with ${json.length} blocks - https://${constant.apexdomain}#${instance}`, util.getAccount(api))
} else {
const elasticinstance = instancelocated[0]._source.blocks || []
if (Array.isArray(elasticinstance)) {
if (json.length !== elasticinstance.length
|| (instancelocated[0]._source.last && instancelocated[0]._source.last < new Date(Date.now() - 2678400000))
|| !instancelocated[0]._source.api
|| !instancelocated[0]._source.nodeinfo
|| !instancelocated[0]._source.peers) {
await client.update({
index: constant.index,
id: instancelocated[0]._id,
doc: {
api: api ? api : instancelocated[0]._source.api,
nodeinfo: nodeinfo ? nodeinfo : instancelocated[0]._source.nodeinfo,
blocks: blocks && blocks.length > 0 ? blocks : elasticinstance,
peers: peers && peers.length > 0 ? peers : instancelocated[0]._source.peers,
last: new Date()
},
doc_as_upsert: true
})
app.locals.updated++
if (instancelocated[0]._source.api && instancelocated[0]._source.api.uri && instancelocated[0]._source.api.contact_account && instancelocated[0]._source.api.contact_account.acct) {
const difference = blocks.filter(block => block.domain && block.domain.trim().length > 0 && !elasticinstance.some(instance => block.domain === instance.domain))
if (difference.length > 0 && difference.length < 50) {
return await util.sendFederatedMessage(constant.nick, 'Detected #Fediblock by Fediblock Instance', `You blocked new instances: ${difference.map(d => d.domain).join(', ')} - https://${constant.apexdomain}#${instance}`, util.getAccount(instancelocated[0]._source.api))
} else {
return
}
} else {
return
}
} else {
return
}
} else {
return
}
}
} else {
return
}
} catch (e) {
console.error(e)
return
}
},
scanPart = async (instancesall, index) => {
for (const instance of instancesall) {
try {
app.locals.scan.emit('data', 'data: ' + (app.locals.scannum > 0 ? '(' + app.locals.scannum-- + ':' + (index + 1) + '): ' + instance : ': ' + instance) + '\n\n')
app.locals.peers++
await scanInstance(instance)
} catch (e) {
console.error(e)
}
}
return
},
work = (part, index) => {
return parentPort.postMessage({ func: scanPart, args: [part, index] })
}
}

View File

@ -30,7 +30,10 @@
</h4> </h4>
<h3>Search in <a href="/api/stats" target="_blank"><span id="count"></span></a> public instances<span id="apigit"> - <h3>Search in <a href="/api/stats" target="_blank"><span id="count"></span></a> public instances<span id="apigit"> -
<a href="api-docs" target="_blank">API docs</a> - <a <a href="api-docs" target="_blank">API docs</a> - <a
href="https://git.manalejandro.com/ale/fediblock-instance" target="_blank">Git</a></span></h3> href="https://git.manalejandro.com/ale/fediblock-instance" target="_blank">Git</a>
<img
src="https://sonar.manalejandro.com/api/project_badges/measure?project=ale%3Afediblock-instance&metric=alert_status&token=sqb_b85f22162c314a3c5d781cf356e90ce304ce6a13" />
<img src="https://drone.manalejandro.com/api/badges/ale/fediblock-instance/status.svg" /></span></h3>
<section> <section>
<input type="text" id="instance" name="instance" placeholder="Type the name of the instance" /> <input type="text" id="instance" name="instance" placeholder="Type the name of the instance" />
<button id="reverse" title="Reverse search...">Reverse</button> <button id="reverse" title="Reverse search...">Reverse</button>