2021-04-12 14:36:44 +02:00
|
|
|
/*
|
|
|
|
Copyright 2021 Yarmo Mackenbach
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
const router = require('express').Router()
|
2021-04-15 15:09:57 +02:00
|
|
|
const { query, validationResult } = require('express-validator')
|
2021-04-12 14:36:44 +02:00
|
|
|
const fetcher = require('../../../fetcher')
|
2021-04-15 14:47:45 +02:00
|
|
|
const E = require('../../../enums')
|
2021-04-12 14:36:44 +02:00
|
|
|
require('dotenv').config()
|
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
const opts = {
|
|
|
|
claims: {
|
2021-04-16 13:03:01 +02:00
|
|
|
irc: {
|
2021-07-09 23:44:52 +02:00
|
|
|
nick: process.env.IRC_NICK || null
|
2021-04-16 13:03:01 +02:00
|
|
|
},
|
|
|
|
matrix: {
|
|
|
|
instance: process.env.MATRIX_INSTANCE || null,
|
2021-07-09 23:44:52 +02:00
|
|
|
accessToken: process.env.MATRIX_ACCESS_TOKEN || null
|
2021-04-16 13:03:01 +02:00
|
|
|
},
|
2021-04-15 14:47:45 +02:00
|
|
|
xmpp: {
|
|
|
|
service: process.env.XMPP_SERVICE || null,
|
|
|
|
username: process.env.XMPP_USERNAME || null,
|
2021-07-09 23:44:52 +02:00
|
|
|
password: process.env.XMPP_PASSWORD || null
|
2021-04-15 14:47:45 +02:00
|
|
|
},
|
|
|
|
twitter: {
|
2021-07-09 23:44:52 +02:00
|
|
|
bearerToken: process.env.TWITTER_BEARER_TOKEN || null
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 14:36:44 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
// Root route
|
2021-04-12 14:36:44 +02:00
|
|
|
router.get('/', async (req, res) => {
|
2021-04-15 15:09:57 +02:00
|
|
|
return res.status(400).json({ errors: 'Invalid endpoint' })
|
2021-04-12 14:36:44 +02:00
|
|
|
})
|
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
// HTTP route
|
2021-04-19 11:44:30 +02:00
|
|
|
router.get(
|
|
|
|
'/get/http',
|
2021-04-15 15:09:57 +02:00
|
|
|
query('url').isURL(),
|
|
|
|
query('format').isIn([E.ProofFormat.JSON, E.ProofFormat.TEXT]),
|
|
|
|
(req, res) => {
|
|
|
|
const errors = validationResult(req)
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(400).json({ errors: errors.array() })
|
2021-04-15 14:47:45 +02:00
|
|
|
}
|
2021-04-15 15:09:57 +02:00
|
|
|
|
2021-04-19 11:44:30 +02:00
|
|
|
fetcher.http
|
|
|
|
.fn(req.query, opts)
|
|
|
|
.then((result) => {
|
2021-04-15 15:09:57 +02:00
|
|
|
switch (req.query.format) {
|
|
|
|
case E.ProofFormat.JSON:
|
|
|
|
return res.status(200).json(result)
|
|
|
|
|
|
|
|
case E.ProofFormat.TEXT:
|
|
|
|
return res.status(200).send(result)
|
|
|
|
}
|
|
|
|
})
|
2021-04-19 11:44:30 +02:00
|
|
|
.catch((err) => {
|
2021-04-15 15:09:57 +02:00
|
|
|
return res.status(400).json({ errors: err.message ? err.message : err })
|
|
|
|
})
|
2021-04-19 11:44:30 +02:00
|
|
|
}
|
|
|
|
)
|
2021-04-12 14:36:44 +02:00
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
// DNS route
|
2021-04-19 11:44:30 +02:00
|
|
|
router.get('/get/dns', query('domain').isFQDN(), (req, res) => {
|
|
|
|
const errors = validationResult(req)
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(400).json({ errors: errors.array() })
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher.dns
|
|
|
|
.fn(req.query, opts)
|
|
|
|
.then((data) => {
|
|
|
|
return res.status(200).send(data)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(400).json({ errors: err.message ? err.message : err })
|
|
|
|
})
|
2021-04-12 14:36:44 +02:00
|
|
|
})
|
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
// XMPP route
|
2021-04-19 11:44:30 +02:00
|
|
|
router.get(
|
|
|
|
'/get/xmpp',
|
2021-04-15 15:09:57 +02:00
|
|
|
query('id').isEmail(),
|
2021-04-19 11:44:30 +02:00
|
|
|
query('field').isIn([
|
|
|
|
'fn',
|
|
|
|
'number',
|
|
|
|
'userid',
|
|
|
|
'url',
|
|
|
|
'bday',
|
|
|
|
'nickname',
|
|
|
|
'note',
|
2021-07-09 23:44:52 +02:00
|
|
|
'desc'
|
2021-04-19 11:44:30 +02:00
|
|
|
]),
|
2021-04-15 15:09:57 +02:00
|
|
|
async (req, res) => {
|
2021-04-19 11:44:30 +02:00
|
|
|
if (
|
|
|
|
!opts.claims.xmpp.service ||
|
|
|
|
!opts.claims.xmpp.username ||
|
|
|
|
!opts.claims.xmpp.password
|
|
|
|
) {
|
2021-04-15 15:09:57 +02:00
|
|
|
return res.status(501).json({ errors: 'XMPP not enabled on server' })
|
|
|
|
}
|
|
|
|
const errors = validationResult(req)
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(400).json({ errors: errors.array() })
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:44:30 +02:00
|
|
|
fetcher.xmpp
|
|
|
|
.fn(req.query, opts)
|
2021-04-15 15:09:57 +02:00
|
|
|
.then((data) => {
|
|
|
|
return res.status(200).send(data)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(400).json({ errors: err.message ? err.message : err })
|
|
|
|
})
|
2021-04-19 11:44:30 +02:00
|
|
|
}
|
|
|
|
)
|
2021-04-12 14:36:44 +02:00
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
// Twitter route
|
2021-04-19 11:44:30 +02:00
|
|
|
router.get('/get/twitter', query('tweetId').isInt(), async (req, res) => {
|
|
|
|
if (!opts.claims.twitter.bearerToken) {
|
|
|
|
return res.status(501).json({ errors: 'Twitter not enabled on server' })
|
|
|
|
}
|
|
|
|
const errors = validationResult(req)
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(400).json({ errors: errors.array() })
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher.twitter
|
|
|
|
.fn(req.query, opts)
|
|
|
|
.then((data) => {
|
|
|
|
return res.status(200).send(data)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(400).json({ errors: err.message ? err.message : err })
|
|
|
|
})
|
2021-04-12 14:36:44 +02:00
|
|
|
})
|
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
// Matrix route
|
2021-04-19 11:44:30 +02:00
|
|
|
router.get(
|
|
|
|
'/get/matrix',
|
2021-04-15 15:09:57 +02:00
|
|
|
query('roomId').isString(),
|
|
|
|
query('eventId').isString(),
|
|
|
|
async (req, res) => {
|
|
|
|
if (!opts.claims.matrix.instance || !opts.claims.matrix.accessToken) {
|
|
|
|
return res.status(501).json({ errors: 'Matrix not enabled on server' })
|
|
|
|
}
|
|
|
|
const errors = validationResult(req)
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(400).json({ errors: errors.array() })
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:44:30 +02:00
|
|
|
fetcher.matrix
|
|
|
|
.fn(req.query, opts)
|
2021-04-15 15:09:57 +02:00
|
|
|
.then((data) => {
|
|
|
|
return res.status(200).send(data)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(400).json({ errors: err.message ? err.message : err })
|
|
|
|
})
|
2021-04-19 11:44:30 +02:00
|
|
|
}
|
|
|
|
)
|
2021-04-12 14:36:44 +02:00
|
|
|
|
2021-04-15 14:47:45 +02:00
|
|
|
// IRC route
|
2021-04-19 11:44:30 +02:00
|
|
|
router.get('/get/irc', query('nick').isString(), async (req, res) => {
|
|
|
|
if (!opts.claims.irc.nick) {
|
|
|
|
return res.status(501).json({ errors: 'IRC not enabled on server' })
|
|
|
|
}
|
|
|
|
const errors = validationResult(req)
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(400).json({ errors: errors.array() })
|
|
|
|
}
|
|
|
|
|
|
|
|
fetcher.irc
|
|
|
|
.fn(req.query, opts)
|
|
|
|
.then((data) => {
|
|
|
|
return res.status(200).send(data)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(400).json({ errors: err.message ? err.message : err })
|
|
|
|
})
|
2021-04-12 14:36:44 +02:00
|
|
|
})
|
|
|
|
|
2021-04-15 23:22:17 +02:00
|
|
|
// Gitlab route
|
2021-04-19 11:44:30 +02:00
|
|
|
router.get(
|
|
|
|
'/get/gitlab',
|
2021-04-15 23:22:17 +02:00
|
|
|
query('domain').isFQDN(),
|
|
|
|
query('username').isString(),
|
|
|
|
async (req, res) => {
|
|
|
|
const errors = validationResult(req)
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return res.status(400).json({ errors: errors.array() })
|
|
|
|
}
|
|
|
|
|
2021-04-19 11:44:30 +02:00
|
|
|
fetcher.gitlab
|
|
|
|
.fn(req.query, opts)
|
2021-04-15 23:22:17 +02:00
|
|
|
.then((data) => {
|
|
|
|
return res.status(200).send(data)
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
return res.status(400).json({ errors: err.message ? err.message : err })
|
|
|
|
})
|
2021-04-19 11:44:30 +02:00
|
|
|
}
|
|
|
|
)
|
2021-04-15 23:22:17 +02:00
|
|
|
|
2021-04-12 14:36:44 +02:00
|
|
|
module.exports = router
|