fix: 过滤AP热点IP,改用checkip.amazonaws.com获取外网IP,新增ipplus360地理位置

Made-with: Cursor
This commit is contained in:
stswangzhiping
2026-03-21 09:20:00 +08:00
parent 878f3592bf
commit 86859a5e0d
2 changed files with 34 additions and 9 deletions

View File

@@ -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}`);
// 外网 IPcheckip.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);