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

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