@myassis/gateway 1.0.43 → 1.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -503,31 +503,49 @@ async function updateService() {
|
|
|
503
503
|
response.data.on('error', reject);
|
|
504
504
|
});
|
|
505
505
|
console.log('✅ 下载完成');
|
|
506
|
-
//
|
|
506
|
+
// 生成延迟替换脚本:Windows 已改为用户级进程(PID 文件 + Run key + launcher.ps1)
|
|
507
|
+
// 因此更新脚本不再依赖 nssm,而是按 PID 文件 kill 旧进程 -> 替换 exe -> 调 launcher.ps1 重启
|
|
507
508
|
if (isWin) {
|
|
508
|
-
const nssmPath = getNssmPath();
|
|
509
|
-
if (!nssmPath || !fs_1.default.existsSync(nssmPath)) {
|
|
510
|
-
throw new Error('未找到 nssm.exe,无法执行 Windows 服务更新');
|
|
511
|
-
}
|
|
512
509
|
const escapePowerShellString = (value) => value.replace(/'/g, "''");
|
|
513
510
|
const updateScriptPath = path_1.default.join(os_1.default.tmpdir(), 'myassis-gateway-update.ps1');
|
|
514
511
|
const psScript = [
|
|
515
512
|
`$ErrorActionPreference = 'Stop'`,
|
|
516
|
-
`$nssm = '${escapePowerShellString(nssmPath)}'`,
|
|
517
513
|
`$serviceName = '${escapePowerShellString(exports.SERVICE_NAME)}'`,
|
|
518
514
|
`$currentExe = '${escapePowerShellString(currentExe)}'`,
|
|
519
515
|
`$tmpExe = '${escapePowerShellString(tmpExe)}'`,
|
|
520
516
|
`$backupExe = '${escapePowerShellString(`${currentExe}.bak`)}'`,
|
|
517
|
+
`$pidFile = '${escapePowerShellString(GATEWAY_PID_FILE)}'`,
|
|
518
|
+
`$launcher = '${escapePowerShellString(GATEWAY_LAUNCHER_FILE)}'`,
|
|
521
519
|
`$logPath = Join-Path $env:TEMP 'myassis-gateway-update.log'`,
|
|
522
520
|
`$logDir = Split-Path -Parent $logPath`,
|
|
523
521
|
`if (-not (Test-Path $logDir)) { New-Item -ItemType Directory -Path $logDir -Force | Out-Null }`,
|
|
524
522
|
`function Write-UpdateLog { param([string]$Message) Add-Content -Path $logPath -Encoding UTF8 -Value \"$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') $Message\" }`,
|
|
523
|
+
`function Stop-GatewayProcess {`,
|
|
524
|
+
` if (-not (Test-Path $pidFile)) { Write-UpdateLog 'pid file not found, skip stop'; return }`,
|
|
525
|
+
` try {`,
|
|
526
|
+
` $pidValue = (Get-Content $pidFile -Raw).Trim()`,
|
|
527
|
+
` if (-not $pidValue) { return }`,
|
|
528
|
+
` $pidInt = [int]$pidValue`,
|
|
529
|
+
` Write-UpdateLog \"stopping gateway process pid=$pidInt\"`,
|
|
530
|
+
` try { Stop-Process -Id $pidInt -Force -ErrorAction Stop } catch { Write-UpdateLog \"stop-process failed: $($_.Exception.Message)\" }`,
|
|
531
|
+
` for ($w = 0; $w -lt 30; $w++) {`,
|
|
532
|
+
` $p = Get-Process -Id $pidInt -ErrorAction SilentlyContinue`,
|
|
533
|
+
` if (-not $p) { break }`,
|
|
534
|
+
` Start-Sleep -Milliseconds 500`,
|
|
535
|
+
` }`,
|
|
536
|
+
` Remove-Item $pidFile -Force -ErrorAction SilentlyContinue`,
|
|
537
|
+
` } catch { Write-UpdateLog \"stop gateway error: $($_.Exception.Message)\" }`,
|
|
538
|
+
`}`,
|
|
539
|
+
`function Start-GatewayProcess {`,
|
|
540
|
+
` if (-not (Test-Path $launcher)) { Write-UpdateLog \"launcher script not found: $launcher\"; return }`,
|
|
541
|
+
` Write-UpdateLog 'starting gateway via launcher.ps1'`,
|
|
542
|
+
` Start-Process -FilePath 'powershell.exe' -ArgumentList @('-NoProfile','-ExecutionPolicy','Bypass','-WindowStyle','Hidden','-File',$launcher) -WindowStyle Hidden`,
|
|
543
|
+
`}`,
|
|
525
544
|
`try {`,
|
|
526
545
|
` Write-UpdateLog 'update script started'`,
|
|
527
546
|
` Start-Sleep -Seconds 5`,
|
|
528
|
-
`
|
|
529
|
-
`
|
|
530
|
-
` Start-Sleep -Seconds 2`,
|
|
547
|
+
` Stop-GatewayProcess`,
|
|
548
|
+
` Start-Sleep -Seconds 1`,
|
|
531
549
|
` Write-UpdateLog 'waiting executable unlock'`,
|
|
532
550
|
` $moved = $false`,
|
|
533
551
|
` for ($i = 0; $i -lt 60; $i++) {`,
|
|
@@ -544,19 +562,18 @@ async function updateService() {
|
|
|
544
562
|
` Write-UpdateLog 'moving new executable'`,
|
|
545
563
|
` Move-Item $tmpExe $currentExe -Force`,
|
|
546
564
|
` Start-Sleep -Seconds 1`,
|
|
547
|
-
`
|
|
548
|
-
` & $nssm start $serviceName | Out-Null`,
|
|
565
|
+
` Start-GatewayProcess`,
|
|
549
566
|
` Remove-Item $backupExe -Force -ErrorAction SilentlyContinue`,
|
|
550
567
|
` Remove-Item $tmpExe -Force -ErrorAction SilentlyContinue`,
|
|
551
568
|
` Write-UpdateLog 'update script finished'`,
|
|
552
569
|
`} catch {`,
|
|
553
570
|
` Write-UpdateLog \"update script failed: $($_.Exception.Message)\"`,
|
|
554
|
-
` try {
|
|
571
|
+
` try { Start-GatewayProcess } catch { Write-UpdateLog \"restart gateway failed: $($_.Exception.Message)\" }`,
|
|
555
572
|
` throw`,
|
|
556
573
|
`}`,
|
|
557
574
|
].join('\r\n');
|
|
558
575
|
fs_1.default.writeFileSync(updateScriptPath, `\uFEFF${psScript}`, 'utf8');
|
|
559
|
-
await execAsync(`cmd.exe /c start \"\" powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"${updateScriptPath}\"`, {
|
|
576
|
+
await execAsync(`cmd.exe /c start \"\" powershell.exe -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File \"${updateScriptPath}\"`, {
|
|
560
577
|
timeout: 10000,
|
|
561
578
|
windowsHide: true
|
|
562
579
|
});
|
|
@@ -10,7 +10,6 @@ const path_1 = __importDefault(require("path"));
|
|
|
10
10
|
const os_1 = __importDefault(require("os"));
|
|
11
11
|
const shared_1 = require("@myassis/shared");
|
|
12
12
|
const crypto_1 = __importDefault(require("crypto"));
|
|
13
|
-
const ServiceManager_js_1 = require("../ServiceManager.js");
|
|
14
13
|
const logger = (0, shared_1.getLogger)('exec');
|
|
15
14
|
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
16
15
|
// 待批准的命令缓存:token -> { command, cwd, timeout, sessionId, expiresAt }
|
|
@@ -176,23 +175,6 @@ exports.execTool = {
|
|
|
176
175
|
return;
|
|
177
176
|
}
|
|
178
177
|
}
|
|
179
|
-
// Windows 下通过 Helper 以登录用户身份执行
|
|
180
|
-
if (process.platform === 'win32') {
|
|
181
|
-
try {
|
|
182
|
-
const result = await (0, ServiceManager_js_1.execAsUser)(command, cwd);
|
|
183
|
-
resolve({
|
|
184
|
-
success: result.exitCode === 0,
|
|
185
|
-
output: result.stdout.substring(0, 100000),
|
|
186
|
-
errorMessage: result.stderr.substring(0, 100000),
|
|
187
|
-
exitCode: result.exitCode,
|
|
188
|
-
});
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
catch (e) {
|
|
192
|
-
resolve({ success: false, errorMessage: e?.message || String(e) });
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
178
|
const options = {
|
|
197
179
|
cwd,
|
|
198
180
|
timeout,
|