fix bot detection

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-03-01 10:15:28 +01:00
parent 0d5a7a4388
commit 94614f1217
3 changed files with 57 additions and 46 deletions

View File

@@ -1,5 +1,3 @@
import { isBot } from '@/bots';
import { logInfo } from '@/utils/logger';
import { getClientIp, parseIp } from '@/utils/parseIp';
import { getReferrerWithQuery, parseReferrer } from '@/utils/parseReferrer';
import { isUserAgentSet, parseUserAgent } from '@/utils/parseUserAgent';
@@ -9,7 +7,7 @@ import { v4 as uuid } from 'uuid';
import { generateDeviceId, getTime, toISOString } from '@mixan/common';
import type { IServiceCreateEventPayload } from '@mixan/db';
import { createBotEvent, createEvent, getEvents, getSalts } from '@mixan/db';
import { createEvent, getEvents, getSalts } from '@mixan/db';
import type { JobsOptions } from '@mixan/queue';
import { eventsQueue, findJobByPrefix } from '@mixan/queue';
import type { PostEventPayload } from '@mixan/sdk';
@@ -155,21 +153,6 @@ export async function postEvent(
return reply.status(200).send('');
}
const bot = isBot(ua);
if (bot) {
request.log.info({ bot, ua }, 'bot detected 2');
try {
await createBotEvent({
...bot,
projectId,
createdAt: new Date(body.timestamp),
});
} catch (e) {
request.log.error(e, 'bot detected 2 failed');
}
return reply.status(200).send('');
}
const [geo, eventsJobs] = await Promise.all([
parseIp(ip),
eventsQueue.getJobs(['delayed']),
@@ -231,7 +214,6 @@ export async function postEvent(
profileId,
projectId,
deviceId,
bot,
geo,
sessionStartEvent,
path,

View File

@@ -1,27 +1,47 @@
import { isBot as isGetBot } from '@/bots';
import { isBot } from '@/bots';
import * as controller from '@/controllers/event.controller';
import { validateSdkRequest } from '@/utils/auth';
import type { FastifyPluginCallback } from 'fastify';
import type { FastifyPluginCallback, FastifyRequest } from 'fastify';
import { createBotEvent } from '@mixan/db';
import type { PostEventPayload } from '@mixan/sdk';
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
fastify.addHook('preHandler', (req, reply, done) => {
const isBot = req.headers['user-agent']
? isGetBot(req.headers['user-agent'])
: false;
if (isBot) {
reply.log.warn({ ...req.headers, bot: isBot }, 'Bot detected');
reply.status(202).send('OK');
}
validateSdkRequest(req.headers)
.then((projectId) => {
fastify.addHook(
'preHandler',
async (
req: FastifyRequest<{
Body: PostEventPayload;
}>,
reply
) => {
try {
const projectId = await validateSdkRequest(req.headers);
req.projectId = projectId;
done();
})
.catch((e) => {
const bot = req.headers['user-agent']
? isBot(req.headers['user-agent'])
: null;
if (bot) {
const path = (req.body?.properties?.__path ||
req.body?.properties?.path) as string | undefined;
reply.log.warn({ ...req.headers, bot }, 'Bot detected (event)');
await createBotEvent({
...bot,
projectId,
path: path ?? '',
createdAt: new Date(req.body?.timestamp),
});
reply.status(202).send('OK');
}
} catch (e) {
reply.log.warn(e, 'Érror');
reply.status(401).send();
});
});
return;
}
}
);
fastify.route({
method: 'POST',

View File

@@ -1,17 +1,26 @@
import { isBot } from '@/bots';
import * as controller from '@/controllers/profile.controller';
import { validateSdkRequest } from '@/utils/auth';
import type { FastifyPluginCallback } from 'fastify';
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
fastify.addHook('preHandler', (req, reply, done) => {
validateSdkRequest(req.headers)
.then((projectId) => {
req.projectId = projectId;
done();
})
.catch((e) => {
reply.status(401).send();
});
fastify.addHook('preHandler', async (req, reply) => {
try {
const projectId = await validateSdkRequest(req.headers);
req.projectId = projectId;
const bot = req.headers['user-agent']
? isBot(req.headers['user-agent'])
: null;
if (bot) {
reply.log.warn({ ...req.headers, bot }, 'Bot detected (profile)');
reply.status(202).send('OK');
}
} catch (e) {
reply.status(401).send();
return;
}
});
fastify.route({