Make v2 proxy API compatible with new fetchers

This commit is contained in:
Yarmo Mackenbach 2021-04-15 14:47:45 +02:00
parent ddebc2d58b
commit c13c65aa5e
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1

View file

@ -15,34 +15,30 @@ limitations under the License.
*/ */
const router = require('express').Router() const router = require('express').Router()
const fetcher = require('../../../fetcher') const fetcher = require('../../../fetcher')
const E = require('../../../enums')
require('dotenv').config() require('dotenv').config()
const xmpp_service = process.env.XMPP_SERVICE || null const opts = {
const xmpp_username = process.env.XMPP_USERNAME || null claims: {
const xmpp_password = process.env.XMPP_PASSWORD || null xmpp: {
const twitter_bearer_token = process.env.TWITTER_BEARER_TOKEN || null service: process.env.XMPP_SERVICE || null,
const matrix_instance = process.env.MATRIX_INSTANCE || null username: process.env.XMPP_USERNAME || null,
const matrix_access_token = process.env.MATRIX_ACCESS_TOKEN || null password: process.env.XMPP_PASSWORD || null,
const irc_nick = process.env.IRC_NICK || null },
twitter: {
let xmpp_enabled = true, bearerToken: process.env.TWITTER_BEARER_TOKEN || null,
twitter_enabled = false, },
matrix_enabled = false, matrix: {
irc_enabled = false instance: process.env.MATRIX_INSTANCE || null,
accessToken: process.env.MATRIX_ACCESS_TOKEN || null,
if (!xmpp_service || !xmpp_username || !xmpp_password) { },
xmpp_enabled = false irc: {
} nick: process.env.IRC_NICK || null,
if (twitter_bearer_token) { },
twitter_enabled = true },
}
if (matrix_instance && matrix_access_token) {
matrix_enabled = true
}
if (irc_nick) {
irc_enabled = true
} }
// Root route
router.get('/', async (req, res) => { router.get('/', async (req, res) => {
return res.status(400).json({ return res.status(400).json({
data: [], data: [],
@ -51,30 +47,67 @@ router.get('/', async (req, res) => {
}) })
}) })
router.param('url', async (req, res, next, url) => { // HTTP route
req.params.url = decodeURI(url) router.get('/get/http', (req, res) => {
if (!req.query.url) {
if (!validUrl.isUri(req.params.url)) { return res.status(400).json({ error: 'Missing parameter(s)' })
return res.status(400).json({ error: 'URL provided was not valid' }) }
if (!validUrl.isUri(req.query.url)) {
return res.status(400).json({ error: 'Invalid URL' })
} }
next() fetcher.http(req.query, opts)
}) .then(result => {
switch (req.query.format) {
case E.ProofFormat.JSON:
return res.status(200).json(result)
break;
router.param('xmppid', async (req, res, next, xmppid) => { case E.ProofFormat.TEXT:
req.params.xmppid = xmppid return res.status(200).send(result)
break;
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(req.params.xmppid)) { default:
next() throw new Error('Invalid proof format')
} else { break;
return res.status(400).json({ error: 'XMPP_ID was not valid' })
} }
})
.catch(err => {
return res.status(400).json({ error: err.message ? err.message : err })
})
}) })
router.param('xmppdata', async (req, res, next, xmppdata) => { // DNS route
req.params.xmppdata = xmppdata.toUpperCase() router.get('/get/dns', (req, res) => {
if (!req.query.domain) {
return res.status(400).json({ error: 'Missing parameter(s)' })
}
const allowedData = [ fetcher
.dns(req.query, opts)
.then((data) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err.message ? err.message : err })
})
})
// XMPP route
router.get('/get/xmpp', async (req, res) => {
if (!opts.claims.xmpp.service || !opts.claims.xmpp.username || !opts.claims.xmpp.password) {
return res.status(501).json({ error: 'XMPP not enabled on server' })
}
if (!req.query.id || !req.query.field) {
return res.status(400).json({ error: 'Missing parameter(s)' })
}
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(req.query.id))) {
return res.status(400).json({ error: 'Invalid XMPP ID' })
}
const allowedField = [
'FN', 'FN',
'NUMBER', 'NUMBER',
'USERID', 'USERID',
@ -84,76 +117,12 @@ router.param('xmppdata', async (req, res, next, xmppdata) => {
'NOTE', 'NOTE',
'DESC', 'DESC',
] ]
if (!allowedField.includes(req.query.field)) {
if (!allowedData.includes(req.params.xmppdata)) { return res.status(400).json({ error: 'Invalid XMPP vCard field' })
return res.status(400).send({
data: [],
error:
'Allowed data are: FN, NUMBER, USERID, URL, BDAY, NICKNAME, NOTE, DESC',
})
}
next()
})
router.get('/get/json/:url', (req, res) => {
bentReq(req.params.url, 'json', {
Accept: 'application/json',
})
.then(async (result) => {
return await result.json()
})
.then(async (data) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err })
})
})
router.get('/get/text/:url', (req, res) => {
bentReq(req.params.url)
.then(async (result) => {
return await result.text()
})
.then(async (result) => {
return res.status(200).send(result)
})
.catch((err) => {
return res.status(400).json({ error: err })
})
})
router.get('/get/dns/:hostname', (req, res) => {
fetcher
.dns(req.params.hostname)
.then((data) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err })
})
})
router.get('/get/xmpp/:xmppid', async (req, res) => {
return res.status(400).send({
data: [],
error:
'Data request parameter missing (FN, NUMBER, USERID, URL, BDAY, NICKNAME, NOTE, DESC)',
})
})
router.get('/get/xmpp/:xmppid/:xmppdata', async (req, res) => {
if (!xmpp_enabled) {
return res.status(501).json({ error: 'XMPP not enabled on server' })
} }
fetcher fetcher
.xmpp(req.params.xmppid, req.params.xmppdata, { .xmpp(req.query, opts)
service: xmpp_service,
username: xmpp_username,
password: xmpp_password,
})
.then((data) => { .then((data) => {
return res.status(200).send(data) return res.status(200).send(data)
}) })
@ -162,55 +131,63 @@ router.get('/get/xmpp/:xmppid/:xmppdata', async (req, res) => {
}) })
}) })
router.get('/get/twitter/:tweetid', async (req, res) => { // Twitter route
if (!twitter_enabled) { router.get('/get/twitter', async (req, res) => {
if (!opts.claims.twitter.bearerToken) {
return res.status(501).json({ error: 'Twitter not enabled on server' }) return res.status(501).json({ error: 'Twitter not enabled on server' })
} }
if (!req.query.tweetId) {
return res.status(400).json({ error: 'Missing parameter(s)' })
}
fetcher fetcher
.twitter(req.params.tweetid, { .twitter(req.query, opts)
bearerToken: twitter_bearer_token,
})
.then((data) => { .then((data) => {
return res.status(200).send(data) return res.status(200).send(data)
}) })
.catch((err) => { .catch((err) => {
return res.status(400).json({ error: err }) return res.status(400).json({ error: err.message ? err.message : err })
}) })
}) })
// Matrix route
router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => { router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => {
if (!matrix_enabled) { if (!opts.claims.matrix.instance || !opts.claims.matrix.accessToken) {
return res.status(501).json({ error: 'Matrix not enabled on server' }) return res.status(501).json({ error: 'Matrix not enabled on server' })
} }
fetcher if (!req.query.id || !req.query.field) {
.matrix(req.params.matrixroomid, req.params.matrixeventid, { return res.status(400).json({ error: 'Missing parameter(s)' })
instance: process.env.MATRIX_INSTANCE,
accessToken: process.env.MATRIX_ACCESS_TOKEN,
})
.then((data) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err })
})
})
router.get('/get/irc/:ircserver/:ircnick', async (req, res) => {
if (!irc_enabled) {
return res.status(501).json({ error: 'IRC not enabled on server' })
} }
fetcher fetcher
.irc(req.params.ircserver, req.params.ircnick, { .matrix(req.params, opts)
nick: 'doipver148927',
})
.then((data) => { .then((data) => {
return res.status(200).send(data) return res.status(200).send(data)
}) })
.catch((err) => { .catch((err) => {
return res.status(400).json({ error: err }) return res.status(400).json({ error: err.message ? err.message : err })
})
})
// IRC route
router.get('/get/irc/:ircserver/:ircnick', async (req, res) => {
if (!opts.claims.irc.nick) {
return res.status(501).json({ error: 'IRC not enabled on server' })
}
if (!req.query.nick) {
return res.status(400).json({ error: 'Missing parameter(s)' })
}
fetcher
.irc(req.params, opts)
.then((data) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err.message ? err.message : err })
}) })
}) })