const os = require('os'); const si = require('systeminformation'); const { formatBytes } = require("./util/convutil") const { exec } = require('child_process'); const { promisify } = require('util'); const fs = require('fs'); const path = require('path'); // 将 exec 函数转换为返回 Promise 的函数 const execPromise = promisify(exec); // 将 readFile 函数转换为返回 Promise 的函数 const readFilePromise = promisify(fs.readFile); // 获取操作系统信息 async function getOSInfo() { return { type: os.type(), platform: os.platform(), release: os.release(), arch: os.arch(), os: await getOSDescription() }; } // 获取 CPU 信息 function getCPUInfo() { const cpus = os.cpus(); const physicalCpuCount = require('physical-cpu-count') return { model: cpus[0].model, speed: cpus[0].speed, cores: cpus.length, physicalCores: physicalCpuCount }; } // 获取内存信息 function getMemoryInfo() { let info = { totalSize: os.totalmem(), freeSize: os.freemem() } return Object.assign(info, { total : formatBytes(info.totalSize), free : formatBytes(info.freeSize), }); } // 主函数,调用上述函数获取所有信息 async function getAllDeviceInfo() { const osInfo = await getOSInfo(); const cpuInfo = getCPUInfo(); const memoryInfo = getMemoryInfo(); const diskInfo = await si.fsSize(); return { os: osInfo, cpu: cpuInfo, memory: memoryInfo, disk: diskInfo }; } async function getOSDescription() { if (os.platform() === 'win32') { return undefined; } try { // 尝试执行 lsb_release 命令 const { stdout } = await execPromise('lsb_release -d'); // 提取描述信息 const distroDescription = stdout.split(':')[1].trim(); return distroDescription; } catch (lsbError) { try { // lsb_release 命令不可用,尝试读取 /etc/os-release 文件 const osReleasePath = path.join('/', 'etc', 'os-release'); const data = await readFilePromise(osReleasePath, 'utf8'); // 解析文件内容 const lines = data.split('\n'); const info = {}; lines.forEach(line => { const [key, value] = line.split('='); if (key && value) { info[key] = value.replace(/^"|"$/g, ''); } }); // 获取 PRETTY_NAME const prettyName = info['PRETTY_NAME']; if (prettyName) { return prettyName; } } catch (fileError) { console.error('读取 /etc/os-release 文件时出错:', fileError); } } return undefined; } module.exports = { getOSInfo, getCPUInfo, getMemoryInfo, getAllDeviceInfo };