Compare commits
9 Commits
f15282a3f9
...
6d71a3ebf4
Author | SHA1 | Date | |
---|---|---|---|
6d71a3ebf4 | |||
75adbf73cf | |||
e5b8557e4c | |||
33d7c94648 | |||
f6b2ae523d | |||
0a500f16cd | |||
61da5d8110 | |||
773b286216 | |||
d097eccbd9 |
10
migrations/20210925171533_bot_preview.js
Normal file
10
migrations/20210925171533_bot_preview.js
Normal 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) {
|
||||
|
||||
};
|
@ -31,6 +31,7 @@
|
||||
"fastify-basic-auth": "^2.1.0",
|
||||
"fastify-cors": "^6.0.2",
|
||||
"fastify-jwt": "^3.0.1",
|
||||
"isbot": "^3.3.3",
|
||||
"knex": "^0.21.21",
|
||||
"sqlite3": "^5.0.2",
|
||||
"uniqid": "^5.3.0"
|
||||
|
@ -2,6 +2,7 @@ const fastify = require('fastify')({ logger: true })
|
||||
var uniqid = require('uniqid');
|
||||
require('dotenv').config();
|
||||
const argon2 = require('argon2');
|
||||
const isBot = require('isbot')
|
||||
|
||||
let config = {
|
||||
domain: process.env.DOMAIN || "localhost:3000",
|
||||
@ -74,27 +75,23 @@ fastify.decorate('verifyJWT', function async(request, reply, done) {
|
||||
|
||||
//Automagic Amazn redirects on /a/
|
||||
fastify.get('/a/:id', async (req, res) => {
|
||||
res.header("X-Robots-Tag","noindex, nofollow");
|
||||
res.redirect(302, `https://amazon.de/dp/${req.params.id}`)
|
||||
await knex('visits').insert({ shortcode: req.params.id, provider: 'a' });
|
||||
})
|
||||
|
||||
//Automagic Youtube redirects on /yt/
|
||||
fastify.get('/yt/:id', async (req, res) => {
|
||||
res.header("X-Robots-Tag","noindex, nofollow");
|
||||
res.redirect(302, `https://youtu.be/${req.params.id}`)
|
||||
await knex('visits').insert({ shortcode: req.params.id, provider: 'yt' });
|
||||
})
|
||||
//Automagic Youtube Playlist redirects on /ytpl/
|
||||
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}`)
|
||||
await knex('visits').insert({ shortcode: req.params.id, provider: 'ytpl' });
|
||||
})
|
||||
|
||||
//Automagic ebay item redirects on /e/
|
||||
fastify.get('/e/:id', async (req, res) => {
|
||||
res.header("X-Robots-Tag","noindex, nofollow");
|
||||
res.redirect(302, `https://ebay.de/itm/${req.params.id}`)
|
||||
await knex('visits').insert({ shortcode: req.params.id, provider: 'e' });
|
||||
})
|
||||
@ -102,19 +99,23 @@ fastify.get('/e/:id', async (req, res) => {
|
||||
//Normal shorturls
|
||||
fastify.get('/:shortcode', async (req, res) => {
|
||||
const shortcode = req.params.shortcode;
|
||||
console.log(req.headers['user-agent'])
|
||||
|
||||
//This should never happen but better safe than 500
|
||||
if (!shortcode) {
|
||||
return 404;
|
||||
}
|
||||
const target = await knex.select('target')
|
||||
const target = await knex.select('target', 'no_preview')
|
||||
.from('urls')
|
||||
.where('shortcode', '=', shortcode)
|
||||
.limit(1);
|
||||
if (!target[0]) {
|
||||
return 404
|
||||
}
|
||||
|
||||
if(isBot(req.headers['user-agent']) && target[0].no_preview){
|
||||
return "Bad Bot!"
|
||||
}
|
||||
|
||||
res.redirect(302, target[0].target);
|
||||
await knex('visits').insert({ shortcode, provider: 'native' });
|
||||
})
|
||||
@ -126,6 +127,7 @@ const newUrlSchema = {
|
||||
properties: {
|
||||
target: { type: 'string' },
|
||||
shortcode: { type: 'string' },
|
||||
no_preview: { type: 'boolean' },
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -134,6 +136,7 @@ const newUrlSchema = {
|
||||
fastify.post('/api', { newUrlSchema }, async (req, res) => {
|
||||
const target = req.body?.target;
|
||||
let shortcode = req.body?.shortcode;
|
||||
let no_preview = req.body?.no_preview || false;
|
||||
|
||||
//Check if the user provided a target
|
||||
if (!target) {
|
||||
@ -153,7 +156,7 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
const exists = await knex.select('shortcode')
|
||||
const exists = await knex.select('shortcode', 'no_preview')
|
||||
.from('urls')
|
||||
.where('target', '=', target)
|
||||
.limit(1);
|
||||
@ -162,7 +165,8 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
|
||||
return {
|
||||
url: `${config.getBaseUrl()}/${shortcode}`,
|
||||
shortcode,
|
||||
target
|
||||
target,
|
||||
no_preview: exists[0].no_preview
|
||||
}
|
||||
}
|
||||
shortcode = uniqid();
|
||||
@ -184,12 +188,13 @@ fastify.post('/api', { newUrlSchema }, async (req, res) => {
|
||||
}
|
||||
|
||||
//Create a new db entry
|
||||
await knex('urls').insert({ target, shortcode });
|
||||
await knex('urls').insert({ target, shortcode, no_preview });
|
||||
|
||||
return {
|
||||
url: `${config.getBaseUrl()}/${shortcode}`,
|
||||
shortcode,
|
||||
target
|
||||
target,
|
||||
no_preview
|
||||
}
|
||||
});
|
||||
|
||||
@ -216,7 +221,7 @@ fastify.get('/api/:shortcode', async (req, res) => {
|
||||
return 404;
|
||||
}
|
||||
|
||||
const exists = await knex.select('shortcode', 'target')
|
||||
const exists = await knex.select('shortcode', 'target', 'no_preview')
|
||||
.from('urls')
|
||||
.where('shortcode', '=', shortcode)
|
||||
.limit(1);
|
||||
@ -232,6 +237,7 @@ fastify.get('/api/:shortcode', async (req, res) => {
|
||||
url: `${config.getBaseUrl()}/${exists[0].shortcode}`,
|
||||
shortcode: exists[0].shortcode,
|
||||
target: exists[0].target,
|
||||
no_preview: exists[0].no_preview,
|
||||
visits: visits.length
|
||||
}
|
||||
});
|
||||
@ -318,7 +324,7 @@ fastify.after(() => {
|
||||
|
||||
//Get all urls api route
|
||||
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');
|
||||
|
||||
for (let url of urls) {
|
||||
|
@ -2228,6 +2228,11 @@ isarray@1.0.0, isarray@~1.0.0:
|
||||
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
|
||||
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:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
|
||||
|
Loading…
x
Reference in New Issue
Block a user