From 5c382c8a2442815504d5dcca4f4cee7ec3e44d27 Mon Sep 17 00:00:00 2001 From: zhiyu1998 <542716863@qq.com> Date: Fri, 22 Nov 2024 20:41:43 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=9B=91=E6=8E=A7=E6=97=A0=E6=B3=95=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + server/app/r/api/network2/route.js | 102 +++++++++++++++++++++-------- server/components/home/network.jsx | 14 ++-- 3 files changed, 84 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index f62f624..221716a 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "js-yaml": "^4.1.0", "next": "^14.2.16", "node-id3": "^0.2.6", + "node-os-utils": "^1.3.7", "os-utils": "^0.0.14", "p-queue": "^8.0.1", "qrcode": "^1.5.3", diff --git a/server/app/r/api/network2/route.js b/server/app/r/api/network2/route.js index 31a7198..a3b5fd3 100644 --- a/server/app/r/api/network2/route.js +++ b/server/app/r/api/network2/route.js @@ -1,48 +1,92 @@ +import { promises as fs } from 'fs'; import os from 'os'; +import si from 'systeminformation'; let lastBytesReceived = 0; let lastBytesSent = 0; let lastTimestamp = Date.now(); -function getNetworkStats() { - const networkInterfaces = os.networkInterfaces(); +async function getLinuxStats() { + const data = await fs.readFile('/proc/net/dev', 'utf8'); + const lines = data.trim().split('\n'); + let bytesReceived = 0; let bytesSent = 0; - // 累加所有网络接口的数据 - Object.values(networkInterfaces).forEach(interfaces => { - interfaces.forEach(netInterface => { - if (netInterface.internal === false) { - bytesReceived += netInterface.bytes_received || 0; - bytesSent += netInterface.bytes_sent || 0; - } - }); - }); + for (let i = 2; i < lines.length; i++) { + const line = lines[i].trim(); + const parts = line.split(/\s+/); - const now = Date.now(); - const timeDiff = (now - lastTimestamp) / 1000; // 转换为秒 + if (parts[0].startsWith('lo:')) continue; - // 计算速率(字节/秒) - const downloadSpeed = (bytesReceived - lastBytesReceived) / timeDiff; - const uploadSpeed = (bytesSent - lastBytesSent) / timeDiff; + bytesReceived += parseInt(parts[1], 10); + bytesSent += parseInt(parts[9], 10); + } - // 更新上次的值 - lastBytesReceived = bytesReceived; - lastBytesSent = bytesSent; - lastTimestamp = now; + return { bytesReceived, bytesSent }; +} - return { - downloadSpeed: (downloadSpeed / 1024).toFixed(2), // KB/s - uploadSpeed: (uploadSpeed / 1024).toFixed(2), // KB/s - totalReceived: (bytesReceived / (1024 * 1024 * 1024)).toFixed(2), // GB - totalSent: (bytesSent / (1024 * 1024 * 1024)).toFixed(2), // GB - timestamp: now - }; +async function getWindowsStats() { + const networkStats = await si.networkStats(); + let bytesReceived = 0; + let bytesSent = 0; + + for (const stat of networkStats) { + bytesReceived += stat.rx_bytes || 0; + bytesSent += stat.tx_bytes || 0; + } + + return { bytesReceived, bytesSent }; +} + +async function getNetworkStats() { + try { + const platform = os.platform(); + let bytesReceived = 0; + let bytesSent = 0; + + if (platform === 'linux') { + const stats = await getLinuxStats(); + bytesReceived = stats.bytesReceived; + bytesSent = stats.bytesSent; + } else { + const stats = await getWindowsStats(); + bytesReceived = stats.bytesReceived; + bytesSent = stats.bytesSent; + } + + const now = Date.now(); + const timeDiff = (now - lastTimestamp) / 1000; + + const downloadSpeed = Math.max(0, (bytesReceived - lastBytesReceived) / timeDiff); + const uploadSpeed = Math.max(0, (bytesSent - lastBytesSent) / timeDiff); + + lastBytesReceived = bytesReceived; + lastBytesSent = bytesSent; + lastTimestamp = now; + + return { + downloadSpeed: (downloadSpeed / 1024).toFixed(2), + uploadSpeed: (uploadSpeed / 1024).toFixed(2), + totalReceived: (bytesReceived / (1024 * 1024 * 1024)).toFixed(2), + totalSent: (bytesSent / (1024 * 1024 * 1024)).toFixed(2), + timestamp: now + }; + } catch (error) { + console.error('获取网络统计信息失败:', error); + return { + downloadSpeed: "0", + uploadSpeed: "0", + totalReceived: "0", + totalSent: "0", + timestamp: Date.now() + }; + } } export async function GET() { try { - const stats = getNetworkStats(); + const stats = await getNetworkStats(); return new Response(JSON.stringify(stats), { status: 200, headers: { 'Content-Type': 'application/json' }, @@ -51,6 +95,6 @@ export async function GET() { return new Response(JSON.stringify({ error: error.message }), { status: 500, headers: { 'Content-Type': 'application/json' }, - }) + }); } } diff --git a/server/components/home/network.jsx b/server/components/home/network.jsx index b3335a5..4e632d6 100644 --- a/server/components/home/network.jsx +++ b/server/components/home/network.jsx @@ -116,8 +116,8 @@ export default function Network() { return (
总接收: {networkData.totalReceived} GB