move sdk packages to its own folder and rename api & dashboard

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-03-11 13:15:44 +01:00
parent 1ca95442b9
commit 6d4f9010d4
318 changed files with 350 additions and 351 deletions

View File

@@ -0,0 +1,61 @@
import type { FastifyRequest } from 'fastify';
import { logger } from './logger';
interface RemoteIpLookupResponse {
country: string | undefined;
city: string | undefined;
stateprov: string | undefined;
continent: string | undefined;
}
interface GeoLocation {
country: string | undefined;
city: string | undefined;
region: string | undefined;
continent: string | undefined;
}
const geo: GeoLocation = {
country: undefined,
city: undefined,
region: undefined,
continent: undefined,
};
const ignore = ['127.0.0.1', '::1'];
export function getClientIp(req: FastifyRequest) {
if (req.headers['cf-connecting-ip']) {
return String(req.headers['cf-connecting-ip']);
}
if (req.headers['x-forwarded-for']) {
return String(req.headers['x-forwarded-for']);
}
return null;
}
export async function parseIp(ip?: string): Promise<GeoLocation> {
if (!ip || ignore.includes(ip)) {
return geo;
}
try {
const geo = await fetch(`${process.env.GEO_IP_HOST}/${ip}`, {
signal: AbortSignal.timeout(2000),
});
const res = (await geo.json()) as RemoteIpLookupResponse;
return {
country: res.country,
city: res.city,
region: res.stateprov,
continent: res.continent,
};
} catch (e) {
logger.error('Failed to fetch geo location for ip', e);
return geo;
}
}