📈 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
moved IP address retrieval logic into a dedicated function
Blue Mousea67b8456dde9
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.tsindex 4c22b5783..d66c0ddc5 100644--- a/apps/production/src/analytics/analytics.controller.ts+++ b/apps/production/src/analytics/analytics.controller.ts@@ -59,7 +59,7 @@ import {REDIS_SESSION_SALT_KEY,clickhouse,} from '../common/constants'-import { getGeoDetails } from '../common/utils'+import { getGeoDetails, getIPFromHeaders } from '../common/utils'import { BotDetection } from '../common/decorators/bot-detection.decorator'import { BotDetectionGuard } from '../common/guards/bot-detection.guard'import { GetCustomEventsDto } from './dto/get-custom-events.dto'@@ -873,7 +873,7 @@ export class AnalyticsController {): Promise<any> {const { 'user-agent': userAgent, origin } = headers- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await this.analyticsService.validate(eventsDTO, origin, 'custom', ip)@@ -945,7 +945,7 @@ export class AnalyticsController {): Promise<any> {const { 'user-agent': userAgent } = headersconst { pid } = logDTO- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''const sessionID = await this.analyticsService.getSessionHash(pid,@@ -974,7 +974,7 @@ export class AnalyticsController {): Promise<any> {const { 'user-agent': userAgent, origin } = headers- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await this.analyticsService.validate(logDTO, origin, 'log', ip)@@ -1094,7 +1094,7 @@ export class AnalyticsController {await this.analyticsService.validate(logDTO, origin)- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''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/apps/production/src/auth/auth.controller.ts b/apps/production/src/auth/auth.controller.tsindex 377e3138a..a58f4afdb 100644--- a/apps/production/src/auth/auth.controller.ts+++ b/apps/production/src/auth/auth.controller.ts@@ -26,7 +26,7 @@ import {import { I18nValidationExceptionFilter, I18n, I18nContext } from 'nestjs-i18n'import * as _pick from 'lodash/pick'-import { checkRateLimit } from '../common/utils'+import { checkRateLimit, getIPFromHeaders } from '../common/utils'import { UserType, User } from '../user/entities/user.entity'import { UserService } from '../user/user.service'import { AuthService } from './auth.service'@@ -85,7 +85,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() requestIp: string,): Promise<RegisterResponseDto> {- const ip = headers['x-forwarded-for'] || requestIp || ''+ const ip = getIPFromHeaders(headers) || requestIp || ''await checkRateLimit(ip, 'register', 5)@@ -134,7 +134,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() requestIp: string,): Promise<LoginResponseDto> {- const ip = headers['x-forwarded-for'] || requestIp || ''+ const ip = getIPFromHeaders(headers) || requestIp || ''await checkRateLimit(ip, 'login', 10, 1800)@@ -196,7 +196,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() requestIp: string,): Promise<void> {- const ip = headers['x-forwarded-for'] || requestIp || ''+ const ip = getIPFromHeaders(headers) || requestIp || ''await checkRateLimit(ip, 'reset-password')await checkRateLimit(body.email, 'reset-password')@@ -444,7 +444,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() reqIP: string,): Promise<any> {- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await checkRateLimit(ip, 'sso-generate', OAUTH_RATE_LIMIT, 1800)@@ -469,7 +469,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() reqIP: string,): Promise<any> {- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await checkRateLimit(ip, 'sso-process', OAUTH_RATE_LIMIT, 1800)@@ -487,7 +487,7 @@ export class AuthController {@Headers() headers: unknown,@Ip() reqIP: string,): Promise<any> {- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''const { hash, provider } = bodydiff --git a/apps/production/src/captcha/captcha.controller.ts b/apps/production/src/captcha/captcha.controller.tsindex 948e47027..4977c70ae 100644--- a/apps/production/src/captcha/captcha.controller.ts+++ b/apps/production/src/captcha/captcha.controller.ts@@ -18,6 +18,7 @@ import * as utc from 'dayjs/plugin/utc'import { AppLoggerService } from '../logger/logger.service'import { CAPTCHA_COOKIE_KEY } from '../common/constants'+import { getIPFromHeaders } from '../common/utils'import { CaptchaService, DUMMY_PIDS, isDummyPID } from './captcha.service'import { BotDetectionGuard } from '../common/guards/bot-detection.guard'import { BotDetection } from '../common/decorators/bot-detection.decorator'@@ -122,7 +123,7 @@ export class CaptchaController {)this.captchaService.setTokenCookie(response, newTokenCookie)- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await this.captchaService.logCaptchaPass(pid,@@ -233,7 +234,7 @@ export class CaptchaController {this.captchaService.setTokenCookie(response, newTokenCookie)- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await this.captchaService.logCaptchaPass(pid,diff --git a/apps/production/src/common/utils.ts b/apps/production/src/common/utils.tsindex c79c6050d..caa3624c7 100644--- a/apps/production/src/common/utils.ts+++ b/apps/production/src/common/utils.ts@@ -10,6 +10,7 @@ import * as _toNumber from 'lodash/toNumber'import * as _replace from 'lodash/replace'import * as _find from 'lodash/find'import * as _round from 'lodash/round'+import * as _split from 'lodash/split'import { redis, isDevelopment } from './constants'@@ -208,6 +209,29 @@ const getGeoDetails = (ip: string, tz?: string): IPGeoDetails => {}}+const CLOUDFLARE_ENABLED = false++const getIPFromHeaders = (headers: any) => {+ if (CLOUDFLARE_ENABLED) {+ return headers['cf-connecting-ip'] || null+ }++ // Get IP based on the NGINX configuration+ let ip = headers['x-real-ip']++ if (ip) {+ return ip+ }++ ip = headers['x-forwarded-for'] || null++ if (!ip) {+ return null+ }++ return _split(ip, ',')[0]+}+export {getRandomTip,checkRateLimit,@@ -218,4 +242,5 @@ export {nFormatter,lookup,getGeoDetails,+ getIPFromHeaders,}diff --git a/apps/production/src/twoFactorAuth/twoFactorAuth.controller.ts b/apps/production/src/twoFactorAuth/twoFactorAuth.controller.tsindex cde1bed2a..2d677f952 100644--- a/apps/production/src/twoFactorAuth/twoFactorAuth.controller.ts+++ b/apps/production/src/twoFactorAuth/twoFactorAuth.controller.ts@@ -21,7 +21,11 @@ import { LetterTemplate } from '../mailer/letter'import { TwoFaNotRequired, Roles, CurrentUserId } from '../auth/decorators'import { RolesGuard } from '../auth/guards/roles.guard'import { TwoFactorAuthDTO } from './dto/2fa-auth.dto'-import { generateRecoveryCode, checkRateLimit } from '../common/utils'+import {+ generateRecoveryCode,+ checkRateLimit,+ getIPFromHeaders,+} from '../common/utils'@ApiTags('2fa')@Controller('2fa')@@ -56,7 +60,7 @@ export class TwoFactorAuthController {) {this.logger.log({ body }, 'POST /2fa/enable')- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await checkRateLimit(ip, '2fa-enable', 10, 1800)const user = await this.userService.findOneWhere({ id })@@ -103,7 +107,7 @@ export class TwoFactorAuthController {) {this.logger.log({ body }, 'POST /2fa/disable')- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await checkRateLimit(ip, '2fa-disable', 10, 1800)const user = await this.userService.findOneWhere({ id })@@ -142,7 +146,7 @@ export class TwoFactorAuthController {) {this.logger.log({ body }, 'POST /2fa/authenticate')- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await checkRateLimit(ip, '2fa-auth', 10, 1800)const user = await this.userService.findOneWhere({ id })diff --git a/apps/production/src/user/user.controller.ts b/apps/production/src/user/user.controller.tsindex 2e17f064a..8002b00b1 100644--- a/apps/production/src/user/user.controller.ts+++ b/apps/production/src/user/user.controller.ts@@ -63,7 +63,11 @@ import { AuthService } from '../auth/auth.service'import { LetterTemplate } from '../mailer/letter'import { AppLoggerService } from '../logger/logger.service'import { UserProfileDTO } from './dto/user.dto'-import { checkRateLimit, getGeoDetails } from '../common/utils'+import {+ checkRateLimit,+ getGeoDetails,+ getIPFromHeaders,+} from '../common/utils'import { IUsageInfo, IMetaInfo } from './interfaces'dayjs.extend(utc)@@ -204,7 +208,7 @@ export class UserController {}> {this.logger.log({ userId }, 'POST /user/api-key')- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''await checkRateLimit(ip, 'generate-api-key', 5, 3600)@@ -655,7 +659,7 @@ export class UserController {@Headers() headers,@Ip() reqIP: string,): Promise<IMetaInfo> {- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''const { country } = getGeoDetails(ip)return {diff --git a/apps/production/src/webhook/webhook.controller.ts b/apps/production/src/webhook/webhook.controller.tsindex 7d6775191..e8de6e016 100644--- a/apps/production/src/webhook/webhook.controller.ts+++ b/apps/production/src/webhook/webhook.controller.ts@@ -12,6 +12,7 @@ import { ApiTags } from '@nestjs/swagger'import * as _find from 'lodash/find'import { ProjectService } from '../project/project.service'+import { getIPFromHeaders } from '../common/utils'import {PlanCode,ACCOUNT_PLANS,@@ -40,7 +41,7 @@ export class WebhookController {@Headers() headers,@Ip() reqIP,): Promise<any> {- const ip = headers['x-forwarded-for'] || reqIP || ''+ const ip = getIPFromHeaders(headers) || reqIP || ''this.webhookService.verifyIP(ip)this.webhookService.validateWebhook(body)