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!

Merge IP-related functions into a single getIPDetails

Blue Mouse4f5c75919221
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts
index 469099f41..8c64d4ba0 100644
--- a/backend/apps/cloud/src/analytics/analytics.controller.ts
+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts
@@ -58,11 +58,7 @@ import {
REDIS_EVENTS_COUNT_KEY,
} from '../common/constants'
import { clickhouse } from '../common/integrations/clickhouse'
-import {
- checkRateLimit,
- getGeoDetails,
- getIPFromHeaders,
-} from '../common/utils'
+import { checkRateLimit, getIPDetails, getIPFromHeaders } from '../common/utils'
import { GetCustomEventsDto } from './dto/get-custom-events.dto'
import { GetFiltersDto } from './dto/get-filters.dto'
import { GetVersionFiltersDto } from './dto/get-version-filters.dto'
@@ -1468,7 +1464,7 @@ export class AnalyticsController {
profileId,
)
- const { city, region, regionCode, country } = getGeoDetails(ip, errorDTO.tz)
+ const { city, region, regionCode, country } = getIPDetails(ip, errorDTO.tz)
this.analyticsService.checkCountryBlacklist(project, country)
@@ -1591,10 +1587,7 @@ export class AnalyticsController {
}
}
- const { city, region, regionCode, country } = getGeoDetails(
- ip,
- eventsDTO.tz,
- )
+ const { city, region, regionCode, country } = getIPDetails(ip, eventsDTO.tz)
this.analyticsService.checkCountryBlacklist(project, country)
@@ -1774,7 +1767,7 @@ export class AnalyticsController {
)
}
- const { city, region, regionCode, country } = getGeoDetails(ip, logDTO.tz)
+ const { city, region, regionCode, country } = getIPDetails(ip, logDTO.tz)
this.analyticsService.checkCountryBlacklist(project, country)
@@ -1925,7 +1918,7 @@ export class AnalyticsController {
profileId,
)
- const { city, region, regionCode, country } = getGeoDetails(ip)
+ const { city, region, regionCode, country } = getIPDetails(ip)
this.analyticsService.checkCountryBlacklist(project, country)
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index aa68dd580..cec779eab 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -65,7 +65,7 @@ import { clickhouse } from '../common/integrations/clickhouse'
import { getDomainsForRefName } from './utils/referrers.map'
import {
calculateRelativePercentage,
- getGeoDetails,
+ getIPDetails,
hash,
millisecondsToSeconds,
sumArrays,
@@ -385,7 +385,7 @@ export class AnalyticsService {
})
if (result.isBot && result.reason) {
- const { country } = getGeoDetails(ip)
+ const { country } = getIPDetails(ip)
try {
await clickhouse.insert({
diff --git a/backend/apps/cloud/src/analytics/bot-detection.service.ts b/backend/apps/cloud/src/analytics/bot-detection.service.ts
index 3401a90af..edddf241b 100644
--- a/backend/apps/cloud/src/analytics/bot-detection.service.ts
+++ b/backend/apps/cloud/src/analytics/bot-detection.service.ts
@@ -6,7 +6,7 @@ import { parse as parseDomain } from 'tldts'
import { BotsProtectionLevel, Project } from '../project/entity/project.entity'
import { ProjectService } from '../project/project.service'
-import { getIspDetails } from '../common/utils'
+import { getIPDetails } from '../common/utils'
export type BotReason =
| 'user_agent'
@@ -259,7 +259,7 @@ export class BotDetectionService {
return { isBot: true, reason: 'referrer_spam' }
}
- if (input.ip && getIspDetails(input.ip).isHosting) {
+ if (input.ip && getIPDetails(input.ip).isHosting) {
return { isBot: true, reason: 'datacenter_ip' }
}
diff --git a/backend/apps/cloud/src/auth/auth.service.ts b/backend/apps/cloud/src/auth/auth.service.ts
index e11a834d3..2a8a9e1a7 100644
--- a/backend/apps/cloud/src/auth/auth.service.ts
+++ b/backend/apps/cloud/src/auth/auth.service.ts
@@ -40,7 +40,7 @@ import {
JWT_REFRESH_TOKEN_LIFETIME,
JWT_REFRESH_TOKEN_SECRET,
} from '../common/constants'
-import { getGeoDetails } from '../common/utils'
+import { getIPDetails } from '../common/utils'
import { TelegramService } from '../integrations/telegram/telegram.service'
import { TwoFactorAuthService } from '../twoFactorAuth/twoFactorAuth.service'
import { SSOProviders } from './dtos'
@@ -351,7 +351,7 @@ export class AuthService {
const browser = ua.browser.name || 'Unknown'
const device = ua.device.type || 'Desktop'
const os = ua.os.name || 'Unknown'
- let { country } = getGeoDetails(ip)
+ let { country } = getIPDetails(ip)
if (country) {
country = getCountry(country)?.name
diff --git a/backend/apps/cloud/src/captcha/captcha.service.ts b/backend/apps/cloud/src/captcha/captcha.service.ts
index a9e200d02..921e6d7b7 100644
--- a/backend/apps/cloud/src/captcha/captcha.service.ts
+++ b/backend/apps/cloud/src/captcha/captcha.service.ts
@@ -15,7 +15,7 @@ import {
getRedisCaptchaKey,
CAPTCHA_TOKEN_LIFETIME,
} from '../common/constants'
-import { getGeoDetails } from '../common/utils'
+import { getIPDetails } from '../common/utils'
import { GeneratedChallenge } from './interfaces/generated-captcha'
import { captchaTransformer } from './utils/transformers'
import { clickhouse } from '../common/integrations/clickhouse'
@@ -151,7 +151,7 @@ export class CaptchaService {
const browserName = ua.browser.name
const osName = ua.os.name
- const { country } = getGeoDetails(ip)
+ const { country } = getIPDetails(ip)
const transformed = captchaTransformer(
pid,
deviceType,
diff --git a/backend/apps/cloud/src/common/utils.ts b/backend/apps/cloud/src/common/utils.ts
index a63bf1040..e9868336d 100644
--- a/backend/apps/cloud/src/common/utils.ts
+++ b/backend/apps/cloud/src/common/utils.ts
@@ -260,52 +260,27 @@ if (fs.existsSync(PRODUCTION_GEOIP_DB_PATH)) {
lookup = new Reader<CityResponse>(buffer)
}
-interface IspDetails {
+interface IPDetails {
+ country: string | null
+ region: string | null
+ regionCode: string | null
+ city: string | null
isHosting: boolean
- isAnonymousProxy: boolean
- isTorExitNode: boolean
-}
-
-const EMPTY_ISP_DETAILS: IspDetails = {
- isHosting: false,
- isAnonymousProxy: false,
- isTorExitNode: false,
}
-export const getIspDetails = (ip: string): IspDetails => {
- if (!ip) return EMPTY_ISP_DETAILS
-
+export const getIPDetails = (ip: string, tz?: string): IPDetails => {
let data
try {
- data = lookup.get(ip)
+ data = ip ? lookup.get(ip) : null
} catch {
- return EMPTY_ISP_DETAILS
+ data = null
}
- if (!data) return EMPTY_ISP_DETAILS
-
- const traits = data.traits ?? {}
+ const traits = data?.traits ?? {}
const isHosting =
traits.is_hosting_provider === true || traits.user_type === 'hosting'
- return {
- isHosting,
- isAnonymousProxy: traits.is_anonymous_proxy === true,
- isTorExitNode: traits.is_tor_exit_node === true,
- }
-}
-
-interface IPGeoDetails {
- country: string | null
- region: string | null
- regionCode: string | null
- city: string | null
-}
-
-export const getGeoDetails = (ip: string, tz?: string): IPGeoDetails => {
// Stage 1: Using IP address based geo lookup
- const data = lookup.get(ip)
-
const country = data?.country?.iso_code || null
// TODO: Add city overrides, for example, Colinton -> Edinburgh, etc.
const city = data?.city?.names?.en || null
@@ -319,6 +294,7 @@ export const getGeoDetails = (ip: string, tz?: string): IPGeoDetails => {
city,
region,
regionCode,
+ isHosting,
}
}
@@ -330,6 +306,7 @@ export const getGeoDetails = (ip: string, tz?: string): IPGeoDetails => {
city: null,
region: null,
regionCode: null,
+ isHosting,
}
}
diff --git a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
index c1c42b5c1..38f84dd2d 100644
--- a/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
+++ b/backend/apps/cloud/src/feature-flag/feature-flag.controller.ts
@@ -52,7 +52,7 @@ import { FeatureFlagService } from './feature-flag.service'
import { ExperimentService } from '../experiment/experiment.service'
import { ExperimentStatus } from '../experiment/entity/experiment.entity'
import { clickhouse } from '../common/integrations/clickhouse'
-import { getIPFromHeaders, getGeoDetails } from '../common/utils'
+import { getIPFromHeaders, getIPDetails } from '../common/utils'
import { getExperimentVariant } from './evaluation'
import { trackCustom } from '../common/analytics'
@@ -283,7 +283,7 @@ export class FeatureFlagController {
// Derive attributes from request headers (like analytics does)
const userAgent = headers['user-agent'] || ''
- const { country, city, region } = getGeoDetails(ip)
+ const { country, city, region } = getIPDetails(ip)
const { deviceType, browserName, osName } =
await this.analyticsService.getRequestInformation(headers)
diff --git a/backend/apps/cloud/src/user/user.controller.ts b/backend/apps/cloud/src/user/user.controller.ts
index 8ff35dd09..c0a94b90e 100644
--- a/backend/apps/cloud/src/user/user.controller.ts
+++ b/backend/apps/cloud/src/user/user.controller.ts
@@ -53,11 +53,7 @@ import { LetterTemplate } from '../mailer/letter'
import { AppLoggerService } from '../logger/logger.service'
import { DeleteSelfDTO } from './dto/delete-self.dto'
import { CancelSubscriptionDTO } from './dto/cancel-subscription.dto'
-import {
- checkRateLimit,
- getGeoDetails,
- getIPFromHeaders,
-} from '../common/utils'
+import { checkRateLimit, getIPDetails, getIPFromHeaders } from '../common/utils'
import { IUsageInfo, IMetaInfo } from './interfaces'
import { ReportFrequency } from '../project/enums'
import { OrganisationService } from '../organisation/organisation.service'
@@ -693,7 +689,7 @@ export class UserController {
@Ip() reqIP: string,
): Promise<IMetaInfo> {
const ip = getIPFromHeaders(headers) || reqIP || ''
- const { country, city, region } = getGeoDetails(ip)
+ const { country, city, region } = getIPDetails(ip)
return {
country,
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index d72185dc7..ef8fbab4f 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -50,11 +50,7 @@ import { GetFunnelsDto } from './dto/getFunnels.dto'
import { GetFunnelSessionsDto } from './dto/get-funnel-sessions.dto'
import { AppLoggerService } from '../logger/logger.service'
import { clickhouse } from '../common/integrations/clickhouse'
-import {
- checkRateLimit,
- getGeoDetails,
- getIPFromHeaders,
-} from '../common/utils'
+import { checkRateLimit, getIPDetails, getIPFromHeaders } from '../common/utils'
import { GetCustomEventsDto } from './dto/get-custom-events.dto'
import { GetBotStatsDto } from './dto/get-bot-stats.dto'
import {
@@ -1107,10 +1103,7 @@ export class AnalyticsController {
}
}
- const { city, region, regionCode, country } = getGeoDetails(
- ip,
- eventsDTO.tz,
- )
+ const { city, region, regionCode, country } = getIPDetails(ip, eventsDTO.tz)
this.analyticsService.checkCountryBlacklist(project, country)
@@ -1289,7 +1282,7 @@ export class AnalyticsController {
)
}
- const { city, region, regionCode, country } = getGeoDetails(ip, logDTO.tz)
+ const { city, region, regionCode, country } = getIPDetails(ip, logDTO.tz)
this.analyticsService.checkCountryBlacklist(project, country)
@@ -1442,7 +1435,7 @@ export class AnalyticsController {
profileId,
)
- const { city, region, regionCode, country } = getGeoDetails(ip, null)
+ const { city, region, regionCode, country } = getIPDetails(ip, null)
this.analyticsService.checkCountryBlacklist(project, country)
@@ -1803,7 +1796,7 @@ export class AnalyticsController {
profileId,
)
- const { city, region, regionCode, country } = getGeoDetails(ip, errorDTO.tz)
+ const { city, region, regionCode, country } = getIPDetails(ip, errorDTO.tz)
this.analyticsService.checkCountryBlacklist(project, country)
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index eee0f7a25..32f896896 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -60,7 +60,7 @@ import { clickhouse } from '../common/integrations/clickhouse'
import { getDomainsForRefName } from './utils/referrers.map'
import {
getFunnelClickhouse,
- getGeoDetails,
+ getIPDetails,
hash,
millisecondsToSeconds,
sumArrays,
@@ -380,7 +380,7 @@ export class AnalyticsService {
})
if (result.isBot && result.reason) {
- const { country } = getGeoDetails(ip)
+ const { country } = getIPDetails(ip)
try {
await clickhouse.insert({
diff --git a/backend/apps/community/src/captcha/captcha.service.ts b/backend/apps/community/src/captcha/captcha.service.ts
index a9e200d02..921e6d7b7 100644
--- a/backend/apps/community/src/captcha/captcha.service.ts
+++ b/backend/apps/community/src/captcha/captcha.service.ts
@@ -15,7 +15,7 @@ import {
getRedisCaptchaKey,
CAPTCHA_TOKEN_LIFETIME,
} from '../common/constants'
-import { getGeoDetails } from '../common/utils'
+import { getIPDetails } from '../common/utils'
import { GeneratedChallenge } from './interfaces/generated-captcha'
import { captchaTransformer } from './utils/transformers'
import { clickhouse } from '../common/integrations/clickhouse'
@@ -151,7 +151,7 @@ export class CaptchaService {
const browserName = ua.browser.name
const osName = ua.os.name
- const { country } = getGeoDetails(ip)
+ const { country } = getIPDetails(ip)
const transformed = captchaTransformer(
pid,
deviceType,
diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.ts
index 40ebba218..3d9acc461 100644
--- a/backend/apps/community/src/common/utils.ts
+++ b/backend/apps/community/src/common/utils.ts
@@ -1123,14 +1123,14 @@ if (SELFHOSTED_GEOIP_DB_PATH && fs.existsSync(SELFHOSTED_GEOIP_DB_PATH)) {
lookup = new Reader<CityResponse>(buffer)
}
-interface IPGeoDetails {
+interface IPDetails {
country: string | null
region: string | null
city: string | null
regionCode: string | null
}
-const getGeoDetails = (ip: string, tz?: string): IPGeoDetails => {
+const getIPDetails = (ip: string, tz?: string): IPDetails => {
// Stage 1: Using IP address based geo lookup
const data = lookup.get(ip)
@@ -1314,7 +1314,7 @@ export {
findRefreshTokenClickhouse,
deleteRefreshTokenClickhouse,
deleteAllRefreshTokensClickhouse,
- getGeoDetails,
+ getIPDetails,
getIPFromHeaders,
sumArrays,
assignUnassignedProjectsToUserClickhouse,
diff --git a/backend/apps/community/src/feature-flag/feature-flag.controller.ts b/backend/apps/community/src/feature-flag/feature-flag.controller.ts
index 838f5b203..0f1245fd5 100644
--- a/backend/apps/community/src/feature-flag/feature-flag.controller.ts
+++ b/backend/apps/community/src/feature-flag/feature-flag.controller.ts
@@ -44,7 +44,7 @@ import {
} from './dto/feature-flag.dto'
import { FeatureFlagService } from './feature-flag.service'
import { clickhouse } from '../common/integrations/clickhouse'
-import { getIPFromHeaders, getGeoDetails } from '../common/utils'
+import { getIPFromHeaders, getIPDetails } from '../common/utils'
const FEATURE_FLAGS_MAXIMUM = 50 // Maximum feature flags per project
const FEATURE_FLAGS_PAGINATION_MAX_TAKE = 100
@@ -228,7 +228,7 @@ export class FeatureFlagController {
// Derive attributes from request headers (like analytics does)
const userAgent = headers['user-agent'] || ''
- const { country, city, region } = getGeoDetails(ip)
+ const { country, city, region } = getIPDetails(ip)
const { deviceType, browserName, osName } =
await this.analyticsService.getRequestInformation(headers)