From d388fd4cd46344de4d96211c4896e46469e91c06 Mon Sep 17 00:00:00 2001 From: ale Date: Tue, 24 Sep 2024 00:28:58 +0200 Subject: [PATCH] refactor fediblock --- lib/fediblock.js | 292 ++++++++++++++++++++--------------------------- 1 file changed, 126 insertions(+), 166 deletions(-) diff --git a/lib/fediblock.js b/lib/fediblock.js index 9fe7e31..ce01b75 100644 --- a/lib/fediblock.js +++ b/lib/fediblock.js @@ -1,4 +1,5 @@ let servers +process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 module.exports = async (client, apex, app) => { const util = require('./util')(apex), constant = require('./constant'), @@ -18,122 +19,92 @@ module.exports = async (client, apex, app) => { } }, requestPart = async uri => { - process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 const ac = new AbortController() - try { - setTimeout(() => { - ac.abort() - }, 5000) - const response = await fetch(uri, { - headers: { 'User-Agent': constant.agent }, - agent: new https.Agent({ - keepAlive: false - }), - signal: ac.signal, - keepalive: false, - timeout: 3000 + setTimeout(() => { + ac.abort() + }, 5000) + const response = await fetch(uri, { + headers: { 'User-Agent': constant.agent }, + agent: new https.Agent({ + keepAlive: false }), - json = await response.json() - // setImmediate(() => { ac.abort() }) - return json - } catch (e) { - // setImmediate(() => { ac.abort() }) - // console.error(e) - throw e - } + signal: ac.signal, + keepalive: false, + timeout: 3000 + }), + json = await response.json() + // setImmediate(() => { ac.abort() }) + return json + s }, scanInstance = async instance => { - try { - const json = await 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([ - requestPart(`https://${instance}/api/v1/instance`), - requestPart(`https://${instance}/nodeinfo/2.0`), - requestPart(`https://${instance}/api/v1/instance/peers`) - ]) - if (instancelocated.length === 0) { - try { - await client.index({ - index: constant.index, - body: { - instance, - api, - nodeinfo, - blocks, - peers, - last: new Date() - } - }) - } catch (e) { - console.error(e) - return - } - 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)) - } 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) { - try { - 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 - }) - } catch (e) { - console.error(e) - return - } - 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}`, getAccount(instancelocated[0]._source.api)) - } else { - return - } - } else { - return - } - } else { - return - } - } else { - return + const json = await 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([ + requestPart(`https://${instance}/api/v1/instance`), + requestPart(`https://${instance}/nodeinfo/2.0`), + 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}`, getAccount(api)) } else { - return + 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}`, getAccount(instancelocated[0]._source.api)) + } + } + } + } } - } catch (e) { - throw e } }, scanPart = async function* (instancesall, index) { @@ -154,66 +125,55 @@ module.exports = async (client, apex, app) => { } }, scanIndex = async (server, instancesall) => { - try { - const [api, nodeinfo, blocks] = await Promise.all([ - requestPart(`https://${server}/api/v1/instance`), - requestPart(`https://${server}/nodeinfo/2.0`), - requestPart(`https://${server}/api/v1/instance/domain_blocks`) - ]) - if (api && typeof api === 'object' && api.version) { - const result = await client.search({ - index: constant.index, - size: 1, - query: { - term: { - instance: server - } - } - }), - instancelocated = result.hits && result.hits.hits ? result.hits.hits : [] - if (instancelocated.length === 0) { - await client.index({ - index: constant.index, - body: { - instance: server, - peers: instancesall, - api, - nodeinfo, - blocks: blocks && blocks.length > 0 ? blocks : [], - last: new Date() - } - }) - app.locals.created++ - } else { - const elasticinstance = instancelocated[0]._source.peers || [] - if (Array.isArray(elasticinstance) && Array.isArray(instancesall) && instancesall.length > 0) { - if (instancesall.length !== elasticinstance.filter(i => !constant.filterdomains.some(d => i.endsWith(d))).length) { - await client.update({ - index: constant.index, - id: instancelocated[0]._id, - doc: { - peers: instancesall.length > 0 ? instancesall : elasticinstance, - api: api ? api : instancelocated[0]._source.api, - nodeinfo: nodeinfo ? nodeinfo : instancelocated[0]._source.nodeinfo, - blocks: blocks && blocks.length > 0 ? blocks : instancelocated[0]._source.blocks, - last: new Date() - }, - doc_as_upsert: true - }) - app.locals.updated++ - } else { - return - } - } else { - return + const [api, nodeinfo, blocks] = await Promise.all([ + requestPart(`https://${server}/api/v1/instance`), + requestPart(`https://${server}/nodeinfo/2.0`), + requestPart(`https://${server}/api/v1/instance/domain_blocks`) + ]) + if (api && typeof api === 'object' && api.version) { + const result = await client.search({ + index: constant.index, + size: 1, + query: { + term: { + instance: server } } + }), + instancelocated = result.hits && result.hits.hits ? result.hits.hits : [] + if (instancelocated.length === 0) { + await client.index({ + index: constant.index, + body: { + instance: server, + peers: instancesall, + api, + nodeinfo, + blocks: blocks && blocks.length > 0 ? blocks : [], + last: new Date() + } + }) + app.locals.created++ } else { - return + const elasticinstance = instancelocated[0]._source.peers || [] + if (Array.isArray(elasticinstance) && Array.isArray(instancesall) && instancesall.length > 0) { + if (instancesall.length !== elasticinstance.filter(i => !constant.filterdomains.some(d => i.endsWith(d))).length) { + await client.update({ + index: constant.index, + id: instancelocated[0]._id, + doc: { + peers: instancesall.length > 0 ? instancesall : elasticinstance, + api: api ? api : instancelocated[0]._source.api, + nodeinfo: nodeinfo ? nodeinfo : instancelocated[0]._source.nodeinfo, + blocks: blocks && blocks.length > 0 ? blocks : instancelocated[0]._source.blocks, + last: new Date() + }, + doc_as_upsert: true + }) + app.locals.updated++ + } + } } - } catch (e) { - console.error(e) - return } }, scanReturn = async () => { @@ -242,7 +202,7 @@ module.exports = async (client, apex, app) => { if (!servers || servers.length === 0) { servers = instancesall.sort(() => Math.random() - 0.5) } - if (server && server === constant.initialscan) { + if (server === constant.initialscan) { return await scan(servers.shift()) } app.locals.scannum = instancessorted.length @@ -263,7 +223,7 @@ module.exports = async (client, apex, app) => { return await scanReturn() } } catch (e) { - console.error(e) + // console.error(e) return await scanReturn() } } else {