2020-01-27 00:47:03 +01:00
|
|
|
const fetch = require('axios')
|
2019-08-10 15:00:08 +02:00
|
|
|
// const request = require('request')
|
2019-07-31 02:01:21 +02:00
|
|
|
const crypto = require('crypto')
|
2019-07-30 18:57:45 +02:00
|
|
|
const config = require('config')
|
2019-08-02 17:29:55 +02:00
|
|
|
const httpSignature = require('http-signature')
|
2019-09-11 21:20:44 +02:00
|
|
|
const debug = require('debug')('federation:helpers')
|
2019-12-06 00:49:44 +01:00
|
|
|
const { ap_user: APUser, instance: Instance } = require('../api/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')
|
2019-07-30 18:32:26 +02:00
|
|
|
|
|
|
|
const Helpers = {
|
2019-09-13 11:08:18 +02:00
|
|
|
|
|
|
|
// ignore unimplemented ping url from fediverse
|
2019-10-28 17:33:20 +01:00
|
|
|
spamFilter (req, res, next) {
|
2019-09-13 11:08:18 +02:00
|
|
|
const urlToIgnore = [
|
|
|
|
'/api/v1/instance',
|
|
|
|
'/api/meta',
|
2019-09-18 12:55:33 +02:00
|
|
|
'/api/statusnet/version.json',
|
|
|
|
'/api/gnusocial/version.json',
|
2019-09-13 11:08:18 +02:00
|
|
|
'/api/statusnet/config.json',
|
2019-10-28 17:33:20 +01:00
|
|
|
'/poco'
|
2019-09-13 11:08:18 +02:00
|
|
|
]
|
2019-10-28 17:33:20 +01:00
|
|
|
if (urlToIgnore.includes(req.path)) { return res.status(404).send('Not Found') }
|
2019-09-13 11:08:18 +02:00
|
|
|
next()
|
|
|
|
},
|
|
|
|
|
2019-12-04 00:50:15 +01:00
|
|
|
async signAndSend (message, inbox) {
|
2019-07-30 18:32:26 +02:00
|
|
|
// get the URI of the actor object and append 'inbox' to it
|
2019-10-30 15:01:15 +01:00
|
|
|
const inboxUrl = new url.URL(inbox)
|
2019-12-04 00:50:15 +01:00
|
|
|
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()
|
2019-11-09 15:06:25 +01:00
|
|
|
const stringToSign = `(request-target): post ${inboxUrl.pathname}\nhost: ${inboxUrl.hostname}\ndate: ${d.toUTCString()}`
|
2019-07-30 18:32:26 +02:00
|
|
|
signer.update(stringToSign)
|
|
|
|
signer.end()
|
|
|
|
const signature = signer.sign(privkey)
|
|
|
|
const signature_b64 = signature.toString('base64')
|
2019-12-04 00:50:15 +01:00
|
|
|
const header = `keyId="${config.baseurl}/federation/u/${settingsController.settings.instance_name}",headers="(request-target) host date",signature="${signature_b64}"`
|
2020-01-27 00:47:03 +01:00
|
|
|
try {
|
|
|
|
const ret = await fetch(inbox, {
|
|
|
|
headers: {
|
|
|
|
Host: inboxUrl.hostname,
|
|
|
|
Date: d.toUTCString(),
|
|
|
|
Signature: header,
|
|
|
|
'Content-Type': 'application/activity+json; charset=utf-8',
|
|
|
|
Accept: 'application/activity+json, application/json; chartset=utf-8'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify(message)
|
|
|
|
})
|
|
|
|
debug('sign %s => %s', ret.status, await ret.text())
|
|
|
|
} catch (e) {
|
|
|
|
debug('ERROR ', e.toString())
|
|
|
|
}
|
2019-07-31 01:43:08 +02:00
|
|
|
},
|
2019-09-11 12:00:13 +02:00
|
|
|
|
2019-12-04 00:50:15 +01:00
|
|
|
async sendEvent (event, type = 'Create') {
|
2019-09-25 14:38:16 +02:00
|
|
|
if (!settingsController.settings.enable_federation) {
|
2019-09-18 12:55:33 +02:00
|
|
|
debug('event not send, federation disabled')
|
2019-09-13 10:17:44 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-06 00:49:44 +01:00
|
|
|
const followers = await APUser.findAll({ where: { follower: true } })
|
2019-10-30 15:01:15 +01:00
|
|
|
const recipients = {}
|
2019-12-04 00:50:15 +01:00
|
|
|
followers.forEach(follower => {
|
2019-09-18 12:55:33 +02:00
|
|
|
const sharedInbox = follower.object.endpoints.sharedInbox
|
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-09-13 10:17:44 +02:00
|
|
|
})
|
|
|
|
|
2019-10-28 17:33:20 +01:00
|
|
|
for (const sharedInbox in recipients) {
|
2019-12-10 22:58:10 +01:00
|
|
|
debug('Notify %s with event %s cc => %d', sharedInbox, event.title, 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`,
|
2019-10-02 21:04:03 +02:00
|
|
|
type,
|
2019-09-11 21:20:44 +02:00
|
|
|
to: ['https://www.w3.org/ns/activitystreams#Public'],
|
2019-12-04 00:50:15 +01:00
|
|
|
cc: [`${config.baseurl}/federation/u/${settingsController.settings.instance_name}/followers`, ...recipients[sharedInbox]],
|
2019-10-28 17:33:20 +01:00
|
|
|
// cc: recipients[sharedInbox],
|
2019-12-04 00:50:15 +01:00
|
|
|
actor: `${config.baseurl}/federation/u/${settingsController.settings.instance_name}`,
|
|
|
|
// object: event.toNoteAP(instanceAdmin.username, [`${config.baseurl}/federation/u/${instanceAdmin.username}/followers`, ...recipients[sharedInbox]])
|
|
|
|
object: event.toNoteAP(settingsController.settings.instance_name, 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',
|
2019-12-10 22:58:10 +01:00
|
|
|
{ Hashtag: 'as:Hashtag' }]
|
2019-12-04 00:50:15 +01:00
|
|
|
Helpers.signAndSend(body, sharedInbox)
|
2019-09-11 12:00:13 +02:00
|
|
|
}
|
2019-08-02 13:43:28 +02:00
|
|
|
},
|
|
|
|
|
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) {
|
2019-12-04 00:50:15 +01:00
|
|
|
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)
|
|
|
|
}
|
2019-11-13 10:56:01 +01:00
|
|
|
return fedi_user
|
2019-10-30 15:01:15 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-12 14:59:51 +02:00
|
|
|
|
2019-12-06 11:30:41 +01:00
|
|
|
fedi_user = await fetch(URL, { headers: { Accept: 'application/jrd+json, application/json' } })
|
2019-08-09 00:19:57 +02:00
|
|
|
.then(res => {
|
|
|
|
if (!res.ok) {
|
2019-10-30 15:01:15 +01:00
|
|
|
debug('[ERR] Actor %s => %s', URL, res.statusText)
|
2019-08-09 00:19:57 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return res.json()
|
2019-08-08 17:48:12 +02:00
|
|
|
})
|
2019-10-30 15:01:15 +01:00
|
|
|
|
2019-09-12 14:59:51 +02:00
|
|
|
if (fedi_user) {
|
2019-12-04 00:50:15 +01:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2019-10-30 15:01:15 +01:00
|
|
|
async getInstance (actor_url, force = false) {
|
|
|
|
actor_url = new url.URL(actor_url)
|
|
|
|
const domain = actor_url.host
|
|
|
|
const instance_url = `${actor_url.protocol}//${actor_url.host}`
|
|
|
|
debug('getInstance %s', domain)
|
|
|
|
let instance
|
|
|
|
if (!force) {
|
2019-12-04 00:50:15 +01:00
|
|
|
instance = await Instance.findByPk(domain)
|
2019-10-30 15:01:15 +01:00
|
|
|
if (instance) { return instance }
|
|
|
|
}
|
|
|
|
|
2019-12-06 11:30:41 +01:00
|
|
|
instance = await fetch(`${instance_url}/api/v1/instance`, { headers: { Accept: 'application/json' } })
|
2019-10-30 15:01:15 +01:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(instance => {
|
|
|
|
const data = {
|
|
|
|
stats: instance.stats,
|
|
|
|
thumbnail: instance.thumbnail
|
|
|
|
}
|
2019-12-04 00:50:15 +01:00
|
|
|
return Instance.create({ name: instance.title, domain, data, blocked: false })
|
2019-10-30 15:01:15 +01:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
debug(e)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return instance
|
|
|
|
},
|
|
|
|
|
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) {
|
2019-10-30 15:01:15 +01:00
|
|
|
const instance = await Helpers.getInstance(req.body.actor)
|
|
|
|
if (!instance) { return res.status(401).send('Instance not found') }
|
|
|
|
if (instance.blocked) {
|
|
|
|
debug('Instance %s blocked', instance.domain)
|
|
|
|
return res.status(401).send('Instance blocked')
|
|
|
|
}
|
|
|
|
|
|
|
|
let user = await Helpers.getActor(req.body.actor, instance)
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.status(401).send('Actor not found') }
|
2019-11-13 10:56:01 +01:00
|
|
|
if (user.blocked) {
|
|
|
|
debug('User %s blocked', user.ap_id)
|
|
|
|
return res.status(401).send('User blocked')
|
|
|
|
}
|
2019-10-30 15:01:15 +01:00
|
|
|
|
2019-08-02 17:29:55 +02:00
|
|
|
// little hack -> https://github.com/joyent/node-http-signature/pull/83
|
|
|
|
req.headers.authorization = 'Signature ' + req.headers.signature
|
2019-10-30 15:01:15 +01:00
|
|
|
|
2019-09-12 14:59:51 +02:00
|
|
|
req.fedi_user = user
|
2019-08-08 17:48:12 +02:00
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
// another little hack :/
|
2019-08-08 17:48:12 +02:00
|
|
|
// 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)
|
2019-11-13 10:56:01 +01:00
|
|
|
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)
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.status(401).send('Actor not found') }
|
2019-11-13 10:56:01 +01:00
|
|
|
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
|
|
|
// still not valid
|
2019-09-11 12:00:13 +02:00
|
|
|
debug('Invalid signature from user %s', req.body.actor)
|
2019-08-08 17:48:12 +02:00
|
|
|
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
|