📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Added decorator for detecting bots
Yevhenii K12a36c5698da
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.tsindex e7855aca9..56b528440 100644--- a/src/analytics/analytics.controller.ts+++ b/src/analytics/analytics.controller.ts@@ -34,6 +34,8 @@ import {REDIS_LOG_DATA_CACHE_KEY, redis, REDIS_LOG_CUSTOM_CACHE_KEY,HEARTBEAT_SID_LIFE_TIME, REDIS_USERS_COUNT_KEY, REDIS_PROJECTS_COUNT_KEY, REDIS_PAGEVIEWS_COUNT_KEY, REDIS_SESSION_SALT_KEY,} from '../common/constants'+import { BotDetection } from 'src/common/decorators/bot-detection.decorator'+import { BotDetectionGuard } from 'src/common/guards/bot-detection.guard'dayjs.extend(utc)dayjs.extend(timezone)@@ -244,14 +246,11 @@ export class AnalyticsController {// Log custom event@Post('/custom')+ @UseGuards(BotDetectionGuard)+ @BotDetection()async logCustom(@Body() eventsDTO: EventsDTO, @Headers() headers, @Ip() reqIP): Promise<any> {const { 'user-agent': userAgent, origin } = headers- // todo: create a decorator for bot traffic detection- if (isbot(userAgent)) {- throw new ForbiddenException('Bot traffic is ignored')- }-const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)@@ -290,14 +289,11 @@ export class AnalyticsController {// Log pageview event@Post('/')+ @UseGuards(BotDetectionGuard)+ @BotDetection()async log(@Body() logDTO: PageviewsDTO, @Headers() headers, @Ip() reqIP): Promise<any> {const { 'user-agent': userAgent, origin } = headers- // todo: create a decorator for bot traffic detection- if (isbot(userAgent)) {- throw new ForbiddenException('Bot traffic is ignored')- }-const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''await this.analyticsService.validate(logDTO, origin, 'log', ip)diff --git a/src/common/decorators/bot-detection.decorator.ts b/src/common/decorators/bot-detection.decorator.tsnew file mode 100644index 000000000..f3be8c233--- /dev/null+++ b/src/common/decorators/bot-detection.decorator.ts@@ -0,0 +1,4 @@+import { SetMetadata } from '@nestjs/common'++export const IS_BOT_DETECTION_ENABLED = 'isBotDetectionEnabled'+export const BotDetection = () => SetMetadata(IS_BOT_DETECTION_ENABLED, true)diff --git a/src/common/guards/bot-detection.guard.ts b/src/common/guards/bot-detection.guard.tsnew file mode 100644index 000000000..dd4156211--- /dev/null+++ b/src/common/guards/bot-detection.guard.ts@@ -0,0 +1,27 @@+import {+ CanActivate,+ ExecutionContext,+ ForbiddenException,+ Injectable,+} from '@nestjs/common'+import { Reflector } from '@nestjs/core'+import * as isbot from 'isbot'+import { IS_BOT_DETECTION_ENABLED } from 'src/common/decorators/bot-detection.decorator'++@Injectable()+export class BotDetectionGuard implements CanActivate {+ constructor(private readonly reflector: Reflector) {}++ canActivate(context: ExecutionContext): boolean {+ this.reflector.get(IS_BOT_DETECTION_ENABLED, context.getHandler())++ const { headers } = context.switchToHttp().getRequest()+ const { 'user-agent': userAgent } = headers++ if (isbot(userAgent)) {+ throw new ForbiddenException('Bot traffic is ignored')+ }++ return false+ }+}