Skip to content

📈 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!

Implemented filtering of IP address traffic by black list

Yevhenii Ke99397639d5e
diff --git a/package.json b/package.json
index cf4c9d7e5..2d4a26bd7 100644
--- a/package.json
+++ b/package.json
@@ -46,6 +46,7 @@
"express": "^4.18.1",
"handlebars": "^4.7.7",
"ioredis": "^5.1.0",
+ "ip-range-check": "^0.2.0",
"isbot": "^3.5.0",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 5976f6e04..e7855aca9 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -99,7 +99,7 @@ export class AnalyticsController {
async getData(@Query() data: AnalyticsGET_DTO, @CurrentUserId() uid: string): Promise<any> {
const { pid, period, timeBucket, from, to, filters, timezone = DEFAULT_TIMEZONE } = data
this.analyticsService.validatePID(pid)
-
+
if (!_isEmpty(period)) {
this.analyticsService.validatePeriod(period)
}
@@ -179,7 +179,7 @@ export class AnalyticsController {
const result = await this.analyticsService.groupByTimeBucket(timeBucket, groupFrom, groupTo, subQuery, filtersQuery, paramsData, timezone)
const customs = await this.analyticsService.processCustomEV(queryCustoms, paramsData)
-
+
return {
...result,
customs,
@@ -252,10 +252,10 @@ export class AnalyticsController {
throw new ForbiddenException('Bot traffic is ignored')
}
- await this.analyticsService.validate(eventsDTO, origin, 'custom')
-
const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
+ await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)
+
if (eventsDTO.unique) {
const salt = await redis.get(REDIS_SESSION_SALT_KEY)
const sessionHash = getSessionKeyCustom(ip, userAgent, eventsDTO.pid, eventsDTO.ev, salt)
@@ -298,10 +298,10 @@ export class AnalyticsController {
throw new ForbiddenException('Bot traffic is ignored')
}
- await this.analyticsService.validate(logDTO, origin)
-
const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || reqIP || ''
+ await this.analyticsService.validate(logDTO, origin, 'log', ip)
+
const salt = await redis.get(REDIS_SESSION_SALT_KEY)
const sessionHash = getSessionKey(ip, userAgent, logDTO.pid, salt)
const unique = await this.analyticsService.isUnique(sessionHash)
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
index 10b455a6d..a36893a0c 100644
--- a/src/analytics/analytics.service.ts
+++ b/src/analytics/analytics.service.ts
@@ -15,6 +15,7 @@ import * as _values from 'lodash/values'
import * as dayjs from 'dayjs'
import * as utc from 'dayjs/plugin/utc'
import * as timezone from 'dayjs/plugin/timezone'
+import * as ipRangeCheck from 'ip-range-check'
import { isLocale } from 'validator'
import { hash } from 'blake3'
import {
@@ -127,7 +128,7 @@ export class AnalyticsService {
// https://stackoverflow.com/questions/59645009/how-to-return-only-some-columns-of-a-relations-with-typeorm
project = await this.projectService.findOne(pid, {
relations: ['admin'],
- select: ['origins', 'active', 'admin', 'public'],
+ select: ['origins', 'active', 'admin', 'public', 'ipBlacklist'],
})
}
if (_isEmpty(project)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
@@ -178,12 +179,18 @@ export class AnalyticsService {
}
}
+ checkIpBlacklist(project: Project, ip: string): void {
+ if (!_isEmpty(project.ipBlacklist) && ipRangeCheck(ip, project.ipBlacklist)) {
+ throw new BadRequestException('Incoming analytics is disabled for this IP address')
+ }
+ }
+
validatePID(pid: string): void {
if (_isEmpty(pid)) throw new BadRequestException('The Project ID (pid) has to be provided')
if (!isValidPID(pid)) throw new BadRequestException('The provided Project ID (pid) is incorrect')
}
- async validate(logDTO: PageviewsDTO | EventsDTO, origin: string, type: 'custom' | 'log' = 'log'): Promise<string | null> {
+ async validate(logDTO: PageviewsDTO | EventsDTO, origin: string, type: 'custom' | 'log' = 'log', ip?: string): Promise<string | null> {
if (_isEmpty(logDTO)) throw new BadRequestException('The request cannot be empty')
const { pid } = logDTO
@@ -226,6 +233,8 @@ export class AnalyticsService {
const project = await this.getRedisProject(pid)
+ this.checkIpBlacklist(project, ip)
+
if (!project.active) throw new BadRequestException('Incoming analytics is disabled for this project')
if (!isSelfhosted) {
diff --git a/src/project/dto/project.dto.ts b/src/project/dto/project.dto.ts
index af7329728..32ded2ab0 100644
--- a/src/project/dto/project.dto.ts
+++ b/src/project/dto/project.dto.ts
@@ -25,6 +25,13 @@ export class ProjectDTO {
})
origins: string[]
+ @ApiProperty({
+ example: '::1,127.0.0.1,192.168.0.1/32',
+ required: false,
+ description: 'Array of blocked IP addresses'
+ })
+ ipBlacklist: string[] | null
+
@ApiProperty({
required: false,
description: 'The project\'s state. If enabled - all the incoming analytics data will be saved.',
diff --git a/src/project/entity/project.entity.ts b/src/project/entity/project.entity.ts
index 92fa3917c..2cebaafd0 100644
--- a/src/project/entity/project.entity.ts
+++ b/src/project/entity/project.entity.ts
@@ -22,6 +22,10 @@ export class Project {
@Column('simple-array')
origins: string[]
+ @ApiProperty()
+ @Column('simple-array', { nullable: true, default: null })
+ ipBlacklist: string[]
+
@ApiProperty()
@Column({
default: true,