diff --git a/bizmatch-server/src/geo/geo.controller.ts b/bizmatch-server/src/geo/geo.controller.ts index d877aea..2997e7e 100644 --- a/bizmatch-server/src/geo/geo.controller.ts +++ b/bizmatch-server/src/geo/geo.controller.ts @@ -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); } } diff --git a/bizmatch-server/src/geo/geo.service.ts b/bizmatch-server/src/geo/geo.service.ts index 986ba5b..f7b3fde 100644 --- a/bizmatch-server/src/geo/geo.service.ts +++ b/bizmatch-server/src/geo/geo.service.ts @@ -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 { - 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 { + 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; } } diff --git a/bizmatch-server/src/models/main.model.ts b/bizmatch-server/src/models/main.model.ts index 602817e..dc6ade4 100644 --- a/bizmatch-server/src/models/main.model.ts +++ b/bizmatch-server/src/models/main.model.ts @@ -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; +}; diff --git a/bizmatch/src/app/services/geo.service.ts b/bizmatch/src/app/services/geo.service.ts index 3cf5d6a..0757c06 100644 --- a/bizmatch/src/app/services/geo.service.ts +++ b/bizmatch/src/app/services/geo.service.ts @@ -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; } - fetchIpAndGeoLocation(): Observable { - return this.http.get(`${this.apiBaseUrl}/bizmatch/geo/ipinfo/georesult/wysiwyg`); + fetchIpAndGeoLocation(): Observable { + return this.http.get(`${this.apiBaseUrl}/bizmatch/geo/ipinfo/georesult/wysiwyg`); } }