more testing with ip

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-02-12 11:27:53 +01:00
parent 500518b6e8
commit e5b5ac32fa
6 changed files with 19 additions and 23 deletions

View File

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

View File

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

View File

@@ -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' });

View File

@@ -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<GeoLocation> {
if (!ip || ignore.includes(ip)) {
return geo;