api: use request-ip

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-03-31 22:19:00 +02:00
parent 4433b9f090
commit b04b9d16c6
5 changed files with 11 additions and 18 deletions

View File

@@ -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",

View File

@@ -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 ?? '',

View File

@@ -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> {