refactoring logs: use winston

This commit is contained in:
les 2021-03-05 14:17:10 +01:00
parent 589a8ce939
commit 65cb94dd6a
22 changed files with 1977 additions and 1229 deletions

View file

@ -29,54 +29,55 @@
], ],
"dependencies": { "dependencies": {
"@nuxtjs/auth": "^4.9.1", "@nuxtjs/auth": "^4.9.1",
"@nuxtjs/axios": "^5.12.5", "@nuxtjs/axios": "^5.13.1",
"accept-language": "^3.0.18", "accept-language": "^3.0.18",
"axios": "^0.21.1", "axios": "^0.21.1",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"config": "^3.3.3", "config": "^3.3.4",
"consola": "^2.15.0",
"cookie-parser": "^1.4.5", "cookie-parser": "^1.4.5",
"cors": "^2.8.5", "cors": "^2.8.5",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"date-fns": "^2.16.1", "date-fns": "^2.19.0",
"dayjs": "^1.10.4", "dayjs": "^1.10.4",
"dompurify": "^2.2.6", "dompurify": "^2.2.6",
"email-templates": "^8.0.3", "email-templates": "^8.0.3",
"express": "^4.17.1", "express": "^4.17.1",
"express-oauth-server": "^2.0.0", "express-oauth-server": "^2.0.0",
"express-prom-bundle": "^6.3.1", "express-prom-bundle": "^6.3.4",
"fs": "^0.0.1-security", "fs": "^0.0.1-security",
"global": "^4.4.0",
"http-signature": "^1.3.5", "http-signature": "^1.3.5",
"ical.js": "^1.4.0", "ical.js": "^1.4.0",
"ics": "^2.26.2", "ics": "^2.27.0",
"inquirer": "^7.3.3", "inquirer": "^8.0.0",
"jsdom": "^16.4.0", "jsdom": "^16.4.0",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"less": "^4.1.1", "less": "^4.1.1",
"linkifyjs": "^2.1.9", "linkifyjs": "^2.1.9",
"lodash": "^4.17.20", "lodash": "^4.17.21",
"microformat-node": "^2.0.1", "microformat-node": "^2.0.1",
"mkdirp": "^1.0.4", "mkdirp": "^1.0.4",
"moment-timezone": "^0.5.32", "moment-timezone": "^0.5.33",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"multer": "^1.4.2", "multer": "^1.4.2",
"nuxt": "^2.14.12", "nuxt": "^2.15.2",
"nuxt-express-module": "^0.0.11", "nuxt-express-module": "^0.0.11",
"nuxt-winston-log": "^1.2.0",
"pg": "^8.5.1", "pg": "^8.5.1",
"pkg": "^4.4.9",
"prom-client": "^13.1.0", "prom-client": "^13.1.0",
"sequelize": "^6.5.0", "sequelize": "^6.5.0",
"sequelize-cli": "^6.2.0", "sequelize-cli": "^6.2.0",
"sharp": "^0.27.1", "sharp": "^0.27.2",
"sqlite3": "^5.0.1", "sqlite3": "^5.0.2",
"tiptap": "^1.32.0", "tiptap": "^1.32.0",
"tiptap-extensions": "^1.35.0", "tiptap-extensions": "^1.35.0",
"to-ico": "^1.1.5", "to-ico": "^1.1.5",
"url": "^0.11.0", "url": "^0.11.0",
"v-calendar": "^2.2.2", "v-calendar": "^2.2.4",
"vue-clipboard2": "^0.3.1", "vue-clipboard2": "^0.3.1",
"vue-i18n": "^8.22.4", "vue-i18n": "^8.23.0",
"winston": "^3.3.3",
"yargs": "^16.1.1" "yargs": "^16.1.1"
}, },
"devDependencies": { "devDependencies": {
@ -84,29 +85,29 @@
"@nuxtjs/eslint-config": "^5.0.0", "@nuxtjs/eslint-config": "^5.0.0",
"@nuxtjs/vuetify": "^1.11.3", "@nuxtjs/vuetify": "^1.11.3",
"babel-eslint": "^10.1.0", "babel-eslint": "^10.1.0",
"eslint": "^7.19.0", "eslint": "^7.21.0",
"eslint-config-prettier": "^7.2.0", "eslint-config-prettier": "^8.1.0",
"eslint-config-standard": "^16.0.2", "eslint-config-standard": "^16.0.2",
"eslint-loader": "^4.0.2", "eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.22.1", "eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": ">=11.1.0", "eslint-plugin-node": ">=11.1.0",
"eslint-plugin-nuxt": "^2.0.0", "eslint-plugin-nuxt": "^2.0.0",
"eslint-plugin-prettier": "^3.3.1", "eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-promise": ">=4.0.1", "eslint-plugin-promise": "^4.3.1",
"eslint-plugin-standard": "^5.0.0", "eslint-plugin-standard": "^5.0.0",
"eslint-plugin-vue": "^7.5.0", "eslint-plugin-vue": "^7.6.0",
"less-loader": "^7.3.0", "less-loader": "7",
"nodemon": "^2.0.7", "nodemon": "^2.0.7",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"pug-plain-loader": "^1.1.0", "pug-plain-loader": "^1.1.0",
"sass": "^1.32.5", "sass": "^1.32.7",
"sass-loader": "^10.1.1", "sass-loader": "10",
"vue-cli-plugin-vuetify": "~2.1.0", "vue-cli-plugin-vuetify": "~2.1.1",
"vuetify-loader": "^1.7.1", "vuetify-loader": "^1.7.1",
"webpack-cli": "^4.4.0" "webpack-cli": "^4.5.0"
}, },
"resolutions": { "resolutions": {
"prosemirror-model": "1.13.1" "prosemirror-model": "1.13.3"
}, },
"bin": { "bin": {
"gancio": "server/cli.js" "gancio": "server/cli.js"

View file

@ -1,4 +1,4 @@
const debug = require('debug')('auth') const log = require('../log')
const oauth = require('./oauth') const oauth = require('./oauth')
const get = require('lodash/get') const get = require('lodash/get')
@ -40,14 +40,13 @@ const Auth = {
// TODO // TODO
hasPerm (scope) { hasPerm (scope) {
return (req, res, next) => { return (req, res, next) => {
debug(scope, req.path) log.debug(scope, req.path)
oauth.oauthServer.authenticate({ scope })(req, res, () => { oauth.oauthServer.authenticate({ scope })(req, res, () => {
debug('has perm') log.debug('has perm')
next() next()
}) })
} }
} }
} }
module.exports = Auth module.exports = Auth

View file

@ -1,5 +1,5 @@
const Announcement = require('../models/announcement') const Announcement = require('../models/announcement')
const debug = require('debug')('announcement:controller') const log = require('../../log')
const announceController = { const announceController = {
async getAll (req, res) { async getAll (req, res) {
@ -17,7 +17,7 @@ const announceController = {
announcement: req.body.announcement, announcement: req.body.announcement,
visible: true visible: true
} }
debug('Create announcement ', req.body.title) log.debug('Create announcement ', req.body.title)
const announce = await Announcement.create(announcementDetail) const announce = await Announcement.create(announcementDetail)
res.json(announce) res.json(announce)
}, },
@ -34,20 +34,20 @@ const announceController = {
announce = await announce.update(announceDetails) announce = await announce.update(announceDetails)
res.json(announce) res.json(announce)
} catch (e) { } catch (e) {
debug('Toggle announcement failed ', e) log.debug('Toggle announcement failed ', e)
res.sendStatus(404) res.sendStatus(404)
} }
}, },
async remove (req, res) { async remove (req, res) {
debug('Remove announcement ', req.params.announce_id) log.debug('Remove announcement ', req.params.announce_id)
const announce_id = req.params.announce_id const announce_id = req.params.announce_id
try { try {
const announce = await Announcement.findByPk(announce_id) const announce = await Announcement.findByPk(announce_id)
await announce.destroy() await announce.destroy()
res.sendStatus(200) res.sendStatus(200)
} catch (e) { } catch (e) {
debug('Remove announcement failed ', e) log.debug('Remove announcement failed ', e)
res.sendStatus(404) res.sendStatus(404)
} }
} }

View file

@ -52,7 +52,7 @@ const eventController = {
}, },
async getNotifications (event, action) { async getNotifications (event, action) {
log.info('getNotifications "%s" (%s)', event.title, action) log.debug(`getNotifications ${event.title} ${action}`)
function match (event, filters) { function match (event, filters) {
// matches if no filter specified // matches if no filter specified
if (!filters) { return true } if (!filters) { return true }
@ -165,11 +165,16 @@ const eventController = {
async confirm (req, res) { async confirm (req, res) {
const id = Number(req.params.event_id) const id = Number(req.params.event_id)
const event = await Event.findByPk(id, { include: [Place, Tag] }) const event = await Event.findByPk(id, { include: [Place, Tag] })
if (!event) { return res.sendStatus(404) } if (!event) {
log.warn(`Trying to confirm a unknown event, id: ${id}`)
return res.sendStatus(404)
}
if (!req.user.is_admin && req.user.id !== event.userId) { if (!req.user.is_admin && req.user.id !== event.userId) {
log.warn(`Someone unallowed is trying to confirm -> "${event.title} `)
return res.sendStatus(403) return res.sendStatus(403)
} }
log.info(`Event "${event.title}" confirmed`)
try { try {
event.is_visible = true event.is_visible = true
@ -189,7 +194,7 @@ const eventController = {
const notifier = require('../../notifier') const notifier = require('../../notifier')
notifier.notifyEvent('Create', event.id) notifier.notifyEvent('Create', event.id)
} catch (e) { } catch (e) {
log.info(e) log.error(e)
res.sendStatus(404) res.sendStatus(404)
} }
}, },
@ -326,8 +331,7 @@ const eventController = {
notifier.notifyEvent('Create', event.id) notifier.notifyEvent('Create', event.id)
} }
} catch (e) { } catch (e) {
process.winstonLog.error(e) log.error(e)
log.info(e)
res.sendStatus(400) res.sendStatus(400)
} }
}, },

View file

@ -7,8 +7,8 @@ const OAuthToken = require('../models/oauth_token')
const OAuthCode = require('../models/oauth_code') const OAuthCode = require('../models/oauth_code')
const User = require('../models/user') const User = require('../models/user')
const debug = require('debug')('oauth') const log = require('../../log')
const moment = require('dayjs') const dayjs = require('dayjs')
async function randomString (len = 16) { async function randomString (len = 16) {
const bytes = await randomBytes(len * 8) const bytes = await randomBytes(len * 8)
@ -42,7 +42,7 @@ const oauthController = {
delete client.id delete client.id
res.json(client) res.json(client)
} catch (e) { } catch (e) {
debug(e) log.error(e)
res.status(400).json(e) res.status(400).json(e)
} }
}, },
@ -136,14 +136,14 @@ const oauthController = {
async saveAuthorizationCode (code, client, user) { async saveAuthorizationCode (code, client, user) {
code.userId = user.id code.userId = user.id
code.clientId = client.id code.clientId = client.id
code.expiresAt = moment(code.expiresAt).toDate() code.expiresAt = dayjs(code.expiresAt).toDate()
const ret = await OAuthCode.create(code) const ret = await OAuthCode.create(code)
return ret return ret
}, },
// TODO // TODO
verifyScope (token, scope) { verifyScope (token, scope) {
debug('VERIFY SCOPE ', scope) log.debug('VERIFY SCOPE ', scope)
if (token.user.is_admin) { if (token.user.is_admin) {
return true return true
} else { } else {

View file

@ -4,7 +4,6 @@ const consola = require('consola')
const path = require('path') const path = require('path')
const fs = require('fs') const fs = require('fs')
const pkg = require('../../../package.json') const pkg = require('../../../package.json')
const debug = require('debug')('settings')
const crypto = require('crypto') const crypto = require('crypto')
const util = require('util') const util = require('util')
const toIco = require('to-ico') const toIco = require('to-ico')
@ -12,6 +11,7 @@ const generateKeyPair = util.promisify(crypto.generateKeyPair)
const readFile = util.promisify(fs.readFile) const readFile = util.promisify(fs.readFile)
const writeFile = util.promisify(fs.writeFile) const writeFile = util.promisify(fs.writeFile)
const sharp = require('sharp') const sharp = require('sharp')
const log = require('../../log')
const defaultSettings = { const defaultSettings = {
instance_timezone: 'Europe/Rome', instance_timezone: 'Europe/Rome',
@ -61,7 +61,7 @@ const settingsController = {
// add pub/priv instance key if needed // add pub/priv instance key if needed
if (!settingsController.settings.publicKey) { if (!settingsController.settings.publicKey) {
debug('Instance priv/pub key not found') log.debug('Instance priv/pub key not found')
const { publicKey, privateKey } = await generateKeyPair('rsa', { const { publicKey, privateKey } = await generateKeyPair('rsa', {
modulusLength: 4096, modulusLength: 4096,
publicKeyEncoding: { publicKeyEncoding: {
@ -92,6 +92,7 @@ const settingsController = {
}, },
async set (key, value, is_secret = false) { async set (key, value, is_secret = false) {
log.debug(`SET ${key} ${value}`)
try { try {
const [setting, created] = await Setting.findOrCreate({ const [setting, created] = await Setting.findOrCreate({
where: { key }, where: { key },
@ -101,7 +102,7 @@ const settingsController = {
settingsController[is_secret ? 'secretSettings' : 'settings'][key] = value settingsController[is_secret ? 'secretSettings' : 'settings'][key] = value
return true return true
} catch (e) { } catch (e) {
debug(e) log.error(e)
return false return false
} }
}, },
@ -125,6 +126,9 @@ const settingsController = {
.resize(400) .resize(400)
.png({ quality: 90 }) .png({ quality: 90 })
.toFile(baseImgPath + '.png', async (err, info) => { .toFile(baseImgPath + '.png', async (err, info) => {
if (err) {
log.error(err)
}
const image = await readFile(baseImgPath + '.png') const image = await readFile(baseImgPath + '.png')
const favicon = await toIco([image], { sizes: [64], resize: true }) const favicon = await toIco([image], { sizes: [64], resize: true })
writeFile(baseImgPath + '.ico', favicon) writeFile(baseImgPath + '.ico', favicon)

View file

@ -4,7 +4,7 @@ const config = require('config')
const mail = require('../mail') const mail = require('../mail')
const User = require('../models/user') const User = require('../models/user')
const settingsController = require('./settings') const settingsController = require('./settings')
const debug = require('debug')('user:controller') const log = require('../../log')
const linkify = require('linkifyjs') const linkify = require('linkifyjs')
const userController = { const userController = {
@ -96,9 +96,9 @@ const userController = {
return res.status(404).json('Invalid email') return res.status(404).json('Invalid email')
} }
debug('Register user ', req.body.email) log.debug('Register user ', req.body.email)
const user = await User.create(req.body) const user = await User.create(req.body)
debug(`Sending registration email to ${user.email}`) log.debug(`Sending registration email to ${user.email}`)
mail.send(user.email, 'register', { user, config }, req.settings.locale) mail.send(user.email, 'register', { user, config }, req.settings.locale)
mail.send(config.admin_email, 'admin_register', { user, config }) mail.send(config.admin_email, 'admin_register', { user, config })
res.sendStatus(200) res.sendStatus(200)

View file

@ -17,7 +17,7 @@ const storage = require('./storage')
const upload = multer({ storage }) const upload = multer({ storage })
const config = require('config') const config = require('config')
const debug = require('debug')('api') const log = require('../log')
const api = express.Router() const api = express.Router()
api.use(express.urlencoded({ extended: false })) api.use(express.urlencoded({ extended: false }))
@ -144,7 +144,7 @@ api.use((req, res) => res.sendStatus(404))
// Handle 500 // Handle 500
api.use((error, req, res, next) => { api.use((error, req, res, next) => {
debug(error) log.error(error)
res.status(500).send('500: Internal Server Error') res.status(500).send('500: Internal Server Error')
}) })

View file

@ -3,7 +3,7 @@ const path = require('path')
const moment = require('dayjs') const moment = require('dayjs')
const config = require('config') const config = require('config')
const settingsController = require('./controller/settings') const settingsController = require('./controller/settings')
const log = process.winstonLog const log = require('../log')
const { Task, TaskManager } = require('../taskManager') const { Task, TaskManager } = require('../taskManager')
const locales = require('../../locales') const locales = require('../../locales')
@ -62,7 +62,8 @@ const mail = {
} }
return email.send(msg) return email.send(msg)
.catch(e => { .catch(e => {
log.error('Error sending email =>', e.toString()) log.error('Error sending email =>')
log.error(e)
}) })
} }
} }

View file

@ -2,7 +2,7 @@ const express = require('express')
const OAuthServer = require('express-oauth-server') const OAuthServer = require('express-oauth-server')
const oauth = express.Router() const oauth = express.Router()
const oauthController = require('./controller/oauth') const oauthController = require('./controller/oauth')
const debug = require('debug')('oauth') const log = require('../log')
const oauthServer = new OAuthServer({ const oauthServer = new OAuthServer({
model: oauthController.model, model: oauthController.model,
@ -34,7 +34,7 @@ oauth.use((req, res) => res.sendStatus(404))
oauth.use((err, req, res, next) => { oauth.use((err, req, res, next) => {
const error_msg = err.toString() const error_msg = err.toString()
debug(error_msg) log.debug(error_msg)
res.status(500).send(error_msg) res.status(500).send(error_msg)
}) })

View file

@ -3,13 +3,13 @@ const path = require('path')
const crypto = require('crypto') const crypto = require('crypto')
const mkdirp = require('mkdirp') const mkdirp = require('mkdirp')
const sharp = require('sharp') const sharp = require('sharp')
const debug = require('debug')('storage') const log = require('../log')
const config = require('config') const config = require('config')
try { try {
mkdirp.sync(config.upload_path + '/thumb') mkdirp.sync(config.upload_path + '/thumb')
} catch (e) { } catch (e) {
debug.warn(e) log.error(e)
} }
const DiskStorage = { const DiskStorage = {
@ -25,10 +25,11 @@ const DiskStorage = {
let onError = false let onError = false
const err = e => { const err = e => {
if (onError) { if (onError) {
log.error(err)
return return
} }
onError = true onError = true
debug(e) log.error(e)
req.err = e req.err = e
cb(null) cb(null)
} }

View file

@ -1,12 +1,12 @@
const Event = require('../api/models/event') const Event = require('../api/models/event')
const config = require('config') const config = require('config')
const debug = require('debug')('fediverse:ego') const log = require('../log')
module.exports = { module.exports = {
async boost (req, res) { async boost (req, res) {
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`) const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length < 2) { return res.status(404).send('Event not found!') } 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])) const event = await Event.findByPk(Number(match[1]))
if (!event) { return res.status(404).send('Event not found!') } if (!event) { return res.status(404).send('Event not found!') }
await event.update({ boost: [...event.boost, req.body.actor] }) await event.update({ boost: [...event.boost, req.body.actor] })
@ -16,7 +16,7 @@ module.exports = {
async unboost (req, res) { async unboost (req, res) {
const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`) const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length < 2) { return res.status(404).send('Event not found!') } 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])) const event = await Event.findByPk(Number(match[1]))
if (!event) { return res.status(404).send('Event not found!') } if (!event) { return res.status(404).send('Event not found!') }
await event.update({ boost: event.boost.filter(actor => actor !== req.body.actor) }) 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/(.*)`) const match = req.body.object.match(`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length < 2) { return res.status(404).send('Event not found!') } if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
const event = await Event.findByPk(Number(match[1])) 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!') } if (!event) { return res.status(404).send('Event not found!') }
await event.update({ likes: [...event.likes, req.body.actor] }) await event.update({ likes: [...event.likes, req.body.actor] })
res.sendStatus(201) res.sendStatus(201)
@ -38,7 +38,7 @@ module.exports = {
const match = object.object.match(`${config.baseurl}/federation/m/(.*)`) const match = object.object.match(`${config.baseurl}/federation/m/(.*)`)
if (!match || match.length < 2) { return res.status(404).send('Event not found!') } if (!match || match.length < 2) { return res.status(404).send('Event not found!') }
const event = await Event.findByPk(Number(match[1])) 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!') } if (!event) { return res.status(404).send('Event not found!') }
await event.update({ likes: event.likes.filter(actor => actor !== body.actor) }) await event.update({ likes: event.likes.filter(actor => actor !== body.actor) })
res.sendStatus(201) res.sendStatus(201)

View file

@ -1,29 +1,33 @@
const config = require('config') const config = require('config')
const Helpers = require('./helpers') const Helpers = require('./helpers')
const crypto = require('crypto') const crypto = require('crypto')
const debug = require('debug')('federation:follows') const log = require('../log')
module.exports = { module.exports = {
// follow request from fediverse // follow request from fediverse
async follow (req, res) { async follow (req, res) {
const body = req.body const body = req.body
log.debug('follow')
if (typeof body.object !== 'string') { return } if (typeof body.object !== 'string') { return }
const username = body.object.replace(`${config.baseurl}/federation/u/`, '') 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 // check for duplicate
// if (!user.followers.includes(body.actor)) { // if (!user.followers.includes(body.actor)) {
// await user.addFollowers([req.fedi_user.id]) // await user.addFollowers([req.fedi_user.id])
// await user.update({ followers: [...user.followers, body.actor] }) // await user.update({ followers: [...user.followers, body.actor] })
await req.fedi_user.update({ follower: true }) 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 guid = crypto.randomBytes(16).toString('hex')
const message = { const message = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'id': `${config.baseurl}/federation/${guid}`, id: `${config.baseurl}/federation/${guid}`,
'type': 'Accept', type: 'Accept',
'actor': `${config.baseurl}/federation/u/${username}`, actor: `${config.baseurl}/federation/u/${username}`,
'object': body object: body
} }
Helpers.signAndSend(message, req.fedi_user.object.inbox) Helpers.signAndSend(message, req.fedi_user.object.inbox)
res.sendStatus(200) res.sendStatus(200)
@ -33,14 +37,17 @@ module.exports = {
async unfollow (req, res) { async unfollow (req, res) {
const body = req.body const body = req.body
const username = body.object.object.replace(`${config.baseurl}/federation/u/`, '') 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) { 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') return res.status(400).send('Bad things')
} }
await req.fedi_user.update({ follower: false }) await req.fedi_user.update({ follower: false })
debug('Unfollowed by %s', body.actor) log.debug(`Unfollowed by ${body.actor}`)
res.sendStatus(200) res.sendStatus(200)
} }
} }

View file

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

View file

@ -2,7 +2,7 @@ const Event = require('../api/models/event')
const Resource = require('../api/models/resource') const Resource = require('../api/models/resource')
const APUser = require('../api/models/ap_user') const APUser = require('../api/models/ap_user')
const debug = require('debug')('fediverse:resource') const log = require('../log')
const helpers = require('../helpers') const helpers = require('../helpers')
const linkifyHtml = require('linkifyjs/html') const linkifyHtml = require('linkifyjs/html')
@ -11,7 +11,7 @@ module.exports = {
// create a resource from AP Note // create a resource from AP Note
async create (req, res) { async create (req, res) {
if (!req.settings.enable_resources) { 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 return
} }
@ -26,7 +26,7 @@ module.exports = {
if (inReplyTo) { if (inReplyTo) {
// .. to an event ? // .. to an event ?
const match = inReplyTo && inReplyTo.match('.*/federation/m/(.*)') const match = inReplyTo && inReplyTo.match('.*/federation/m/(.*)')
debug('Event reply => ', inReplyTo) log.info(`Event reply => ${inReplyTo}`)
if (match) { if (match) {
event = await Event.findByPk(Number(match[1])) event = await Event.findByPk(Number(match[1]))
} else { } 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)) body.object.content = helpers.sanitizeHTML(linkifyHtml(body.object.content))
await Resource.create({ await Resource.create({
@ -58,15 +62,15 @@ module.exports = {
include: [{ model: APUser, required: false, attributes: ['ap_id'] }] include: [{ model: APUser, required: false, attributes: ['ap_id'] }]
}) })
if (!resource) { 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') return res.status(404).send('Not found')
} }
// check if fedi_user that requested resource removal // check if fedi_user that requested resource removal
// is the same that created the resource at first place // 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) { if (res.fedi_user.ap_id === resource.ap_user.id) {
await resource.destroy() await resource.destroy()
debug('Comment %s removed!', req.body.object.id) log.info(`Comment ${req.body.object.id} removed`)
res.sendStatus(201) res.sendStatus(201)
} else { } else {
res.sendStatus(403) res.sendStatus(403)

View file

@ -8,19 +8,21 @@ const cors = require('cors')
const settingsController = require('../api/controller/settings') const settingsController = require('../api/controller/settings')
const version = require('../../package.json').version const version = require('../../package.json').version
const url = require('url') const url = require('url')
const debug = require('debug')('webfinger') const log = require('../log')
router.use(cors()) router.use(cors())
router.use((req, res, next) => { router.use((req, res, next) => {
// is federation enabled ? // is federation enabled ?
if (req.settings.enable_federation) { return next() } if (req.settings.enable_federation) {
debug('Federation disabled') return next()
}
log.debug('Federation disabled')
res.status(404).send('Federation disabled') res.status(404).send('Federation disabled')
}) })
router.get('/webfinger', (req, res) => { router.get('/webfinger', (req, res) => {
if (!req.query || !req.query.resource || !req.query.resource.includes('acct:')) { 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.') 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 domain = (new url.URL(req.settings.baseurl)).host
const [, name, req_domain] = resource.match(/acct:(.*)@(.*)/) const [, name, req_domain] = resource.match(/acct:(.*)@(.*)/)
if (domain !== req_domain) { 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.') 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) { 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}`) return res.status(404).send(`No record found for ${name}`)
} }
log.info(`webfinger ${resource} ${domain}`)
const ret = { const ret = {
subject: `acct:${name}@${domain}`, subject: `acct:${name}@${domain}`,
links: [ links: [
@ -121,6 +124,7 @@ router.get('/nodeinfo', (req, res) => {
}) })
router.use('/host-meta', (req, res) => { router.use('/host-meta', (req, res) => {
log.debug('host-meta')
res.type('application/xml') res.type('application/xml')
res.send(`<?xml version="1.0" encoding="UTF-8"?> res.send(`<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"> <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
@ -130,13 +134,13 @@ router.use('/host-meta', (req, res) => {
// Handle 404 // Handle 404
router.use((req, res) => { 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') res.status(404).send('404: Page not Found')
}) })
// Handle 500 // Handle 500
router.use((error, req, res, next) => { router.use((error, req, res, next) => {
debug(error) log.error(error)
res.status(500).send('500: Internal Server Error') res.status(500).send('500: Internal Server Error')
}) })

View file

@ -7,7 +7,7 @@ const timezone = require('dayjs/plugin/timezone')
dayjs.extend(timezone) dayjs.extend(timezone)
const config = require('config') const config = require('config')
const debug = require('debug')('helpers') const log = require('./log')
const pkg = require('../package.json') const pkg = require('../package.json')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
@ -77,7 +77,7 @@ module.exports = {
}, },
async getImageFromURL (url) { async getImageFromURL (url) {
debug(`getImageFromURL ${url}`) log.debug(`getImageFromURL ${url}`)
const filename = crypto.randomBytes(16).toString('hex') + '.jpg' const filename = crypto.randomBytes(16).toString('hex') + '.jpg'
const finalPath = path.resolve(config.upload_path, filename) const finalPath = path.resolve(config.upload_path, filename)
const thumbPath = path.resolve(config.upload_path, 'thumb', filename) const thumbPath = path.resolve(config.upload_path, 'thumb', filename)
@ -164,7 +164,7 @@ module.exports = {
// console.error(event) // console.error(event)
// console.error(response) // console.error(response)
} catch (e) { } catch (e) {
debug(e) log.error(e)
} }
// res.json('ok') // res.json('ok')

View file

@ -3,7 +3,7 @@ const { Nuxt, Builder } = require('nuxt')
// Import and Set Nuxt.js options // Import and Set Nuxt.js options
const nuxtConfig = require('../nuxt.config.js') const nuxtConfig = require('../nuxt.config.js')
const config = require('config') const config = require('config')
const consola = require('consola') const log = require('./log')
async function main () { async function main () {
nuxtConfig.server = config.server nuxtConfig.server = config.server
@ -21,21 +21,20 @@ async function main () {
try { try {
await nuxt.listen() await nuxt.listen()
} catch (e) { } catch (e) {
consola.error(e.toString()) log.err(e)
process.winstonLog.error(e)
return return
} }
const { TaskManager } = require('./taskManager') const { TaskManager } = require('./taskManager')
TaskManager.start() TaskManager.start()
const msg = `Listen on ${config.server.host}:${config.server.port} , visit me here => ${config.baseurl}` const msg = `Listen on ${config.server.host}:${config.server.port} , visit me here => ${config.baseurl}`
consola.info(msg) log.info(msg)
process.winstonLog.info(msg)
// close connections/port/unix socket // close connections/port/unix socket
function shutdown () { function shutdown () {
TaskManager.stop() TaskManager.stop()
nuxt.close(async () => { nuxt.close(async () => {
log.info('Closing DB')
const sequelize = require('./api/models') const sequelize = require('./api/models')
await sequelize.close() await sequelize.close()
process.exit() process.exit()

View file

@ -1 +1,16 @@
module.exports = process.winstonLog const { createLogger, transports, format } = require('winston')
const logger = createLogger({
transports: process.env.NODE_ENV !== 'production'
? [new transports.Console(
{ level: 'debug', format: format.combine(format.colorize(), format.simple(), format.errors({ stack: true })) }
)]
: [new transports.File(
{
filename: 'gancio.log',
format: format.combine(format.simple(), format.errors({ stack: true }))
}
)]
})
module.exports = logger

View file

@ -9,7 +9,7 @@ const cookieParser = require('cookie-parser')
const federation = require('./federation') const federation = require('./federation')
const webfinger = require('./federation/webfinger') const webfinger = require('./federation/webfinger')
const { spamFilter } = require('./federation/helpers') const { spamFilter } = require('./federation/helpers')
const debug = require('debug')('routes') const log = require('./log')
const exportController = require('./api/controller/export') const exportController = require('./api/controller/export')
const eventController = require('./api/controller/event') const eventController = require('./api/controller/event')
const announceController = require('./api/controller/announce') const announceController = require('./api/controller/announce')
@ -26,7 +26,7 @@ app.use(spamFilter)
app.use(metricsMiddleware) app.use(metricsMiddleware)
app.use((req, res, next) => { app.use((req, res, next) => {
debug(req.method, req.path) log.debug(`${req.method} ${req.path}`)
next() next()
}) })
@ -62,7 +62,7 @@ app.use('/oauth', oauth)
// // Handle 500 // // Handle 500
app.use((error, req, res, next) => { app.use((error, req, res, next) => {
debug('Error 500: %s', error) log.error(error)
res.status(500).send('500: Internal Server Error') res.status(500).send('500: Internal Server Error')
}) })

View file

@ -1,4 +1,4 @@
const log = process.winstonLog // require('./log') // // require('debug')('TaskManager') const log = require('./log')
const eventController = require('./api/controller/event') const eventController = require('./api/controller/event')
// const notifier = require('./notifier') // const notifier = require('./notifier')
@ -47,6 +47,8 @@ class Task {
class TaskManager { class TaskManager {
constructor () { constructor () {
this.tasks = [] this.tasks = []
this.interval = 1
this.timeout = null
} }
start (interval = loopInterval) { start (interval = loopInterval) {
@ -64,7 +66,7 @@ class TaskManager {
} }
add (task) { add (task) {
log.info(`ADD TASK ${task.name}`) log.debug(`[TASK] Add ${task.name} (${task.repeatEach * this.interval} seconds)`)
this.tasks.push(task) this.tasks.push(task)
} }

2908
yarn.lock

File diff suppressed because it is too large Load diff