diff --git a/apps/sdk-api/package.json b/apps/sdk-api/package.json index 11579c20..f044890a 100644 --- a/apps/sdk-api/package.json +++ b/apps/sdk-api/package.json @@ -19,7 +19,6 @@ "fastify-sse-v2": "^3.1.2", "pino": "^8.17.2", "ramda": "^0.29.1", - "request-ip": "^3.3.0", "ua-parser-js": "^1.0.37" }, "devDependencies": { @@ -28,7 +27,6 @@ "@mixan/tsconfig": "workspace:*", "@mixan/types": "workspace:*", "@types/ramda": "^0.29.6", - "@types/request-ip": "^0.0.41", "@types/ua-parser-js": "^0.7.39", "eslint": "^8.48.0", "prettier": "^3.0.3", diff --git a/apps/sdk-api/src/controllers/event.controller.ts b/apps/sdk-api/src/controllers/event.controller.ts index 3d20566c..1198b29c 100644 --- a/apps/sdk-api/src/controllers/event.controller.ts +++ b/apps/sdk-api/src/controllers/event.controller.ts @@ -1,9 +1,8 @@ -import { parseIp } from '@/utils/parseIp'; +import { getClientIp, parseIp } from '@/utils/parseIp'; import { getReferrerWithQuery, parseReferrer } from '@/utils/parseReferrer'; import { parseUserAgent } from '@/utils/parseUserAgent'; import type { FastifyReply, FastifyRequest } from 'fastify'; import { omit } from 'ramda'; -import { getClientIp } from 'request-ip'; import { generateProfileId, getTime, toISOString } from '@mixan/common'; import type { IServiceCreateEventPayload } from '@mixan/db'; @@ -86,6 +85,8 @@ export async function postEvent( eventsQueue.getJobs(['delayed']), ]); + console.log('----->', projectId, ip, geo); + // find session_end job const sessionEndJobCurrentProfileId = findJobByPrefix( eventsJobs, diff --git a/apps/sdk-api/src/controllers/profile.controller.ts b/apps/sdk-api/src/controllers/profile.controller.ts index c4754be8..e04fdc64 100644 --- a/apps/sdk-api/src/controllers/profile.controller.ts +++ b/apps/sdk-api/src/controllers/profile.controller.ts @@ -1,11 +1,10 @@ -import { parseIp } from '@/utils/parseIp'; +import { getClientIp, parseIp } from '@/utils/parseIp'; import { parseUserAgent } from '@/utils/parseUserAgent'; import type { FastifyReply, FastifyRequest } from 'fastify'; import { assocPath, mergeDeepRight, path } from 'ramda'; -import { getClientIp } from 'request-ip'; import { generateProfileId, toDots } from '@mixan/common'; -import type { IDBProfile, Profile } from '@mixan/db'; +import type { IDBProfile } from '@mixan/db'; import { db, getSalts } from '@mixan/db'; import type { IncrementProfilePayload, diff --git a/apps/sdk-api/src/index.ts b/apps/sdk-api/src/index.ts index a84f4285..de2de442 100644 --- a/apps/sdk-api/src/index.ts +++ b/apps/sdk-api/src/index.ts @@ -2,7 +2,6 @@ import cors from '@fastify/cors'; import Fastify from 'fastify'; import { FastifySSEPlugin } from 'fastify-sse-v2'; import pino from 'pino'; -import { getClientIp } from 'request-ip'; import { redisPub } from '@mixan/redis'; @@ -30,15 +29,6 @@ const startServer = async () => { origin: '*', }); - fastify.addHook('preHandler', (req, reply, done) => { - const ip = getClientIp(req)!; - console.log('---------------'); - console.log('ip', ip); - console.log('heacders', req.headers); - console.log('---------------'); - done(); - }); - fastify.register(FastifySSEPlugin); fastify.decorateRequest('projectId', ''); fastify.register(eventRouter, { prefix: '/event' }); diff --git a/apps/sdk-api/src/utils/parseIp.ts b/apps/sdk-api/src/utils/parseIp.ts index 115cd64c..ed945d53 100644 --- a/apps/sdk-api/src/utils/parseIp.ts +++ b/apps/sdk-api/src/utils/parseIp.ts @@ -1,3 +1,5 @@ +import type { FastifyRequest, RawRequestDefaultExpression } from 'fastify'; + interface RemoteIpLookupResponse { country: string | undefined; city: string | undefined; @@ -21,6 +23,18 @@ 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; +} + export async function parseIp(ip?: string): Promise { if (!ip || ignore.includes(ip)) { return geo; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6167afdb..ed3fac17 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -216,9 +216,6 @@ importers: ramda: specifier: ^0.29.1 version: 0.29.1 - request-ip: - specifier: ^3.3.0 - version: 3.3.0 ua-parser-js: specifier: ^1.0.37 version: 1.0.37 @@ -238,9 +235,6 @@ importers: '@types/ramda': specifier: ^0.29.6 version: 0.29.7 - '@types/request-ip': - specifier: ^0.0.41 - version: 0.0.41 '@types/ua-parser-js': specifier: ^0.7.39 version: 0.7.39