Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

api: use request-ip

Carl-Gerhard Lindesvärdb04b9d16c60c
diff --git a/apps/api/package.json b/apps/api/package.json
index 2ed53e2c3..f9bbb0c25 100644
--- a/apps/api/package.json
+++ b/apps/api/package.json
@@ -23,6 +23,7 @@
"pino": "^8.17.2",
"pino-pretty": "^10.3.1",
"ramda": "^0.29.1",
+ "request-ip": "^3.3.0",
"sharp": "^0.33.2",
"sqlstring": "^2.3.3",
"ua-parser-js": "^1.0.37",
@@ -35,6 +36,7 @@
"@openpanel/sdk": "workspace:*",
"@openpanel/tsconfig": "workspace:*",
"@types/ramda": "^0.29.6",
+ "@types/request-ip": "^0.0.41",
"@types/sqlstring": "^2.3.2",
"@types/ua-parser-js": "^0.7.39",
"@types/uuid": "^9.0.8",
diff --git a/apps/api/src/controllers/event.controller.ts b/apps/api/src/controllers/event.controller.ts
index 570d2e7f8..601d1f6ad 100644
--- a/apps/api/src/controllers/event.controller.ts
+++ b/apps/api/src/controllers/event.controller.ts
@@ -121,7 +121,7 @@ export async function postEvent(
country: event?.country || geo.country || '',
city: event?.city || geo.city || '',
region: event?.region || geo.region || '',
- continent: event?.continent ?? '',
+ continent: event?.continent || geo.continent || '',
os: event?.os ?? '',
osVersion: event?.osVersion ?? '',
browser: event?.browser ?? '',
diff --git a/apps/api/src/utils/parseIp.ts b/apps/api/src/utils/parseIp.ts
index b0acd4eb5..1d9b48a1b 100644
--- a/apps/api/src/utils/parseIp.ts
+++ b/apps/api/src/utils/parseIp.ts
@@ -1,4 +1,5 @@
import type { FastifyRequest } from 'fastify';
+import requestIp from 'request-ip';
import { logger } from './logger';
@@ -26,15 +27,7 @@ const geo: GeoLocation = {
const ignore = ['127.0.0.1', '::1'];
export function getClientIp(req: FastifyRequest) {
- if (req.headers['cf-connecting-ip']) {
- return String(req.headers['cf-connecting-ip']);
- }
-
- if (req.headers['x-forwarded-for']) {
- return String(req.headers['x-forwarded-for']);
- }
-
- return null;
+ return requestIp.getClientIp(req);
}
export async function parseIp(ip?: string): Promise<GeoLocation> {
diff --git a/apps/dashboard/package.json b/apps/dashboard/package.json
index 8ea4baea5..79fa3c24f 100644
--- a/apps/dashboard/package.json
+++ b/apps/dashboard/package.json
@@ -86,7 +86,6 @@
"react-use-websocket": "^4.7.0",
"react-virtualized-auto-sizer": "^1.0.22",
"recharts": "^2.12.0",
- "request-ip": "^3.3.0",
"short-unique-id": "^5.0.3",
"slugify": "^1.6.6",
"sonner": "^1.4.0",
@@ -109,7 +108,6 @@
"@types/react": "^18.2.20",
"@types/react-dom": "^18.2.7",
"@types/react-syntax-highlighter": "^15.5.11",
- "@types/request-ip": "^0.0.41",
"@types/sqlstring": "^2.3.2",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ceb605874..116fef41a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -62,6 +62,9 @@ importers:
ramda:
specifier: ^0.29.1
version: 0.29.1
+ request-ip:
+ specifier: ^3.3.0
+ version: 3.3.0
sharp:
specifier: ^0.33.2
version: 0.33.2
@@ -93,6 +96,9 @@ importers:
'@types/ramda':
specifier: ^0.29.6
version: 0.29.10
+ '@types/request-ip':
+ specifier: ^0.0.41
+ version: 0.0.41
'@types/sqlstring':
specifier: ^2.3.2
version: 2.3.2
@@ -339,9 +345,6 @@ importers:
recharts:
specifier: ^2.12.0
- request-ip:
- specifier: ^3.3.0
- version: 3.3.0
short-unique-id:
specifier: ^5.0.3
version: 5.0.3
@@ -403,9 +406,6 @@ importers:
'@types/react-syntax-highlighter':
specifier: ^15.5.11
version: 15.5.11
- '@types/request-ip':
- specifier: ^0.0.41
- version: 0.0.41
'@types/sqlstring':
specifier: ^2.3.2
version: 2.3.2