refactoring logs: use winston
This commit is contained in:
@@ -3,11 +3,11 @@ const axios = require('axios')
|
||||
const crypto = require('crypto')
|
||||
const config = require('config')
|
||||
const httpSignature = require('http-signature')
|
||||
const debug = require('debug')('federation:helpers')
|
||||
const APUser = require('../api/models/ap_user')
|
||||
const Instance = require('../api/models/instance')
|
||||
const url = require('url')
|
||||
const settingsController = require('../api/controller/settings')
|
||||
const log = require('../log')
|
||||
|
||||
const Helpers = {
|
||||
|
||||
@@ -25,7 +25,11 @@ const Helpers = {
|
||||
'/friendica/json',
|
||||
'/poco'
|
||||
]
|
||||
if (urlToIgnore.includes(req.path)) { return res.status(404).send('Not Found') }
|
||||
if (urlToIgnore.includes(req.path)) {
|
||||
log.debug(`Ignore noisy fediverse ${req.path}`)
|
||||
log.debug(req)
|
||||
return res.status(404).send('Not Found')
|
||||
}
|
||||
next()
|
||||
},
|
||||
|
||||
@@ -53,15 +57,15 @@ const Helpers = {
|
||||
method: 'post',
|
||||
data: JSON.stringify(message)
|
||||
})
|
||||
debug('sign %s => %s', ret.status, ret.data)
|
||||
log.debug(`sign ${ret.status} => ${ret.data}`)
|
||||
} catch (e) {
|
||||
debug('ERROR ', e.toString())
|
||||
log.error(e)
|
||||
}
|
||||
},
|
||||
|
||||
async sendEvent (event, type = 'Create') {
|
||||
if (!settingsController.settings.enable_federation) {
|
||||
debug('event not send, federation disabled')
|
||||
log.debug('event not send, federation disabled')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -74,7 +78,7 @@ const Helpers = {
|
||||
})
|
||||
|
||||
for (const sharedInbox in recipients) {
|
||||
debug('Notify %s with event %s cc => %d', sharedInbox, event.title, recipients[sharedInbox].length)
|
||||
log.debug(`Notify ${sharedInbox} with event ${event.title} cc => ${recipients[sharedInbox].length}`)
|
||||
const body = {
|
||||
id: `${config.baseurl}/federation/m/${event.id}#create`,
|
||||
type,
|
||||
@@ -112,18 +116,18 @@ const Helpers = {
|
||||
fedi_user = await axios.get(URL, { headers: { Accept: 'application/jrd+json, application/json' } })
|
||||
.then(res => {
|
||||
if (res.status !== 200) {
|
||||
debug('[ERR] Actor %s => %s', URL, res.statusText)
|
||||
log.warn(`Actor ${URL} => ${res.statusText}`)
|
||||
return false
|
||||
}
|
||||
return res.data
|
||||
})
|
||||
.catch(e => {
|
||||
debug(`[ERR] ${URL}: ${e}`)
|
||||
log.error(`${URL}: ${e}`)
|
||||
return false
|
||||
})
|
||||
|
||||
if (fedi_user) {
|
||||
debug(`Create a new AP User => ${URL}`)
|
||||
log.debug(`Create a new AP User => ${URL}`)
|
||||
fedi_user = await APUser.create({ ap_id: URL, object: fedi_user })
|
||||
}
|
||||
return fedi_user
|
||||
@@ -133,7 +137,7 @@ const Helpers = {
|
||||
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)
|
||||
log.debug(`getInstance ${domain}`)
|
||||
let instance
|
||||
if (!force) {
|
||||
instance = await Instance.findByPk(domain)
|
||||
@@ -151,7 +155,7 @@ const Helpers = {
|
||||
return Instance.create({ name: instance.title, domain, data, blocked: false })
|
||||
})
|
||||
.catch(e => {
|
||||
debug(e)
|
||||
log.error(e)
|
||||
return false
|
||||
})
|
||||
return instance
|
||||
@@ -160,16 +164,22 @@ const Helpers = {
|
||||
// ref: https://blog.joinmastodon.org/2018/07/how-to-make-friends-and-verify-requests/
|
||||
async verifySignature (req, res, next) {
|
||||
const instance = await Helpers.getInstance(req.body.actor)
|
||||
if (!instance) { return res.status(401).send('Instance not found') }
|
||||
if (!instance) {
|
||||
log.warn(`[AP] Verify Signature: Instance not found ${req.body.actor}`)
|
||||
return res.status(401).send('Instance not found')
|
||||
}
|
||||
if (instance.blocked) {
|
||||
debug('Instance %s blocked', instance.domain)
|
||||
log.warn(`Instance ${instance.domain} blocked`)
|
||||
return res.status(401).send('Instance blocked')
|
||||
}
|
||||
|
||||
let user = await Helpers.getActor(req.body.actor, instance)
|
||||
if (!user) { return res.status(401).send('Actor not found') }
|
||||
if (!user) {
|
||||
log.info(`Actor ${req.body.actor} not found`)
|
||||
return res.status(401).send('Actor not found')
|
||||
}
|
||||
if (user.blocked) {
|
||||
debug('User %s blocked', user.ap_id)
|
||||
log.info(`User ${user.ap_id} blocked`)
|
||||
return res.status(401).send('User blocked')
|
||||
}
|
||||
|
||||
@@ -186,11 +196,14 @@ const Helpers = {
|
||||
|
||||
// signature not valid, try without cache
|
||||
user = await Helpers.getActor(req.body.actor, instance, true)
|
||||
if (!user) { return res.status(401).send('Actor not found') }
|
||||
if (!user) {
|
||||
log.debug(`Actor ${req.body.actor} not found`)
|
||||
return res.status(401).send('Actor not found')
|
||||
}
|
||||
if (httpSignature.verifySignature(parsed, user.object.publicKey.publicKeyPem)) { return next() }
|
||||
|
||||
// still not valid
|
||||
debug('Invalid signature from user %s', req.body.actor)
|
||||
log.debug(`Invalid signature from user ${req.body.actor}`)
|
||||
res.send('Request signature could not be verified', 401)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user