feat: report local network types
This commit is contained in:
@@ -507,22 +507,37 @@ function isWifiStaConnected() {
|
||||
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 地址,逗号拼接返回
|
||||
* 例:'192.168.1.100' 或 '192.168.1.100,10.0.0.5'
|
||||
* 获取本机所有非回环 IPv4 地址,逗号拼接返回。
|
||||
* 保持旧协议字段 local_ip 兼容:'192.168.1.100' 或 '192.168.1.100,10.0.0.5'。
|
||||
*/
|
||||
function getLocalIps() {
|
||||
try {
|
||||
const ifaces = os.networkInterfaces();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
const ips = _localNetworkEntries().map((entry) => entry.ip);
|
||||
return ips.length > 0 ? ips.join(',') : null;
|
||||
} catch (e) {
|
||||
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 = {
|
||||
hasInternet,
|
||||
hasWiredCarrier,
|
||||
@@ -547,4 +576,5 @@ module.exports = {
|
||||
stopAP,
|
||||
AP_IP,
|
||||
getLocalIps,
|
||||
getLocalNetworks,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user