dsh-mobile-plugin 0.5.96 → 0.5.97
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/README.md +37 -0
- package/config/agent-presets/standard/skills/dsh-mobile-kb/SKILL.md +3 -1
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/cloudflare.md +7 -6
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/jdxb.md +52 -0
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/network-guide.md +2 -1
- package/config/agent-presets/standard/skills/dsh-mobile-kb/references/zeronews.md +56 -0
- package/lib/cloudflared/runner.cjs +110 -33
- package/lib/cloudflared/wiring.cjs +17 -1
- package/lib/impl.js +23 -4
- package/lib/jdxb/probe.cjs +165 -0
- package/lib/jdxb/provision.cjs +218 -0
- package/lib/panel/index.html +26 -46
- package/lib/relay/config-store.js +64 -5
- package/lib/relay/control.js +51 -3
- package/lib/relay/core.js +21 -0
- package/lib/relay/lan-proxy.js +50 -14
- package/lib/relay/public-ip.cjs +82 -0
- package/lib/remote/host-control.cjs +19 -1
- package/lib/routes/control-plane.js +225 -23
- package/lib/routes/mobile-api.js +22 -0
- package/lib/update-install.cjs +3 -2
- package/lib/zeronews/probe.cjs +126 -0
- package/lib/zeronews/provision.cjs +128 -0
- package/lib/zeronews/runner.cjs +303 -0
- package/lib/zeronews/watcher.cjs +78 -0
- package/lib/zeronews/wiring.cjs +159 -0
- package/package.json +1 -1
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// 读本机节点小宝设备端的状态与绑定链接,给面板「接入这条线路」用。
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 节点小宝(iepose)的设备端以系统服务方式装在本机,插件不启动、不管理它的进程 ——
|
|
6
|
+
* 它自己开机自启,死了也会自己重连。这里只做两件只读的事:问它「你装好没有、绑没绑」,
|
|
7
|
+
* 以及没绑时把它的绑定链接取出来交给面板。
|
|
8
|
+
*
|
|
9
|
+
* 为什么走宿主而不是面板直接 fetch:设备端本地管理口在回环上,面板在浏览器里跨域读它会被
|
|
10
|
+
* CORS 挡下;宿主是本机 node 进程,读回环没有跨域问题,而且这条路径本身是 localOnly 控制面。
|
|
11
|
+
*
|
|
12
|
+
* 端口是实测出来的、不是它对外的承诺:macOS 精简版 2080,Linux 安装脚本起在 9118。
|
|
13
|
+
* 两个都试,哪个先答话用哪个;都不答就是没装或没起。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** 设备端本地管理口:按平台列候选,顺序即偏好。 */
|
|
17
|
+
const LOCAL_PORTS = { darwin: [2080], linux: [9118] };
|
|
18
|
+
/** /detect/version 是轻量探针,超时短;/bind 要等它去云端换短链,实测 5~16 秒。 */
|
|
19
|
+
const PROBE_TIMEOUT_MS = 3000;
|
|
20
|
+
const BIND_TIMEOUT_MS = 30_000;
|
|
21
|
+
|
|
22
|
+
function portsFor(platform) {
|
|
23
|
+
const key = String(platform || process.platform);
|
|
24
|
+
const list = LOCAL_PORTS[key];
|
|
25
|
+
return Array.isArray(list) ? list : [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 问一个本地管理口要状态。返回 `{ ok, peerid, tun, workmode, error }`:
|
|
30
|
+
* `ok=false` 表示这个口没人应答(没装 / 没起 / 被别的程序占着)。
|
|
31
|
+
*/
|
|
32
|
+
async function probePort(port, options = {}) {
|
|
33
|
+
const doFetch = typeof options.fetchImpl === 'function'
|
|
34
|
+
? options.fetchImpl
|
|
35
|
+
: (typeof globalThis.fetch === 'function' ? globalThis.fetch : null);
|
|
36
|
+
if (!doFetch) return { ok: false, peerid: '', error: '当前环境没有可用的 fetch' };
|
|
37
|
+
const timeoutMs = Number(options.timeoutMs) > 0 ? Number(options.timeoutMs) : PROBE_TIMEOUT_MS;
|
|
38
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
|
39
|
+
const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
40
|
+
try {
|
|
41
|
+
const response = await doFetch(`http://127.0.0.1:${port}/detect/version`, {
|
|
42
|
+
headers: { accept: 'application/json' },
|
|
43
|
+
...(controller ? { signal: controller.signal } : {}),
|
|
44
|
+
});
|
|
45
|
+
if (!response || response.ok !== true) return { ok: false, peerid: '', error: `HTTP ${response && response.status ? response.status : '?'}` };
|
|
46
|
+
const payload = await response.json();
|
|
47
|
+
// 名字对不上说明这个口是别的程序,不能因为「有个东西在 2080 上答话」就认它是节点小宝。
|
|
48
|
+
if (!payload || payload.name !== 'NodeBabyLinkService') return { ok: false, peerid: '', error: '端口上不是节点小宝' };
|
|
49
|
+
return {
|
|
50
|
+
ok: true,
|
|
51
|
+
peerid: typeof payload.peerid === 'string' ? payload.peerid : '',
|
|
52
|
+
tun: typeof payload.tun === 'string' ? payload.tun : '',
|
|
53
|
+
workmode: typeof payload.workmode === 'string' ? payload.workmode : '',
|
|
54
|
+
error: '',
|
|
55
|
+
};
|
|
56
|
+
} catch (error) {
|
|
57
|
+
const raw = error && error.message ? error.message : String(error);
|
|
58
|
+
return { ok: false, peerid: '', error: raw };
|
|
59
|
+
} finally {
|
|
60
|
+
if (timer) clearTimeout(timer);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 读本机设备端状态。
|
|
66
|
+
*
|
|
67
|
+
* @returns {Promise<{ supported: boolean, installed: boolean, bound: boolean, peerid: string,
|
|
68
|
+
* tun: string, port: number, error: string }>}
|
|
69
|
+
* `supported=false` 是这一版没接的平台(Windows 设备端没验过本地口,不乱猜);
|
|
70
|
+
* `installed=false` 表示本机找不到设备端;`bound=false` 表示装了但还没登录绑定。
|
|
71
|
+
*/
|
|
72
|
+
async function readStatus(options = {}) {
|
|
73
|
+
const platform = String(options.platform || process.platform);
|
|
74
|
+
const ports = Array.isArray(options.ports) ? options.ports : portsFor(platform);
|
|
75
|
+
if (!ports.length) {
|
|
76
|
+
return { supported: false, installed: false, bound: false, peerid: '', tun: '', port: 0, error: '这一版只接了 macOS 与 Linux 的节点小宝设备端' };
|
|
77
|
+
}
|
|
78
|
+
let lastError = '';
|
|
79
|
+
for (const port of ports) {
|
|
80
|
+
const result = await probePort(port, options);
|
|
81
|
+
if (result.ok) {
|
|
82
|
+
return {
|
|
83
|
+
supported: true,
|
|
84
|
+
installed: true,
|
|
85
|
+
bound: result.peerid !== '',
|
|
86
|
+
peerid: result.peerid,
|
|
87
|
+
tun: result.tun,
|
|
88
|
+
port,
|
|
89
|
+
error: '',
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
lastError = result.error || lastError;
|
|
93
|
+
}
|
|
94
|
+
return { supported: true, installed: false, bound: false, peerid: '', tun: '', port: 0, error: lastError };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 取设备端的绑定链接。设备端回 302 指向它的云端绑定页,把那一段 Location 原样交给面板。
|
|
99
|
+
* 已经绑过的设备不会再给链接(它回 404 或直接给已绑页面),这里按 `ok:false` 如实说明。
|
|
100
|
+
*/
|
|
101
|
+
async function readBindUrl(options = {}) {
|
|
102
|
+
const doFetch = typeof options.fetchImpl === 'function'
|
|
103
|
+
? options.fetchImpl
|
|
104
|
+
: (typeof globalThis.fetch === 'function' ? globalThis.fetch : null);
|
|
105
|
+
if (!doFetch) return { ok: false, url: '', error: '当前环境没有可用的 fetch' };
|
|
106
|
+
const port = Number(options.port) > 0 ? Number(options.port) : (portsFor(options.platform)[0] || 0);
|
|
107
|
+
if (!port) return { ok: false, url: '', error: '这一版没接这个平台' };
|
|
108
|
+
const timeoutMs = Number(options.timeoutMs) > 0 ? Number(options.timeoutMs) : BIND_TIMEOUT_MS;
|
|
109
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
|
110
|
+
const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
111
|
+
try {
|
|
112
|
+
// 手动接管跳转:要的是那一跳的目标地址,不是跳过去之后的页面。
|
|
113
|
+
const response = await doFetch(`http://127.0.0.1:${port}/bind`, {
|
|
114
|
+
redirect: 'manual',
|
|
115
|
+
headers: { accept: 'text/html' },
|
|
116
|
+
...(controller ? { signal: controller.signal } : {}),
|
|
117
|
+
});
|
|
118
|
+
const location = response && response.headers && typeof response.headers.get === 'function'
|
|
119
|
+
? response.headers.get('location') : '';
|
|
120
|
+
if (typeof location === 'string' && /^https?:\/\//i.test(location.trim())) {
|
|
121
|
+
return { ok: true, url: location.trim(), error: '' };
|
|
122
|
+
}
|
|
123
|
+
return { ok: false, url: '', error: '设备端没有给出绑定链接(可能已经绑定过)' };
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (controller && controller.signal.aborted) {
|
|
126
|
+
return { ok: false, url: '', error: '向设备端要绑定链接超时,请确认它正在运行' };
|
|
127
|
+
}
|
|
128
|
+
const raw = error && error.message ? error.message : String(error);
|
|
129
|
+
return { ok: false, url: '', error: `连不上本机设备端:${raw}` };
|
|
130
|
+
} finally {
|
|
131
|
+
if (timer) clearTimeout(timer);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 把用户在面板里填的那一条归一成线路:`https://域名` 走 http 线路,`主机:端口` 走 tcp 线路。
|
|
137
|
+
*
|
|
138
|
+
* 两种都要认,因为节点小宝有「网页服务」和「TCP服务」两类;但对我们的 App 只有前者能用 ——
|
|
139
|
+
* 它的 TCP 网关会拦 HTTP 形状的流量(实测 GET/POST/HEAD/PUT/OPTIONS 全被换成一张拒绝页),
|
|
140
|
+
* 而 App 发的就是 HTTP。保留 tcp 形状是为了不把用户填的东西悄悄改掉。
|
|
141
|
+
*/
|
|
142
|
+
function lineOf(value) {
|
|
143
|
+
const raw = typeof value === 'string' ? value.trim().replace(/\/+$/, '') : '';
|
|
144
|
+
if (!raw) return null;
|
|
145
|
+
if (/^https?:\/\//i.test(raw)) {
|
|
146
|
+
try {
|
|
147
|
+
const url = new URL(raw);
|
|
148
|
+
if (!url.hostname) return null;
|
|
149
|
+
return { url: raw, transport: 'http' };
|
|
150
|
+
} catch {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const { tcpDirectTarget } = require('../relay/lan-network.cjs');
|
|
155
|
+
const target = tcpDirectTarget(raw);
|
|
156
|
+
return target ? { url: target, transport: 'tcp' } : null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
module.exports = {
|
|
160
|
+
LOCAL_PORTS,
|
|
161
|
+
lineOf,
|
|
162
|
+
probePort,
|
|
163
|
+
readStatus,
|
|
164
|
+
readBindUrl,
|
|
165
|
+
};
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// 节点小宝设备端的安装与启动:下载官方二进制、按平台装成系统服务、确保它在跑。
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 这一步跟 ZeroNews / cloudflared 最大的不同:那两个在自己目录里放个二进制就能跑,节点小宝要
|
|
6
|
+
* 装成**系统服务**——它的设备身份写在系统目录里,只有以 root 跑才存得住,否则每次重启都当新
|
|
7
|
+
* 设备(实测:普通用户跑,重启后 peerid 变空、线路全掉)。
|
|
8
|
+
*
|
|
9
|
+
* 所以这里绕不开一次管理员授权。插件能做的是把整件事自动化:自己下载、自己装、自己拉起,
|
|
10
|
+
* 用户只在系统弹出的授权框里确认一次(Touch ID / 输密码)。这是操作系统对「装系统服务」的
|
|
11
|
+
* 规矩,不是我们多要一步。
|
|
12
|
+
*
|
|
13
|
+
* 只做 macOS:它的 launchd 服务定义简单、我们也在这套上实测过。Linux 的 systemd 与 Windows
|
|
14
|
+
* 的服务注册各自还有一套没验过的细节,先如实报「这一版没接」,不猜着写。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const { execFile } = require('node:child_process');
|
|
18
|
+
const fs = require('node:fs');
|
|
19
|
+
const os = require('node:os');
|
|
20
|
+
const path = require('node:path');
|
|
21
|
+
|
|
22
|
+
const VERSION = '4.3.5';
|
|
23
|
+
const INSTALL_DIR = '/usr/local/nblink';
|
|
24
|
+
const BINARY = path.join(INSTALL_DIR, 'NodeBabyLinkService');
|
|
25
|
+
const PLIST = '/Library/LaunchDaemons/com.iepose.NodeBabyLinkService.plist';
|
|
26
|
+
const LOG_DIR = path.join(INSTALL_DIR, 'logs');
|
|
27
|
+
const CDN = 'https://cdn.ionewu.com/upgrade/jdxb';
|
|
28
|
+
const DOWNLOAD_TIMEOUT_MS = 120_000;
|
|
29
|
+
/** 授权框等人点:给足三分钟,但别无限挂着。 */
|
|
30
|
+
const ELEVATE_TIMEOUT_MS = 180_000;
|
|
31
|
+
/** 装完等它把本地管理口听上。 */
|
|
32
|
+
const START_WAIT_MS = 15_000;
|
|
33
|
+
|
|
34
|
+
function platformKey(platform, arch) {
|
|
35
|
+
const p = String(platform || process.platform);
|
|
36
|
+
const a = String(arch || process.arch);
|
|
37
|
+
if (p !== 'darwin') return '';
|
|
38
|
+
if (a === 'arm64') return 'darwin-arm64';
|
|
39
|
+
if (a === 'x64') return 'darwin-x64';
|
|
40
|
+
return '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function downloadUrl(key) {
|
|
44
|
+
const arch = key === 'darwin-arm64' ? 'arm64' : 'amd64';
|
|
45
|
+
return `${CDN}/NodeBabyLinkService.darwin.${arch}.${VERSION}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 一个 Mach-O 可执行文件的开头;下载坏了/被挡成网页时当场认出来,别把垃圾装进系统目录。
|
|
50
|
+
* 小端与大端、32/64 位、以及 fat 包都算 —— 官方给的是小端 64 位(首字节 CF FA ED FE),
|
|
51
|
+
* 只按大端认会把真文件拒掉。
|
|
52
|
+
*/
|
|
53
|
+
const MACHO_MAGICS = new Set([0xfeedface, 0xfeedfacf, 0xcefaedfe, 0xcffaedfe, 0xcafebabe, 0xbebafeca]);
|
|
54
|
+
function looksLikeMachO(buffer) {
|
|
55
|
+
if (!buffer || buffer.length < 4) return false;
|
|
56
|
+
return MACHO_MAGICS.has(buffer.readUInt32BE(0));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** `launchctl list <label>`:加载了就有输出,没加载退出码非 0。 */
|
|
60
|
+
function serviceLoaded(options = {}) {
|
|
61
|
+
const run = typeof options.run === 'function' ? options.run : defaultRun;
|
|
62
|
+
return run('launchctl', ['list', 'com.iepose.NodeBabyLinkService'], { timeoutMs: 5000 })
|
|
63
|
+
.then((result) => result.code === 0 && /\bcom\.iepose\.NodeBabyLinkService\b/.test(result.stdout))
|
|
64
|
+
.catch(() => false);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function defaultRun(file, args, opts = {}) {
|
|
68
|
+
return new Promise((resolve) => {
|
|
69
|
+
execFile(file, args, { timeout: opts.timeoutMs || 10_000, maxBuffer: 1 << 20 }, (err, stdout, stderr) => {
|
|
70
|
+
resolve({ code: err ? (typeof err.code === 'number' ? err.code : 1) : 0, stdout: String(stdout || ''), stderr: String(stderr || '') });
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 本机装没装:服务定义与二进制都在,才算装过(只装了一半的照实报没装)。 */
|
|
76
|
+
function installedSync() {
|
|
77
|
+
try {
|
|
78
|
+
return fs.existsSync(PLIST) && fs.existsSync(BINARY);
|
|
79
|
+
} catch {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function plistBody() {
|
|
85
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
86
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
87
|
+
<plist version="1.0">
|
|
88
|
+
<dict>
|
|
89
|
+
<key>Label</key>
|
|
90
|
+
<string>com.iepose.NodeBabyLinkService</string>
|
|
91
|
+
<key>ProgramArguments</key>
|
|
92
|
+
<array>
|
|
93
|
+
<string>${BINARY}</string>
|
|
94
|
+
</array>
|
|
95
|
+
<key>RunAtLoad</key>
|
|
96
|
+
<true/>
|
|
97
|
+
<key>KeepAlive</key>
|
|
98
|
+
<true/>
|
|
99
|
+
<key>StandardOutPath</key>
|
|
100
|
+
<string>${path.join(LOG_DIR, 'stdout.log')}</string>
|
|
101
|
+
<key>StandardErrorPath</key>
|
|
102
|
+
<string>${path.join(LOG_DIR, 'stderr.log')}</string>
|
|
103
|
+
</dict>
|
|
104
|
+
</plist>
|
|
105
|
+
`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 以管理员权限跑一段 shell。macOS 上就是系统自带的授权框(`do shell script ... with
|
|
110
|
+
* administrator privileges`);它会弹出 Touch ID / 密码,用户点一下确认,脚本才以 root 跑。
|
|
111
|
+
*
|
|
112
|
+
* 脚本先写到用户自己的临时目录(0700):同一个人能改的只有这一份,跨不过权限边界;写死命令
|
|
113
|
+
* 串反而要跟一堆引号打架,出错时也看不出哪一步断的。
|
|
114
|
+
*/
|
|
115
|
+
function runElevated(script, options = {}) {
|
|
116
|
+
const run = typeof options.run === 'function' ? options.run : defaultRun;
|
|
117
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-jdxb-'));
|
|
118
|
+
const file = path.join(dir, 'install.sh');
|
|
119
|
+
fs.writeFileSync(file, script, { mode: 0o700 });
|
|
120
|
+
// AppleScript 字符串里只需处理反斜杠与双引号;路径是我们自己造的,没有空格与怪字符。
|
|
121
|
+
const escaped = file.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
|
122
|
+
return run('osascript', ['-e', `do shell script "sh ${escaped}" with administrator privileges`], {
|
|
123
|
+
timeoutMs: options.timeoutMs || ELEVATE_TIMEOUT_MS,
|
|
124
|
+
}).finally(() => {
|
|
125
|
+
try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 装好并拉起来。
|
|
131
|
+
*
|
|
132
|
+
* @returns {Promise<{ ok: boolean, installed: boolean, started: boolean, error: string }>}
|
|
133
|
+
* `ok=false` 时 `error` 是人能看懂的原因(下载失败 / 用户取消授权 / 服务没起来)。
|
|
134
|
+
*/
|
|
135
|
+
async function install(options = {}) {
|
|
136
|
+
const key = options.platformKey || platformKey(options.platform, options.arch);
|
|
137
|
+
if (!key) return { ok: false, installed: installedSync(), started: false, error: '这一版只接了 macOS 的自动安装' };
|
|
138
|
+
if (installedSync() && await serviceLoaded(options)) {
|
|
139
|
+
return { ok: true, installed: true, started: true, error: '' };
|
|
140
|
+
}
|
|
141
|
+
const fetchImpl = typeof options.fetchImpl === 'function' ? options.fetchImpl : globalThis.fetch;
|
|
142
|
+
if (typeof fetchImpl !== 'function') return { ok: false, installed: installedSync(), started: false, error: '当前环境没有可用的 fetch' };
|
|
143
|
+
|
|
144
|
+
let temp = '';
|
|
145
|
+
try {
|
|
146
|
+
const response = await fetchImpl(downloadUrl(key), { redirect: 'follow' });
|
|
147
|
+
if (!response || response.ok !== true) {
|
|
148
|
+
return { ok: false, installed: installedSync(), started: false, error: `下载设备端失败(HTTP ${response && response.status ? response.status : '?'})` };
|
|
149
|
+
}
|
|
150
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
151
|
+
if (!looksLikeMachO(buffer)) {
|
|
152
|
+
return { ok: false, installed: installedSync(), started: false, error: '下载到的不是可执行文件,可能被网络拦截了' };
|
|
153
|
+
}
|
|
154
|
+
temp = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-jdxb-bin-')), 'NodeBabyLinkService');
|
|
155
|
+
fs.writeFileSync(temp, buffer, { mode: 0o700 });
|
|
156
|
+
} catch (error) {
|
|
157
|
+
return { ok: false, installed: installedSync(), started: false, error: `下载设备端失败:${error && error.message ? error.message : String(error)}` };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const script = [
|
|
161
|
+
'set -e',
|
|
162
|
+
`mkdir -p '${INSTALL_DIR}' '${LOG_DIR}'`,
|
|
163
|
+
`cp '${temp}' '${BINARY}'`,
|
|
164
|
+
`chmod 755 '${BINARY}'`,
|
|
165
|
+
`cat > '${PLIST}' <<'PLIST_EOF'`,
|
|
166
|
+
plistBody(),
|
|
167
|
+
'PLIST_EOF',
|
|
168
|
+
`chmod 644 '${PLIST}'`,
|
|
169
|
+
`launchctl unload '${PLIST}' 2>/dev/null || true`,
|
|
170
|
+
`launchctl load -w '${PLIST}'`,
|
|
171
|
+
].join('\n');
|
|
172
|
+
|
|
173
|
+
const elevated = await runElevated(script, options);
|
|
174
|
+
try { fs.rmSync(path.dirname(temp), { recursive: true, force: true }); } catch {}
|
|
175
|
+
if (elevated.code !== 0) {
|
|
176
|
+
const detail = String(elevated.stderr || '').trim().split('\n').filter(Boolean).slice(-1)[0] || '';
|
|
177
|
+
// 用户点了取消时 osascript 会回 -128;这时别报「安装失败」,说清是他取消了。
|
|
178
|
+
const cancelled = /-128|User canceled/i.test(`${elevated.stderr || ''}${elevated.stdout || ''}`);
|
|
179
|
+
return {
|
|
180
|
+
ok: false,
|
|
181
|
+
installed: installedSync(),
|
|
182
|
+
started: false,
|
|
183
|
+
error: cancelled ? '授权被取消,设备端没有安装' : `安装没成功${detail ? `:${detail}` : ''}`,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// 脚本以 root 跑完了就当装上了:文件在不在不该由这个进程猜二遍(它也无权读 root 区)。
|
|
188
|
+
// 真要用得起来看的是下面这条端口应答。
|
|
189
|
+
const started = await waitForPort(options);
|
|
190
|
+
return { ok: started, installed: true, started, error: started ? '' : '设备端装好了,但本地管理口没起来,请查看系统服务状态' };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 装完等管理口应答:`launchctl load` 返回不代表进程已经监听,别装完立刻报没起来。 */
|
|
194
|
+
async function waitForPort(options = {}) {
|
|
195
|
+
const probe = typeof options.probePort === 'function' ? options.probePort : require('./probe.cjs').probePort;
|
|
196
|
+
const port = Number(options.port) > 0 ? Number(options.port) : 2080;
|
|
197
|
+
const deadline = Date.now() + (Number(options.waitMs) > 0 ? Number(options.waitMs) : START_WAIT_MS);
|
|
198
|
+
while (Date.now() < deadline) {
|
|
199
|
+
const result = await probe(port, { timeoutMs: 1500 });
|
|
200
|
+
if (result && result.ok) return true;
|
|
201
|
+
if (typeof options.sleep === 'function') await options.sleep(500);
|
|
202
|
+
else await new Promise((resolve) => setTimeout(resolve, 500));
|
|
203
|
+
}
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
module.exports = {
|
|
208
|
+
VERSION,
|
|
209
|
+
INSTALL_DIR,
|
|
210
|
+
BINARY,
|
|
211
|
+
PLIST,
|
|
212
|
+
platformKey,
|
|
213
|
+
downloadUrl,
|
|
214
|
+
installedSync,
|
|
215
|
+
serviceLoaded,
|
|
216
|
+
install,
|
|
217
|
+
waitForPort,
|
|
218
|
+
};
|