feat: report local network types

This commit is contained in:
stswangzhiping
2026-04-26 17:29:58 +08:00
parent f6aad310a8
commit a1c9cc9657
2 changed files with 50 additions and 17 deletions

View File

@@ -11,7 +11,7 @@ const { collect } = require('./metrics');
const { getDashboardInfo, resolveOpenclawConfigFile, startTtyd, FrpcManager } = require('./frpc'); // getDashboardInfo 也用于心跳中定期刷新 const { getDashboardInfo, resolveOpenclawConfigFile, startTtyd, FrpcManager } = require('./frpc'); // getDashboardInfo 也用于心跳中定期刷新
const { ProvisionManager } = require('./provisioning'); const { ProvisionManager } = require('./provisioning');
const { BtMonitor } = require('./bt-monitor'); const { BtMonitor } = require('./bt-monitor');
const { hasInternet, hasWiredInternetProbe, getLocalIps } = require('./network'); const { hasInternet, hasWiredInternetProbe, getLocalIps, getLocalNetworks } = require('./network');
const { applyFullProviderFromVps, removeProviderByName, refreshModelsIfChanged, isFullProvider } = require('./openclaw-provider'); const { applyFullProviderFromVps, removeProviderByName, refreshModelsIfChanged, isFullProvider } = require('./openclaw-provider');
const led = require('./led'); const led = require('./led');
@@ -372,6 +372,7 @@ class ClawClient {
claw_id: this._cfg.claw_id ?? null, claw_id: this._cfg.claw_id ?? null,
token: this._cfg.token ?? null, token: this._cfg.token ?? null,
local_ip: getLocalIps(), local_ip: getLocalIps(),
local_networks: getLocalNetworks(),
external_ip: this._externalIp ?? null, external_ip: this._externalIp ?? null,
location: this._location ?? null, location: this._location ?? null,
...this._dashInfo, ...this._dashInfo,
@@ -597,6 +598,8 @@ class ClawClient {
type: 'heartbeat', type: 'heartbeat',
claw_id: this._cfg.claw_id, claw_id: this._cfg.claw_id,
token: this._cfg.token, token: this._cfg.token,
local_ip: getLocalIps(),
local_networks: getLocalNetworks(),
...this._dashInfo, ...this._dashInfo,
}; };
if (this._hbCount % METRICS_EVERY_N === 0) { if (this._hbCount % METRICS_EVERY_N === 0) {

View File

@@ -507,22 +507,37 @@ function isWifiStaConnected() {
return true; return true;
} }
function _ifaceNetworkType(name) {
const wifi = getWifiIface();
if (name === wifi || name.startsWith('wl')) return 'wifi';
if (name === DEFAULT_ETH_IFACE || name.startsWith('en') || name.startsWith('eth')) return 'lan';
return null;
}
function _localNetworkEntries() {
const ifaces = os.networkInterfaces();
const entries = [];
for (const [name, addrs] of Object.entries(ifaces)) {
if (!addrs) continue;
const type = _ifaceNetworkType(name);
if (!type) continue;
for (const addr of addrs) {
if (addr.family !== 'IPv4' || addr.internal) continue;
// clawd-hotspot 的 AP 管理网段只用于配网,不上报为 BOX 可访问地址。
if (addr.address.startsWith('10.42.')) continue;
entries.push({ ip: addr.address, type, iface: name });
}
}
return entries;
}
/** /**
* 获取本机所有非回环 IPv4 地址,逗号拼接返回 * 获取本机所有非回环 IPv4 地址,逗号拼接返回
* '192.168.1.100' 或 '192.168.1.100,10.0.0.5' * 保持旧协议字段 local_ip 兼容'192.168.1.100' 或 '192.168.1.100,10.0.0.5'
*/ */
function getLocalIps() { function getLocalIps() {
try { try {
const ifaces = os.networkInterfaces(); const ips = _localNetworkEntries().map((entry) => entry.ip);
const ips = [];
for (const [name, addrs] of Object.entries(ifaces)) {
if (!addrs) continue;
for (const addr of addrs) {
if (addr.family === 'IPv4' && !addr.internal && !addr.address.startsWith('10.42.')) {
ips.push(addr.address);
}
}
}
return ips.length > 0 ? ips.join(',') : null; return ips.length > 0 ? ips.join(',') : null;
} catch (e) { } catch (e) {
log.warn('network', '获取本机 IP 失败:', e.message); log.warn('network', '获取本机 IP 失败:', e.message);
@@ -530,6 +545,20 @@ function getLocalIps() {
} }
} }
/**
* 获取本机 IPv4 地址及网络类型,用于上报服务器。
* 例:[{ ip: '192.168.1.100', type: 'wifi', iface: 'wlan0' }]
*/
function getLocalNetworks() {
try {
const entries = _localNetworkEntries();
return entries.length > 0 ? entries : null;
} catch (e) {
log.warn('network', '获取本机网络类型失败:', e.message);
return null;
}
}
module.exports = { module.exports = {
hasInternet, hasInternet,
hasWiredCarrier, hasWiredCarrier,
@@ -547,4 +576,5 @@ module.exports = {
stopAP, stopAP,
AP_IP, AP_IP,
getLocalIps, getLocalIps,
getLocalNetworks,
}; };