Files
MyIP/api/ip2location-io.js
T
jason5ng32andClaude Opus 5.5 c29e48687b Refactor(layout): split backend-only code out of common/, group frontend utils
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>
2026-10-05 13:10:02 +08:00

34 lines
1.2 KiB
JavaScript

// /api/ip2location — geolocation source handler backed by api.ip2location.io.
// Picks a random API key and normalizes the response into the canonical
// geo shape via the shared makeGeoHandler factory.
import { makeGeoHandler } from '../server/geo-handler.js';
function buildUrl(req) {
const ipAddress = req.query.ip;
const keys = (process.env.IP2LOCATION_API_KEY).split(',');
const key = keys[Math.floor(Math.random() * keys.length)];
return `https://api.ip2location.io/?ip=${ipAddress}&key=${key}`;
}
function modifyJsonForIPAPI(json) {
let asn = json.asn || {};
const { ip, country_code, country_name, region_name, city_name, latitude, longitude, as } = json;
return {
ip: ip,
city: city_name || 'N/A',
region: region_name || 'N/A',
country: country_code || 'N/A',
country_name: country_name || 'N/A',
country_code: country_code || 'N/A',
latitude: latitude || 'N/A',
longitude: longitude || 'N/A',
asn: asn === undefined || asn === null ? 'N/A' : 'AS' + asn,
org: as || 'N/A',
};
}
export default makeGeoHandler({ name: 'ip2location-io', buildUrl, normalize: modifyJsonForIPAPI });