refactoring logs: use winston
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
const Event = require('../api/models/event')
|
||||
const config = require('config')
|
||||
const debug = require('debug')('fediverse:ego')
|
||||
const log = require('../log')
|
||||
|
||||
module.exports = {
|
||||
async boost (req, res) {
|
||||
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
|
||||
if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
|
||||
debug('boost %s', match[1])
|
||||
log.debug(`boost ${match[1]}`)
|
||||
const event = await Event.findByPk(Number(match[1]))
|
||||
if (!event) { return res.status(404).send('Event not found!') }
|
||||
await event.update({ boost: [...event.boost, req.body.actor] })
|
||||
@@ -16,7 +16,7 @@ module.exports = {
|
||||
async unboost (req, res) {
|
||||
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
|
||||
if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
|
||||
debug('unboost %s', match[1])
|
||||
log.debug(`unboost ${match[1]}`)
|
||||
const event = await Event.findByPk(Number(match[1]))
|
||||
if (!event) { return res.status(404).send('Event not found!') }
|
||||
await event.update({ boost: event.boost.filter(actor => actor !== req.body.actor) })
|
||||
@@ -26,7 +26,7 @@ module.exports = {
|
||||
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
|
||||
if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
|
||||
const event = await Event.findByPk(Number(match[1]))
|
||||
debug('%s bookmark %s (%d)', req.body.actor, event.title, event.likes.length)
|
||||
log.debug(`${req.body.actor} bookmark ${event.title} (${event.likes.length})`)
|
||||
if (!event) { return res.status(404).send('Event not found!') }
|
||||
await event.update({ likes: [...event.likes, req.body.actor] })
|
||||
res.sendStatus(201)
|
||||
@@ -38,7 +38,7 @@ module.exports = {
|
||||
const match = object.object.match(`${config.baseurl}/federation/m/(.*)`)
|
||||
if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
|
||||
const event = await Event.findByPk(Number(match[1]))
|
||||
debug('%s unbookmark %s (%d)', body.actor, event.title, event.likes.length)
|
||||
log.debug(`${body.actor} unbookmark ${event.title} (${event.likes.length})`)
|
||||
if (!event) { return res.status(404).send('Event not found!') }
|
||||
await event.update({ likes: event.likes.filter(actor => actor !== body.actor) })
|
||||
res.sendStatus(201)
|
||||
|
||||
@@ -1,29 +1,33 @@
|
||||
const config = require('config')
|
||||
const Helpers = require('./helpers')
|
||||
const crypto = require('crypto')
|
||||
const debug = require('debug')('federation:follows')
|
||||
const log = require('../log')
|
||||
|
||||
module.exports = {
|
||||
// follow request from fediverse
|
||||
async follow (req, res) {
|
||||
const body = req.body
|
||||
log.debug('follow')
|
||||
if (typeof body.object !== 'string') { return }
|
||||
const username = body.object.replace(`${config.baseurl}/federation/u/`, '')
|
||||
if (username !== req.settings.instance_name) { return res.status(404).send('User not found') }
|
||||
if (username !== req.settings.instance_name) {
|
||||
log.warn(`Following the wrong user: ${username} instead of ${req.settings.instance_name}`)
|
||||
return res.status(404).send('User not found')
|
||||
}
|
||||
|
||||
// check for duplicate
|
||||
// if (!user.followers.includes(body.actor)) {
|
||||
// await user.addFollowers([req.fedi_user.id])
|
||||
// await user.update({ followers: [...user.followers, body.actor] })
|
||||
await req.fedi_user.update({ follower: true })
|
||||
debug('Followed by %s', body.actor)
|
||||
log.debug(`Followed by ${body.actor}`)
|
||||
const guid = crypto.randomBytes(16).toString('hex')
|
||||
const message = {
|
||||
'@context': 'https://www.w3.org/ns/activitystreams',
|
||||
'id': `${config.baseurl}/federation/${guid}`,
|
||||
'type': 'Accept',
|
||||
'actor': `${config.baseurl}/federation/u/${username}`,
|
||||
'object': body
|
||||
id: `${config.baseurl}/federation/${guid}`,
|
||||
type: 'Accept',
|
||||
actor: `${config.baseurl}/federation/u/${username}`,
|
||||
object: body
|
||||
}
|
||||
Helpers.signAndSend(message, req.fedi_user.object.inbox)
|
||||
res.sendStatus(200)
|
||||
@@ -33,14 +37,17 @@ module.exports = {
|
||||
async unfollow (req, res) {
|
||||
const body = req.body
|
||||
const username = body.object.object.replace(`${config.baseurl}/federation/u/`, '')
|
||||
if (username !== req.settings.instance_name) { return res.status(404).send('User not found') }
|
||||
if (username !== req.settings.instance_name) {
|
||||
log.warn(`Unfollowing wrong user: ${username} instead of ${req.settings.instance_name}`)
|
||||
return res.status(404).send('User not found')
|
||||
}
|
||||
|
||||
if (body.actor !== body.object.actor || body.actor !== req.fedi_user.ap_id) {
|
||||
debug('Unfollow an user created by a different actor !?!?')
|
||||
log.debug('Unfollow an user created by a different actor !?!?')
|
||||
return res.status(400).send('Bad things')
|
||||
}
|
||||
await req.fedi_user.update({ follower: false })
|
||||
debug('Unfollowed by %s', body.actor)
|
||||
log.debug(`Unfollowed by ${body.actor}`)
|
||||
res.sendStatus(200)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ const Event = require('../api/models/event')
|
||||
const Resource = require('../api/models/resource')
|
||||
const APUser = require('../api/models/ap_user')
|
||||
|
||||
const debug = require('debug')('fediverse:resource')
|
||||
const log = require('../log')
|
||||
const helpers = require('../helpers')
|
||||
const linkifyHtml = require('linkifyjs/html')
|
||||
|
||||
@@ -11,7 +11,7 @@ module.exports = {
|
||||
// create a resource from AP Note
|
||||
async create (req, res) {
|
||||
if (!req.settings.enable_resources) {
|
||||
debug('Ignore resource as it is disabled in settings')
|
||||
log.info('Ignore resource as it is disabled in settings')
|
||||
return
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ module.exports = {
|
||||
if (inReplyTo) {
|
||||
// .. to an event ?
|
||||
const match = inReplyTo && inReplyTo.match('.*/federation/m/(.*)')
|
||||
debug('Event reply => ', inReplyTo)
|
||||
log.info(`Event reply => ${inReplyTo}`)
|
||||
if (match) {
|
||||
event = await Event.findByPk(Number(match[1]))
|
||||
} else {
|
||||
@@ -36,10 +36,14 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
debug('resource from %s to "%s"', req.body.actor, event && event.title)
|
||||
if (!event) {
|
||||
log.error('This is a direct message. Just ignore it')
|
||||
log.error(body)
|
||||
return res.status(404).send('Not found')
|
||||
}
|
||||
|
||||
log.debug(`resource from ${req.body.actor} to "${event.title}"`)
|
||||
|
||||
// TODO should probably map links here
|
||||
// clean resource
|
||||
body.object.content = helpers.sanitizeHTML(linkifyHtml(body.object.content))
|
||||
|
||||
await Resource.create({
|
||||
@@ -58,15 +62,15 @@ module.exports = {
|
||||
include: [{ model: APUser, required: false, attributes: ['ap_id'] }]
|
||||
})
|
||||
if (!resource) {
|
||||
debug('Comment %s not found', req.body.object.id)
|
||||
log.info(`Comment ${req.body.object.id} not found`)
|
||||
return res.status(404).send('Not found')
|
||||
}
|
||||
// check if fedi_user that requested resource removal
|
||||
// is the same that created the resource at first place
|
||||
debug(res.fedi_user.ap_id, resource.ap_user.ap_id)
|
||||
log.debug(res.fedi_user.ap_id, resource.ap_user.ap_id)
|
||||
if (res.fedi_user.ap_id === resource.ap_user.id) {
|
||||
await resource.destroy()
|
||||
debug('Comment %s removed!', req.body.object.id)
|
||||
log.info(`Comment ${req.body.object.id} removed`)
|
||||
res.sendStatus(201)
|
||||
} else {
|
||||
res.sendStatus(403)
|
||||
|
||||
@@ -8,19 +8,21 @@ const cors = require('cors')
|
||||
const settingsController = require('../api/controller/settings')
|
||||
const version = require('../../package.json').version
|
||||
const url = require('url')
|
||||
const debug = require('debug')('webfinger')
|
||||
const log = require('../log')
|
||||
|
||||
router.use(cors())
|
||||
router.use((req, res, next) => {
|
||||
// is federation enabled ?
|
||||
if (req.settings.enable_federation) { return next() }
|
||||
debug('Federation disabled')
|
||||
if (req.settings.enable_federation) {
|
||||
return next()
|
||||
}
|
||||
log.debug('Federation disabled')
|
||||
res.status(404).send('Federation disabled')
|
||||
})
|
||||
|
||||
router.get('/webfinger', (req, res) => {
|
||||
if (!req.query || !req.query.resource || !req.query.resource.includes('acct:')) {
|
||||
debug('Bad webfinger request => %s', req.query && req.query.resource)
|
||||
log.debug('Bad webfinger request => ', req.query && req.query.resource)
|
||||
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
|
||||
}
|
||||
|
||||
@@ -28,14 +30,15 @@ router.get('/webfinger', (req, res) => {
|
||||
const domain = (new url.URL(req.settings.baseurl)).host
|
||||
const [, name, req_domain] = resource.match(/acct:(.*)@(.*)/)
|
||||
if (domain !== req_domain) {
|
||||
debug('Bad webfinger request, requested domain "%s" instead of "%s"', req_domain, domain)
|
||||
log.warn(`Bad webfinger request, requested domain "${req_domain}" instead of "${domain}"`)
|
||||
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
|
||||
}
|
||||
if (name !== req.settings.instance_name) {
|
||||
debug('User not found: %s', name)
|
||||
log.warn(`User not found: ${name}`)
|
||||
return res.status(404).send(`No record found for ${name}`)
|
||||
}
|
||||
|
||||
log.info(`webfinger ${resource} ${domain}`)
|
||||
const ret = {
|
||||
subject: `acct:${name}@${domain}`,
|
||||
links: [
|
||||
@@ -121,6 +124,7 @@ router.get('/nodeinfo', (req, res) => {
|
||||
})
|
||||
|
||||
router.use('/host-meta', (req, res) => {
|
||||
log.debug('host-meta')
|
||||
res.type('application/xml')
|
||||
res.send(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
||||
@@ -130,13 +134,13 @@ router.use('/host-meta', (req, res) => {
|
||||
|
||||
// Handle 404
|
||||
router.use((req, res) => {
|
||||
debug('404 Page not found: %s', req.path)
|
||||
log.error('404 Page not found: ', req.path)
|
||||
res.status(404).send('404: Page not Found')
|
||||
})
|
||||
|
||||
// Handle 500
|
||||
router.use((error, req, res, next) => {
|
||||
debug(error)
|
||||
log.error(error)
|
||||
res.status(500).send('500: Internal Server Error')
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user