Anpassungen zum Thema IP Resolving

This commit is contained in:
Andreas Knuth 2024-09-12 16:01:41 +02:00
parent 68d2615f0f
commit 77c9973256
3 changed files with 14 additions and 7 deletions

View File

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

View File

@ -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<any> {
this.logger.info(`IP:${ip}`);
const response = await fetch(`${process.env.IP_INFO_URL}/${ip}/geo?token=${process.env.IP_INFO_TOKEN}`, {
method: 'GET',
});

View File

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