linkylinky/src/server.js

357 lines
10 KiB
JavaScript
Raw Normal View History

2021-08-12 17:10:36 +00:00
const fastify = require('fastify')({ logger: true })
2021-08-12 18:17:10 +00:00
var uniqid = require('uniqid');
2021-08-14 08:15:59 +00:00
require('dotenv').config();
const argon2 = require('argon2');
2021-08-12 18:17:10 +00:00
2021-08-12 18:24:26 +00:00
let config = {
domain: process.env.DOMAIN || "localhost:3000",
2021-08-12 18:26:33 +00:00
https: (process.env.SSL === 'true') || false,
env: process.env.NODE_ENV || 'development',
2021-08-14 08:26:44 +00:00
recognizeProviders: !(process.env.DISABLE_PROVIDERS === 'true'),
registrationEnabled: (process.env.ENABLE_REGISTER === 'true'),
2021-08-12 19:44:58 +00:00
getBaseUrl() {
if (config.https) {
2021-08-12 18:24:26 +00:00
return `https://${config.domain}`;
}
return `http://${config.domain}`;
}
}
const knexConfiguration = require('../knexfile')[config.env];
2021-08-14 11:23:11 +00:00
const knex = require('knex')(knexConfiguration);
2021-08-12 17:10:36 +00:00
2021-08-14 08:09:05 +00:00
const authenticate = { realm: 'Short' }
fastify.register(require('fastify-auth'))
2021-08-14 08:09:05 +00:00
fastify.register(require('fastify-basic-auth'), { validate, authenticate });
fastify.register(require('fastify-cors'), {
2021-08-14 12:19:08 +00:00
origin: true,
preflight: true,
preflightContinue: true
})
2021-08-12 17:10:36 +00:00
2021-08-12 17:22:14 +00:00
//Automagic Amazn redirects on /a/
fastify.get('/a/:id', async (req, res) => {
2021-08-12 17:10:36 +00:00
res.redirect(302, `https://amazon.de/dp/${req.params.id}`)
})
2021-08-12 17:22:14 +00:00
//Automagic Youtube redirects on /yt/
fastify.get('/yt/:id', async (req, res) => {
res.redirect(302, `https://youtu.be/${req.params.id}`)
2021-08-12 17:10:36 +00:00
})
//Automagic Youtube Playlist redirects on /ytpl/
2021-08-12 19:44:58 +00:00
fastify.get('/ytpl/:id', async (req, res) => {
res.redirect(302, `https://youtube.com/playlist?list=${req.params.id}`)
})
2021-08-12 17:10:36 +00:00
2021-08-14 13:47:58 +00:00
//Automagic ebay item redirects on /e/
fastify.get('/e/:id', async (req, res) => {
res.redirect(302, `https://ebay.de/itm/${req.params.id}`)
})
//Normal shorturls
2021-08-12 18:13:46 +00:00
fastify.get('/:shortcode', async (req, res) => {
const shortcode = req.params.shortcode;
2021-08-12 19:21:23 +00:00
//This should never happen but better safe than 500
2021-08-12 18:13:46 +00:00
if (!shortcode) {
2021-08-12 17:22:14 +00:00
return 404;
}
2021-08-12 18:13:46 +00:00
const target = await knex.select('target')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
if (!target[0]) {
return 404
}
2021-08-14 07:23:55 +00:00
res.redirect(302, target[0].target);
await knex('visits').insert({ shortcode });
2021-08-12 17:22:14 +00:00
})
2021-08-12 17:10:36 +00:00
2021-08-12 19:21:23 +00:00
//Create new url schema
2021-08-12 17:39:10 +00:00
const newUrlSchema = {
body: {
type: 'object',
properties: {
target: { type: 'string' },
shortcode: { type: 'string' },
}
}
};
//Create new url api route
fastify.post('/api', { newUrlSchema }, async (req, res) => {
const target = req.body?.target;
2021-08-12 17:34:36 +00:00
let shortcode = req.body?.shortcode;
2021-08-12 19:21:23 +00:00
//Check if the user provided a target
2021-08-12 17:39:10 +00:00
if (!target) {
res.statusCode = 400;
return "Missing target";
}
2021-08-12 18:13:46 +00:00
2021-08-12 19:21:23 +00:00
/**
* If no custom shortcode is provided: Check if a code for the target already exists.
* If it exists: No new get's generated => The existing code get's used.
* If it doesn't exist: Generate a new code and proceed to creating a new db entry.
*/
if (!shortcode) {
2021-08-12 19:44:58 +00:00
if (config.recognizeProviders) {
2021-08-12 19:23:40 +00:00
const response = checkKnownProviders(target);
2021-08-12 19:44:58 +00:00
if (response) {
2021-08-12 19:23:40 +00:00
return response;
}
}
2021-08-12 18:13:46 +00:00
const exists = await knex.select('shortcode')
.from('urls')
.where('target', '=', target)
.limit(1);
if (exists.length != 0) {
shortcode = exists[0].shortcode;
return {
2021-08-12 18:24:26 +00:00
url: `${config.getBaseUrl()}/${shortcode}`,
2021-08-12 18:13:46 +00:00
shortcode,
target
}
}
2021-08-12 18:17:10 +00:00
shortcode = uniqid();
}
2021-08-12 19:21:23 +00:00
/**
* If a custom shortcode is provided: Check for collisions.
* Collision detected: Warn user
* No collision: Proceed to db entry creation
*/
2021-08-12 17:39:10 +00:00
else {
2021-08-12 18:13:46 +00:00
const exists = await knex.select('shortcode')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
if (exists.length != 0) {
res.statusCode = 400;
return "Shortcode already exists, please choose another code";
}
}
2021-08-12 19:21:23 +00:00
//Create a new db entry
2021-08-12 19:44:58 +00:00
await knex('urls').insert({ target, shortcode });
2021-08-12 18:13:46 +00:00
return {
2021-08-12 18:24:26 +00:00
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target
2021-08-12 18:13:46 +00:00
}
2021-08-12 19:23:40 +00:00
});
2021-08-18 13:21:36 +00:00
//Get stats api route
fastify.get('/api/stats', async (req, res) => {
const urls = await knex.select('shortcode')
.from('urls');
const visits = await knex.select('timestamp')
.from('visits');
return {
urls: urls.length,
visits: visits.length,
}
});
//Get url api route
fastify.get('/api/:shortcode', async (req, res) => {
const shortcode = req.params.shortcode;
//This should never happen but better safe than 500
if (!shortcode) {
return 404;
}
const exists = await knex.select('shortcode', 'target')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
if (exists.length == 0) {
return 404;
}
2021-08-14 07:27:59 +00:00
const visits = await knex.select('timestamp')
.from('visits')
.where('shortcode', '=', shortcode);
return {
url: `${config.getBaseUrl()}/${exists[0].shortcode}`,
shortcode: exists[0].shortcode,
2021-08-14 07:27:59 +00:00
target: exists[0].target,
visits: visits.length
}
});
2021-08-14 07:28:10 +00:00
2021-08-18 13:21:36 +00:00
2021-08-14 08:26:44 +00:00
//User registration
fastify.post('/api/register', async (req, res) => {
if (!config.registrationEnabled) {
res.statusCode = 400;
return "Registration was disabled by your admin";
}
const username = req.body?.username;
let password = req.body?.password;
//Check
if (!username || !password) {
res.statusCode = 400;
return "Missing username or password";
}
const exists = await knex.select('username')
.from('users')
.where('username', '=', username)
.limit(1);
if (exists.length != 0) {
res.statusCode = 400;
return "User already exists";
}
password = await argon2.hash(password);
//Create a new db entry
await knex('users').insert({ username, password });
return "Done!"
});
2021-08-16 13:19:04 +00:00
//Anything in here has some kind of auth
2021-08-14 08:09:05 +00:00
fastify.after(() => {
//Get url api route
fastify.get('/api/:shortcode/visits', { onRequest: fastify.auth([fastify.basicAuth]) }, async (req, res) => {
2021-08-14 08:09:05 +00:00
const shortcode = req.params.shortcode;
2021-08-14 07:28:10 +00:00
2021-08-14 08:09:05 +00:00
//This should never happen but better safe than 500
if (!shortcode) {
return 404;
}
2021-08-14 07:28:10 +00:00
2021-08-14 08:09:05 +00:00
const exists = await knex.select('shortcode', 'target')
.from('urls')
.where('shortcode', '=', shortcode)
.limit(1);
if (exists.length == 0) {
return 404;
}
2021-08-14 07:28:10 +00:00
2021-08-14 08:09:05 +00:00
const visits = await knex.select('timestamp')
.from('visits')
.where('shortcode', '=', shortcode);
2021-08-14 07:28:10 +00:00
2021-08-14 08:09:05 +00:00
return visits;
});
2021-08-14 08:09:05 +00:00
//Get url api route
fastify.delete('/api/:shortcode', { onRequest: fastify.auth([fastify.basicAuth]) }, async (req, res) => {
2021-08-14 08:09:05 +00:00
const shortcode = req.params.shortcode;
2021-08-14 07:30:16 +00:00
2021-08-14 08:09:05 +00:00
//This should never happen but better safe than 500
if (!shortcode) {
return 404;
}
2021-08-14 07:30:16 +00:00
2021-08-14 08:09:05 +00:00
await knex('urls')
.where('shortcode', '=', shortcode)
.delete();
res.statusCode = 204;
return true;
});
2021-08-14 07:30:16 +00:00
2021-08-14 09:50:29 +00:00
//Get all urls api route
fastify.get('/api', { onRequest: fastify.auth([fastify.basicAuth]) }, async (req, res) => {
2021-08-14 09:50:29 +00:00
urls = await knex.select('target', 'shortcode')
.from('urls');
for (let url of urls) {
url.url = `${config.getBaseUrl()}/${url.shortcode}`
if (req.query.showVisits) {
2021-08-14 09:50:29 +00:00
url.visits = (await knex.select('timestamp')
.from('visits')
.where('shortcode', '=', url.shortcode)).length;
}
}
return urls;
});
2021-08-14 07:30:16 +00:00
});
2021-08-14 08:09:05 +00:00
2021-08-12 19:23:40 +00:00
/**
* Checks for some default providers with custom url schemes (amazon and youtube r/n)
* @param {string} target The target URL
* @returns Standard shortening response if provider recognized or null
*/
2021-08-12 19:44:58 +00:00
function checkKnownProviders(target) {
target = decodeURIComponent(target);
2021-08-12 19:44:58 +00:00
const youtubeVideoID = target.match(/(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/)
if (youtubeVideoID) {
const shortcode = `yt/${youtubeVideoID[1]}`
return {
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target
}
}
const youtubePlaylistID = target.match(/(?:youtube(?:-nocookie)?\.com\/)playlist\?(?:.*)list=([a-zA-Z0-9_-]*)(?:&.*|)/)
if (youtubePlaylistID) {
const shortcode = `ytpl/${youtubePlaylistID[1]}`
2021-08-12 19:23:40 +00:00
return {
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target
}
}
const amazonID = target.match(/(?:https?:\/\/|)(www|smile|)\.?(amazon|smile)\.(de)(?:(?:\/.*\/|\/)(?:dp|gp))(\/product\/|\/)([A-Z0-9]+)/);
2021-08-12 19:44:58 +00:00
if (amazonID) {
const shortcode = `a/${amazonID[5]}`
return {
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target
}
}
2021-08-14 13:47:03 +00:00
const ebayID = target.match(/(?:[ebay]*(?:[\/]|[itm=])|^)([0-9]{9,12})/);
if (ebayID) {
const shortcode = `e/${ebayID[1]}`
return {
url: `${config.getBaseUrl()}/${shortcode}`,
shortcode,
target
}
}
return null;
2021-08-12 19:23:40 +00:00
}
2021-08-12 17:10:36 +00:00
2021-08-14 08:09:05 +00:00
async function validate(username, password, req, reply) {
2021-08-14 08:15:59 +00:00
if (!username || !password) {
2021-08-14 08:09:05 +00:00
return new Error('Sorry only authorized users can do that.')
}
2021-08-14 08:15:59 +00:00
2021-08-14 08:26:44 +00:00
const user = await knex.select('username', 'password')
2021-08-14 08:15:59 +00:00
.from('users')
2021-08-14 08:26:44 +00:00
.where('username', '=', username)
2021-08-14 08:15:59 +00:00
.limit(1);
if (user.length == 0) {
return new Error('Sorry m8, looks like you are not on the inivtation list');
}
2021-08-14 08:26:44 +00:00
if (!(await argon2.verify(user[0].password, password))) {
2021-08-14 08:15:59 +00:00
return new Error('Wrong credentials');
}
2021-08-14 08:09:05 +00:00
}
2021-08-12 17:10:36 +00:00
// Run the server!
const start = async () => {
2021-08-14 07:56:43 +00:00
try {
await fastify.listen(3000, '0.0.0.0')
2021-08-12 17:22:14 +00:00
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
2021-08-12 17:10:36 +00:00
}
start()