|
|
@@ -1,5 +1,7 @@
|
|
|
const urlRegex = require('url-regex');
|
|
|
const URL = require('url');
|
|
|
+const dns = require('dns');
|
|
|
+const { promisify } = require('util');
|
|
|
const generate = require('nanoid/generate');
|
|
|
const useragent = require('useragent');
|
|
|
const geoip = require('geoip-lite');
|
|
|
@@ -17,8 +19,13 @@ const {
|
|
|
getUrls,
|
|
|
setCustomDomain,
|
|
|
urlCountFromDate,
|
|
|
+ banUrl,
|
|
|
+ getBannedDomain,
|
|
|
+ getBannedHost,
|
|
|
} = require('../db/url');
|
|
|
|
|
|
+const dnsLookup = promisify(dns.lookup);
|
|
|
+
|
|
|
const { addProtocol, generateShortUrl } = require('../utils');
|
|
|
const config = require('../config');
|
|
|
|
|
|
@@ -72,6 +79,22 @@ exports.urlShortener = async ({ body, user }, res) => {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // If domain or host is banned
|
|
|
+ const domain = URL.parse(body.target).hostname;
|
|
|
+ const isDomainBanned = await getBannedDomain(domain);
|
|
|
+
|
|
|
+ let isHostBanned;
|
|
|
+ try {
|
|
|
+ const dnsRes = await dnsLookup(domain);
|
|
|
+ isHostBanned = await getBannedHost(dnsRes && dnsRes.address);
|
|
|
+ } catch (error) {
|
|
|
+ isHostBanned = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isDomainBanned || isHostBanned) {
|
|
|
+ return res.status(400).json({ error: 'URL is containing malware/scam.' });
|
|
|
+ }
|
|
|
+
|
|
|
// Create new URL
|
|
|
const id = (user && body.customurl) || (await generateId());
|
|
|
const target = addProtocol(body.target);
|
|
|
@@ -104,12 +127,20 @@ exports.goToUrl = async (req, res, next) => {
|
|
|
botList.some(bot => agent.source.toLowerCase().includes(bot)) || agent.family === 'Other';
|
|
|
if (!urls && !urls.length) return next();
|
|
|
const url = urls.find(item => (domain ? item.domain === domain : !item.domain));
|
|
|
+
|
|
|
+ if (!url) return next();
|
|
|
+
|
|
|
+ if (url.banned) {
|
|
|
+ return res.redirect('/banned');
|
|
|
+ }
|
|
|
+
|
|
|
const doesRequestInfo = /.*\+$/gi.test(reqestedId);
|
|
|
if (doesRequestInfo && !url.password) {
|
|
|
req.urlTarget = url.target;
|
|
|
req.pageType = 'info';
|
|
|
return next();
|
|
|
}
|
|
|
+
|
|
|
if (url.password && !req.body.password) {
|
|
|
req.protectedUrl = id;
|
|
|
req.pageType = 'password';
|
|
|
@@ -194,3 +225,35 @@ exports.getStats = async ({ query: { id, domain }, user }, res) => {
|
|
|
if (!stats) return res.status(400).json({ error: 'Could not get the short URL stats.' });
|
|
|
return res.status(200).json(stats);
|
|
|
};
|
|
|
+
|
|
|
+exports.ban = async ({ body }, res) => {
|
|
|
+ if (!body.id) return res.status(400).json({ error: 'No id has been provided.' });
|
|
|
+
|
|
|
+ const urls = await findUrl({ id: body.id });
|
|
|
+ const [url] = urls.filter(item => !item.domain);
|
|
|
+
|
|
|
+ if (!url) return res.status(400).json({ error: "Couldn't find the URL." });
|
|
|
+
|
|
|
+ if (url.banned) return res.status(200).json({ message: 'URL was banned already' });
|
|
|
+
|
|
|
+ const domain = URL.parse(url.target).hostname;
|
|
|
+
|
|
|
+ let host;
|
|
|
+ if (body.host) {
|
|
|
+ try {
|
|
|
+ const dnsRes = await dnsLookup(domain);
|
|
|
+ host = dnsRes && dnsRes.address;
|
|
|
+ } catch (error) {
|
|
|
+ host = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ await banUrl({
|
|
|
+ domain: body.domain && domain,
|
|
|
+ host,
|
|
|
+ id: body.id,
|
|
|
+ user: body.user,
|
|
|
+ });
|
|
|
+
|
|
|
+ return res.status(200).json({ message: 'URL has been banned successfully' });
|
|
|
+};
|