Adaptions according cloudflare
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user