@lethevimlet/sshift 1.0.8 → 1.0.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -323,22 +323,51 @@ $LogPath = '${updateLogPath}'
323
323
  $NodeExe = '${nodeExe}'
324
324
  $SshiftBin = '${sshiftBinPath}'
325
325
 
326
+ # Check if running under Task Scheduler
327
+ $TaskName = "sshift"
328
+ $IsScheduled = $false
329
+ try {
330
+ $Task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
331
+ if ($Task -ne $null) {
332
+ $IsScheduled = $true
333
+ Write-Output "Detected Task Scheduler task: $TaskName" | Out-File -FilePath $LogPath -Append
334
+ Write-Output "Stopping scheduled task..." | Out-File -FilePath $LogPath -Append
335
+ Stop-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
336
+ Start-Sleep -Seconds 2
337
+ }
338
+ } catch {}
339
+
326
340
  try {
327
341
  & npm install -g @lethevimlet/sshift@latest 2>&1 | Out-File -FilePath $LogPath -Append
328
342
  if ($LASTEXITCODE -ne 0) {
329
343
  Write-Output "npm install failed with exit code $LASTEXITCODE" | Out-File -FilePath $LogPath -Append
330
344
  Remove-Item -Path $MarkerPath -Force -ErrorAction SilentlyContinue
331
- & $NodeExe $SshiftBin
345
+ if ($IsScheduled) {
346
+ Write-Output "Starting scheduled task (old version)" | Out-File -FilePath $LogPath -Append
347
+ Start-ScheduledTask -TaskName $TaskName
348
+ } else {
349
+ & $NodeExe $SshiftBin
350
+ }
332
351
  exit 1
333
352
  }
334
353
  } catch {
335
354
  Write-Output $_.Exception.Message | Out-File -FilePath $LogPath -Append
336
355
  Remove-Item -Path $MarkerPath -Force -ErrorAction SilentlyContinue
337
- & $NodeExe $SshiftBin
356
+ if ($IsScheduled) {
357
+ Write-Output "Starting scheduled task (old version)" | Out-File -FilePath $LogPath -Append
358
+ Start-ScheduledTask -TaskName $TaskName
359
+ } else {
360
+ & $NodeExe $SshiftBin
361
+ }
338
362
  exit 1
339
363
  }
340
364
  Remove-Item -Path $MarkerPath -Force -ErrorAction SilentlyContinue
341
- & $NodeExe $SshiftBin
365
+ if ($IsScheduled) {
366
+ Write-Output "Starting scheduled task" | Out-File -FilePath $LogPath -Append
367
+ Start-ScheduledTask -TaskName $TaskName
368
+ } else {
369
+ & $NodeExe $SshiftBin
370
+ }
342
371
  `;
343
372
  const psScriptPath = updateScriptPath.replace('.sh', '.ps1');
344
373
  fs.writeFileSync(psScriptPath, psScript, 'utf8');
@@ -356,6 +385,28 @@ echo "$(date): npm path: $NPM_BIN" >> "$LOG"
356
385
  echo "$(date): node path: $NODE_BIN" >> "$LOG"
357
386
  echo "$(date): sshift fallback: $SSHIFT_FALLBACK" >> "$LOG"
358
387
 
388
+ # Check if running under systemd
389
+ SYSTEMD_UNIT=""
390
+ if command -v systemctl >/dev/null 2>&1; then
391
+ SYSTEMD_UNIT="$(systemctl show -p Id -value sshift.service 2>/dev/null || true)"
392
+ if [ -z "$SYSTEMD_UNIT" ]; then
393
+ # Try to find sshift in systemd by looking at the running process
394
+ _my_pid="$$"
395
+ _my_cgroup="$(cat /proc/$_my_pid/cgroup 2>/dev/null | grep -o 'name=systemd.*sshift[.]service' | head -1)"
396
+ if [ -n "$_my_cgroup" ]; then
397
+ SYSTEMD_UNIT="sshift.service"
398
+ fi
399
+ fi
400
+ fi
401
+
402
+ if [ -n "$SYSTEMD_UNIT" ]; then
403
+ echo "$(date): Detected systemd service: $SYSTEMD_UNIT" >> "$LOG"
404
+ # Stop the service before updating to prevent systemd from restarting old version
405
+ echo "$(date): Stopping $SYSTEMD_UNIT..." >> "$LOG"
406
+ systemctl stop "$SYSTEMD_UNIT" >> "$LOG" 2>&1 || true
407
+ sleep 1
408
+ fi
409
+
359
410
  # Find the current sshift binary before npm changes anything
360
411
  CURRENT_SSHIFT="$(command -v sshift 2>/dev/null || which sshift 2>/dev/null || echo "$SSHIFT_FALLBACK")"
361
412
 
@@ -366,24 +417,35 @@ if ! "$NPM_BIN" install -g @lethevimlet/sshift@latest >> "$LOG" 2>&1; then
366
417
  echo "npm install failed" >> "$LOG"
367
418
  rm -f "$MARKER"
368
419
  # Always restart the server — the old version is still installed
369
- echo "$(date): Restarting sshift (old version) from: $CURRENT_SSHIFT" >> "$LOG"
370
- exec "$NODE_BIN" "$CURRENT_SSHIFT"
420
+ if [ -n "$SYSTEMD_UNIT" ]; then
421
+ echo "$(date): Restarting via systemd (old version)" >> "$LOG"
422
+ systemctl start "$SYSTEMD_UNIT" >> "$LOG" 2>&1
423
+ else
424
+ echo "$(date): Restarting sshift (old version) from: $CURRENT_SSHIFT" >> "$LOG"
425
+ exec "$NODE_BIN" "$CURRENT_SSHIFT"
426
+ fi
427
+ exit 1
371
428
  fi
372
429
  echo "$(date): npm install completed successfully" >> "$LOG"
373
430
 
374
431
  # Remove the update marker before restart
375
432
  rm -f "$MARKER"
376
433
 
377
- # Find the sshift binary — prefer PATH lookup since npm just updated it
378
- SSHIFT_BIN="$(command -v sshift 2>/dev/null || which sshift 2>/dev/null || echo "$SSHIFT_FALLBACK")"
379
- if [ ! -x "$SSHIFT_BIN" ]; then
380
- echo "$(date): ERROR: sshift binary not found or not executable at: $SSHIFT_BIN" >> "$LOG"
381
- echo "$(date): Attempting to restart with fallback path" >> "$LOG"
382
- SSHIFT_BIN="$SSHIFT_FALLBACK"
434
+ if [ -n "$SYSTEMD_UNIT" ]; then
435
+ echo "$(date): Restarting via systemd" >> "$LOG"
436
+ systemctl start "$SYSTEMD_UNIT" >> "$LOG" 2>&1
437
+ else
438
+ # Find the sshift binary — prefer PATH lookup since npm just updated it
439
+ SSHIFT_BIN="$(command -v sshift 2>/dev/null || which sshift 2>/dev/null || echo "$SSHIFT_FALLBACK")"
440
+ if [ ! -x "$SSHIFT_BIN" ]; then
441
+ echo "$(date): ERROR: sshift binary not found or not executable at: $SSHIFT_BIN" >> "$LOG"
442
+ echo "$(date): Attempting to restart with fallback path" >> "$LOG"
443
+ SSHIFT_BIN="$SSHIFT_FALLBACK"
444
+ fi
445
+
446
+ echo "$(date): Restarting sshift from: $SSHIFT_BIN" >> "$LOG"
447
+ exec "$NODE_BIN" "$SSHIFT_BIN"
383
448
  fi
384
-
385
- echo "$(date): Restarting sshift from: $SSHIFT_BIN" >> "$LOG"
386
- exec "$NODE_BIN" "$SSHIFT_BIN"
387
449
  `;
388
450
  fs.writeFileSync(updateScriptPath, updateScript, 'utf8');
389
451
  fs.chmodSync(updateScriptPath, 0o755);
@@ -394,6 +456,32 @@ exec "$NODE_BIN" "$SSHIFT_BIN"
394
456
  console.log('[UPDATE] sshift restart path:', sshiftBinPath);
395
457
  console.log('[UPDATE] Log file:', updateLogPath);
396
458
 
459
+ // Detect if running under systemd
460
+ let isSystemdManaged = false;
461
+ let systemdUnit = '';
462
+ try {
463
+ const { execSync: execSync2 } = require('child_process');
464
+ // Check if this process is managed by systemd
465
+ const myCgroup = fs.readFileSync(`/proc/${process.pid}/cgroup`, 'utf8');
466
+ if (myCgroup.includes('sshift')) {
467
+ isSystemdManaged = true;
468
+ systemdUnit = 'sshift.service';
469
+ console.log('[UPDATE] Detected systemd-managed process, unit:', systemdUnit);
470
+ } else if (process.ppid === 1) {
471
+ // Parent is init (systemd), check for sshift service
472
+ try {
473
+ execSync2('systemctl is-enabled sshift.service 2>/dev/null', { encoding: 'utf8' });
474
+ isSystemdManaged = true;
475
+ systemdUnit = 'sshift.service';
476
+ console.log('[UPDATE] Detected systemd service:', systemdUnit);
477
+ } catch (e) {
478
+ // Not a systemd service
479
+ }
480
+ }
481
+ } catch (e) {
482
+ // /proc not available (non-Linux), ignore
483
+ }
484
+
397
485
  let spawnFailed = false;
398
486
  let spawnCommand, spawnArgs;
399
487
  if (platform === 'win32') {
@@ -425,14 +513,24 @@ exec "$NODE_BIN" "$SSHIFT_BIN"
425
513
  // Give the update script enough time to start before exiting.
426
514
  // The script runs npm install (which can take time) in the background,
427
515
  // but we need to wait long enough for the spawn to succeed.
516
+ // When running under systemd, the update script will stop/start the service,
517
+ // so we DON'T exit ourselves — the script's `systemctl stop` will kill us.
518
+ // For non-systemd, we exit so the script can exec the new binary.
519
+ const exitDelay = isSystemdManaged ? 500 : 2000;
428
520
  setTimeout(() => {
429
521
  if (spawnFailed) {
430
522
  console.error('[UPDATE] Update process failed to start. Keeping server running.');
431
523
  return;
432
524
  }
525
+ if (isSystemdManaged) {
526
+ // Under systemd, the update script will call `systemctl stop` to stop us.
527
+ // We don't need to exit ourselves. Just keep running until systemd stops us.
528
+ console.log('[UPDATE] Update script running under systemd. Service will be stopped by the script.');
529
+ return;
530
+ }
433
531
  console.log('[UPDATE] Exiting for update...');
434
532
  process.exit(0);
435
- }, 2000);
533
+ }, exitDelay);
436
534
  } catch (err) {
437
535
  console.error('Error triggering update:', err);
438
536
  res.status(500).json({ error: 'Failed to trigger update' });