fix bot detection
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
import { isBot } from '@/bots';
|
|
||||||
import { logInfo } from '@/utils/logger';
|
|
||||||
import { getClientIp, parseIp } from '@/utils/parseIp';
|
import { getClientIp, parseIp } from '@/utils/parseIp';
|
||||||
import { getReferrerWithQuery, parseReferrer } from '@/utils/parseReferrer';
|
import { getReferrerWithQuery, parseReferrer } from '@/utils/parseReferrer';
|
||||||
import { isUserAgentSet, parseUserAgent } from '@/utils/parseUserAgent';
|
import { isUserAgentSet, parseUserAgent } from '@/utils/parseUserAgent';
|
||||||
@@ -9,7 +7,7 @@ import { v4 as uuid } from 'uuid';
|
|||||||
|
|
||||||
import { generateDeviceId, getTime, toISOString } from '@mixan/common';
|
import { generateDeviceId, getTime, toISOString } from '@mixan/common';
|
||||||
import type { IServiceCreateEventPayload } from '@mixan/db';
|
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 type { JobsOptions } from '@mixan/queue';
|
||||||
import { eventsQueue, findJobByPrefix } from '@mixan/queue';
|
import { eventsQueue, findJobByPrefix } from '@mixan/queue';
|
||||||
import type { PostEventPayload } from '@mixan/sdk';
|
import type { PostEventPayload } from '@mixan/sdk';
|
||||||
@@ -155,21 +153,6 @@ export async function postEvent(
|
|||||||
return reply.status(200).send('');
|
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([
|
const [geo, eventsJobs] = await Promise.all([
|
||||||
parseIp(ip),
|
parseIp(ip),
|
||||||
eventsQueue.getJobs(['delayed']),
|
eventsQueue.getJobs(['delayed']),
|
||||||
@@ -231,7 +214,6 @@ export async function postEvent(
|
|||||||
profileId,
|
profileId,
|
||||||
projectId,
|
projectId,
|
||||||
deviceId,
|
deviceId,
|
||||||
bot,
|
|
||||||
geo,
|
geo,
|
||||||
sessionStartEvent,
|
sessionStartEvent,
|
||||||
path,
|
path,
|
||||||
|
|||||||
@@ -1,27 +1,47 @@
|
|||||||
import { isBot as isGetBot } from '@/bots';
|
import { isBot } from '@/bots';
|
||||||
import * as controller from '@/controllers/event.controller';
|
import * as controller from '@/controllers/event.controller';
|
||||||
import { validateSdkRequest } from '@/utils/auth';
|
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) => {
|
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
|
||||||
fastify.addHook('preHandler', (req, reply, done) => {
|
fastify.addHook(
|
||||||
const isBot = req.headers['user-agent']
|
'preHandler',
|
||||||
? isGetBot(req.headers['user-agent'])
|
async (
|
||||||
: false;
|
req: FastifyRequest<{
|
||||||
if (isBot) {
|
Body: PostEventPayload;
|
||||||
reply.log.warn({ ...req.headers, bot: isBot }, 'Bot detected');
|
}>,
|
||||||
reply.status(202).send('OK');
|
reply
|
||||||
}
|
) => {
|
||||||
|
try {
|
||||||
validateSdkRequest(req.headers)
|
const projectId = await validateSdkRequest(req.headers);
|
||||||
.then((projectId) => {
|
|
||||||
req.projectId = projectId;
|
req.projectId = projectId;
|
||||||
done();
|
|
||||||
})
|
const bot = req.headers['user-agent']
|
||||||
.catch((e) => {
|
? 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();
|
reply.status(401).send();
|
||||||
});
|
return;
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
fastify.route({
|
fastify.route({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
@@ -1,17 +1,26 @@
|
|||||||
|
import { isBot } from '@/bots';
|
||||||
import * as controller from '@/controllers/profile.controller';
|
import * as controller from '@/controllers/profile.controller';
|
||||||
import { validateSdkRequest } from '@/utils/auth';
|
import { validateSdkRequest } from '@/utils/auth';
|
||||||
import type { FastifyPluginCallback } from 'fastify';
|
import type { FastifyPluginCallback } from 'fastify';
|
||||||
|
|
||||||
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
|
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
|
||||||
fastify.addHook('preHandler', (req, reply, done) => {
|
fastify.addHook('preHandler', async (req, reply) => {
|
||||||
validateSdkRequest(req.headers)
|
try {
|
||||||
.then((projectId) => {
|
const projectId = await validateSdkRequest(req.headers);
|
||||||
req.projectId = projectId;
|
req.projectId = projectId;
|
||||||
done();
|
|
||||||
})
|
const bot = req.headers['user-agent']
|
||||||
.catch((e) => {
|
? isBot(req.headers['user-agent'])
|
||||||
reply.status(401).send();
|
: 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({
|
fastify.route({
|
||||||
|
|||||||
Reference in New Issue
Block a user