2022-06-27 10:46:21 +02:00
|
|
|
const { Resolver } = require('dns').promises;
|
2021-03-13 07:44:25 +01:00
|
|
|
import isbot from 'isbot';
|
2021-04-26 08:57:49 +02:00
|
|
|
import ipaddr from 'ipaddr.js';
|
2022-07-12 23:14:36 +02:00
|
|
|
import { savePageView, saveEvent } from 'queries';
|
2020-07-28 08:52:14 +02:00
|
|
|
import { useCors, useSession } from 'lib/middleware';
|
2022-03-11 04:01:33 +01:00
|
|
|
import { getJsonBody, getIpAddress } from 'lib/request';
|
2022-06-27 10:46:21 +02:00
|
|
|
import { ok, send, badRequest, forbidden } from 'lib/response';
|
2020-10-03 05:33:46 +02:00
|
|
|
import { createToken } from 'lib/crypto';
|
2022-01-26 06:23:40 +01:00
|
|
|
import { removeTrailingSlash } from 'lib/url';
|
2022-08-05 05:15:21 +02:00
|
|
|
import { uuid } from 'lib/crypto';
|
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);
|
|
|
|
|
2021-03-13 07:44:25 +01:00
|
|
|
if (isbot(req.headers['user-agent'])) {
|
2020-09-11 05:37:07 +02:00
|
|
|
return ok(res);
|
|
|
|
}
|
|
|
|
|
2022-04-10 12:51:43 +02:00
|
|
|
const ignoreIps = process.env.IGNORE_IP;
|
2022-06-27 10:46:21 +02:00
|
|
|
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 => {
|
|
|
|
ips.push(...resolvedIps.filter(n => n).flatMap(n => n));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const clientIp = getIpAddress(req);
|
|
|
|
|
|
|
|
const blocked = ips.find(ip => {
|
|
|
|
if (ip === clientIp) return true;
|
2020-10-04 04:07:56 +02:00
|
|
|
|
2021-04-26 08:57:49 +02:00
|
|
|
// CIDR notation
|
2022-06-27 10:46:21 +02:00
|
|
|
if (ip.indexOf('/') > 0) {
|
|
|
|
const addr = ipaddr.parse(clientIp);
|
|
|
|
const range = ipaddr.parseCIDR(ip);
|
2021-04-26 08:57:49 +02:00
|
|
|
|
2021-08-11 14:31:32 +02:00
|
|
|
if (addr.kind() === range[0].kind() && addr.match(range)) return true;
|
2021-04-26 08:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (blocked) {
|
2022-06-27 10:46:21 +02:00
|
|
|
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) {
|
|
|
|
url = removeTrailingSlash(url);
|
|
|
|
}
|
2020-07-24 04:56:55 +02:00
|
|
|
|
2022-08-05 05:15:21 +02:00
|
|
|
const event_uuid = uuid(website_id, url, session_uuid, event_name);
|
|
|
|
|
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
|
|
|
|
2020-10-03 05:33:46 +02:00
|
|
|
const token = await createToken({ website_id, session_id });
|
|
|
|
|
2022-03-11 05:39:11 +01:00
|
|
|
return send(res, token);
|
2020-07-17 10:03:38 +02:00
|
|
|
};
|