ssh-net 26.11.3 → 26.11.66
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/CHANGELOG.md +16 -3
- package/README.md +13 -6
- package/libexec/cli.mjs +4 -2
- package/libexec/commands/install.mjs +74 -59
- package/libexec/commands/start.mjs +7 -1
- package/libexec/commands/status.mjs +45 -26
- package/libexec/commands/stop.mjs +6 -8
- package/libexec/commands/uninstall.mjs +9 -20
- package/libexec/commands/update.mjs +49 -21
- package/libexec/lib/conf.mjs +14 -3
- package/libexec/lib/config.mjs +3 -13
- package/libexec/lib/constants.mjs +3 -0
- package/libexec/lib/exec.mjs +2 -0
- package/libexec/lib/fsutil.mjs +29 -3
- package/libexec/lib/identity.mjs +16 -0
- package/libexec/lib/jobs.mjs +3 -7
- package/libexec/lib/launchd.mjs +11 -41
- package/libexec/lib/manage.mjs +95 -28
- package/libexec/lib/probe.mjs +19 -16
- package/libexec/lib/sshconfig.mjs +98 -17
- package/libexec/lib/systemd.mjs +214 -55
- package/libexec/sn-ipv6 +2 -2
- package/libexec/sn-route +84 -41
- package/libexec/ssh-net-link-core +0 -2
- package/package.json +1 -1
package/libexec/sn-route
CHANGED
|
@@ -10,14 +10,13 @@
|
|
|
10
10
|
# 1. RELAY_ON 不存在 → 失败(lan 直连;config 的 test -e 已先
|
|
11
11
|
# 短路,这层是双保险)
|
|
12
12
|
# 2. banner probe [::1]:10022 通 → 成功(旧回拨活着,PEER_ON 有无都先探
|
|
13
|
-
# ——软关 peer
|
|
14
|
-
# 本次走中继,保留已有会话;只有端口未开才继续报名
|
|
13
|
+
# ——软关 peer 后仍允许用已建立的回拨)
|
|
15
14
|
# 3. PEER_ON 不存在 → 失败,不报名(软关语义)
|
|
16
15
|
# 4. PROBE_FAIL 印记新鲜(15s) → 失败,不报名;SSH_NET_ROUTE_FORCE=1
|
|
17
16
|
# 仅供 start 的 peer 恢复路径跳过这层短路,其他层序与锁纪律不变
|
|
18
17
|
# 5. sn-ipv6 为空 → 失败,写印记
|
|
19
|
-
# 6.
|
|
20
|
-
#
|
|
18
|
+
# 6. 拿到 ENSURE 锁后再探。旧端口仍无 SSH 标识时,尝试结束持有它的 sshd 会话,
|
|
19
|
+
# 再经 relay 报名。其他连接只跟随探测。清理和等待回拨共用 6s 预算。
|
|
21
20
|
# 本次报名等到新回拨后,先让现存转发重连,再发一次桌面通知(peer_ready)
|
|
22
21
|
# probe 永远在印记前面:失败印记物理上不可能压住一个真活着的 peer。成功不落盘
|
|
23
22
|
# (成功缓存会在 core 硬重启后的僵尸窗口把连接挂死 banner 等待,OpenSSH 无
|
|
@@ -32,7 +31,7 @@ MV=${MV:-/bin/mv}
|
|
|
32
31
|
RM=${RM:-/bin/rm}
|
|
33
32
|
SLEEP=${SLEEP:-/bin/sleep}
|
|
34
33
|
LAUNCHCTL=${LAUNCHCTL:-/bin/launchctl}
|
|
35
|
-
|
|
34
|
+
LSOF=${LSOF:-/usr/sbin/lsof}
|
|
36
35
|
# edge 生产是 macOS lockf;同脚本在 Linux 跑时用 util-linux flock,接口差异
|
|
37
36
|
# 收在 lock_try,不引第三方锁包。
|
|
38
37
|
if [ -x /usr/bin/lockf ]; then
|
|
@@ -66,11 +65,18 @@ relay_alias=$3
|
|
|
66
65
|
relay_port=$4
|
|
67
66
|
peer_user=$5
|
|
68
67
|
|
|
69
|
-
#
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
#
|
|
73
|
-
|
|
68
|
+
# 本轮预算用 Bash 内建的 SECONDS 按整秒计时。跨进程读取的失败印记仍用 date epoch
|
|
69
|
+
deadline=$((SECONDS + DEADLINE))
|
|
70
|
+
|
|
71
|
+
# 每次刷新剩余整秒数,探测和查询监听者时直接用它限制等待时间
|
|
72
|
+
update_budget() {
|
|
73
|
+
remaining_seconds=$((deadline - SECONDS))
|
|
74
|
+
[ "$remaining_seconds" -ge 1 ]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
callback_enabled() {
|
|
78
|
+
[ -e "$STATE_DIR/RELAY_ON" ] && [ -e "$STATE_DIR/PEER_ON" ]
|
|
79
|
+
}
|
|
74
80
|
|
|
75
81
|
# 进度只在 stderr 是终端时输出:VS Code/Remote-SSH 的 stderr 是管道,scp、
|
|
76
82
|
# 脚本调用同样自动静默。恒返回 0,输出不影响判决
|
|
@@ -120,14 +126,11 @@ probe() (
|
|
|
120
126
|
exit 2
|
|
121
127
|
)
|
|
122
128
|
|
|
123
|
-
#
|
|
124
|
-
# 可保证时返回 3,不再启动可能越过预算的 probe;调用方据此收尾。
|
|
129
|
+
# Bash 3.2 的读取超时按整秒指定,预算不足一秒时用退出码 3 通知调用方收尾
|
|
125
130
|
probe_in_budget() {
|
|
126
|
-
local
|
|
127
|
-
|
|
128
|
-
[ "$
|
|
129
|
-
w=$PROBE_W
|
|
130
|
-
[ "$w" -le "$room" ] || w=$room
|
|
131
|
+
local w=$PROBE_W
|
|
132
|
+
update_budget || return 3
|
|
133
|
+
[ "$w" -le "$remaining_seconds" ] || w=$remaining_seconds
|
|
131
134
|
probe "$w"
|
|
132
135
|
}
|
|
133
136
|
|
|
@@ -150,13 +153,15 @@ fail_relay() {
|
|
|
150
153
|
exit 1
|
|
151
154
|
}
|
|
152
155
|
|
|
153
|
-
#
|
|
156
|
+
# 已通就复用。端口未开(1)或无标识(2)时返回探测结果,供持锁的报名者使用
|
|
154
157
|
check_existing_peer() {
|
|
158
|
+
local rc
|
|
155
159
|
probe_in_budget
|
|
156
|
-
|
|
160
|
+
rc=$?
|
|
161
|
+
case $rc in
|
|
157
162
|
0) exit 0 ;;
|
|
158
|
-
2) exit 1 ;;
|
|
159
163
|
3) fail_relay ;;
|
|
164
|
+
*) return "$rc" ;;
|
|
160
165
|
esac
|
|
161
166
|
}
|
|
162
167
|
|
|
@@ -175,10 +180,7 @@ if [ "$ROUTE_FORCE" != 1 ] && stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; t
|
|
|
175
180
|
fi
|
|
176
181
|
|
|
177
182
|
# ── 层 5:查询本机 v6
|
|
178
|
-
|
|
179
|
-
if [ "$room" -lt 1 ]; then
|
|
180
|
-
fail_relay
|
|
181
|
-
fi
|
|
183
|
+
update_budget || fail_relay
|
|
182
184
|
# sn-ipv6 只做毫秒级本地 route/ifconfig 查询,保留预算外例外的刻意取舍
|
|
183
185
|
v6=$("$BIN_DIR/sn-ipv6" 2>/dev/null)
|
|
184
186
|
if [ -z "$v6" ]; then
|
|
@@ -197,20 +199,60 @@ follower_tick=0
|
|
|
197
199
|
# macOS 系统 lockf 用 BSD flock 锁已打开的 fd;锁属于内核中的打开文件描述,
|
|
198
200
|
# 不由文件是否存在决定。所有进程保留同一个空锁文件(删路径会让后来者锁到
|
|
199
201
|
# 不同 inode),进程正常退出/kill -9/重启都自动释放,不需要陈旧判断和 rm
|
|
200
|
-
# 接管。报名
|
|
201
|
-
#
|
|
202
|
-
exec 9
|
|
202
|
+
# 接管。报名 SSH 会关闭额外的描述符;把空锁文件作为标准输入,它既能读到 EOF,
|
|
203
|
+
# 又能在父脚本被强杀后继续持锁。桌面通知单独关闭 fd9
|
|
204
|
+
exec 9<>"$lock"
|
|
203
205
|
|
|
204
|
-
#
|
|
205
|
-
|
|
206
|
+
# 只查本用户的 SSH 服务进程及指定的 IPv6 环回监听。查询失败时丢弃部分输出
|
|
207
|
+
clear_stalled_listener() {
|
|
208
|
+
local timeout=$1 holders pid query_seconds rc
|
|
209
|
+
update_budget || fail_relay
|
|
210
|
+
# SECONDS 只给整秒计数,预留可能已经过去的零头,慢查询不能占用整轮之外的时间
|
|
211
|
+
query_seconds=$((remaining_seconds - 1))
|
|
212
|
+
[ "$query_seconds" -gt 0 ] || fail_relay
|
|
213
|
+
holders=$("$timeout" -s KILL "$query_seconds" "$LSOF" -nP -a -u "$EUID" \
|
|
214
|
+
-c '/^sshd(-session)?$/' "-i6TCP@[::1]:$PORT" -sTCP:LISTEN -t 9>&- 2>/dev/null)
|
|
215
|
+
rc=$?
|
|
216
|
+
callback_enabled || exit 1
|
|
217
|
+
case $rc in
|
|
218
|
+
0) ;;
|
|
219
|
+
124|137) fail_relay ;;
|
|
220
|
+
*) holders='' ;;
|
|
221
|
+
esac
|
|
222
|
+
# 查进程期间可能已有健康回拨接管了端口。复用它,避免断开刚恢复的连接
|
|
223
|
+
check_existing_peer
|
|
224
|
+
[ "$?" = 2 ] || return 0
|
|
225
|
+
callback_enabled || exit 1
|
|
226
|
+
update_budget || fail_relay
|
|
227
|
+
while IFS= read -r pid; do
|
|
228
|
+
[[ $pid =~ ^[1-9][0-9]*$ ]] || continue
|
|
229
|
+
kill -TERM "$pid" 2>/dev/null || true
|
|
230
|
+
done <<< "$holders"
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
# 根据持锁后的探测结果报名。stdout 丢弃,stderr 留给失败时的终端提示
|
|
206
234
|
announce_now() {
|
|
235
|
+
local probe_result=$1 timeout search_path=${SSH_NET_TIMEOUT_PATH:-/usr/bin:/opt/homebrew/opt/coreutils/libexec/gnubin:/usr/local/opt/coreutils/libexec/gnubin}
|
|
236
|
+
# Bash 3.2 的 type -P 也可能返回不可执行的文件;不能因此改用不带时限的 SSH
|
|
237
|
+
timeout=$(PATH="$search_path" type -P timeout)
|
|
238
|
+
if [ ! -x "$timeout" ]; then
|
|
239
|
+
if [ "$ROUTE_FORCE" = 1 ] || [ -t 2 ]; then
|
|
240
|
+
printf '%s\n' '缺少可执行的 timeout,未发起回拨。请在这台 edge 上运行 brew install coreutils,再运行 ssh-net start peer' >&2
|
|
241
|
+
fi
|
|
242
|
+
fail_relay
|
|
243
|
+
fi
|
|
244
|
+
if [ "$probe_result" = 2 ]; then clear_stalled_listener "$timeout"; fi
|
|
245
|
+
callback_enabled || exit 1
|
|
246
|
+
update_budget || fail_relay
|
|
247
|
+
say "直连暂不可用,正在经中继请 $peer 连过来…"
|
|
207
248
|
ann_err="$STATE_DIR/ANNOUNCE.stderr.tmp.$$"
|
|
208
|
-
|
|
249
|
+
# 报名无需终端,独立进程组让超时信号同时送达 SSH 与 ProxyJump 子进程
|
|
250
|
+
"$timeout" -k 1 "$remaining_seconds" "$SSH" -J "$relay_alias" -p "$relay_port" \
|
|
209
251
|
-o HostKeyAlias="$peer" -o BatchMode=yes -o ConnectTimeout=5 \
|
|
210
252
|
-o ControlMaster=no -o ControlPath=none -o IdentitiesOnly=yes \
|
|
211
253
|
-o StrictHostKeyChecking=accept-new \
|
|
212
254
|
"$peer_user@127.0.0.1" "ssh-net _register $self $v6" \
|
|
213
|
-
|
|
255
|
+
<&9 >/dev/null 2>"$ann_err" &
|
|
214
256
|
ann_pid=$!
|
|
215
257
|
}
|
|
216
258
|
|
|
@@ -218,13 +260,14 @@ announce_now() {
|
|
|
218
260
|
# 失败收尾已经 wait,可逐行读出;read/say 都是内建,空文件仍给退出码。
|
|
219
261
|
show_announce_error() {
|
|
220
262
|
local rc=$1 line='' shown=
|
|
263
|
+
[ "$ROUTE_FORCE" = 1 ] || [ -t 2 ] || return 0
|
|
221
264
|
if [ -r "$ann_err" ]; then
|
|
222
265
|
while IFS= read -r line || [ -n "$line" ]; do
|
|
223
|
-
|
|
266
|
+
printf '报名失败:%s\n' "$line" >&2
|
|
224
267
|
shown=1
|
|
225
268
|
done <"$ann_err"
|
|
226
269
|
fi
|
|
227
|
-
[ -n "$shown" ] ||
|
|
270
|
+
[ -n "$shown" ] || printf '报名 ssh 失败(退出码 %s)\n' "$rc" >&2
|
|
228
271
|
}
|
|
229
272
|
|
|
230
273
|
# 回拨建立后的本地动作,仅由本次报名的 leader 在等到新回拨时执行一次:
|
|
@@ -234,9 +277,9 @@ show_announce_error() {
|
|
|
234
277
|
# 纯本地调用、恒 0、绝不拖选路。非 tty(VS Code/scp)也弹,正是非交互
|
|
235
278
|
# 场景唯一可见的反馈
|
|
236
279
|
peer_ready() {
|
|
237
|
-
if [
|
|
280
|
+
if [ -e "$STATE_DIR/RELAY_ON" ]; then
|
|
238
281
|
"$LAUNCHCTL" kill TERM \
|
|
239
|
-
"gui/$
|
|
282
|
+
"gui/$EUID/local.ssh-net-link-core-$peer" 2>/dev/null || true
|
|
240
283
|
fi
|
|
241
284
|
local msg="与 $peer 的直连已就绪"
|
|
242
285
|
if [ "$TERMINAL_NOTIFIER" != '' ]; then
|
|
@@ -270,14 +313,14 @@ trap 'cleanup; exit 1' TERM INT
|
|
|
270
313
|
if lock_try; then
|
|
271
314
|
[ -e "$STATE_DIR/RELAY_ON" ] || exit 1
|
|
272
315
|
check_existing_peer
|
|
316
|
+
locked_probe_rc=$?
|
|
273
317
|
if [ "$ROUTE_FORCE" != 1 ] && stamp_fresh "$STATE_DIR/PROBE_FAIL" "$FAIL_TTL"; then
|
|
274
318
|
say "直连未就绪,本次选择中继($peer)"
|
|
275
319
|
exit 1
|
|
276
320
|
fi
|
|
277
|
-
|
|
321
|
+
callback_enabled || exit 1
|
|
278
322
|
role=leader
|
|
279
|
-
|
|
280
|
-
announce_now
|
|
323
|
+
announce_now "$locked_probe_rc"
|
|
281
324
|
say "等待 $peer 连过来…"
|
|
282
325
|
fi
|
|
283
326
|
|
|
@@ -294,7 +337,8 @@ while :; do
|
|
|
294
337
|
# 不兜挂起——选路已经判成功,绝不能让收尾外部程序拖住 Match exec;
|
|
295
338
|
# peer_ready 内先通知旧转发退出,通知挂起也不会丢本次 link 秒切
|
|
296
339
|
if [ "$role" = leader ]; then
|
|
297
|
-
|
|
340
|
+
# 转发自己发起选路时,launchd 会随旧 SSH 一起清理同进程组的子进程
|
|
341
|
+
( trap - EXIT TERM INT; set -m; peer_ready & disown ) </dev/null >/dev/null 2>&1 9>&-
|
|
298
342
|
fi
|
|
299
343
|
say "直连已恢复"
|
|
300
344
|
exit 0
|
|
@@ -326,8 +370,7 @@ while :; do
|
|
|
326
370
|
fi
|
|
327
371
|
fi
|
|
328
372
|
# probe 后重算剩余整秒;不足一秒就不再睡,避免收尾越过预算
|
|
329
|
-
|
|
330
|
-
[ "$room" -ge 1 ] || break
|
|
373
|
+
update_budget || break
|
|
331
374
|
"$SLEEP" "$POLL" 2>/dev/null || true
|
|
332
375
|
done
|
|
333
376
|
|