@lethevimlet/sshift 0.9.5 → 0.9.7

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,991 @@
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_content="{
415
+ \"port\": $port,
416
+ \"devPort\": 3000,
417
+ \"bind\": \"0.0.0.0\",
418
+ \"enableHttps\": true,
419
+ \"sticky\": true,
420
+ \"sshKeepaliveInterval\": 15000,
421
+ \"sshKeepaliveCountMax\": 500,
422
+ \"bookmarks\": [],
423
+ \"folders\": []
424
+ }"
425
+
426
+ # Write config to user install directory (preferred by updated config loader)
427
+ echo "$config_content" > "$INSTALL_DIR/.env/config.json"
428
+
429
+ # Also write config to the npm package directory so the older config loader finds it
430
+ local sshift_bin_path=$(which sshift 2>/dev/null)
431
+ if [ -n "$sshift_bin_path" ]; then
432
+ local real_path="$sshift_bin_path"
433
+ # Resolve symlinks portably (readlink -f is not available on macOS BSD)
434
+ while [ -L "$real_path" ]; do
435
+ local target=$(readlink "$real_path" 2>/dev/null)
436
+ if [ -z "$target" ]; then break; fi
437
+ # Handle relative symlinks
438
+ case "$target" in
439
+ /*) real_path="$target" ;;
440
+ *) real_path="$(dirname "$real_path")/$target" ;;
441
+ esac
442
+ done
443
+ local pkg_dir=$(dirname "$real_path")
444
+ if [ -d "$pkg_dir" ]; then
445
+ if mkdir -p "$pkg_dir/.env" 2>/dev/null; then
446
+ echo "$config_content" > "$pkg_dir/.env/config.json"
447
+ echo "$config_content" > "$pkg_dir/config.json"
448
+ else
449
+ # Package directory may be root-owned (e.g. Homebrew npm global installs on macOS)
450
+ # The user-space config at INSTALL_DIR is already written, so this is just a
451
+ # backwards-compat fallback — skip silently if we lack permissions.
452
+ warn "Cannot write to package directory $pkg_dir (permission denied). Using config at $INSTALL_DIR/.env/config.json instead."
453
+ fi
454
+ fi
455
+ fi
456
+
457
+ success "Configuration created with HTTPS enabled on port $port"
458
+ }
459
+
460
+ add_to_path() {
461
+ local sshift_bin_path=$(which sshift 2>/dev/null)
462
+
463
+ # Check if sshift command is already accessible
464
+ if [ -n "$sshift_bin_path" ]; then
465
+ local bin_dir=$(dirname "$sshift_bin_path")
466
+ # The command works - check if its directory is in PATH
467
+ if echo "$PATH" | grep -q "$bin_dir"; then
468
+ success "sshift command available at $sshift_bin_path"
469
+ return
470
+ fi
471
+ # Command works but dir not in PATH (unlikely but handle it)
472
+ success "sshift command available at $sshift_bin_path"
473
+ return
474
+ fi
475
+
476
+ # sshift not found in PATH - need to add its bin directory
477
+ # Determine the npm global bin directory
478
+ local npm_bin_dir=$(npm config get prefix 2>/dev/null)/bin
479
+ if [ ! -d "$npm_bin_dir" ]; then
480
+ # Some npm setups use prefix directly as the bin dir
481
+ local npm_prefix=$(npm config get prefix 2>/dev/null)
482
+ if [ -f "$npm_prefix/sshift" ]; then
483
+ npm_bin_dir="$npm_prefix"
484
+ fi
485
+ fi
486
+
487
+ # Fall back to common locations
488
+ if [ -z "$npm_bin_dir" ] || [ ! -d "$npm_bin_dir" ]; then
489
+ for candidate in "/usr/local/bin" "/usr/bin" "$HOME/.local/bin" "$HOME/.npm-global/bin"; do
490
+ if [ -f "$candidate/sshift" ]; then
491
+ npm_bin_dir="$candidate"
492
+ break
493
+ fi
494
+ done
495
+ fi
496
+
497
+ if [ -z "$npm_bin_dir" ]; then
498
+ warn "Could not determine sshift binary location. You may need to add it to PATH manually."
499
+ return
500
+ fi
501
+
502
+ local shell_rc=""
503
+ if [ -n "$ZSH_VERSION" ]; then
504
+ shell_rc="$HOME/.zshrc"
505
+ elif [ -n "$BASH_VERSION" ]; then
506
+ shell_rc="$HOME/.bashrc"
507
+ else
508
+ shell_rc="$HOME/.profile"
509
+ fi
510
+
511
+ if ! grep -q "$npm_bin_dir" "$shell_rc" 2>/dev/null; then
512
+ echo "" >> "$shell_rc"
513
+ echo "# sshift" >> "$shell_rc"
514
+ echo "export PATH=\"$npm_bin_dir:\$PATH\"" >> "$shell_rc"
515
+ success "Added $npm_bin_dir to PATH in $shell_rc"
516
+ info "Run: source $shell_rc (or restart your terminal)"
517
+ fi
518
+ }
519
+
520
+ setup_autostart() {
521
+ info "Setting up autostart (on boot)..."
522
+
523
+ local env_dir="$INSTALL_DIR/.env"
524
+ mkdir -p "$env_dir"
525
+
526
+ local sshift_path=$(which sshift 2>/dev/null || echo "sshift")
527
+ local node_path=$(which node 2>/dev/null || echo "node")
528
+
529
+ if [ "$(uname)" = "Darwin" ]; then
530
+ local plist_path="/Library/LaunchDaemons/com.sshift.plist"
531
+
532
+ # Resolve symlinks to get the real sshift script path
533
+ local real_sshift_path="$sshift_path"
534
+ while [ -L "$real_sshift_path" ]; do
535
+ local target=$(readlink "$real_sshift_path" 2>/dev/null)
536
+ if [ -z "$target" ]; then break; fi
537
+ case "$target" in
538
+ /*) real_sshift_path="$target" ;;
539
+ *) real_sshift_path="$(dirname "$real_sshift_path")/$target" ;;
540
+ esac
541
+ done
542
+
543
+ # Resolve node path
544
+ local real_node_path="$node_path"
545
+ while [ -L "$real_node_path" ]; do
546
+ local target=$(readlink "$real_node_path" 2>/dev/null)
547
+ if [ -z "$target" ]; then break; fi
548
+ case "$target" in
549
+ /*) real_node_path="$target" ;;
550
+ *) real_node_path="$(dirname "$real_node_path")/$target" ;;
551
+ esac
552
+ done
553
+
554
+ # Build PATH from common macOS node locations
555
+ local path_dirs=""
556
+ local bin_dirs=("/opt/homebrew/bin" "/usr/local/bin" "/usr/bin" "$HOME/.local/bin")
557
+ for d in "${bin_dirs[@]}"; do
558
+ if [ -d "$d" ]; then
559
+ if [ -n "$path_dirs" ]; then
560
+ path_dirs="$path_dirs:$d"
561
+ else
562
+ path_dirs="$d"
563
+ fi
564
+ fi
565
+ done
566
+
567
+ # Unload existing plist if present (check both old and new locations)
568
+ local old_plist_path="$HOME/Library/LaunchAgents/com.sshift.plist"
569
+ if [ -f "$old_plist_path" ]; then
570
+ launchctl bootout gui/$(id -u) "$old_plist_path" 2>/dev/null || \
571
+ launchctl unload "$old_plist_path" 2>/dev/null || true
572
+ rm -f "$old_plist_path"
573
+ fi
574
+ if [ -f "$plist_path" ]; then
575
+ sudo launchctl bootout system "$plist_path" 2>/dev/null || \
576
+ sudo launchctl unload "$plist_path" 2>/dev/null || true
577
+ fi
578
+
579
+ cat << EOF | sudo tee "$plist_path" > /dev/null
580
+ <?xml version="1.0" encoding="UTF-8"?>
581
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
582
+ <plist version="1.0">
583
+ <dict>
584
+ <key>Label</key>
585
+ <string>com.sshift</string>
586
+ <key>ProgramArguments</key>
587
+ <array>
588
+ <string>$real_node_path</string>
589
+ <string>$real_sshift_path</string>
590
+ </array>
591
+ <key>WorkingDirectory</key>
592
+ <string>$INSTALL_DIR</string>
593
+ <key>EnvironmentVariables</key>
594
+ <dict>
595
+ <key>PATH</key>
596
+ <string>$path_dirs</string>
597
+ <key>HOME</key>
598
+ <string>$HOME</string>
599
+ </dict>
600
+ <key>UserName</key>
601
+ <string>$(whoami)</string>
602
+ <key>RunAtLoad</key>
603
+ <true/>
604
+ <key>KeepAlive</key>
605
+ <false/>
606
+ <key>StandardOutPath</key>
607
+ <string>$INSTALL_DIR/sshift.log</string>
608
+ <key>StandardErrorPath</key>
609
+ <string>$INSTALL_DIR/sshift-error.log</string>
610
+ </dict>
611
+ </plist>
612
+ EOF
613
+
614
+ sudo launchctl bootstrap system "$plist_path" 2>/dev/null || \
615
+ sudo launchctl load "$plist_path" 2>/dev/null || true
616
+ success "Autostart configured via launchd"
617
+ else
618
+ local service_path="/etc/systemd/system/$SERVICE_NAME.service"
619
+
620
+ local real_sshift_path="$sshift_path"
621
+ while [ -L "$real_sshift_path" ]; do
622
+ local target=$(readlink "$real_sshift_path" 2>/dev/null)
623
+ if [ -z "$target" ]; then break; fi
624
+ case "$target" in
625
+ /*) real_sshift_path="$target" ;;
626
+ *) real_sshift_path="$(dirname "$real_sshift_path")/$target" ;;
627
+ esac
628
+ done
629
+
630
+ local real_node_path="$node_path"
631
+ while [ -L "$real_node_path" ]; do
632
+ local target=$(readlink "$real_node_path" 2>/dev/null)
633
+ if [ -z "$target" ]; then break; fi
634
+ case "$target" in
635
+ /*) real_node_path="$target" ;;
636
+ *) real_node_path="$(dirname "$real_node_path")/$target" ;;
637
+ esac
638
+ done
639
+
640
+ local path_dirs=""
641
+ local bin_dirs=("/usr/local/bin" "/usr/bin" "/bin" "/usr/sbin" "/sbin" "$HOME/.local/bin" "$(dirname "$real_node_path")")
642
+ for d in "${bin_dirs[@]}"; do
643
+ if [ -d "$d" ]; then
644
+ if [ -n "$path_dirs" ]; then
645
+ path_dirs="$path_dirs:$d"
646
+ else
647
+ path_dirs="$d"
648
+ fi
649
+ fi
650
+ done
651
+
652
+ if [ -f "$PID_FILE" ]; then
653
+ info "Stopping existing sshift instance so systemd can manage it..."
654
+ stop_app
655
+ fi
656
+
657
+ cat << EOF | sudo tee "$service_path" > /dev/null
658
+ [Unit]
659
+ Description=sshift - Web-based SSH Terminal
660
+ After=network.target
661
+
662
+ [Service]
663
+ Type=simple
664
+ ExecStart=$real_node_path $real_sshift_path
665
+ WorkingDirectory=$INSTALL_DIR
666
+ User=$USER
667
+ Environment=HOME=$HOME
668
+ Environment=PATH=$path_dirs
669
+ Restart=on-failure
670
+ RestartSec=10
671
+
672
+ [Install]
673
+ WantedBy=multi-user.target
674
+ EOF
675
+
676
+ sudo systemctl daemon-reload
677
+ sudo systemctl enable "$SERVICE_NAME"
678
+ sudo systemctl start "$SERVICE_NAME"
679
+
680
+ success "Autostart configured via systemd"
681
+ fi
682
+ }
683
+
684
+ remove_autostart() {
685
+ if [ "$(uname)" = "Darwin" ]; then
686
+ local new_plist_path="/Library/LaunchDaemons/com.sshift.plist"
687
+ local old_plist_path="$HOME/Library/LaunchAgents/com.sshift.plist"
688
+ if [ -f "$new_plist_path" ]; then
689
+ sudo launchctl bootout system "$new_plist_path" 2>/dev/null || \
690
+ sudo launchctl unload "$new_plist_path" 2>/dev/null || true
691
+ sudo rm -f "$new_plist_path"
692
+ fi
693
+ if [ -f "$old_plist_path" ]; then
694
+ launchctl bootout gui/$(id -u) "$old_plist_path" 2>/dev/null || \
695
+ launchctl unload "$old_plist_path" 2>/dev/null || true
696
+ rm -f "$old_plist_path"
697
+ fi
698
+ else
699
+ # Remove system-level service
700
+ if [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
701
+ sudo systemctl stop "$SERVICE_NAME" 2>/dev/null || true
702
+ sudo systemctl disable "$SERVICE_NAME" 2>/dev/null || true
703
+ sudo rm -f "/etc/systemd/system/$SERVICE_NAME.service"
704
+ sudo systemctl daemon-reload 2>/dev/null || true
705
+ fi
706
+ # Also remove old user-level service if it exists
707
+ if [ -f "$HOME/.config/systemd/user/$SERVICE_NAME.service" ]; then
708
+ systemctl --user stop "$SERVICE_NAME" 2>/dev/null || true
709
+ systemctl --user disable "$SERVICE_NAME" 2>/dev/null || true
710
+ rm -f "$HOME/.config/systemd/user/$SERVICE_NAME.service"
711
+ systemctl --user daemon-reload 2>/dev/null || true
712
+ fi
713
+ fi
714
+ }
715
+
716
+ print_summary() {
717
+ local port=$(get_effective_port)
718
+ local lan_ip=$(get_lan_ip)
719
+
720
+ local url1="https://localhost:$port"
721
+ local url2="https://$lan_ip:$port"
722
+ local data_dir="$INSTALL_DIR"
723
+
724
+ local autostart_note=""
725
+ if [ "$(uname)" = "Darwin" ]; then
726
+ autostart_note=" (launchd)"
727
+ else
728
+ autostart_note=" (systemd)"
729
+ fi
730
+
731
+ local content=(
732
+ " sshift installed${autostart_note}"
733
+ ""
734
+ " Path: ${data_dir}"
735
+ ""
736
+ " URLs:"
737
+ " ${url1}"
738
+ " ${url2}"
739
+ )
740
+
741
+ local max_w=0
742
+ for line in "${content[@]}"; do
743
+ local w=${#line}
744
+ (( w > max_w )) && max_w=$w
745
+ done
746
+ (( max_w += 2 ))
747
+
748
+ local border
749
+ border=$(printf '%*s' "$max_w" '' | tr ' ' -)
750
+
751
+ echo ""
752
+ echo -e " ${GREEN}+${border}+${NC}"
753
+ for line in "${content[@]}"; do
754
+ printf " ${GREEN}|${NC}%-${max_w}s${GREEN}|${NC}\n" "$line"
755
+ done
756
+ echo -e " ${GREEN}+${border}+${NC}"
757
+ echo ""
758
+ }
759
+
760
+ uninstall_sshift() {
761
+ echo ""
762
+ echo -e "${BOLD}=========================================="
763
+ echo -e " sshift Uninstallation Script"
764
+ echo -e "==========================================${NC}"
765
+ echo ""
766
+
767
+ if ! command_exists sshift; then
768
+ warn "sshift is not installed"
769
+ return
770
+ fi
771
+
772
+ info "This will remove:"
773
+ echo " - sshift npm package"
774
+ echo " - Configuration files in $INSTALL_DIR"
775
+ echo " - Autostart configuration (if any)"
776
+ echo " - PATH configuration"
777
+ echo ""
778
+
779
+ prompt_user "Continue with uninstallation? [y/N] " "n"
780
+ if [[ ! $PROMPT_RESPONSE =~ ^[Yy]$ ]]; then
781
+ info "Uninstallation cancelled"
782
+ return
783
+ fi
784
+
785
+ if is_running; then
786
+ info "Stopping running instance..."
787
+ stop_app
788
+ fi
789
+
790
+ info "Removing autostart configuration..."
791
+ remove_autostart
792
+
793
+ info "Uninstalling sshift..."
794
+ if [ "$(id -u)" -eq 0 ]; then
795
+ npm uninstall -g @lethevimlet/sshift
796
+ elif [ -w /usr/lib/node_modules ] 2>/dev/null || [ -w /usr/local/lib/node_modules ] 2>/dev/null; then
797
+ npm uninstall -g @lethevimlet/sshift
798
+ else
799
+ sudo npm uninstall -g @lethevimlet/sshift
800
+ fi
801
+
802
+ if [ -d "$INSTALL_DIR" ]; then
803
+ info "Removing configuration directory..."
804
+ rm -rf "$INSTALL_DIR"
805
+ success "Configuration directory removed"
806
+ fi
807
+
808
+ info "Removing PATH configuration..."
809
+ local shell_rc=""
810
+ if [ -n "$ZSH_VERSION" ]; then
811
+ shell_rc="$HOME/.zshrc"
812
+ elif [ -n "$BASH_VERSION" ]; then
813
+ shell_rc="$HOME/.bashrc"
814
+ else
815
+ shell_rc="$HOME/.profile"
816
+ fi
817
+
818
+ if [ -f "$shell_rc" ]; then
819
+ sed -i.bak '/# sshift/d' "$shell_rc"
820
+ sed -i.bak "/sshift/d" "$shell_rc"
821
+ rm -f "${shell_rc}.bak"
822
+ fi
823
+
824
+ echo ""
825
+ echo -e "${GREEN}sshift uninstalled successfully!${NC}"
826
+ echo ""
827
+ info "You may need to restart your terminal for PATH changes to take effect"
828
+ }
829
+
830
+ check_updates() {
831
+ info "Checking for updates..."
832
+
833
+ local local_version=$(get_installed_version)
834
+ local remote_version=$(get_latest_version)
835
+
836
+ info "Installed version: $local_version"
837
+ info "Latest version: $remote_version"
838
+
839
+ compare_versions "$local_version" "$remote_version"
840
+ local result=$?
841
+
842
+ if [ $result -eq 1 ]; then
843
+ warn "New version available: $remote_version (current: $local_version)"
844
+ return 1
845
+ elif [ $result -eq 2 ]; then
846
+ info "Local version is newer than remote (development version?)"
847
+ return 0
848
+ else
849
+ success "Already up to date (version $local_version)"
850
+ return 0
851
+ fi
852
+ }
853
+
854
+ main() {
855
+ while [ $# -gt 0 ]; do
856
+ case "$1" in
857
+ --install-dir)
858
+ INSTALL_DIR="$2"
859
+ shift 2
860
+ ;;
861
+ --port)
862
+ SERVER_PORT="$2"
863
+ shift 2
864
+ ;;
865
+ --start)
866
+ start_app
867
+ exit 0
868
+ ;;
869
+ --stop)
870
+ stop_app
871
+ exit 0
872
+ ;;
873
+ --restart)
874
+ info "Restarting sshift..."
875
+ stop_app
876
+ start_app
877
+ exit 0
878
+ ;;
879
+ --status)
880
+ if is_running; then
881
+ if command_exists systemctl && [ -f "/etc/systemd/system/$SERVICE_NAME.service" ] && systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
882
+ success "sshift is running (systemd)"
883
+ elif [ "$(uname)" = "Darwin" ] && [ -f "/Library/LaunchDaemons/com.sshift.plist" ]; then
884
+ local pid=$(sudo launchctl list 2>/dev/null | awk '$3 == "com.sshift" && $1 != "-" {print $1}')
885
+ if [ -n "$pid" ]; then
886
+ success "sshift is running (launchd, PID: $pid)"
887
+ else
888
+ success "sshift is running (launchd)"
889
+ fi
890
+ elif [ -f "$PID_FILE" ]; then
891
+ local pid=$(cat "$PID_FILE")
892
+ success "sshift is running (PID: $pid)"
893
+ else
894
+ success "sshift is running"
895
+ fi
896
+ exit 0
897
+ else
898
+ info "sshift is not running"
899
+ exit 1
900
+ fi
901
+ ;;
902
+ --update)
903
+ if ! command_exists sshift; then
904
+ error "sshift is not installed"
905
+ fi
906
+
907
+ echo ""
908
+ echo -e "${BOLD}=========================================="
909
+ echo -e " sshift Update Script"
910
+ echo -e "==========================================${NC}"
911
+ echo ""
912
+
913
+ info "Updating sshift..."
914
+ update_sshift
915
+ print_summary
916
+ exit 0
917
+ ;;
918
+ --uninstall)
919
+ uninstall_sshift
920
+ exit 0
921
+ ;;
922
+ --help|-h)
923
+ show_help
924
+ ;;
925
+ *)
926
+ error "Unknown option: $1. Use --help for usage information."
927
+ ;;
928
+ esac
929
+ done
930
+
931
+ echo ""
932
+ echo -e "${BOLD}=========================================="
933
+ echo -e " sshift Installation Script"
934
+ echo -e "==========================================${NC}"
935
+ echo ""
936
+
937
+ info "Installation method: npm"
938
+ if [ -n "$SERVER_PORT" ]; then
939
+ info "Server port: $SERVER_PORT"
940
+ fi
941
+
942
+ if command_exists sshift; then
943
+ info "sshift is already installed"
944
+
945
+ if ! check_updates; then
946
+ prompt_user "Update sshift? [Y/n] " "y"
947
+ if [[ ! $PROMPT_RESPONSE =~ ^[Nn]$ ]]; then
948
+ update_sshift
949
+ fi
950
+ else
951
+ if is_running; then
952
+ if command_exists systemctl && [ -f "/etc/systemd/system/$SERVICE_NAME.service" ] && systemctl is-active "$SERVICE_NAME" >/dev/null 2>&1; then
953
+ info "sshift is already running (systemd)"
954
+ elif [ -f "$PID_FILE" ]; then
955
+ local pid=$(cat "$PID_FILE")
956
+ info "sshift is already running (PID: $pid)"
957
+ else
958
+ info "sshift is already running"
959
+ fi
960
+ fi
961
+ fi
962
+ else
963
+ install_nodejs
964
+ install_sshift
965
+ create_config
966
+ fi
967
+
968
+ echo ""
969
+ info "Configuration options:"
970
+ echo ""
971
+
972
+ prompt_user "Add sshift to PATH? [Y/n] " "y"
973
+ if [[ ! $PROMPT_RESPONSE =~ ^[Nn]$ ]]; then
974
+ add_to_path
975
+ fi
976
+
977
+ local autostart_configured=false
978
+ prompt_user "Start sshift automatically on boot? [y/N] " "n"
979
+ if [[ $PROMPT_RESPONSE =~ ^[Yy]$ ]]; then
980
+ setup_autostart
981
+ autostart_configured=true
982
+ fi
983
+
984
+ if ! is_running; then
985
+ start_app
986
+ fi
987
+
988
+ print_summary
989
+ }
990
+
991
+ main "$@"