refactor fediblock
Some checks reported errors
continuous-integration/drone/push Build was killed

This commit is contained in:
ale 2024-09-24 00:28:58 +02:00
parent e13c5f6744
commit d388fd4cd4

View File

@ -1,4 +1,5 @@
let servers let servers
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
module.exports = async (client, apex, app) => { module.exports = async (client, apex, app) => {
const util = require('./util')(apex), const util = require('./util')(apex),
constant = require('./constant'), constant = require('./constant'),
@ -18,9 +19,7 @@ module.exports = async (client, apex, app) => {
} }
}, },
requestPart = async uri => { requestPart = async uri => {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
const ac = new AbortController() const ac = new AbortController()
try {
setTimeout(() => { setTimeout(() => {
ac.abort() ac.abort()
}, 5000) }, 5000)
@ -36,14 +35,9 @@ module.exports = async (client, apex, app) => {
json = await response.json() json = await response.json()
// setImmediate(() => { ac.abort() }) // setImmediate(() => { ac.abort() })
return json return json
} catch (e) { s
// setImmediate(() => { ac.abort() })
// console.error(e)
throw e
}
}, },
scanInstance = async instance => { scanInstance = async instance => {
try {
const json = await requestPart(`https://${instance}/api/v1/instance/domain_blocks`) const json = await requestPart(`https://${instance}/api/v1/instance/domain_blocks`)
if (Array.isArray(json) && json.length > 0) { if (Array.isArray(json) && json.length > 0) {
const result = await client.search({ const result = await client.search({
@ -68,7 +62,6 @@ module.exports = async (client, apex, app) => {
requestPart(`https://${instance}/api/v1/instance/peers`) requestPart(`https://${instance}/api/v1/instance/peers`)
]) ])
if (instancelocated.length === 0) { if (instancelocated.length === 0) {
try {
await client.index({ await client.index({
index: constant.index, index: constant.index,
body: { body: {
@ -80,10 +73,6 @@ module.exports = async (client, apex, app) => {
last: new Date() last: new Date()
} }
}) })
} catch (e) {
console.error(e)
return
}
app.locals.created++ app.locals.created++
return await util.sendFederatedMessage(constant.nick, 'New Fediblock Instance', `Fediblock Instance ${instance} with ${json.length} blocks - https://${constant.apexdomain}#${instance}`, getAccount(api)) return await util.sendFederatedMessage(constant.nick, 'New Fediblock Instance', `Fediblock Instance ${instance} with ${json.length} blocks - https://${constant.apexdomain}#${instance}`, getAccount(api))
} else { } else {
@ -94,7 +83,6 @@ module.exports = async (client, apex, app) => {
|| !instancelocated[0]._source.api || !instancelocated[0]._source.api
|| !instancelocated[0]._source.nodeinfo || !instancelocated[0]._source.nodeinfo
|| !instancelocated[0]._source.peers) { || !instancelocated[0]._source.peers) {
try {
await client.update({ await client.update({
index: constant.index, index: constant.index,
id: instancelocated[0]._id, id: instancelocated[0]._id,
@ -107,33 +95,16 @@ module.exports = async (client, apex, app) => {
}, },
doc_as_upsert: true doc_as_upsert: true
}) })
} catch (e) {
console.error(e)
return
}
app.locals.updated++ 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) { 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)) 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) { 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}`, getAccount(instancelocated[0]._source.api)) 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}`, getAccount(instancelocated[0]._source.api))
} else {
return
}
} else {
return
}
} else {
return
}
} else {
return
} }
} }
} else {
return
} }
} catch (e) { }
throw e }
} }
}, },
scanPart = async function* (instancesall, index) { scanPart = async function* (instancesall, index) {
@ -154,7 +125,6 @@ module.exports = async (client, apex, app) => {
} }
}, },
scanIndex = async (server, instancesall) => { scanIndex = async (server, instancesall) => {
try {
const [api, nodeinfo, blocks] = await Promise.all([ const [api, nodeinfo, blocks] = await Promise.all([
requestPart(`https://${server}/api/v1/instance`), requestPart(`https://${server}/api/v1/instance`),
requestPart(`https://${server}/nodeinfo/2.0`), requestPart(`https://${server}/nodeinfo/2.0`),
@ -201,19 +171,9 @@ module.exports = async (client, apex, app) => {
doc_as_upsert: true doc_as_upsert: true
}) })
app.locals.updated++ app.locals.updated++
} else {
return
}
} else {
return
} }
} }
} else {
return
} }
} catch (e) {
console.error(e)
return
} }
}, },
scanReturn = async () => { scanReturn = async () => {
@ -242,7 +202,7 @@ module.exports = async (client, apex, app) => {
if (!servers || servers.length === 0) { if (!servers || servers.length === 0) {
servers = instancesall.sort(() => Math.random() - 0.5) servers = instancesall.sort(() => Math.random() - 0.5)
} }
if (server && server === constant.initialscan) { if (server === constant.initialscan) {
return await scan(servers.shift()) return await scan(servers.shift())
} }
app.locals.scannum = instancessorted.length app.locals.scannum = instancessorted.length
@ -263,7 +223,7 @@ module.exports = async (client, apex, app) => {
return await scanReturn() return await scanReturn()
} }
} catch (e) { } catch (e) {
console.error(e) // console.error(e)
return await scanReturn() return await scanReturn()
} }
} else { } else {