fix:docker容器正确获取宿主机系统信息 (#119)

* 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

* Enhance Linux distribution detection logic

* Improve Linux distro detection and version parsing

Enhanced Linux distribution detection logic by adding kernel version parsing and improved source handling for OS release information.

* Enhance kernel and distro version detection on Linux

Refactor kernel version extraction and improve distro version parsing logic.

* Enhance RHEL family detection and error handling

* Enhance distro name formatting in overview-page

* Extend RHEL family detection and supported distros

* Add support for additional Linux distributions

* Enhance OS detection for Armbian and DietPi

Added support for detecting Armbian and DietPi distributions from kernel release strings.

* Add support for Raspbian and DietPi in OS name formatting
This commit is contained in:
stla
2026-06-22 01:39:11 +08:00
committed by GitHub
parent 9efe16237d
commit 907cfddfa3
2 changed files with 153 additions and 22 deletions
+138 -21
View File
@@ -122,17 +122,143 @@ async function fetchQqAvatar(uin: string): Promise<{ body: Uint8Array; contentTy
// ── 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 */ }
// Helper: extract major.minor.patch from a kernel version string
const parseKernel = (v: string): string | null => { const m = v.match(/(\d+\.\d+\.\d+)/); return m?.[1] ?? null; };
// ── Linux ──────────────────────────────────────────────────────────
if (os.platform() === 'linux') {
const kernelRelease = os.release();
const kernelVer = parseKernel(kernelRelease);
// Normalize RHEL-family distro names before comparison.
// /proc/version typically shows "Red Hat" (GCC build tag) even on
// Rocky / Alma / Oracle Linux, while /etc/os-release carries the
// actual distro name. Treat the whole family as a single group so
// they are never considered "disagreeing".
const isRhelFamily = (name: string): boolean =>
/^(red hat|centos|rocky|alma|oracle|scientific|anolis|tencentos|bclinux|opencloudos)/.test(name);
// Extract distro version from kernel release string.
// Distros embed their version in the kernel ABI / build tag:
// Debian: "6.8.12-1-deb13-amd64" → 13
// RHEL family: "4.18.0-513.el9.x86_64" → 9
// Fedora: "6.8.12-300.fc40.x86_64" → 40
// Amazon Linux: "6.1.158-178.288.amzn2023" → 2023
// Mageia: "6.8.12-1.mga10" → 10
const kernelDistroVer = (distro: string | null): string | null => {
if (!distro) return null;
const lr = kernelRelease.toLowerCase();
const ld = distro.toLowerCase();
if (ld === 'debian') { const m = lr.match(/deb(\d+)/); if (m) return m[1]; }
if (isRhelFamily(ld)) { const m = lr.match(/el(\d+)/); if (m) return m[1]; }
if (ld === 'fedora') { const m = lr.match(/fc(\d+)/); if (m) return m[1]; }
if (ld === 'amazon' || ld.includes('amazon')) { const m = lr.match(/amzn(\d+)/); if (m) return m[1]; }
if (ld === 'mageia') { const m = lr.match(/mga(\d+)/); if (m) return m[1]; }
if (ld === 'armbian') { const m = lr.match(/armbian(\d+)/); if (m) return m[1]; }
if (ld === 'dietpi') { const m = lr.match(/dietpi(\d+)/); if (m) return m[1]; }
if (ld.includes('libreelec')) { const m = lr.match(/libreelec(\d+)/); if (m) return m[1]; }
if (ld.includes('coreelec')) { const m = lr.match(/coreelec(\d+)/); if (m) return m[1]; }
return null;
};
// Source A: /proc/version (host kernel build, crosses container boundary)
let hostName: string | null = null;
try {
const raw = readFileSync('/proc/version', 'utf8').trim();
const vm = raw.match(/^Linux version\s+(\S+)/);
if (vm) {
// Step 1: kernel release string — embedded distros embed their name
// here (e.g. "6.6.16-armbian", "6.1.60-dietpi"). More reliable than
// the GCC build tag, which often shows the cross-compilation toolchain
// (e.g. "Ubuntu" for Armbian / DietPi) rather than the actual OS.
const releaseStr = vm[1].toLowerCase();
const releaseNameMatch = releaseStr.match(/(armbian|dietpi|libreelec|coreelec)/);
if (releaseNameMatch) {
const nameMap: Record<string, string> = {
armbian: 'Armbian',
dietpi: 'DietPi',
libreelec: 'LibreELEC',
coreelec: 'CoreELEC',
};
hostName = nameMap[releaseNameMatch[1]] ?? releaseNameMatch[1];
} else {
// Step 2: GCC build tag — matches the distribution that compiled
// the running kernel (e.g. "(Ubuntu ...)", "(Debian ...)",
// "(Red Hat ...)").
const dm = raw.match(/\b(Debian|Ubuntu|Red Hat|CentOS|Fedora|Alpine|Arch|Gentoo|SUSE|Proxmox|OpenWrt|Deepin|Kylin|openEuler|Anolis|UOS|Linux Mint|Slackware|Manjaro|NixOS|Void|Mageia|Kali|Amazon|Solus|Alibaba|Armbian|DietPi|Raspbian)\b/i);
hostName = dm ? dm[1] : null;
}
}
} catch { /* source A unavailable */ }
// Source B: /etc/os-release (container / local OS)
let osReleaseName: string | null = null;
let osReleaseVer: string | null = null;
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) {
const nm = pretty.match(/^([^0-9]+)/);
osReleaseName = nm ? nm[1].trim() : pretty;
osReleaseVer = ver;
break;
}
}
} catch { /* source B unavailable */ }
// Decide which source to trust for the distro name & version
let finalName: string;
let finalVer: string | null;
if (hostName && osReleaseName) {
// Normalize and compare: e.g. "Debian" vs "Debian GNU/Linux" → match.
// RHEL family members (Red Hat, CentOS, Rocky, Alma, Oracle, Scientific)
// are normalised to the same group — /proc/version usually shows "Red Hat"
// (GCC build tag) even when the actual distro is Rocky / Alma / Oracle.
const a = hostName.toLowerCase();
const b = osReleaseName.toLowerCase();
if (a.includes(b) || b.includes(a) || (isRhelFamily(a) && isRhelFamily(b))) {
// Agree → prefer version from kernel release, fall back to os-release
finalName = osReleaseName;
finalVer = kernelDistroVer(hostName) ?? osReleaseVer;
} else {
// Disagree → host kernel build wins (crosses container boundary)
finalName = hostName;
finalVer = kernelDistroVer(hostName);
}
} else if (hostName) {
finalName = hostName;
finalVer = kernelDistroVer(hostName);
} else if (osReleaseName) {
finalName = osReleaseName;
finalVer = kernelDistroVer(osReleaseName) ?? osReleaseVer;
} else {
// Source C: legacy release 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)) {
const raw = prefix + readFileSync(path, 'utf8').trim();
return kernelVer ? `${raw} (kernel ${kernelVer})` : raw;
}
} catch { /* try next */ }
}
return kernelVer ? `Linux (kernel ${kernelVer})` : 'Linux';
}
// Unified output: <Name> <version> (kernel <kernel>)
const base = finalVer ? `${finalName} ${finalVer}` : finalName;
return kernelVer ? `${base} (kernel ${kernelVer})` : base;
}
// ── Windows ────────────────────────────────────────────────────────
if (os.platform() === 'win32') {
try {
const productName = execSync(
@@ -157,16 +283,6 @@ function detectDistro(): string {
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();
}
@@ -184,11 +300,12 @@ function normalizeArch(arch: string): string {
s390x: 'S/390x',
ppc: 'PowerPC',
ppc64: 'PowerPC64',
ppc64le: 'PowerPC64 (LE)',
};
return map[arch] ?? arch;
}
const CACHED_DISTRO = detectDistro();
const CACHED_DISTRO = (() => { try { return detectDistro(); } catch { return os.platform(); } })();
const CACHED_ARCH_LABEL = normalizeArch(os.arch());
export async function initWebUI(
@@ -385,8 +385,22 @@ function shortDistro(distro: string): string {
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(/^Rocky Linux /, 'Rocky ');
s = s.replace(/^Alma(?:Linux)? /, 'Alma ');
s = s.replace(/^Oracle Linux /, 'Oracle ');
s = s.replace(/^Amazon Linux /, 'Amazon ');
s = s.replace(/^Scientific Linux /, 'Scientific ');
s = s.replace(/^SUSE Linux Enterprise (?:Server |Desktop )?/, 'SUSE ');
s = s.replace(/^Anolis OS /, 'Anolis ');
s = s.replace(/^TencentOS Server /, 'TencentOS ');
s = s.replace(/^Ubuntu Kylin /, 'Ubuntu ');
s = s.replace(/^Manjaro Linux /, 'Manjaro ');
s = s.replace(/^Void Linux /, 'Void ');
s = s.replace(/^Alibaba Cloud Linux /, 'Alibaba ');
s = s.replace(/^Raspbian GNU\/Linux /, 'Raspbian ');
s = s.replace(/^DietPi v /, 'DietPi ');
s = s.replace(/^Kali GNU\/Linux /, 'Kali ');
s = s.replace(/^Proxmox VE /, 'Proxmox ');
s = s.replace(/^Windows Server /, 'Win Svr ');
s = s.replace(/^Windows (\d+)/, 'Win $1');
s = s.replace(/\s*\([^)]+\)/g, '');
@@ -476,7 +490,7 @@ function StatTileWidget({ id }: { id: string }) {
icon={<Server className="size-5" />}
label="主机名"
value={systemInfo?.hostname ?? '—'}
subtext={systemInfo ? <Tooltip><TooltipTrigger className="truncate text-left">{shortDistro(systemInfo.distro)} · {systemInfo.archLabel}</TooltipTrigger><TooltipContent>{systemInfo.distro} · {systemInfo.archLabel}</TooltipContent></Tooltip> : '加载中'}
subtext={systemInfo ? <Tooltip><TooltipTrigger className="block truncate text-left min-w-0">{shortDistro(systemInfo.distro)} · {systemInfo.archLabel}</TooltipTrigger><TooltipContent>{systemInfo.distro} · {systemInfo.archLabel}</TooltipContent></Tooltip> : '加载中'}
/>
);
case 'stat:uptime':