From e2415210cd16a82766d50d6d65910295aca26ba6 Mon Sep 17 00:00:00 2001 From: stla <130404897+Stlara-F@users.noreply.github.com> Date: Sun, 21 Jun 2026 19:45:23 +0800 Subject: [PATCH] Enhance OS detection and update StatTile for distribution display (#115) * Add function to detect operating system distribution * Add 'distro' property to system types * Update subtext in StatTile to show distro instead of platform * Add Linux distribution detection and architecture normalization * Add archLabel to SystemInfo interface * Update subtext to use archLabel instead of arch * Add distro summarization function and update StatTile * Refactor distro summary to use tooltip for details * Refactor host system info detection functions --- packages/core/src/webui/server.ts | 76 ++++++++++++++++++- .../src/components/pages/overview-page.tsx | 17 ++++- packages/webui/src/types.ts | 2 + 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/packages/core/src/webui/server.ts b/packages/core/src/webui/server.ts index 674c09ae..337d6ff0 100644 --- a/packages/core/src/webui/server.ts +++ b/packages/core/src/webui/server.ts @@ -6,6 +6,7 @@ import { loadOneBotConfig, saveOneBotConfig } from '@snowluma/onebot/config'; import type { OneBotManager } from '@snowluma/onebot/manager'; import type { OneBotConfig, JsonObject as OneBotJsonObject } from '@snowluma/onebot/types'; import { readRuntimeConfig, updateRuntimeConfig, resolveRuntimeEnvOverrides } from '@snowluma/common/runtime'; +import { execSync } from 'child_process'; import { randomBytes } from 'crypto'; import { chmodSync, copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'fs'; import { createServer as createHttpsServer } from 'https'; @@ -118,6 +119,78 @@ async function fetchQqAvatar(uin: string): Promise<{ body: Uint8Array; contentTy return { body, contentType }; } +// ── Host system info (cached at module level, invariant over process lifetime) ── + +function detectDistro(): string { + try { + for (const f of ['/etc/os-release', '/usr/lib/os-release']) { + if (!existsSync(f)) continue; + const raw = readFileSync(f, 'utf8'); + const get = (k: string) => { const m = raw.match(new RegExp(`^${k}=("?)(.+?)\\1$`, 'm')); return m?.[2] ?? null; }; + const pretty = get('PRETTY_NAME') || get('NAME'); + const ver = get('VERSION_ID'); + if (pretty) return ver && !pretty.includes(ver) ? `${pretty} ${ver}` : pretty; + } + } catch { /* fall through */ } + + if (os.platform() === 'win32') { + try { + const productName = execSync( + 'reg query "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" /v ProductName', + { encoding: 'utf8', timeout: 3000, stdio: 'pipe' }, + ); + const m = productName.match(/ProductName\s+REG_SZ\s+(.+)/); + let name = m ? m[1].trim() : `Windows ${os.release()}`; + // ProductName is still "Windows 10 Pro" on Windows 11 — check build number. + try { + const buildOut = execSync( + 'reg query "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" /v CurrentBuildNumber', + { encoding: 'utf8', timeout: 3000, stdio: 'pipe' }, + ); + const bm = buildOut.match(/CurrentBuildNumber\s+REG_SZ\s+(\d+)/); + if (bm && parseInt(bm[1], 10) >= 22000) { + name = name.replace(/^Windows 10/, 'Windows 11'); + } + } catch { /* keep name as-is */ } + return name; + } catch { /* fall through */ } + return `Windows ${os.release()}`; + } + + // Linux fallback files + for (const [path, prefix] of [ + ['/etc/alpine-release', 'Alpine Linux '], + ['/etc/redhat-release', ''], + ['/etc/debian_version', 'Debian '], + ] as [string, string][]) { + try { + if (existsSync(path)) return prefix + readFileSync(path, 'utf8').trim(); + } catch { /* try next */ } + } + return os.platform(); +} + +function normalizeArch(arch: string): string { + const map: Record = { + loong64: 'LoongArch', + riscv64: 'RISC-V', + mips: 'MIPS', + mipsel: 'MIPS (LE)', + arm: 'ARM', + arm64: 'ARM64', + x64: 'x86_64', + ia32: 'x86', + s390: 'S/390', + s390x: 'S/390x', + ppc: 'PowerPC', + ppc64: 'PowerPC64', + }; + return map[arch] ?? arch; +} + +const CACHED_DISTRO = detectDistro(); +const CACHED_ARCH_LABEL = normalizeArch(os.arch()); + export async function initWebUI( desiredPort: number = 5099, oneBotManager: OneBotManager, @@ -413,7 +486,6 @@ export async function initWebUI( return c.json(await getUpdateInfo(force)); }); - // Host system info let lastCpuTimes: { idle: number; total: number }[] | null = null; function sampleCpuLoad(): number[] { const cpus = os.cpus(); @@ -448,7 +520,9 @@ export async function initWebUI( hostname: os.hostname(), platform: os.platform(), arch: os.arch(), + archLabel: CACHED_ARCH_LABEL, release: os.release(), + distro: CACHED_DISTRO, uptime: os.uptime(), processUptime: process.uptime(), nodeVersion: process.version, diff --git a/packages/webui/src/components/pages/overview-page.tsx b/packages/webui/src/components/pages/overview-page.tsx index 10eda4ca..d89730a7 100644 --- a/packages/webui/src/components/pages/overview-page.tsx +++ b/packages/webui/src/components/pages/overview-page.tsx @@ -10,6 +10,7 @@ import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Progress } from '@/components/ui/progress'; import { ScrollArea } from '@/components/ui/scroll-area'; +import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Skeleton } from '@/components/ui/skeleton'; import { cn, formatBytes, formatUptime } from '@/lib/utils'; @@ -379,6 +380,20 @@ function DropPlaceholder({ onAdd }: { onAdd: (id: string, x: number, y: number) // ─────────────── stat tiles (one widget each) ─────────────── +function shortDistro(distro: string): string { + let s = distro; + s = s.replace(/^Debian GNU\/Linux /, 'Debian '); + s = s.replace(/^Red Hat Enterprise Linux /, 'RHEL '); + s = s.replace(/^CentOS (?:Linux )?release /, 'CentOS '); + s = s.replace(/^SUSE Linux Enterprise (?:Server |Desktop )?/, 'SUSE '); + s = s.replace(/^Kali GNU\/Linux /, 'Kali '); + s = s.replace(/^Windows Server /, 'Win Svr '); + s = s.replace(/^Windows (\d+)/, 'Win $1'); + s = s.replace(/\s*\([^)]+\)/g, ''); + s = s.replace(/\s{2,}/g, ' '); + return s.trim(); +} + function StatTile({ icon, label, value, subtext, accent = false, to, }: { @@ -461,7 +476,7 @@ function StatTileWidget({ id }: { id: string }) { icon={} label="主机名" value={systemInfo?.hostname ?? '—'} - subtext={systemInfo ? `${systemInfo.platform} · ${systemInfo.arch}` : '加载中'} + subtext={systemInfo ? {shortDistro(systemInfo.distro)} · {systemInfo.archLabel}{systemInfo.distro} · {systemInfo.archLabel} : '加载中'} /> ); case 'stat:uptime': diff --git a/packages/webui/src/types.ts b/packages/webui/src/types.ts index 79253613..90d4fc97 100644 --- a/packages/webui/src/types.ts +++ b/packages/webui/src/types.ts @@ -203,7 +203,9 @@ export interface SystemInfo { hostname: string; platform: string; arch: string; + archLabel: string; release: string; + distro: string; uptime: number; processUptime: number; nodeVersion: string;