Now with working bad bot detection

ref #2
This commit is contained in:
Nicolai Ort 2021-09-25 17:27:41 +02:00
parent e5b8557e4c
commit 75adbf73cf
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
1 changed files with 5 additions and 3 deletions

View File

@ -112,6 +112,7 @@ fastify.get('/:shortcode', async (req, res) => {
return 404 return 404
} }
console.log({ua: req.headers['user-agent'], bot: isBot(req.headers['user-agent']), prev: target[0].no_preview})
if(isBot(req.headers['user-agent']) && target[0].no_preview){ if(isBot(req.headers['user-agent']) && target[0].no_preview){
return "Bad Bot!" return "Bad Bot!"
} }
@ -156,7 +157,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);
@ -165,7 +166,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();
@ -187,7 +189,7 @@ 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}`,