From 77c9973256926e84dc60eb1cc20fe3a4490bf13e Mon Sep 17 00:00:00 2001 From: Andreas Knuth Date: Thu, 12 Sep 2024 16:01:41 +0200 Subject: [PATCH] Anpassungen zum Thema IP Resolving --- bizmatch-server/src/geo/geo.controller.ts | 10 +++++++--- bizmatch-server/src/geo/geo.service.ts | 8 +++++--- bizmatch-server/src/main.ts | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bizmatch-server/src/geo/geo.controller.ts b/bizmatch-server/src/geo/geo.controller.ts index ded4075..d877aea 100644 --- a/bizmatch-server/src/geo/geo.controller.ts +++ b/bizmatch-server/src/geo/geo.controller.ts @@ -1,7 +1,11 @@ -import { Body, Controller, Get, Ip, Param, Post } from '@nestjs/common'; +import { Body, Controller, createParamDecorator, ExecutionContext, Get, Param, Post } from '@nestjs/common'; import { CountyRequest } from 'src/models/server.model'; import { GeoService } from './geo.service'; - +export const RealIp = createParamDecorator((data: unknown, ctx: ExecutionContext) => { + const request = ctx.switchToHttp().getRequest(); + const realIp = request.headers['x-real-ip'] || request.headers['x-forwarded-for']?.split(',')[0] || request.connection.remoteAddress; + return realIp; +}); @Controller('geo') export class GeoController { constructor(private geoService: GeoService) {} @@ -25,7 +29,7 @@ export class GeoController { return this.geoService.findCountiesStartingWith(countyRequest.prefix, countyRequest.states); } @Get('ipinfo/georesult/wysiwyg') - fetchIpAndGeoLocation(@Ip() userIp: string): any { + fetchIpAndGeoLocation(@RealIp() userIp: string): any { return this.geoService.fetchIpAndGeoLocation(userIp); } } diff --git a/bizmatch-server/src/geo/geo.service.ts b/bizmatch-server/src/geo/geo.service.ts index eb5385b..986ba5b 100644 --- a/bizmatch-server/src/geo/geo.service.ts +++ b/bizmatch-server/src/geo/geo.service.ts @@ -1,9 +1,10 @@ -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; import { readFileSync } from 'fs'; +import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; import { join } from 'path'; import { CityAndStateResult, CountyResult, GeoResult } from 'src/models/main.model'; +import { Logger } from 'winston'; import { City, CountyData, Geo, State } from '../models/server.model'; - // const __filename = fileURLToPath(import.meta.url); // const __dirname = path.dirname(__filename); @@ -11,7 +12,7 @@ import { City, CountyData, Geo, State } from '../models/server.model'; export class GeoService { geo: Geo; counties: CountyData[]; - constructor() { + constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger) { this.loadGeo(); } private loadGeo(): void { @@ -101,6 +102,7 @@ export class GeoService { return this.geo.states.find(s => s.state_code === state).cities.find(c => c.name === city); } async fetchIpAndGeoLocation(ip: string): Promise { + this.logger.info(`IP:${ip}`); const response = await fetch(`${process.env.IP_INFO_URL}/${ip}/geo?token=${process.env.IP_INFO_TOKEN}`, { method: 'GET', }); diff --git a/bizmatch-server/src/main.ts b/bizmatch-server/src/main.ts index 8e2bfd4..99089f5 100644 --- a/bizmatch-server/src/main.ts +++ b/bizmatch-server/src/main.ts @@ -4,7 +4,8 @@ import express from 'express'; import { AppModule } from './app.module'; async function bootstrap() { - express(); + const server = express(); + server.set('trust proxy', true); const app = await NestFactory.create(AppModule); app.use('/bizmatch/payment/webhook', bodyParser.raw({ type: 'application/json' })); app.setGlobalPrefix('bizmatch');