This commit is contained in:
parent
e13c5f6744
commit
d388fd4cd4
292
lib/fediblock.js
292
lib/fediblock.js
@ -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,122 +19,92 @@ 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)
|
const response = await fetch(uri, {
|
||||||
const response = await fetch(uri, {
|
headers: { 'User-Agent': constant.agent },
|
||||||
headers: { 'User-Agent': constant.agent },
|
agent: new https.Agent({
|
||||||
agent: new https.Agent({
|
keepAlive: false
|
||||||
keepAlive: false
|
|
||||||
}),
|
|
||||||
signal: ac.signal,
|
|
||||||
keepalive: false,
|
|
||||||
timeout: 3000
|
|
||||||
}),
|
}),
|
||||||
json = await response.json()
|
signal: ac.signal,
|
||||||
// setImmediate(() => { ac.abort() })
|
keepalive: false,
|
||||||
return json
|
timeout: 3000
|
||||||
} catch (e) {
|
}),
|
||||||
// setImmediate(() => { ac.abort() })
|
json = await response.json()
|
||||||
// console.error(e)
|
// setImmediate(() => { ac.abort() })
|
||||||
throw e
|
return json
|
||||||
}
|
s
|
||||||
},
|
},
|
||||||
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({
|
index: constant.index,
|
||||||
index: constant.index,
|
size: 1,
|
||||||
size: 1,
|
query: {
|
||||||
query: {
|
term: {
|
||||||
term: {
|
instance: instance
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
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 {
|
} 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) {
|
scanPart = async function* (instancesall, index) {
|
||||||
@ -154,66 +125,55 @@ 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`),
|
requestPart(`https://${server}/api/v1/instance/domain_blocks`)
|
||||||
requestPart(`https://${server}/api/v1/instance/domain_blocks`)
|
])
|
||||||
])
|
if (api && typeof api === 'object' && api.version) {
|
||||||
if (api && typeof api === 'object' && api.version) {
|
const result = await client.search({
|
||||||
const result = await client.search({
|
index: constant.index,
|
||||||
index: constant.index,
|
size: 1,
|
||||||
size: 1,
|
query: {
|
||||||
query: {
|
term: {
|
||||||
term: {
|
instance: server
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}),
|
||||||
|
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 {
|
} 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 () => {
|
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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user