公開済みのソース
ソースの ZIP をダウンロード
CH-J Tools
特定のアプリケーションリリースのディレクトリとファイルを閲覧できます。
CH-J / Server Manager
406 ファイル
core-20261024T135205Z-7c487d719de2
追加日時: · ビルド日時:
1
"use strict";3
const crypto = require("node:crypto");4
const fs = require("node:fs");5
const dns = require("node:dns").promises;6
const net = require("node:net");7
const { EventEmitter } = require("node:events");8
const { StringDecoder } = require("node:string_decoder");9
const { performance } = require("node:perf_hooks");10
const { Client, utils: sshUtils } = require("ssh2");11
const path = require("node:path");12
const { LatencyMonitor } = require("./latencyMonitor");13
const REMOTE_EDITOR = fs.readFileSync(path.join(__dirname, "../files/remoteEditor.py"), "utf8");14
const EDITOR_STARTED = "\x1eCHJ_EDITOR_START\n";15
// Privileged helper runs go through the shared PrivilegedExecutor protocol (sudo prompt marker,16
// READY, then the request as a stdin frame) instead of a separate sudo implementation.17
const EDITOR_PRIVILEGED_SCRIPT = String.raw`PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin; export PATH18
printf '\036CHJ_READY\n'19
command -v python3 >/dev/null 2>&1 || { printf '\036CHJ_ERR=DEPENDENCY_UNAVAILABLE\n'; exit 127; }20
printf '\036CHJ_EDITOR_START\n'21
exec python3 -I -B -c "$1"`;22
// Operations whose effect is unknown once the request was delivered but the reply was lost.23
const EDITOR_MUTATIONS = new Set(["finalize", "publish", "rename", "remove", "mkdir", "create", "cleanup", "stage-out", "transfer-remove"]);24
const EDITOR_OUTPUT_LIMIT = 36 * 1024 * 1024;25
const WINDOWS_HASH_SCRIPT = String.raw`param([string]$ArgsJson)26
$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'27
$A = $ArgsJson | ConvertFrom-Json28
$p = [string]$A.path29
if ($p -match '^/[A-Za-z]:') { $p = $p.Substring(1) }30
$h = (Get-FileHash -Algorithm SHA256 -LiteralPath $p).Hash.ToLowerInvariant()31
[Console]::Out.WriteLine('CHJ_RESULT:' + [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{ hash = $h } -Compress))))`;33
const SESSION_ID_PATTERN = /^[A-Za-z0-9._-]{1,80}$/;34
const MAX_INPUT_LENGTH = 64 * 1024;35
const MAX_KEY_BYTES = 2 * 1024 * 1024;36
const MAX_COMMAND_OUTPUT = 512 * 1024;37
const MAX_NGINX_CONFIG_BYTES = 512 * 1024;38
const USERNAME_PATTERN = /^[a-z_][a-z0-9_-]{0,31}$/;39
const METRICS_COMMAND = String.raw`LC_ALL=C40
platform=$(uname -s 2>/dev/null || echo unknown)41
kernel=$(uname -r 2>/dev/null || echo unknown)42
host=$(hostname 2>/dev/null || echo unknown)43
cpu_arch=$(uname -m 2>/dev/null || echo unknown)44
cpu_usage=045
cpu_logical=046
cpu_physical=047
cpu_sockets=048
cpu_model=unknown49
cpu_core_types=50
network_default=52
if [ -r /proc/uptime ]; then53
uptime_s=$(awk '{print int($1)}' /proc/uptime)54
else55
boot_epoch=$(sysctl -n kern.boottime 2>/dev/null | awk -F'[=,]' '{gsub(/[^0-9]/,"",$2); print $2}')56
now_epoch=$(date +%s 2>/dev/null || echo 0)57
if [ -n "$boot_epoch" ] && [ "$now_epoch" -ge "$boot_epoch" ] 2>/dev/null; then uptime_s=$((now_epoch-boot_epoch)); else uptime_s=0; fi58
fi59
if [ -r /proc/loadavg ]; then load1=$(awk '{print $1}' /proc/loadavg); else load1=$(uptime | sed -E 's/.*load averages?:[[:space:]]*([0-9.]+).*/\1/'); fi61
if [ -r /proc/meminfo ]; then62
mem_total=$(awk '/^MemTotal:/{print $2*1024}' /proc/meminfo)63
mem_avail=$(awk '/^MemAvailable:/{print $2*1024}' /proc/meminfo)64
swap_total=$(awk '/^SwapTotal:/{print $2*1024}' /proc/meminfo)65
swap_free=$(awk '/^SwapFree:/{print $2*1024}' /proc/meminfo)66
cpu_first=$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=9 && i<=NF;i++)total+=$i; printf "%.0f:%.0f",idle,total; exit}' /proc/stat)67
sleep 0.268
cpu_second=$(awk '/^cpu /{idle=$5+$6; total=0; for(i=2;i<=9 && i<=NF;i++)total+=$i; printf "%.0f:%.0f",idle,total; exit}' /proc/stat)69
cpu_usage=$(awk -v first="$cpu_first" -v second="$cpu_second" 'BEGIN{split(first,a,":");split(second,b,":");total=b[2]-a[2];idle=b[1]-a[1];if(total>0)printf "%.2f",100*(total-idle)/total;else print 0}')70
cpu_logical=$(getconf _NPROCESSORS_ONLN 2>/dev/null || nproc 2>/dev/null || awk '/^processor[[:space:]]*:/{n++}END{print n+0}' /proc/cpuinfo)71
cpu_model=$(awk -F: '/^(model name|Processor|Hardware)[[:space:]]*:/{gsub(/^[[:space:]]+|[[:space:]]+$/,"",$2);if($2!=""){print $2;exit}}' /proc/cpuinfo)72
if command -v lscpu >/dev/null 2>&1; then73
cpu_physical=$(lscpu -p=Core,Socket 2>/dev/null | awk -F, '!/^#/&&$1~/^[0-9]+$/{seen[$1":"$2]=1}END{for(k in seen)n++;print n+0}')74
cpu_sockets=$(lscpu -p=Socket 2>/dev/null | awk -F, '!/^#/&&$1~/^[0-9]+$/{seen[$1]=1}END{for(k in seen)n++;print n+0}')75
fi76
[ "$cpu_physical" -gt 0 ] 2>/dev/null || cpu_physical=$cpu_logical77
[ "$cpu_sockets" -gt 0 ] 2>/dev/null || cpu_sockets=178
if ls /sys/devices/system/cpu/cpu*/topology/core_type >/dev/null 2>&1; then79
core_type_records=$(for type_file in /sys/devices/system/cpu/cpu*/topology/core_type; do topology=$(dirname "$type_file"); type=$(cat "$type_file" 2>/dev/null); core=$(cat "$topology/core_id" 2>/dev/null); package=$(cat "$topology/physical_package_id" 2>/dev/null); printf '%s:%s:%s\n' "$type" "$package" "$core"; done | sort -u)80
efficiency_cores=$(printf '%s\n' "$core_type_records" | awk -F: '$1==1{n++}END{print n+0}')81
performance_cores=$(printf '%s\n' "$core_type_records" | awk -F: '$1==2{n++}END{print n+0}')82
[ "$performance_cores" -gt 0 ] && cpu_core_types="P-core:$performance_cores"83
if [ "$efficiency_cores" -gt 0 ]; then [ -n "$cpu_core_types" ] && separator=, || separator=; cpu_core_types=$(printf '%s%sE-core:%s' "$cpu_core_types" "$separator" "$efficiency_cores"); fi84
fi85
if command -v ip >/dev/null 2>&1; then network_default=$(ip route show default 2>/dev/null | awk '{for(i=1;i<=NF;i++)if($i=="dev"){print $(i+1);exit}}'); fi86
else87
mem_total=$(sysctl -n hw.memsize 2>/dev/null || echo 0)88
page_size=$(vm_stat 2>/dev/null | awk '/page size of/{print $8}')89
[ -n "$page_size" ] || page_size=409690
mem_avail=$(vm_stat 2>/dev/null | awk -v page="$page_size" '/Pages (free|inactive|speculative):/{gsub(/\./,"",$3);pages+=$3}END{printf "%.0f",pages*page}')91
swap_total=092
swap_free=093
cpu_logical=$(sysctl -n hw.logicalcpu 2>/dev/null || echo 0)94
cpu_physical=$(sysctl -n hw.physicalcpu 2>/dev/null || echo "$cpu_logical")95
cpu_sockets=196
cpu_model=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || sysctl -n hw.model 2>/dev/null || echo unknown)97
cpu_usage=$(top -l 1 -n 0 2>/dev/null | awk '/CPU usage:/{idle=$7;gsub(/%/,"",idle)}END{if(idle!="")printf "%.2f",100-idle;else print 0}')98
for level in 0 1 2; do99
count=$(sysctl -n "hw.perflevel$level.physicalcpu" 2>/dev/null || echo 0)100
if [ "$count" -gt 0 ] 2>/dev/null; then101
name=$(sysctl -n "hw.perflevel$level.name" 2>/dev/null || echo "Cluster $level")102
[ -n "$cpu_core_types" ] && separator=, || separator=103
cpu_core_types=$(printf '%s%s%s:%s' "$cpu_core_types" "$separator" "$name" "$count")104
fi105
done106
network_default=$(route -n get default 2>/dev/null | awk '/interface:/{print $2;exit}')107
fi109
[ -n "$cpu_model" ] || cpu_model=unknown110
cpu_model=$(printf '%s' "$cpu_model" | tr '\r\n|\036' ' ')111
cpu_core_types=$(printf '%s' "$cpu_core_types" | tr '\r\n|\036' ' ')112
disk=$(df -Pk / 2>/dev/null | awk 'NR==2{print $2*1024":"$3*1024":"$4*1024}')113
printf 'platform=%s\nkernel=%s\nhostname=%s\nuptimeSeconds=%s\nload1=%s\nmemoryTotal=%s\nmemoryAvailable=%s\nswapTotal=%s\nswapFree=%s\ndisk=%s\ncpuUsage=%s\ncpuArchitecture=%s\ncpuModel=%s\ncpuLogical=%s\ncpuPhysical=%s\ncpuSockets=%s\ncpuCoreTypes=%s\nnetworkDefault=%s\n' "$platform" "$kernel" "$host" "$uptime_s" "$load1" "$mem_total" "$mem_avail" "$swap_total" "$swap_free" "$disk" "$cpu_usage" "$cpu_arch" "$cpu_model" "$cpu_logical" "$cpu_physical" "$cpu_sockets" "$cpu_core_types" "$network_default"115
# Detailed memory and swap for the "Memory and virtual memory" section (kB fields converted to bytes).116
if [ -r /proc/meminfo ]; then117
awk '$1 ~ /^(MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapCached|Active|Inactive|Slab|SReclaimable|SUnreclaim|Shmem|KernelStack|PageTables|VmallocUsed|Percpu|Dirty|Writeback|SwapTotal|SwapFree|Zswap|Zswapped|CommitLimit|Committed_AS):$/ { key=$1; sub(/:$/, "", key); value=($3=="kB") ? $2*1024 : $2; printf "meminfo=%s|%.0f\n", key, value }' /proc/meminfo118
fi119
if [ -r /proc/swaps ]; then120
awk 'NR>1 { printf "swapArea=%s|%s|%.0f|%.0f|%s\n", $1, $2, $3*1024, $4*1024, $5 }' /proc/swaps121
fi122
if [ -r /proc/vmstat ]; then123
awk '$1=="pswpin" || $1=="pswpout" || $1=="pgmajfault" { printf "vmstat=%s|%s\n", $1, $2 }' /proc/vmstat124
fi125
for zram in /sys/block/zram*; do126
[ -r "$zram/mm_stat" ] || continue127
printf 'zram=%s|%s|%s|%s\n' "$(basename "$zram")" "$(cat "$zram/disksize" 2>/dev/null)" "$(cat "$zram/mm_stat" 2>/dev/null | tr -s ' ' ',')" "$(cat "$zram/comp_algorithm" 2>/dev/null | sed -n 's/.*\[\([^]]*\)\].*/\1/p')"128
done129
if [ -r /sys/module/zswap/parameters/enabled ]; then130
printf 'zswap=%s|%s|%s\n' "$(cat /sys/module/zswap/parameters/enabled)" "$(cat /sys/module/zswap/parameters/compressor 2>/dev/null)" "$(cat /sys/module/zswap/parameters/max_pool_percent 2>/dev/null)"131
fi132
printf 'pageSize=%s\n' "$(getconf PAGESIZE 2>/dev/null || echo 4096)"134
if [ -d /sys/class/net ]; then135
for iface_path in /sys/class/net/*; do136
[ -e "$iface_path" ] || continue137
iface=$(basename "$iface_path")138
[ "$iface" = lo ] && continue139
state=$(cat "$iface_path/operstate" 2>/dev/null || echo unknown)140
mac=$(cat "$iface_path/address" 2>/dev/null || true)141
rx=$(cat "$iface_path/statistics/rx_bytes" 2>/dev/null || echo 0)142
tx=$(cat "$iface_path/statistics/tx_bytes" 2>/dev/null || echo 0)143
if command -v ip >/dev/null 2>&1; then addresses=$(ip -o addr show dev "$iface" scope global 2>/dev/null | awk '{print $4}' | paste -sd, -); else addresses=; fi144
printf 'networkInterface=%s|%s|%s|%s|%s|%s\n' "$iface" "$state" "$mac" "$rx" "$tx" "$addresses"145
done146
elif command -v ifconfig >/dev/null 2>&1; then147
for iface in $(ifconfig -l 2>/dev/null); do148
[ "$iface" = lo0 ] && continue149
state=$(ifconfig "$iface" 2>/dev/null | awk '/status:/{print $2;exit}')150
mac=$(ifconfig "$iface" 2>/dev/null | awk '/ether /{print $2;exit}')151
addresses=$(ifconfig "$iface" 2>/dev/null | awk '/^[[:space:]]+inet /{print $2}/^[[:space:]]+inet6 /{print $2}' | paste -sd, -)152
counters=$(netstat -ibn -I "$iface" 2>/dev/null | awk 'NR==2{print $7":"$10;exit}')153
rx=$(printf '%s' "$counters" | cut -d: -f1); tx=$(printf '%s' "$counters" | cut -d: -f2)154
[ -n "$state" ] || state=unknown; [ -n "$rx" ] || rx=0; [ -n "$tx" ] || tx=0155
printf 'networkInterface=%s|%s|%s|%s|%s|%s\n' "$iface" "$state" "$mac" "$rx" "$tx" "$addresses"156
done157
fi`;158
const NGINX_INSPECT_COMMAND = String.raw`LC_ALL=C159
nginx_bin=$(command -v nginx 2>/dev/null || true)160
if [ -z "$nginx_bin" ]; then printf 'installed=0\n'; exit 0; fi161
printf 'installed=1\n'162
printf 'binary=%s\n' "$nginx_bin"163
version=$($nginx_bin -v 2>&1 | sed 's/^nginx version: //')164
build=$($nginx_bin -V 2>&1 | tr '\n' ' ')165
conf_path=$(printf '%s' "$build" | sed -n 's/.*--conf-path=\([^ ]*\).*/\1/p')166
[ -n "$conf_path" ] || conf_path=/etc/nginx/nginx.conf167
if command -v systemctl >/dev/null 2>&1; then service_state=$(systemctl is-active nginx 2>/dev/null || true); else service_state=$(pgrep -x nginx >/dev/null 2>&1 && echo active || echo inactive); fi168
[ -n "$service_state" ] || service_state=unknown169
printf 'version=%s\nconfigPath=%s\nserviceState=%s\n' "$version" "$conf_path" "$service_state"170
for directory in /etc/nginx /etc/nginx/conf.d /etc/nginx/snippets /etc/nginx/sites-available /etc/nginx/sites-enabled /etc/nginx/modules-available /etc/nginx/modules-enabled /etc/nginx/stream-conf.d /usr/local/etc/nginx /usr/local/etc/nginx/conf.d /opt/homebrew/etc/nginx /opt/homebrew/etc/nginx/servers; do171
[ -d "$directory" ] || continue172
find "$directory" -maxdepth 1 \( -type f -o -type l \) ! -name '*.chj-backup-*' -print 2>/dev/null | while IFS= read -r file; do173
[ -L "$file" ] && kind=symlink || kind=file174
printf 'config=%s|%s\n' "$kind" "$file"175
done176
done`;178
const NGINX_CONFIG_PATH_PATTERN = /^\/(?:etc\/nginx\/(?:nginx\.conf|(?:conf\.d|snippets|modules-(?:available|enabled)|stream-conf\.d)\/[A-Za-z0-9][A-Za-z0-9._-]{0,127}|sites-(?:available|enabled)\/[A-Za-z0-9][A-Za-z0-9._-]{0,127})|usr\/local\/etc\/nginx\/(?:nginx\.conf|conf\.d\/[A-Za-z0-9][A-Za-z0-9._-]{0,127})|opt\/homebrew\/etc\/nginx\/(?:nginx\.conf|servers\/[A-Za-z0-9][A-Za-z0-9._-]{0,127}))$/;180
function normalizeNginxConfigPath(value) {181
const path = String(value || "").trim();182
if (!NGINX_CONFIG_PATH_PATTERN.test(path) || path.includes(".chj-backup-")) {183
throw new SessionError("NGINX_CONFIG_PATH_INVALID", "The NGINX configuration path is outside the managed directories.");184
}185
return path;186
}188
function shellQuote(value) {189
return `'${String(value).replace(/'/g, `'"'"'`)}'`;190
}192
function openExec(client, command, callback) {193
try { client.exec(command, callback); }194
catch (error) { callback(error); }195
}197
// SHA256 fingerprint of the session's public key, used only to refuse removing the key in use.198
function publicKeyFingerprintForPrivateKey(privateKeyPath) {199
let blob = null;200
try {201
const parsed = sshUtils.parseKey(fs.readFileSync(privateKeyPath));202
if (!(parsed instanceof Error)) blob = (Array.isArray(parsed) ? parsed[0] : parsed).getPublicSSH();203
} catch {}204
if (!blob) {205
try { blob = Buffer.from(fs.readFileSync(`${privateKeyPath}.pub`, "utf8").trim().split(/\s+/)[1] || "", "base64"); } catch {}206
}207
return blob?.length ? `SHA256:${crypto.createHash("sha256").update(blob).digest("base64").replace(/=+$/, "")}` : null;208
}210
function normalizeSudoPassword(value) {211
const password = String(value || "");212
if (password.length > 1024 || /[\0\r\n]/.test(password)) throw new SessionError("SUDO_PASSWORD_INVALID", "Invalid sudo password.");213
return password;214
}216
function parseNginxInspectionOutput(stdout) {217
const result = { installed: false, binary: "", version: "", configPath: "", serviceState: "unknown", configs: [] };218
const seen = new Set();219
for (const line of String(stdout || "").split(/\r?\n/)) {220
if (line.startsWith("config=")) {221
const separator = line.indexOf("|", 7);222
if (separator < 0) continue;223
const kind = line.slice(7, separator) === "symlink" ? "symlink" : "file";224
try {225
const path = normalizeNginxConfigPath(line.slice(separator + 1));226
if (!seen.has(path)) { seen.add(path); result.configs.push({ path, kind, writable: kind === "file" }); }227
} catch {}228
continue;229
}230
const separator = line.indexOf("=");231
if (separator < 1) continue;232
const key = line.slice(0, separator);233
const value = line.slice(separator + 1).replace(/[\x00-\x1f\x7f]/g, " ").trim().slice(0, 512);234
if (key === "installed") result.installed = value === "1";235
else if (["binary", "version", "configPath", "serviceState"].includes(key)) result[key] = value;236
}237
result.configs.sort((left, right) => left.path.localeCompare(right.path));238
return result;239
}241
class SessionError extends Error {242
constructor(code, message, details = {}) {243
super(message);244
this.name = "SessionError";245
this.code = code;246
Object.assign(this, details);247
}249
toJSON() {250
const result = { code: this.code, message: this.message };251
for (const key of ["sessionId", "profileId", "host", "port", "fingerprint", "knownFingerprint"]) {252
if (this[key] !== undefined) result[key] = this[key];253
}254
return result;255
}256
}258
function normalizeTerminalSize(value, fallback, min, max) {259
const number = Number(value);260
return Number.isInteger(number) ? Math.max(min, Math.min(max, number)) : fallback;261
}263
function metricNumber(value, minimum = 0, maximum = Number.MAX_SAFE_INTEGER) {264
const number = Number(value);265
return Number.isFinite(number) ? Math.max(minimum, Math.min(maximum, number)) : minimum;266
}268
function metricText(value, fallback = "unknown", maximumLength = 240) {269
const normalized = String(value ?? "")270
.replace(/[\x00-\x1f\x7f|]/g, " ")271
.replace(/\s+/g, " ")272
.trim()273
.slice(0, maximumLength);274
return normalized || fallback;275
}277
function parseCpuCoreTypes(value) {278
return String(value || "").split(",").slice(0, 16).map((entry) => {279
const separator = entry.lastIndexOf(":");280
if (separator <= 0) return null;281
const name = metricText(entry.slice(0, separator), "", 80);282
const count = Math.floor(metricNumber(entry.slice(separator + 1), 0, 4096));283
return name && count > 0 ? { name, count } : null;284
}).filter(Boolean);285
}287
function parseNetworkInterfaces(lines, defaultInterface) {288
return lines.filter((line) => line.startsWith("networkInterface=")).slice(0, 64).map((line) => {289
const fields = line.slice("networkInterface=".length).split("|");290
const name = metricText(fields[0], "unknown", 64);291
const state = metricText(fields[1], "unknown", 32).toLowerCase();292
const addresses = String(fields[5] || "").split(",").slice(0, 16)293
.map((address) => metricText(address, "", 128)).filter(Boolean);294
return {295
name,296
state,297
mac: metricText(fields[2], "", 64),298
rxBytes: metricNumber(fields[3]),299
txBytes: metricNumber(fields[4]),300
addresses,301
isDefault: name === defaultInterface302
};303
});304
}306
const LIST_PREFIXES = ["networkInterface=", "meminfo=", "swapArea=", "vmstat=", "zram=", "zswap="];308
function parseLinuxMemoryDetail(lines) {309
const meminfo = {};310
for (const line of lines.filter((entry) => entry.startsWith("meminfo="))) {311
const [key, value] = line.slice(8).split("|");312
if (/^[A-Za-z_()]+$/.test(key) && Number.isFinite(Number(value))) meminfo[key] = Number(value);313
}314
if (!meminfo.MemTotal) return null;315
const get = (key) => (key in meminfo ? meminfo[key] : null);316
const total = meminfo.MemTotal;317
const free = get("MemFree") ?? 0;318
const buffers = get("Buffers") ?? 0;319
const cached = get("Cached") ?? 0;320
const reclaimableSlab = get("SReclaimable") ?? 0;321
// Non-overlapping slices: Cached already contains Shmem; SwapCached is reported separately (not a slice).322
const used = Math.max(0, total - free - buffers - cached - reclaimableSlab);323
const vmstat = Object.fromEntries(lines.filter((entry) => entry.startsWith("vmstat=")).map((line) => line.slice(7).split("|")).map(([key, value]) => [key, Number(value)]));324
const swapAreas = lines.filter((entry) => entry.startsWith("swapArea=")).slice(0, 32).map((line) => {325
const [name = "", type = "", size = "", usedBytes = "", priority = ""] = line.slice(9).split("|");326
const path = name.replace(/\\040/g, " ");327
return { name: metricText(path, "", 255), type: /^\/dev\/zram\d+$/.test(path) ? "zram" : metricText(type, "unknown", 16), size: metricNumber(size), used: metricNumber(usedBytes), priority: Number.isFinite(Number(priority)) ? Number(priority) : null };328
});329
const zram = lines.filter((entry) => entry.startsWith("zram=")).slice(0, 16).map((line) => {330
const [device = "", disksize = "", mmStat = "", algorithm = ""] = line.slice(5).split("|");331
const stat = mmStat.split(",").filter(Boolean).map(Number);332
return { device: metricText(device, "", 32), diskSize: metricNumber(disksize), originalData: stat[0] ?? null, compressedData: stat[1] ?? null, memoryUsed: stat[2] ?? null, memoryLimit: stat[3] || null, algorithm: metricText(algorithm, "", 32) || null };333
});334
const zswapLine = lines.find((entry) => entry.startsWith("zswap="));335
const [zswapEnabled = "", zswapCompressor = "", zswapPool = ""] = zswapLine ? zswapLine.slice(6).split("|") : [];336
return {337
source: "/proc/meminfo",338
total,339
free,340
available: get("MemAvailable"),341
used,342
buffers,343
cached,344
shmem: get("Shmem"),345
swapCached: get("SwapCached"),346
slab: get("Slab"),347
slabReclaimable: get("SReclaimable"),348
slabUnreclaimable: get("SUnreclaim"),349
kernelStack: get("KernelStack"),350
pageTables: get("PageTables"),351
vmallocUsed: get("VmallocUsed"),352
dirty: get("Dirty"),353
writeback: get("Writeback"),354
commit: { committed: get("Committed_AS"), limit: get("CommitLimit") },355
swapTotal: get("SwapTotal"),356
swapFree: get("SwapFree"),357
zswapPool: get("Zswap"),358
zswapStored: get("Zswapped"),359
paging: { swapInPages: Number.isFinite(vmstat.pswpin) ? vmstat.pswpin : null, swapOutPages: Number.isFinite(vmstat.pswpout) ? vmstat.pswpout : null, majorFaults: Number.isFinite(vmstat.pgmajfault) ? vmstat.pgmajfault : null },360
swapAreas,361
zram,362
zswap: zswapLine ? { enabled: /^[Yy1]/.test(zswapEnabled), compressor: metricText(zswapCompressor, "", 32) || null, maxPoolPercent: Number(zswapPool) || null } : null363
};364
}366
function parseSystemMetricsOutput(stdout, fallbackHostname = "unknown") {367
const lines = String(stdout || "").split(/\r?\n/).filter(Boolean);368
const values = Object.fromEntries(lines.filter((line) => !LIST_PREFIXES.some((prefix) => line.startsWith(prefix))).map((line) => {369
const index = line.indexOf("=");370
return index > 0 ? [line.slice(0, index), line.slice(index + 1)] : [line, ""];371
}));372
const disk = String(values.disk || "0:0:0").split(":").map((value) => metricNumber(value));373
const memoryTotal = metricNumber(values.memoryTotal);374
const memoryAvailable = metricNumber(values.memoryAvailable, 0, memoryTotal);375
const swapTotal = metricNumber(values.swapTotal);376
const swapFree = metricNumber(values.swapFree, 0, swapTotal);377
const defaultInterface = metricText(values.networkDefault, "", 64);378
const memoryDetail = parseLinuxMemoryDetail(lines);379
return {380
os: "linux",381
loadAverageSupported: true,382
pageSize: metricNumber(values.pageSize, 0, 1 << 30) || null,383
memoryDetail,384
swapAreas: memoryDetail?.swapAreas || [],385
platform: metricText(values.platform),386
kernel: metricText(values.kernel),387
hostname: metricText(values.hostname, fallbackHostname),388
uptimeSeconds: metricNumber(values.uptimeSeconds),389
load1: metricNumber(values.load1),390
memory: { total: memoryTotal, available: memoryAvailable },391
swap: { total: swapTotal, used: Math.max(0, swapTotal - swapFree), free: swapFree },392
disk: {393
mount: "/",394
total: disk[0] || 0,395
used: Math.min(disk[0] || 0, disk[1] || 0),396
available: Math.min(disk[0] || 0, disk[2] || 0)397
},398
cpu: {399
usagePercent: metricNumber(values.cpuUsage, 0, 100),400
architecture: metricText(values.cpuArchitecture),401
model: metricText(values.cpuModel),402
logicalCores: Math.floor(metricNumber(values.cpuLogical, 0, 4096)),403
physicalCores: Math.floor(metricNumber(values.cpuPhysical, 0, 4096)),404
sockets: Math.floor(metricNumber(values.cpuSockets, 0, 256)),405
coreTypes: parseCpuCoreTypes(values.cpuCoreTypes)406
},407
network: {408
defaultInterface,409
interfaces: parseNetworkInterfaces(lines, defaultInterface)410
}411
};412
}414
class SessionManager extends EventEmitter {415
constructor(options) {416
super();417
this.profileService = options.profileService;418
this.logger = options.logger;419
this.clientFactory = options.clientFactory || (() => new Client());420
this.lookupHost = options.lookupHost || dns.lookup;421
this.resolve4 = options.resolve4 || dns.resolve4;422
this.resolve6 = options.resolve6 || dns.resolve6;423
this.latencyMonitor = options.latencyMonitor || new LatencyMonitor();424
this.sessions = new Map();425
this.pendingTrust = new Map();426
}428
list() {429
return [...this.sessions.values()].map((record) => this._publicStatus(record));430
}432
async connect(options = {}) {433
const sessionId = String(options.sessionId || "").trim();434
if (!SESSION_ID_PATTERN.test(sessionId)) throw new SessionError("INVALID_SESSION_ID", "Invalid terminal session ID.");435
await this.disconnect(sessionId, "reconnect");436
const profile = this.profileService.get(String(options.profileId || ""));437
const config = this._buildConnectConfig(profile, options);438
const resolvedHost = await this._resolveConnectionAddress(profile.host);439
config.host = resolvedHost.address;440
const knownFingerprint = this.profileService.getHostKey(profile.host, profile.port);441
const client = this.clientFactory();442
const record = {443
sessionId,444
profileId: profile.id,445
label: profile.label,446
host: profile.host,447
address: resolvedHost.address,448
port: profile.port,449
username: profile.username,450
authMethod: profile.authMethod,451
state: "connecting",452
client,453
stream: null,454
decoder: new StringDecoder("utf8"),455
finalized: false456
};457
this.sessions.set(sessionId, record);458
this.emit("state", this._publicStatus(record));460
let hostKeyIssue = null;461
config.hostHash = "sha256";462
config.hostVerifier = (fingerprint) => {463
const normalized = String(fingerprint || "").toLowerCase();464
if (!knownFingerprint) {465
hostKeyIssue = { code: "HOST_KEY_UNKNOWN", fingerprint: normalized };466
this.pendingTrust.set(sessionId, {467
sessionId,468
profileId: profile.id,469
host: profile.host,470
port: profile.port,471
fingerprint: normalized,472
knownFingerprint: null,473
expiresAt: Date.now() + 5 * 60 * 1000474
});475
return false;476
}477
if (knownFingerprint !== normalized) {478
hostKeyIssue = { code: "HOST_KEY_MISMATCH", fingerprint: normalized, knownFingerprint };479
this.pendingTrust.set(sessionId, {480
sessionId,481
profileId: profile.id,482
host: profile.host,483
port: profile.port,484
fingerprint: normalized,485
knownFingerprint,486
expiresAt: Date.now() + 5 * 60 * 1000487
});488
return false;489
}490
return true;491
};493
return new Promise((resolve, reject) => {494
let settled = false;495
const fail = (error) => {496
if (settled) {497
const normalized = this._normalizeConnectionError(error, record);498
this.emit("sessionError", normalized.toJSON());499
this._finalize(record, normalized.code);500
return;501
}502
settled = true;503
const normalized = hostKeyIssue504
? new SessionError(hostKeyIssue.code, hostKeyIssue.code === "HOST_KEY_UNKNOWN" ? "The SSH host key is not trusted yet." : "The SSH host key changed.", {505
sessionId,506
profileId: profile.id,507
host: profile.host,508
port: profile.port,509
...hostKeyIssue510
})511
: this._normalizeConnectionError(error, record);512
this.logger?.warn("SSH connection failed.", { sessionId, code: normalized.code, message: normalized.message, address: record.address, port: record.port });513
this._finalize(record, normalized.code);514
reject(normalized);515
};517
client.once("ready", () => {518
const cols = normalizeTerminalSize(options.cols, 100, 20, 500);519
const rows = normalizeTerminalSize(options.rows, 30, 5, 300);520
client.shell({ term: "xterm-256color", cols, rows }, (error, stream) => {521
if (error) { fail(error); return; }522
if (settled) { try { stream.close(); } catch {} return; }523
settled = true;524
record.stream = stream;525
record.state = "connected";526
this.pendingTrust.delete(sessionId);527
this._bindShell(record);528
this.profileService.markUsed(profile.id);529
this.logger?.info("SSH session connected.", {530
sessionId,531
profileId: profile.id,532
host: profile.host,533
port: profile.port,534
username: profile.username,535
authMethod: profile.authMethod536
});537
const status = this._publicStatus(record);538
this.emit("state", status);539
record.stopLatency = this.latencyMonitor.start(record,540
() => this._sshRoundTrip(record),541
(latency) => {542
if (record.finalized || this.sessions.get(sessionId) !== record) return;543
record.latency = latency;544
this.emit("state", this._publicStatus(record));545
});546
resolve(status);547
});548
});549
client.on("error", fail);550
client.once("close", () => {551
if (!settled) fail(new Error("SSH connection closed before it became ready."));552
else this._finalize(record, "remote-close");553
});554
try { client.connect(config); } catch (error) { fail(error); }555
});556
}558
trustPending(options = {}) {559
const sessionId = String(options.sessionId || "");560
const pending = this.pendingTrust.get(sessionId);561
const fingerprint = String(options.fingerprint || "").toLowerCase();562
if (!pending || pending.expiresAt < Date.now()) {563
this.pendingTrust.delete(sessionId);564
throw new SessionError("HOST_KEY_CONFIRMATION_EXPIRED", "The host-key confirmation expired.");565
}566
if (pending.profileId !== options.profileId || pending.fingerprint !== fingerprint) {567
throw new SessionError("HOST_KEY_CONFIRMATION_INVALID", "The host-key confirmation does not match the pending connection.");568
}569
if (pending.knownFingerprint) {570
const confirmedKnownFingerprint = String(options.knownFingerprint || "").toLowerCase();571
if (options.replaceKnown !== true || confirmedKnownFingerprint !== pending.knownFingerprint) {572
throw new SessionError("HOST_KEY_REPLACEMENT_CONFIRMATION_REQUIRED", "Replacing a changed SSH host key requires explicit confirmation of both fingerprints.");573
}574
if (this.profileService.getHostKey(pending.host, pending.port) !== pending.knownFingerprint) {575
this.pendingTrust.delete(sessionId);576
throw new SessionError("HOST_KEY_REPLACEMENT_STALE", "The stored SSH host key changed while confirmation was pending.");577
}578
} else if (options.replaceKnown === true) {579
throw new SessionError("HOST_KEY_CONFIRMATION_INVALID", "A new host key cannot be confirmed as a replacement.");580
}581
const result = this.profileService.trustHostKey(pending.host, pending.port, pending.fingerprint);582
this.pendingTrust.delete(sessionId);583
const details = { host: pending.host, port: pending.port, fingerprint: pending.fingerprint, replacedFingerprint: pending.knownFingerprint };584
if (pending.knownFingerprint) this.logger?.warn("Changed SSH host key replaced by user confirmation.", details);585
else this.logger?.info("SSH host key trusted by user.", details);586
return result;587
}589
write(sessionId, data) {590
const record = this._requireConnected(sessionId);591
const text = String(data ?? "");592
if (Buffer.byteLength(text, "utf8") > MAX_INPUT_LENGTH) throw new SessionError("TERMINAL_INPUT_TOO_LARGE", "Terminal input is too large.");593
record.stream.write(text);594
}596
resize(sessionId, cols, rows) {597
const record = this._requireConnected(sessionId);598
const normalizedCols = normalizeTerminalSize(cols, 100, 20, 500);599
const normalizedRows = normalizeTerminalSize(rows, 30, 5, 300);600
record.stream.setWindow(normalizedRows, normalizedCols, 0, 0);601
return { cols: normalizedCols, rows: normalizedRows };602
}604
async readSystemMetrics(sessionId) {605
const record = this._requireConnected(sessionId);606
const platform = await this._platform(record);607
if (platform.os === "windows") return this._readWindowsMetrics(record);608
if (platform.os === "unknown") throw new SessionError("METRICS_PLATFORM_UNSUPPORTED", `The remote operating system could not be identified: ${platform.reason || "no POSIX shell and no Windows PowerShell"}.`);609
const output = await this._execFixed(record, METRICS_COMMAND, 10000);610
return {611
sessionId: record.sessionId,612
...parseSystemMetricsOutput(output.stdout, record.host),613
collectedAt: new Date().toISOString()614
};615
}617
// Remote OS of a live session (cached). Required lazily to avoid a require cycle.618
_platform(record) {619
if (record.platform) return Promise.resolve(record.platform);620
return require("./platformDetect").detectPlatform(this, record.sessionId);621
}623
async _assertPosix(record, feature) {624
const platform = await this._platform(record);625
if (platform.os === "windows") throw new SessionError("FEATURE_PLATFORM_UNSUPPORTED", `${feature} is available only on Linux/POSIX servers; this server runs ${platform.caption || "Windows"}.`);626
}628
// Win32-OpenSSH stalls the whole connection when PowerShell helpers fed through stdin run concurrently on one629
// session (reproduced with System Monitor polling next to Users or three parallel metric reads: every channel,630
// even a plain cmd /c echo, then times out; sshd logs "chan_shutdown_write ... fd -1"). Helpers of one Windows631
// session therefore run one after another; each timeout starts when its turn comes. SFTP is not queued.632
_windowsSerial(record, task) {633
const turn = (record.windowsQueue || Promise.resolve()).then(task);634
record.windowsQueue = turn.catch(() => {});635
return turn;636
}638
// Runs a fixed PowerShell script on a Windows session (short encoded bootstrap; script and JSON on stdin).639
async _runWindowsScript(record, script, args, timeoutMs = 30000) {640
const { BOOTSTRAP, bootstrapInput, encodedCommand, parseWindowsOutput } = require("./windowsPrivilegedExec");641
const result = await this._windowsSerial(record, () => this._execStream(record, encodedCommand(BOOTSTRAP), {642
timeoutMs,643
outputLimit: 4 * 1024 * 1024,644
onUpdate: (controller, kind) => { if (kind === "open") { controller.write(bootstrapInput(script, args)); controller.end(); } }645
}).done);646
return parseWindowsOutput(result.stdout, result.stderr, result.code, "METRICS_");647
}649
async _readWindowsMetrics(record) {650
const { METRICS_SCRIPT, normalizeWindowsMetrics } = require("./windowsMetrics");651
record.metricsState ||= {};652
const state = record.metricsState;653
const raw = await this._runWindowsScript(record, METRICS_SCRIPT, { static: !state.static }, 30000);654
if (raw.static) state.static = raw.static;655
const metrics = normalizeWindowsMetrics(raw, state.static, state, record.host);656
if (raw.cpuRaw) state.cpuRaw = raw.cpuRaw;657
if (raw.memoryCounters) state.memoryCounters = raw.memoryCounters;658
return { sessionId: record.sessionId, ...metrics, collectedAt: new Date().toISOString() };659
}661
openSftp(sessionId) {662
const record = this._requireConnected(sessionId);663
return new Promise((resolve, reject) => record.client.sftp((error, sftp) => error ? reject(new SessionError("SFTP_OPEN_FAILED", error.message)) : resolve(sftp)));664
}666
// Internal fixed protocol only. Neither renderer nor plugin can supply code.667
async remoteEditor(sessionId, request, authorization = {}) {668
const record = this._requireConnected(sessionId);669
await this._assertPosix(record, "The remote text editor");670
let password;671
try { password = normalizeSudoPassword(authorization.sudoPassword); }672
catch { throw new SessionError("FILE_SUDO_PASSWORD_INVALID", "Invalid sudo password."); }673
if (password && authorization.sudo !== true) throw new SessionError("FILE_SUDO_AUTHORIZATION_REQUIRED", "Explicit sudo authorization is required.");674
const timeoutMs = Math.min(Math.max(Number(authorization.timeoutMs) || 60000, 10000), 24 * 3600 * 1000);675
const mutation = EDITOR_MUTATIONS.has(request.operation);676
let output;677
if (authorization.sudo === true) {678
try {679
const result = await this._privilegedExecutor().run(sessionId, {680
script: EDITOR_PRIVILEGED_SCRIPT, args: [REMOTE_EDITOR], frames: [{ tag: "REQ", data: JSON.stringify(request) }],681
sudoPassword: password, rememberPassword: authorization.rememberSudo === true, useCachedPassword: authorization.useCachedSudo !== false,682
purpose: `files.${request.operation}`, errorPrefix: "FILE_", timeoutMs, outputLimit: EDITOR_OUTPUT_LIMIT683
});684
output = { stdout: result.stdout, code: result.code };685
} catch (error) {686
// sudo refusals happen before the helper starts: definite, original untouched.687
if (/^SUDO_[A-Z_]+$/.test(error.code) || error.code === "FILE_DEPENDENCY_UNAVAILABLE") throw error;688
throw new SessionError(mutation ? "FILE_RESULT_UNKNOWN" : "FILE_EXEC_FAILED", "Remote file operation was not confirmed. Inspect the target before retrying.");689
}690
} else {691
const command = `printf '\\036CHJ_EDITOR_START\\n'; exec python3 -I -B -c ${shellQuote(REMOTE_EDITOR)} ${shellQuote(JSON.stringify(request))}`;692
try {693
output = await this._execFixed(record, command, timeoutMs, EDITOR_OUTPUT_LIMIT);694
} catch (error) {695
// Remote stderr never becomes editor errors or logs.696
throw new SessionError(error.remoteExitCode === 127 ? "FILE_DEPENDENCY_UNAVAILABLE" : mutation ? "FILE_RESULT_UNKNOWN" : "FILE_EXEC_FAILED",697
"Remote file operation was not confirmed. Inspect the target before retrying.");698
}699
}700
let response;701
try {702
if (!output.stdout.startsWith(EDITOR_STARTED)) throw new Error("Missing editor marker");703
response = JSON.parse(output.stdout.slice(EDITOR_STARTED.length));704
}705
catch { throw new SessionError(output.code === 127 ? "FILE_DEPENDENCY_UNAVAILABLE" : mutation ? "FILE_RESULT_UNKNOWN" : "FILE_EXEC_FAILED", "Remote file operation result could not be verified."); }706
if (!response || response.ok !== true || !response.value || typeof response.value !== "object") {707
const code = /^FILE_[A-Z_]+$/.test(response?.code) ? response.code : "FILE_PROTOCOL_ERROR";708
throw new SessionError(code, `${code}: Remote file operation stopped${mutation ? "; the original was kept and recoverable copies were retained" : ""}.`);709
}710
return response.value;711
}713
// SHA-256 of a remote file with the standard tools, for servers without python3.714
async remoteChecksum(sessionId, remotePath) {715
const record = this._requireConnected(sessionId);716
const target = String(remotePath || "");717
if (!target.startsWith("/") || /[\0\r\n]/.test(target)) throw new SessionError("FILE_UNSAFE_PATH", "Remote path must be absolute.");718
if ((await this._platform(record)).os === "windows") {719
// Windows OpenSSH SFTP paths look like /C:/Users/x; the path travels as JSON, never in the command line.720
const result = await this._runWindowsScript(record, WINDOWS_HASH_SCRIPT, { path: target }, 6 * 3600 * 1000);721
if (!/^[a-f0-9]{64}$/.test(String(result?.hash || ""))) throw new SessionError("FILE_CHECKSUM_UNAVAILABLE", "The remote checksum could not be read.");722
return result.hash;723
}724
const command = `LC_ALL=C; p=${shellQuote(target)}; if command -v sha256sum >/dev/null 2>&1; then sha256sum -- "$p"; elif command -v shasum >/dev/null 2>&1; then shasum -a 256 -- "$p"; else exit 127; fi`;725
const output = await this._execFixed(record, command, 6 * 3600 * 1000);726
const match = /^([a-f0-9]{64})\b/.exec(output.stdout.trim());727
if (!match) throw new SessionError("FILE_CHECKSUM_UNAVAILABLE", "The remote checksum could not be read.");728
return match[1];729
}731
_privilegedExecutor() {732
if (!this.privilegedExecutor) {733
// Lazy: privilegedExec.js depends on this module's exports.734
const { PrivilegedExecutor } = require("./privilegedExec");735
this.privilegedExecutor = new PrivilegedExecutor({ sessionManager: this, logger: this.logger });736
}737
return this.privilegedExecutor;738
}740
// Identity facts for lockout protection. Secrets are never returned.741
sessionAuthInfo(sessionId) {742
const record = this._requireConnected(sessionId);743
let publicKeyFingerprint = null;744
if (record.authMethod === "privateKey") {745
try {746
const profile = this.profileService.get(record.profileId);747
publicKeyFingerprint = publicKeyFingerprintForPrivateKey(profile.privateKeyPath);748
} catch {}749
}750
return { username: record.username, authMethod: record.authMethod, address: record.address, publicKeyFingerprint };751
}753
// Reports whether a fresh login can be attempted without asking the user for more secrets.754
probeLoginRequirements(sessionId, credentials = {}) {755
const record = this._requireConnected(sessionId);756
let profile;757
try { profile = this.profileService.get(record.profileId); }758
catch { return { ready: false, missing: "profile" }; }759
if (profile.authMethod === "privateKey") {760
let data;761
try { data = fs.readFileSync(profile.privateKeyPath); } catch { return { ready: false, missing: "privateKey" }; }762
const parsed = sshUtils.parseKey(data, credentials.passphrase ? String(credentials.passphrase) : undefined);763
return parsed instanceof Error ? { ready: false, missing: "passphrase" } : { ready: true, missing: null };764
}765
const password = String(credentials.password || this.profileService.getStoredPassword?.(profile.id) || "");766
return password ? { ready: true, missing: null } : { ready: false, missing: "password" };767
}769
// Profile and address of a live session, kept by callers that must reconnect after sshd restarts.770
connectionTarget(sessionId) {771
const record = this._requireConnected(sessionId);772
return { sessionId: record.sessionId, profileId: record.profileId, address: record.address, host: record.host, port: record.port };773
}775
// Opens a separate SSH connection with the session profile to prove that new logins still work.776
// The host key must match the trusted fingerprint; nothing is stored or trusted implicitly.777
async probeLogin(sessionId, credentials = {}) {778
const result = await this.freshExec(this.connectionTarget(sessionId), credentials, { command: "true", acceptAnyExit: true });779
return { ok: true, address: result.address, checkedAt: result.checkedAt };780
}782
// Runs one fixed command on a new, short-lived connection (used to confirm configuration changes783
// even when the original session did not survive an sshd restart). Output is returned, not logged.784
async freshExec(target, credentials = {}, { command, input = "", timeoutMs = 60000, acceptAnyExit = false } = {}) {785
const profile = this.profileService.get(target.profileId);786
const config = this._buildConnectConfig(profile, credentials);787
config.host = target.address;788
config.readyTimeout = 15000;789
config.keepaliveInterval = 0;790
const knownFingerprint = this.profileService.getHostKey(profile.host, profile.port);791
config.hostHash = "sha256";792
config.hostVerifier = (fingerprint) => Boolean(knownFingerprint) && String(fingerprint || "").toLowerCase() === knownFingerprint;793
const client = this.clientFactory();794
const reference = { sessionId: target.sessionId, profileId: target.profileId, host: profile.host, port: profile.port };795
return new Promise((resolve, reject) => {796
let settled = false;797
let stdout = "";798
let stderr = "";799
const done = (error, code) => {800
if (settled) return;801
settled = true;802
clearTimeout(timer);803
try { client.end(); } catch {}804
if (error) {805
const normalized = this._normalizeConnectionError(error, reference);806
reject(new SessionError("SSH_PROBE_FAILED", `A new SSH login was not confirmed: ${normalized.message}`, { causeCode: normalized.code }));807
} else resolve({ ok: true, code, stdout, stderr, address: target.address, checkedAt: new Date().toISOString() });808
};809
const timer = setTimeout(() => done(new Error("Login probe timed out.")), timeoutMs);810
timer.unref?.();811
client.once("ready", () => openExec(client, command, (error, stream) => {812
if (error) { done(error); return; }813
stream.on("data", (chunk) => { if (stdout.length < MAX_COMMAND_OUTPUT) stdout += chunk.toString("utf8"); });814
stream.stderr?.on("data", (chunk) => { if (stderr.length < MAX_COMMAND_OUTPUT) stderr += chunk.toString("utf8"); });815
stream.once("close", (code) => {816
if (acceptAnyExit || code === 0 || code === undefined || code === null) done(null, code);817
else done(new Error(`Probe command exited with ${code}.`));818
});819
try { stream.end(input); } catch {}820
}));821
client.on("error", done);822
client.once("close", () => done(new Error("SSH connection closed before the login was confirmed.")));823
try { client.connect(config); } catch (error) { done(error); }824
});825
}827
async inspectNginx(sessionId) {828
const record = this._requireConnected(sessionId);829
await this._assertPosix(record, "NGINX management");830
const output = await this._execFixed(record, NGINX_INSPECT_COMMAND, 10000);831
return { sessionId: record.sessionId, ...parseNginxInspectionOutput(output.stdout), collectedAt: new Date().toISOString() };832
}834
async readNginxConfig(sessionId, payload = {}) {835
const record = this._requireConnected(sessionId);836
const path = normalizeNginxConfigPath(payload.path);837
const sudoPassword = normalizeSudoPassword(payload.sudoPassword);838
const command = `target=${shellQuote(path)}; [ -f "$target" ] || { echo 'NGINX configuration file was not found.' >&2; exit 41; }; size=$(wc -c < "$target"); [ "$size" -le ${MAX_NGINX_CONFIG_BYTES} ] || { echo 'NGINX configuration exceeds the 512 KiB editor limit.' >&2; exit 42; }; cat -- "$target"`;839
const output = sudoPassword840
? await this._execSudo(record, command, sudoPassword, 10000)841
: await this._execFixed(record, command, 10000);842
return { path, text: output.stdout, size: Buffer.byteLength(output.stdout, "utf8") };843
}845
async dumpNginxConfig(sessionId, payload = {}) {846
const record = this._requireConnected(sessionId);847
const sudoPassword = normalizeSudoPassword(payload.sudoPassword);848
const output = await this._execSudo(record, "nginx -T 2>&1", sudoPassword, 20000);849
return { text: output.stdout, generatedAt: new Date().toISOString() };850
}852
async testNginxConfig(sessionId, payload = {}) {853
const record = this._requireConnected(sessionId);854
const sudoPassword = normalizeSudoPassword(payload.sudoPassword);855
const command = "set +e; output=$(nginx -t 2>&1); code=$?; printf '%s\\n' \"$output\"; printf '\\036CHJ_EXIT=%s\\n' \"$code\"; exit 0";856
const output = await this._execSudo(record, command, sudoPassword, 20000);857
const marker = output.stdout.match(/\x1eCHJ_EXIT=(\d+)\s*$/);858
const code = marker ? Number(marker[1]) : 1;859
return { ok: code === 0, output: output.stdout.replace(/\x1eCHJ_EXIT=\d+\s*$/, "").trim(), testedAt: new Date().toISOString() };860
}862
async saveNginxConfig(sessionId, payload = {}) {863
const record = this._requireConnected(sessionId);864
const path = normalizeNginxConfigPath(payload.path);865
const sudoPassword = normalizeSudoPassword(payload.sudoPassword);866
const text = String(payload.text ?? "");867
const bytes = Buffer.byteLength(text, "utf8");868
if (bytes <= 0 || bytes > MAX_NGINX_CONFIG_BYTES || text.includes("\0")) {869
throw new SessionError("NGINX_CONFIG_INVALID", "NGINX configuration must be UTF-8 text between 1 byte and 512 KiB.");870
}871
const encoded = Buffer.from(text, "utf8").toString("base64");872
const command = `target=${shellQuote(path)}; data=${shellQuote(encoded)}; [ -f "$target" ] || { echo 'NGINX configuration file was not found.' >&2; exit 41; }; [ ! -L "$target" ] || { echo 'Edit the corresponding sites-available file instead of replacing an enabled symlink.' >&2; exit 42; }; backup="$target.chj-backup-$(date +%Y%m%dT%H%M%S)"; temporary=$(mktemp "$target.chj-tmp.XXXXXX") || exit 43; cleanup(){ [ -z "$temporary" ] || rm -f -- "$temporary"; }; trap cleanup EXIT HUP INT TERM; if ! printf '%s' "$data" | base64 --decode > "$temporary" 2>/dev/null; then printf '%s' "$data" | base64 -d > "$temporary" || exit 44; fi; chmod --reference="$target" "$temporary"; chown --reference="$target" "$temporary"; cp -p -- "$target" "$backup"; mv -f -- "$temporary" "$target"; temporary=; test_output=$(nginx -t 2>&1); test_code=$?; if [ "$test_code" -ne 0 ]; then cp -p -- "$backup" "$target"; printf '%s\\nConfiguration was rolled back from %s.\\n' "$test_output" "$backup" >&2; exit 45; fi; printf '%s\\n\\036CHJ_BACKUP=%s\\n' "$test_output" "$backup"`;873
const output = await this._execSudo(record, command, sudoPassword, 30000);874
const marker = output.stdout.match(/\x1eCHJ_BACKUP=([^\r\n]+)\s*$/);875
const backupPath = marker ? marker[1].trim() : "";876
this.logger?.warn("Remote NGINX configuration saved after successful validation.", { sessionId: record.sessionId, host: record.host, path, backupPath });877
return { path, backupPath, size: bytes, testOutput: output.stdout.replace(/\x1eCHJ_BACKUP=[^\r\n]+\s*$/, "").trim(), savedAt: new Date().toISOString() };878
}880
async reloadNginx(sessionId, payload = {}) {881
const record = this._requireConnected(sessionId);882
const sudoPassword = normalizeSudoPassword(payload.sudoPassword);883
if (payload.confirm !== true) throw new SessionError("NGINX_RELOAD_CONFIRMATION_REQUIRED", "Reloading NGINX requires explicit confirmation.");884
const command = "test_output=$(nginx -t 2>&1) || { printf '%s\\n' \"$test_output\" >&2; exit 41; }; if command -v systemctl >/dev/null 2>&1; then systemctl reload nginx; elif command -v service >/dev/null 2>&1; then service nginx reload; else nginx -s reload; fi; printf '%s\\nNGINX configuration reloaded gracefully.\\n' \"$test_output\"";885
const output = await this._execSudo(record, command, sudoPassword, 30000);886
this.logger?.warn("Remote NGINX graceful reload completed.", { sessionId: record.sessionId, host: record.host });887
return { ok: true, output: output.stdout.trim(), reloadedAt: new Date().toISOString() };888
}890
async disconnect(sessionId, reason = "user") {891
const record = this.sessions.get(String(sessionId || ""));892
this.pendingTrust.delete(String(sessionId || ""));893
if (!record) return { disconnected: false };894
this._finalize(record, reason);895
return { disconnected: true };896
}898
async disconnectAll(reason = "lock") {899
const ids = [...this.sessions.keys()];900
for (const id of ids) await this.disconnect(id, reason);901
this.pendingTrust.clear();902
return { disconnected: ids.length };903
}905
_buildConnectConfig(profile, options) {906
const config = {907
host: profile.host,908
port: profile.port,909
username: profile.username,910
readyTimeout: 15000,911
keepaliveInterval: 15000,912
keepaliveCountMax: 3913
};914
if (profile.authMethod === "privateKey") {915
if (profile.privateKeyPath.toLowerCase().endsWith(".pub")) throw new SessionError("PUBLIC_KEY_SELECTED", "Select a private key, not a .pub file.");916
let stat;917
try { stat = fs.statSync(profile.privateKeyPath); } catch { throw new SessionError("PRIVATE_KEY_NOT_FOUND", "The private key file cannot be read."); }918
if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_KEY_BYTES) throw new SessionError("PRIVATE_KEY_INVALID", "The private key file is invalid or too large.");919
config.privateKey = fs.readFileSync(profile.privateKeyPath);920
const passphrase = String(options.passphrase || "");921
if (passphrase) config.passphrase = passphrase;922
} else {923
const password = String(options.password || this.profileService.getStoredPassword?.(profile.id) || "");924
if (!password) throw new SessionError("PASSWORD_REQUIRED", "Enter the SSH password.");925
config.password = password;926
}927
return config;928
}930
_bindShell(record) {931
record.stream.on("data", (chunk) => this.emit("data", { sessionId: record.sessionId, data: record.decoder.write(chunk) }));932
record.stream.stderr?.on("data", (chunk) => this.emit("data", { sessionId: record.sessionId, data: record.decoder.write(chunk) }));933
record.stream.once("close", () => this._finalize(record, "shell-close"));934
record.stream.once("error", (error) => {935
this.emit("sessionError", { sessionId: record.sessionId, message: error?.message || String(error) });936
this._finalize(record, "shell-error");937
});938
}940
_requireConnected(sessionId) {941
const record = this.sessions.get(String(sessionId || ""));942
if (!record || record.state !== "connected" || !record.stream) throw new SessionError("SESSION_NOT_CONNECTED", "The terminal session is not connected.");943
return record;944
}946
_normalizeConnectionError(error, record) {947
const message = error?.message || String(error || "SSH connection failed.");948
let code = "SSH_CONNECTION_FAILED";949
if (process.platform === "darwin" && ["EPERM", "EACCES"].includes(error?.code)) code = "SSH_LOCAL_NETWORK_DENIED";950
else if (process.platform === "darwin" && ["EHOSTUNREACH", "ENETUNREACH"].includes(error?.code)) code = "SSH_MAC_NETWORK_UNREACHABLE";951
else if (error?.code === "ENOTFOUND" || error?.code === "EAI_AGAIN" || /getaddrinfo|name or service not known|nodename nor servname/i.test(message)) code = "SSH_DNS_RESOLUTION_FAILED";952
else if (/authentication/i.test(message)) code = "SSH_AUTHENTICATION_FAILED";953
else if (/timed?\s*out/i.test(message)) code = "SSH_TIMEOUT";954
else if (/refused/i.test(message)) code = "SSH_CONNECTION_REFUSED";955
return new SessionError(code, message, { sessionId: record.sessionId, profileId: record.profileId, host: record.host, port: record.port });956
}958
async _resolveConnectionAddress(host) {959
const literalFamily = net.isIP(host);960
if (literalFamily) return { address: host, family: literalFamily, source: "literal" };961
let addresses = [];962
let lookupError = null;963
try {964
addresses = await this.lookupHost(host, { all: true });965
} catch (error) {966
lookupError = error;967
}968
let normalized = (Array.isArray(addresses) ? addresses : [addresses]).filter((entry) => entry && net.isIP(entry.address) === Number(entry.family));969
let source = "system";970
if (!normalized.length) {971
const [ipv4, ipv6] = await Promise.allSettled([this.resolve4(host), this.resolve6(host)]);972
normalized = [973
...(ipv4.status === "fulfilled" ? ipv4.value.map((address) => ({ address, family: 4 })) : []),974
...(ipv6.status === "fulfilled" ? ipv6.value.map((address) => ({ address, family: 6 })) : [])975
].filter((entry) => net.isIP(entry.address) === entry.family);976
source = "dns";977
}978
if (!normalized.length) {979
throw new SessionError("SSH_DNS_RESOLUTION_FAILED", `DNS name "${host}" could not be resolved.`, { host, causeCode: lookupError?.code });980
}981
const selected = normalized.find((entry) => entry.family === 4) || normalized[0];982
this.logger?.info("SSH hostname resolved.", { host, address: selected.address, addressFamily: selected.family, addressCount: normalized.length, source });983
return { ...selected, source };984
}986
_execFixed(record, command, timeoutMs, outputLimit = MAX_COMMAND_OUTPUT) {987
return new Promise((resolve, reject) => {988
let settled = false;989
let stdout = "";990
let stderr = "";991
let channel;992
const cancel = () => finish(new SessionError("SESSION_NOT_CONNECTED", "The SSH connection was lost."));993
record.pendingExec ||= new Set();994
record.pendingExec.add(cancel);995
const timer = setTimeout(() => {996
finish(new SessionError("REMOTE_COMMAND_TIMEOUT", "The remote command timed out."));997
try { channel?.close(); } catch {}998
}, timeoutMs);999
timer.unref?.();1000
const finish = (error, result) => {1001
if (settled) return;1002
settled = true;1003
clearTimeout(timer);1004
record.pendingExec.delete(cancel);1005
if (error) reject(error); else resolve(result);1006
};1007
openExec(record.client, command, (error, stream) => {1008
if (settled) { try { stream?.close(); } catch {} return; }1009
if (error) { finish(new SessionError("REMOTE_COMMAND_FAILED", error.message)); return; }1010
channel = stream;1011
const append = (target, chunk) => {1012
const next = target + chunk.toString("utf8");1013
if (Buffer.byteLength(next, "utf8") > outputLimit) {1014
try { stream.close(); } catch {}1015
finish(new SessionError("REMOTE_OUTPUT_TOO_LARGE", "The system metrics response is too large."));1016
return target;1017
}1018
return next;1019
};1020
stream.on("data", (chunk) => { stdout = append(stdout, chunk); });1021
stream.stderr?.on("data", (chunk) => { stderr = append(stderr, chunk); });1022
stream.once("error", (streamError) => finish(new SessionError("REMOTE_COMMAND_FAILED", streamError.message)));1023
stream.once("close", (code) => {1024
if (!Number.isInteger(code) || code !== 0) finish(new SessionError("REMOTE_COMMAND_FAILED", stderr.trim() || `Remote command exited with ${code}.`, { remoteExitCode: code, editorStarted: stdout.startsWith(EDITOR_STARTED) }));1025
else finish(null, { stdout, stderr });1026
});1027
});1028
});1029
}1031
// SSH round trip over the existing transport: a keepalive@openssh.com global request with want-reply.1032
// SUCCESS and FAILURE replies both complete a client->server->client round trip; no channel, no command1033
// and no new authentication. Falls back to the no-op "exit 0" (valid in sh, cmd.exe and PowerShell),1034
// which is labelled as a command round trip including remote process start.1035
_sshRoundTrip(record, timeoutMs = 5000) {1036
const client = record.client;1037
if (client && Array.isArray(client._callbacks) && typeof client._protocol?.ping === "function") {1038
return new Promise((resolve, reject) => {1039
let done = false;1040
const started = performance.now();1041
const timer = setTimeout(() => { if (!done) { done = true; reject(new SessionError("SSH_RTT_TIMEOUT", "The SSH server did not answer the round-trip request in time.")); } }, timeoutMs);1042
timer.unref?.();1043
// Replies are matched to callbacks in order, so the callback stays queued even after a timeout.1044
client._callbacks.push(() => {1045
if (done) return;1046
done = true;1047
clearTimeout(timer);1048
resolve({ ms: performance.now() - started, method: "transport" });1049
});1050
try { client._protocol.ping(); }1051
catch (error) { if (!done) { done = true; clearTimeout(timer); reject(new SessionError("SSH_RTT_FAILED", error.message)); } }1052
});1053
}1054
return this._execFixed(record, "exit 0", timeoutMs).then(() => ({ method: "command" }));1055
}1057
// Streaming exec channel with stdin access. Resolves with the exit code instead of rejecting on1058
// non-zero exits so callers can classify sudo and protocol failures themselves.1059
_execStream(record, command, { timeoutMs = 30000, outputLimit = MAX_COMMAND_OUTPUT, onUpdate } = {}) {1060
let settled = false;1061
let channel = null;1062
let received = 0;1063
let resolveDone; let rejectDone;1064
const decoders = { stdout: new StringDecoder("utf8"), stderr: new StringDecoder("utf8") };1065
const controller = {1066
stdout: "",1067
stderr: "",1068
done: new Promise((resolve, reject) => { resolveDone = resolve; rejectDone = reject; }),1069
write(text) { if (!settled && channel) { try { channel.write(text); } catch {} } },1070
end() { if (channel) { try { channel.end(); } catch {} } },1071
close() { if (channel) { try { channel.close(); } catch {} } }1072
};1073
const finish = (error, result) => {1074
if (settled) return;1075
settled = true;1076
clearTimeout(timer);1077
record.pendingExec?.delete(cancel);1078
if (error) rejectDone(error); else resolveDone(result);1079
};1080
const cancel = () => finish(new SessionError("SESSION_NOT_CONNECTED", "The SSH connection was lost."));1081
record.pendingExec ||= new Set();1082
record.pendingExec.add(cancel);1083
const timer = setTimeout(() => {1084
finish(new SessionError("REMOTE_COMMAND_TIMEOUT", "The remote command timed out."));1085
controller.close();1086
}, timeoutMs);1087
timer.unref?.();1088
const notify = (kind) => {1089
try { onUpdate?.(controller, kind); }1090
catch (error) { finish(error); controller.close(); }1091
};1092
openExec(record.client, command, (error, stream) => {1093
if (settled) { try { stream?.close(); } catch {} return; }1094
if (error) { finish(new SessionError("REMOTE_COMMAND_FAILED", error.message)); return; }1095
channel = stream;1096
const append = (kind, chunk) => {1097
if (settled) return;1098
received += chunk.length;1099
if (received > outputLimit) {1100
finish(new SessionError("REMOTE_OUTPUT_TOO_LARGE", "The remote response is too large."));1101
controller.close();1102
return;1103
}1104
controller[kind] += decoders[kind].write(chunk);1105
notify(kind);1106
};1107
stream.on("data", (chunk) => append("stdout", chunk));1108
stream.stderr?.on("data", (chunk) => append("stderr", chunk));1109
stream.once("error", (streamError) => finish(new SessionError("REMOTE_COMMAND_FAILED", streamError.message)));1110
stream.once("close", (code, signal) => {1111
controller.stdout += decoders.stdout.end();1112
controller.stderr += decoders.stderr.end();1113
finish(null, { code: Number.isInteger(code) ? code : null, signal: signal || null, stdout: controller.stdout, stderr: controller.stderr });1114
});1115
notify("open");1116
});1117
return controller;1118
}1120
_execSudo(record, command, sudoPassword, timeoutMs, outputLimit = MAX_COMMAND_OUTPUT) {1121
return new Promise((resolve, reject) => {1122
let settled = false; let stdout = ""; let stderr = "";1123
let channel;1124
const cancel = () => finish(new SessionError("SESSION_NOT_CONNECTED", "The SSH connection was lost."));1125
record.pendingExec ||= new Set();1126
record.pendingExec.add(cancel);1127
const timer = setTimeout(() => {1128
finish(new SessionError("REMOTE_COMMAND_TIMEOUT", "The remote command timed out."));1129
try { channel?.close(); } catch {}1130
}, timeoutMs);1131
timer.unref?.();1132
const finish = (error, result) => {1133
if (settled) return;1134
settled = true; clearTimeout(timer);1135
record.pendingExec.delete(cancel);1136
if (error) reject(error); else resolve(result);1137
};1138
const isRootSession = record.username === "root";1139
const prefix = isRootSession ? "sh -c " : (sudoPassword ? "sudo -S -p '' -- sh -c " : "sudo -n -- sh -c ");1140
const quoted = `'${String(command).replace(/'/g, `'"'"'`)}'`;1141
openExec(record.client, prefix + quoted, (error, stream) => {1142
if (settled) { try { stream?.close(); } catch {} return; }1143
if (error) { finish(new SessionError("USER_ACTION_FAILED", error.message)); return; }1144
channel = stream;1145
const append = (target, chunk) => {1146
const next = target + chunk.toString("utf8");1147
if (Buffer.byteLength(next, "utf8") > outputLimit) {1148
try { stream.close(); } catch {}1149
finish(new SessionError("REMOTE_OUTPUT_TOO_LARGE", "The user-management response is too large."));1150
return target;1151
}1152
return next;1153
};1154
stream.on("data", (chunk) => { stdout = append(stdout, chunk); });1155
stream.stderr?.on("data", (chunk) => { stderr = append(stderr, chunk); });1156
stream.once("error", (streamError) => finish(new SessionError("USER_ACTION_FAILED", streamError.message)));1157
stream.once("close", (code) => {1158
if (!Number.isInteger(code) || code !== 0) finish(new SessionError("USER_ACTION_FAILED", stderr.trim() || `User command exited with ${code}.`, { remoteExitCode: code, editorStarted: stdout.startsWith(EDITOR_STARTED) }));1159
else finish(null, { stdout, stderr });1160
});1161
if (!isRootSession && sudoPassword) stream.end(`${sudoPassword}\n`); else stream.end();1162
});1163
});1164
}1166
_finalize(record, reason) {1167
if (record.finalized) return;1168
record.finalized = true;1169
record.state = "disconnected";1170
record.stopLatency?.();1171
record.latency = null;1172
for (const cancel of record.pendingExec || []) cancel();1173
if (this.sessions.get(record.sessionId) === record) this.sessions.delete(record.sessionId);1174
try { record.stream?.end(); } catch {}1175
try { record.client?.end(); } catch {}1176
const trailing = record.decoder.end();1177
if (trailing) this.emit("data", { sessionId: record.sessionId, data: trailing });1178
const status = { ...this._publicStatus(record), reason };1179
this.emit("state", status);1180
this.logger?.info("SSH session disconnected.", { sessionId: record.sessionId, profileId: record.profileId, reason });1181
}1183
_publicStatus(record) {1184
return {1185
sessionId: record.sessionId,1186
profileId: record.profileId,1187
label: record.label,1188
host: record.host,1189
port: record.port,1190
username: record.username,1191
authMethod: record.authMethod,1192
state: record.state,1193
latency: record.latency || { pingMs: null, sshRttMs: null, measuredAt: null }1194
};1195
}1196
}1198
module.exports = {1199
MAX_NGINX_CONFIG_BYTES,1200
METRICS_COMMAND,1201
NGINX_INSPECT_COMMAND,1202
SessionError,1203
SessionManager,1204
USERNAME_PATTERN,1205
normalizeNginxConfigPath,1206
normalizeTerminalSize,1207
openExec,1208
parseNginxInspectionOutput,1209
shellQuote,1210
parseSystemMetricsOutput1211
};SHA-256
2e7c3890dd2ba975c2876bd502c3dea72e6c734a6cc81c7c87b222f8a9a7aff3CH-J Proprietary Software License 1.14 · ソースは CH-J Proprietary Software License 1.14 に基づいて提供されています。公開されていても、ライセンス条項が変わったり、追加の権利が付与されたりすることはありません。
アーカイブの SHA-256
476ed8aef0f1e96a5acdf223127b6692acdf4492eeaf5d0816b6e2a634fbbda6