ssh-net 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +81 -0
- package/libexec/cli.mjs +114 -0
- package/libexec/commands/checkin.mjs +86 -0
- package/libexec/commands/install.mjs +129 -0
- package/libexec/commands/restart.mjs +30 -0
- package/libexec/commands/start.mjs +46 -0
- package/libexec/commands/status.mjs +187 -0
- package/libexec/commands/stop.mjs +37 -0
- package/libexec/commands/uninstall.mjs +85 -0
- package/libexec/lib/config.mjs +227 -0
- package/libexec/lib/constants.mjs +6 -0
- package/libexec/lib/exec.mjs +67 -0
- package/libexec/lib/identity.mjs +34 -0
- package/libexec/lib/ipv6.mjs +39 -0
- package/libexec/lib/jobctl.mjs +40 -0
- package/libexec/lib/launchd.mjs +106 -0
- package/libexec/lib/links.mjs +61 -0
- package/libexec/lib/manage.mjs +108 -0
- package/libexec/lib/paths.mjs +76 -0
- package/libexec/lib/probe.mjs +116 -0
- package/libexec/lib/render.mjs +157 -0
- package/libexec/lib/state.mjs +76 -0
- package/libexec/lib/systemd.mjs +30 -0
- package/libexec/peer-probe +20 -0
- package/libexec/ssh-net-link-core +94 -0
- package/libexec/ssh-net-uplink-edge +97 -0
- package/libexec/ssh-net-uplink-relay +15 -0
- package/libexec/v6-address +59 -0
- package/package.json +38 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// 易变状态的全部 IO(design §3):state 目录只有 on 空标记与 edges/<别名>
|
|
2
|
+
// (最近上报的 v6)。没有心跳、时间戳、checkout。目录 0700、文件 0600,
|
|
3
|
+
// 写 v6 走 tmp+rename 防 checkin 与 uplink-edge 读到半截。
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {Object} StateStore
|
|
9
|
+
* @property {() => boolean} isOn
|
|
10
|
+
* @property {() => void} setOn
|
|
11
|
+
* @property {() => boolean} removeOn 删除前是否存在
|
|
12
|
+
* @property {(alias: string) => string | null} readEdgeV6
|
|
13
|
+
* @property {(alias: string, v6: string) => void} writeEdgeV6
|
|
14
|
+
* @property {() => void} removeAllEdges
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param {string} stateDir
|
|
19
|
+
* @returns {StateStore}
|
|
20
|
+
*/
|
|
21
|
+
export function createStateStore(stateDir) {
|
|
22
|
+
const edgesDir = path.join(stateDir, 'edges');
|
|
23
|
+
const onFile = path.join(stateDir, 'on');
|
|
24
|
+
|
|
25
|
+
const ensureState = () => {
|
|
26
|
+
fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
isOn() {
|
|
31
|
+
return fs.existsSync(onFile);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
setOn() {
|
|
35
|
+
ensureState();
|
|
36
|
+
// 'a':不截断、已存在不报错(touch 语义)
|
|
37
|
+
const fd = fs.openSync(onFile, 'a');
|
|
38
|
+
fs.closeSync(fd);
|
|
39
|
+
fs.chmodSync(onFile, 0o600);
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
removeOn() {
|
|
43
|
+
try {
|
|
44
|
+
fs.unlinkSync(onFile);
|
|
45
|
+
return true;
|
|
46
|
+
} catch (err) {
|
|
47
|
+
if (err && err.code === 'ENOENT') return false;
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
readEdgeV6(alias) {
|
|
53
|
+
try {
|
|
54
|
+
const v6 = fs.readFileSync(path.join(edgesDir, alias), 'utf8').trim();
|
|
55
|
+
return v6.length > 0 ? v6 : null;
|
|
56
|
+
} catch (err) {
|
|
57
|
+
if (err && err.code === 'ENOENT') return null;
|
|
58
|
+
throw err;
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
writeEdgeV6(alias, v6) {
|
|
63
|
+
ensureState();
|
|
64
|
+
fs.mkdirSync(edgesDir, { recursive: true, mode: 0o700 });
|
|
65
|
+
const final = path.join(edgesDir, alias);
|
|
66
|
+
const tmp = path.join(edgesDir, `.${alias}.tmp-${process.pid}`);
|
|
67
|
+
fs.writeFileSync(tmp, `${v6}\n`, { mode: 0o600 });
|
|
68
|
+
fs.renameSync(tmp, final);
|
|
69
|
+
fs.chmodSync(final, 0o600);
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
removeAllEdges() {
|
|
73
|
+
fs.rmSync(edgesDir, { recursive: true, force: true });
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// node 机 systemd 编排。node 登录为 root(migration 实测前提),直接
|
|
2
|
+
// systemctl、直接写 /etc/systemd/system,不包 sudo;非 root 调用由命令层拒绝。
|
|
3
|
+
// Restart=always/RestartSec/StartLimitIntervalSec 在渲染的 unit 里定死。
|
|
4
|
+
|
|
5
|
+
/** @param {import('./exec.mjs').Runner} runner @param {string} unit 含 .service */
|
|
6
|
+
const sc = (runner, args) => runner.run('systemctl', args);
|
|
7
|
+
|
|
8
|
+
export const daemonReload = (runner) => sc(runner, ['daemon-reload']);
|
|
9
|
+
export const enableUnit = (runner, unit) => sc(runner, ['enable', unit]);
|
|
10
|
+
export const disableUnit = (runner, unit) => sc(runner, ['disable', unit]);
|
|
11
|
+
export const startUnit = (runner, unit) => sc(runner, ['start', unit]);
|
|
12
|
+
export const stopUnit = (runner, unit) => sc(runner, ['stop', unit]);
|
|
13
|
+
export const restartUnit = (runner, unit) => sc(runner, ['restart', unit]);
|
|
14
|
+
|
|
15
|
+
/** @returns {string} active|inactive|failed|... */
|
|
16
|
+
export const isActive = (runner, unit) =>
|
|
17
|
+
sc(runner, ['is-active', unit]).stdout.trim();
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* unit 文件校验;systemd-analyze 不存在时返回 skipped=true(不阻塞)。
|
|
21
|
+
* @returns {{ok:boolean, skipped?:boolean, stdout:string, stderr:string}}
|
|
22
|
+
*/
|
|
23
|
+
export function verifyUnit(runner, unitPath) {
|
|
24
|
+
const r = runner.run('systemdAnalyze', ['verify', unitPath]);
|
|
25
|
+
if (r.error && r.error.code === 'ENOENT') {
|
|
26
|
+
return { ok: true, skipped: true, stdout: r.stdout, stderr: r.stderr };
|
|
27
|
+
}
|
|
28
|
+
// verify 对只告警也可能非零;这里把非零视为失败,命令层打印 stderr
|
|
29
|
+
return { ok: r.code === 0, stdout: r.stdout, stderr: r.stderr };
|
|
30
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# peer-probe:连 edge 环回 10022,2s 内必须读到来自隧道另一端 core sshd 的
|
|
3
|
+
# SSH 协议 banner。僵尸 -R 监听 TCP 能连上却永远不吐 banner,只探端口会被
|
|
4
|
+
# 骗死(design §2.2/§6.4,notes 有完整论证)。Match exec 与 link 的 8s
|
|
5
|
+
# 窗口共用本脚本;自身不产生任何 stdout/stderr。
|
|
6
|
+
#
|
|
7
|
+
# argv:无(常量见下;SSH_NET_PROBE_* 仅测试注入)
|
|
8
|
+
LC_ALL=C
|
|
9
|
+
export LC_ALL
|
|
10
|
+
NC=${NC:-/usr/bin/nc}
|
|
11
|
+
HOST=${SSH_NET_PROBE_HOST:-127.0.0.1}
|
|
12
|
+
PORT=${SSH_NET_PROBE_PORT:-10022}
|
|
13
|
+
GTIMEOUT=${SSH_NET_PROBE_G:-2} # mac nc:-G 连接超时
|
|
14
|
+
WTIMEOUT=${SSH_NET_PROBE_W:-2} # mac nc:-w 总体超时
|
|
15
|
+
|
|
16
|
+
line=$("$NC" -G "$GTIMEOUT" -w "$WTIMEOUT" "$HOST" "$PORT" 2>/dev/null | /usr/bin/head -c 80)
|
|
17
|
+
case "$line" in
|
|
18
|
+
SSH-*) exit 0 ;;
|
|
19
|
+
*) exit 1 ;;
|
|
20
|
+
esac
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# ssh-net-link-core:edge 上到 core 的常驻转发 ssh(-D SOCKS + -L VNC)。
|
|
3
|
+
# argv: <self 别名> <peer 别名> <转发参数…>
|
|
4
|
+
# 不做状态机:走 peer 还是 relay 由 ssh config Match(peer-probe)决定,
|
|
5
|
+
# 循环只做「自检开关 → 报名 → 给回拨窗口 → 起一条 ssh」(design §6.1)。
|
|
6
|
+
set -u
|
|
7
|
+
LC_ALL=C
|
|
8
|
+
export LC_ALL
|
|
9
|
+
SSH=${SSH:-/usr/bin/ssh}
|
|
10
|
+
STATE_DIR=${SSH_NET_STATE_DIR:-$HOME/.local/state/ssh-net}
|
|
11
|
+
BIN_DIR=${SSH_NET_BIN_DIR:-$HOME/.ssh/ssh-net/bin}
|
|
12
|
+
RELAY_TTL=${SSH_NET_RELAY_TTL:-600} # relay 态寿命,到期回循环重估
|
|
13
|
+
PEER_WINDOW=${SSH_NET_PEER_WINDOW:-8} # 回拨建立窗口(秒)
|
|
14
|
+
PROBE_INTERVAL=${SSH_NET_PROBE_INTERVAL:-1}
|
|
15
|
+
LOOP_SLEEP=${SSH_NET_LOOP_SLEEP:-5}
|
|
16
|
+
|
|
17
|
+
self=$1
|
|
18
|
+
peer=$2
|
|
19
|
+
shift 2
|
|
20
|
+
fwd=("$@")
|
|
21
|
+
|
|
22
|
+
on_file="$STATE_DIR/on"
|
|
23
|
+
ssh_pid=
|
|
24
|
+
timer_pid=
|
|
25
|
+
|
|
26
|
+
on_term() {
|
|
27
|
+
[ -n "$ssh_pid" ] && kill "$ssh_pid" 2>/dev/null
|
|
28
|
+
[ -n "$timer_pid" ] && kill "$timer_pid" 2>/dev/null
|
|
29
|
+
exit 0
|
|
30
|
+
}
|
|
31
|
+
trap on_term TERM INT
|
|
32
|
+
|
|
33
|
+
rounds=0
|
|
34
|
+
while :; do
|
|
35
|
+
if [ -n "${SSH_NET_MAX_ROUNDS:-}" ]; then
|
|
36
|
+
rounds=$((rounds + 1))
|
|
37
|
+
[ "$rounds" -gt "$SSH_NET_MAX_ROUNDS" ] && exit 0
|
|
38
|
+
fi
|
|
39
|
+
|
|
40
|
+
# stop 复活防线:on 没了就不再报名/重连
|
|
41
|
+
[ -e "$on_file" ] || exit 0
|
|
42
|
+
|
|
43
|
+
v6=$("$BIN_DIR/v6-address" 2>/dev/null)
|
|
44
|
+
|
|
45
|
+
# 报名无条件但走固定 relay 别名(peer 僵尸/冷启动都可达);失败不阻断
|
|
46
|
+
if [ -n "$v6" ]; then
|
|
47
|
+
"$SSH" -o BatchMode=yes -o ConnectTimeout=15 \
|
|
48
|
+
-o ServerAliveInterval=10 -o ServerAliveCountMax=2 \
|
|
49
|
+
-o ControlMaster=no -o ControlPath=none \
|
|
50
|
+
"$peer-via-relay" "ssh-net checkin $self $v6" >/dev/null 2>&1 || true
|
|
51
|
+
fi
|
|
52
|
+
|
|
53
|
+
# 给 core 回拨建立窗口:窗口内轮询 probe,命中即 peer;无 v6 不等
|
|
54
|
+
mode=relay
|
|
55
|
+
if [ -n "$v6" ]; then
|
|
56
|
+
ticks=$((PEER_WINDOW / PROBE_INTERVAL))
|
|
57
|
+
i=0
|
|
58
|
+
while [ "$i" -lt "$ticks" ]; do
|
|
59
|
+
if "$BIN_DIR/peer-probe" >/dev/null 2>&1; then
|
|
60
|
+
mode=peer
|
|
61
|
+
break
|
|
62
|
+
fi
|
|
63
|
+
/bin/sleep "$PROBE_INTERVAL"
|
|
64
|
+
i=$((i + 1))
|
|
65
|
+
done
|
|
66
|
+
fi
|
|
67
|
+
|
|
68
|
+
# 唯一一条转发 ssh(转发参数来自 argv;bash 3.2 空数组展开写法)
|
|
69
|
+
"$SSH" -N ${fwd[@]+"${fwd[@]}"} \
|
|
70
|
+
-o ExitOnForwardFailure=yes -o BatchMode=yes -o IdentitiesOnly=yes \
|
|
71
|
+
-o ControlMaster=no -o ControlPath=none \
|
|
72
|
+
-o ServerAliveInterval=35 -o ServerAliveCountMax=3 \
|
|
73
|
+
-o ConnectTimeout=15 \
|
|
74
|
+
"$peer" &
|
|
75
|
+
ssh_pid=$!
|
|
76
|
+
|
|
77
|
+
# relay 寿命:单次 sleep+kill,到点回循环重报重探;peer 不挂寿命
|
|
78
|
+
timer_pid=
|
|
79
|
+
if [ "$mode" = relay ]; then
|
|
80
|
+
(
|
|
81
|
+
trap 'kill "$sleep_pid" 2>/dev/null; exit 0' TERM INT
|
|
82
|
+
/bin/sleep "$RELAY_TTL" & sleep_pid=$!
|
|
83
|
+
wait "$sleep_pid"
|
|
84
|
+
kill "$ssh_pid" 2>/dev/null
|
|
85
|
+
) &
|
|
86
|
+
timer_pid=$!
|
|
87
|
+
fi
|
|
88
|
+
|
|
89
|
+
wait "$ssh_pid"
|
|
90
|
+
[ -n "$timer_pid" ] && kill "$timer_pid" 2>/dev/null
|
|
91
|
+
timer_pid=
|
|
92
|
+
ssh_pid=
|
|
93
|
+
/bin/sleep "$LOOP_SLEEP"
|
|
94
|
+
done
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# ssh-net-uplink-edge:core 回拨某台 edge 的 -R 10022 常驻隧道,自带失败计数
|
|
3
|
+
# 与 enable/disable 翻转(design §6.2)。argv: <edge 别名> <edge 登录用户>,
|
|
4
|
+
# v6 读 state/edges/<别名>。软链/plist 一生成一次长期保留,6 次连续快速失败
|
|
5
|
+
# 后 launchctl disable + exit 0 休眠;checkin 用 enable + kickstart 唤醒。
|
|
6
|
+
set -u
|
|
7
|
+
LC_ALL=C
|
|
8
|
+
export LC_ALL
|
|
9
|
+
SSH=${SSH:-/usr/bin/ssh}
|
|
10
|
+
LAUNCHCTL=${LAUNCHCTL:-/bin/launchctl}
|
|
11
|
+
ID=${ID:-/usr/bin/id}
|
|
12
|
+
STATE_DIR=${SSH_NET_STATE_DIR:-$HOME/.local/state/ssh-net}
|
|
13
|
+
CLEAR_TTL=${SSH_NET_CLEAR_TTL:-30} # 持续存活满 30s 清零失败计数
|
|
14
|
+
FAIL_LIMIT=${SSH_NET_FAIL_LIMIT:-6}
|
|
15
|
+
RETRY_SLEEP=${SSH_NET_RETRY_SLEEP:-30}
|
|
16
|
+
|
|
17
|
+
alias_name=$1
|
|
18
|
+
user=$2
|
|
19
|
+
label="local.ssh-net-uplink-edge-$alias_name"
|
|
20
|
+
state_file="$STATE_DIR/edges/$alias_name"
|
|
21
|
+
|
|
22
|
+
ssh_pid=
|
|
23
|
+
timer_pid=
|
|
24
|
+
|
|
25
|
+
# uninstall/stop 杀父进程时转给 ssh 子进程与清零定时器,不留孤儿
|
|
26
|
+
on_term() {
|
|
27
|
+
[ -n "$ssh_pid" ] && kill "$ssh_pid" 2>/dev/null
|
|
28
|
+
[ -n "$timer_pid" ] && kill "$timer_pid" 2>/dev/null
|
|
29
|
+
exit 0
|
|
30
|
+
}
|
|
31
|
+
trap on_term TERM INT
|
|
32
|
+
trap 'fails=0' USR1 # 活满 CLEAR_TTL 的清零信号
|
|
33
|
+
|
|
34
|
+
# 内层 trap 负责把未睡完的 sleep 一起收掉,否则 kill 子 shell 会留下孤儿 sleep
|
|
35
|
+
start_clear_timer() {
|
|
36
|
+
[ -n "$timer_pid" ] && kill "$timer_pid" 2>/dev/null # 先杀上一轮残留
|
|
37
|
+
(
|
|
38
|
+
trap 'kill "$sleep_pid" 2>/dev/null; exit 0' TERM
|
|
39
|
+
/bin/sleep "$CLEAR_TTL" & sleep_pid=$!
|
|
40
|
+
wait "$sleep_pid"
|
|
41
|
+
kill -USR1 "$$" 2>/dev/null
|
|
42
|
+
) &
|
|
43
|
+
timer_pid=$!
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
fails=0
|
|
47
|
+
rounds=0
|
|
48
|
+
while :; do
|
|
49
|
+
if [ -n "${SSH_NET_MAX_ROUNDS:-}" ]; then
|
|
50
|
+
rounds=$((rounds + 1))
|
|
51
|
+
[ "$rounds" -gt "$SSH_NET_MAX_ROUNDS" ] && exit 0
|
|
52
|
+
fi
|
|
53
|
+
|
|
54
|
+
v6=
|
|
55
|
+
if [ -r "$state_file" ]; then
|
|
56
|
+
# 纯 bash 内建消空白(launchd 最小环境,不依赖 PATH 里有 tr)
|
|
57
|
+
v6=$(<"$state_file")
|
|
58
|
+
v6=${v6//[[:space:]]/}
|
|
59
|
+
fi
|
|
60
|
+
# 文件缺失只可能是人工误删(休眠不删 state):disable 休眠等 checkin,
|
|
61
|
+
# 不空转拨号,也不删任何东西
|
|
62
|
+
if [ -z "$v6" ]; then
|
|
63
|
+
"$LAUNCHCTL" disable "gui/$("$ID" -u)/$label"
|
|
64
|
+
exit 0
|
|
65
|
+
fi
|
|
66
|
+
|
|
67
|
+
"$SSH" -N -R "10022:127.0.0.1:22" \
|
|
68
|
+
-i "$HOME/.ssh/id_ed25519" \
|
|
69
|
+
-o ExitOnForwardFailure=yes -o BatchMode=yes -o IdentitiesOnly=yes \
|
|
70
|
+
-o ControlMaster=no -o ControlPath=none \
|
|
71
|
+
-o ServerAliveInterval=35 -o ServerAliveCountMax=3 \
|
|
72
|
+
-o ConnectTimeout=15 \
|
|
73
|
+
-o StrictHostKeyChecking=yes \
|
|
74
|
+
-o UserKnownHostsFile="$HOME/.ssh/ssh-net/known-hosts" \
|
|
75
|
+
-o HostKeyAlias="$alias_name" \
|
|
76
|
+
"$user@$v6" &
|
|
77
|
+
ssh_pid=$!
|
|
78
|
+
start_clear_timer
|
|
79
|
+
|
|
80
|
+
# USR1 会中断 wait 内置(mac bash 3.2 返回非零):必须循环等到 ssh 真退出,
|
|
81
|
+
# 否则活连接被定时器打断后会误走下方 fails++
|
|
82
|
+
while kill -0 "$ssh_pid" 2>/dev/null; do
|
|
83
|
+
wait "$ssh_pid" 2>/dev/null
|
|
84
|
+
done
|
|
85
|
+
kill "$timer_pid" 2>/dev/null
|
|
86
|
+
timer_pid=
|
|
87
|
+
ssh_pid=
|
|
88
|
+
|
|
89
|
+
fails=$((fails + 1)) # 退出一律计数(合盖判死也算)
|
|
90
|
+
if [ "$fails" -ge "$FAIL_LIMIT" ]; then
|
|
91
|
+
# 当下零重拉靠本 exit 0(SuccessfulExit=false 只在非自愿退出时重拉);
|
|
92
|
+
# disable 管加载期拦截与跨登录持久(实测矩阵见 notes)
|
|
93
|
+
"$LAUNCHCTL" disable "gui/$("$ID" -u)/$label"
|
|
94
|
+
exit 0
|
|
95
|
+
fi
|
|
96
|
+
/bin/sleep "$RETRY_SLEEP"
|
|
97
|
+
done
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# ssh-net-uplink-relay:core/node 到 relay 的常驻 -R 注册隧道。
|
|
3
|
+
# argv: <s100 环回注册口> <relay 的 ssh 别名>
|
|
4
|
+
# 以 exec ssh 收尾——非常驻 bash、信号直达 ssh,trap 总则的唯一例外
|
|
5
|
+
# (design §2.3)。User/HostName 由 ssh config 的 Host 块提供。
|
|
6
|
+
set -u
|
|
7
|
+
LC_ALL=C
|
|
8
|
+
export LC_ALL
|
|
9
|
+
SSH=${SSH:-/usr/bin/ssh}
|
|
10
|
+
|
|
11
|
+
exec "$SSH" -N -R "127.0.0.1:$1:127.0.0.1:22" \
|
|
12
|
+
-o ExitOnForwardFailure=yes -o BatchMode=yes -o IdentitiesOnly=yes \
|
|
13
|
+
-o ControlMaster=no -o ControlPath=none -o StrictHostKeyChecking=yes \
|
|
14
|
+
-o ServerAliveInterval=35 -o ServerAliveCountMax=3 -o ConnectTimeout=15 \
|
|
15
|
+
"$2"
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# v6-address:选取本机可注册的公网 v6——默认路由网卡上 2000::/3 全局单播、
|
|
3
|
+
# autoconf secured、非 deprecated/temporary 的第一个地址(design §6.4)。
|
|
4
|
+
# 无输出 = 没有可注册 v6(v4-only/咖啡厅场景),退出码恒 0。
|
|
5
|
+
# 除 route/ifconfig 不引外部命令;ROUTE/IFCONFIG/SSH_NET_DEFAULT_IF 仅测试注入。
|
|
6
|
+
set -u
|
|
7
|
+
LC_ALL=C
|
|
8
|
+
export LC_ALL
|
|
9
|
+
ROUTE=${ROUTE:-/sbin/route}
|
|
10
|
+
IFCONFIG=${IFCONFIG:-/sbin/ifconfig}
|
|
11
|
+
|
|
12
|
+
# 首 hextet ∈ [0x2000,0x3fff] 即 2000::/3。合法地址首段直接取首个冒号前,
|
|
13
|
+
# 不需展开 ::;拒内嵌点(v4/v4-mapped)与非十六进制。
|
|
14
|
+
in_global_unicast() {
|
|
15
|
+
local a=$1 g v
|
|
16
|
+
case "$a" in *.*) return 1 ;; esac
|
|
17
|
+
g=${a%%:*}
|
|
18
|
+
[ -n "$g" ] || return 1
|
|
19
|
+
case "$g" in *[!0-9a-fA-F]*) return 1 ;; esac
|
|
20
|
+
v=$((16#$g))
|
|
21
|
+
[ "$v" -ge $((16#2000)) ] && [ "$v" -le $((16#3fff)) ]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
# 默认路由网卡(route 输出 "interface: en0"),纯内建解析
|
|
25
|
+
ifname=${SSH_NET_DEFAULT_IF:-}
|
|
26
|
+
if [ -z "$ifname" ]; then
|
|
27
|
+
while read -r key val; do
|
|
28
|
+
if [ "$key" = "interface:" ]; then
|
|
29
|
+
ifname=$val
|
|
30
|
+
break
|
|
31
|
+
fi
|
|
32
|
+
done < <("$ROUTE" -n get -inet6 default 2>/dev/null)
|
|
33
|
+
fi
|
|
34
|
+
[ -n "$ifname" ] || exit 0
|
|
35
|
+
|
|
36
|
+
# 只扫目标网卡段:行首无空白的 "en0: flags=.." 切段,段内空白起头的 inet6 行
|
|
37
|
+
inseg=
|
|
38
|
+
while IFS= read -r line; do
|
|
39
|
+
case "$line" in
|
|
40
|
+
[A-Za-z0-9]*:*)
|
|
41
|
+
seg=${line%%:*}
|
|
42
|
+
if [ "$seg" = "$ifname" ]; then inseg=1; else inseg=; fi
|
|
43
|
+
continue
|
|
44
|
+
;;
|
|
45
|
+
esac
|
|
46
|
+
[ -n "$inseg" ] || continue
|
|
47
|
+
set -- $line
|
|
48
|
+
[ "${1:-}" = "inet6" ] || continue
|
|
49
|
+
# link-local 只有 secured 无 autoconf,故两标记必须同时在
|
|
50
|
+
case "$line" in *autoconf*secured*) ;; *) continue ;; esac
|
|
51
|
+
case "$line" in *deprecated*|*temporary*) continue ;; esac
|
|
52
|
+
addr=${2%%%*} # 去可能的 scope
|
|
53
|
+
if in_global_unicast "$addr"; then
|
|
54
|
+
printf '%s\n' "$addr"
|
|
55
|
+
exit 0
|
|
56
|
+
fi
|
|
57
|
+
done < <("$IFCONFIG" "$ifname" 2>/dev/null)
|
|
58
|
+
|
|
59
|
+
exit 0
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ssh-net",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "个人 SSH 组网:v6 直连优先、relay 兜底、关机有界重试后静默",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ssh-net": "libexec/cli.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"libexec",
|
|
11
|
+
"CHANGELOG.md"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node --test",
|
|
15
|
+
"prepublishOnly": "npm test"
|
|
16
|
+
},
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=24"
|
|
19
|
+
},
|
|
20
|
+
"os": [
|
|
21
|
+
"darwin",
|
|
22
|
+
"linux"
|
|
23
|
+
],
|
|
24
|
+
"keywords": [
|
|
25
|
+
"ssh",
|
|
26
|
+
"tunnel",
|
|
27
|
+
"launchd",
|
|
28
|
+
"systemd",
|
|
29
|
+
"ipv6",
|
|
30
|
+
"reverse-proxy"
|
|
31
|
+
],
|
|
32
|
+
"author": "Aex",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/adaex/ssh-net.git"
|
|
37
|
+
}
|
|
38
|
+
}
|