CH-J Server Managerserverbeheer over SSH
Menu
Gepubliceerde bron

CH-J Server Manager

Blader door mappen en bestanden voor een specifieke toepassing release.

Download bron ZIP
CH-J / Server Manager 377 bestanden
core-20261019T111616Z-160a00bfcddc Toegevoegd: · Gebouwd:
platformDetect.js Toegevoegd: Datum niet geregistreerd 4,5 KB · 76 regels
Rubriek Bestand downloaden
1"use strict";
3const { encodedCommand, parseWindowsOutput } = require("./windowsPrivilegedExec");
5// Detects the remote operating system from system facts, never from prompts or host names.
6// 1) "uname -s" identifies POSIX systems (Linux, BSD, macOS).
7// 2) Otherwise a self-contained encoded PowerShell command reads the Windows version (registry, plus
8// Win32_OperatingSystem when WMI is permitted) and the OpenSSH DefaultShell registry value; it works whether
9// the default shell is cmd.exe, powershell.exe or pwsh.
10const POSIX_KERNELS = /^(Linux|Darwin|FreeBSD|OpenBSD|NetBSD|DragonFly|SunOS|AIX)\b/;
11const WINDOWS_PROBE = String.raw`$ProgressPreference='SilentlyContinue'
12try {
13 # Registry first: standard accounts on an SSH network logon are usually denied WMI/CIM access.
14 $nt = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -ErrorAction SilentlyContinue
15 $shell = (Get-ItemProperty 'HKLM:\SOFTWARE\OpenSSH' -ErrorAction SilentlyContinue).DefaultShell
16 $caption = [string]$nt.ProductName; $build = [string]$nt.CurrentBuildNumber
17 if ($caption -match '^Windows 10' -and [int]("0$build") -ge 22000) { $caption = $caption -replace '^Windows 10', 'Windows 11' }
18 $type = [string](Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ProductOptions' -ErrorAction SilentlyContinue).ProductType
19 $productType = @{ 'WinNT' = 1; 'LanmanNT' = 2; 'ServerNT' = 3 }[$type]
20 $version = [Environment]::OSVersion.Version.ToString()
21 try { $os = Get-CimInstance Win32_OperatingSystem -ErrorAction Stop; $caption = $os.Caption; $version = $os.Version; $productType = [int]$os.ProductType } catch {}
22 $value = [ordered]@{ caption = $caption; version = $version; build = $build; productType = $productType
23 installationType = [string]$nt.InstallationType; defaultShell = [string]$shell; psVersion = $PSVersionTable.PSVersion.ToString()
24 is64BitProcess = [Environment]::Is64BitProcess; languageMode = [string]$ExecutionContext.SessionState.LanguageMode
25 localAccounts = [bool](Get-Command Get-LocalUser -ErrorAction SilentlyContinue); pwsh = [string](Get-Command pwsh.exe -ErrorAction SilentlyContinue).Source }
26 [Console]::Out.WriteLine('CHJ_RESULT:' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(($value | ConvertTo-Json -Compress))))
27} catch { [Console]::Out.WriteLine('CHJ_ERR:PLATFORM_PROBE_FAILED:' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($_.Exception.Message))) }`;
29function shellName(defaultShell) {
30 const value = String(defaultShell || "").toLowerCase();
31 if (!value) return "cmd.exe";
32 if (/pwsh(\.exe)?$/.test(value)) return "pwsh";
33 if (/powershell(\.exe)?$/.test(value)) return "powershell";
34 if (/cmd(\.exe)?$/.test(value)) return "cmd.exe";
35 if (/bash(\.exe)?$/.test(value)) return "bash";
36 return value.split(/[\\/]/).pop();
39async function detectPlatform(sessionManager, sessionId, { refresh = false } = {}) {
40 const record = sessionManager._requireConnected(sessionId);
41 if (record.platform && !refresh) return record.platform;
42 const uname = await sessionManager._execStream(record, "uname -s", { timeoutMs: 15000 }).done.catch(() => null);
43 const kernel = String(uname?.stdout || "").trim().split(/\r?\n/)[0] || "";
44 if (uname?.code === 0 && POSIX_KERNELS.test(kernel)) {
45 record.platform = { os: kernel.startsWith("Linux") ? "linux" : "posix", kernel, detectedBy: "uname" };
46 return record.platform;
47 }
48 const probe = await sessionManager._execStream(record, encodedCommand(WINDOWS_PROBE), { timeoutMs: 60000 }).done;
49 let info;
50 try { info = parseWindowsOutput(probe.stdout, probe.stderr, probe.code, "PLATFORM_"); }
51 catch (error) {
52 record.platform = { os: "unknown", kernel, detectedBy: "none", reason: error.message };
53 return record.platform;
54 }
55 record.platform = {
56 os: "windows",
57 detectedBy: "Win32_OperatingSystem",
58 caption: String(info.caption || ""),
59 version: String(info.version || ""),
60 build: String(info.build || ""),
61 server: Number(info.productType) !== 1,
62 serverCore: /server core/i.test(String(info.installationType || "")),
63 installationType: String(info.installationType || ""),
64 defaultShell: shellName(info.defaultShell),
65 defaultShellPath: String(info.defaultShell || ""),
66 powershell: String(info.psVersion || ""),
67 is64BitProcess: info.is64BitProcess === true,
68 languageMode: String(info.languageMode || ""),
69 localAccounts: info.localAccounts === true,
70 pwsh: String(info.pwsh || ""),
71 unameOutput: kernel
72 };
73 return record.platform;
76module.exports = { WINDOWS_PROBE, detectPlatform, shellName };
SHA-256c7aca0b0fd5dd2aba1f5d128f3c297f49e9678516f6685325a843442f8d4bf3a

CH-J Proprietary Software License 1.14 · Bron wordt geleverd onder de CH-J Proprietary Software License 1.14. De beschikbaarheid verandert de licentievoorwaarden niet of verleent geen extra rechten.

Archief SHA-256a9605c0870a223b6a8542bb0705edd51d8b04f3069582653417a8397fabcc09a
Bestanden