2022-10-18 09:37:05 +02:00
|
|
|
/* eslint-disable no-console */
|
2022-11-09 07:34:28 +01:00
|
|
|
// const { Resolver } = require('dns').promises;
|
2022-11-09 07:25:08 +01:00
|
|
|
// import isbot from 'isbot';
|
2022-08-29 17:31:16 +02:00
|
|
|
import { secret, uuid } from 'lib/crypto';
|
2022-11-09 07:34:28 +01:00
|
|
|
import { useCors, useSession } from 'lib/middleware';
|
|
|
|
import { getJsonBody } from 'lib/request';
|
|
|
|
import { badRequest, createToken, send } from 'next-basics';
|
|
|
|
import { saveEvent, savePageView } from 'queries';
|
2020-07-17 10:03:38 +02:00
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-10-09 19:48:47 +02:00
|
|
|
await useCors(req, res);
|
|
|
|
|
2022-11-09 07:25:08 +01:00
|
|
|
// if (isbot(req.headers['user-agent']) && !process.env.DISABLE_BOT_CHECK) {
|
|
|
|
// return unauthorized(res);
|
|
|
|
// }
|
2020-09-11 05:37:07 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// const ignoreIps = process.env.IGNORE_IP;
|
|
|
|
// const ignoreHostnames = process.env.IGNORE_HOSTNAME;
|
2022-06-27 10:46:21 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// if (ignoreIps || ignoreHostnames) {
|
|
|
|
// const ips = [];
|
2022-06-27 10:46:21 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// if (ignoreIps) {
|
|
|
|
// ips.push(...ignoreIps.split(',').map(n => n.trim()));
|
|
|
|
// }
|
2022-06-27 10:46:21 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// if (ignoreHostnames) {
|
|
|
|
// const resolver = new Resolver();
|
|
|
|
// const promises = ignoreHostnames
|
|
|
|
// .split(',')
|
|
|
|
// .map(n => resolver.resolve4(n.trim()).catch(() => {}));
|
2022-06-27 10:46:21 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// await Promise.all(promises).then(resolvedIps => {
|
|
|
|
// ips.push(...resolvedIps.filter(n => n).flatMap(n => n));
|
|
|
|
// });
|
|
|
|
// }
|
2022-06-27 10:46:21 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// const clientIp = getIpAddress(req);
|
2022-06-27 10:46:21 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// const blocked = ips.find(ip => {
|
|
|
|
// console.log('collect.js', ip, clientIp);
|
|
|
|
// if (ip === clientIp) return true;
|
2020-10-04 04:07:56 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// // CIDR notation
|
|
|
|
// if (ip.indexOf('/') > 0) {
|
|
|
|
// const addr = ipaddr.parse(clientIp);
|
|
|
|
// const range = ipaddr.parseCIDR(ip);
|
2021-04-26 08:57:49 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// if (addr.kind() === range[0].kind() && addr.match(range)) return true;
|
|
|
|
// }
|
2021-04-26 08:57:49 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// return false;
|
|
|
|
// });
|
2021-04-26 08:57:49 +02:00
|
|
|
|
2022-11-09 07:34:28 +01:00
|
|
|
// if (blocked) {
|
|
|
|
// return forbidden(res);
|
|
|
|
// }
|
|
|
|
// }
|
2020-10-04 04:07:56 +02:00
|
|
|
|
2020-07-28 08:52:14 +02:00
|
|
|
await useSession(req, res);
|
2020-07-18 19:36:46 +02:00
|
|
|
|
2020-08-21 04:17:27 +02:00
|
|
|
const {
|
2022-07-22 23:43:19 +02:00
|
|
|
session: { website_id, session_id, session_uuid },
|
2020-08-21 04:17:27 +02:00
|
|
|
} = req;
|
2020-07-17 10:03:38 +02:00
|
|
|
|
2022-03-11 04:01:33 +01:00
|
|
|
const { type, payload } = getJsonBody(req);
|
|
|
|
|
2022-07-30 07:30:09 +02:00
|
|
|
let { url, referrer, event_name, event_data } = payload;
|
2022-01-26 06:23:40 +01:00
|
|
|
|
|
|
|
if (process.env.REMOVE_TRAILING_SLASH) {
|
2022-08-29 05:20:54 +02:00
|
|
|
url = url.replace(/\/$/, '');
|
2022-01-26 06:23:40 +01:00
|
|
|
}
|
2020-07-24 04:56:55 +02:00
|
|
|
|
2022-08-22 21:24:57 +02:00
|
|
|
const event_uuid = uuid();
|
2022-08-05 05:15:21 +02:00
|
|
|
|
2022-01-26 06:23:40 +01:00
|
|
|
if (type === 'pageview') {
|
2022-07-22 23:43:19 +02:00
|
|
|
await savePageView(website_id, { session_id, session_uuid, url, referrer });
|
2020-07-20 10:54:21 +02:00
|
|
|
} else if (type === 'event') {
|
2022-08-05 05:15:21 +02:00
|
|
|
await saveEvent(website_id, {
|
|
|
|
event_uuid,
|
|
|
|
session_id,
|
|
|
|
session_uuid,
|
|
|
|
url,
|
|
|
|
event_name,
|
|
|
|
event_data,
|
|
|
|
});
|
2020-08-08 02:19:42 +02:00
|
|
|
} else {
|
|
|
|
return badRequest(res);
|
2020-07-24 04:56:55 +02:00
|
|
|
}
|
2020-07-23 05:45:09 +02:00
|
|
|
|
2022-08-29 17:31:16 +02:00
|
|
|
const token = createToken({ website_id, session_id, session_uuid }, secret());
|
2020-10-03 05:33:46 +02:00
|
|
|
|
2022-03-11 05:39:11 +01:00
|
|
|
return send(res, token);
|
2020-07-17 10:03:38 +02:00
|
|
|
};
|