Skip to content

Commit

Permalink
Removed hostname lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Nov 28, 2023
1 parent 02c9e01 commit e074394
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions src/pages/api/send.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Resolver } from 'dns/promises';
import ipaddr from 'ipaddr.js';
import isbot from 'isbot';
import { COLLECTION_TYPE, HOSTNAME_REGEX } from 'lib/constants';
Expand Down Expand Up @@ -78,14 +77,14 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
return ok(res);
}

const { type, payload } = req.body;

await useValidate(schema, req, res);

if (await hasBlockedIp(req)) {
if (hasBlockedIp(req)) {
return forbidden(res);
}

const { type, payload } = req.body;

const { url, referrer, name: eventName, data: eventData, title: pageTitle } = payload;

await useSession(req, res);
Expand Down Expand Up @@ -143,28 +142,16 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
return methodNotAllowed(res);
};

async function hasBlockedIp(req: NextApiRequestCollect) {
function hasBlockedIp(req: NextApiRequestCollect) {
const ignoreIps = process.env.IGNORE_IP;
const ignoreHostnames = process.env.IGNORE_HOSTNAME;

if (ignoreIps || ignoreHostnames) {
if (ignoreIps) {
const ips = [];

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(() => {}));

await Promise.all(promises).then(resolvedIps => {
ips.push(...resolvedIps.filter(n => n).flatMap(n => n as string[]));
});
}

const clientIp = getIpAddress(req);

return ips.find(ip => {
Expand All @@ -177,8 +164,8 @@ async function hasBlockedIp(req: NextApiRequestCollect) {

if (addr.kind() === range[0].kind() && addr.match(range)) return true;
}

return false;
});
}

return false;
}

0 comments on commit e074394

Please sign in to comment.