add bot detection
This commit is contained in:
6
apps/sdk-api/src/bots/bots.readme.md
Normal file
6
apps/sdk-api/src/bots/bots.readme.md
Normal file
@@ -0,0 +1,6 @@
|
||||
# Device Detector - The Universal Device Detection library for parsing User Agents
|
||||
|
||||
> @link https://matomo.org
|
||||
> @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or lat
|
||||
|
||||
[bots.ts](./bots.ts) is based on matomo bots.yml file. You can see the original version here [here](https://raw.githubusercontent.com/matomo-org/device-detector/master/regexes/bots.yml).
|
||||
4901
apps/sdk-api/src/bots/bots.ts
Normal file
4901
apps/sdk-api/src/bots/bots.ts
Normal file
File diff suppressed because it is too large
Load Diff
19
apps/sdk-api/src/bots/index.ts
Normal file
19
apps/sdk-api/src/bots/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import bots from './bots';
|
||||
|
||||
export function isBot(ua: string) {
|
||||
const res = bots.find((bot) => {
|
||||
if (new RegExp(bot.regex).test(ua)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if (!res) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
name: res.name,
|
||||
type: res.category,
|
||||
};
|
||||
}
|
||||
@@ -1,17 +1,24 @@
|
||||
import { isBot as isGetBot } from '@/bots';
|
||||
import * as controller from '@/controllers/event.controller';
|
||||
import { validateSdkRequest } from '@/utils/auth';
|
||||
import type { FastifyPluginCallback } from 'fastify';
|
||||
|
||||
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) => {
|
||||
req.projectId = projectId;
|
||||
done();
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
|
||||
reply.status(401).send();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user