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>
61 lines
2.4 KiB
JavaScript
61 lines
2.4 KiB
JavaScript
// /api/ipinfo — geolocation source handler backed by ipinfo.io.
|
|
// Picks a random API token (when configured) and normalizes the response
|
|
// into the canonical geo shape via the shared makeGeoHandler factory.
|
|
|
|
import getCountryName from '../common/country-name.js';
|
|
import { makeGeoHandler } from '../server/geo-handler.js';
|
|
|
|
function buildUrl(req) {
|
|
const ipAddress = req.query.ip;
|
|
|
|
// Build request URL for ipinfo.io.
|
|
// IPINFO_API_TOKEN is the pre-rename spelling — keep reading it so
|
|
// existing deployments don't lose the token on upgrade.
|
|
const tokens = (process.env.IPINFO_API_KEY || process.env.IPINFO_API_TOKEN || '').split(',');
|
|
const token = tokens[Math.floor(Math.random() * tokens.length)];
|
|
|
|
const url_hasToken = `https://ipinfo.io/${ipAddress}?token=${token}`;
|
|
const url_noToken = `https://ipinfo.io/${ipAddress}`;
|
|
return token ? url_hasToken : url_noToken;
|
|
}
|
|
|
|
// Parse one half of `loc`. Blank and non-numeric halves become null rather
|
|
// than the 0 Number() would hand back — 0 is a real coordinate.
|
|
const toCoordinate = (raw) => {
|
|
const value = Number(raw);
|
|
return raw?.trim() && Number.isFinite(value) ? value : null;
|
|
};
|
|
|
|
// Every field is optional upstream: anycast ranges, bogons ({"bogon":true})
|
|
// and degraded answers all come back 200 with `loc`, `org` or `country`
|
|
// missing, so nothing here may be split or dereferenced unguarded. Absent
|
|
// data degrades to null / empty while the canonical shape stays complete.
|
|
export const modifyJson = (json) => {
|
|
const { ip, city, region, country, loc, org } = json || {};
|
|
|
|
const countryName = getCountryName(country, 'en') || 'Unknown Country';
|
|
|
|
// "37.4056,-122.0775" — a partial or non-numeric pair degrades to null.
|
|
const [rawLat, rawLon] = typeof loc === 'string' ? loc.split(',') : [];
|
|
const latitude = toCoordinate(rawLat);
|
|
const longitude = toCoordinate(rawLon);
|
|
|
|
// "AS15169 Google LLC" — leading token is the ASN, the rest the org name.
|
|
const [asn = '', ...orgName] = typeof org === 'string' ? org.split(' ') : [];
|
|
|
|
return {
|
|
ip: ip ?? null,
|
|
city: city ?? null,
|
|
region: region ?? null,
|
|
country: country ?? null,
|
|
country_name: countryName,
|
|
country_code: country ?? null,
|
|
latitude,
|
|
longitude,
|
|
asn,
|
|
org: orgName.join(' ')
|
|
};
|
|
};
|
|
|
|
export default makeGeoHandler({ name: 'ipinfo-io', buildUrl, normalize: modifyJson });
|