fix: 过滤AP热点IP,改用checkip.amazonaws.com获取外网IP,新增ipplus360地理位置
Made-with: Cursor
This commit is contained in:
@@ -35,7 +35,8 @@ class ClawClient {
|
|||||||
this._frpc = new FrpcManager();
|
this._frpc = new FrpcManager();
|
||||||
this._dashInfo = {};
|
this._dashInfo = {};
|
||||||
this._hbCount = 0; // 心跳计数器,用于定期刷新 dashboard 信息
|
this._hbCount = 0; // 心跳计数器,用于定期刷新 dashboard 信息
|
||||||
this._externalIp = null; // 外网 IP(连接时查询一次,用于服务端地理位置解析)
|
this._externalIp = null; // 外网 IP
|
||||||
|
this._location = null; // 地理位置(由 ipplus360 返回,如"北京市-北京市西城区")
|
||||||
|
|
||||||
// WS 层活性检测
|
// WS 层活性检测
|
||||||
this._pingTimer = null;
|
this._pingTimer = null;
|
||||||
@@ -113,24 +114,47 @@ class ClawClient {
|
|||||||
]);
|
]);
|
||||||
this._dashInfo = dashInfo || {};
|
this._dashInfo = dashInfo || {};
|
||||||
|
|
||||||
// 查询外网 IP(用于服务端地理位置解析),失败不阻断连接
|
// 查询外网 IP 和地理位置,失败不阻断连接
|
||||||
try {
|
try {
|
||||||
const https = require('https');
|
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 = '';
|
let body = '';
|
||||||
res.on('data', d => { body += d; });
|
res.on('data', d => { body += d; });
|
||||||
res.on('end', () => {
|
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('error', () => resolve(null));
|
||||||
req.on('timeout', () => { req.destroy(); 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) {
|
} catch (e) {
|
||||||
log.warn('clawd', '外网 IP 查询失败:', e.message);
|
log.warn('clawd', '网络信息查询失败:', e.message);
|
||||||
this._externalIp = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._connect();
|
this._connect();
|
||||||
@@ -290,6 +314,7 @@ class ClawClient {
|
|||||||
token: this._cfg.token ?? null,
|
token: this._cfg.token ?? null,
|
||||||
local_ip: getLocalIps(),
|
local_ip: getLocalIps(),
|
||||||
external_ip: this._externalIp ?? null,
|
external_ip: this._externalIp ?? null,
|
||||||
|
location: this._location ?? null,
|
||||||
...this._dashInfo,
|
...this._dashInfo,
|
||||||
};
|
};
|
||||||
this._send(msg);
|
this._send(msg);
|
||||||
|
|||||||
@@ -235,7 +235,7 @@ function getLocalIps() {
|
|||||||
for (const [name, addrs] of Object.entries(ifaces)) {
|
for (const [name, addrs] of Object.entries(ifaces)) {
|
||||||
if (!addrs) continue;
|
if (!addrs) continue;
|
||||||
for (const addr of addrs) {
|
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);
|
ips.push(addr.address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user