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!

Add support for datacenter_ip to Swetrix CE

Blue Mousea5a747bfb980
diff --git a/backend/apps/cloud/src/analytics/bot-detection.service.ts b/backend/apps/cloud/src/analytics/bot-detection.service.ts
index edddf241b..3c03ca736 100644
--- a/backend/apps/cloud/src/analytics/bot-detection.service.ts
+++ b/backend/apps/cloud/src/analytics/bot-detection.service.ts
@@ -1,6 +1,6 @@
import fs from 'fs'
import path from 'path'
-import { Injectable, Logger } from '@nestjs/common'
+import { Injectable } from '@nestjs/common'
import { isbot } from 'isbot'
import { parse as parseDomain } from 'tldts'
@@ -179,8 +179,6 @@ const headerValue = (
@Injectable()
export class BotDetectionService {
- private readonly logger = new Logger(BotDetectionService.name)
-
constructor(private readonly projectService: ProjectService) {}
static get spamSetSize(): number {
diff --git a/backend/apps/community/src/analytics/bot-detection.service.ts b/backend/apps/community/src/analytics/bot-detection.service.ts
index 72a0a5a85..db844a8bb 100644
--- a/backend/apps/community/src/analytics/bot-detection.service.ts
+++ b/backend/apps/community/src/analytics/bot-detection.service.ts
@@ -1,20 +1,20 @@
import fs from 'fs'
import path from 'path'
-import { Injectable, Logger } from '@nestjs/common'
+import { Injectable } from '@nestjs/common'
import { isbot } from 'isbot'
import { parse as parseDomain } from 'tldts'
import { BotsProtectionLevel, Project } from '../project/entity/project.entity'
import { ProjectService } from '../project/project.service'
+import { getIPDetails } from '../common/utils'
-// Swetrix CE omits `datacenter_ip` because that detection relies on the
-// paid DB-IP ip-to-location-isp MMDB (cloud only)
export type BotReason =
| 'user_agent'
| 'headless_browser'
| 'suspicious_headers'
| 'probe_path'
| 'referrer_spam'
+ | 'datacenter_ip'
export type BotEndpoint =
| 'pageview'
@@ -176,8 +176,6 @@ const headerValue = (
@Injectable()
export class BotDetectionService {
- private readonly logger = new Logger(BotDetectionService.name)
-
constructor(private readonly projectService: ProjectService) {}
static get spamSetSize(): number {
@@ -243,6 +241,10 @@ export class BotDetectionService {
return { isBot: true, reason: 'referrer_spam' }
}
+ if (input.ip && getIPDetails(input.ip).isHosting) {
+ return { isBot: true, reason: 'datacenter_ip' }
+ }
+
return NEGATIVE
}
diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.ts
index 3d9acc461..0157f17d6 100644
--- a/backend/apps/community/src/common/utils.ts
+++ b/backend/apps/community/src/common/utils.ts
@@ -1126,17 +1126,28 @@ if (SELFHOSTED_GEOIP_DB_PATH && fs.existsSync(SELFHOSTED_GEOIP_DB_PATH)) {
interface IPDetails {
country: string | null
region: string | null
- city: string | null
regionCode: string | null
+ city: string | null
+ isHosting: boolean
}
const getIPDetails = (ip: string, tz?: string): IPDetails => {
- // Stage 1: Using IP address based geo lookup
- const data = lookup.get(ip)
+ let data
+ try {
+ data = ip ? lookup.get(ip) : null
+ } catch {
+ data = null
+ }
+ const traits = data?.traits ?? {}
+ const isHosting =
+ traits.is_hosting_provider === true || traits.user_type === 'hosting'
+
+ // Stage 1: Using IP address based geo lookup
const country = data?.country?.iso_code || null
// TODO: Add city overrides, for example, Colinton -> Edinburgh, etc.
const city = data?.city?.names?.en || null
+ // TODO: Store ISO code, not region name
const region = data?.subdivisions?.[0]?.names?.en || null
const regionCode = data?.subdivisions?.[0]?.iso_code || null
@@ -1146,6 +1157,7 @@ const getIPDetails = (ip: string, tz?: string): IPDetails => {
city,
region,
regionCode,
+ isHosting,
}
}
@@ -1157,6 +1169,7 @@ const getIPDetails = (ip: string, tz?: string): IPDetails => {
city: null,
region: null,
regionCode: null,
+ isHosting,
}
}