mirror of
https://github.com/MrUnknownDE/utools.git
synced 2026-04-19 14:13:44 +02:00
fix geoip on ip-adress-lookup
This commit is contained in:
@@ -34,37 +34,37 @@ router.get('/', async (req, res, next) => {
|
|||||||
// Get initialized MaxMind readers
|
// Get initialized MaxMind readers
|
||||||
const { cityReader, asnReader } = getMaxMindReaders();
|
const { cityReader, asnReader } = getMaxMindReaders();
|
||||||
|
|
||||||
// Perform lookups in parallel
|
let geoResult = null;
|
||||||
const geoPromise = cityReader.city(targetIp)
|
try {
|
||||||
.then(geoData => {
|
const geoData = cityReader.city(targetIp); // Synchronous call
|
||||||
let geo = {
|
let geo = {
|
||||||
city: geoData.city?.names?.en, region: geoData.subdivisions?.[0]?.isoCode,
|
city: geoData.city?.names?.en, region: geoData.subdivisions?.[0]?.isoCode,
|
||||||
country: geoData.country?.isoCode, countryName: geoData.country?.names?.en,
|
country: geoData.country?.isoCode, countryName: geoData.country?.names?.en,
|
||||||
postalCode: geoData.postal?.code, latitude: geoData.location?.latitude,
|
postalCode: geoData.postal?.code, latitude: geoData.location?.latitude,
|
||||||
longitude: geoData.location?.longitude, timezone: geoData.location?.timeZone,
|
longitude: geoData.location?.longitude, timezone: geoData.location?.timeZone,
|
||||||
};
|
};
|
||||||
geo = Object.fromEntries(Object.entries(geo).filter(([_, v]) => v != null));
|
geo = Object.fromEntries(Object.entries(geo).filter(([_, v]) => v != null));
|
||||||
logger.debug({ targetIp, geo }, 'GeoIP lookup successful for lookup');
|
logger.debug({ targetIp, geo }, 'GeoIP lookup successful for lookup');
|
||||||
return Object.keys(geo).length > 0 ? geo : null; // Return null if empty
|
geoResult = Object.keys(geo).length > 0 ? geo : null; // Assign result or null
|
||||||
})
|
} catch (e) {
|
||||||
.catch(e => {
|
logger.warn({ targetIp, error: e.message }, `MaxMind City lookup failed for lookup`);
|
||||||
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).' };
|
||||||
return { error: 'GeoIP lookup failed (IP not found in database or private range).' };
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const asnPromise = asnReader.asn(targetIp)
|
let asnResult = null;
|
||||||
.then(asnData => {
|
try {
|
||||||
let asn = { number: asnData.autonomousSystemNumber, organization: asnData.autonomousSystemOrganization };
|
const asnData = asnReader.asn(targetIp); // Synchronous call
|
||||||
asn = Object.fromEntries(Object.entries(asn).filter(([_, v]) => v != null));
|
let asn = { number: asnData.autonomousSystemNumber, organization: asnData.autonomousSystemOrganization };
|
||||||
logger.debug({ targetIp, asn }, 'ASN lookup successful for lookup');
|
asn = Object.fromEntries(Object.entries(asn).filter(([_, v]) => v != null));
|
||||||
return Object.keys(asn).length > 0 ? asn : null; // Return null if empty
|
logger.debug({ targetIp, asn }, 'ASN lookup successful for lookup');
|
||||||
})
|
asnResult = Object.keys(asn).length > 0 ? asn : null; // Assign result or null
|
||||||
.catch(e => {
|
} catch (e) {
|
||||||
logger.warn({ targetIp, error: e.message }, `MaxMind ASN lookup failed for lookup`);
|
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).' };
|
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 => {
|
.then(hostnames => {
|
||||||
logger.debug({ targetIp, rdns: hostnames }, 'rDNS lookup successful for lookup');
|
logger.debug({ targetIp, rdns: hostnames }, 'rDNS lookup successful for lookup');
|
||||||
return hostnames; // Returns array of hostnames
|
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'})` };
|
return { error: `rDNS lookup failed (${e.code || 'Unknown error'})` };
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for all promises to settle
|
// Combine results and send response
|
||||||
const [geoResult, asnResult, rdnsResult] = await Promise.all([
|
|
||||||
geoPromise,
|
|
||||||
asnPromise,
|
|
||||||
rdnsPromise
|
|
||||||
]);
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true, // Indicate overall success of the request processing
|
success: true, // Indicate overall success of the request processing
|
||||||
ip: targetIp,
|
ip: targetIp,
|
||||||
geo: geoResult, // Will be the geo object, null, or error object
|
geo: geoResult, // Result from the sync try...catch
|
||||||
asn: asnResult, // Will be the asn object, null, or error object
|
asn: asnResult, // Result from the sync try...catch
|
||||||
rdns: rdnsResult // Will be the hostname array or error object
|
rdns: rdnsResult // Result from the async operation
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} 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');
|
logger.error({ targetIp, requestIp, error: error.message, stack: error.stack }, 'Error processing lookup');
|
||||||
Sentry.captureException(error, { extra: { targetIp, requestIp } });
|
Sentry.captureException(error, { extra: { targetIp, requestIp } });
|
||||||
next(error); // Pass to the main error handler
|
next(error); // Pass to the main error handler
|
||||||
|
|||||||
Reference in New Issue
Block a user