gancio-upstream/server/federation/helpers.js

270 lines
9.9 KiB
JavaScript
Raw Normal View History

2020-02-05 00:48:55 +01:00
const axios = require('axios')
2019-07-31 02:01:21 +02:00
const crypto = require('crypto')
2021-09-27 10:42:17 +02:00
const config = require('../config')
const httpSignature = require('@peertube/http-signature')
const { APUser, Instance } = require('../api/models/models')
2019-09-11 12:00:13 +02:00
const url = require('url')
2019-09-25 14:38:16 +02:00
const settingsController = require('../api/controller/settings')
2021-03-05 14:17:10 +01:00
const log = require('../log')
2019-07-30 18:32:26 +02:00
const Helpers = {
// ignore unimplemented ping url from fediverse
2019-10-28 17:33:20 +01:00
spamFilter (req, res, next) {
const urlToIgnore = [
'/api/v1/instance',
'/api/meta',
2019-09-18 12:55:33 +02:00
'/api/statusnet/version.json',
'/api/gnusocial/version.json',
'/api/statusnet/config.json',
2020-01-30 15:33:12 +01:00
'/status.php',
'/siteinfo.json',
'/friendika/json',
'/friendica/json',
2019-10-28 17:33:20 +01:00
'/poco'
]
2021-03-05 14:17:10 +01:00
if (urlToIgnore.includes(req.path)) {
log.debug(`Ignore noisy fediverse ${req.path}`)
return res.status(404).send('Not Found')
}
next()
},
async signAndSend (message, inbox, method='post') {
log.debug('[FEDI] Sign and send %s to %s', message, inbox)
2019-10-30 15:01:15 +01:00
const inboxUrl = new url.URL(inbox)
const privkey = settingsController.secretSettings.privateKey
2019-07-30 18:32:26 +02:00
const signer = crypto.createSign('sha256')
2019-07-31 01:43:08 +02:00
const d = new Date()
let header
let digest
if (method === 'post') {
digest = crypto.createHash('sha256')
.update(message)
.digest('base64')
const stringToSign = `(request-target): post ${inboxUrl.pathname}\nhost: ${inboxUrl.hostname}\ndate: ${d.toUTCString()}\ndigest: SHA-256=${digest}`
signer.update(stringToSign)
signer.end()
const signature = signer.sign(privkey)
const signature_b64 = signature.toString('base64')
header = `keyId="${config.baseurl}/federation/u/${settingsController.settings.instance_name}#main-key",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="${signature_b64}"`
} else {
const stringToSign = `(request-target): get ${inboxUrl.pathname}\nhost: ${inboxUrl.hostname}\ndate: ${d.toUTCString()}`
signer.update(stringToSign)
signer.end()
const signature = signer.sign(privkey)
const signature_b64 = signature.toString('base64')
header = `keyId="${config.baseurl}/federation/u/${settingsController.settings.instance_name}#main-key",algorithm="rsa-sha256",headers="(request-target) host date",signature="${signature_b64}"`
}
2020-01-27 00:47:03 +01:00
try {
2020-02-05 00:48:55 +01:00
const ret = await axios(inbox, {
2020-01-27 00:47:03 +01:00
headers: {
Host: inboxUrl.hostname,
Date: d.toUTCString(),
Signature: header,
...(method === 'post' && ({ Digest: `SHA-256=${digest}` })),
'Content-Type': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
2020-01-27 00:47:03 +01:00
},
method,
...( method === 'post' && ({ data: message}))
2020-01-27 00:47:03 +01:00
})
log.debug(`[FEDI] signed ${ret.status} => %s`, ret.data)
return ret.data
2020-01-27 00:47:03 +01:00
} catch (e) {
log.error("[FEDI] Error in sign and send [%s]: %s", inbox, e?.response?.data?.error ?? e?.response?.statusMessage + ' ' + String(e))
2020-01-27 00:47:03 +01:00
}
2019-07-31 01:43:08 +02:00
},
2019-09-11 12:00:13 +02:00
async sendEvent (event, type = 'Create') {
2019-09-25 14:38:16 +02:00
if (!settingsController.settings.enable_federation) {
2021-04-28 12:44:26 +02:00
log.info('event not send, federation disabled')
return
}
2019-12-06 00:49:44 +01:00
const followers = await APUser.findAll({ where: { follower: true } })
log.debug("[FEDI] Sending to [%s]", followers.map(f => f.ap_id).join(', '))
2019-10-30 15:01:15 +01:00
const recipients = {}
followers.forEach(follower => {
const sharedInbox = follower?.object?.endpoints?.sharedInbox ?? follower?.object?.inbox
2019-10-28 17:33:20 +01:00
if (!recipients[sharedInbox]) { recipients[sharedInbox] = [] }
2019-09-18 12:55:33 +02:00
recipients[sharedInbox].push(follower.ap_id)
})
2019-10-28 17:33:20 +01:00
for (const sharedInbox in recipients) {
2021-03-05 14:17:10 +01:00
log.debug(`Notify ${sharedInbox} with event ${event.title} cc => ${recipients[sharedInbox].length}`)
2019-09-11 13:12:05 +02:00
const body = {
2019-09-11 21:20:44 +02:00
id: `${config.baseurl}/federation/m/${event.id}#create`,
type,
2020-11-06 11:05:05 +01:00
to: ['https://www.w3.org/ns/activitystreams#Public'],
cc: [...recipients[sharedInbox], `${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`],
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
object: event.toAP(settingsController.settings, recipients[sharedInbox])
2019-09-11 13:12:05 +02:00
}
2019-09-26 22:46:04 +02:00
body['@context'] = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
2020-11-06 11:05:05 +01:00
{
2021-07-19 12:29:35 +02:00
Hashtag: 'as:Hashtag',
focalPoint: { '@container': '@list', '@id': 'toot:focalPoint' }
2020-11-06 11:05:05 +01:00
}]
2021-03-18 17:15:50 +01:00
await Helpers.signAndSend(JSON.stringify(body), sharedInbox)
2019-09-11 12:00:13 +02:00
}
2019-08-02 13:43:28 +02:00
},
async followActor (actor) {
log.debug(`Following actor ${actor.ap_id}`)
const body = {
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${config.baseurl}/federation/m/${actor.ap_id}#follow`,
type: 'Follow',
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
object: actor.ap_id
}
await Helpers.signAndSend(JSON.stringify(body), actor.object.endpoints?.sharedInbox || actor.object.inbox)
await actor.update({ following: 1 })
},
async unfollowActor (actor) {
log.debug(`Unfollowing actor ${actor.ap_id}`)
const body = {
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${config.baseurl}/federation/m/${actor.ap_id}#follow`,
type: 'Unfollow',
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
object: actor.ap_id
}
await Helpers.signAndSend(JSON.stringify(body), actor.object.endpoints?.sharedInbox || actor.object.inbox)
return actor.update({ following: 0 })
},
2019-10-30 15:01:15 +01:00
async getActor (URL, instance, force = false) {
2019-09-12 14:59:51 +02:00
let fedi_user
2019-10-30 15:01:15 +01:00
2019-08-09 01:58:11 +02:00
// try with cache first
2019-10-30 15:01:15 +01:00
if (!force) {
fedi_user = await APUser.findByPk(URL, { include: Instance })
2019-10-30 15:01:15 +01:00
if (fedi_user) {
if (!fedi_user.instances) {
fedi_user.setInstance(instance)
}
return fedi_user
2019-10-30 15:01:15 +01:00
}
}
2019-09-12 14:59:51 +02:00
fedi_user = await Helpers.signAndSend('', URL, 'get')
2020-06-01 19:14:46 +02:00
.catch(e => {
log.error(`[FEDI] getActor ${URL}: %s`, e?.response?.data?.error ?? String(e) )
2020-06-01 19:14:46 +02:00
return false
})
2019-10-30 15:01:15 +01:00
2019-09-12 14:59:51 +02:00
if (fedi_user) {
2021-04-28 12:44:26 +02:00
log.info(`Create a new AP User => ${URL}`)
fedi_user = await APUser.create({ ap_id: URL, object: fedi_user })
2019-09-12 14:59:51 +02:00
}
return fedi_user
2019-08-02 13:43:28 +02:00
},
async getNodeInfo (instance_url) {
const versions = await axios.get(`${instance_url}/.well-known/nodeinfo`, { headers: { Accept: 'application/json' } }).then(res => res.data)
console.error(versions)
if (versions.links) {
const choosen = versions.links.find(l => l.rel === 'http://nodeinfo.diaspora.software/ns/schema/2.1' || 'http://nodeinfo.diaspora.software/ns/schema/2.0')
console.error(choosen)
if (!choosen) {
throw new Error('Not found!')
}
const data = await axios.get(choosen.href).then(res => res.data)
console.error('INSTANCE', data)
return data
}
},
2019-10-30 15:01:15 +01:00
async getInstance (actor_url, force = false) {
log.debug(`[FEDI] getInstance ${actor_url}`)
2019-10-30 15:01:15 +01:00
actor_url = new url.URL(actor_url)
const domain = actor_url.host
const instance_url = `${actor_url.protocol}//${actor_url.host}`
2021-03-05 14:17:10 +01:00
log.debug(`getInstance ${domain}`)
2019-10-30 15:01:15 +01:00
let instance
if (!force) {
instance = await Instance.findByPk(domain)
2019-10-30 15:01:15 +01:00
if (instance) { return instance }
}
try {
instance = await Helpers.getNodeInfo(instance_url)
return Instance.create({ name: instance?.metadata?.nodeLabel || instance?.metadata?.nodeName || domain, domain, data: instance, blocked: false })
} catch(e) {
log.error('NodeInfo not supported', e)
return Instance.create({ name: domain, domain, blocked: false })
}
2019-10-30 15:01:15 +01:00
},
2019-08-02 17:29:55 +02:00
// ref: https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
2019-09-11 19:12:24 +02:00
async verifySignature (req, res, next) {
2021-04-26 11:25:35 +02:00
// TODO: why do I need instance?
2019-10-30 15:01:15 +01:00
const instance = await Helpers.getInstance(req.body.actor)
2021-03-05 14:17:10 +01:00
if (!instance) {
2021-04-26 11:25:35 +02:00
log.warn(`Verify Signature: Instance not found ${req.body.actor}`)
2021-03-05 14:17:10 +01:00
return res.status(401).send('Instance not found')
}
2019-10-30 15:01:15 +01:00
if (instance.blocked) {
2021-03-05 14:17:10 +01:00
log.warn(`Instance ${instance.domain} blocked`)
2019-10-30 15:01:15 +01:00
return res.status(401).send('Instance blocked')
}
let user = await Helpers.getActor(req.body.actor, instance)
2021-03-05 14:17:10 +01:00
if (!user) {
log.info(`Actor ${req.body.actor} not found`)
if (req.body.type === 'Delete') {
return res.sendStatus(201)
}
2021-03-05 14:17:10 +01:00
return res.status(401).send('Actor not found')
}
if (user.blocked) {
2021-03-05 14:17:10 +01:00
log.info(`User ${user.ap_id} blocked`)
return res.status(401).send('User blocked')
}
2019-10-30 15:01:15 +01:00
2022-02-26 21:27:40 +01:00
res.locals.fedi_user = user
2021-04-26 11:25:35 +02:00
// TODO: check Digest // cannot do this with json bodyparser
// const digest = crypto.createHash('sha256')
// .update(req.body)
// .digest('base64')
// if (`SHA-256=${digest}` !== req.headers.signature) {
// log.warn(`Signature mismatch ${req.headers.signature} - ${digest}`)
2021-04-26 11:25:35 +02:00
// return res.status(401).send('Signature mismatch')
// }
2019-09-11 19:12:24 +02:00
// another little hack :/
// https://github.com/joyent/node-http-signature/issues/87
req.url = '/federation' + req.url
2019-08-02 17:29:55 +02:00
const parsed = httpSignature.parseRequest(req)
if (httpSignature.verifySignature(parsed, user.object.publicKey.publicKeyPem)) { return next() }
2019-10-30 15:01:15 +01:00
2019-08-02 17:29:55 +02:00
// signature not valid, try without cache
2019-10-30 15:01:15 +01:00
user = await Helpers.getActor(req.body.actor, instance, true)
2021-03-05 14:17:10 +01:00
if (!user) {
2021-04-28 12:44:26 +02:00
log.info(`Actor ${req.body.actor} not found`)
2021-03-05 14:17:10 +01:00
return res.status(401).send('Actor not found')
}
2021-06-25 11:43:50 +02:00
if (httpSignature.verifySignature(parsed, user.object.publicKey.publicKeyPem)) {
log.debug(`Valid signature from ${req.body.actor} `)
return next()
}
2019-10-30 15:01:15 +01:00
2019-08-02 17:29:55 +02:00
// still not valid
2021-04-28 12:44:26 +02:00
log.info(`Invalid signature from user ${req.body.actor}`)
res.send('Request signature could not be verified', 401)
2019-07-30 18:32:26 +02:00
}
}
2019-07-30 18:57:45 +02:00
module.exports = Helpers