main
HerrHase 1 year ago
parent e3ed476e23
commit 5c0e64d71c

5454
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -12,7 +12,11 @@ const server = fastify()
// adding eventEmitter // adding eventEmitter
server.decorate('eventEmitter', new EventEmitter()) server.decorate('eventEmitter', new EventEmitter())
// adding xmpp /**
* add xmpp
*
*/
import xmpp from './plugins/xmpp.js' import xmpp from './plugins/xmpp.js'
server.register(xmpp, { server.register(xmpp, {
@ -23,6 +27,43 @@ server.register(xmpp, {
to: process.env.XMPP_TO to: process.env.XMPP_TO
}) })
/**
* add helmet
*
*/
import helmet from '@fastify/helmet'
server.register(
helmet, {
referrerPolicy: {
policy: ['origin']
}
}
)
/**
* add rateLimit
*
*/
import rateLimit from '@fastify/rate-limit'
const rateLimitSettings = {
max: 100,
timeWindow: 60000
}
if (process.env.APP_RATE_LIMIT_MAX) {
rateLimitSettings.max = process.env.APP_RATE_LIMIT_MAX
}
if (process.env.APP_RATE_LIMIT_TIMEWINDOW) {
rateLimitSettings.timeWindow = process.env.APP_RATE_LIMIT_TIMEWINDOW
}
server.register(rateLimit, rateLimitSettings)
/** /**
* add routes * add routes
* *

@ -0,0 +1,35 @@
import * as DOMPurify from 'dompurify'
/**
*
* handle parser
*
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/super-fastify-directus.git
*
*/
async function parserHandler(request, response) {
// getting allowed parsers from .env as array
const allowedParsers = process.env.APP_API_ALLOWED_PARSERS.split(',')
// getting name from request
const parserName = DOMPurify.sanitize(request.params.parser)
// if parser not found send 404
if (allowedParsers.indexOf(parserName) === -1) {
return response
.code(404)
.send()
}
// getting parser and set body to parser
const Parser = await import('./../../parsers/' + parserName + '.js')
response.locals.parser = new Parser.default(request.body)
}
export default parserHandler

@ -0,0 +1,18 @@
/**
* handle token
*
* @author Björn Hase <me@herr-hase.wtf>
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://gitea.node001.net/HerrHase/super-fastify-directus.git
*
*/
async function tokenHandler(request, token) {
if (request.params.token !== process.env.APP_API_TOKEN) {
return response
.code(401)
.send()
}
}
export default tokenHandler

@ -16,9 +16,9 @@ export default async function(fastify, opts)
* @param {object} response * @param {object} response
* *
*/ */
fastify.get('/v1', async function (request, reply) fastify.get('/v1', async function (request, response)
{ {
reply response
.code(200) .code(200)
.send() .send()
}) })

@ -1,3 +1,6 @@
import tokenHandler from './../../handlers/token.js'
import parserHandler from './../../handlers/parser.js'
/** /**
* handle webhook * handle webhook
* *
@ -7,8 +10,11 @@
* *
*/ */
export default async function(fastify, opts) export default async function(fastify, options) {
{
fastify.addHook('preHandler', tokenHandler)
fastify.addHook('preHandler', parserHandler)
/** /**
* getting post getting allowed parser class and send over xmpp * getting post getting allowed parser class and send over xmpp
* *
@ -16,36 +22,19 @@ export default async function(fastify, opts)
* @param {object} response * @param {object} response
* *
*/ */
fastify.post('/v1/:parser([a-zA-Z0-9]{0,255})/:token([a-zA-Z0-9])', async function (request, reply) fastify.post('/v1/:parser([a-zA-Z0-9]{0,255})/:token([a-zA-Z0-9])', async function (request, response) {
{
if (request.params.token !== process.env.APP_API_TOKEN) {
return reply
.code(401)
.send()
}
// getting allowed parsers from .env as array // getting parser from preHandler: parserHandler
const allowedParsers = process.env.APP_API_ALLOWED_PARSERS.split(',') const result = response.locals.parser.run()
if (allowedParsers.indexOf(request.params.parser) === -1) {
return reply
.code(404)
.send()
}
// getting parser and set body to parser
const Parser = await import('./../../parsers/' + request.params.parser + '.js')
const parser = new Parser.default(request.body)
const result = parser.run()
// send event for send xmpp // send event for send xmpp
fastify.eventEmitter.emit('send-message', { fastify.eventEmitter.emit('send-message', {
'message': result 'message': result
}) })
reply response
.code(200) .code(200)
.send() .send()
}) })
} }

@ -1,10 +1,14 @@
import server from './bootstrap.js' import server from './_bootstrap.js'
// let it rain // let it rain
const start = async () => { const start = async () => {
try { try {
await server.listen(process.env.APP_PORT) await server.listen({
console.log('Server is running on port ' + process.env.APP_PORT) port: process.env.APP_PORT
})
console.log('Server: start')
console.log('Server: running on port ' + process.env.APP_PORT)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
process.exit(1) process.exit(1)

File diff suppressed because it is too large Load Diff

@ -1,16 +1,18 @@
{ {
"private": true, "private": true,
"name": "server", "name": "server",
"version": "0.1.1", "version": "0.1.0",
"scripts": { "scripts": {
"start": "node index.js" "start": "node index.js"
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@xmpp/client": "^0.13.0", "@fastify/helmet": "^11.0.0",
"@fastify/rate-limit": "^8.0.1",
"@xmpp/client": "^0.13.1",
"dompurify": "^3.0.4",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
"fastify": "^3.24.0", "fastify": "^4.19.2",
"fastify-formbody": "^5.2.0", "fastify-plugin": "^4.5.0"
"isomorphic-dompurify": "^0.16.0"
} }
} }

@ -1,5 +1,5 @@
import Parser from './parser.js' import Parser from './parser.js'
import DOMPurify from 'isomorphic-dompurify' import * as DOMPurify from 'dompurify'
/** /**
* Parser for Kuma, getting only error message * Parser for Kuma, getting only error message
@ -9,10 +9,14 @@ import DOMPurify from 'isomorphic-dompurify'
* @link https://gitea.node001.net/HerrHase/tellme-bot.git * @link https://gitea.node001.net/HerrHase/tellme-bot.git
* *
*/ */
class Kuma extends Parser class Kuma extends Parser {
{
parse() /**
{ *
*
*/
parse() {
// check for msg and clean it // check for msg and clean it
if (this.body.msg) { if (this.body.msg) {
this.message = DOMPurify.sanitize(this.body.msg) this.message = DOMPurify.sanitize(this.body.msg)

@ -7,14 +7,14 @@
* @link https://gitea.node001.net/HerrHase/tellme-bot.git * @link https://gitea.node001.net/HerrHase/tellme-bot.git
* *
*/ */
class Parser class Parser {
{
/** /**
* *
* *
*/ */
constructor(body) constructor(body) {
{
// body from webhook // body from webhook
this.body = body this.body = body
@ -28,8 +28,7 @@ class Parser
* @return string * @return string
* *
*/ */
run() run() {
{
this.parse() this.parse()
return this.message return this.message
} }

@ -1,5 +1,5 @@
import Parser from './parser.js' import Parser from './parser.js'
import DOMPurify from 'isomorphic-dompurify' import * as DOMPurify from 'dompurify'
/** /**
* Parser for "text" in Json, is used by slack * Parser for "text" in Json, is used by slack
@ -9,10 +9,14 @@ import DOMPurify from 'isomorphic-dompurify'
* @link https://gitea.node001.net/HerrHase/tellme-bot.git * @link https://gitea.node001.net/HerrHase/tellme-bot.git
* *
*/ */
class Text extends Parser class Text extends Parser {
{
parse() /**
{ *
*
*/
parse() {
// check for msg and clean it // check for msg and clean it
if (this.body.text) { if (this.body.text) {
this.message = DOMPurify.sanitize(this.body.text) this.message = DOMPurify.sanitize(this.body.text)

@ -21,17 +21,22 @@ export default function (fastify, options, done) {
*/ */
async function handleSendMessage(data) { async function handleSendMessage(data) {
let send = false
// Sends a chat message to itself // Sends a chat message to itself
const message = xml( const message = xml(
'message', 'message', {
{
type: 'chat', type: 'chat',
to: options.to to: options.to
}, },
xml('body', {}, data.message) xml('body', {}, data.message)
) )
try {
await xmpp.send(message) await xmpp.send(message)
} catch (error) {
console.log(error)
}
} }
const xmpp = client({ const xmpp = client({
@ -47,9 +52,8 @@ export default function (fastify, options, done) {
}) })
// handle if client goes online // handle if client goes online
xmpp.on('online', (address) => xmpp.on('online', (address) => {
{ console.log('Xmpp: connected to ' + address)
console.log('connected to ' + address)
// check for event and remove it // check for event and remove it
if (fastify.eventEmitter.listeners('send-message').length > 0) { if (fastify.eventEmitter.listeners('send-message').length > 0) {

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save