xmpp-bot/lib/webhook/index.js

195 lines
6.2 KiB
JavaScript
Raw Permalink Normal View History

2019-10-13 03:38:25 +02:00
/**
* Webhooks listener
*
* Create webhooks listener
*
* @file This files defines the webhooks listener
* @author nioc
* @since 1.0.0
* @license AGPL-3.0+
*/
module.exports = (logger, config, xmpp) => {
const http = require('http')
const express = require('express')
const bodyParser = require('body-parser')
const basicAuth = require('express-basic-auth')
const jmespath = require('jmespath')
const port = config.listener.port || 8000
const portSsl = config.listener.ssl.port || 8001
const webhook = express()
2019-10-13 03:38:25 +02:00
// handle connection from proxy (get IP in 'X-Forwarded-For' header)
webhook.set('trust proxy', true)
// logs request
if (config.listener.log.active) {
const morgan = require('morgan')
const fs = require('fs')
// create path if not exists
if (!fs.existsSync(config.listener.log.path)) {
try {
fs.mkdirSync(config.listener.log.path)
} catch (error) {
logger.fatal(`Can not create webhooks log folder: ${error.message}`)
process.exit(99)
}
}
// create log
const accessLogStream = fs.createWriteStream(config.listener.log.path + config.listener.log.filename, { flags: 'a' })
accessLogStream.on('error', (err) => {
logger.fatal('Can not create webhooks log file: ' + err.message)
process.exit(99)
})
webhook.use(morgan('combined', { stream: accessLogStream }))
}
// parse request
webhook.use(bodyParser.json())
// add basic authentification
webhook.use(basicAuth({
users: config.listener.users,
unauthorizedResponse: 'Invalid authorization'
}))
// handle post request
webhook.post(config.listener.path + '/*', (req, res) => {
logger.info(`Incoming webhook from ${req.auth.user}`)
const webhook = config.getWebhookAction(req.path)
2019-10-13 03:38:25 +02:00
if (!webhook) {
logger.error(`Webhook received: ${req.path}, not found`)
return res.status(404).send('Webhook not found')
}
logger.debug(`Webhook received: ${webhook.path}, start action: ${webhook.action}`)
logger.trace(req.body)
switch (webhook.action) {
case 'send_xmpp_message': {
2019-10-13 03:38:25 +02:00
// get destination
if ('destination' in req.body === false) {
logger.error('Destination not found')
return res.status(400).send('Destination not found')
}
const destination = req.body.destination
2019-10-13 03:38:25 +02:00
// get message
if ('message' in req.body === false) {
logger.error('Message not found')
return res.status(400).send('Message not found')
}
const message = req.body.message
2019-10-13 03:38:25 +02:00
// check if destination is a group chat
const type = config.xmpp.rooms.some((room) => room.id === destination) ? 'groupchat' : 'chat'
2019-10-13 03:38:25 +02:00
// send message
logger.trace(`Send to ${destination} (group:${type}) following message :\r\n${message}`)
xmpp.send(destination, message, type)
2019-11-23 20:30:13 +01:00
.then(() => {
return res.status(200).send({ status: 'ok', destination })
2019-11-23 20:30:13 +01:00
})
.catch(() => {
return res.status(500).send('Could not send message')
})
break
}
2019-10-13 03:38:25 +02:00
case 'send_xmpp_template': {
2019-10-13 03:38:25 +02:00
// bind data in template
const msg = webhook.template.replace(/\$\{(.+?)\}/g, (match, $1) => {
2019-10-13 03:38:25 +02:00
return jmespath.search(req.body, $1) || ''
})
logger.trace(`Message:\r\n${msg}`)
logger.trace(`Arguments: ${webhook.args}`)
// send message
logger.trace(`Send to ${webhook.args.destination} (group:${webhook.args.type}) following message :\r\n${msg}`)
xmpp.send(webhook.args.destination, msg, webhook.args.type)
2019-11-23 20:30:13 +01:00
.then(() => {
return res.status(200).send('ok')
})
.catch(() => {
return res.status(500).send('Could not send message')
})
break
}
2019-10-13 03:38:25 +02:00
default:
return res.status(204).send()
}
})
// handle non post requests
webhook.all('*', (req, res) => {
return res.status(405).send('Method not allowed')
})
// handle server error
webhook.on('error', (error) => {
logger.error('Error', error)
})
2019-11-24 21:39:23 +01:00
// get IP v4 addresses and prepare endpoints for output
let addresses = []
const networkInterfaces = require('os').networkInterfaces()
for (const ifaceName in networkInterfaces) {
2019-11-24 21:39:23 +01:00
addresses = addresses.concat(networkInterfaces[ifaceName].reduce((add, iface) => {
if (iface.family === 'IPv4') {
add.push(iface.address)
2019-11-24 21:39:23 +01:00
}
return add
}, []))
}
2019-10-13 03:38:25 +02:00
// start HTTP listener
const httpServer = http.createServer(webhook).listen(port, () => {
2019-11-24 21:39:23 +01:00
let endpoints = `http://localhost:${port}${config.listener.path}`
addresses.forEach(address => {
endpoints += ` http://${address}:${port}${config.listener.path}`
})
logger.info(`Listening webhooks on ${endpoints}`)
2019-10-13 03:38:25 +02:00
})
// start HTTPS listener
if (config.listener.ssl.port !== null) {
if (process.getuid) {
logger.debug(`App is started with uid: ${process.getuid()}`)
}
logger.debug(`Start HTTPS on port ${portSsl}, private key: ${config.listener.ssl.keyPath}, cert: ${config.listener.ssl.certPath}`)
const https = require('https')
const fs = require('fs')
// check if cert is readable
try {
fs.accessSync(config.listener.ssl.keyPath, fs.constants.R_OK)
logger.debug('Can read private key')
try {
fs.accessSync(config.listener.ssl.certPath, fs.constants.R_OK)
logger.debug('Can read certificate')
const credentials = {
2019-10-13 03:38:25 +02:00
key: fs.readFileSync(config.listener.ssl.keyPath),
cert: fs.readFileSync(config.listener.ssl.certPath)
}
https.createServer(credentials, webhook).listen(portSsl, () => {
2019-11-24 21:39:23 +01:00
let endpoints = `https://localhost:${portSsl}${config.listener.path}`
addresses.forEach(address => {
endpoints += ` https://${address}:${portSsl}${config.listener.path}`
})
logger.info(`Listening webhooks on ${endpoints}`)
2019-10-13 03:38:25 +02:00
})
} catch (err) {
logger.error(`Can not read certificate: ${err.message}`)
}
} catch (err) {
logger.error(`Can not read private key: ${err.message}`)
}
}
// Closing HTTP listener (for test)
webhook.close = () => {
httpServer.close()
}
return webhook
}