Support CIDR notation in IGNORE_IP, closes #544.

This commit is contained in:
Mike Cao 2021-04-25 23:57:49 -07:00
parent 0cf115b2de
commit 92705815ed
3 changed files with 21 additions and 1 deletions

View file

@ -1,4 +1,5 @@
import isbot from 'isbot';
import ipaddr from 'ipaddr.js';
import { savePageView, saveEvent } from 'lib/queries';
import { useCors, useSession } from 'lib/middleware';
import { getIpAddress } from 'lib/request';
@ -15,8 +16,21 @@ export default async (req, res) => {
if (process.env.IGNORE_IP) {
const ips = process.env.IGNORE_IP.split(',').map(n => n.trim());
const ip = getIpAddress(req);
const blocked = ips.find(i => {
if (i === ip) return true;
if (ips.includes(ip)) {
// CIDR notation
if (i.indexOf('/') > 0) {
const addr = ipaddr.parse(ip);
const range = ipaddr.parseCIDR(i);
if (addr.match(range)) return true;
}
return false;
});
if (blocked) {
return ok(res);
}
}