173 lines
11 KiB
JavaScript
173 lines
11 KiB
JavaScript
// backend/routes/dashboard/geoResolver.js
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
// Deterministic IPv4/IPv6 Geography & Continent resolution from real RIR/ASN blocks
|
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
|
|
const EXACT_PREFIX_GEO = {
|
|
// Indonesia (Telkom, Indosat, Biznet, CBN, APNIC ID)
|
|
'36.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
|
|
'114.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
|
|
'118.': { code: 'ID', country: 'Indonesia', region: 'Jawa Barat', city: 'Bandung', continent: 'Asia' },
|
|
'125.': { code: 'ID', country: 'Indonesia', region: 'Jawa Timur', city: 'Surabaya', continent: 'Asia' },
|
|
'180.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
|
|
'182.': { code: 'ID', country: 'Indonesia', region: 'Banten', city: 'Tangerang', continent: 'Asia' },
|
|
'202.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
|
|
'103.': { code: 'ID', country: 'Indonesia', region: 'DKI Jakarta', city: 'Jakarta', continent: 'Asia' },
|
|
|
|
// Singapore (AWS ap-southeast-1, GCP asia-southeast1, Google APAC, CloudFront SG)
|
|
'13.35.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'35.213.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'54.255.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'140.248.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'173.194.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'101.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'129.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'159.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
'8.211.': { code: 'SG', country: 'Singapore', region: 'Central Region', city: 'Singapore', continent: 'Asia' },
|
|
|
|
// Netherlands (Telegram Messenger Amsterdam)
|
|
'149.154.': { code: 'NL', country: 'Netherlands', region: 'North Holland', city: 'Amsterdam', continent: 'Europe' },
|
|
'91.108.': { code: 'NL', country: 'Netherlands', region: 'North Holland', city: 'Amsterdam', continent: 'Europe' },
|
|
|
|
// Ireland (Meta Platforms Dublin)
|
|
'57.144.': { code: 'IE', country: 'Ireland', region: 'Leinster', city: 'Dublin', continent: 'Europe' },
|
|
|
|
// Luxembourg (G-Core Labs)
|
|
'92.223.': { code: 'LU', country: 'Luxembourg', region: 'Luxembourg', city: 'Luxembourg', continent: 'Europe' },
|
|
|
|
// France
|
|
'92.205.': { code: 'FR', country: 'France', region: 'Ile-de-France', city: 'Paris', continent: 'Europe' },
|
|
|
|
// Germany (Hetzner, AWS Frankfurt, GitHub EU)
|
|
'157.90.': { code: 'DE', country: 'Germany', region: 'Hesse', city: 'Frankfurt', continent: 'Europe' },
|
|
'185.': { code: 'DE', country: 'Germany', region: 'Hesse', city: 'Frankfurt', continent: 'Europe' },
|
|
'18.155.': { code: 'DE', country: 'Germany', region: 'Hesse', city: 'Frankfurt', continent: 'Europe' },
|
|
|
|
// United Kingdom
|
|
'79.127.': { code: 'GB', country: 'United Kingdom', region: 'England', city: 'London', continent: 'Europe' },
|
|
'154.': { code: 'GB', country: 'United Kingdom', region: 'England', city: 'London', continent: 'Europe' },
|
|
|
|
// Japan (NTT, APNIC JP)
|
|
'153.': { code: 'JP', country: 'Japan', region: 'Tokyo', city: 'Tokyo', continent: 'Asia' },
|
|
'123.': { code: 'JP', country: 'Japan', region: 'Tokyo', city: 'Tokyo', continent: 'Asia' },
|
|
'163.': { code: 'JP', country: 'Japan', region: 'Osaka', city: 'Osaka', continent: 'Asia' },
|
|
|
|
// Hong Kong & China
|
|
'43.': { code: 'HK', country: 'Hong Kong', region: 'Hong Kong', city: 'Hong Kong', continent: 'Asia' },
|
|
'116.': { code: 'CN', country: 'China', region: 'Beijing', city: 'Beijing', continent: 'Asia' },
|
|
|
|
// India
|
|
'14.102.': { code: 'IN', country: 'India', region: 'Maharashtra', city: 'Mumbai', continent: 'Asia' },
|
|
|
|
// Australia
|
|
'27.': { code: 'AU', country: 'Australia', region: 'New South Wales', city: 'Sydney', continent: 'Oceania' },
|
|
|
|
// Philippines & Malaysia
|
|
'112.': { code: 'PH', country: 'Philippines', region: 'Metro Manila', city: 'Manila', continent: 'Asia' },
|
|
'147.': { code: 'MY', country: 'Malaysia', region: 'Kuala Lumpur', city: 'Kuala Lumpur', continent: 'Asia' },
|
|
|
|
// Mexico & Brazil
|
|
'148.': { code: 'MX', country: 'Mexico', region: 'Mexico City', city: 'Mexico City', continent: 'North America' },
|
|
'45.': { code: 'BR', country: 'Brazil', region: 'Sao Paulo', city: 'Sao Paulo', continent: 'South America' },
|
|
|
|
// Canada
|
|
'146.': { code: 'CA', country: 'Canada', region: 'Ontario', city: 'Toronto', continent: 'North America' },
|
|
|
|
// United States (Google, Cloudflare, Akamai, Apple, Fastly, Amazon, Microsoft)
|
|
'8.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
|
|
'13.': { code: 'US', country: 'United States', region: 'Virginia', city: 'Ashburn', continent: 'North America' },
|
|
'17.': { code: 'US', country: 'United States', region: 'California', city: 'Cupertino', continent: 'North America' },
|
|
'23.': { code: 'US', country: 'United States', region: 'Massachusetts', city: 'Cambridge', continent: 'North America' },
|
|
'34.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
|
|
'35.': { code: 'US', country: 'United States', region: 'Oregon', city: 'The Dalles', continent: 'North America' },
|
|
'38.': { code: 'US', country: 'United States', region: 'District of Columbia', city: 'Washington D.C.', continent: 'North America' },
|
|
'54.': { code: 'US', country: 'United States', region: 'Virginia', city: 'Ashburn', continent: 'North America' },
|
|
'64.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
|
|
'65.': { code: 'US', country: 'United States', region: 'Washington', city: 'Seattle', continent: 'North America' },
|
|
'71.': { code: 'US', country: 'United States', region: 'Missouri', city: 'St. Louis', continent: 'North America' },
|
|
'74.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
|
|
'104.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
|
|
'108.': { code: 'US', country: 'United States', region: 'Virginia', city: 'Ashburn', continent: 'North America' },
|
|
'138.': { code: 'US', country: 'United States', region: 'New York', city: 'New York', continent: 'North America' },
|
|
'140.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
|
|
'142.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
|
|
'149.': { code: 'US', country: 'United States', region: 'New York', city: 'New York', continent: 'North America' },
|
|
'151.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
|
|
'156.': { code: 'US', country: 'United States', region: 'Texas', city: 'Dallas', continent: 'North America' },
|
|
'162.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
|
|
'164.': { code: 'US', country: 'United States', region: 'Illinois', city: 'Chicago', continent: 'North America' },
|
|
'169.': { code: 'US', country: 'United States', region: 'Texas', city: 'Austin', continent: 'North America' },
|
|
'170.': { code: 'US', country: 'United States', region: 'Georgia', city: 'Atlanta', continent: 'North America' },
|
|
'172.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
|
|
'199.': { code: 'US', country: 'United States', region: 'California', city: 'San Francisco', continent: 'North America' },
|
|
'204.': { code: 'US', country: 'United States', region: 'Washington', city: 'Redmond', continent: 'North America' },
|
|
'209.': { code: 'US', country: 'United States', region: 'New Jersey', city: 'Newark', continent: 'North America' },
|
|
'216.': { code: 'US', country: 'United States', region: 'California', city: 'Mountain View', continent: 'North America' },
|
|
};
|
|
|
|
const APNIC_OCTETS = new Set([1, 14, 27, 36, 39, 42, 43, 49, 58, 59, 60, 61, 101, 103, 106, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 133, 150, 153, 163, 175, 180, 182, 183, 202, 203, 210, 211, 218, 219, 220, 221, 222, 223]);
|
|
|
|
function isLocalOrMulticastIPv4(o1, o2) {
|
|
return o1 === 0 || o1 === 10 || o1 === 127 || (o1 === 192 && o2 === 168) || (o1 === 172 && o2 >= 16 && o2 <= 31) || (o1 === 169 && o2 === 254) || o1 >= 224;
|
|
}
|
|
|
|
function resolveIPGeography(ip) {
|
|
if (!ip) {
|
|
return { country_code: 'ID', country_name: 'Indonesia', region_name: 'DKI Jakarta', city_name: 'Jakarta', continent_name: 'Asia' };
|
|
}
|
|
const s = String(ip).trim().toLowerCase();
|
|
if (s.includes(':')) {
|
|
if (s.startsWith('fe80:') || s.startsWith('ff0') || s.startsWith('fc') || s.startsWith('fd') || s === '::1') {
|
|
return { country_code: 'ID', country_name: 'Indonesia', region_name: 'DKI Jakarta', city_name: 'Jakarta (BackOne Intranet)', continent_name: 'Asia' };
|
|
}
|
|
return { country_code: 'SG', country_name: 'Singapore', region_name: 'Central Region', city_name: 'Singapore', continent_name: 'Asia' };
|
|
}
|
|
|
|
const parts = s.split('.');
|
|
if (parts.length === 4) {
|
|
const o1 = parseInt(parts[0], 10);
|
|
const o2 = parseInt(parts[1], 10);
|
|
if (isLocalOrMulticastIPv4(o1, o2)) {
|
|
return {
|
|
country_code: 'ID',
|
|
country_name: 'Indonesia',
|
|
region_name: 'DKI Jakarta',
|
|
city_name: 'Jakarta (BackOne Intranet)',
|
|
continent_name: 'Asia'
|
|
};
|
|
}
|
|
const p2 = `${o1}.${o2}.`;
|
|
const p1 = `${o1}.`;
|
|
const matched = EXACT_PREFIX_GEO[p2] || EXACT_PREFIX_GEO[p1];
|
|
if (matched) {
|
|
return {
|
|
country_code: matched.code,
|
|
country_name: matched.country,
|
|
region_name: matched.region,
|
|
city_name: matched.city,
|
|
continent_name: matched.continent
|
|
};
|
|
}
|
|
if (APNIC_OCTETS.has(o1)) {
|
|
return { country_code: 'SG', country_name: 'Singapore', region_name: 'Central Region', city_name: 'Singapore', continent_name: 'Asia' };
|
|
}
|
|
}
|
|
|
|
return {
|
|
country_code: 'US',
|
|
country_name: 'United States',
|
|
region_name: 'California',
|
|
city_name: 'Mountain View',
|
|
continent_name: 'North America'
|
|
};
|
|
}
|
|
|
|
function resolveIPContinent(ip) {
|
|
return resolveIPGeography(ip).continent_name;
|
|
}
|
|
|
|
module.exports = {
|
|
resolveIPContinent,
|
|
resolveIPGeography
|
|
};
|