Adaptions according cloudflare

This commit is contained in:
2024-09-12 16:14:57 +02:00
parent 77c9973256
commit d2f6b3ec3f
4 changed files with 35 additions and 12 deletions

View File

@@ -1,10 +1,16 @@
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) => {
export interface RealIpInfo {
ip: string;
countryCode?: string;
}
export const RealIp = createParamDecorator((data: unknown, ctx: ExecutionContext): RealIpInfo => {
const request = ctx.switchToHttp().getRequest();
const realIp = request.headers['x-real-ip'] || request.headers['x-forwarded-for']?.split(',')[0] || request.connection.remoteAddress;
return realIp;
const ip = request.headers['cf-connecting-ip'] || request.headers['x-real-ip'] || request.headers['x-forwarded-for']?.split(',')[0] || request.connection.remoteAddress;
const countryCode = request.headers['cf-ipcountry'];
return { ip, countryCode };
});
@Controller('geo')
export class GeoController {
@@ -29,7 +35,7 @@ export class GeoController {
return this.geoService.findCountiesStartingWith(countyRequest.prefix, countyRequest.states);
}
@Get('ipinfo/georesult/wysiwyg')
fetchIpAndGeoLocation(@RealIp() userIp: string): any {
return this.geoService.fetchIpAndGeoLocation(userIp);
fetchIpAndGeoLocation(@RealIp() ipInfo: RealIpInfo): any {
return this.geoService.fetchIpAndGeoLocation(ipInfo);
}
}