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!

fixed IP retreiving when server is proxied by CF or NGINX

Blue Mouse73c13b08e9db
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
index 5b6f3f024..25ac9f6b5 100644
--- a/src/analytics/analytics.controller.ts
+++ b/src/analytics/analytics.controller.ts
@@ -8,7 +8,7 @@ import * as utc from 'dayjs/plugin/utc'
import { v4 as uuidv4 } from 'uuid'
import { hash } from 'blake3'
import {
- Controller, Body, Query, UseGuards, Get, Post, Ip, Headers, BadRequestException,
+ Controller, Body, Query, UseGuards, Get, Post, Headers, BadRequestException,
InternalServerErrorException, NotImplementedException, UnprocessableEntityException,
} from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
@@ -55,7 +55,7 @@ export class AnalyticsController {
@Get('/')
@UseGuards(RolesGuard)
- // @Roles(UserType.CUSTOMER, UserType.ADMIN)
+ @Roles(UserType.CUSTOMER, UserType.ADMIN)
async getData(@Query() data: AnalyticsGET_DTO): Promise<any> {
this.logger.log({ ...data }, 'GET /log')
@@ -123,11 +123,16 @@ export class AnalyticsController {
}
@Post('/')
- async log(@Body() logDTO: PageviewsDTO, @Ip() ip, @Headers() headers): Promise<any> {
- this.logger.log({ logDTO, ip, headers }, 'POST /log')
+ async log(@Body() logDTO: PageviewsDTO, @Headers() headers): Promise<any> {
+ this.logger.log({ logDTO, headers }, 'POST /log')
+
const { 'user-agent': userAgent, origin } = headers
await this.analyticsService.validate(logDTO, origin)
+ // the NestJS server HAS TO be proxied either by Cloudflare or by NGINX, which are setting the request IP
+ // if the app is hosted raw, @nestjs/common @Ip module should be used to retreive the request IP address
+ const ip = headers['cf-connecting-ip'] || headers['x-forwarded-for'] || ''
+
const sessionHash = getSessionKey(ip, userAgent, logDTO.pid)
const unique = await this.analyticsService.isUnique(sessionHash)
let dto: Array<string | number>