@lethevimlet/sshift 0.9.6 → 0.9.8

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.
@@ -0,0 +1,986 @@
1
+ #!/bin/bash
2
+ # sshift Installation Script for Linux/macOS
3
+ # Installs Node.js (if not present) and installs sshift via npm
4
+ # Checks for updates and restarts the app if needed
5
+ #
6
+ # Usage: ./sshift-install.sh [OPTIONS]
7
+ # --install-dir DIR Installation directory (default: ~/.local/share/sshift)
8
+ # --port PORT Server port (default: 8022)
9
+ # --start Start sshift after installation/update
10
+ # --stop Stop running sshift instance
11
+ # --restart Restart sshift
12
+ # --status Check if sshift is running
13
+ # --update Update existing installation (non-interactive)
14
+ # --uninstall Remove sshift from the system
15
+ # --help Show this help message
16
+
17
+ set -e
18
+
19
+ NODE_VERSION="20"
20
+ NPM_PACKAGE="@lethevimlet/sshift"
21
+ INSTALL_DIR="$HOME/.local/share/sshift"
22
+ PID_FILE="/tmp/sshift.pid"
23
+ SERVICE_NAME="sshift"
24
+ SERVER_PORT=""
25
+
26
+ RED='\033[0;31m'
27
+ GREEN='\033[0;32m'
28
+ YELLOW='\033[1;33m'
29
+ BLUE='\033[0;34m'
30
+ CYAN='\033[0;36m'
31
+ BOLD='\033[1m'
32
+ NC='\033[0m'
33
+
34
+ info() { echo -e "${BLUE}[INFO]${NC} $1"; }
35
+ success() { echo -e "${GREEN}[OK]${NC} $1"; }
36
+ warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
37
+ error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
38
+
39
+ prompt_user() {
40
+ local prompt="$1"
41
+ local default="${2:-}"
42
+
43
+ if [ -t 0 ]; then
44
+ # stdin is a terminal, read normally
45
+ read -p "$prompt" -n 1 -r
46
+ echo
47
+ PROMPT_RESPONSE="$REPLY"
48
+ else
49
+ # No interactive terminal (piped or non-interactive, e.g. curl | bash)
50
+ # Try /dev/tty first so the user can still interact
51
+ local prompted=false
52
+ if [ -e /dev/tty ]; then
53
+ printf "%s" "$prompt" > /dev/tty 2>/dev/null && prompted=true
54
+ fi
55
+ if $prompted; then
56
+ IFS= read -n 1 -r < /dev/tty 2>/dev/null
57
+ echo > /dev/tty 2>/dev/null
58
+ PROMPT_RESPONSE="${REPLY:-$default}"
59
+ elif [ -n "$default" ]; then
60
+ # No terminal available at all, use default
61
+ info "$prompt(default: $default)"
62
+ PROMPT_RESPONSE="$default"
63
+ else
64
+ error "Cannot read user input (no terminal available) and no default value"
65
+ fi
66
+ fi
67
+ }
68
+
69
+ show_help() {
70
+ cat << EOF
71
+ sshift Installation Script for Linux/macOS
72
+
73
+ Usage: ./sshift-install.sh [OPTIONS]
74
+
75
+ Options:
76
+ --install-dir DIR Installation directory (default: ~/.local/share/sshift)
77
+ --port PORT Server port (default: 8022)
78
+ --start Start sshift after installation/update
79
+ --stop Stop running sshift instance
80
+ --restart Restart sshift
81
+ --status Check if sshift is running
82
+ --update Update existing installation (non-interactive)
83
+ --uninstall Remove sshift from the system
84
+ --help Show this help message
85
+
86
+ Examples:
87
+ ./sshift-install.sh # Install with defaults
88
+ ./sshift-install.sh --port 8080 # Install with custom port
89
+ ./sshift-install.sh --install-dir /opt/sshift # Install to custom directory
90
+ ./sshift-install.sh --update # Update existing installation
91
+ ./sshift-install.sh --start # Start sshift
92
+ ./sshift-install.sh --stop # Stop sshift
93
+ ./sshift-install.sh --restart # Restart sshift
94
+ ./sshift-install.sh --status # Check status
95
+ ./sshift-install.sh --uninstall # Remove sshift
96
+
97
+ One-liner installation:
98
+ curl -fsSL https://raw.githubusercontent.com/lethevimlet/sshift/main/sshift-install.sh | bash
99
+ wget -qO- https://raw.githubusercontent.com/lethevimlet/sshift/main/sshift-install.sh | bash
100
+ EOF
101
+ exit 0
102
+ }
103
+
104
+ command_exists() {
105
+ command -v "$1" >/dev/null 2>&1
106
+ }
107
+
108
+ get_installed_version() {
109
+ if command_exists sshift; then
110
+ local version=$(npm list -g @lethevimlet/sshift --depth=0 --json 2>/dev/null | grep -oP '"version":\s*"\K[0-9]+\.[0-9]+\.[0-9]+' | head -1)
111
+ if [ -n "$version" ]; then
112
+ echo "$version"
113
+ else
114
+ npm list -g @lethevimlet/sshift --depth=0 2>/dev/null | grep -oP '@lethevimlet/sshift@\K[0-9]+\.[0-9]+\.[0-9]+' || echo "0.0.0"
115
+ fi
116
+ else
117
+ echo "0.0.0"
118
+ fi
119
+ }
120
+
121
+ get_latest_version() {
122
+ npm view @lethevimlet/sshift version 2>/dev/null || echo "0.0.0"
123
+ }
124
+
125
+ compare_versions() {
126
+ local local_version="$1"
127
+ local remote_version="$2"
128
+
129
+ if [ "$local_version" = "$remote_version" ]; then
130
+ return 0
131
+ fi
132
+
133
+ local IFS='.'
134
+ local i local_parts remote_parts
135
+ read -ra local_parts <<< "$local_version"
136
+ read -ra remote_parts <<< "$remote_version"
137
+
138
+ for ((i=0; i<3; i++)); do
139
+ local local_num="${local_parts[i]:-0}"
140
+ local remote_num="${remote_parts[i]:-0}"
141
+
142
+ if [ "$local_num" -lt "$remote_num" ]; then
143
+ return 1
144
+ elif [ "$local_num" -gt "$remote_num" ]; then
145
+ return 2
146
+ fi
147
+ done
148
+
149
+ return 0
150
+ }
151
+
152
+ get_lan_ip() {
153
+ local ip=""
154
+ if command_exists ip; then
155
+ ip=$(ip route get 1 2>/dev/null | grep -oP 'src \K[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | head -1)
156
+ fi
157
+ if [ -z "$ip" ] && command_exists hostname; then
158
+ ip=$(hostname -I 2>/dev/null | awk '{print $1}')
159
+ fi
160
+ if [ -z "$ip" ] && command_exists ifconfig; then
161
+ ip=$(ifconfig 2>/dev/null | grep -E 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | head -1)
162
+ fi
163
+ if [ -z "$ip" ]; then
164
+ ip="0.0.0.0"
165
+ fi
166
+ echo "$ip"
167
+ }
168
+
169
+ get_effective_port() {
170
+ local port="${SERVER_PORT:-8022}"
171
+ local config_path="$INSTALL_DIR/.env/config.json"
172
+ if [ -z "$SERVER_PORT" ] && [ -f "$config_path" ]; then
173
+ local config_port=$(grep -oP '"port"\s*:\s*\K[0-9]+' "$config_path" 2>/dev/null | head -1)
174
+ if [ -n "$config_port" ]; then
175
+ port="$config_port"
176
+ fi
177
+ fi
178
+ echo "$port"
179
+ }
180
+
181
+ is_running() {
182
+ if command_exists systemctl && [ -f "/etc/systemd/system/$SERVICE_NAME.service" ] && systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
183
+ return 0
184
+ fi
185
+
186
+ if [ "$(uname)" = "Darwin" ] && [ -f "/Library/LaunchDaemons/com.sshift.plist" ]; then
187
+ local pid=$(sudo launchctl list 2>/dev/null | awk '$3 == "com.sshift" && $1 != "-" {print $1}')
188
+ if [ -n "$pid" ] && ps -p "$pid" >/dev/null 2>&1; then
189
+ return 0
190
+ fi
191
+ fi
192
+
193
+ if [ -f "$PID_FILE" ]; then
194
+ local pid=$(cat "$PID_FILE")
195
+ if ps -p "$pid" >/dev/null 2>&1; then
196
+ return 0
197
+ else
198
+ rm -f "$PID_FILE"
199
+ fi
200
+ fi
201
+ return 1
202
+ }
203
+
204
+ stop_app() {
205
+ if command_exists systemctl && systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
206
+ info "Stopping sshift via systemd..."
207
+ sudo systemctl stop "$SERVICE_NAME"
208
+ sleep 2
209
+ return
210
+ fi
211
+
212
+ if is_running; then
213
+ local pid=$(cat "$PID_FILE")
214
+ info "Stopping sshift (PID: $pid)..."
215
+
216
+ kill "$pid" 2>/dev/null || true
217
+ sleep 2
218
+
219
+ if ps -p "$pid" >/dev/null 2>&1; then
220
+ warn "Process did not stop gracefully, force killing..."
221
+ kill -9 "$pid" 2>/dev/null || true
222
+ sleep 1
223
+ fi
224
+
225
+ rm -f "$PID_FILE"
226
+ success "sshift stopped"
227
+ else
228
+ if [ -f "$PID_FILE" ]; then
229
+ rm -f "$PID_FILE"
230
+ info "Removed stale PID file"
231
+ fi
232
+ fi
233
+ }
234
+
235
+ start_app() {
236
+ info "Starting sshift..."
237
+
238
+ if command_exists systemctl && [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
239
+ if systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
240
+ success "sshift is already running (via systemd)"
241
+ return
242
+ fi
243
+ info "Starting sshift via systemd..."
244
+ if sudo systemctl start "$SERVICE_NAME"; then
245
+ sleep 2
246
+ if systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
247
+ success "sshift started via systemd"
248
+ return
249
+ fi
250
+ fi
251
+ warn "systemd start failed, falling back to direct start..."
252
+ fi
253
+
254
+ if is_running; then
255
+ local pid=$(cat "$PID_FILE" 2>/dev/null)
256
+ warn "sshift is already running (PID: $pid)"
257
+ return
258
+ fi
259
+
260
+ mkdir -p "$INSTALL_DIR/.env"
261
+
262
+ local sshift_path=$(which sshift 2>/dev/null || echo "sshift")
263
+
264
+ cd "$INSTALL_DIR"
265
+ nohup "$sshift_path" > "$INSTALL_DIR/sshift.log" 2>&1 &
266
+ local pid=$!
267
+
268
+ mkdir -p "$(dirname "$PID_FILE")"
269
+ echo "$pid" > "$PID_FILE"
270
+
271
+ sleep 2
272
+
273
+ if ps -p "$pid" >/dev/null 2>&1; then
274
+ success "sshift started (PID: $pid)"
275
+ info "Logs: $INSTALL_DIR/sshift.log"
276
+ else
277
+ error "sshift failed to start. Check logs at $INSTALL_DIR/sshift.log"
278
+ fi
279
+ }
280
+
281
+ install_nodejs() {
282
+ if command_exists node; then
283
+ local node_version=$(node -v 2>/dev/null | sed 's/v//')
284
+
285
+ if [ -z "$node_version" ]; then
286
+ warn "Node.js is installed but not working. This may be a glibc version issue."
287
+ warn "On Arch-based systems, please run: sudo pacman -Syu"
288
+ error "Node.js is not functional. Please fix the issue and try again."
289
+ fi
290
+
291
+ local major_version=$(echo "$node_version" | cut -d. -f1)
292
+
293
+ if [ "$major_version" -ge "$NODE_VERSION" ]; then
294
+ success "Node.js $(node -v) is already installed"
295
+ return
296
+ else
297
+ warn "Node.js version $(node -v) is too old, upgrading..."
298
+ fi
299
+ fi
300
+
301
+ if ! command_exists npm; then
302
+ info "npm not found, will be installed with Node.js"
303
+ fi
304
+
305
+ info "Installing Node.js $NODE_VERSION..."
306
+
307
+ if [ "$(uname)" = "Darwin" ]; then
308
+ if command_exists brew; then
309
+ brew install node@${NODE_VERSION}
310
+ else
311
+ error "Homebrew is not installed. Please install Homebrew first: https://brew.sh/"
312
+ fi
313
+ else
314
+ if [ -f /etc/arch-release ] || command_exists pacman; then
315
+ info "Detected Arch-based system"
316
+ info "Updating system packages (required for Node.js compatibility)..."
317
+
318
+ local glibc_version=$(ldd --version 2>/dev/null | head -n1 | grep -oP '\d+\.\d+' | head -1)
319
+ info "Current glibc version: $glibc_version"
320
+
321
+ if ! sudo pacman -Syu --noconfirm; then
322
+ warn "System update failed or was cancelled"
323
+ warn "If you're on an Arch-based system, please run: sudo pacman -Syu"
324
+ error "System update required for Node.js compatibility"
325
+ fi
326
+
327
+ if ! sudo pacman -S --noconfirm nodejs npm; then
328
+ error "Failed to install Node.js via pacman"
329
+ fi
330
+
331
+ if ! node -v >/dev/null 2>&1; then
332
+ error "Node.js installed but not working. Your system may need a reboot after glibc update."
333
+ fi
334
+ elif command_exists apt-get; then
335
+ info "Detected Debian/Ubuntu system"
336
+ if ! command_exists curl; then
337
+ sudo apt-get update
338
+ sudo apt-get install -y curl
339
+ fi
340
+ curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | sudo -E bash -
341
+ sudo apt-get install -y nodejs
342
+ elif command_exists yum; then
343
+ info "Detected RHEL/CentOS system"
344
+ curl -fsSL https://rpm.nodesource.com/setup_${NODE_VERSION}.x | sudo bash -
345
+ sudo yum install -y nodejs
346
+ elif command_exists dnf; then
347
+ info "Detected Fedora system"
348
+ curl -fsSL https://rpm.nodesource.com/setup_${NODE_VERSION}.x | sudo bash -
349
+ sudo dnf install -y nodejs
350
+ else
351
+ error "Unsupported package manager. Please install Node.js $NODE_VERSION manually from https://nodejs.org/"
352
+ fi
353
+ fi
354
+
355
+ if command_exists node && node -v >/dev/null 2>&1; then
356
+ success "Node.js $(node -v) installed successfully"
357
+ else
358
+ error "Failed to install Node.js or Node.js is not functional"
359
+ fi
360
+
361
+ if command_exists npm; then
362
+ success "npm $(npm -v) installed successfully"
363
+ else
364
+ error "npm was not installed. Please install npm manually."
365
+ fi
366
+ }
367
+
368
+ install_sshift() {
369
+ info "Installing sshift via npm..."
370
+
371
+ if [ "$(id -u)" -eq 0 ]; then
372
+ npm install -g @lethevimlet/sshift
373
+ elif [ -w /usr/lib/node_modules ] 2>/dev/null || [ -w /usr/local/lib/node_modules ] 2>/dev/null; then
374
+ npm install -g @lethevimlet/sshift
375
+ else
376
+ sudo npm install -g @lethevimlet/sshift
377
+ fi
378
+
379
+ if [ $? -eq 0 ]; then
380
+ success "sshift installed successfully"
381
+ else
382
+ error "Failed to install sshift"
383
+ fi
384
+ }
385
+
386
+ update_sshift() {
387
+ info "Updating sshift..."
388
+
389
+ if is_running; then
390
+ stop_app
391
+ fi
392
+
393
+ if [ "$(id -u)" -eq 0 ]; then
394
+ npm update -g @lethevimlet/sshift
395
+ elif [ -w /usr/lib/node_modules ] 2>/dev/null || [ -w /usr/local/lib/node_modules ] 2>/dev/null; then
396
+ npm update -g @lethevimlet/sshift
397
+ else
398
+ sudo npm update -g @lethevimlet/sshift
399
+ fi
400
+
401
+ if [ $? -eq 0 ]; then
402
+ success "sshift updated successfully"
403
+ else
404
+ error "Failed to update sshift"
405
+ fi
406
+ }
407
+
408
+ create_config() {
409
+ info "Creating configuration..."
410
+
411
+ mkdir -p "$INSTALL_DIR/.env"
412
+
413
+ local port="${SERVER_PORT:-8022}"
414
+ local config_file="$INSTALL_DIR/.env/config.json"
415
+
416
+ # Merge default values into existing config:
417
+ # - Preserves user's existing values
418
+ # - Adds new default properties from newer versions
419
+ # - If --port was explicitly set, overrides the port value
420
+ local port_override=""
421
+ if [ -n "$SERVER_PORT" ]; then
422
+ port_override="$SERVER_PORT"
423
+ fi
424
+
425
+ local config_action
426
+ config_action=$(node -e "
427
+ const fs = require('fs');
428
+ const defaults = {port: $port, devPort: 3000, bind: '0.0.0.0', enableHttps: true, sticky: true, sshKeepaliveInterval: 15000, sshKeepaliveCountMax: 500, bookmarks: [], folders: []};
429
+ const configPath = '$config_file';
430
+ let existing = {};
431
+ let wasNew = true;
432
+ if (fs.existsSync(configPath)) {
433
+ try { existing = JSON.parse(fs.readFileSync(configPath, 'utf8')); wasNew = false; } catch(e) { process.stderr.write('Warning: Could not parse existing config, using defaults\n'); }
434
+ }
435
+ const merged = Object.assign({}, defaults, existing);
436
+ const portOverride = '$port_override';
437
+ if (portOverride) { merged.port = parseInt(portOverride); }
438
+ fs.writeFileSync(configPath, JSON.stringify(merged, null, 2) + '\n');
439
+ process.stdout.write(wasNew ? 'created' : 'merged');
440
+ " 2>&1) || config_action="created"
441
+
442
+ if [ "$config_action" = "merged" ]; then
443
+ info "Existing configuration merged with defaults at $config_file"
444
+ else
445
+ info "Configuration created at $config_file"
446
+ fi
447
+
448
+ local effective_port=$(grep -oP '"port"\s*:\s*\K[0-9]+' "$config_file" 2>/dev/null | head -1)
449
+ if [ -n "$effective_port" ]; then
450
+ port="$effective_port"
451
+ fi
452
+ success "Configuration created with HTTPS enabled on port $port"
453
+ }
454
+
455
+ add_to_path() {
456
+ local sshift_bin_path=$(which sshift 2>/dev/null)
457
+
458
+ # Check if sshift command is already accessible
459
+ if [ -n "$sshift_bin_path" ]; then
460
+ local bin_dir=$(dirname "$sshift_bin_path")
461
+ # The command works - check if its directory is in PATH
462
+ if echo "$PATH" | grep -q "$bin_dir"; then
463
+ success "sshift command available at $sshift_bin_path"
464
+ return
465
+ fi
466
+ # Command works but dir not in PATH (unlikely but handle it)
467
+ success "sshift command available at $sshift_bin_path"
468
+ return
469
+ fi
470
+
471
+ # sshift not found in PATH - need to add its bin directory
472
+ # Determine the npm global bin directory
473
+ local npm_bin_dir=$(npm config get prefix 2>/dev/null)/bin
474
+ if [ ! -d "$npm_bin_dir" ]; then
475
+ # Some npm setups use prefix directly as the bin dir
476
+ local npm_prefix=$(npm config get prefix 2>/dev/null)
477
+ if [ -f "$npm_prefix/sshift" ]; then
478
+ npm_bin_dir="$npm_prefix"
479
+ fi
480
+ fi
481
+
482
+ # Fall back to common locations
483
+ if [ -z "$npm_bin_dir" ] || [ ! -d "$npm_bin_dir" ]; then
484
+ for candidate in "/usr/local/bin" "/usr/bin" "$HOME/.local/bin" "$HOME/.npm-global/bin"; do
485
+ if [ -f "$candidate/sshift" ]; then
486
+ npm_bin_dir="$candidate"
487
+ break
488
+ fi
489
+ done
490
+ fi
491
+
492
+ if [ -z "$npm_bin_dir" ]; then
493
+ warn "Could not determine sshift binary location. You may need to add it to PATH manually."
494
+ return
495
+ fi
496
+
497
+ local shell_rc=""
498
+ if [ -n "$ZSH_VERSION" ]; then
499
+ shell_rc="$HOME/.zshrc"
500
+ elif [ -n "$BASH_VERSION" ]; then
501
+ shell_rc="$HOME/.bashrc"
502
+ else
503
+ shell_rc="$HOME/.profile"
504
+ fi
505
+
506
+ if ! grep -q "$npm_bin_dir" "$shell_rc" 2>/dev/null; then
507
+ echo "" >> "$shell_rc"
508
+ echo "# sshift" >> "$shell_rc"
509
+ echo "export PATH=\"$npm_bin_dir:\$PATH\"" >> "$shell_rc"
510
+ success "Added $npm_bin_dir to PATH in $shell_rc"
511
+ info "Run: source $shell_rc (or restart your terminal)"
512
+ fi
513
+ }
514
+
515
+ setup_autostart() {
516
+ info "Setting up autostart (on boot)..."
517
+
518
+ local env_dir="$INSTALL_DIR/.env"
519
+ mkdir -p "$env_dir"
520
+
521
+ local sshift_path=$(which sshift 2>/dev/null || echo "sshift")
522
+ local node_path=$(which node 2>/dev/null || echo "node")
523
+
524
+ if [ "$(uname)" = "Darwin" ]; then
525
+ local plist_path="/Library/LaunchDaemons/com.sshift.plist"
526
+
527
+ # Resolve symlinks to get the real sshift script path
528
+ local real_sshift_path="$sshift_path"
529
+ while [ -L "$real_sshift_path" ]; do
530
+ local target=$(readlink "$real_sshift_path" 2>/dev/null)
531
+ if [ -z "$target" ]; then break; fi
532
+ case "$target" in
533
+ /*) real_sshift_path="$target" ;;
534
+ *) real_sshift_path="$(dirname "$real_sshift_path")/$target" ;;
535
+ esac
536
+ done
537
+
538
+ # Resolve node path
539
+ local real_node_path="$node_path"
540
+ while [ -L "$real_node_path" ]; do
541
+ local target=$(readlink "$real_node_path" 2>/dev/null)
542
+ if [ -z "$target" ]; then break; fi
543
+ case "$target" in
544
+ /*) real_node_path="$target" ;;
545
+ *) real_node_path="$(dirname "$real_node_path")/$target" ;;
546
+ esac
547
+ done
548
+
549
+ # Build PATH from common macOS node locations
550
+ local path_dirs=""
551
+ local bin_dirs=("/opt/homebrew/bin" "/usr/local/bin" "/usr/bin" "$HOME/.local/bin")
552
+ for d in "${bin_dirs[@]}"; do
553
+ if [ -d "$d" ]; then
554
+ if [ -n "$path_dirs" ]; then
555
+ path_dirs="$path_dirs:$d"
556
+ else
557
+ path_dirs="$d"
558
+ fi
559
+ fi
560
+ done
561
+
562
+ # Unload existing plist if present (check both old and new locations)
563
+ local old_plist_path="$HOME/Library/LaunchAgents/com.sshift.plist"
564
+ if [ -f "$old_plist_path" ]; then
565
+ launchctl bootout gui/$(id -u) "$old_plist_path" 2>/dev/null || \
566
+ launchctl unload "$old_plist_path" 2>/dev/null || true
567
+ rm -f "$old_plist_path"
568
+ fi
569
+ if [ -f "$plist_path" ]; then
570
+ sudo launchctl bootout system "$plist_path" 2>/dev/null || \
571
+ sudo launchctl unload "$plist_path" 2>/dev/null || true
572
+ fi
573
+
574
+ cat << EOF | sudo tee "$plist_path" > /dev/null
575
+ <?xml version="1.0" encoding="UTF-8"?>
576
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
577
+ <plist version="1.0">
578
+ <dict>
579
+ <key>Label</key>
580
+ <string>com.sshift</string>
581
+ <key>ProgramArguments</key>
582
+ <array>
583
+ <string>$real_node_path</string>
584
+ <string>$real_sshift_path</string>
585
+ </array>
586
+ <key>WorkingDirectory</key>
587
+ <string>$INSTALL_DIR</string>
588
+ <key>EnvironmentVariables</key>
589
+ <dict>
590
+ <key>PATH</key>
591
+ <string>$path_dirs</string>
592
+ <key>HOME</key>
593
+ <string>$HOME</string>
594
+ </dict>
595
+ <key>UserName</key>
596
+ <string>$(whoami)</string>
597
+ <key>RunAtLoad</key>
598
+ <true/>
599
+ <key>KeepAlive</key>
600
+ <false/>
601
+ <key>StandardOutPath</key>
602
+ <string>$INSTALL_DIR/sshift.log</string>
603
+ <key>StandardErrorPath</key>
604
+ <string>$INSTALL_DIR/sshift-error.log</string>
605
+ </dict>
606
+ </plist>
607
+ EOF
608
+
609
+ sudo launchctl bootstrap system "$plist_path" 2>/dev/null || \
610
+ sudo launchctl load "$plist_path" 2>/dev/null || true
611
+ success "Autostart configured via launchd"
612
+ else
613
+ local service_path="/etc/systemd/system/$SERVICE_NAME.service"
614
+
615
+ local real_sshift_path="$sshift_path"
616
+ while [ -L "$real_sshift_path" ]; do
617
+ local target=$(readlink "$real_sshift_path" 2>/dev/null)
618
+ if [ -z "$target" ]; then break; fi
619
+ case "$target" in
620
+ /*) real_sshift_path="$target" ;;
621
+ *) real_sshift_path="$(dirname "$real_sshift_path")/$target" ;;
622
+ esac
623
+ done
624
+
625
+ local real_node_path="$node_path"
626
+ while [ -L "$real_node_path" ]; do
627
+ local target=$(readlink "$real_node_path" 2>/dev/null)
628
+ if [ -z "$target" ]; then break; fi
629
+ case "$target" in
630
+ /*) real_node_path="$target" ;;
631
+ *) real_node_path="$(dirname "$real_node_path")/$target" ;;
632
+ esac
633
+ done
634
+
635
+ local path_dirs=""
636
+ local bin_dirs=("/usr/local/bin" "/usr/bin" "/bin" "/usr/sbin" "/sbin" "$HOME/.local/bin" "$(dirname "$real_node_path")")
637
+ for d in "${bin_dirs[@]}"; do
638
+ if [ -d "$d" ]; then
639
+ if [ -n "$path_dirs" ]; then
640
+ path_dirs="$path_dirs:$d"
641
+ else
642
+ path_dirs="$d"
643
+ fi
644
+ fi
645
+ done
646
+
647
+ if [ -f "$PID_FILE" ]; then
648
+ info "Stopping existing sshift instance so systemd can manage it..."
649
+ stop_app
650
+ fi
651
+
652
+ cat << EOF | sudo tee "$service_path" > /dev/null
653
+ [Unit]
654
+ Description=sshift - Web-based SSH Terminal
655
+ After=network.target
656
+
657
+ [Service]
658
+ Type=simple
659
+ ExecStart=$real_node_path $real_sshift_path
660
+ WorkingDirectory=$INSTALL_DIR
661
+ User=$USER
662
+ Environment=HOME=$HOME
663
+ Environment=PATH=$path_dirs
664
+ Restart=on-failure
665
+ RestartSec=10
666
+
667
+ [Install]
668
+ WantedBy=multi-user.target
669
+ EOF
670
+
671
+ sudo systemctl daemon-reload
672
+ sudo systemctl enable "$SERVICE_NAME"
673
+ sudo systemctl start "$SERVICE_NAME"
674
+
675
+ success "Autostart configured via systemd"
676
+ fi
677
+ }
678
+
679
+ remove_autostart() {
680
+ if [ "$(uname)" = "Darwin" ]; then
681
+ local new_plist_path="/Library/LaunchDaemons/com.sshift.plist"
682
+ local old_plist_path="$HOME/Library/LaunchAgents/com.sshift.plist"
683
+ if [ -f "$new_plist_path" ]; then
684
+ sudo launchctl bootout system "$new_plist_path" 2>/dev/null || \
685
+ sudo launchctl unload "$new_plist_path" 2>/dev/null || true
686
+ sudo rm -f "$new_plist_path"
687
+ fi
688
+ if [ -f "$old_plist_path" ]; then
689
+ launchctl bootout gui/$(id -u) "$old_plist_path" 2>/dev/null || \
690
+ launchctl unload "$old_plist_path" 2>/dev/null || true
691
+ rm -f "$old_plist_path"
692
+ fi
693
+ else
694
+ # Remove system-level service
695
+ if [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
696
+ sudo systemctl stop "$SERVICE_NAME" 2>/dev/null || true
697
+ sudo systemctl disable "$SERVICE_NAME" 2>/dev/null || true
698
+ sudo rm -f "/etc/systemd/system/$SERVICE_NAME.service"
699
+ sudo systemctl daemon-reload 2>/dev/null || true
700
+ fi
701
+ # Also remove old user-level service if it exists
702
+ if [ -f "$HOME/.config/systemd/user/$SERVICE_NAME.service" ]; then
703
+ systemctl --user stop "$SERVICE_NAME" 2>/dev/null || true
704
+ systemctl --user disable "$SERVICE_NAME" 2>/dev/null || true
705
+ rm -f "$HOME/.config/systemd/user/$SERVICE_NAME.service"
706
+ systemctl --user daemon-reload 2>/dev/null || true
707
+ fi
708
+ fi
709
+ }
710
+
711
+ print_summary() {
712
+ local port=$(get_effective_port)
713
+ local lan_ip=$(get_lan_ip)
714
+
715
+ local url1="https://localhost:$port"
716
+ local url2="https://$lan_ip:$port"
717
+ local data_dir="$INSTALL_DIR"
718
+
719
+ local autostart_note=""
720
+ if [ "$(uname)" = "Darwin" ]; then
721
+ autostart_note=" (launchd)"
722
+ else
723
+ autostart_note=" (systemd)"
724
+ fi
725
+
726
+ local content=(
727
+ " sshift installed${autostart_note}"
728
+ ""
729
+ " Path: ${data_dir}"
730
+ ""
731
+ " URLs:"
732
+ " ${url1}"
733
+ " ${url2}"
734
+ )
735
+
736
+ local max_w=0
737
+ for line in "${content[@]}"; do
738
+ local w=${#line}
739
+ (( w > max_w )) && max_w=$w
740
+ done
741
+ (( max_w += 2 ))
742
+
743
+ local border
744
+ border=$(printf '%*s' "$max_w" '' | tr ' ' -)
745
+
746
+ echo ""
747
+ echo -e " ${GREEN}+${border}+${NC}"
748
+ for line in "${content[@]}"; do
749
+ printf " ${GREEN}|${NC}%-${max_w}s${GREEN}|${NC}\n" "$line"
750
+ done
751
+ echo -e " ${GREEN}+${border}+${NC}"
752
+ echo ""
753
+ }
754
+
755
+ uninstall_sshift() {
756
+ echo ""
757
+ echo -e "${BOLD}=========================================="
758
+ echo -e " sshift Uninstallation Script"
759
+ echo -e "==========================================${NC}"
760
+ echo ""
761
+
762
+ if ! command_exists sshift; then
763
+ warn "sshift is not installed"
764
+ return
765
+ fi
766
+
767
+ info "This will remove:"
768
+ echo " - sshift npm package"
769
+ echo " - Configuration files in $INSTALL_DIR"
770
+ echo " - Autostart configuration (if any)"
771
+ echo " - PATH configuration"
772
+ echo ""
773
+
774
+ prompt_user "Continue with uninstallation? [y/N] " "n"
775
+ if [[ ! $PROMPT_RESPONSE =~ ^[Yy]$ ]]; then
776
+ info "Uninstallation cancelled"
777
+ return
778
+ fi
779
+
780
+ if is_running; then
781
+ info "Stopping running instance..."
782
+ stop_app
783
+ fi
784
+
785
+ info "Removing autostart configuration..."
786
+ remove_autostart
787
+
788
+ info "Uninstalling sshift..."
789
+ if [ "$(id -u)" -eq 0 ]; then
790
+ npm uninstall -g @lethevimlet/sshift
791
+ elif [ -w /usr/lib/node_modules ] 2>/dev/null || [ -w /usr/local/lib/node_modules ] 2>/dev/null; then
792
+ npm uninstall -g @lethevimlet/sshift
793
+ else
794
+ sudo npm uninstall -g @lethevimlet/sshift
795
+ fi
796
+
797
+ if [ -d "$INSTALL_DIR" ]; then
798
+ info "Removing configuration directory..."
799
+ rm -rf "$INSTALL_DIR"
800
+ success "Configuration directory removed"
801
+ fi
802
+
803
+ info "Removing PATH configuration..."
804
+ local shell_rc=""
805
+ if [ -n "$ZSH_VERSION" ]; then
806
+ shell_rc="$HOME/.zshrc"
807
+ elif [ -n "$BASH_VERSION" ]; then
808
+ shell_rc="$HOME/.bashrc"
809
+ else
810
+ shell_rc="$HOME/.profile"
811
+ fi
812
+
813
+ if [ -f "$shell_rc" ]; then
814
+ sed -i.bak '/# sshift/d' "$shell_rc"
815
+ sed -i.bak "/sshift/d" "$shell_rc"
816
+ rm -f "${shell_rc}.bak"
817
+ fi
818
+
819
+ echo ""
820
+ echo -e "${GREEN}sshift uninstalled successfully!${NC}"
821
+ echo ""
822
+ info "You may need to restart your terminal for PATH changes to take effect"
823
+ }
824
+
825
+ check_updates() {
826
+ info "Checking for updates..."
827
+
828
+ local local_version=$(get_installed_version)
829
+ local remote_version=$(get_latest_version)
830
+
831
+ info "Installed version: $local_version"
832
+ info "Latest version: $remote_version"
833
+
834
+ compare_versions "$local_version" "$remote_version"
835
+ local result=$?
836
+
837
+ if [ $result -eq 1 ]; then
838
+ warn "New version available: $remote_version (current: $local_version)"
839
+ return 1
840
+ elif [ $result -eq 2 ]; then
841
+ info "Local version is newer than remote (development version?)"
842
+ return 0
843
+ else
844
+ success "Already up to date (version $local_version)"
845
+ return 0
846
+ fi
847
+ }
848
+
849
+ main() {
850
+ while [ $# -gt 0 ]; do
851
+ case "$1" in
852
+ --install-dir)
853
+ INSTALL_DIR="$2"
854
+ shift 2
855
+ ;;
856
+ --port)
857
+ SERVER_PORT="$2"
858
+ shift 2
859
+ ;;
860
+ --start)
861
+ start_app
862
+ exit 0
863
+ ;;
864
+ --stop)
865
+ stop_app
866
+ exit 0
867
+ ;;
868
+ --restart)
869
+ info "Restarting sshift..."
870
+ stop_app
871
+ start_app
872
+ exit 0
873
+ ;;
874
+ --status)
875
+ if is_running; then
876
+ if command_exists systemctl && [ -f "/etc/systemd/system/$SERVICE_NAME.service" ] && systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
877
+ success "sshift is running (systemd)"
878
+ elif [ "$(uname)" = "Darwin" ] && [ -f "/Library/LaunchDaemons/com.sshift.plist" ]; then
879
+ local pid=$(sudo launchctl list 2>/dev/null | awk '$3 == "com.sshift" && $1 != "-" {print $1}')
880
+ if [ -n "$pid" ]; then
881
+ success "sshift is running (launchd, PID: $pid)"
882
+ else
883
+ success "sshift is running (launchd)"
884
+ fi
885
+ elif [ -f "$PID_FILE" ]; then
886
+ local pid=$(cat "$PID_FILE")
887
+ success "sshift is running (PID: $pid)"
888
+ else
889
+ success "sshift is running"
890
+ fi
891
+ exit 0
892
+ else
893
+ info "sshift is not running"
894
+ exit 1
895
+ fi
896
+ ;;
897
+ --update)
898
+ if ! command_exists sshift; then
899
+ error "sshift is not installed"
900
+ fi
901
+
902
+ echo ""
903
+ echo -e "${BOLD}=========================================="
904
+ echo -e " sshift Update Script"
905
+ echo -e "==========================================${NC}"
906
+ echo ""
907
+
908
+ info "Updating sshift..."
909
+ update_sshift
910
+ print_summary
911
+ exit 0
912
+ ;;
913
+ --uninstall)
914
+ uninstall_sshift
915
+ exit 0
916
+ ;;
917
+ --help|-h)
918
+ show_help
919
+ ;;
920
+ *)
921
+ error "Unknown option: $1. Use --help for usage information."
922
+ ;;
923
+ esac
924
+ done
925
+
926
+ echo ""
927
+ echo -e "${BOLD}=========================================="
928
+ echo -e " sshift Installation Script"
929
+ echo -e "==========================================${NC}"
930
+ echo ""
931
+
932
+ info "Installation method: npm"
933
+ if [ -n "$SERVER_PORT" ]; then
934
+ info "Server port: $SERVER_PORT"
935
+ fi
936
+
937
+ if command_exists sshift; then
938
+ info "sshift is already installed"
939
+
940
+ if ! check_updates; then
941
+ prompt_user "Update sshift? [Y/n] " "y"
942
+ if [[ ! $PROMPT_RESPONSE =~ ^[Nn]$ ]]; then
943
+ update_sshift
944
+ fi
945
+ else
946
+ if is_running; then
947
+ if command_exists systemctl && [ -f "/etc/systemd/system/$SERVICE_NAME.service" ] && systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
948
+ info "sshift is already running (systemd)"
949
+ elif [ -f "$PID_FILE" ]; then
950
+ local pid=$(cat "$PID_FILE")
951
+ info "sshift is already running (PID: $pid)"
952
+ else
953
+ info "sshift is already running"
954
+ fi
955
+ fi
956
+ fi
957
+ else
958
+ install_nodejs
959
+ install_sshift
960
+ create_config
961
+ fi
962
+
963
+ echo ""
964
+ info "Configuration options:"
965
+ echo ""
966
+
967
+ prompt_user "Add sshift to PATH? [Y/n] " "y"
968
+ if [[ ! $PROMPT_RESPONSE =~ ^[Nn]$ ]]; then
969
+ add_to_path
970
+ fi
971
+
972
+ local autostart_configured=false
973
+ prompt_user "Start sshift automatically on boot? [y/N] " "n"
974
+ if [[ $PROMPT_RESPONSE =~ ^[Yy]$ ]]; then
975
+ setup_autostart
976
+ autostart_configured=true
977
+ fi
978
+
979
+ if ! is_running; then
980
+ start_app
981
+ fi
982
+
983
+ print_summary
984
+ }
985
+
986
+ main "$@"