From aa38eddc07c5537b02d0bab05a2ef1adaca892f9 Mon Sep 17 00:00:00 2001 From: MrUnknownDE Date: Sat, 29 Mar 2025 18:38:12 +0100 Subject: [PATCH] fix geoip on ip-adress-lookup --- backend/routes/lookup.js | 74 ++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/backend/routes/lookup.js b/backend/routes/lookup.js index d9afa81..f326682 100644 --- a/backend/routes/lookup.js +++ b/backend/routes/lookup.js @@ -34,37 +34,37 @@ router.get('/', async (req, res, next) => { // Get initialized MaxMind readers const { cityReader, asnReader } = getMaxMindReaders(); - // Perform lookups in parallel - const geoPromise = cityReader.city(targetIp) - .then(geoData => { - let geo = { - city: geoData.city?.names?.en, region: geoData.subdivisions?.[0]?.isoCode, - country: geoData.country?.isoCode, countryName: geoData.country?.names?.en, - postalCode: geoData.postal?.code, latitude: geoData.location?.latitude, - longitude: geoData.location?.longitude, timezone: geoData.location?.timeZone, - }; - geo = Object.fromEntries(Object.entries(geo).filter(([_, v]) => v != null)); - logger.debug({ targetIp, geo }, 'GeoIP lookup successful for lookup'); - return Object.keys(geo).length > 0 ? geo : null; // Return null if empty - }) - .catch(e => { - logger.warn({ targetIp, error: e.message }, `MaxMind City lookup failed for lookup`); - return { error: 'GeoIP lookup failed (IP not found in database or private range).' }; - }); + let geoResult = null; + try { + const geoData = cityReader.city(targetIp); // Synchronous call + let geo = { + city: geoData.city?.names?.en, region: geoData.subdivisions?.[0]?.isoCode, + country: geoData.country?.isoCode, countryName: geoData.country?.names?.en, + postalCode: geoData.postal?.code, latitude: geoData.location?.latitude, + longitude: geoData.location?.longitude, timezone: geoData.location?.timeZone, + }; + geo = Object.fromEntries(Object.entries(geo).filter(([_, v]) => v != null)); + logger.debug({ targetIp, geo }, 'GeoIP lookup successful for lookup'); + geoResult = Object.keys(geo).length > 0 ? geo : null; // Assign result or null + } catch (e) { + logger.warn({ targetIp, error: e.message }, `MaxMind City lookup failed for lookup`); + geoResult = { error: 'GeoIP lookup failed (IP not found in database or private range).' }; + } - const asnPromise = asnReader.asn(targetIp) - .then(asnData => { - let asn = { number: asnData.autonomousSystemNumber, organization: asnData.autonomousSystemOrganization }; - asn = Object.fromEntries(Object.entries(asn).filter(([_, v]) => v != null)); - logger.debug({ targetIp, asn }, 'ASN lookup successful for lookup'); - return Object.keys(asn).length > 0 ? asn : null; // Return null if empty - }) - .catch(e => { - logger.warn({ targetIp, error: e.message }, `MaxMind ASN lookup failed for lookup`); - return { error: 'ASN lookup failed (IP not found in database or private range).' }; - }); + let asnResult = null; + try { + const asnData = asnReader.asn(targetIp); // Synchronous call + let asn = { number: asnData.autonomousSystemNumber, organization: asnData.autonomousSystemOrganization }; + asn = Object.fromEntries(Object.entries(asn).filter(([_, v]) => v != null)); + logger.debug({ targetIp, asn }, 'ASN lookup successful for lookup'); + asnResult = Object.keys(asn).length > 0 ? asn : null; // Assign result or null + } catch (e) { + logger.warn({ targetIp, error: e.message }, `MaxMind ASN lookup failed for lookup`); + asnResult = { error: 'ASN lookup failed (IP not found in database or private range).' }; + } - const rdnsPromise = dns.reverse(targetIp) + // Perform async rDNS lookup + const rdnsResult = await dns.reverse(targetIp) .then(hostnames => { logger.debug({ targetIp, rdns: hostnames }, 'rDNS lookup successful for lookup'); return hostnames; // Returns array of hostnames @@ -78,23 +78,17 @@ router.get('/', async (req, res, next) => { return { error: `rDNS lookup failed (${e.code || 'Unknown error'})` }; }); - // Wait for all promises to settle - const [geoResult, asnResult, rdnsResult] = await Promise.all([ - geoPromise, - asnPromise, - rdnsPromise - ]); - + // Combine results and send response res.json({ success: true, // Indicate overall success of the request processing ip: targetIp, - geo: geoResult, // Will be the geo object, null, or error object - asn: asnResult, // Will be the asn object, null, or error object - rdns: rdnsResult // Will be the hostname array or error object + geo: geoResult, // Result from the sync try...catch + asn: asnResult, // Result from the sync try...catch + rdns: rdnsResult // Result from the async operation }); } catch (error) { - // Catch unexpected errors (e.g., issue with getMaxMindReaders or Promise.all) + // Catch unexpected errors (e.g., issue with getMaxMindReaders or dns.reverse if not caught above) logger.error({ targetIp, requestIp, error: error.message, stack: error.stack }, 'Error processing lookup'); Sentry.captureException(error, { extra: { targetIp, requestIp } }); next(error); // Pass to the main error handler