添加错误修复

This commit is contained in:
CN-JS-HuiBai
2026-04-05 01:44:53 +08:00
parent 0cf10a7e8a
commit 236a548f58

View File

@@ -1,4 +1,6 @@
const axios = require('axios');
const net = require('net');
const dns = require('dns').promises;
const db = require('./db');
/**
@@ -27,9 +29,22 @@ function normalizeGeo(geo) {
return geo;
}
async function getLocation(ip) {
// Normalize IP (strip port if present)
const cleanIp = ip.split(':')[0];
async function getLocation(target) {
// Normalize target (strip port if present)
const cleanTarget = target.split(':')[0];
// Resolve domain to IP if needed
let cleanIp = cleanTarget;
if (net.isIP(cleanTarget) === 0) {
try {
console.log(`[Geo Service] Resolving domain to IP: ${cleanTarget}`);
const lookup = await dns.lookup(cleanTarget);
cleanIp = lookup.address;
} catch (err) {
console.error(`[Geo Service] DNS resolution failed for ${cleanTarget}:`, err.message);
return null;
}
}
// Skip local/reserved IPs
if (isLocalIp(cleanIp)) {