remove is ignoreIps
parent
a9c32ecca9
commit
85be465d67
|
@ -1,12 +1,11 @@
|
|||
/* eslint-disable no-console */
|
||||
const { Resolver } = require('dns').promises;
|
||||
// const { Resolver } = require('dns').promises;
|
||||
// import isbot from 'isbot';
|
||||
import ipaddr from 'ipaddr.js';
|
||||
import { createToken, send, badRequest, forbidden } from 'next-basics';
|
||||
import { savePageView, saveEvent } from 'queries';
|
||||
import { useCors, useSession } from 'lib/middleware';
|
||||
import { getJsonBody, getIpAddress } from 'lib/request';
|
||||
import { secret, uuid } from 'lib/crypto';
|
||||
import { useCors, useSession } from 'lib/middleware';
|
||||
import { getJsonBody } from 'lib/request';
|
||||
import { badRequest, createToken, send } from 'next-basics';
|
||||
import { saveEvent, savePageView } from 'queries';
|
||||
|
||||
export default async (req, res) => {
|
||||
await useCors(req, res);
|
||||
|
@ -15,48 +14,48 @@ export default async (req, res) => {
|
|||
// return unauthorized(res);
|
||||
// }
|
||||
|
||||
const ignoreIps = process.env.IGNORE_IP;
|
||||
const ignoreHostnames = process.env.IGNORE_HOSTNAME;
|
||||
// const ignoreIps = process.env.IGNORE_IP;
|
||||
// const ignoreHostnames = process.env.IGNORE_HOSTNAME;
|
||||
|
||||
if (ignoreIps || ignoreHostnames) {
|
||||
const ips = [];
|
||||
// if (ignoreIps || ignoreHostnames) {
|
||||
// const ips = [];
|
||||
|
||||
if (ignoreIps) {
|
||||
ips.push(...ignoreIps.split(',').map(n => n.trim()));
|
||||
}
|
||||
// if (ignoreIps) {
|
||||
// ips.push(...ignoreIps.split(',').map(n => n.trim()));
|
||||
// }
|
||||
|
||||
if (ignoreHostnames) {
|
||||
const resolver = new Resolver();
|
||||
const promises = ignoreHostnames
|
||||
.split(',')
|
||||
.map(n => resolver.resolve4(n.trim()).catch(() => {}));
|
||||
// if (ignoreHostnames) {
|
||||
// const resolver = new Resolver();
|
||||
// const promises = ignoreHostnames
|
||||
// .split(',')
|
||||
// .map(n => resolver.resolve4(n.trim()).catch(() => {}));
|
||||
|
||||
await Promise.all(promises).then(resolvedIps => {
|
||||
ips.push(...resolvedIps.filter(n => n).flatMap(n => n));
|
||||
});
|
||||
}
|
||||
// await Promise.all(promises).then(resolvedIps => {
|
||||
// ips.push(...resolvedIps.filter(n => n).flatMap(n => n));
|
||||
// });
|
||||
// }
|
||||
|
||||
const clientIp = getIpAddress(req);
|
||||
// const clientIp = getIpAddress(req);
|
||||
|
||||
const blocked = ips.find(ip => {
|
||||
console.log('collect.js', ip, clientIp);
|
||||
if (ip === clientIp) return true;
|
||||
// const blocked = ips.find(ip => {
|
||||
// console.log('collect.js', ip, clientIp);
|
||||
// if (ip === clientIp) return true;
|
||||
|
||||
// CIDR notation
|
||||
if (ip.indexOf('/') > 0) {
|
||||
const addr = ipaddr.parse(clientIp);
|
||||
const range = ipaddr.parseCIDR(ip);
|
||||
// // CIDR notation
|
||||
// if (ip.indexOf('/') > 0) {
|
||||
// const addr = ipaddr.parse(clientIp);
|
||||
// const range = ipaddr.parseCIDR(ip);
|
||||
|
||||
if (addr.kind() === range[0].kind() && addr.match(range)) return true;
|
||||
}
|
||||
// if (addr.kind() === range[0].kind() && addr.match(range)) return true;
|
||||
// }
|
||||
|
||||
return false;
|
||||
});
|
||||
// return false;
|
||||
// });
|
||||
|
||||
if (blocked) {
|
||||
return forbidden(res);
|
||||
}
|
||||
}
|
||||
// if (blocked) {
|
||||
// return forbidden(res);
|
||||
// }
|
||||
// }
|
||||
|
||||
await useSession(req, res);
|
||||
|
||||
|
|
Loading…
Reference in New Issue