dsh-mobile-plugin 0.5.112 → 0.5.113
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/config/agent-presets/standard/skills/dsh-mobile-kb/references/jdxb.md +8 -3
- package/lib/ai-diagnosis.js +198 -0
- package/lib/client.js +11 -1
- package/lib/jdxb/probe.cjs +79 -39
- package/lib/jdxb/provision.cjs +138 -47
- package/lib/mobile/extension-catalog.js +6 -7
- package/lib/mobile/extension-dev.js +6 -7
- package/lib/mobile/feedback-watch.cjs +106 -0
- package/lib/mobile/feedback.js +26 -7
- package/lib/mobile/host-settings.js +57 -0
- package/lib/mobile/pack-directory.js +26 -0
- package/lib/panel/index.html +94 -13
- package/lib/remote/controller.cjs +4 -0
- package/lib/remote/local-port.cjs +21 -0
- package/lib/remote/local-upstream.cjs +3 -1
- package/lib/remote/local.cjs +29 -12
- package/lib/routes/control-plane.js +22 -3
- package/package.json +14 -13
|
@@ -15,7 +15,9 @@
|
|
|
15
15
|
|
|
16
16
|
面板那页写着这四步,回答时按同样的顺序问:
|
|
17
17
|
|
|
18
|
-
1.
|
|
18
|
+
1. **一键安装设备端**:macOS 上插件下载官方二进制、装成系统服务、自己拉起;Windows 跑官方 PowerShell
|
|
19
|
+
一键安装脚本,Linux 跑官方 shell 一键安装脚本。会弹一次系统授权框(macOS 的 Touch ID / 密码、
|
|
20
|
+
Windows 的 UAC、Linux 的 polkit 框),让用户确认一下。装好后开机自启,重启 DSH、插件更新都不影响它。
|
|
19
21
|
2. **绑定账号**:点「取绑定链接」,在打开的页面里登录并绑定。面板每 3 秒问一次设备端,绑上之后链接自动收掉——看到链接消失就是成功了,不用再手动确认。
|
|
20
22
|
3. **建一条「网页服务」**:在节点小宝控制台新建网页服务,内网地址是 `127.0.0.1` 加面板给的端口(默认 18787,以面板上显示的为准),外网域名随便填。
|
|
21
23
|
4. **填外网地址并保存**:把分到的 `https://xxx` 域名抄到面板里点「保存线路」。手机在下一轮心跳里看到这条线路,名字显示「节点小宝」。
|
|
@@ -29,8 +31,11 @@
|
|
|
29
31
|
## 几个容易卡住的地方
|
|
30
32
|
|
|
31
33
|
- **必须装成系统服务,不能改成普通进程跑**:设备身份写在系统目录里、要以 root 跑才存得住。实测普通用户跑,重启后 peerid 变空、线路全掉。所以那一次管理员授权省不掉,这是系统对装服务的规矩。
|
|
32
|
-
-
|
|
33
|
-
|
|
34
|
+
- **平台**:自动安装接了 macOS、Windows x64 与 Linux x64/arm64;Windows 只接 64 位。没有图形授权
|
|
35
|
+
组件的 Linux 服务器 / 容器弹不出 polkit 框,会如实报错,这种情况只能人工到机器上装。
|
|
36
|
+
- **本地管理口**:macOS 是 2080、Linux 是 9118、Windows 先问虚拟回环 `127.66.36.18:2080` 再退回
|
|
37
|
+
`127.0.0.1:2080`,都在本机回环,插件靠它判断装没装、绑没绑。端口没应答就是没装、没起,或被
|
|
38
|
+
别的程序占着。
|
|
34
39
|
- **端口别记死**:网页服务的内网端口要按面板给的来。插件监听的口改过之后,记得回控制台把网页服务的端口一起改。
|
|
35
40
|
- **取不到绑定链接**:已经绑过的设备不再给链接,这是正常现象;超时或连不上,先确认设备端在跑。
|
|
36
41
|
- **保存后手机没看到**:地址要下一轮心跳才到手机,让手机切回前台或重连一次;改过地址要重新保存过一次才算。
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// 2026-11-24:本机面板全局悬浮 AI 问诊按钮的取选项与建会话发首句,只走 localOnly 控制面。
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 本机面板的 AI 问诊入口(面板里那颗全局悬浮按钮用)。
|
|
6
|
+
*
|
|
7
|
+
* 只挂在 /api/relay/* 的 localOnly 路由上:工作区与模型目录都读这台电脑上宿主 apiProxy,
|
|
8
|
+
* 会话也只经宿主建、宿主发。不收移动端 token,不碰 shell,不接受任意路径;对外形状在这里
|
|
9
|
+
* 收敛成小对象,任一路 RPC 失败或回执形状异常都回 { ok:false, error },不把异常抛给路由层。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
|
|
14
|
+
const require = createRequire(import.meta.url);
|
|
15
|
+
const { unwrap } = require('./mobile/envelope.js');
|
|
16
|
+
const { callSessions } = require('./mobile/rpc.js');
|
|
17
|
+
|
|
18
|
+
const MAX_WORKSPACES = 100;
|
|
19
|
+
const MAX_MODEL_GROUPS = 16;
|
|
20
|
+
const MAX_MODELS_PER_GROUP = 40;
|
|
21
|
+
const MAX_PROMPT_CHARS = 20000;
|
|
22
|
+
const MAX_OPTION_CHARS = 200;
|
|
23
|
+
const DEFAULT_WORKSPACE_DIR = 'default-workspace';
|
|
24
|
+
|
|
25
|
+
/** 抛出来的异常也归一成信封里那套 { code, message },不另起一套错误形状。 */
|
|
26
|
+
function errorOf(error) {
|
|
27
|
+
if (error && typeof error === 'object' && typeof error.code === 'string' && error.code) {
|
|
28
|
+
return {
|
|
29
|
+
code: error.code,
|
|
30
|
+
message: typeof error.message === 'string' && error.message ? error.message : error.code,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return { code: 'internal', message: String(error && error.message ? error.message : error) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function badRequest(message) {
|
|
37
|
+
return { ok: false, error: { code: 'bad-request', message } };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 与 app/src/drawerGroups.ts 的 isDefaultWorkspace 同一判据:标题或路径末段是那个目录名。 */
|
|
41
|
+
function isDefaultWorkspace(item) {
|
|
42
|
+
const title = typeof item.title === 'string' ? item.title.trim() : '';
|
|
43
|
+
if (title === DEFAULT_WORKSPACE_DIR) return true;
|
|
44
|
+
const raw = typeof item.path === 'string' ? item.path.trim().replace(/[/\\]+$/, '') : '';
|
|
45
|
+
return raw.split(/[\\/]/).pop() === DEFAULT_WORKSPACE_DIR;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 工作区只透四格;没有 workspaceId 的条目也留着,面板用 path 兜底。 */
|
|
49
|
+
function normalizeWorkspace(item) {
|
|
50
|
+
if (!item || typeof item !== 'object') return null;
|
|
51
|
+
return {
|
|
52
|
+
workspaceId: typeof item.workspaceId === 'string' ? item.workspaceId.trim() : '',
|
|
53
|
+
title: typeof item.title === 'string' ? item.title.trim() : '',
|
|
54
|
+
path: typeof item.path === 'string' ? item.path.trim() : '',
|
|
55
|
+
isDefault: isDefaultWorkspace(item),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 与 host-settings.js 的 normalizeModelGroup 同款精简,这里只要 id / name 两格。 */
|
|
60
|
+
function normalizeModelGroup(group) {
|
|
61
|
+
if (!group || typeof group !== 'object') return null;
|
|
62
|
+
const provider = typeof group.id === 'string' ? group.id.trim() : '';
|
|
63
|
+
if (!provider) return null;
|
|
64
|
+
const displayName = typeof group.name === 'string' && group.name.trim() ? group.name.trim() : provider;
|
|
65
|
+
const models = (Array.isArray(group.models) ? group.models : [])
|
|
66
|
+
.map((item) => {
|
|
67
|
+
if (!item || typeof item !== 'object') return null;
|
|
68
|
+
const id = typeof item.id === 'string' ? item.id.trim() : '';
|
|
69
|
+
if (!id) return null;
|
|
70
|
+
const name = typeof item.name === 'string' && item.name.trim() ? item.name.trim() : id;
|
|
71
|
+
return { id, name };
|
|
72
|
+
})
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.slice(0, MAX_MODELS_PER_GROUP);
|
|
75
|
+
if (models.length === 0) return null;
|
|
76
|
+
return { provider, displayName, models };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 全局默认模型:优先 defaultModel,老宿主只给 current;两格不齐就当没有。 */
|
|
80
|
+
function normalizeDefaultModel(value) {
|
|
81
|
+
const raw = value.defaultModel && typeof value.defaultModel === 'object'
|
|
82
|
+
? value.defaultModel
|
|
83
|
+
: (value.current && typeof value.current === 'object' ? value.current : null);
|
|
84
|
+
if (!raw) return null;
|
|
85
|
+
const provider = typeof raw.provider === 'string' ? raw.provider.trim() : '';
|
|
86
|
+
const model = typeof raw.model === 'string' ? raw.model.trim() : '';
|
|
87
|
+
return provider && model ? { provider, model } : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** GET 选项:宿主工作区清单 + 模型目录,任一路坏掉都如实回错误。 */
|
|
91
|
+
async function buildAiDiagnosisOptions(ctx) {
|
|
92
|
+
try {
|
|
93
|
+
const workspace = unwrap(await ctx.apiProxy.workspace.list({
|
|
94
|
+
rpcId: 'panel', method: 'workspace.list', payload: {},
|
|
95
|
+
}));
|
|
96
|
+
if (workspace.error) return { ok: false, error: workspace.error };
|
|
97
|
+
const items = Array.isArray(workspace.value) ? workspace.value
|
|
98
|
+
: (workspace.value && Array.isArray(workspace.value.items) ? workspace.value.items : null);
|
|
99
|
+
if (!items) {
|
|
100
|
+
return {
|
|
101
|
+
ok: false,
|
|
102
|
+
error: { code: 'host-workspace-list-degraded', message: '宿主工作区清单形状异常:响应里没有 items 数组' },
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// 默认工作区排在前面(同 isDefault 的保持宿主原序),再截上限:默认项不该被 100 条上限截掉。
|
|
106
|
+
const rows = items.map(normalizeWorkspace).filter(Boolean);
|
|
107
|
+
const workspaces = [...rows.filter((row) => row.isDefault), ...rows.filter((row) => !row.isDefault)]
|
|
108
|
+
.slice(0, MAX_WORKSPACES);
|
|
109
|
+
|
|
110
|
+
const models = unwrap(await ctx.apiProxy.llm.models({
|
|
111
|
+
rpcId: 'panel', method: 'llm.models', payload: {},
|
|
112
|
+
}));
|
|
113
|
+
if (models.error) return { ok: false, error: models.error };
|
|
114
|
+
const value = models.value;
|
|
115
|
+
if (!value || typeof value !== 'object' || !Array.isArray(value.groups)) {
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
error: { code: 'host-model-list-degraded', message: '宿主模型目录形状异常:响应里没有 groups 数组' },
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
ok: true,
|
|
123
|
+
workspaces,
|
|
124
|
+
models: value.groups.map(normalizeModelGroup).filter(Boolean).slice(0, MAX_MODEL_GROUPS),
|
|
125
|
+
defaultModel: normalizeDefaultModel(value),
|
|
126
|
+
};
|
|
127
|
+
} catch (error) {
|
|
128
|
+
return { ok: false, error: errorOf(error) };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 可选字段:空串当没带,类型不对或超长回 null 让调用方拒绝。 */
|
|
133
|
+
function optionalText(body, key) {
|
|
134
|
+
const raw = body && body[key];
|
|
135
|
+
if (raw === undefined || raw === null || raw === '') return '';
|
|
136
|
+
if (typeof raw !== 'string') return null;
|
|
137
|
+
const text = raw.trim();
|
|
138
|
+
return text.length <= MAX_OPTION_CHARS ? text : null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 发第一句失败时收掉刚建的会话(宿主侧的归档动词叫 workspace.archiveSession,
|
|
143
|
+
* 见 lib/mobile/workspaces.js 与 host-bridge/endpoints.js;没有 session.archive 这个方法)。
|
|
144
|
+
* 归档是尽力而为:它失败也不能盖掉本次发消息的原始错误。
|
|
145
|
+
*/
|
|
146
|
+
async function archiveQuietly(ctx, sessionId) {
|
|
147
|
+
try {
|
|
148
|
+
await ctx.apiProxy.workspace.archiveSession({
|
|
149
|
+
rpcId: 'panel', method: 'workspace.archiveSession', payload: { sessionId },
|
|
150
|
+
});
|
|
151
|
+
} catch { /* 原始失败才是要报给面板的那一个 */ }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** POST 建会话 + 带模型发送第一句;成功回 sessionId,失败回错误且不抛。 */
|
|
155
|
+
async function launchAiDiagnosis(ctx, body) {
|
|
156
|
+
try {
|
|
157
|
+
const prompt = body && typeof body.prompt === 'string' ? body.prompt : '';
|
|
158
|
+
if (!prompt.trim() || prompt.length > MAX_PROMPT_CHARS) {
|
|
159
|
+
return badRequest(prompt.length > MAX_PROMPT_CHARS
|
|
160
|
+
? `prompt 不能超过 ${MAX_PROMPT_CHARS} 字符`
|
|
161
|
+
: '需要非空的 prompt');
|
|
162
|
+
}
|
|
163
|
+
const workspaceId = optionalText(body, 'workspaceId');
|
|
164
|
+
const provider = optionalText(body, 'provider');
|
|
165
|
+
const model = optionalText(body, 'model');
|
|
166
|
+
if (workspaceId === null || provider === null || model === null) {
|
|
167
|
+
return badRequest(`workspaceId / provider / model 必须是不超过 ${MAX_OPTION_CHARS} 字符的字符串`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const created = unwrap(await callSessions(ctx, 'create', { ...(workspaceId ? { workspaceId } : {}) }));
|
|
171
|
+
if (created.error) return { ok: false, error: created.error };
|
|
172
|
+
const sessionId = created.value && typeof created.value.sessionId === 'string' ? created.value.sessionId : '';
|
|
173
|
+
if (!sessionId) return { ok: false, error: { code: 'internal', message: '新建会话未返回 sessionId' } };
|
|
174
|
+
|
|
175
|
+
const payload = {
|
|
176
|
+
sessionId,
|
|
177
|
+
content: [{ type: 'text', text: prompt }],
|
|
178
|
+
// 宿主 session.prompt 支持本次发送直接带模型,省掉「先建再 select」两步。
|
|
179
|
+
...(provider && model ? { modelSelection: { provider, model } } : {}),
|
|
180
|
+
};
|
|
181
|
+
let sent;
|
|
182
|
+
try {
|
|
183
|
+
sent = unwrap(await callSessions(ctx, 'prompt', payload, 'panel-ai-diagnosis'));
|
|
184
|
+
} catch (error) {
|
|
185
|
+
await archiveQuietly(ctx, sessionId);
|
|
186
|
+
return { ok: false, error: errorOf(error) };
|
|
187
|
+
}
|
|
188
|
+
if (sent.error) {
|
|
189
|
+
await archiveQuietly(ctx, sessionId);
|
|
190
|
+
return { ok: false, error: sent.error };
|
|
191
|
+
}
|
|
192
|
+
return { ok: true, sessionId };
|
|
193
|
+
} catch (error) {
|
|
194
|
+
return { ok: false, error: errorOf(error) };
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export { buildAiDiagnosisOptions, launchAiDiagnosis };
|
package/lib/client.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// 用途:桌面端入口与诊断反馈;打开配置页失败附带稳定原因码,不外发原始错误和访问凭据。
|
|
2
2
|
// 2026-11-24:两种诊断页都把「完整重启 DSH」摆第一条;打开失败的那一屏补上版本/来源/激活时间/shellChanged。
|
|
3
|
+
// 2026-11-24:就地反馈附件补 plugin_source / activated_at / shell_changed,便于判断装完没重启。
|
|
3
4
|
/**
|
|
4
5
|
* 移动端插件提供电脑插件页面目录;标准页面保留宿主上下文并支持直接导航。
|
|
5
6
|
* 聊天嵌入服务合并顶部栏显示声明,随提供方与消费方生命周期释放。
|
|
@@ -223,7 +224,7 @@ window.__ModuleLoader__.load({
|
|
|
223
224
|
* 就地反馈附件的诊断文本:只放插件自己认得的固定字段。原始错误串、面板地址与 UA
|
|
224
225
|
* 只留在「复制诊断信息」那份给人看的全文里,不随附件出门。
|
|
225
226
|
*/
|
|
226
|
-
function feedbackDiagnosisText({ version, stage, code, scope, error_code }) {
|
|
227
|
+
function feedbackDiagnosisText({ version, stage, code, scope, error_code, source, activatedAt, shellChanged }) {
|
|
227
228
|
return [
|
|
228
229
|
'dsh-mobile-plugin feedback',
|
|
229
230
|
`source=plugin`,
|
|
@@ -233,6 +234,9 @@ window.__ModuleLoader__.load({
|
|
|
233
234
|
`code=${code === undefined || code === null ? '' : String(code)}`,
|
|
234
235
|
`scope=${scope || ''}`,
|
|
235
236
|
...(error_code ? [`error_code=${error_code}`] : []),
|
|
237
|
+
...(source ? [`plugin_source=${source}`] : []),
|
|
238
|
+
...(activatedAt ? [`activated_at=${activatedAt}`] : []),
|
|
239
|
+
...(shellChanged ? [`shell_changed=${shellChanged}`] : []),
|
|
236
240
|
].join('\n');
|
|
237
241
|
}
|
|
238
242
|
|
|
@@ -2129,6 +2133,9 @@ window.__ModuleLoader__.load({
|
|
|
2129
2133
|
code: current && Number.isFinite(current.status) ? String(current.status) : '',
|
|
2130
2134
|
scope: snapshot.scope,
|
|
2131
2135
|
error_code: current && typeof current.code === 'string' && /^[A-Za-z][A-Za-z0-9._-]{0,79}$/.test(current.code) ? current.code : '',
|
|
2136
|
+
source: half && half.source ? String(half.source) : '',
|
|
2137
|
+
activatedAt: half && half.activatedAt ? String(half.activatedAt) : '',
|
|
2138
|
+
shellChanged: half && half.shellChanged ? 'true' : '',
|
|
2132
2139
|
};
|
|
2133
2140
|
const result = await submitIssueFeedback({
|
|
2134
2141
|
summary: `${title}\n${summaryOf()}`,
|
|
@@ -2451,6 +2458,9 @@ window.__ModuleLoader__.load({
|
|
|
2451
2458
|
code: current && Number.isFinite(current.status) ? String(current.status) : '',
|
|
2452
2459
|
scope: snapshot.scope,
|
|
2453
2460
|
error_code: current && typeof current.code === 'string' && /^[A-Za-z][A-Za-z0-9._-]{0,79}$/.test(current.code) ? current.code : '',
|
|
2461
|
+
source: half && half.source ? String(half.source) : '',
|
|
2462
|
+
activatedAt: half && half.activatedAt ? String(half.activatedAt) : '',
|
|
2463
|
+
shellChanged: half && half.shellChanged ? 'true' : '',
|
|
2454
2464
|
};
|
|
2455
2465
|
const result = await submitIssueFeedback({
|
|
2456
2466
|
summary: `${t('diagTitleOpen')}\n${current ? t('diagSummaryOpenFailed') : ''}`,
|
package/lib/jdxb/probe.cjs
CHANGED
|
@@ -9,20 +9,37 @@
|
|
|
9
9
|
* 为什么走宿主而不是面板直接 fetch:设备端本地管理口在回环上,面板在浏览器里跨域读它会被
|
|
10
10
|
* CORS 挡下;宿主是本机 node 进程,读回环没有跨域问题,而且这条路径本身是 localOnly 控制面。
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* 端口是实测 / 官方文档给的、不是它对外的承诺:macOS 2080、Linux 9118、Windows 在它自己的
|
|
13
|
+
* 虚拟回环 127.66.36.18:2080(官方文档给的绑定地址),也试本地回环 2080 兜底。哪个先答话
|
|
14
|
+
* 用哪个;都不答就是没装或没起。返回的 `service.installed` 以这次探测为准,面板据此决定
|
|
15
|
+
* 显示「一键安装」还是「启动」,不靠各平台的文件标记。
|
|
14
16
|
*/
|
|
15
17
|
|
|
16
18
|
/** 设备端本地管理口:按平台列候选,顺序即偏好。 */
|
|
17
|
-
const
|
|
19
|
+
const LOCAL_ENDPOINTS = {
|
|
20
|
+
darwin: [{ host: '127.0.0.1', port: 2080 }],
|
|
21
|
+
linux: [{ host: '127.0.0.1', port: 9118 }],
|
|
22
|
+
win32: [{ host: '127.66.36.18', port: 2080 }, { host: '127.0.0.1', port: 2080 }],
|
|
23
|
+
};
|
|
24
|
+
/** 兼容只按端口取候选的调用方:同一平台的端口按偏好去重。 */
|
|
25
|
+
const LOCAL_PORTS = Object.fromEntries(
|
|
26
|
+
Object.entries(LOCAL_ENDPOINTS).map(([platform, endpoints]) => [platform, [...new Set(endpoints.map((endpoint) => endpoint.port))]]),
|
|
27
|
+
);
|
|
18
28
|
/** /detect/version 是轻量探针,超时短;/bind 要等它去云端换短链,实测 5~16 秒。 */
|
|
19
29
|
const PROBE_TIMEOUT_MS = 3000;
|
|
20
30
|
const BIND_TIMEOUT_MS = 30_000;
|
|
21
31
|
|
|
22
|
-
function
|
|
32
|
+
function endpointsFor(platform) {
|
|
23
33
|
const key = String(platform || process.platform);
|
|
24
|
-
|
|
25
|
-
|
|
34
|
+
return Array.isArray(LOCAL_ENDPOINTS[key]) ? LOCAL_ENDPOINTS[key] : [];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function portsFor(platform) {
|
|
38
|
+
return LOCAL_PORTS[String(platform || process.platform)] || [];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function hostsFor(platform) {
|
|
42
|
+
return [...new Set(endpointsFor(platform).map((endpoint) => endpoint.host))];
|
|
26
43
|
}
|
|
27
44
|
|
|
28
45
|
/**
|
|
@@ -30,6 +47,7 @@ function portsFor(platform) {
|
|
|
30
47
|
* `ok=false` 表示这个口没人应答(没装 / 没起 / 被别的程序占着)。
|
|
31
48
|
*/
|
|
32
49
|
async function probePort(port, options = {}) {
|
|
50
|
+
const host = typeof options.host === 'string' && options.host ? options.host : '127.0.0.1';
|
|
33
51
|
const doFetch = typeof options.fetchImpl === 'function'
|
|
34
52
|
? options.fetchImpl
|
|
35
53
|
: (typeof globalThis.fetch === 'function' ? globalThis.fetch : null);
|
|
@@ -38,7 +56,7 @@ async function probePort(port, options = {}) {
|
|
|
38
56
|
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
|
39
57
|
const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
40
58
|
try {
|
|
41
|
-
const response = await doFetch(`http
|
|
59
|
+
const response = await doFetch(`http://${host}:${port}/detect/version`, {
|
|
42
60
|
headers: { accept: 'application/json' },
|
|
43
61
|
...(controller ? { signal: controller.signal } : {}),
|
|
44
62
|
});
|
|
@@ -65,19 +83,24 @@ async function probePort(port, options = {}) {
|
|
|
65
83
|
* 读本机设备端状态。
|
|
66
84
|
*
|
|
67
85
|
* @returns {Promise<{ supported: boolean, installed: boolean, bound: boolean, peerid: string,
|
|
68
|
-
* tun: string, port: number, error: string }>}
|
|
69
|
-
* `supported=false` 是这一版没接的平台(Windows
|
|
86
|
+
* tun: string, host: string, port: number, service: {installed:boolean}, error: string }>}
|
|
87
|
+
* `supported=false` 是这一版没接的平台(Windows 只接 x64,Linux 接 x64/arm64);
|
|
70
88
|
* `installed=false` 表示本机找不到设备端;`bound=false` 表示装了但还没登录绑定。
|
|
71
89
|
*/
|
|
72
90
|
async function readStatus(options = {}) {
|
|
73
91
|
const platform = String(options.platform || process.platform);
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
92
|
+
const endpoints = Array.isArray(options.endpoints)
|
|
93
|
+
? options.endpoints
|
|
94
|
+
: (Array.isArray(options.ports) ? options.ports.map((port) => ({ host: '127.0.0.1', port })) : endpointsFor(platform));
|
|
95
|
+
if (!endpoints.length) {
|
|
96
|
+
return {
|
|
97
|
+
supported: false, installed: false, bound: false, peerid: '', tun: '', host: '', port: 0,
|
|
98
|
+
service: { installed: false }, error: '这一版只接了 macOS、Windows 与 Linux 的节点小宝设备端',
|
|
99
|
+
};
|
|
77
100
|
}
|
|
78
101
|
let lastError = '';
|
|
79
|
-
for (const port of
|
|
80
|
-
const result = await probePort(port, options);
|
|
102
|
+
for (const { host, port } of endpoints) {
|
|
103
|
+
const result = await probePort(port, { ...options, host });
|
|
81
104
|
if (result.ok) {
|
|
82
105
|
return {
|
|
83
106
|
supported: true,
|
|
@@ -85,51 +108,64 @@ async function readStatus(options = {}) {
|
|
|
85
108
|
bound: result.peerid !== '',
|
|
86
109
|
peerid: result.peerid,
|
|
87
110
|
tun: result.tun,
|
|
111
|
+
host,
|
|
88
112
|
port,
|
|
113
|
+
service: { installed: true },
|
|
89
114
|
error: '',
|
|
90
115
|
};
|
|
91
116
|
}
|
|
92
117
|
lastError = result.error || lastError;
|
|
93
118
|
}
|
|
94
|
-
return {
|
|
119
|
+
return {
|
|
120
|
+
supported: true, installed: false, bound: false, peerid: '', tun: '', host: '', port: 0,
|
|
121
|
+
service: { installed: false }, error: lastError,
|
|
122
|
+
};
|
|
95
123
|
}
|
|
96
124
|
|
|
97
125
|
/**
|
|
98
126
|
* 取设备端的绑定链接。设备端回 302 指向它的云端绑定页,把那一段 Location 原样交给面板。
|
|
99
127
|
* 已经绑过的设备不会再给链接(它回 404 或直接给已绑页面),这里按 `ok:false` 如实说明。
|
|
128
|
+
* 只给了端口没给主机时,按平台的候选主机逐个问(Windows 要先问它的虚拟回环)。
|
|
100
129
|
*/
|
|
101
130
|
async function readBindUrl(options = {}) {
|
|
102
131
|
const doFetch = typeof options.fetchImpl === 'function'
|
|
103
132
|
? options.fetchImpl
|
|
104
133
|
: (typeof globalThis.fetch === 'function' ? globalThis.fetch : null);
|
|
105
134
|
if (!doFetch) return { ok: false, url: '', error: '当前环境没有可用的 fetch' };
|
|
106
|
-
const
|
|
107
|
-
|
|
135
|
+
const platform = String(options.platform || process.platform);
|
|
136
|
+
const port = Number(options.port) > 0 ? Number(options.port) : 0;
|
|
137
|
+
const hosts = typeof options.host === 'string' && options.host ? [options.host] : hostsFor(platform);
|
|
138
|
+
const endpoints = port ? hosts.map((host) => ({ host, port })) : endpointsFor(platform);
|
|
139
|
+
if (!endpoints.length) return { ok: false, url: '', error: '这一版没接这个平台' };
|
|
108
140
|
const timeoutMs = Number(options.timeoutMs) > 0 ? Number(options.timeoutMs) : BIND_TIMEOUT_MS;
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
141
|
+
let lastError = '';
|
|
142
|
+
for (const { host, port: candidatePort } of endpoints) {
|
|
143
|
+
const controller = typeof AbortController === 'function' ? new AbortController() : null;
|
|
144
|
+
const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
145
|
+
try {
|
|
146
|
+
// 手动接管跳转:要的是那一跳的目标地址,不是跳过去之后的页面。
|
|
147
|
+
const response = await doFetch(`http://${host}:${candidatePort}/bind`, {
|
|
148
|
+
redirect: 'manual',
|
|
149
|
+
headers: { accept: 'text/html' },
|
|
150
|
+
...(controller ? { signal: controller.signal } : {}),
|
|
151
|
+
});
|
|
152
|
+
const location = response && response.headers && typeof response.headers.get === 'function'
|
|
153
|
+
? response.headers.get('location') : '';
|
|
154
|
+
if (typeof location === 'string' && /^https?:\/\//i.test(location.trim())) {
|
|
155
|
+
return { ok: true, url: location.trim(), error: '' };
|
|
156
|
+
}
|
|
157
|
+
lastError = '设备端没有给出绑定链接(可能已经绑定过)';
|
|
158
|
+
} catch (error) {
|
|
159
|
+
if (controller && controller.signal.aborted) {
|
|
160
|
+
lastError = '向设备端要绑定链接超时,请确认它正在运行';
|
|
161
|
+
} else {
|
|
162
|
+
lastError = `连不上本机设备端:${error && error.message ? error.message : String(error)}`;
|
|
163
|
+
}
|
|
164
|
+
} finally {
|
|
165
|
+
if (timer) clearTimeout(timer);
|
|
127
166
|
}
|
|
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
167
|
}
|
|
168
|
+
return { ok: false, url: '', error: lastError };
|
|
133
169
|
}
|
|
134
170
|
|
|
135
171
|
/**
|
|
@@ -157,7 +193,11 @@ function lineOf(value) {
|
|
|
157
193
|
}
|
|
158
194
|
|
|
159
195
|
module.exports = {
|
|
196
|
+
LOCAL_ENDPOINTS,
|
|
160
197
|
LOCAL_PORTS,
|
|
198
|
+
endpointsFor,
|
|
199
|
+
portsFor,
|
|
200
|
+
hostsFor,
|
|
161
201
|
lineOf,
|
|
162
202
|
probePort,
|
|
163
203
|
readStatus,
|