pinggy 0.6.7 → 0.6.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/ARCHITECTURE.md +29 -17
- package/CLAUDE.md +7 -7
- package/README.md +15 -4
- package/dist/{TunnelManager-JRVEADS4.js → TunnelManager-CDQGC6ZU.js} +3 -3
- package/dist/{TunnelTui-6H4NPUNF.js → TunnelTui-M5SIZX6B.js} +3 -3
- package/dist/{chunk-2GIJXE4K.js → chunk-3IPMFCEF.js} +2 -2
- package/dist/{chunk-3WMTTJHI.js → chunk-5HBGVJSW.js} +44 -8
- package/dist/{chunk-LWCVB42S.js → chunk-DGAH7UVX.js} +2 -2
- package/dist/{chunk-PMZJ6HFH.js → chunk-HJECJ7LC.js} +15 -8
- package/dist/{chunk-FWTQF6AB.js → chunk-LF6WMFWT.js} +671 -148
- package/dist/{chunk-3KCXR33Y.js → chunk-OQVFQ5VG.js} +145 -58
- package/dist/{chunk-H527WMTF.js → chunk-Q5RYSWFK.js} +3 -3
- package/dist/{chunk-CZOKR73F.js → chunk-VD5ZFCZW.js} +1 -1
- package/dist/{chunk-MAK5R4WQ.js → chunk-WFHMERT3.js} +13 -0
- package/dist/{configStore-JJUWT2YO.js → configStore-6LCHFPQX.js} +3 -3
- package/dist/{daemonChild-24Y3AKII.js → daemonChild-WTZ7X6SZ.js} +9 -9
- package/dist/{daemonConfig-VWK4TXVS.js → daemonConfig-R34QH73H.js} +2 -2
- package/dist/index.cjs +1110 -418
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +7 -7
- package/dist/{main-HSIFQAGU.js → main-INPCUGAN.js} +9 -9
- package/dist/{updateChild-JNGDWIAK.js → updateChild-BPWBQ3WR.js} +4 -4
- package/dist/workers/file_serve_worker.js +1 -1
- package/package.json +4 -2
- package/scripts/install/install.ps1 +301 -23
- package/scripts/install/install.sh +487 -52
|
@@ -1,24 +1,38 @@
|
|
|
1
1
|
#!/bin/sh
|
|
2
2
|
# Installs or updates the Pinggy CLI on macOS and Linux, then asks whether to sign this machine in as a Pinggy device.
|
|
3
3
|
# Once the device connects, it asks whether to start pinggy at login, unless that service is already installed.
|
|
4
|
+
# Before the sign-in, when a pinggy daemon runs another version than the installed one, it asks to restart the daemon.
|
|
4
5
|
#
|
|
5
6
|
# curl -fsSL https://pinggy.io/install.sh | sh
|
|
6
7
|
# curl -fsSL https://pinggy.io/install.sh | DEVICE_AGENT_TOKEN=<token> sh
|
|
7
8
|
#
|
|
9
|
+
# It updates the copy that the pinggy command runs in this terminal:
|
|
10
|
+
# no copy, or this script's binary installs or updates the binary, as below
|
|
11
|
+
# an npm copy updates it with `npm install -g`. No binary, no PATH change, and no login
|
|
12
|
+
# service question, because the npm version cannot run as a service
|
|
13
|
+
# another program file asks to install this script's binary. No signs in with that file
|
|
14
|
+
# a script (npm link, pnpm, yarn, bun) stops with a message
|
|
15
|
+
#
|
|
8
16
|
# The binary goes to ~/.pinggy/versions/<version>/pinggy. ~/.pinggy/bin/pinggy links to it, and a symlink in
|
|
9
|
-
# ~/.local/bin puts it on PATH.
|
|
10
|
-
# goes into the shell's rc file, once.
|
|
17
|
+
# ~/.local/bin puts it on PATH. The script never runs sudo, and the binary flow writes only under $HOME. When
|
|
18
|
+
# ~/.local/bin is not on PATH, 1 marked block goes into the shell's rc file, once.
|
|
11
19
|
#
|
|
12
20
|
# Setting, from the dashboard's command:
|
|
13
|
-
# DEVICE_AGENT_TOKEN a device token.
|
|
14
|
-
#
|
|
15
|
-
# Set it on sh, not on curl, so curl never holds it
|
|
21
|
+
# DEVICE_AGENT_TOKEN a device token. `pinggy device connect` runs with no email and no code. Every update question
|
|
22
|
+
# is still asked, and a pinggy older than 0.6.2 ignores the token. The token reaches the CLI in
|
|
23
|
+
# its environment, never on a command line. Set it on sh, not on curl, so curl never holds it
|
|
16
24
|
#
|
|
17
25
|
# Overrides, for dev and tests:
|
|
18
|
-
# PINGGY_VERSION a release tag, such as v0.6.1. Default: the version pinggy.io publishes
|
|
19
|
-
#
|
|
26
|
+
# PINGGY_VERSION a release tag, such as v0.6.1. Default: the version pinggy.io publishes, or for an npm copy the
|
|
27
|
+
# npm registry's latest
|
|
28
|
+
# PINGGY_BINARY_URL a full binary URL, file:// included. Replaces the installed binary without a version check,
|
|
29
|
+
# whatever the pinggy command runs
|
|
20
30
|
# PINGGY_MANAGE a non-production dashboard host, passed to `pinggy device login` or `connect` as --manage
|
|
21
31
|
#
|
|
32
|
+
# Set by a downloaded pinggy binary that installs itself, with PINGGY_BINARY_URL naming that file:
|
|
33
|
+
# PINGGY_INSTALL_ONLY 1: install, then end. No sign-in, and no PATH warning, which the binary prints after it
|
|
34
|
+
# removes npm's copy
|
|
35
|
+
#
|
|
22
36
|
# The source lives in Pinggy-io/cli-js, scripts/install/. A release copies it to pinggy.io.
|
|
23
37
|
#
|
|
24
38
|
# Nothing runs until the last line. A download cut off halfway defines some functions and runs no command.
|
|
@@ -34,6 +48,24 @@ LINK_DIR="$HOME/.local/bin"
|
|
|
34
48
|
LINK_PATH="$LINK_DIR/pinggy"
|
|
35
49
|
RC_MARKER="# Added by the Pinggy installer"
|
|
36
50
|
VERSION_PREFIX="Pinggy CLI version: "
|
|
51
|
+
NPM_PACKAGE="pinggy"
|
|
52
|
+
# npm keeps a global package at <prefix>/lib/node_modules/<name>. yarn and bun use other folders.
|
|
53
|
+
NPM_PACKAGE_SEGMENT="/lib/node_modules/$NPM_PACKAGE/"
|
|
54
|
+
# As the kernel's limit, so a link loop ends.
|
|
55
|
+
LINK_HOP_LIMIT=40
|
|
56
|
+
# A request ends after this many seconds with no data. Nothing limits the total time, so a slow download that keeps
|
|
57
|
+
# moving finishes. The OS ends a connection attempt with no answer, and curl a stalled TLS handshake after 5 min.
|
|
58
|
+
STALL_SECONDS=60
|
|
59
|
+
BYTES_PER_MEGABYTE=1048576
|
|
60
|
+
# The file a running daemon writes, where src/utils/configDir.ts puts it.
|
|
61
|
+
DAEMON_INFO_PATH="${XDG_CONFIG_HOME:-$HOME/.config}/pinggy/daemon.json"
|
|
62
|
+
# Every daemon process runs with this flag: getDaemonSpawnArgs() and the service files that serviceInstaller.ts writes.
|
|
63
|
+
DAEMON_PROCESS_FLAG="--_daemon-child"
|
|
64
|
+
# The service names src/daemon/lifecycle/serviceInstaller.ts writes.
|
|
65
|
+
LAUNCHD_LABEL="io.pinggy.agent"
|
|
66
|
+
SYSTEMD_UNIT="pinggy"
|
|
67
|
+
# How long a restarted daemon may take to stop, and then to write daemon.json.
|
|
68
|
+
DAEMON_RESTART_WAIT_SECONDS=10
|
|
37
69
|
|
|
38
70
|
say() {
|
|
39
71
|
printf '%s\n' "$*"
|
|
@@ -130,11 +162,13 @@ require_downloader() {
|
|
|
130
162
|
fi
|
|
131
163
|
}
|
|
132
164
|
|
|
165
|
+
# --speed-limit 1 --speed-time ends a request that gets less than 1 byte per second for STALL_SECONDS. wget's read
|
|
166
|
+
# timeout is also time without data. Its default is 900 s, with 20 tries.
|
|
133
167
|
fetch_text() {
|
|
134
168
|
if has_command curl; then
|
|
135
|
-
curl -fsSL --proto '=https' --tlsv1.2 "$1"
|
|
169
|
+
curl -fsSL --proto '=https' --tlsv1.2 --speed-limit 1 --speed-time "$STALL_SECONDS" "$1"
|
|
136
170
|
else
|
|
137
|
-
wget -q --https-only -O - "$1"
|
|
171
|
+
wget -q --https-only --read-timeout="$STALL_SECONDS" --tries=2 -O - "$1"
|
|
138
172
|
fi
|
|
139
173
|
}
|
|
140
174
|
|
|
@@ -153,6 +187,7 @@ resolve_target_tag() {
|
|
|
153
187
|
esac
|
|
154
188
|
return 0
|
|
155
189
|
fi
|
|
190
|
+
say "Checking the latest version of Pinggy..."
|
|
156
191
|
version_json=$(fetch_text "$VERSION_URL") || fail_no_latest_version "could not reach"
|
|
157
192
|
latest_version=$(printf '%s\n' "$version_json" \
|
|
158
193
|
| sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([0-9][0-9.]*\)".*/\1/p' | head -n 1)
|
|
@@ -176,15 +211,7 @@ download() {
|
|
|
176
211
|
cp "${url#file://}" "$destination"
|
|
177
212
|
;;
|
|
178
213
|
https://*)
|
|
179
|
-
|
|
180
|
-
if [ -t 2 ]; then
|
|
181
|
-
curl -fL --proto '=https' --tlsv1.2 --progress-bar -o "$destination" "$url"
|
|
182
|
-
else
|
|
183
|
-
curl -fsSL --proto '=https' --tlsv1.2 -o "$destination" "$url"
|
|
184
|
-
fi
|
|
185
|
-
else
|
|
186
|
-
wget -q --https-only -O "$destination" "$url"
|
|
187
|
-
fi
|
|
214
|
+
download_https "$url" "$destination"
|
|
188
215
|
;;
|
|
189
216
|
*)
|
|
190
217
|
fail "PINGGY_BINARY_URL must start with https:// or file://"
|
|
@@ -192,6 +219,86 @@ download() {
|
|
|
192
219
|
esac
|
|
193
220
|
}
|
|
194
221
|
|
|
222
|
+
# The download runs in the background, and with a terminal on stderr 1 line shows its size each second. curl's own
|
|
223
|
+
# bar draws an animation until a redirect's headers name the size, and draws for 79 columns, because `curl | sh`
|
|
224
|
+
# gives curl no terminal width. curl's errors wait in a file, so they print after the line ends, not inside it.
|
|
225
|
+
# curl and wget start here directly, not through a function, so $! is their pid and the EXIT trap can stop them.
|
|
226
|
+
download_https() {
|
|
227
|
+
headers_path="$download_dir/headers"
|
|
228
|
+
errors_path="$download_dir/errors"
|
|
229
|
+
: >"$headers_path"
|
|
230
|
+
: >"$2"
|
|
231
|
+
if has_command curl; then
|
|
232
|
+
curl -fsSL --proto '=https' --tlsv1.2 --speed-limit 1 --speed-time "$STALL_SECONDS" \
|
|
233
|
+
-D "$headers_path" -o "$2" "$1" 2>"$errors_path" &
|
|
234
|
+
else
|
|
235
|
+
wget -q --https-only --read-timeout="$STALL_SECONDS" --tries=2 -O "$2" "$1" 2>"$errors_path" &
|
|
236
|
+
fi
|
|
237
|
+
download_pid=$!
|
|
238
|
+
total_bytes=""
|
|
239
|
+
show_progress=0
|
|
240
|
+
if [ -t 2 ]; then
|
|
241
|
+
show_progress=1
|
|
242
|
+
fi
|
|
243
|
+
# dash and bash reap the finished download while they wait for sleep, so kill -0 then fails and the loop ends.
|
|
244
|
+
while kill -0 "$download_pid" 2>/dev/null; do
|
|
245
|
+
if [ "$show_progress" = 1 ]; then
|
|
246
|
+
print_progress "$2"
|
|
247
|
+
fi
|
|
248
|
+
sleep 1
|
|
249
|
+
done
|
|
250
|
+
download_status=0
|
|
251
|
+
wait "$download_pid" || download_status=$?
|
|
252
|
+
download_pid=""
|
|
253
|
+
# The last line in the loop can be up to 1 s old.
|
|
254
|
+
if [ "$show_progress" = 1 ] && [ "$download_status" -eq 0 ]; then
|
|
255
|
+
print_progress "$2"
|
|
256
|
+
fi
|
|
257
|
+
end_progress_line
|
|
258
|
+
cat "$errors_path" >&2
|
|
259
|
+
return "$download_status"
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
# Rewrites the progress line with \r. %-30s pads it past the longest text, " 999.9 MB of 999.9 MB (100%)", so a
|
|
263
|
+
# shorter text covers a longer one. macOS wc pads its count with spaces, which the arithmetic drops.
|
|
264
|
+
print_progress() {
|
|
265
|
+
if [ -z "$total_bytes" ]; then
|
|
266
|
+
total_bytes=$(read_total_bytes)
|
|
267
|
+
fi
|
|
268
|
+
downloaded_bytes=$(($(wc -c <"$1")))
|
|
269
|
+
progress_text=" $(format_megabytes "$downloaded_bytes")"
|
|
270
|
+
if [ -n "$total_bytes" ]; then
|
|
271
|
+
percent=$((downloaded_bytes * 100 / total_bytes))
|
|
272
|
+
if [ "$percent" -gt 100 ]; then
|
|
273
|
+
percent=100
|
|
274
|
+
fi
|
|
275
|
+
progress_text="$progress_text of $(format_megabytes "$total_bytes") ($percent%)"
|
|
276
|
+
fi
|
|
277
|
+
printf '\r%-30s' "$progress_text" >&2
|
|
278
|
+
progress_line_open=1
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
end_progress_line() {
|
|
282
|
+
if [ "${progress_line_open:-0}" = 1 ]; then
|
|
283
|
+
printf '\n' >&2
|
|
284
|
+
progress_line_open=0
|
|
285
|
+
fi
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
# The Content-Length of the 2xx response in curl's header file, or nothing. A redirect writes its own headers first,
|
|
289
|
+
# and GitHub's redirect sends Content-Length 0. HTTP/2 sends header names in lowercase, HTTP/1.1 does not.
|
|
290
|
+
read_total_bytes() {
|
|
291
|
+
awk 'tolower($1) ~ /^http\// { success = ($2 ~ /^2/) }
|
|
292
|
+
success && tolower($1) == "content-length:" { total = $2 + 0 }
|
|
293
|
+
END { if (total > 0) print total }' "$headers_path"
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
# Integer math, so the decimal point does not follow the locale and every sh can compute it.
|
|
297
|
+
format_megabytes() {
|
|
298
|
+
tenths=$(($1 * 10 / BYTES_PER_MEGABYTE))
|
|
299
|
+
printf '%d.%d MB' $((tenths / 10)) $((tenths % 10))
|
|
300
|
+
}
|
|
301
|
+
|
|
195
302
|
# Node's error line names the cause, such as a missing libssl.so.3 on a minimal Debian image. A binary for another
|
|
196
303
|
# CPU prints nothing useful, and the message below already names the platform.
|
|
197
304
|
fail_does_not_run() {
|
|
@@ -202,28 +309,42 @@ fail_does_not_run() {
|
|
|
202
309
|
fail "the downloaded file does not run on this machine ($os-$cpu). The installed pinggy is unchanged."
|
|
203
310
|
}
|
|
204
311
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
312
|
+
# The EXIT trap. A non-interactive sh starts a background command with SIGINT ignored, so Ctrl+C stops only the
|
|
313
|
+
# script, and the download would go on without this kill. After a hangup the terminal is gone and the newline cannot
|
|
314
|
+
# be written, so it comes last and its failure does not stop the trap under set -e.
|
|
315
|
+
clean_up_download() {
|
|
316
|
+
if [ -n "${download_pid:-}" ]; then
|
|
317
|
+
kill "$download_pid" 2>/dev/null || true
|
|
318
|
+
fi
|
|
319
|
+
if [ -n "${download_dir:-}" ]; then
|
|
320
|
+
rm -rf "$download_dir"
|
|
208
321
|
fi
|
|
322
|
+
end_progress_line 2>/dev/null || true
|
|
209
323
|
}
|
|
210
324
|
|
|
211
|
-
# Writes the binary to a temp
|
|
212
|
-
# own version folder and ~/.pinggy/bin/pinggy links to it. A running agent keeps its own file.
|
|
325
|
+
# Writes the binary to a temp folder under ~/.pinggy, so the final mv is a rename on 1 filesystem. The binary goes in
|
|
326
|
+
# its own version folder and ~/.pinggy/bin/pinggy links to it. A running agent keeps its own file.
|
|
213
327
|
install_binary() {
|
|
214
328
|
if [ -n "${PINGGY_BINARY_URL:-}" ]; then
|
|
215
329
|
url="$PINGGY_BINARY_URL"
|
|
330
|
+
download_title="$url"
|
|
216
331
|
else
|
|
217
332
|
url="$RELEASES_URL/download/$tag/pinggy-$os-$cpu"
|
|
333
|
+
download_title="pinggy ${tag#v} for $os-$cpu"
|
|
218
334
|
fi
|
|
219
335
|
|
|
220
336
|
mkdir -p "$INSTALL_DIR"
|
|
221
|
-
|
|
222
|
-
|
|
337
|
+
download_dir=$(mktemp -d "$INSTALL_DIR/pinggy.download.XXXXXX")
|
|
338
|
+
download_path="$download_dir/pinggy"
|
|
339
|
+
trap clean_up_download EXIT
|
|
340
|
+
trap 'exit 129' HUP
|
|
223
341
|
trap 'exit 130' INT
|
|
224
342
|
trap 'exit 143' TERM
|
|
225
343
|
|
|
226
|
-
|
|
344
|
+
case "$url" in
|
|
345
|
+
file://*) say "Copying ${url#file://}" ;;
|
|
346
|
+
*) say "Downloading $download_title" ;;
|
|
347
|
+
esac
|
|
227
348
|
download "$url" "$download_path" || fail "the download failed: $url"
|
|
228
349
|
chmod 755 "$download_path"
|
|
229
350
|
|
|
@@ -235,7 +356,8 @@ install_binary() {
|
|
|
235
356
|
previous_target=$(readlink "$INSTALL_PATH" 2>/dev/null || true)
|
|
236
357
|
mkdir -p "$VERSIONS_DIR/$downloaded_version"
|
|
237
358
|
mv -f "$download_path" "$VERSIONS_DIR/$downloaded_version/pinggy"
|
|
238
|
-
|
|
359
|
+
rm -rf "$download_dir"
|
|
360
|
+
download_dir=""
|
|
239
361
|
# A temp link and a rename, because ln -sf removes the old link before it creates the new one.
|
|
240
362
|
ln -s "$VERSIONS_DIR/$downloaded_version/pinggy" "$INSTALL_DIR/pinggy.link.$$"
|
|
241
363
|
mv -f "$INSTALL_DIR/pinggy.link.$$" "$INSTALL_PATH"
|
|
@@ -276,10 +398,6 @@ install_or_update() {
|
|
|
276
398
|
install_binary
|
|
277
399
|
elif [ "$installed_version" = "$target_version" ]; then
|
|
278
400
|
say "pinggy $installed_version is up to date."
|
|
279
|
-
elif has_device_token; then
|
|
280
|
-
# An older CLI ignores DEVICE_AGENT_TOKEN, so keeping it would leave the machine unconnected.
|
|
281
|
-
say "Updating pinggy $installed_version to $target_version."
|
|
282
|
-
install_binary
|
|
283
401
|
elif confirm "pinggy $installed_version is installed. Update to $target_version? [Y/n] "; then
|
|
284
402
|
install_binary
|
|
285
403
|
else
|
|
@@ -295,6 +413,165 @@ warn_auto_update_replaces_it() {
|
|
|
295
413
|
fi
|
|
296
414
|
}
|
|
297
415
|
|
|
416
|
+
check_binary_prerequisites() {
|
|
417
|
+
if [ "$os" = linux ]; then
|
|
418
|
+
require_glibc
|
|
419
|
+
fi
|
|
420
|
+
detect_cpu
|
|
421
|
+
case "${PINGGY_BINARY_URL:-}" in
|
|
422
|
+
file://*) ;;
|
|
423
|
+
*) require_downloader ;;
|
|
424
|
+
esac
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
install_with_binary() {
|
|
428
|
+
check_binary_prerequisites
|
|
429
|
+
install_or_update
|
|
430
|
+
warn_auto_update_replaces_it
|
|
431
|
+
link_into_path
|
|
432
|
+
pinggy_path="$INSTALL_PATH"
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
# Prints the file $1 leads to, with every link resolved, in the file and in the folders above it, as realpath does.
|
|
436
|
+
# macOS before 13 has no realpath. Prints nothing for a link loop. CDPATH would make cd print the folder.
|
|
437
|
+
physical_path() {
|
|
438
|
+
resolved_path="$1"
|
|
439
|
+
link_hops=0
|
|
440
|
+
while [ -L "$resolved_path" ]; do
|
|
441
|
+
link_hops=$((link_hops + 1))
|
|
442
|
+
if [ "$link_hops" -gt "$LINK_HOP_LIMIT" ]; then
|
|
443
|
+
return 1
|
|
444
|
+
fi
|
|
445
|
+
link_target=$(readlink "$resolved_path")
|
|
446
|
+
case "$link_target" in
|
|
447
|
+
/*) resolved_path="$link_target" ;;
|
|
448
|
+
*) resolved_path="$(dirname "$resolved_path")/$link_target" ;;
|
|
449
|
+
esac
|
|
450
|
+
done
|
|
451
|
+
resolved_dir=$(CDPATH='' cd -P "$(dirname "$resolved_path")" 2>/dev/null && pwd -P) || return 1
|
|
452
|
+
printf '%s/%s\n' "$resolved_dir" "$(basename "$resolved_path")"
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
# pnpm's launcher, and the JS file that npm link, yarn or bun link to, start with #!. A downloaded binary does not.
|
|
456
|
+
is_script_file() {
|
|
457
|
+
[ "$(dd if="$1" bs=2 count=1 2>/dev/null)" = '#!' ]
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
# Sets pinggy_command to the file the pinggy command runs in this terminal, and pinggy_kind to none, binary (this
|
|
461
|
+
# script's), npm, other_binary or other_script. `command -v` prints a name, not a path, for a shell function.
|
|
462
|
+
find_pinggy_command() {
|
|
463
|
+
pinggy_command=$(command -v pinggy 2>/dev/null || true)
|
|
464
|
+
case "$pinggy_command" in
|
|
465
|
+
/*) ;;
|
|
466
|
+
*)
|
|
467
|
+
pinggy_kind=none
|
|
468
|
+
return 0
|
|
469
|
+
;;
|
|
470
|
+
esac
|
|
471
|
+
pinggy_real_path=$(physical_path "$pinggy_command" || true)
|
|
472
|
+
if [ -z "$pinggy_real_path" ]; then
|
|
473
|
+
pinggy_real_path="$pinggy_command"
|
|
474
|
+
fi
|
|
475
|
+
home_real_path=$(CDPATH='' cd -P "$HOME" 2>/dev/null && pwd -P || printf '%s' "$HOME")
|
|
476
|
+
case "$pinggy_real_path" in
|
|
477
|
+
"$home_real_path"/.pinggy/*) pinggy_kind=binary ;;
|
|
478
|
+
*"$NPM_PACKAGE_SEGMENT"*) pinggy_kind=npm ;;
|
|
479
|
+
*)
|
|
480
|
+
if is_script_file "$pinggy_real_path"; then
|
|
481
|
+
pinggy_kind=other_script
|
|
482
|
+
else
|
|
483
|
+
pinggy_kind=other_binary
|
|
484
|
+
fi
|
|
485
|
+
;;
|
|
486
|
+
esac
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
# npm publish runs as a separate job from the binaries, so the registry can name an older version than pinggy.io.
|
|
490
|
+
# `npm install -g` reads the registry, so the target comes from there.
|
|
491
|
+
resolve_npm_target_version() {
|
|
492
|
+
if [ -n "${PINGGY_VERSION:-}" ]; then
|
|
493
|
+
npm_target_version=${PINGGY_VERSION#v}
|
|
494
|
+
return 0
|
|
495
|
+
fi
|
|
496
|
+
say "Checking the latest version of pinggy on npm..."
|
|
497
|
+
npm_target_version=$(npm view "$NPM_PACKAGE" version </dev/null 2>/dev/null || true)
|
|
498
|
+
if [ -z "$npm_target_version" ]; then
|
|
499
|
+
fail "could not read the latest pinggy version from the npm registry. Try again later."
|
|
500
|
+
fi
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
# npm writes the package folder and the command link under its prefix. A prefix this user cannot write, such as a
|
|
504
|
+
# system Node owned by root, needs sudo, and this script never runs sudo.
|
|
505
|
+
install_npm_package() {
|
|
506
|
+
package_spec="$NPM_PACKAGE@$npm_target_version"
|
|
507
|
+
if [ ! -w "$npm_prefix/lib/node_modules" ] || [ ! -w "$npm_prefix/bin" ]; then
|
|
508
|
+
fail "this user cannot write npm's global folder, $npm_prefix." \
|
|
509
|
+
"Run: sudo npm install -g --prefix $npm_prefix $package_spec, then run this script again."
|
|
510
|
+
fi
|
|
511
|
+
say "Running: npm install -g --prefix $(shown "$npm_prefix") $package_spec"
|
|
512
|
+
npm install -g --prefix "$npm_prefix" --no-fund --no-audit "$package_spec" </dev/null \
|
|
513
|
+
|| fail "npm could not install $package_spec. npm printed the reason above."
|
|
514
|
+
say "Installed pinggy $npm_target_version with npm"
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
# The prefix is the path before lib/node_modules/pinggy. --prefix updates this copy even when the npm on PATH uses
|
|
518
|
+
# another global folder, such as nvm's Node while pinggy came from the system Node.
|
|
519
|
+
update_npm_copy() {
|
|
520
|
+
npm_prefix=${pinggy_real_path%%"$NPM_PACKAGE_SEGMENT"*}
|
|
521
|
+
if ! has_command npm; then
|
|
522
|
+
fail "pinggy at $(shown "$pinggy_command") came from npm, but npm is not on PATH."
|
|
523
|
+
fi
|
|
524
|
+
resolve_npm_target_version
|
|
525
|
+
installed_version=$(binary_version "$pinggy_command")
|
|
526
|
+
if [ -z "$installed_version" ]; then
|
|
527
|
+
if ! confirm "$(shown "$pinggy_command") does not run. Reinstall it with npm? [Y/n] "; then
|
|
528
|
+
fail "$(shown "$pinggy_command") does not run, so it cannot sign in this device."
|
|
529
|
+
fi
|
|
530
|
+
install_npm_package
|
|
531
|
+
elif [ "$installed_version" = "$npm_target_version" ]; then
|
|
532
|
+
say "pinggy $installed_version, installed with npm, is up to date."
|
|
533
|
+
elif confirm "pinggy $installed_version is installed with npm. Update to $npm_target_version? [Y/n] "; then
|
|
534
|
+
install_npm_package
|
|
535
|
+
else
|
|
536
|
+
say "Keeping pinggy $installed_version."
|
|
537
|
+
fi
|
|
538
|
+
pinggy_path="$pinggy_command"
|
|
539
|
+
skip_login_service=1
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
# A program file this script did not install, such as a binary downloaded by hand. No keeps it, and the script signs
|
|
543
|
+
# in with it.
|
|
544
|
+
offer_binary_over_other_copy() {
|
|
545
|
+
check_binary_prerequisites
|
|
546
|
+
resolve_target_tag
|
|
547
|
+
target_version=${tag#v}
|
|
548
|
+
other_version=$(binary_version "$pinggy_command")
|
|
549
|
+
if [ -n "$other_version" ]; then
|
|
550
|
+
say "pinggy $other_version at $(shown "$pinggy_command") was not installed by this script."
|
|
551
|
+
else
|
|
552
|
+
say "$(shown "$pinggy_command") was not installed by this script."
|
|
553
|
+
fi
|
|
554
|
+
if ! confirm "Install the latest pinggy, $target_version, to $(shown "$INSTALL_PATH")? [Y/n] "; then
|
|
555
|
+
say "Keeping $(shown "$pinggy_command")."
|
|
556
|
+
pinggy_path="$pinggy_command"
|
|
557
|
+
skip_login_service=1
|
|
558
|
+
return 0
|
|
559
|
+
fi
|
|
560
|
+
if [ "$(binary_version "$INSTALL_PATH")" = "$target_version" ]; then
|
|
561
|
+
say "pinggy $target_version is up to date in $(shown "$INSTALL_PATH")."
|
|
562
|
+
else
|
|
563
|
+
install_binary
|
|
564
|
+
fi
|
|
565
|
+
warn_auto_update_replaces_it
|
|
566
|
+
link_into_path
|
|
567
|
+
pinggy_path="$INSTALL_PATH"
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
fail_unknown_copy() {
|
|
571
|
+
fail "pinggy at $(shown "$pinggy_command") was not installed by this script or npm.
|
|
572
|
+
Update it the way you installed it."
|
|
573
|
+
}
|
|
574
|
+
|
|
298
575
|
link_into_path() {
|
|
299
576
|
mkdir -p "$LINK_DIR"
|
|
300
577
|
if [ -L "$LINK_PATH" ]; then
|
|
@@ -315,6 +592,9 @@ link_into_path() {
|
|
|
315
592
|
|
|
316
593
|
# The rc block puts ~/.local/bin first, so this matters only when ~/.local/bin was already on PATH.
|
|
317
594
|
warn_if_shadowed() {
|
|
595
|
+
if is_install_only; then
|
|
596
|
+
return 0
|
|
597
|
+
fi
|
|
318
598
|
found_path=$(command -v pinggy 2>/dev/null || true)
|
|
319
599
|
if [ -n "$found_path" ] && [ "$found_path" != "$LINK_PATH" ]; then
|
|
320
600
|
say "Another pinggy, $(shown "$found_path"), comes before $(shown "$LINK_DIR") on PATH, so the pinggy command runs it."
|
|
@@ -375,8 +655,8 @@ add_link_dir_to_rc_file() {
|
|
|
375
655
|
# question, and service-install replaces it.
|
|
376
656
|
login_service_installed() {
|
|
377
657
|
case "$os" in
|
|
378
|
-
macos) service_file="$HOME/Library/LaunchAgents
|
|
379
|
-
linux) service_file="$HOME/.config/systemd/user
|
|
658
|
+
macos) service_file="$HOME/Library/LaunchAgents/$LAUNCHD_LABEL.plist" ;;
|
|
659
|
+
linux) service_file="$HOME/.config/systemd/user/$SYSTEMD_UNIT.service" ;;
|
|
380
660
|
esac
|
|
381
661
|
[ -f "$service_file" ] && grep -qF "$INSTALL_PATH" "$service_file"
|
|
382
662
|
}
|
|
@@ -390,7 +670,11 @@ login_service_supported() {
|
|
|
390
670
|
}
|
|
391
671
|
|
|
392
672
|
# Asks once the device connects, so the agent starts again after a restart. Leaves an installed service as it is.
|
|
673
|
+
# `pinggy daemon service-install` runs only from this script's binary, so another copy skips the question.
|
|
393
674
|
offer_login_service() {
|
|
675
|
+
if [ "${skip_login_service:-0}" = 1 ]; then
|
|
676
|
+
return 0
|
|
677
|
+
fi
|
|
394
678
|
if login_service_installed || ! login_service_supported; then
|
|
395
679
|
return 0
|
|
396
680
|
fi
|
|
@@ -409,18 +693,161 @@ offer_login_service() {
|
|
|
409
693
|
fi
|
|
410
694
|
}
|
|
411
695
|
|
|
412
|
-
#
|
|
413
|
-
#
|
|
414
|
-
|
|
415
|
-
|
|
696
|
+
# Prints field $1 of daemon.json. JSON.stringify(info, null, 2) writes 1 key per line, so sed reads it without a JSON
|
|
697
|
+
# parser. A string prints without its quotes.
|
|
698
|
+
daemon_info_field() {
|
|
699
|
+
sed -n "s/^[[:space:]]*\"$1\"[[:space:]]*:[[:space:]]*\"\{0,1\}\([^\",]*\)\"\{0,1\},\{0,1\}[[:space:]]*\$/\1/p" \
|
|
700
|
+
"$DAEMON_INFO_PATH" | head -n 1
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
# Sets daemon_pid, daemon_version and daemon_host when daemon.json names a live daemon. A daemon.json left by a crash
|
|
704
|
+
# can name a pid that a reboot gave to another program, so the process must run DAEMON_PROCESS_FLAG. Without ps, as
|
|
705
|
+
# in a slim container, a live pid is enough.
|
|
706
|
+
read_running_daemon() {
|
|
707
|
+
if [ ! -f "$DAEMON_INFO_PATH" ]; then
|
|
708
|
+
return 1
|
|
709
|
+
fi
|
|
710
|
+
daemon_pid=$(daemon_info_field pid)
|
|
711
|
+
daemon_version=$(daemon_info_field cliVersion)
|
|
712
|
+
daemon_host=$(daemon_info_field host)
|
|
713
|
+
case "$daemon_pid" in
|
|
714
|
+
'' | *[!0-9]*) return 1 ;;
|
|
715
|
+
esac
|
|
716
|
+
if ! kill -0 "$daemon_pid" 2>/dev/null; then
|
|
717
|
+
return 1
|
|
718
|
+
fi
|
|
719
|
+
if ! has_command ps; then
|
|
720
|
+
return 0
|
|
721
|
+
fi
|
|
722
|
+
case "$(ps -p "$daemon_pid" -o args= 2>/dev/null)" in
|
|
723
|
+
*"$DAEMON_PROCESS_FLAG"*) return 0 ;;
|
|
724
|
+
*) return 1 ;;
|
|
725
|
+
esac
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
launchd_target() {
|
|
729
|
+
printf 'gui/%s/%s' "$(id -u)" "$LAUNCHD_LABEL"
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
# Sets daemon_restart to launchd or systemd when that service's process is the daemon, as serviceRestartInstruction()
|
|
733
|
+
# in src/daemon/lifecycle/serviceInstaller.ts decides. A plain stop would leave the service stopped, and
|
|
734
|
+
# `daemon start` would then run a daemon outside it.
|
|
735
|
+
choose_daemon_restart() {
|
|
736
|
+
daemon_restart=plain
|
|
737
|
+
if [ "$os" = macos ]; then
|
|
738
|
+
if launchctl print "$(launchd_target)" 2>/dev/null | grep -q "^[[:space:]]*pid = $daemon_pid\$"; then
|
|
739
|
+
daemon_restart=launchd
|
|
740
|
+
fi
|
|
741
|
+
elif has_command systemctl \
|
|
742
|
+
&& [ "$(systemctl --user show -p MainPID --value "$SYSTEMD_UNIT" 2>/dev/null)" = "$daemon_pid" ]; then
|
|
743
|
+
daemon_restart=systemd
|
|
744
|
+
fi
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
# The command the user runs to restart the daemon later. `pinggy daemon stop` waits for the exit before it returns.
|
|
748
|
+
daemon_restart_command() {
|
|
749
|
+
case "$daemon_restart" in
|
|
750
|
+
launchd) printf 'launchctl kickstart -k %s' "$(launchd_target)" ;;
|
|
751
|
+
systemd) printf 'systemctl --user restart %s' "$SYSTEMD_UNIT" ;;
|
|
752
|
+
*) printf 'pinggy daemon stop && pinggy daemon start' ;;
|
|
753
|
+
esac
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
# Returns 1 when process $1 still runs after DAEMON_RESTART_WAIT_SECONDS.
|
|
757
|
+
wait_for_exit() {
|
|
758
|
+
waited_seconds=0
|
|
759
|
+
while kill -0 "$1" 2>/dev/null; do
|
|
760
|
+
if [ "$waited_seconds" -ge "$DAEMON_RESTART_WAIT_SECONDS" ]; then
|
|
761
|
+
return 1
|
|
762
|
+
fi
|
|
763
|
+
sleep 1
|
|
764
|
+
waited_seconds=$((waited_seconds + 1))
|
|
765
|
+
done
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
# Stops daemon $1 and starts a new one from $pinggy_path, or through its service. A daemon copies the environment of
|
|
769
|
+
# the command that starts it, so the subshell unsets DEVICE_AGENT_TOKEN first. `pinggy daemon stop` exits 0 when the
|
|
770
|
+
# stop fails, so the script checks the process itself before `daemon start`.
|
|
771
|
+
restart_daemon() {
|
|
772
|
+
(
|
|
773
|
+
unset DEVICE_AGENT_TOKEN
|
|
774
|
+
case "$daemon_restart" in
|
|
775
|
+
launchd) launchctl kickstart -k "$(launchd_target)" >/dev/null ;;
|
|
776
|
+
systemd) systemctl --user restart "$SYSTEMD_UNIT" >/dev/null ;;
|
|
777
|
+
*)
|
|
778
|
+
"$pinggy_path" daemon stop </dev/null >/dev/null
|
|
779
|
+
if wait_for_exit "$1"; then
|
|
780
|
+
"$pinggy_path" daemon start </dev/null >/dev/null
|
|
781
|
+
fi
|
|
782
|
+
;;
|
|
783
|
+
esac
|
|
784
|
+
) || true
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
# Returns 0 once daemon.json names a daemon other than $1 that runs version $2. launchd and systemd return before the
|
|
788
|
+
# new daemon writes daemon.json.
|
|
789
|
+
wait_for_new_daemon() {
|
|
790
|
+
waited_seconds=0
|
|
791
|
+
while [ "$waited_seconds" -lt "$DAEMON_RESTART_WAIT_SECONDS" ]; do
|
|
792
|
+
if read_running_daemon && [ "$daemon_pid" != "$1" ] && [ "$daemon_version" = "$2" ]; then
|
|
793
|
+
return 0
|
|
794
|
+
fi
|
|
795
|
+
sleep 1
|
|
796
|
+
waited_seconds=$((waited_seconds + 1))
|
|
797
|
+
done
|
|
798
|
+
return 1
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
# A daemon keeps the version it started with. When it runs another version than $pinggy_path, the copy this script
|
|
802
|
+
# signs in with, the script asks to restart it, so the device agent and auto-start tunnels run the new version. The
|
|
803
|
+
# Pinggy app's daemon belongs to the app, which refuses /shutdown. A daemon.json with no cliVersion comes from an
|
|
804
|
+
# older build, so it differs.
|
|
805
|
+
offer_daemon_restart() {
|
|
806
|
+
if ! read_running_daemon || [ "$daemon_host" = app ]; then
|
|
807
|
+
return 0
|
|
808
|
+
fi
|
|
809
|
+
new_version=$(binary_version "$pinggy_path")
|
|
810
|
+
if [ -z "$new_version" ] || [ "$daemon_version" = "$new_version" ]; then
|
|
811
|
+
return 0
|
|
812
|
+
fi
|
|
813
|
+
old_daemon_pid=$daemon_pid
|
|
814
|
+
running_version=${daemon_version:-an older version}
|
|
815
|
+
choose_daemon_restart
|
|
816
|
+
restart_command=$(daemon_restart_command)
|
|
817
|
+
|
|
818
|
+
say ""
|
|
819
|
+
# confirm() takes no terminal as yes, and a restart stops tunnels.
|
|
820
|
+
if ! has_terminal; then
|
|
821
|
+
say "The pinggy daemon still runs $running_version. To restart it with $new_version, run: $restart_command"
|
|
822
|
+
return 0
|
|
823
|
+
fi
|
|
824
|
+
say "The pinggy daemon still runs $running_version. Restarting it stops its tunnels." \
|
|
825
|
+
"Auto-start tunnels and the device agent start again."
|
|
826
|
+
if ! confirm "Restart it with $new_version? [Y/n] "; then
|
|
827
|
+
say "To restart it later, run: $restart_command"
|
|
828
|
+
return 0
|
|
829
|
+
fi
|
|
830
|
+
restart_daemon "$old_daemon_pid"
|
|
831
|
+
if wait_for_new_daemon "$old_daemon_pid" "$new_version"; then
|
|
832
|
+
say "The pinggy daemon now runs $new_version."
|
|
833
|
+
else
|
|
834
|
+
say "Could not restart the pinggy daemon. Run: $restart_command"
|
|
835
|
+
fi
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
# Runs $pinggy_path, the copy this script installed, updated or kept, by its full path, so another pinggy earlier on
|
|
839
|
+
# PATH does not answer. connect reads DEVICE_AGENT_TOKEN from the environment this script passes on, and reads no
|
|
840
|
+
# key, so it runs the same with no terminal. -b is the default now. It is passed anyway, so an older pinggy the user
|
|
841
|
+
# kept runs the agent in its daemon too. The token leaves the environment before the service question, so no later
|
|
842
|
+
# command inherits it.
|
|
416
843
|
connect_with_device_token() {
|
|
417
844
|
export DEVICE_AGENT_TOKEN
|
|
418
845
|
say ""
|
|
419
846
|
status=0
|
|
420
847
|
if [ -n "${PINGGY_MANAGE:-}" ]; then
|
|
421
|
-
"$
|
|
848
|
+
"$pinggy_path" device connect -b --manage "$PINGGY_MANAGE" </dev/null || status=$?
|
|
422
849
|
else
|
|
423
|
-
"$
|
|
850
|
+
"$pinggy_path" device connect -b </dev/null || status=$?
|
|
424
851
|
fi
|
|
425
852
|
unset DEVICE_AGENT_TOKEN
|
|
426
853
|
if [ "$status" -ne 0 ]; then
|
|
@@ -430,7 +857,7 @@ connect_with_device_token() {
|
|
|
430
857
|
exit 0
|
|
431
858
|
}
|
|
432
859
|
|
|
433
|
-
# Runs
|
|
860
|
+
# Runs $pinggy_path by its full path, as connect_with_device_token does. -b as above.
|
|
434
861
|
start_sign_in() {
|
|
435
862
|
if [ -n "${PINGGY_MANAGE:-}" ]; then
|
|
436
863
|
login_command="pinggy device login --manage $PINGGY_MANAGE"
|
|
@@ -452,9 +879,9 @@ start_sign_in() {
|
|
|
452
879
|
|
|
453
880
|
status=0
|
|
454
881
|
if [ -n "${PINGGY_MANAGE:-}" ]; then
|
|
455
|
-
"$
|
|
882
|
+
"$pinggy_path" device login -b --manage "$PINGGY_MANAGE" </dev/tty || status=$?
|
|
456
883
|
else
|
|
457
|
-
"$
|
|
884
|
+
"$pinggy_path" device login -b </dev/tty || status=$?
|
|
458
885
|
fi
|
|
459
886
|
if [ "$status" -ne 0 ]; then
|
|
460
887
|
exit "$status"
|
|
@@ -462,19 +889,27 @@ start_sign_in() {
|
|
|
462
889
|
offer_login_service
|
|
463
890
|
}
|
|
464
891
|
|
|
892
|
+
is_install_only() {
|
|
893
|
+
[ "${PINGGY_INSTALL_ONLY:-}" = 1 ]
|
|
894
|
+
}
|
|
895
|
+
|
|
465
896
|
main() {
|
|
466
897
|
detect_os
|
|
467
|
-
if [ "$
|
|
468
|
-
|
|
898
|
+
if [ -n "${PINGGY_BINARY_URL:-}" ]; then
|
|
899
|
+
install_with_binary
|
|
900
|
+
else
|
|
901
|
+
find_pinggy_command
|
|
902
|
+
case "$pinggy_kind" in
|
|
903
|
+
npm) update_npm_copy ;;
|
|
904
|
+
other_binary) offer_binary_over_other_copy ;;
|
|
905
|
+
other_script) fail_unknown_copy ;;
|
|
906
|
+
*) install_with_binary ;;
|
|
907
|
+
esac
|
|
469
908
|
fi
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
esac
|
|
475
|
-
install_or_update
|
|
476
|
-
warn_auto_update_replaces_it
|
|
477
|
-
link_into_path
|
|
909
|
+
if is_install_only; then
|
|
910
|
+
return 0
|
|
911
|
+
fi
|
|
912
|
+
offer_daemon_restart
|
|
478
913
|
if has_device_token; then
|
|
479
914
|
connect_with_device_token
|
|
480
915
|
fi
|