mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 17:53:40 +00:00
common/ had become the backend's library: 30 of its 41 modules were imported by nothing in the frontend, so the name no longer said what belonged there. It now holds only what the frontend or the build imports too; backend-only code moved to server/, flat except for datasets/ (the dataset engine and its readers). The *-db/ dataset directories stay in common/: self-hosting docs and Docker volumes mount those paths. frontend/utils/ keeps its app-wide modules at the root and gains ip/ (IP / ASN shaping and validation), report/ (the report pipeline) and features/ (the pure layer of a single feature). Paths only — no logic changes. The Dockerfile copies server/, and the AGENTS.md files say where a new module goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.5 KiB
JavaScript
40 lines
1.5 KiB
JavaScript
// /api/ipapiis — geolocation source handler backed by api.ipapi.is.
|
|
// Picks a random API key and normalizes the response into the canonical
|
|
// geo shape (plus isHosting / isProxy) via the shared makeGeoHandler factory.
|
|
|
|
import { makeGeoHandler } from '../server/geo-handler.js';
|
|
|
|
function buildUrl(req) {
|
|
const ipAddress = req.query.ip;
|
|
|
|
const keys = (process.env.IPAPIIS_API_KEY).split(',');
|
|
const key = keys[Math.floor(Math.random() * keys.length)];
|
|
return `https://api.ipapi.is?q=${ipAddress}&key=${key}`;
|
|
}
|
|
|
|
// Both nested objects are optional: the upstream answers 200 with a null
|
|
// `location` (and no `asn`) for addresses it can't place — anycast ranges,
|
|
// bogons — so neither may be dereferenced directly.
|
|
export function modifyJsonForIPAPI(json) {
|
|
const asn = json.asn || {};
|
|
const location = json.location || {};
|
|
const { ip, is_datacenter, is_proxy, is_vpn, is_tor } = json;
|
|
|
|
return {
|
|
ip: ip,
|
|
city: location.city || 'N/A',
|
|
region: location.state || 'N/A',
|
|
country: location.country_code || 'N/A',
|
|
country_name: location.country || 'N/A',
|
|
country_code: location.country_code || 'N/A',
|
|
latitude: location.latitude || 'N/A',
|
|
longitude: location.longitude || 'N/A',
|
|
asn: asn.asn === undefined ? 'N/A' : 'AS' + asn.asn,
|
|
org: asn.org || 'N/A',
|
|
isHosting: is_datacenter || false,
|
|
isProxy: is_proxy || is_vpn || is_tor || false
|
|
};
|
|
}
|
|
|
|
export default makeGeoHandler({ name: 'ipapi-is', buildUrl, normalize: modifyJsonForIPAPI });
|