@lethevimlet/sshift 1.0.20 → 1.1.0

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.20",
3
+ "version": "1.1.0",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -302,36 +302,72 @@ function registerSystemEndpoints(app, io) {
302
302
  const updateScriptPath = path.join(dataDir, '.sshift-update.sh');
303
303
  const updateLogPath = path.join(dataDir, '.sshift-update.log');
304
304
 
305
- // Detect if running under systemd (moved before script template so the
306
- // isSystemdManaged and systemdUnit variables are available for interpolation)
307
- let isSystemdManaged = false;
308
- let systemdUnit = '';
305
+ // Detect service manager (systemd, launchd, Task Scheduler) so the
306
+ // update script can use the right stop/restart commands instead of exec.
307
+ // This prevents race conditions where the service manager restarts the
308
+ // old version before npm install completes.
309
+ let isServiceManaged = false;
310
+ let serviceManager = ''; // 'systemd', 'launchd', 'taskscheduler'
311
+ let serviceUnit = '';
309
312
  try {
310
- // Check if this process is managed by systemd via cgroup
313
+ // Linux: check if this process is managed by systemd via cgroup
311
314
  const myPid = process.pid;
312
315
  const myCgroup = fs.readFileSync(`/proc/${myPid}/cgroup`, 'utf8');
313
316
  if (myCgroup.includes('sshift')) {
314
- isSystemdManaged = true;
315
- systemdUnit = 'sshift.service';
316
- console.log('[UPDATE] Detected systemd-managed process via cgroup, unit:', systemdUnit);
317
+ isServiceManaged = true;
318
+ serviceManager = 'systemd';
319
+ serviceUnit = 'sshift.service';
320
+ console.log('[UPDATE] Detected systemd-managed process via cgroup, unit:', serviceUnit);
317
321
  }
318
322
  } catch (e) {
319
323
  // /proc not available (non-Linux), ignore
320
324
  }
321
- // Fallback: check if sshift.service is active in systemd
322
- if (!isSystemdManaged) {
325
+ // Fallback for Linux: check if sshift.service is active in systemd
326
+ if (!isServiceManaged && platform === 'linux') {
323
327
  try {
324
328
  const { execSync: execSync2 } = require('child_process');
325
329
  const isActive = execSync2('systemctl is-active sshift.service 2>/dev/null', { encoding: 'utf8' }).trim();
326
330
  if (isActive === 'active') {
327
- isSystemdManaged = true;
328
- systemdUnit = 'sshift.service';
329
- console.log('[UPDATE] Detected active systemd service:', systemdUnit);
331
+ isServiceManaged = true;
332
+ serviceManager = 'systemd';
333
+ serviceUnit = 'sshift.service';
334
+ console.log('[UPDATE] Detected active systemd service:', serviceUnit);
330
335
  }
331
336
  } catch (e) {
332
337
  // Not a systemd service or systemctl not available
333
338
  }
334
339
  }
340
+ // macOS: check if sshift is managed by launchd
341
+ if (!isServiceManaged && platform === 'darwin') {
342
+ try {
343
+ const { execSync: execSync2 } = require('child_process');
344
+ // Check for the sshift launchd plist (installed by sshift-install.sh)
345
+ const plistCheck = execSync2('launchctl list com.sshift.server 2>/dev/null', { encoding: 'utf8' }).trim();
346
+ if (plistCheck && plistCheck.length > 0) {
347
+ isServiceManaged = true;
348
+ serviceManager = 'launchd';
349
+ serviceUnit = 'com.sshift.server';
350
+ console.log('[UPDATE] Detected launchd-managed service:', serviceUnit);
351
+ }
352
+ } catch (e) {
353
+ // Not a launchd service
354
+ }
355
+ }
356
+ // Windows: check if sshift is in Task Scheduler
357
+ if (!isServiceManaged && platform === 'win32') {
358
+ try {
359
+ const { execSync: execSync2 } = require('child_process');
360
+ const taskCheck = execSync2('schtasks /query /tn sshift 2>nul', { encoding: 'utf8' }).trim();
361
+ if (taskCheck && taskCheck.includes('sshift')) {
362
+ isServiceManaged = true;
363
+ serviceManager = 'taskscheduler';
364
+ serviceUnit = 'sshift';
365
+ console.log('[UPDATE] Detected Task Scheduler task:', serviceUnit);
366
+ }
367
+ } catch (e) {
368
+ // Not a scheduled task
369
+ }
370
+ }
335
371
 
336
372
  // Build PATH for the update script to ensure npm and node are findable
337
373
  const pathDirs = [];
@@ -426,14 +462,15 @@ NPM_BIN="${npmBinPath}"
426
462
  SSHIFT_FALLBACK="${sshiftBinPath}"
427
463
  NODE_BIN="${nodeExe}"
428
464
  OLD_VERSION="${oldVersion}"
429
- SYSTEMD_MANAGED="${isSystemdManaged ? '1' : '0'}"
430
- SYSTEMD_UNIT="${systemdUnit}"
465
+ SERVICE_MANAGED="${isServiceManaged ? '1' : '0'}"
466
+ SERVICE_MANAGER="${serviceManager}"
467
+ SERVICE_UNIT="${serviceUnit}"
431
468
 
432
469
  echo "$(date): Starting sshift update (from v${oldVersion})..." > "$LOG"
433
470
  echo "$(date): npm path: $NPM_BIN" >> "$LOG"
434
471
  echo "$(date): node path: $NODE_BIN" >> "$LOG"
435
472
  echo "$(date): sshift fallback: $SSHIFT_FALLBACK" >> "$LOG"
436
- echo "$(date): systemd_managed: $SYSTEMD_MANAGED" >> "$LOG"
473
+ echo "$(date): service_managed: $SERVICE_MANAGED manager: $SERVICE_MANAGER unit: $SERVICE_UNIT" >> "$LOG"
437
474
 
438
475
  # Find the current sshift binary before npm changes anything
439
476
  CURRENT_SSHIFT="$(command -v sshift 2>/dev/null || which sshift 2>/dev/null || echo "$SSHIFT_FALLBACK")"
@@ -463,9 +500,12 @@ if [ "$NPM_SUCCESS" != "true" ]; then
463
500
  echo "npm install failed" >> "$LOG"
464
501
  rm -f "$MARKER"
465
502
  # Always restart the server — the old version is still installed
466
- if [ "$SYSTEMD_MANAGED" = "1" ]; then
503
+ if [ "$SERVICE_MANAGED" = "1" ] && [ "$SERVICE_MANAGER" = "systemd" ]; then
467
504
  echo "$(date): Restarting via systemd (old version)" >> "$LOG"
468
- systemctl start "$SYSTEMD_UNIT" >> "$LOG" 2>&1
505
+ systemctl start "$SERVICE_UNIT" >> "$LOG" 2>&1
506
+ elif [ "$SERVICE_MANAGED" = "1" ] && [ "$SERVICE_MANAGER" = "launchd" ]; then
507
+ echo "$(date): Restarting via launchd (old version)" >> "$LOG"
508
+ launchctl start "$SERVICE_UNIT" >> "$LOG" 2>&1
469
509
  else
470
510
  echo "$(date): Restarting sshift (old version) from: $CURRENT_SSHIFT" >> "$LOG"
471
511
  exec "$NODE_BIN" "$CURRENT_SSHIFT"
@@ -477,9 +517,15 @@ echo "$(date): npm install completed successfully" >> "$LOG"
477
517
  # Remove the update marker before restart
478
518
  rm -f "$MARKER"
479
519
 
480
- if [ "$SYSTEMD_MANAGED" = "1" ]; then
520
+ if [ "$SERVICE_MANAGED" = "1" ] && [ "$SERVICE_MANAGER" = "systemd" ]; then
481
521
  echo "$(date): Restarting via systemd" >> "$LOG"
482
- systemctl restart "$SYSTEMD_UNIT" >> "$LOG" 2>&1
522
+ systemctl restart "$SERVICE_UNIT" >> "$LOG" 2>&1
523
+ elif [ "$SERVICE_MANAGED" = "1" ] && [ "$SERVICE_MANAGER" = "launchd" ]; then
524
+ echo "$(date): Restarting via launchd" >> "$LOG"
525
+ # launchd doesn't have restart; stop and start
526
+ launchctl stop "$SERVICE_UNIT" >> "$LOG" 2>&1 || true
527
+ sleep 1
528
+ launchctl start "$SERVICE_UNIT" >> "$LOG" 2>&1
483
529
  else
484
530
  # Find the sshift binary — prefer PATH lookup since npm just updated it
485
531
  SSHIFT_BIN="$(command -v sshift 2>/dev/null || which sshift 2>/dev/null || echo "$SSHIFT_FALLBACK")"
@@ -515,11 +561,13 @@ fi
515
561
  const updateEnv = { ...process.env };
516
562
  updateEnv.PATH = scriptPath;
517
563
 
518
- // When running under systemd, spawn the update script in a transient scope
519
- // so it survives `systemctl restart` of the sshift service. Without this,
520
- // the script is in the same cgroup as sshift and gets killed when the
521
- // service restarts.
522
- if (isSystemdManaged && platform !== 'win32') {
564
+ // When running under a service manager (systemd/launchd), spawn the update
565
+ // script in its own scope so it survives a service restart.
566
+ // For systemd: use systemd-run --scope to escape the service cgroup.
567
+ // For launchd on macOS: no special handling needed (launchd doesn't kill
568
+ // child processes on stop like systemd does, it only sends SIGTERM to the
569
+ // main process, and our process.exit(0) handles that).
570
+ if (isServiceManaged && serviceManager === 'systemd' && platform !== 'win32') {
523
571
  try {
524
572
  const systemdRunCheck = require('child_process').execSync('command -v systemd-run', { encoding: 'utf8' }).trim();
525
573
  if (systemdRunCheck) {
@@ -548,11 +596,11 @@ fi
548
596
  updateProcess.unref();
549
597
 
550
598
  // Give the update script enough time to start before exiting.
551
- // Under systemd: we exit so the service can be restarted by `systemctl
552
- // restart` in the script. The script runs in a separate systemd scope
553
- // so it survives the service restart.
554
- // Non-systemd: we exit so the script can exec the new binary.
555
- const exitDelay = isSystemdManaged ? 1000 : 2000;
599
+ // Under systemd: we exit so systemctl restart can bring up the new version.
600
+ // The script runs in a separate systemd scope so it survives the restart.
601
+ // Under launchd: we exit so launchd stop/start can bring up the new version.
602
+ // For non-managed: we exit so the script can exec the new binary.
603
+ const exitDelay = isServiceManaged ? 1000 : 2000;
556
604
  setTimeout(() => {
557
605
  if (spawnFailed) {
558
606
  console.error('[UPDATE] Update process failed to start. Keeping server running.');
package/sshift CHANGED
@@ -37,7 +37,6 @@ const os = require('os');
37
37
  // Get the directory where this script is located
38
38
  const scriptDir = __dirname;
39
39
  const serverPath = path.join(scriptDir, 'src', 'server', 'index.js');
40
- const pidFile = path.join(os.homedir(), '.local', 'share', 'sshift', '.sshift.pid');
41
40
 
42
41
  // CLI argument parsing
43
42
  const args = process.argv.slice(2);
@@ -83,6 +82,9 @@ for (let i = 0; i < args.length; i++) {
83
82
  }
84
83
  }
85
84
 
85
+ // PID file: dev instance uses a separate file so both can run side by side
86
+ const pidFile = path.join(os.homedir(), '.local', 'share', 'sshift', options.dev ? '.sshift-dev.pid' : '.sshift.pid');
87
+
86
88
  // Show help
87
89
  if (options.help) {
88
90
  console.log(`
@@ -96,12 +98,15 @@ Usage:
96
98
  ./sshift --stop Stop running instance
97
99
  ./sshift --restart Restart running instance
98
100
  ./sshift --status Check if sshift is running
101
+ ./sshift --dev --status Check if dev instance is running
102
+ ./sshift --dev --stop Stop dev instance
103
+ ./sshift --dev --restart Restart dev instance
99
104
  ./sshift --help Show this help message
100
105
 
101
106
  Options:
102
107
  -p, --port <port> Server port (default: 8022, dev: 3000)
103
108
  -b, --bind <address> Bind address (default: 0.0.0.0)
104
- -d, --dev Development mode
109
+ -d, --dev Development mode (separate PID file for --status/--stop/--restart)
105
110
  -s, --stop Stop running instance
106
111
  -r, --restart Restart running instance
107
112
  --status Check running status
@@ -183,15 +188,15 @@ function isRunning() {
183
188
  }
184
189
 
185
190
  // Helper: Stop running instance
186
- function stopInstance() {
191
+ function stopInstance(label) {
187
192
  const status = isRunning();
188
193
 
189
194
  if (!status.running) {
190
- console.log('sshift is not running');
195
+ console.log(`${label} is not running`);
191
196
  return true;
192
197
  }
193
198
 
194
- console.log(`Stopping sshift (PID: ${status.pid})...`);
199
+ console.log(`Stopping ${label} (PID: ${status.pid})...`);
195
200
 
196
201
  try {
197
202
  process.kill(status.pid, 'SIGTERM');
@@ -225,10 +230,10 @@ function stopInstance() {
225
230
  fs.unlinkSync(pidFile);
226
231
  }
227
232
 
228
- console.log('sshift stopped successfully');
233
+ console.log(`${label} stopped successfully`);
229
234
  return true;
230
235
  } catch (e) {
231
- console.error(`Error stopping sshift: ${e.message}`);
236
+ console.error(`Error stopping ${label}: ${e.message}`);
232
237
  return false;
233
238
  }
234
239
  }
@@ -236,25 +241,28 @@ function stopInstance() {
236
241
  // Handle --status
237
242
  if (options.status) {
238
243
  const status = isRunning();
244
+ const label = options.dev ? 'sshift (dev)' : 'sshift';
239
245
  if (status.running) {
240
- console.log(`sshift is running (PID: ${status.pid})`);
246
+ console.log(`${label} is running (PID: ${status.pid})`);
241
247
  process.exit(0);
242
248
  } else {
243
- console.log('sshift is not running');
249
+ console.log(`${label} is not running`);
244
250
  process.exit(1);
245
251
  }
246
252
  }
247
253
 
248
254
  // Handle --stop
249
255
  if (options.stop) {
250
- const success = stopInstance();
256
+ const label = options.dev ? 'sshift (dev)' : 'sshift';
257
+ const success = stopInstance(label);
251
258
  process.exit(success ? 0 : 1);
252
259
  }
253
260
 
254
261
  // Handle --restart
255
262
  if (options.restart) {
256
- console.log('Restarting sshift...');
257
- stopInstance();
263
+ const label = options.dev ? 'sshift (dev)' : 'sshift';
264
+ console.log(`Restarting ${label}...`);
265
+ stopInstance(label);
258
266
  // Continue to start below
259
267
  }
260
268