Adaptions according cloudflare
This commit is contained in:
parent
77c9973256
commit
d2f6b3ec3f
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@ 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 { CityAndStateResult, CountyResult, GeoResult, IpInfo } from 'src/models/main.model';
|
||||
import { Logger } from 'winston';
|
||||
import { City, CountyData, Geo, State } from '../models/server.model';
|
||||
import { RealIpInfo } from './geo.controller';
|
||||
// const __filename = fileURLToPath(import.meta.url);
|
||||
// const __dirname = path.dirname(__filename);
|
||||
|
||||
|
|
@ -101,15 +102,21 @@ export class GeoService {
|
|||
getCityWithCoords(state: string, city: string): City {
|
||||
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}`, {
|
||||
async fetchIpAndGeoLocation(ipInfo: RealIpInfo): Promise<IpInfo> {
|
||||
this.logger.info(`IP:${ipInfo.ip} - CountryCode:${ipInfo.countryCode}`);
|
||||
const response = await fetch(`${process.env.IP_INFO_URL}/${ipInfo.ip}/geo?token=${process.env.IP_INFO_TOKEN}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
|
||||
// Fügen Sie den Ländercode aus Cloudflare hinzu, falls verfügbar
|
||||
if (ipInfo.countryCode) {
|
||||
data.cloudflareCountry = ipInfo.countryCode;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -385,3 +385,13 @@ export function createDefaultBusinessListing(): BusinessListing {
|
|||
};
|
||||
}
|
||||
export type StripeSubscription = Stripe.Subscription;
|
||||
export type IpInfo = {
|
||||
ip: string;
|
||||
city: string;
|
||||
region: string;
|
||||
country: string;
|
||||
loc: string; // Coordinates in "latitude,longitude" format
|
||||
org: string;
|
||||
postal: string;
|
||||
timezone: string;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs';
|
||||
import { CityAndStateResult, CountyResult, GeoResult } from '../../../../bizmatch-server/src/models/main.model';
|
||||
import { CityAndStateResult, CountyResult, GeoResult, IpInfo } from '../../../../bizmatch-server/src/models/main.model';
|
||||
import { Place } from '../../../../bizmatch-server/src/models/server.model';
|
||||
import { environment } from '../../environments/environment';
|
||||
|
||||
|
|
@ -27,7 +27,7 @@ export class GeoService {
|
|||
let headers = new HttpHeaders().set('X-Hide-Loading', 'true').set('Accept-Language', 'en-US');
|
||||
return this.http.get(`${this.baseUrl}?q=${prefix},US&format=json&addressdetails=1&limit=5`, { headers }) as Observable<Place[]>;
|
||||
}
|
||||
fetchIpAndGeoLocation(): Observable<any> {
|
||||
return this.http.get(`${this.apiBaseUrl}/bizmatch/geo/ipinfo/georesult/wysiwyg`);
|
||||
fetchIpAndGeoLocation(): Observable<IpInfo> {
|
||||
return this.http.get<IpInfo>(`${this.apiBaseUrl}/bizmatch/geo/ipinfo/georesult/wysiwyg`);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue