Compare commits

...

9 Commits

Author SHA1 Message Date
6d71a3ebf4
Removed unused log
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-25 17:27:59 +02:00
75adbf73cf
Now with working bad bot detection
ref #2
2021-09-25 17:27:41 +02:00
e5b8557e4c
Getters now return the no_preview status 2021-09-25 17:21:30 +02:00
33d7c94648
New urls can now be created with disabled preview
ref #2
2021-09-25 17:19:36 +02:00
f6b2ae523d
Added bot check for native short urls
ref #2
2021-09-25 17:17:42 +02:00
0a500f16cd
Added migration for disallowing bot previews 2021-09-25 17:16:38 +02:00
61da5d8110
Added basic bot checking 2021-09-25 17:14:34 +02:00
773b286216
Added package for bot recognition 2021-09-25 17:12:52 +02:00
d097eccbd9
removed logging 2021-09-25 17:12:22 +02:00
4 changed files with 34 additions and 12 deletions

View File

@ -0,0 +1,10 @@
exports.up = function(knex) {
return knex.schema.table('urls', function (table) {
table.boolean('no_preview').defaultTo(false);
});
};
exports.down = function(knex) {
};

View File

@ -31,6 +31,7 @@
"fastify-basic-auth": "^2.1.0", "fastify-basic-auth": "^2.1.0",
"fastify-cors": "^6.0.2", "fastify-cors": "^6.0.2",
"fastify-jwt": "^3.0.1", "fastify-jwt": "^3.0.1",
"isbot": "^3.3.3",
"knex": "^0.21.21", "knex": "^0.21.21",
"sqlite3": "^5.0.2", "sqlite3": "^5.0.2",
"uniqid": "^5.3.0" "uniqid": "^5.3.0"

View File

@ -2,6 +2,7 @@ const fastify = require('fastify')({ logger: true })
var uniqid = require('uniqid'); var uniqid = require('uniqid');
require('dotenv').config(); require('dotenv').config();
const argon2 = require('argon2'); const argon2 = require('argon2');
const isBot = require('isbot')
let config = { let config = {
domain: process.env.DOMAIN || "localhost:3000", domain: process.env.DOMAIN || "localhost:3000",
@ -74,27 +75,23 @@ fastify.decorate('verifyJWT', function async(request, reply, done) {
//Automagic Amazn redirects on /a/ //Automagic Amazn redirects on /a/
fastify.get('/a/:id', async (req, res) => { fastify.get('/a/:id', async (req, res) => {
res.header("X-Robots-Tag","noindex, nofollow");
res.redirect(302, `https://amazon.de/dp/${req.params.id}`) res.redirect(302, `https://amazon.de/dp/${req.params.id}`)
await knex('visits').insert({ shortcode: req.params.id, provider: 'a' }); await knex('visits').insert({ shortcode: req.params.id, provider: 'a' });
}) })
//Automagic Youtube redirects on /yt/ //Automagic Youtube redirects on /yt/
fastify.get('/yt/:id', async (req, res) => { fastify.get('/yt/:id', async (req, res) => {
res.header("X-Robots-Tag","noindex, nofollow");
res.redirect(302, `https://youtu.be/${req.params.id}`) res.redirect(302, `https://youtu.be/${req.params.id}`)
await knex('visits').insert({ shortcode: req.params.id, provider: 'yt' }); await knex('visits').insert({ shortcode: req.params.id, provider: 'yt' });
}) })
//Automagic Youtube Playlist redirects on /ytpl/ //Automagic Youtube Playlist redirects on /ytpl/
fastify.get('/ytpl/:id', async (req, res) => { fastify.get('/ytpl/:id', async (req, res) => {
res.header("X-Robots-Tag","noindex, nofollow");
res.redirect(302, `https://youtube.com/playlist?list=${req.params.id}`) res.redirect(302, `https://youtube.com/playlist?list=${req.params.id}`)
await knex('visits').insert({ shortcode: req.params.id, provider: 'ytpl' }); await knex('visits').insert({ shortcode: req.params.id, provider: 'ytpl' });
}) })
//Automagic ebay item redirects on /e/ //Automagic ebay item redirects on /e/
fastify.get('/e/:id', async (req, res) => { fastify.get('/e/:id', async (req, res) => {
res.header("X-Robots-Tag","noindex, nofollow");
res.redirect(302, `https://ebay.de/itm/${req.params.id}`) res.redirect(302, `https://ebay.de/itm/${req.params.id}`)
await knex('visits').insert({ shortcode: req.params.id, provider: 'e' }); await knex('visits').insert({ shortcode: req.params.id, provider: 'e' });
}) })
@ -102,19 +99,23 @@ fastify.get('/e/:id', async (req, res) => {
//Normal shorturls //Normal shorturls
fastify.get('/:shortcode', async (req, res) => { fastify.get('/:shortcode', async (req, res) => {
const shortcode = req.params.shortcode; const shortcode = req.params.shortcode;
console.log(req.headers['user-agent'])
//This should never happen but better safe than 500 //This should never happen but better safe than 500
if (!shortcode) { if (!shortcode) {
return 404; return 404;
} }
const target = await knex.select('target') const target = await knex.select('target', 'no_preview')
.from('urls') .from('urls')
.where('shortcode', '=', shortcode) .where('shortcode', '=', shortcode)
.limit(1); .limit(1);
if (!target[0]) { if (!target[0]) {
return 404 return 404
} }
if(isBot(req.headers['user-agent']) && target[0].no_preview){
return "Bad Bot!"
}
res.redirect(302, target[0].target); res.redirect(302, target[0].target);
await knex('visits').insert({ shortcode, provider: 'native' }); await knex('visits').insert({ shortcode, provider: 'native' });
}) })
@ -126,6 +127,7 @@ const newUrlSchema = {
properties: { properties: {
target: { type: 'string' }, target: { type: 'string' },
shortcode: { type: 'string' }, shortcode: { type: 'string' },
no_preview: { type: 'boolean' },
} }
} }
}; };
@ -134,6 +136,7 @@ const newUrlSchema = {
fastify.post('/api', { newUrlSchema }, async (req, res) => { fastify.post('/api', { newUrlSchema }, async (req, res) => {
const target = req.body?.target; const target = req.body?.target;
let shortcode = req.body?.shortcode; let shortcode = req.body?.shortcode;
let no_preview = req.body?.no_preview || false;
//Check if the user provided a target //Check if the user provided a target
if (!target) { if (!target) {
@ -153,7 +156,7 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
return response; return response;
} }
} }
const exists = await knex.select('shortcode') const exists = await knex.select('shortcode', 'no_preview')
.from('urls') .from('urls')
.where('target', '=', target) .where('target', '=', target)
.limit(1); .limit(1);
@ -162,7 +165,8 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
return { return {
url: `${config.getBaseUrl()}/${shortcode}`, url: `${config.getBaseUrl()}/${shortcode}`,
shortcode, shortcode,
target target,
no_preview: exists[0].no_preview
} }
} }
shortcode = uniqid(); shortcode = uniqid();
@ -184,12 +188,13 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
} }
//Create a new db entry //Create a new db entry
await knex('urls').insert({ target, shortcode }); await knex('urls').insert({ target, shortcode, no_preview });
return { return {
url: `${config.getBaseUrl()}/${shortcode}`, url: `${config.getBaseUrl()}/${shortcode}`,
shortcode, shortcode,
target target,
no_preview
} }
}); });
@ -216,7 +221,7 @@ fastify.get('/api/:shortcode', async (req, res) => {
return 404; return 404;
} }
const exists = await knex.select('shortcode', 'target') const exists = await knex.select('shortcode', 'target', 'no_preview')
.from('urls') .from('urls')
.where('shortcode', '=', shortcode) .where('shortcode', '=', shortcode)
.limit(1); .limit(1);
@ -232,6 +237,7 @@ fastify.get('/api/:shortcode', async (req, res) => {
url: `${config.getBaseUrl()}/${exists[0].shortcode}`, url: `${config.getBaseUrl()}/${exists[0].shortcode}`,
shortcode: exists[0].shortcode, shortcode: exists[0].shortcode,
target: exists[0].target, target: exists[0].target,
no_preview: exists[0].no_preview,
visits: visits.length visits: visits.length
} }
}); });
@ -318,7 +324,7 @@ fastify.after(() => {
//Get all urls api route //Get all urls api route
fastify.get('/api', { onRequest: fastify.auth([fastify.basicAuth, fastify.verifyJWT]) }, async (req, res) => { fastify.get('/api', { onRequest: fastify.auth([fastify.basicAuth, fastify.verifyJWT]) }, async (req, res) => {
urls = await knex.select('target', 'shortcode') urls = await knex.select('target', 'shortcode', 'no_preview')
.from('urls'); .from('urls');
for (let url of urls) { for (let url of urls) {

View File

@ -2228,6 +2228,11 @@ isarray@1.0.0, isarray@~1.0.0:
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=
isbot@^3.3.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/isbot/-/isbot-3.3.3.tgz#6a2124a6c1dda5db2d0060c4e292b346ecbd2249"
integrity sha512-a3HFPPsvtLroqpuTHHJTaUpPHUO0vjPbptJDzJYkymRvOI8tugWM6zE2oq22w5VOq4A5hrX+YRS7VdIPAgWLfw==
isexe@^2.0.0: isexe@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10" resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"