From 86859a5e0d9a5eadd031346071ac9e82dc42f788 Mon Sep 17 00:00:00 2001 From: stswangzhiping <59632378+stswangzhiping@users.noreply.github.com> Date: Sat, 21 Mar 2026 09:20:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BF=87=E6=BB=A4AP=E7=83=AD=E7=82=B9IP?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E7=94=A8checkip.amazonaws.com=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=A4=96=E7=BD=91IP=EF=BC=8C=E6=96=B0=E5=A2=9Eipplus3?= =?UTF-8?q?60=E5=9C=B0=E7=90=86=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Made-with: Cursor --- lib/client.js | 41 +++++++++++++++++++++++++++++++++-------- lib/network.js | 2 +- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/client.js b/lib/client.js index 2d2af7c..c30a510 100644 --- a/lib/client.js +++ b/lib/client.js @@ -35,7 +35,8 @@ class ClawClient { this._frpc = new FrpcManager(); this._dashInfo = {}; this._hbCount = 0; // 心跳计数器,用于定期刷新 dashboard 信息 - this._externalIp = null; // 外网 IP(连接时查询一次,用于服务端地理位置解析) + this._externalIp = null; // 外网 IP + this._location = null; // 地理位置(由 ipplus360 返回,如"北京市-北京市西城区") // WS 层活性检测 this._pingTimer = null; @@ -113,24 +114,47 @@ class ClawClient { ]); this._dashInfo = dashInfo || {}; - // 查询外网 IP(用于服务端地理位置解析),失败不阻断连接 + // 查询外网 IP 和地理位置,失败不阻断连接 try { const https = require('https'); - this._externalIp = await new Promise((resolve) => { - const req = https.get('https://api.ipify.org?format=json', { timeout: 5000 }, (res) => { + + const fetchText = (url) => new Promise((resolve) => { + const req = https.get(url, { timeout: 5000 }, (res) => { + let body = ''; + res.on('data', d => { body += d; }); + res.on('end', () => resolve(body.trim())); + }); + req.on('error', () => resolve(null)); + req.on('timeout', () => { req.destroy(); resolve(null); }); + }); + + const fetchJson = (url) => new Promise((resolve) => { + const req = https.get(url, { timeout: 5000 }, (res) => { let body = ''; res.on('data', d => { body += d; }); res.on('end', () => { - try { resolve(JSON.parse(body).ip || null); } catch { resolve(null); } + try { resolve(JSON.parse(body)); } catch { resolve(null); } }); }); req.on('error', () => resolve(null)); req.on('timeout', () => { req.destroy(); resolve(null); }); }); - if (this._externalIp) log.info('clawd', `外网 IP: ${this._externalIp}`); + + // 外网 IP:checkip.amazonaws.com 返回纯文本 IP + const ip = await fetchText('https://checkip.amazonaws.com'); + if (ip && /^\d{1,3}(\.\d{1,3}){3}$/.test(ip)) { + this._externalIp = ip; + log.info('clawd', `外网 IP: ${this._externalIp}`); + } + + // 地理位置:ipplus360(国内访问)返回 {"success":true,"data":"北京市-北京市西城区"} + const geoResp = await fetchJson('https://www.ipplus360.com/getLocation'); + if (geoResp && geoResp.success && geoResp.data) { + this._location = geoResp.data; + log.info('clawd', `地理位置: ${this._location}`); + } } catch (e) { - log.warn('clawd', '外网 IP 查询失败:', e.message); - this._externalIp = null; + log.warn('clawd', '网络信息查询失败:', e.message); } this._connect(); @@ -290,6 +314,7 @@ class ClawClient { token: this._cfg.token ?? null, local_ip: getLocalIps(), external_ip: this._externalIp ?? null, + location: this._location ?? null, ...this._dashInfo, }; this._send(msg); diff --git a/lib/network.js b/lib/network.js index 19fca75..00e5120 100644 --- a/lib/network.js +++ b/lib/network.js @@ -235,7 +235,7 @@ function getLocalIps() { for (const [name, addrs] of Object.entries(ifaces)) { if (!addrs) continue; for (const addr of addrs) { - if (addr.family === 'IPv4' && !addr.internal) { + if (addr.family === 'IPv4' && !addr.internal && !addr.address.startsWith('10.42.')) { ips.push(addr.address); } }