From 4467b62a013451441f4ce1e6c5f0a6f6de52532c Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Fri, 10 Apr 2026 23:53:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A0=E6=B3=95=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/geo-service.js | 15 +++++++++++++-- server/index.js | 43 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/server/geo-service.js b/server/geo-service.js index ee183cc..4af35cc 100644 --- a/server/geo-service.js +++ b/server/geo-service.js @@ -31,8 +31,19 @@ function normalizeGeo(geo) { } async function getLocation(target) { - // Normalize target (strip port if present) - const cleanTarget = target.split(':')[0]; + // Normalize target (strip port if present, handle IPv6 brackets) + let cleanTarget = target; + if (cleanTarget.startsWith('[')) { + const closingBracket = cleanTarget.indexOf(']'); + if (closingBracket !== -1) { + cleanTarget = cleanTarget.substring(1, closingBracket); + } + } else { + const parts = cleanTarget.split(':'); + if (parts.length === 2) { + cleanTarget = parts[0]; + } + } // 1. Check if we already have this IP/Domain in DB (FASTEST) try { diff --git a/server/index.js b/server/index.js index dcbdc6a..c5a9a71 100644 --- a/server/index.js +++ b/server/index.js @@ -1090,7 +1090,21 @@ async function getOverview(force = false) { // --- Add Geo Information to Servers --- const geoServers = await Promise.all(overview.servers.map(async (server) => { const realInstance = server.originalInstance || prometheusService.resolveToken(server.instance); - const cleanIp = realInstance.split(':')[0]; + // Helper to get host from instance (handles IPv6 with brackets, IPv4:port, etc.) + let cleanIp = realInstance; + if (cleanIp.startsWith('[')) { + const closingBracket = cleanIp.indexOf(']'); + if (closingBracket !== -1) { + cleanIp = cleanIp.substring(1, closingBracket); + } + } else { + const parts = cleanIp.split(':'); + // If exactly one colon, it's likely IPv4:port or host:port + if (parts.length === 2) { + cleanIp = parts[0]; + } + // If more than 1 colon and no brackets, it's an IPv6 without port - keep as is + } let geoData = null; try { @@ -1417,16 +1431,27 @@ async function broadcastMetrics() { async function start() { try { console.log('🔧 Initializing services...'); - // Ensure DB is ready before starting anything else + + // 1. Initial check + await checkDb(); + + // 2. Automated repair/migration try { const dbFixed = await checkAndFixDatabase(); if (dbFixed) { - // Re-run the local checkDb() to update isDbInitialized and other status variables + // Refresh state after fix await checkDb(); + if (isDbInitialized) { + console.log(' ✅ Database integrity verified and initialized'); + } } } catch (dbErr) { - console.warn('⚠️ Database initialization check encountered an error:', dbErr.message); - // We don't necessarily crash here, but users will see the setup screen if isDbInitialized is false + console.error('❌ Critical database initialization error:', dbErr.message); + // If we have an .env but can't connect, this is a fatal config error + if (fs.existsSync(path.join(__dirname, '..', '.env'))) { + console.error(' Please check your MYSQL settings in .env'); + process.exit(1); + } } // Start services @@ -1456,4 +1481,12 @@ async function start() { } } +process.on('unhandledRejection', (reason, promise) => { + console.error('[System] Unhandled Rejection at:', promise, 'reason:', reason); +}); + +process.on('uncaughtException', (err) => { + console.error('[System] Uncaught Exception:', err); +}); + start();