2022-10-06 22:35:38 +02:00
|
|
|
import isbot from 'isbot';
|
|
|
|
import ipaddr from 'ipaddr.js';
|
2023-02-08 01:29:25 +01:00
|
|
|
import { createToken, ok, send, badRequest, forbidden } from 'next-basics';
|
2023-03-26 13:15:08 +02:00
|
|
|
import { saveEvent } from 'queries';
|
2022-10-06 22:35:38 +02:00
|
|
|
import { useCors, useSession } from 'lib/middleware';
|
2022-12-28 05:20:44 +01:00
|
|
|
import { getJsonBody, getIpAddress } from 'lib/detect';
|
2022-11-08 07:35:51 +01:00
|
|
|
import { secret } from 'lib/crypto';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
2023-02-08 01:29:25 +01:00
|
|
|
import { Resolver } from 'dns/promises';
|
2022-11-15 22:21:14 +01:00
|
|
|
|
|
|
|
export interface NextApiRequestCollect extends NextApiRequest {
|
|
|
|
session: {
|
|
|
|
id: string;
|
|
|
|
websiteId: string;
|
|
|
|
hostname: string;
|
|
|
|
browser: string;
|
|
|
|
os: string;
|
|
|
|
device: string;
|
|
|
|
screen: string;
|
|
|
|
language: string;
|
|
|
|
country: string;
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1: string;
|
|
|
|
subdivision2: string;
|
|
|
|
city: string;
|
2022-11-15 22:21:14 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
2022-10-06 22:35:38 +02:00
|
|
|
await useCors(req, res);
|
|
|
|
|
|
|
|
if (isbot(req.headers['user-agent']) && !process.env.DISABLE_BOT_CHECK) {
|
2023-02-08 01:29:25 +01:00
|
|
|
return ok(res);
|
2022-10-06 22:35:38 +02:00
|
|
|
}
|
|
|
|
|
2022-11-23 00:06:52 +01:00
|
|
|
const { type, payload } = getJsonBody(req);
|
|
|
|
|
2023-03-26 13:15:08 +02:00
|
|
|
if (type !== 'event') {
|
|
|
|
return badRequest(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { url, referrer, name: eventName, data: eventData, title: pageTitle } = payload;
|
2023-03-23 22:01:15 +01:00
|
|
|
|
|
|
|
// Validate eventData is JSON
|
|
|
|
if (eventData && !(typeof eventData === 'object' && !Array.isArray(eventData))) {
|
2023-03-26 13:15:08 +02:00
|
|
|
return badRequest(res, 'Invalid event data.');
|
2023-03-23 22:01:15 +01:00
|
|
|
}
|
|
|
|
|
2022-10-06 22:35:38 +02:00
|
|
|
const ignoreIps = process.env.IGNORE_IP;
|
|
|
|
const ignoreHostnames = process.env.IGNORE_HOSTNAME;
|
|
|
|
|
|
|
|
if (ignoreIps || ignoreHostnames) {
|
|
|
|
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 => {
|
2023-02-08 01:29:25 +01:00
|
|
|
ips.push(...resolvedIps.filter(n => n).flatMap(n => n as string[]));
|
2022-10-06 22:35:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientIp = getIpAddress(req);
|
|
|
|
|
|
|
|
const blocked = ips.find(ip => {
|
|
|
|
if (ip === clientIp) return true;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (blocked) {
|
|
|
|
return forbidden(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await useSession(req, res);
|
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
const session = req.session;
|
2022-10-06 22:35:38 +02:00
|
|
|
|
2023-03-26 13:15:08 +02:00
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
let [urlPath, urlQuery] = url?.split('?') || [];
|
|
|
|
let [referrerPath, referrerQuery] = referrer?.split('?') || [];
|
2023-03-16 00:06:51 +01:00
|
|
|
let referrerDomain;
|
|
|
|
|
2023-03-30 08:14:09 +02:00
|
|
|
if (!urlPath) {
|
|
|
|
urlPath = '/';
|
|
|
|
}
|
|
|
|
|
2023-03-26 13:15:08 +02:00
|
|
|
if (referrerPath.startsWith('http')) {
|
|
|
|
const refUrl = new URL(referrer);
|
|
|
|
referrerPath = refUrl.pathname;
|
|
|
|
referrerQuery = refUrl.search.substring(1);
|
|
|
|
referrerDomain = refUrl.hostname;
|
2023-03-16 00:06:51 +01:00
|
|
|
}
|
|
|
|
|
2022-10-06 22:35:38 +02:00
|
|
|
if (process.env.REMOVE_TRAILING_SLASH) {
|
2023-03-30 08:14:09 +02:00
|
|
|
urlPath = urlPath.replace(/.+\/$/, '');
|
2022-10-06 22:35:38 +02:00
|
|
|
}
|
|
|
|
|
2023-03-26 13:15:08 +02:00
|
|
|
await saveEvent({
|
|
|
|
urlPath,
|
|
|
|
urlQuery,
|
|
|
|
referrerPath,
|
|
|
|
referrerQuery,
|
|
|
|
referrerDomain,
|
|
|
|
pageTitle,
|
|
|
|
eventName,
|
|
|
|
eventData,
|
|
|
|
...session,
|
|
|
|
sessionId: session.id,
|
|
|
|
});
|
2022-10-06 22:35:38 +02:00
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
const token = createToken(session, secret());
|
2022-10-06 22:35:38 +02:00
|
|
|
|
|
|
|
return send(res, token);
|
2020-07-17 10:03:38 +02:00
|
|
|
};
|