dsh-mobile-ios 0.2.16 → 0.2.18
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/LICENSE +21 -21
- package/README.md +311 -284
- package/cordis.patch.yml +20 -20
- package/entries/plugin.mjs +197 -223
- package/entries/sidecar.mjs +92 -111
- package/lib/client.js +1754 -1621
- package/lib/vendor/qrcode-generator.cjs +2297 -2297
- package/lib/vendor/qrcode-utf8.cjs +26 -26
- package/package.json +3 -2
- package/src/adapter/artifacts.mjs +235 -235
- package/src/adapter/cordis-host.mjs +1444 -1225
- package/src/adapter/mock-host.mjs +498 -430
- package/src/adapter/mux-host.mjs +99 -99
- package/src/adapter/session-history.mjs +241 -241
- package/src/cloudflare.mjs +735 -698
- package/src/dns-label.mjs +32 -32
- package/src/dns-probe.mjs +110 -110
- package/src/file-log.mjs +68 -68
- package/src/gateway.mjs +285 -285
- package/src/host-name.mjs +42 -0
- package/src/pairing.mjs +300 -300
- package/src/protocol.mjs +59 -59
- package/src/provision-local.mjs +883 -560
- package/src/provision-select.mjs +105 -0
- package/src/rest.mjs +958 -894
- package/src/ring.mjs +148 -148
- package/src/server.mjs +246 -246
- package/src/tunnel-error.mjs +91 -0
- package/src/tunnel-state.mjs +122 -0
- package/src/tunnel.mjs +544 -544
- package/src/version.mjs +24 -24
- package/src/ws.mjs +332 -332
- package/src/zone-health.mjs +430 -251
package/lib/client.js
CHANGED
|
@@ -1,1621 +1,1754 @@
|
|
|
1
|
-
// lib/client.js — 桌面端 UI 注入模块(X3)
|
|
2
|
-
//
|
|
3
|
-
// 契约(考古自社区 dsh-mobile 0.8.11 的 lib/client.js):
|
|
4
|
-
// 宿主 web app 按包的 `exports["./client"]` 加载本文件,浏览器里执行到
|
|
5
|
-
// `window.__ModuleLoader__.load({ id, factory })` 注册模块;
|
|
6
|
-
// factory 返回的模块需导出 `apply(ctx)` 与 `inject = ["slots"]`,
|
|
7
|
-
// 宿主把 slots 服务注入进来,UI 通过 slots 挂载。
|
|
8
|
-
//
|
|
9
|
-
// 我们挂两个东西:
|
|
10
|
-
// 1. 左下角侧栏 banner(slot: sidebar.footer.action)——手机图标 + 「iOS移动伴侣」
|
|
11
|
-
// + 连接状态点;窄栏(rail)只留图标。
|
|
12
|
-
// 2. 点击展开的状态面板(原生 DOM overlay,宿主未提供 react-dom)——
|
|
13
|
-
// 网关状态 / 适配器 / 已配对设备 / 隧道 / 生成配对码。
|
|
14
|
-
//
|
|
15
|
-
// 如实边界:网关端口被占会自动顺延(3081–3085 探测);面板数据来自
|
|
16
|
-
// /mobile/v1/desktop/summary(回环-only,无敏感字段),拿不到就明确显示
|
|
17
|
-
// 「网关不可达」,绝不编造状态。
|
|
18
|
-
window.__ModuleLoader__.load({
|
|
19
|
-
id: 'dsh-mobile-ios',
|
|
20
|
-
factory: (require) => {
|
|
21
|
-
var module = { exports: {} };
|
|
22
|
-
var exports = module.exports;
|
|
23
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
24
|
-
|
|
25
|
-
let react = require('react');
|
|
26
|
-
|
|
27
|
-
// ── 状态 ─────────────────────────────────────────────────────
|
|
28
|
-
const PORT_CANDIDATES = [3081, 3082, 3083, 3084, 3085];
|
|
29
|
-
let gatewayPort = null;
|
|
30
|
-
let summary = null; // /desktop/summary 结果或 null
|
|
31
|
-
let probing = false;
|
|
32
|
-
|
|
33
|
-
function baseUrl() {
|
|
34
|
-
return gatewayPort ? `http://127.0.0.1:${gatewayPort}` : null;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async function fetchJson(path, options, timeoutMs = 1500) {
|
|
38
|
-
const base = baseUrl();
|
|
39
|
-
if (!base) throw new Error('gateway-not-found');
|
|
40
|
-
const controller = new AbortController();
|
|
41
|
-
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
42
|
-
try {
|
|
43
|
-
const res = await fetch(base + path, { ...options, signal: controller.signal });
|
|
44
|
-
const body = await res.json();
|
|
45
|
-
if (!res.ok) throw Object.assign(new Error(body?.error?.message || res.statusText), { body });
|
|
46
|
-
return body;
|
|
47
|
-
} finally {
|
|
48
|
-
clearTimeout(timer);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** 探测网关端口(顺延容忍),成功后拉一次摘要 */
|
|
53
|
-
async function probe() {
|
|
54
|
-
if (probing) return;
|
|
55
|
-
probing = true;
|
|
56
|
-
try {
|
|
57
|
-
let found = null;
|
|
58
|
-
for (const port of PORT_CANDIDATES) {
|
|
59
|
-
const controller = new AbortController();
|
|
60
|
-
const timer = setTimeout(() => controller.abort(), 700);
|
|
61
|
-
try {
|
|
62
|
-
const res = await fetch(`http://127.0.0.1:${port}/mobile/v1/health`, { signal: controller.signal });
|
|
63
|
-
if (res.ok) { found = port; break; }
|
|
64
|
-
} catch {} // 端口不是我们/没起,继续探
|
|
65
|
-
finally { clearTimeout(timer); }
|
|
66
|
-
}
|
|
67
|
-
// 必须跟着探到的结果走:只在成功时赋值、失败时清空。
|
|
68
|
-
// 以前失败会保留旧端口,网关换端口后一直去敲那个死端口(若被别的服务
|
|
69
|
-
// 占用还会解析出非 JSON),面板就一直红着。
|
|
70
|
-
gatewayPort = found;
|
|
71
|
-
if (gatewayPort) summary = await fetchJson('/mobile/v1/desktop/summary').catch(() => null);
|
|
72
|
-
else summary = null;
|
|
73
|
-
} finally {
|
|
74
|
-
probing = false;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** 面板打开时的轻量刷新:端口已经探到,只拉摘要并重绘,不再重复扫端口。 */
|
|
79
|
-
async function refreshSummary() {
|
|
80
|
-
if (!gatewayPort || summaryFetching) return;
|
|
81
|
-
summaryFetching = true;
|
|
82
|
-
try {
|
|
83
|
-
const next = await fetchJson('/mobile/v1/desktop/summary').catch(() => null);
|
|
84
|
-
if (next?.ok) {
|
|
85
|
-
summary = next;
|
|
86
|
-
renderSummary();
|
|
87
|
-
}
|
|
88
|
-
} finally {
|
|
89
|
-
summaryFetching = false;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 首屏快速补探。
|
|
95
|
-
*
|
|
96
|
-
* 宿主重启时插件先于网关绑端口加载,第一次探测必然落空;
|
|
97
|
-
* 只靠 15 秒轮询会让面板红着不动,用户看到的就是「插件坏了」。
|
|
98
|
-
*/
|
|
99
|
-
async function probeUntilUp(attempts = 5, delayMs = 1200) {
|
|
100
|
-
for (let i = 0; i < attempts; i += 1) {
|
|
101
|
-
await probe();
|
|
102
|
-
if (summary && summary.ok) return;
|
|
103
|
-
if (i < attempts - 1) await new Promise((resolve) => { setTimeout(resolve, delayMs); });
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** 生成配对码(本机动作,回环-only 端点)。route 显式选路:'lan' | 'tunnel' | undefined=自动 */
|
|
108
|
-
async function issuePairingCode(route) {
|
|
109
|
-
return fetchJson('/mobile/v1/pairing/code', {
|
|
110
|
-
method: 'POST',
|
|
111
|
-
headers: { 'content-type': 'application/json' },
|
|
112
|
-
body: JSON.stringify({ deviceName: '待连接设备', ...(route ? { route } : {}) }),
|
|
113
|
-
}, 2500);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** 隧道启停(本机管理面动作,回环-only 端点) */
|
|
117
|
-
/** 单台下线:管理面动作,与出码同样的本机约束,绝不走公网域名。 */
|
|
118
|
-
async function revokeDevice(deviceId) {
|
|
119
|
-
return fetchJson('/mobile/v1/pairing/revoke-device', {
|
|
120
|
-
method: 'POST',
|
|
121
|
-
headers: { 'content-type': 'application/json' },
|
|
122
|
-
body: JSON.stringify({ deviceId }),
|
|
123
|
-
}, 2500);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async function tunnelAction(action) {
|
|
127
|
-
// start 首次会顺带「开通固定地址 + 等公网注册 + DNS 复查」,实测经常超过 20s;
|
|
128
|
-
// 以前 20s 就掐断,用户点第一次报错、点第二次才出码——就是这里太短。
|
|
129
|
-
return fetchJson(`/mobile/v1/tunnel/${action}`, { method: 'POST' }, action === 'start' ? 60000 : 25000);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// ── 样式 ─────────────────────────────────────────────────────
|
|
133
|
-
const STYLE_ID = 'dsh-mios-style';
|
|
134
|
-
function installStyle() {
|
|
135
|
-
if (document.getElementById(STYLE_ID)) return;
|
|
136
|
-
const style = document.createElement('style');
|
|
137
|
-
style.id = STYLE_ID;
|
|
138
|
-
// 主题跟随宿主:颜色全部走 CSS 变量,.light 类由 detectHostTheme() 动态切换。
|
|
139
|
-
// 以前全硬编码深色,宿主开浅色主题时面板像一块黑补丁(2026-11-18 真机反馈)。
|
|
140
|
-
style.textContent = `
|
|
141
|
-
.dsh-mios-trigger { display: inline-flex; align-items: center; gap: 7px; background: none;
|
|
142
|
-
border: none; color: inherit; cursor: pointer; padding: 6px 8px; border-radius: 8px;
|
|
143
|
-
font: inherit; line-height: 1; }
|
|
144
|
-
.dsh-mios-trigger:hover { background: rgba(255,255,255,.08); }
|
|
145
|
-
.dsh-mios-trigger.is-rail { padding: 8px; }
|
|
146
|
-
.dsh-mios-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
|
|
147
|
-
.dsh-mios-dot.ok { background: #3fb27f; }
|
|
148
|
-
.dsh-mios-dot.down { background: #d94f3d; }
|
|
149
|
-
.dsh-mios-panel { --bg: #171b24; --fg: #e8eaf0; --muted: #8b93a3; --border: #303a52;
|
|
150
|
-
--border-hover: #4a5678; --shadow: rgba(0,0,0,.5); --fg-soft: rgba(232,234,240,.7);
|
|
151
|
-
--muted-border: rgba(139,147,163,.45); --muted-bg: rgba(139,147,163,.12);
|
|
152
|
-
--muted-bg-hover: rgba(139,147,163,.2); }
|
|
153
|
-
.dsh-mios-panel.light { --bg: #ffffff; --fg: #1f2430; --muted: #6b7280; --border: #d9dee8;
|
|
154
|
-
--border-hover: #9aa5bd; --shadow: rgba(31,36,48,.18); --fg-soft: rgba(31,36,48,.7);
|
|
155
|
-
--muted-border: rgba(107,114,128,.4); --muted-bg: rgba(107,114,128,.1);
|
|
156
|
-
--muted-bg-hover: rgba(107,114,128,.18); }
|
|
157
|
-
.dsh-mios-panel { position: fixed; left: 14px; bottom: 52px; width: 300px; z-index: 99990;
|
|
158
|
-
background: var(--bg); color: var(--fg); border: 1px solid var(--border); border-radius: 14px;
|
|
159
|
-
box-shadow: 0 14px 44px var(--shadow); font: 13px/1.6 -apple-system, "PingFang SC", sans-serif;
|
|
160
|
-
padding: 14px; }
|
|
161
|
-
.dsh-mios-panel h3 { margin: 0 0 10px; font-size: 14px; display: flex; align-items: center; gap: 8px; }
|
|
162
|
-
.dsh-mios-upd { margin-left: auto; background: none; border: 1px solid var(--border); border-radius: 7px;
|
|
163
|
-
color: var(--muted); font-family: inherit; font-size: 11px; font-weight: 500; line-height: 1.4;
|
|
164
|
-
cursor: pointer; padding: 3px 8px; }
|
|
165
|
-
.dsh-mios-upd:hover { color: var(--fg); border-color: var(--border-hover); }
|
|
166
|
-
.dsh-mios-modal-mask { --modal-bg:#1c2230; --modal-fg:#e8eaf0; --modal-muted:#8b93a3;
|
|
167
|
-
--modal-border:#3a4560; --modal-btn:rgba(139,147,163,.12); --modal-accent:#3fb27f; --modal-shadow:rgba(0,0,0,.6);
|
|
168
|
-
position:fixed; inset:0; z-index:2147483000; background:var(--modal-shadow);
|
|
169
|
-
display:flex; align-items:center; justify-content:center; padding:20px;
|
|
170
|
-
font-family:-apple-system, "PingFang SC", sans-serif; font-size:13px; line-height:1.6; }
|
|
171
|
-
.dsh-mios-modal-mask.light { --modal-bg:#ffffff; --modal-fg:#1f2430; --modal-muted:#6b7280;
|
|
172
|
-
--modal-border:#d9dee8; --modal-btn:rgba(107,114,128,.1); --modal-accent:#2f9d70; --modal-shadow:rgba(31,36,48,.35); }
|
|
173
|
-
.dsh-mios-modal { width:100%; max-width:340px; box-sizing:border-box; background:var(--modal-bg);
|
|
174
|
-
color:var(--modal-fg); border:1px solid var(--modal-border); border-radius:14px; padding:18px;
|
|
175
|
-
box-shadow:0 18px 60px var(--modal-shadow); }
|
|
176
|
-
.dsh-mios-modal h4 { margin:0 0 12px; font-size:15px; display:flex; align-items:center; gap:8px; }
|
|
177
|
-
.dsh-mios-modal .modal-banner { margin:0 0 12px; padding:9px 11px; border-radius:9px; font-size:12px;
|
|
178
|
-
font-weight:600; line-height:1.5; background:rgba(63,178,127,.12); color:var(--modal-accent);
|
|
179
|
-
border:1px solid var(--modal-accent); }
|
|
180
|
-
.dsh-mios-modal .modal-banner.success { background:rgba(63,178,127,.16); color:#2f9d70; }
|
|
181
|
-
.dsh-mios-modal .modal-banner.warn { background:rgba(224,160,60,.14); color:#d18a26; border-color:#d18a26; }
|
|
182
|
-
.dsh-mios-modal .modal-banner.error { background:rgba(217,79,61,.14); color:#d94f3d; border-color:#d94f3d; }
|
|
183
|
-
.dsh-mios-modal .modal-x { margin-left:auto; background:none; border:none; color:var(--modal-muted);
|
|
184
|
-
font-size:16px; cursor:pointer; line-height:1; padding:2px 4px; }
|
|
185
|
-
.dsh-mios-modal .modal-line { display:flex; justify-content:space-between; gap:10px;
|
|
186
|
-
font-size:12px; color:var(--modal-muted); margin:6px 0; }
|
|
187
|
-
.dsh-mios-modal .modal-line b { color:var(--modal-fg); font-weight:600; max-width:200px;
|
|
188
|
-
overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
189
|
-
.dsh-mios-modal .modal-line b.new { color:var(--modal-accent); }
|
|
190
|
-
.dsh-mios-modal .modal-note { margin:12px 0 4px; font-size:11px; color:var(--modal-muted); line-height:1.6; }
|
|
191
|
-
.dsh-mios-modal .modal-actions { display:flex; gap:8px; margin-top:14px; }
|
|
192
|
-
.dsh-mios-modal .modal-actions button { flex:1; padding:8px 0; border-radius:8px; cursor:pointer;
|
|
193
|
-
font-family:inherit; font-size:12px; font-weight:600; line-height:1.4;
|
|
194
|
-
border:1px solid var(--modal-border); background:var(--modal-btn); color:var(--modal-fg); }
|
|
195
|
-
.dsh-mios-modal .modal-actions button.primary { border-color:var(--modal-accent);
|
|
196
|
-
background:rgba(63,178,127,.16); color:var(--modal-accent); }
|
|
197
|
-
.dsh-mios-modal .modal-actions button:disabled { opacity:.6; cursor:default; }
|
|
198
|
-
/* 复制结果必须看得见:dsh-app:// 页面里剪贴板可能不可用,只改按钮文字容易被无视。
|
|
199
|
-
.modal-toast 常驻在动作条上方,成功/失败都留痕。 */
|
|
200
|
-
.dsh-mios-modal .modal-toast { margin:10px 0 0; padding:7px 10px; border-radius:8px; font-size:11px;
|
|
201
|
-
font-weight:600; line-height:1.5; background:rgba(139,147,163,.14); color:var(--modal-muted); }
|
|
202
|
-
.dsh-mios-modal .modal-toast.success { background:rgba(63,178,127,.16); color:#2f9d70; }
|
|
203
|
-
.dsh-mios-modal .modal-toast.error { background:rgba(217,79,61,.14); color:#d94f3d; }
|
|
204
|
-
/* 复制全线失败时的兜底:给一个已自动全选的输入框,用户 Ctrl/⌘+C 就能拿走 */
|
|
205
|
-
.dsh-mios-modal .modal-pick { width:100%; box-sizing:border-box; margin:10px 0 0; padding:8px 10px;
|
|
206
|
-
border-radius:8px; border:1px solid var(--modal-accent); background:rgba(63,178,127,.10);
|
|
207
|
-
color:var(--modal-fg); font-family:ui-monospace, SFMono-Regular, Menlo, monospace; font-size:12px;
|
|
208
|
-
user-select:text; -webkit-user-select:text; }
|
|
209
|
-
.dsh-mios-modal .modal-steps { margin:10px 0 0; padding-left:18px; font-size:11px;
|
|
210
|
-
color:var(--modal-muted); line-height:1.7; }
|
|
211
|
-
.dsh-mios-modal .modal-steps li { margin:2px 0; }
|
|
212
|
-
.dsh-mios-panel .row { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); margin: 3px 0; }
|
|
213
|
-
.dsh-mios-panel .row b { color: var(--fg); font-weight: 600; text-align: right; max-width: 170px;
|
|
214
|
-
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
215
|
-
.dsh-mios-device-toggle { background: none; border: none; color: var(--muted); cursor: pointer;
|
|
216
|
-
font: inherit; padding: 0; display: inline-flex; align-items: center; gap: 5px; }
|
|
217
|
-
.dsh-mios-device-toggle:hover { color: var(--fg); }
|
|
218
|
-
.dsh-mios-devices { margin: 2px 0 6px; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
|
|
219
|
-
.dsh-mios-devices[hidden] { display: none; }
|
|
220
|
-
.dsh-mios-device-row { display: flex; align-items: center; gap: 8px; padding: 5px 10px;
|
|
221
|
-
font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); }
|
|
222
|
-
.dsh-mios-device-row:first-child { border-top: none; }
|
|
223
|
-
.dsh-mios-device-name { flex: 1; min-width: 0; overflow-wrap: anywhere; white-space: normal;
|
|
224
|
-
line-height: 1.35; color: var(--fg); }
|
|
225
|
-
.dsh-mios-device-online { color: #3fb27f; flex: none; }
|
|
226
|
-
.dsh-mios-device-offline { color: var(--muted); flex: none; }
|
|
227
|
-
.dsh-mios-device-kick { margin-left: auto; background: none; border: 1px solid rgba(217,79,61,.4); border-radius: 4px;
|
|
228
|
-
color: #c97a6d; cursor: pointer; font-family: inherit; font-size: 9px; font-weight: 500;
|
|
229
|
-
line-height: 1.3; padding: 1px 5px; flex: none; }
|
|
230
|
-
.dsh-mios-device-kick:hover { color: #d94f3d; border-color: rgba(217,79,61,.7); }
|
|
231
|
-
.dsh-mios-devices-empty { padding: 6px 8px; font-size: 11px; color: var(--muted); }
|
|
232
|
-
.dsh-mios-panel .pairbtn { width: 100%; margin-top: 10px; padding: 8px 0; border-radius: 9px;
|
|
233
|
-
border: 1px solid rgba(63,178,127,.45); background: rgba(63,178,127,.14); color: #3fb27f;
|
|
234
|
-
font: 600 13px/1.4 inherit-font, sans-serif; cursor: pointer; }
|
|
235
|
-
.dsh-mios-panel .pairbtn:disabled { opacity: .5; cursor: default; }
|
|
236
|
-
.dsh-mios-banner { border: 1px solid var(--border); border-radius: 10px; padding: 10px 10px 11px; margin-top: 10px; }
|
|
237
|
-
.dsh-mios-banner h4 { margin: 0; font-size: 12px; font-weight: 600; color: var(--fg);
|
|
238
|
-
display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
|
|
239
|
-
.dsh-mios-banner .banner-state { font-size: 11px; font-weight: 400; color: var(--muted);
|
|
240
|
-
max-width: 190px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
241
|
-
.dsh-mios-banner .banner-guide { margin-top: 4px; font-size: 11px; color: var(--muted); line-height: 1.5; }
|
|
242
|
-
.dsh-mios-banner .banner-addr { margin-top: 4px; font-size: 11px; color: var(--muted);
|
|
243
|
-
font-family: ui-monospace, "SF Mono", Menlo, monospace; word-break: break-all; line-height: 1.5; }
|
|
244
|
-
.dsh-mios-banner .pairbtn { margin-top: 8px; }
|
|
245
|
-
.dsh-mios-banner .tunnelbtn { border-color: var(--muted-border); background: var(--muted-bg); color: var(--muted); }
|
|
246
|
-
.dsh-mios-banner .tunnelbtn:hover:not(:disabled) { border-color: var(--fg-soft); color: var(--fg); background: var(--muted-bg-hover); }
|
|
247
|
-
.dsh-mios-banner .tunnelbtn:disabled { opacity: .5; cursor: default; }
|
|
248
|
-
.dsh-mios-banner .stopbtn { margin-top: 6px; padding: 5px 0; font-weight: 500;
|
|
249
|
-
border-color: rgba(217,79,61,.4); background: none; color: #c97a6d; }
|
|
250
|
-
.dsh-mios-banner .stopbtn:hover:not(:disabled) { border-color: rgba(217,79,61,.7); color: #d94f3d; }
|
|
251
|
-
.dsh-mios-panel .code { margin-top: 10px; text-align: center; font-size: 30px; letter-spacing: 8px;
|
|
252
|
-
font-weight: 700; color: var(--fg); font-variant-numeric: tabular-nums; }
|
|
253
|
-
.dsh-mios-qr { margin-top: 10px; padding: 10px; background: #fff; border-radius: 10px;
|
|
254
|
-
display: flex; justify-content: center; }
|
|
255
|
-
.dsh-mios-qr svg { width: 180px; height: 180px; display: block; }
|
|
256
|
-
.dsh-mios-panel .err { margin-top: 10px; font-size: 12px; color: #d94f3d; text-align: center; }
|
|
257
|
-
.dsh-mios-panel footer { margin-top: 10px; font-size: 11px; color: var(--muted); text-align: center; }
|
|
258
|
-
.dsh-mios-close { margin-left: auto; background: none; border: none; color: var(--muted);
|
|
259
|
-
font-size: 16px; cursor: pointer; padding: 2px 6px; }
|
|
260
|
-
`;
|
|
261
|
-
document.head.append(style);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// ── 面板(原生 DOM;宿主无 react-dom) ───────────────────────
|
|
265
|
-
// 双 banner 布局:上面「局域网连接」,下面「远程通道连接」。
|
|
266
|
-
// 远程 banner 是合并后的单一主按钮:没开过→开通;开过没跑→开启并出码;
|
|
267
|
-
// 跑着→直接出码。停止远程是旁边的小按钮,只在该停的时候出现。
|
|
268
|
-
const PANEL_TITLE = 'DSH iOS移动伴侣';
|
|
269
|
-
const BANNER_LABEL = '📱 iOS移动伴侣';
|
|
270
|
-
// 插件更新检查:读 npm registry 的 latest(与「添加插件」安装源同源)。
|
|
271
|
-
// npm 官方源带 CORS *,桌面端 webview 可直接 fetch;npmmirror 作国内备用。
|
|
272
|
-
const PLUGIN_NAME = 'dsh-mobile-ios';
|
|
273
|
-
const PLUGIN_REGISTRIES = [
|
|
274
|
-
'https://registry.npmjs.org',
|
|
275
|
-
'https://registry.npmmirror.com',
|
|
276
|
-
];
|
|
277
|
-
const stripUrl = (u) => String(u || '').replace(/^https?:\/\//, '');
|
|
278
|
-
const stripLocal = (h) => String(h || '').replace(/\.local$/i, '');
|
|
279
|
-
|
|
280
|
-
// 宿主插件管理服务(remote.pluginManager)。注入模块经 apply 拿到;
|
|
281
|
-
// 非 Desktop 或老宿主拿不到时保持 null,更新入口降级为「复制包名」手动安装。
|
|
282
|
-
let pluginManager = null;
|
|
283
|
-
let updateInFlight = false; // 一步更新的并发锁:更新中禁止再次触发
|
|
284
|
-
let panelEl = null;
|
|
285
|
-
let tunnelBusy = false;
|
|
286
|
-
const bannerBusy = { lan: false, remote: false };
|
|
287
|
-
let outsideCloser = null;
|
|
288
|
-
let devicesExpanded = false;
|
|
289
|
-
let kickingDeviceId = null;
|
|
290
|
-
// 面板打开期间的摘要刷新:probe() 只更新数据不重绘,正是「扫码后要关掉重开才更新」的根因。
|
|
291
|
-
let summaryPollTimer = null;
|
|
292
|
-
let summaryFetching = false;
|
|
293
|
-
let lastDeviceSignature = null;
|
|
294
|
-
|
|
295
|
-
/** 探测宿主主题:显式标记(class/data-theme)→ body 亮度启发 → 系统偏好。
|
|
296
|
-
* Electron 宿主切换主题只改页面 class,matchMedia 不一定跟着变,所以三级都查。 */
|
|
297
|
-
function detectHostTheme() {
|
|
298
|
-
const root = document.documentElement;
|
|
299
|
-
const body = document.body;
|
|
300
|
-
const marked = `${root.className} ${body?.className || ''} ${root.getAttribute('data-theme') || ''} ${body?.getAttribute('data-theme') || ''} ${root.getAttribute('data-color-mode') || ''}`.toLowerCase();
|
|
301
|
-
if (/\bdark\b/.test(marked)) return 'dark';
|
|
302
|
-
if (/\blight\b/.test(marked)) return 'light';
|
|
303
|
-
// 亮度启发:宿主页面底色亮就是浅色主题(面板挂在这个页面上,跟它一致最直观)。
|
|
304
|
-
// 全黑或全透明底色不可判,落到系统偏好。
|
|
305
|
-
const bgc = body ? getComputedStyle(body).backgroundColor
|
|
306
|
-
: getComputedStyle(root).backgroundColor;
|
|
307
|
-
const m = bgc.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
|
|
308
|
-
if (m && (+m[1] + +m[2] + +m[3]) > 0) {
|
|
309
|
-
return (0.2126 * +m[1] + 0.7152 * +m[2] + 0.0722 * +m[3]) > 140 ? 'light' : 'dark';
|
|
310
|
-
}
|
|
311
|
-
return window.matchMedia?.('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/** 面板主题实时跟随宿主:每次渲染都重判(用户可能中途切主题)。 */
|
|
315
|
-
function applyPanelTheme() {
|
|
316
|
-
if (!panelEl) return;
|
|
317
|
-
panelEl.classList.toggle('light', detectHostTheme() === 'light');
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
function removePanel() {
|
|
321
|
-
panelEl?.remove();
|
|
322
|
-
panelEl = null;
|
|
323
|
-
closeUpdateModal();
|
|
324
|
-
if (summaryPollTimer) {
|
|
325
|
-
clearInterval(summaryPollTimer);
|
|
326
|
-
summaryPollTimer = null;
|
|
327
|
-
}
|
|
328
|
-
lastDeviceSignature = null;
|
|
329
|
-
if (outsideCloser) {
|
|
330
|
-
document.removeEventListener('pointerdown', outsideCloser);
|
|
331
|
-
outsideCloser = null;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
function row(label, valueId) {
|
|
336
|
-
const div = document.createElement('div');
|
|
337
|
-
div.className = 'row';
|
|
338
|
-
const l = document.createElement('span');
|
|
339
|
-
l.textContent = label;
|
|
340
|
-
const v = document.createElement('b');
|
|
341
|
-
v.id = valueId;
|
|
342
|
-
v.textContent = '—';
|
|
343
|
-
div.append(l, v);
|
|
344
|
-
return div;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/** 「同时在线设备」行:标签可点开展开在线设备清单,右侧显示 在线/上限。 */
|
|
348
|
-
function deviceRows() {
|
|
349
|
-
const wrap = document.createElement('div');
|
|
350
|
-
const head = document.createElement('div');
|
|
351
|
-
head.className = 'row';
|
|
352
|
-
const toggle = document.createElement('button');
|
|
353
|
-
toggle.className = 'dsh-mios-device-toggle';
|
|
354
|
-
toggle.id = 'dsh-mios-device-toggle';
|
|
355
|
-
toggle.type = 'button';
|
|
356
|
-
toggle.textContent = '▸';
|
|
357
|
-
toggle.setAttribute('aria-label', '展开或收起当前在线设备清单');
|
|
358
|
-
const label = document.createElement('span');
|
|
359
|
-
label.textContent = '同时在线设备(最多5台)';
|
|
360
|
-
const left = document.createElement('span');
|
|
361
|
-
left.append(toggle, label);
|
|
362
|
-
const value = document.createElement('b');
|
|
363
|
-
value.id = 'dsh-mios-devices';
|
|
364
|
-
value.textContent = '—';
|
|
365
|
-
head.append(left, value);
|
|
366
|
-
toggle.addEventListener('click', () => {
|
|
367
|
-
devicesExpanded = !devicesExpanded;
|
|
368
|
-
renderDeviceList();
|
|
369
|
-
});
|
|
370
|
-
const list = document.createElement('div');
|
|
371
|
-
list.className = 'dsh-mios-devices';
|
|
372
|
-
list.id = 'dsh-mios-devices-list';
|
|
373
|
-
list.hidden = true;
|
|
374
|
-
wrap.append(head, list);
|
|
375
|
-
return wrap;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
/** 在线设备清单:默认折叠;只列当前在线设备(同时在线上限 5 台),下线逐台二次确认。 */
|
|
379
|
-
function renderDeviceList() {
|
|
380
|
-
const box = panelEl?.querySelector('#dsh-mios-devices-list');
|
|
381
|
-
if (!box) return;
|
|
382
|
-
box.hidden = !devicesExpanded;
|
|
383
|
-
const toggle = panelEl.querySelector('#dsh-mios-device-toggle');
|
|
384
|
-
if (toggle) toggle.textContent = devicesExpanded ? '▾' : '▸';
|
|
385
|
-
box.replaceChildren();
|
|
386
|
-
if (!summary?.ok) return;
|
|
387
|
-
// 后端已限制活跃设备上限;这里再按在线态筛选并截到 5,保证面板只展示真实并发连接。
|
|
388
|
-
const devices = (summary.pairing?.devices || []).filter((d) => d.online).slice(0, 5);
|
|
389
|
-
if (!devices.length) {
|
|
390
|
-
const empty = document.createElement('div');
|
|
391
|
-
empty.className = 'dsh-mios-devices-empty';
|
|
392
|
-
empty.textContent = '当前没有在线设备';
|
|
393
|
-
box.append(empty);
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
for (const device of devices) {
|
|
397
|
-
const line = document.createElement('div');
|
|
398
|
-
line.className = 'dsh-mios-device-row';
|
|
399
|
-
const name = document.createElement('span');
|
|
400
|
-
name.className = 'dsh-mios-device-name';
|
|
401
|
-
name.textContent = device.deviceName || '未命名设备';
|
|
402
|
-
name.title = name.textContent;
|
|
403
|
-
const state = document.createElement('span');
|
|
404
|
-
state.className = device.online ? 'dsh-mios-device-online' : 'dsh-mios-device-offline';
|
|
405
|
-
state.textContent = device.online ? '在线' : '离线';
|
|
406
|
-
const kick = document.createElement('button');
|
|
407
|
-
kick.className = 'dsh-mios-device-kick';
|
|
408
|
-
kick.textContent = kickingDeviceId === device.deviceId ? '处理中' : '下线';
|
|
409
|
-
kick.disabled = kickingDeviceId !== null;
|
|
410
|
-
kick.title = `将 ${name.textContent} 从这台电脑移除`;
|
|
411
|
-
kick.addEventListener('click', async () => {
|
|
412
|
-
// 二次确认:下线会让那台手机立刻失去读任务的能力,不能误点。
|
|
413
|
-
if (!window.confirm(`确定让「${name.textContent}」下线?该设备需要重新扫码才能再次连接。`)) return;
|
|
414
|
-
kickingDeviceId = device.deviceId;
|
|
415
|
-
renderDeviceList();
|
|
416
|
-
try {
|
|
417
|
-
await revokeDevice(device.deviceId);
|
|
418
|
-
await probe();
|
|
419
|
-
} catch (err) {
|
|
420
|
-
window.alert('下线失败:' + (err?.body?.error?.message || err.message));
|
|
421
|
-
} finally {
|
|
422
|
-
kickingDeviceId = null;
|
|
423
|
-
renderSummary();
|
|
424
|
-
}
|
|
425
|
-
});
|
|
426
|
-
line.append(name, state, kick);
|
|
427
|
-
box.append(line);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
function renderSummary() {
|
|
432
|
-
if (!panelEl) return;
|
|
433
|
-
applyPanelTheme();
|
|
434
|
-
const dot = panelEl.querySelector('#dsh-mios-panel-dot');
|
|
435
|
-
const ok = !!(summary && summary.ok);
|
|
436
|
-
dot.className = 'dsh-mios-dot ' + (ok ? 'ok' : 'down');
|
|
437
|
-
const set = (id, text) => { const el = panelEl.querySelector('#' + id); if (el) el.textContent = text; };
|
|
438
|
-
set('dsh-mios-adapter', ok ? `v${summary.version}` : '—');
|
|
439
|
-
set('dsh-mios-host', ok ? (stripLocal(summary.hostName) || '未上报') : '—');
|
|
440
|
-
if (ok) {
|
|
441
|
-
const max = summary.pairing?.maxDevices ?? 5;
|
|
442
|
-
const online = summary.pairing?.onlineDevices ?? 0;
|
|
443
|
-
set('dsh-mios-devices', `${online}/${max} 台`);
|
|
444
|
-
} else {
|
|
445
|
-
set('dsh-mios-devices', '—');
|
|
446
|
-
}
|
|
447
|
-
// 只有设备名/在线态/顺序变化才重建列表:1.2s 刷新不能让 hover 提示和下线按钮闪烁。
|
|
448
|
-
const devices = ok ? (summary.pairing?.devices || []) : [];
|
|
449
|
-
const signature = devices
|
|
450
|
-
.map((d) => `${d.deviceId}\u0000${d.deviceName || ''}\u0000${d.online ? 1 : 0}`)
|
|
451
|
-
.join('\u0001');
|
|
452
|
-
if (signature !== lastDeviceSignature) {
|
|
453
|
-
lastDeviceSignature = signature;
|
|
454
|
-
renderDeviceList();
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
// ── 局域网 banner ──
|
|
458
|
-
const lan = ok ? summary.lan : null;
|
|
459
|
-
const lanState = panelEl.querySelector('#dsh-mios-lan-state');
|
|
460
|
-
if (lanState) lanState.textContent = ok ? (lan ? `电脑地址 ${lan}` : '未获取到局域网地址') : '网关不可达';
|
|
461
|
-
const lanCodeBtn = panelEl.querySelector('#dsh-mios-lan-code');
|
|
462
|
-
if (lanCodeBtn) {
|
|
463
|
-
lanCodeBtn.disabled = !ok || !lan || bannerBusy.lan;
|
|
464
|
-
lanCodeBtn.title = ok && lan ? '' : '网关不可达或没有局域网地址';
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
// ── 远程通道 banner ──
|
|
468
|
-
// 四种事实分开说:运行中 / 已开通但没开 / 根本没开通 / 域名没生效。
|
|
469
|
-
// 「域名未生效」必须单列:地址开出来了但公网解析不到,显示「已开通」会误导人去扫码。
|
|
470
|
-
const tunnel = ok ? summary.tunnel : null;
|
|
471
|
-
const dnsBroken = tunnel && tunnel.dnsOk === false;
|
|
472
|
-
// 解析失败已经归因到 zone 层时,别再用「解析异常」这种症状词:
|
|
473
|
-
// moved 是归属冲突(改 NS 或换账号才能解),重探一万次也不会变。
|
|
474
|
-
const zoneCode = tunnel?.zone?.code || null;
|
|
475
|
-
// 「进程在跑」与「地址可用」是两件事:域名没生效时隧道进程可能已经起来了,
|
|
476
|
-
// 但扫码必然连不上,所以 running 仍然要求 dnsOk 不为 false。
|
|
477
|
-
const processUp = !!(tunnel && tunnel.state === 'running');
|
|
478
|
-
const running = processUp && !dnsBroken;
|
|
479
|
-
const remoteState = panelEl.querySelector('#dsh-mios-remote-state');
|
|
480
|
-
if (remoteState) {
|
|
481
|
-
remoteState.textContent = !tunnel
|
|
482
|
-
? '—'
|
|
483
|
-
: dnsBroken
|
|
484
|
-
? (zoneCode === 'moved'
|
|
485
|
-
? '域名归属冲突'
|
|
486
|
-
: processUp ? '已开启 · 域名未生效' : '解析异常 · 未开启')
|
|
487
|
-
: running
|
|
488
|
-
? '已开启'
|
|
489
|
-
: tunnel.provisioned && tunnel.fixedUrl
|
|
490
|
-
? '已开通 · 未开启'
|
|
491
|
-
: tunnel.mode === 'named' ? '未开通固定地址' : tunnel.state;
|
|
492
|
-
}
|
|
493
|
-
// 固定地址独占一行完整显示,不再和状态词挤在一起被省略号截断
|
|
494
|
-
const remoteAddr = panelEl.querySelector('#dsh-mios-remote-addr');
|
|
495
|
-
if (remoteAddr) {
|
|
496
|
-
const addr = tunnel && (tunnel.fixedUrl || tunnel.url);
|
|
497
|
-
remoteAddr.textContent = tunnel && tunnel.provisioned ? stripUrl(addr) : '—';
|
|
498
|
-
// 默认色交给 CSS 变量(跟随主题);只有解析异常时才覆盖成红色
|
|
499
|
-
remoteAddr.style.color = dnsBroken ? '#d94f3d' : '';
|
|
500
|
-
}
|
|
501
|
-
const mainBtn = panelEl.querySelector('#dsh-mios-remote-main');
|
|
502
|
-
if (mainBtn) {
|
|
503
|
-
// 出码态走绿色主按钮;开启态走灰色次按钮(悬浮变白),视觉上分清两件事
|
|
504
|
-
mainBtn.className = 'pairbtn' + (running || dnsBroken ? '' : ' tunnelbtn');
|
|
505
|
-
mainBtn.style.display = tunnel && tunnel.state !== 'disabled' ? '' : 'none';
|
|
506
|
-
// 关键:dnsBroken 不再禁用按钮。
|
|
507
|
-
// 以前 disabled = ... || dnsBroken,而唯一的重探入口就在点击之后 ——
|
|
508
|
-
// 按钮永远点不到,NS 改对了也不恢复,只能删状态文件(自锁死)。
|
|
509
|
-
mainBtn.disabled = tunnelBusy || bannerBusy.remote;
|
|
510
|
-
const needsInstall = tunnel.executable === 'missing';
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
const
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
);
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
const
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
const
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
ta.
|
|
828
|
-
ta.
|
|
829
|
-
ta.
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
box
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
'
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
)
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
catch {
|
|
1021
|
-
}
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
if (
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
}
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
const
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
showUpdateModal({
|
|
1266
|
-
title: '
|
|
1267
|
-
banner: { text: '
|
|
1268
|
-
lines: [],
|
|
1269
|
-
note:
|
|
1270
|
-
actions: [
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
},
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
},
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
{
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
*
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
const
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
}
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
//
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
}
|
|
1
|
+
// lib/client.js — 桌面端 UI 注入模块(X3)
|
|
2
|
+
//
|
|
3
|
+
// 契约(考古自社区 dsh-mobile 0.8.11 的 lib/client.js):
|
|
4
|
+
// 宿主 web app 按包的 `exports["./client"]` 加载本文件,浏览器里执行到
|
|
5
|
+
// `window.__ModuleLoader__.load({ id, factory })` 注册模块;
|
|
6
|
+
// factory 返回的模块需导出 `apply(ctx)` 与 `inject = ["slots"]`,
|
|
7
|
+
// 宿主把 slots 服务注入进来,UI 通过 slots 挂载。
|
|
8
|
+
//
|
|
9
|
+
// 我们挂两个东西:
|
|
10
|
+
// 1. 左下角侧栏 banner(slot: sidebar.footer.action)——手机图标 + 「iOS移动伴侣」
|
|
11
|
+
// + 连接状态点;窄栏(rail)只留图标。
|
|
12
|
+
// 2. 点击展开的状态面板(原生 DOM overlay,宿主未提供 react-dom)——
|
|
13
|
+
// 网关状态 / 适配器 / 已配对设备 / 隧道 / 生成配对码。
|
|
14
|
+
//
|
|
15
|
+
// 如实边界:网关端口被占会自动顺延(3081–3085 探测);面板数据来自
|
|
16
|
+
// /mobile/v1/desktop/summary(回环-only,无敏感字段),拿不到就明确显示
|
|
17
|
+
// 「网关不可达」,绝不编造状态。
|
|
18
|
+
window.__ModuleLoader__.load({
|
|
19
|
+
id: 'dsh-mobile-ios',
|
|
20
|
+
factory: (require) => {
|
|
21
|
+
var module = { exports: {} };
|
|
22
|
+
var exports = module.exports;
|
|
23
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
24
|
+
|
|
25
|
+
let react = require('react');
|
|
26
|
+
|
|
27
|
+
// ── 状态 ─────────────────────────────────────────────────────
|
|
28
|
+
const PORT_CANDIDATES = [3081, 3082, 3083, 3084, 3085];
|
|
29
|
+
let gatewayPort = null;
|
|
30
|
+
let summary = null; // /desktop/summary 结果或 null
|
|
31
|
+
let probing = false;
|
|
32
|
+
|
|
33
|
+
function baseUrl() {
|
|
34
|
+
return gatewayPort ? `http://127.0.0.1:${gatewayPort}` : null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function fetchJson(path, options, timeoutMs = 1500) {
|
|
38
|
+
const base = baseUrl();
|
|
39
|
+
if (!base) throw new Error('gateway-not-found');
|
|
40
|
+
const controller = new AbortController();
|
|
41
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
42
|
+
try {
|
|
43
|
+
const res = await fetch(base + path, { ...options, signal: controller.signal });
|
|
44
|
+
const body = await res.json();
|
|
45
|
+
if (!res.ok) throw Object.assign(new Error(body?.error?.message || res.statusText), { body });
|
|
46
|
+
return body;
|
|
47
|
+
} finally {
|
|
48
|
+
clearTimeout(timer);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 探测网关端口(顺延容忍),成功后拉一次摘要 */
|
|
53
|
+
async function probe() {
|
|
54
|
+
if (probing) return;
|
|
55
|
+
probing = true;
|
|
56
|
+
try {
|
|
57
|
+
let found = null;
|
|
58
|
+
for (const port of PORT_CANDIDATES) {
|
|
59
|
+
const controller = new AbortController();
|
|
60
|
+
const timer = setTimeout(() => controller.abort(), 700);
|
|
61
|
+
try {
|
|
62
|
+
const res = await fetch(`http://127.0.0.1:${port}/mobile/v1/health`, { signal: controller.signal });
|
|
63
|
+
if (res.ok) { found = port; break; }
|
|
64
|
+
} catch {} // 端口不是我们/没起,继续探
|
|
65
|
+
finally { clearTimeout(timer); }
|
|
66
|
+
}
|
|
67
|
+
// 必须跟着探到的结果走:只在成功时赋值、失败时清空。
|
|
68
|
+
// 以前失败会保留旧端口,网关换端口后一直去敲那个死端口(若被别的服务
|
|
69
|
+
// 占用还会解析出非 JSON),面板就一直红着。
|
|
70
|
+
gatewayPort = found;
|
|
71
|
+
if (gatewayPort) summary = await fetchJson('/mobile/v1/desktop/summary').catch(() => null);
|
|
72
|
+
else summary = null;
|
|
73
|
+
} finally {
|
|
74
|
+
probing = false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 面板打开时的轻量刷新:端口已经探到,只拉摘要并重绘,不再重复扫端口。 */
|
|
79
|
+
async function refreshSummary() {
|
|
80
|
+
if (!gatewayPort || summaryFetching) return;
|
|
81
|
+
summaryFetching = true;
|
|
82
|
+
try {
|
|
83
|
+
const next = await fetchJson('/mobile/v1/desktop/summary').catch(() => null);
|
|
84
|
+
if (next?.ok) {
|
|
85
|
+
summary = next;
|
|
86
|
+
renderSummary();
|
|
87
|
+
}
|
|
88
|
+
} finally {
|
|
89
|
+
summaryFetching = false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 首屏快速补探。
|
|
95
|
+
*
|
|
96
|
+
* 宿主重启时插件先于网关绑端口加载,第一次探测必然落空;
|
|
97
|
+
* 只靠 15 秒轮询会让面板红着不动,用户看到的就是「插件坏了」。
|
|
98
|
+
*/
|
|
99
|
+
async function probeUntilUp(attempts = 5, delayMs = 1200) {
|
|
100
|
+
for (let i = 0; i < attempts; i += 1) {
|
|
101
|
+
await probe();
|
|
102
|
+
if (summary && summary.ok) return;
|
|
103
|
+
if (i < attempts - 1) await new Promise((resolve) => { setTimeout(resolve, delayMs); });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** 生成配对码(本机动作,回环-only 端点)。route 显式选路:'lan' | 'tunnel' | undefined=自动 */
|
|
108
|
+
async function issuePairingCode(route) {
|
|
109
|
+
return fetchJson('/mobile/v1/pairing/code', {
|
|
110
|
+
method: 'POST',
|
|
111
|
+
headers: { 'content-type': 'application/json' },
|
|
112
|
+
body: JSON.stringify({ deviceName: '待连接设备', ...(route ? { route } : {}) }),
|
|
113
|
+
}, 2500);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** 隧道启停(本机管理面动作,回环-only 端点) */
|
|
117
|
+
/** 单台下线:管理面动作,与出码同样的本机约束,绝不走公网域名。 */
|
|
118
|
+
async function revokeDevice(deviceId) {
|
|
119
|
+
return fetchJson('/mobile/v1/pairing/revoke-device', {
|
|
120
|
+
method: 'POST',
|
|
121
|
+
headers: { 'content-type': 'application/json' },
|
|
122
|
+
body: JSON.stringify({ deviceId }),
|
|
123
|
+
}, 2500);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function tunnelAction(action) {
|
|
127
|
+
// start 首次会顺带「开通固定地址 + 等公网注册 + DNS 复查」,实测经常超过 20s;
|
|
128
|
+
// 以前 20s 就掐断,用户点第一次报错、点第二次才出码——就是这里太短。
|
|
129
|
+
return fetchJson(`/mobile/v1/tunnel/${action}`, { method: 'POST' }, action === 'start' ? 60000 : 25000);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ── 样式 ─────────────────────────────────────────────────────
|
|
133
|
+
const STYLE_ID = 'dsh-mios-style';
|
|
134
|
+
function installStyle() {
|
|
135
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
136
|
+
const style = document.createElement('style');
|
|
137
|
+
style.id = STYLE_ID;
|
|
138
|
+
// 主题跟随宿主:颜色全部走 CSS 变量,.light 类由 detectHostTheme() 动态切换。
|
|
139
|
+
// 以前全硬编码深色,宿主开浅色主题时面板像一块黑补丁(2026-11-18 真机反馈)。
|
|
140
|
+
style.textContent = `
|
|
141
|
+
.dsh-mios-trigger { display: inline-flex; align-items: center; gap: 7px; background: none;
|
|
142
|
+
border: none; color: inherit; cursor: pointer; padding: 6px 8px; border-radius: 8px;
|
|
143
|
+
font: inherit; line-height: 1; }
|
|
144
|
+
.dsh-mios-trigger:hover { background: rgba(255,255,255,.08); }
|
|
145
|
+
.dsh-mios-trigger.is-rail { padding: 8px; }
|
|
146
|
+
.dsh-mios-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
|
|
147
|
+
.dsh-mios-dot.ok { background: #3fb27f; }
|
|
148
|
+
.dsh-mios-dot.down { background: #d94f3d; }
|
|
149
|
+
.dsh-mios-panel { --bg: #171b24; --fg: #e8eaf0; --muted: #8b93a3; --border: #303a52;
|
|
150
|
+
--border-hover: #4a5678; --shadow: rgba(0,0,0,.5); --fg-soft: rgba(232,234,240,.7);
|
|
151
|
+
--muted-border: rgba(139,147,163,.45); --muted-bg: rgba(139,147,163,.12);
|
|
152
|
+
--muted-bg-hover: rgba(139,147,163,.2); }
|
|
153
|
+
.dsh-mios-panel.light { --bg: #ffffff; --fg: #1f2430; --muted: #6b7280; --border: #d9dee8;
|
|
154
|
+
--border-hover: #9aa5bd; --shadow: rgba(31,36,48,.18); --fg-soft: rgba(31,36,48,.7);
|
|
155
|
+
--muted-border: rgba(107,114,128,.4); --muted-bg: rgba(107,114,128,.1);
|
|
156
|
+
--muted-bg-hover: rgba(107,114,128,.18); }
|
|
157
|
+
.dsh-mios-panel { position: fixed; left: 14px; bottom: 52px; width: 300px; z-index: 99990;
|
|
158
|
+
background: var(--bg); color: var(--fg); border: 1px solid var(--border); border-radius: 14px;
|
|
159
|
+
box-shadow: 0 14px 44px var(--shadow); font: 13px/1.6 -apple-system, "PingFang SC", sans-serif;
|
|
160
|
+
padding: 14px; }
|
|
161
|
+
.dsh-mios-panel h3 { margin: 0 0 10px; font-size: 14px; display: flex; align-items: center; gap: 8px; }
|
|
162
|
+
.dsh-mios-upd { margin-left: auto; background: none; border: 1px solid var(--border); border-radius: 7px;
|
|
163
|
+
color: var(--muted); font-family: inherit; font-size: 11px; font-weight: 500; line-height: 1.4;
|
|
164
|
+
cursor: pointer; padding: 3px 8px; }
|
|
165
|
+
.dsh-mios-upd:hover { color: var(--fg); border-color: var(--border-hover); }
|
|
166
|
+
.dsh-mios-modal-mask { --modal-bg:#1c2230; --modal-fg:#e8eaf0; --modal-muted:#8b93a3;
|
|
167
|
+
--modal-border:#3a4560; --modal-btn:rgba(139,147,163,.12); --modal-accent:#3fb27f; --modal-shadow:rgba(0,0,0,.6);
|
|
168
|
+
position:fixed; inset:0; z-index:2147483000; background:var(--modal-shadow);
|
|
169
|
+
display:flex; align-items:center; justify-content:center; padding:20px;
|
|
170
|
+
font-family:-apple-system, "PingFang SC", sans-serif; font-size:13px; line-height:1.6; }
|
|
171
|
+
.dsh-mios-modal-mask.light { --modal-bg:#ffffff; --modal-fg:#1f2430; --modal-muted:#6b7280;
|
|
172
|
+
--modal-border:#d9dee8; --modal-btn:rgba(107,114,128,.1); --modal-accent:#2f9d70; --modal-shadow:rgba(31,36,48,.35); }
|
|
173
|
+
.dsh-mios-modal { width:100%; max-width:340px; box-sizing:border-box; background:var(--modal-bg);
|
|
174
|
+
color:var(--modal-fg); border:1px solid var(--modal-border); border-radius:14px; padding:18px;
|
|
175
|
+
box-shadow:0 18px 60px var(--modal-shadow); }
|
|
176
|
+
.dsh-mios-modal h4 { margin:0 0 12px; font-size:15px; display:flex; align-items:center; gap:8px; }
|
|
177
|
+
.dsh-mios-modal .modal-banner { margin:0 0 12px; padding:9px 11px; border-radius:9px; font-size:12px;
|
|
178
|
+
font-weight:600; line-height:1.5; background:rgba(63,178,127,.12); color:var(--modal-accent);
|
|
179
|
+
border:1px solid var(--modal-accent); }
|
|
180
|
+
.dsh-mios-modal .modal-banner.success { background:rgba(63,178,127,.16); color:#2f9d70; }
|
|
181
|
+
.dsh-mios-modal .modal-banner.warn { background:rgba(224,160,60,.14); color:#d18a26; border-color:#d18a26; }
|
|
182
|
+
.dsh-mios-modal .modal-banner.error { background:rgba(217,79,61,.14); color:#d94f3d; border-color:#d94f3d; }
|
|
183
|
+
.dsh-mios-modal .modal-x { margin-left:auto; background:none; border:none; color:var(--modal-muted);
|
|
184
|
+
font-size:16px; cursor:pointer; line-height:1; padding:2px 4px; }
|
|
185
|
+
.dsh-mios-modal .modal-line { display:flex; justify-content:space-between; gap:10px;
|
|
186
|
+
font-size:12px; color:var(--modal-muted); margin:6px 0; }
|
|
187
|
+
.dsh-mios-modal .modal-line b { color:var(--modal-fg); font-weight:600; max-width:200px;
|
|
188
|
+
overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
|
189
|
+
.dsh-mios-modal .modal-line b.new { color:var(--modal-accent); }
|
|
190
|
+
.dsh-mios-modal .modal-note { margin:12px 0 4px; font-size:11px; color:var(--modal-muted); line-height:1.6; }
|
|
191
|
+
.dsh-mios-modal .modal-actions { display:flex; gap:8px; margin-top:14px; }
|
|
192
|
+
.dsh-mios-modal .modal-actions button { flex:1; padding:8px 0; border-radius:8px; cursor:pointer;
|
|
193
|
+
font-family:inherit; font-size:12px; font-weight:600; line-height:1.4;
|
|
194
|
+
border:1px solid var(--modal-border); background:var(--modal-btn); color:var(--modal-fg); }
|
|
195
|
+
.dsh-mios-modal .modal-actions button.primary { border-color:var(--modal-accent);
|
|
196
|
+
background:rgba(63,178,127,.16); color:var(--modal-accent); }
|
|
197
|
+
.dsh-mios-modal .modal-actions button:disabled { opacity:.6; cursor:default; }
|
|
198
|
+
/* 复制结果必须看得见:dsh-app:// 页面里剪贴板可能不可用,只改按钮文字容易被无视。
|
|
199
|
+
.modal-toast 常驻在动作条上方,成功/失败都留痕。 */
|
|
200
|
+
.dsh-mios-modal .modal-toast { margin:10px 0 0; padding:7px 10px; border-radius:8px; font-size:11px;
|
|
201
|
+
font-weight:600; line-height:1.5; background:rgba(139,147,163,.14); color:var(--modal-muted); }
|
|
202
|
+
.dsh-mios-modal .modal-toast.success { background:rgba(63,178,127,.16); color:#2f9d70; }
|
|
203
|
+
.dsh-mios-modal .modal-toast.error { background:rgba(217,79,61,.14); color:#d94f3d; }
|
|
204
|
+
/* 复制全线失败时的兜底:给一个已自动全选的输入框,用户 Ctrl/⌘+C 就能拿走 */
|
|
205
|
+
.dsh-mios-modal .modal-pick { width:100%; box-sizing:border-box; margin:10px 0 0; padding:8px 10px;
|
|
206
|
+
border-radius:8px; border:1px solid var(--modal-accent); background:rgba(63,178,127,.10);
|
|
207
|
+
color:var(--modal-fg); font-family:ui-monospace, SFMono-Regular, Menlo, monospace; font-size:12px;
|
|
208
|
+
user-select:text; -webkit-user-select:text; }
|
|
209
|
+
.dsh-mios-modal .modal-steps { margin:10px 0 0; padding-left:18px; font-size:11px;
|
|
210
|
+
color:var(--modal-muted); line-height:1.7; }
|
|
211
|
+
.dsh-mios-modal .modal-steps li { margin:2px 0; }
|
|
212
|
+
.dsh-mios-panel .row { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); margin: 3px 0; }
|
|
213
|
+
.dsh-mios-panel .row b { color: var(--fg); font-weight: 600; text-align: right; max-width: 170px;
|
|
214
|
+
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
215
|
+
.dsh-mios-device-toggle { background: none; border: none; color: var(--muted); cursor: pointer;
|
|
216
|
+
font: inherit; padding: 0; display: inline-flex; align-items: center; gap: 5px; }
|
|
217
|
+
.dsh-mios-device-toggle:hover { color: var(--fg); }
|
|
218
|
+
.dsh-mios-devices { margin: 2px 0 6px; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
|
|
219
|
+
.dsh-mios-devices[hidden] { display: none; }
|
|
220
|
+
.dsh-mios-device-row { display: flex; align-items: center; gap: 8px; padding: 5px 10px;
|
|
221
|
+
font-size: 11px; color: var(--muted); border-top: 1px solid var(--border); }
|
|
222
|
+
.dsh-mios-device-row:first-child { border-top: none; }
|
|
223
|
+
.dsh-mios-device-name { flex: 1; min-width: 0; overflow-wrap: anywhere; white-space: normal;
|
|
224
|
+
line-height: 1.35; color: var(--fg); }
|
|
225
|
+
.dsh-mios-device-online { color: #3fb27f; flex: none; }
|
|
226
|
+
.dsh-mios-device-offline { color: var(--muted); flex: none; }
|
|
227
|
+
.dsh-mios-device-kick { margin-left: auto; background: none; border: 1px solid rgba(217,79,61,.4); border-radius: 4px;
|
|
228
|
+
color: #c97a6d; cursor: pointer; font-family: inherit; font-size: 9px; font-weight: 500;
|
|
229
|
+
line-height: 1.3; padding: 1px 5px; flex: none; }
|
|
230
|
+
.dsh-mios-device-kick:hover { color: #d94f3d; border-color: rgba(217,79,61,.7); }
|
|
231
|
+
.dsh-mios-devices-empty { padding: 6px 8px; font-size: 11px; color: var(--muted); }
|
|
232
|
+
.dsh-mios-panel .pairbtn { width: 100%; margin-top: 10px; padding: 8px 0; border-radius: 9px;
|
|
233
|
+
border: 1px solid rgba(63,178,127,.45); background: rgba(63,178,127,.14); color: #3fb27f;
|
|
234
|
+
font: 600 13px/1.4 inherit-font, sans-serif; cursor: pointer; }
|
|
235
|
+
.dsh-mios-panel .pairbtn:disabled { opacity: .5; cursor: default; }
|
|
236
|
+
.dsh-mios-banner { border: 1px solid var(--border); border-radius: 10px; padding: 10px 10px 11px; margin-top: 10px; }
|
|
237
|
+
.dsh-mios-banner h4 { margin: 0; font-size: 12px; font-weight: 600; color: var(--fg);
|
|
238
|
+
display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
|
|
239
|
+
.dsh-mios-banner .banner-state { font-size: 11px; font-weight: 400; color: var(--muted);
|
|
240
|
+
max-width: 190px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
241
|
+
.dsh-mios-banner .banner-guide { margin-top: 4px; font-size: 11px; color: var(--muted); line-height: 1.5; }
|
|
242
|
+
.dsh-mios-banner .banner-addr { margin-top: 4px; font-size: 11px; color: var(--muted);
|
|
243
|
+
font-family: ui-monospace, "SF Mono", Menlo, monospace; word-break: break-all; line-height: 1.5; }
|
|
244
|
+
.dsh-mios-banner .pairbtn { margin-top: 8px; }
|
|
245
|
+
.dsh-mios-banner .tunnelbtn { border-color: var(--muted-border); background: var(--muted-bg); color: var(--muted); }
|
|
246
|
+
.dsh-mios-banner .tunnelbtn:hover:not(:disabled) { border-color: var(--fg-soft); color: var(--fg); background: var(--muted-bg-hover); }
|
|
247
|
+
.dsh-mios-banner .tunnelbtn:disabled { opacity: .5; cursor: default; }
|
|
248
|
+
.dsh-mios-banner .stopbtn { margin-top: 6px; padding: 5px 0; font-weight: 500;
|
|
249
|
+
border-color: rgba(217,79,61,.4); background: none; color: #c97a6d; }
|
|
250
|
+
.dsh-mios-banner .stopbtn:hover:not(:disabled) { border-color: rgba(217,79,61,.7); color: #d94f3d; }
|
|
251
|
+
.dsh-mios-panel .code { margin-top: 10px; text-align: center; font-size: 30px; letter-spacing: 8px;
|
|
252
|
+
font-weight: 700; color: var(--fg); font-variant-numeric: tabular-nums; }
|
|
253
|
+
.dsh-mios-qr { margin-top: 10px; padding: 10px; background: #fff; border-radius: 10px;
|
|
254
|
+
display: flex; justify-content: center; }
|
|
255
|
+
.dsh-mios-qr svg { width: 180px; height: 180px; display: block; }
|
|
256
|
+
.dsh-mios-panel .err { margin-top: 10px; font-size: 12px; color: #d94f3d; text-align: center; }
|
|
257
|
+
.dsh-mios-panel footer { margin-top: 10px; font-size: 11px; color: var(--muted); text-align: center; }
|
|
258
|
+
.dsh-mios-close { margin-left: auto; background: none; border: none; color: var(--muted);
|
|
259
|
+
font-size: 16px; cursor: pointer; padding: 2px 6px; }
|
|
260
|
+
`;
|
|
261
|
+
document.head.append(style);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ── 面板(原生 DOM;宿主无 react-dom) ───────────────────────
|
|
265
|
+
// 双 banner 布局:上面「局域网连接」,下面「远程通道连接」。
|
|
266
|
+
// 远程 banner 是合并后的单一主按钮:没开过→开通;开过没跑→开启并出码;
|
|
267
|
+
// 跑着→直接出码。停止远程是旁边的小按钮,只在该停的时候出现。
|
|
268
|
+
const PANEL_TITLE = 'DSH iOS移动伴侣';
|
|
269
|
+
const BANNER_LABEL = '📱 iOS移动伴侣';
|
|
270
|
+
// 插件更新检查:读 npm registry 的 latest(与「添加插件」安装源同源)。
|
|
271
|
+
// npm 官方源带 CORS *,桌面端 webview 可直接 fetch;npmmirror 作国内备用。
|
|
272
|
+
const PLUGIN_NAME = 'dsh-mobile-ios';
|
|
273
|
+
const PLUGIN_REGISTRIES = [
|
|
274
|
+
'https://registry.npmjs.org',
|
|
275
|
+
'https://registry.npmmirror.com',
|
|
276
|
+
];
|
|
277
|
+
const stripUrl = (u) => String(u || '').replace(/^https?:\/\//, '');
|
|
278
|
+
const stripLocal = (h) => String(h || '').replace(/\.local$/i, '');
|
|
279
|
+
|
|
280
|
+
// 宿主插件管理服务(remote.pluginManager)。注入模块经 apply 拿到;
|
|
281
|
+
// 非 Desktop 或老宿主拿不到时保持 null,更新入口降级为「复制包名」手动安装。
|
|
282
|
+
let pluginManager = null;
|
|
283
|
+
let updateInFlight = false; // 一步更新的并发锁:更新中禁止再次触发
|
|
284
|
+
let panelEl = null;
|
|
285
|
+
let tunnelBusy = false;
|
|
286
|
+
const bannerBusy = { lan: false, remote: false };
|
|
287
|
+
let outsideCloser = null;
|
|
288
|
+
let devicesExpanded = false;
|
|
289
|
+
let kickingDeviceId = null;
|
|
290
|
+
// 面板打开期间的摘要刷新:probe() 只更新数据不重绘,正是「扫码后要关掉重开才更新」的根因。
|
|
291
|
+
let summaryPollTimer = null;
|
|
292
|
+
let summaryFetching = false;
|
|
293
|
+
let lastDeviceSignature = null;
|
|
294
|
+
|
|
295
|
+
/** 探测宿主主题:显式标记(class/data-theme)→ body 亮度启发 → 系统偏好。
|
|
296
|
+
* Electron 宿主切换主题只改页面 class,matchMedia 不一定跟着变,所以三级都查。 */
|
|
297
|
+
function detectHostTheme() {
|
|
298
|
+
const root = document.documentElement;
|
|
299
|
+
const body = document.body;
|
|
300
|
+
const marked = `${root.className} ${body?.className || ''} ${root.getAttribute('data-theme') || ''} ${body?.getAttribute('data-theme') || ''} ${root.getAttribute('data-color-mode') || ''}`.toLowerCase();
|
|
301
|
+
if (/\bdark\b/.test(marked)) return 'dark';
|
|
302
|
+
if (/\blight\b/.test(marked)) return 'light';
|
|
303
|
+
// 亮度启发:宿主页面底色亮就是浅色主题(面板挂在这个页面上,跟它一致最直观)。
|
|
304
|
+
// 全黑或全透明底色不可判,落到系统偏好。
|
|
305
|
+
const bgc = body ? getComputedStyle(body).backgroundColor
|
|
306
|
+
: getComputedStyle(root).backgroundColor;
|
|
307
|
+
const m = bgc.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
|
|
308
|
+
if (m && (+m[1] + +m[2] + +m[3]) > 0) {
|
|
309
|
+
return (0.2126 * +m[1] + 0.7152 * +m[2] + 0.0722 * +m[3]) > 140 ? 'light' : 'dark';
|
|
310
|
+
}
|
|
311
|
+
return window.matchMedia?.('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** 面板主题实时跟随宿主:每次渲染都重判(用户可能中途切主题)。 */
|
|
315
|
+
function applyPanelTheme() {
|
|
316
|
+
if (!panelEl) return;
|
|
317
|
+
panelEl.classList.toggle('light', detectHostTheme() === 'light');
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function removePanel() {
|
|
321
|
+
panelEl?.remove();
|
|
322
|
+
panelEl = null;
|
|
323
|
+
closeUpdateModal();
|
|
324
|
+
if (summaryPollTimer) {
|
|
325
|
+
clearInterval(summaryPollTimer);
|
|
326
|
+
summaryPollTimer = null;
|
|
327
|
+
}
|
|
328
|
+
lastDeviceSignature = null;
|
|
329
|
+
if (outsideCloser) {
|
|
330
|
+
document.removeEventListener('pointerdown', outsideCloser);
|
|
331
|
+
outsideCloser = null;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function row(label, valueId) {
|
|
336
|
+
const div = document.createElement('div');
|
|
337
|
+
div.className = 'row';
|
|
338
|
+
const l = document.createElement('span');
|
|
339
|
+
l.textContent = label;
|
|
340
|
+
const v = document.createElement('b');
|
|
341
|
+
v.id = valueId;
|
|
342
|
+
v.textContent = '—';
|
|
343
|
+
div.append(l, v);
|
|
344
|
+
return div;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** 「同时在线设备」行:标签可点开展开在线设备清单,右侧显示 在线/上限。 */
|
|
348
|
+
function deviceRows() {
|
|
349
|
+
const wrap = document.createElement('div');
|
|
350
|
+
const head = document.createElement('div');
|
|
351
|
+
head.className = 'row';
|
|
352
|
+
const toggle = document.createElement('button');
|
|
353
|
+
toggle.className = 'dsh-mios-device-toggle';
|
|
354
|
+
toggle.id = 'dsh-mios-device-toggle';
|
|
355
|
+
toggle.type = 'button';
|
|
356
|
+
toggle.textContent = '▸';
|
|
357
|
+
toggle.setAttribute('aria-label', '展开或收起当前在线设备清单');
|
|
358
|
+
const label = document.createElement('span');
|
|
359
|
+
label.textContent = '同时在线设备(最多5台)';
|
|
360
|
+
const left = document.createElement('span');
|
|
361
|
+
left.append(toggle, label);
|
|
362
|
+
const value = document.createElement('b');
|
|
363
|
+
value.id = 'dsh-mios-devices';
|
|
364
|
+
value.textContent = '—';
|
|
365
|
+
head.append(left, value);
|
|
366
|
+
toggle.addEventListener('click', () => {
|
|
367
|
+
devicesExpanded = !devicesExpanded;
|
|
368
|
+
renderDeviceList();
|
|
369
|
+
});
|
|
370
|
+
const list = document.createElement('div');
|
|
371
|
+
list.className = 'dsh-mios-devices';
|
|
372
|
+
list.id = 'dsh-mios-devices-list';
|
|
373
|
+
list.hidden = true;
|
|
374
|
+
wrap.append(head, list);
|
|
375
|
+
return wrap;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** 在线设备清单:默认折叠;只列当前在线设备(同时在线上限 5 台),下线逐台二次确认。 */
|
|
379
|
+
function renderDeviceList() {
|
|
380
|
+
const box = panelEl?.querySelector('#dsh-mios-devices-list');
|
|
381
|
+
if (!box) return;
|
|
382
|
+
box.hidden = !devicesExpanded;
|
|
383
|
+
const toggle = panelEl.querySelector('#dsh-mios-device-toggle');
|
|
384
|
+
if (toggle) toggle.textContent = devicesExpanded ? '▾' : '▸';
|
|
385
|
+
box.replaceChildren();
|
|
386
|
+
if (!summary?.ok) return;
|
|
387
|
+
// 后端已限制活跃设备上限;这里再按在线态筛选并截到 5,保证面板只展示真实并发连接。
|
|
388
|
+
const devices = (summary.pairing?.devices || []).filter((d) => d.online).slice(0, 5);
|
|
389
|
+
if (!devices.length) {
|
|
390
|
+
const empty = document.createElement('div');
|
|
391
|
+
empty.className = 'dsh-mios-devices-empty';
|
|
392
|
+
empty.textContent = '当前没有在线设备';
|
|
393
|
+
box.append(empty);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
for (const device of devices) {
|
|
397
|
+
const line = document.createElement('div');
|
|
398
|
+
line.className = 'dsh-mios-device-row';
|
|
399
|
+
const name = document.createElement('span');
|
|
400
|
+
name.className = 'dsh-mios-device-name';
|
|
401
|
+
name.textContent = device.deviceName || '未命名设备';
|
|
402
|
+
name.title = name.textContent;
|
|
403
|
+
const state = document.createElement('span');
|
|
404
|
+
state.className = device.online ? 'dsh-mios-device-online' : 'dsh-mios-device-offline';
|
|
405
|
+
state.textContent = device.online ? '在线' : '离线';
|
|
406
|
+
const kick = document.createElement('button');
|
|
407
|
+
kick.className = 'dsh-mios-device-kick';
|
|
408
|
+
kick.textContent = kickingDeviceId === device.deviceId ? '处理中' : '下线';
|
|
409
|
+
kick.disabled = kickingDeviceId !== null;
|
|
410
|
+
kick.title = `将 ${name.textContent} 从这台电脑移除`;
|
|
411
|
+
kick.addEventListener('click', async () => {
|
|
412
|
+
// 二次确认:下线会让那台手机立刻失去读任务的能力,不能误点。
|
|
413
|
+
if (!window.confirm(`确定让「${name.textContent}」下线?该设备需要重新扫码才能再次连接。`)) return;
|
|
414
|
+
kickingDeviceId = device.deviceId;
|
|
415
|
+
renderDeviceList();
|
|
416
|
+
try {
|
|
417
|
+
await revokeDevice(device.deviceId);
|
|
418
|
+
await probe();
|
|
419
|
+
} catch (err) {
|
|
420
|
+
window.alert('下线失败:' + (err?.body?.error?.message || err.message));
|
|
421
|
+
} finally {
|
|
422
|
+
kickingDeviceId = null;
|
|
423
|
+
renderSummary();
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
line.append(name, state, kick);
|
|
427
|
+
box.append(line);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function renderSummary() {
|
|
432
|
+
if (!panelEl) return;
|
|
433
|
+
applyPanelTheme();
|
|
434
|
+
const dot = panelEl.querySelector('#dsh-mios-panel-dot');
|
|
435
|
+
const ok = !!(summary && summary.ok);
|
|
436
|
+
dot.className = 'dsh-mios-dot ' + (ok ? 'ok' : 'down');
|
|
437
|
+
const set = (id, text) => { const el = panelEl.querySelector('#' + id); if (el) el.textContent = text; };
|
|
438
|
+
set('dsh-mios-adapter', ok ? `v${summary.version}` : '—');
|
|
439
|
+
set('dsh-mios-host', ok ? (stripLocal(summary.hostName) || '未上报') : '—');
|
|
440
|
+
if (ok) {
|
|
441
|
+
const max = summary.pairing?.maxDevices ?? 5;
|
|
442
|
+
const online = summary.pairing?.onlineDevices ?? 0;
|
|
443
|
+
set('dsh-mios-devices', `${online}/${max} 台`);
|
|
444
|
+
} else {
|
|
445
|
+
set('dsh-mios-devices', '—');
|
|
446
|
+
}
|
|
447
|
+
// 只有设备名/在线态/顺序变化才重建列表:1.2s 刷新不能让 hover 提示和下线按钮闪烁。
|
|
448
|
+
const devices = ok ? (summary.pairing?.devices || []) : [];
|
|
449
|
+
const signature = devices
|
|
450
|
+
.map((d) => `${d.deviceId}\u0000${d.deviceName || ''}\u0000${d.online ? 1 : 0}`)
|
|
451
|
+
.join('\u0001');
|
|
452
|
+
if (signature !== lastDeviceSignature) {
|
|
453
|
+
lastDeviceSignature = signature;
|
|
454
|
+
renderDeviceList();
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// ── 局域网 banner ──
|
|
458
|
+
const lan = ok ? summary.lan : null;
|
|
459
|
+
const lanState = panelEl.querySelector('#dsh-mios-lan-state');
|
|
460
|
+
if (lanState) lanState.textContent = ok ? (lan ? `电脑地址 ${lan}` : '未获取到局域网地址') : '网关不可达';
|
|
461
|
+
const lanCodeBtn = panelEl.querySelector('#dsh-mios-lan-code');
|
|
462
|
+
if (lanCodeBtn) {
|
|
463
|
+
lanCodeBtn.disabled = !ok || !lan || bannerBusy.lan;
|
|
464
|
+
lanCodeBtn.title = ok && lan ? '' : '网关不可达或没有局域网地址';
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// ── 远程通道 banner ──
|
|
468
|
+
// 四种事实分开说:运行中 / 已开通但没开 / 根本没开通 / 域名没生效。
|
|
469
|
+
// 「域名未生效」必须单列:地址开出来了但公网解析不到,显示「已开通」会误导人去扫码。
|
|
470
|
+
const tunnel = ok ? summary.tunnel : null;
|
|
471
|
+
const dnsBroken = tunnel && tunnel.dnsOk === false;
|
|
472
|
+
// 解析失败已经归因到 zone 层时,别再用「解析异常」这种症状词:
|
|
473
|
+
// moved 是归属冲突(改 NS 或换账号才能解),重探一万次也不会变。
|
|
474
|
+
const zoneCode = tunnel?.zone?.code || null;
|
|
475
|
+
// 「进程在跑」与「地址可用」是两件事:域名没生效时隧道进程可能已经起来了,
|
|
476
|
+
// 但扫码必然连不上,所以 running 仍然要求 dnsOk 不为 false。
|
|
477
|
+
const processUp = !!(tunnel && tunnel.state === 'running');
|
|
478
|
+
const running = processUp && !dnsBroken;
|
|
479
|
+
const remoteState = panelEl.querySelector('#dsh-mios-remote-state');
|
|
480
|
+
if (remoteState) {
|
|
481
|
+
remoteState.textContent = !tunnel
|
|
482
|
+
? '—'
|
|
483
|
+
: dnsBroken
|
|
484
|
+
? (zoneCode === 'moved'
|
|
485
|
+
? '域名归属冲突'
|
|
486
|
+
: processUp ? '已开启 · 域名未生效' : '解析异常 · 未开启')
|
|
487
|
+
: running
|
|
488
|
+
? '已开启'
|
|
489
|
+
: tunnel.provisioned && tunnel.fixedUrl
|
|
490
|
+
? '已开通 · 未开启'
|
|
491
|
+
: tunnel.mode === 'named' ? '未开通固定地址' : tunnel.state;
|
|
492
|
+
}
|
|
493
|
+
// 固定地址独占一行完整显示,不再和状态词挤在一起被省略号截断
|
|
494
|
+
const remoteAddr = panelEl.querySelector('#dsh-mios-remote-addr');
|
|
495
|
+
if (remoteAddr) {
|
|
496
|
+
const addr = tunnel && (tunnel.fixedUrl || tunnel.url);
|
|
497
|
+
remoteAddr.textContent = tunnel && tunnel.provisioned ? stripUrl(addr) : '—';
|
|
498
|
+
// 默认色交给 CSS 变量(跟随主题);只有解析异常时才覆盖成红色
|
|
499
|
+
remoteAddr.style.color = dnsBroken ? '#d94f3d' : '';
|
|
500
|
+
}
|
|
501
|
+
const mainBtn = panelEl.querySelector('#dsh-mios-remote-main');
|
|
502
|
+
if (mainBtn) {
|
|
503
|
+
// 出码态走绿色主按钮;开启态走灰色次按钮(悬浮变白),视觉上分清两件事
|
|
504
|
+
mainBtn.className = 'pairbtn' + (running || dnsBroken ? '' : ' tunnelbtn');
|
|
505
|
+
mainBtn.style.display = tunnel && tunnel.state !== 'disabled' ? '' : 'none';
|
|
506
|
+
// 关键:dnsBroken 不再禁用按钮。
|
|
507
|
+
// 以前 disabled = ... || dnsBroken,而唯一的重探入口就在点击之后 ——
|
|
508
|
+
// 按钮永远点不到,NS 改对了也不恢复,只能删状态文件(自锁死)。
|
|
509
|
+
mainBtn.disabled = tunnelBusy || bannerBusy.remote;
|
|
510
|
+
const needsInstall = tunnel.executable === 'missing';
|
|
511
|
+
// 凭据确定不可用时先把「换账号登录」摆在前面:此刻点开启必然在写 DNS 处失败,
|
|
512
|
+
// 直接给可执行的出口,比让人反复点一个注定失败的按钮有用。
|
|
513
|
+
const credBlocked = Boolean(tunnel.credentials?.blocking) && !running && !needsInstall;
|
|
514
|
+
mainBtn.textContent = needsInstall && tunnel.canAutoInstall
|
|
515
|
+
? '安装 cloudflared 并开启远程'
|
|
516
|
+
: credBlocked
|
|
517
|
+
? '开启远程(需重新授权)'
|
|
518
|
+
: dnsBroken
|
|
519
|
+
? (zoneCode === 'moved' ? '查看原因(域名被占用)' : '重新检测域名解析')
|
|
520
|
+
: running
|
|
521
|
+
? (bannerBusy.remote ? '生成中…' : '生成配对码(远程)')
|
|
522
|
+
: tunnel.mode === 'named'
|
|
523
|
+
? (tunnel.provisioned ? '开启远程并生成配对码' : '开启远程(分配固定地址)')
|
|
524
|
+
: '启动隧道(外网连接)';
|
|
525
|
+
}
|
|
526
|
+
const stopBtn = panelEl.querySelector('#dsh-mios-remote-stop');
|
|
527
|
+
if (stopBtn) {
|
|
528
|
+
// 停止按钮只在该停的时候出现:没开过/没在跑都没有「停」可言。
|
|
529
|
+
// 域名未生效但进程在跑时仍要给「停止」——否则用户无法收尾。
|
|
530
|
+
stopBtn.style.display = processUp && !tunnelBusy ? '' : 'none';
|
|
531
|
+
stopBtn.disabled = tunnelBusy;
|
|
532
|
+
}
|
|
533
|
+
// 远程码跟着可用性走:不是 running 就收起(与「停止即收码」同一语义,
|
|
534
|
+
// 兜住任何没经过停止按钮的路径,比如进程意外退出)。
|
|
535
|
+
if (!running && !bannerBusy.remote) {
|
|
536
|
+
panelEl.querySelector('#dsh-mios-remote-main')?.closest('.dsh-mios-banner')
|
|
537
|
+
?.querySelectorAll('.dsh-mios-code-slot, .err').forEach((e) => e.remove());
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// 提示行承载告警与错误:warning 优先,其次是 tunnel.error。
|
|
541
|
+
// 以前只读 warning,服务端明确报的 error(如「主机未安装 cloudflared」)
|
|
542
|
+
// 被 1.2s 一次的刷新瞬间清空,用户点了按钮只看到一闪而过、误以为「无反应」。
|
|
543
|
+
const hint = panelEl.querySelector('#dsh-mios-hint');
|
|
544
|
+
if (hint) {
|
|
545
|
+
const t = ok ? summary.tunnel : null;
|
|
546
|
+
// 凭据预检不通过是「确定写不了 DNS」:要在用户点按钮之前就说,
|
|
547
|
+
// 而不是等他点了、写失败了,再看到一句 cloudflared 的命令回显。
|
|
548
|
+
const credBlocked = t?.credentials?.blocking ? t.credentials : null;
|
|
549
|
+
const msg = t?.warning || t?.error || credBlocked?.hint || null;
|
|
550
|
+
hint.textContent = msg ? `⚠️ ${msg}` : '';
|
|
551
|
+
hint.style.color = t?.error && !t?.warning ? '#d94f3d' : '#e0a03c';
|
|
552
|
+
hint.style.display = msg ? '' : 'none';
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** 双 banner 里的一块:标题 + 状态行 + 指引一句 + 内容区 */
|
|
557
|
+
function bannerBox(title, stateId, guideText) {
|
|
558
|
+
const box = document.createElement('div');
|
|
559
|
+
box.className = 'dsh-mios-banner';
|
|
560
|
+
const head = document.createElement('h4');
|
|
561
|
+
head.textContent = title;
|
|
562
|
+
const state = document.createElement('span');
|
|
563
|
+
state.className = 'banner-state';
|
|
564
|
+
state.id = stateId;
|
|
565
|
+
state.textContent = '—';
|
|
566
|
+
head.append(state);
|
|
567
|
+
const guide = document.createElement('div');
|
|
568
|
+
guide.className = 'banner-guide';
|
|
569
|
+
guide.textContent = guideText;
|
|
570
|
+
box.append(head, guide);
|
|
571
|
+
return box;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/** 出码结果挂进所属 banner:QR + 码 + 地址说明(同一 banner 只保留最新一码) */
|
|
575
|
+
function mountCode(banner, issued) {
|
|
576
|
+
banner.querySelectorAll('.dsh-mios-code-slot').forEach((e) => e.remove());
|
|
577
|
+
const slot = document.createElement('div');
|
|
578
|
+
slot.className = 'dsh-mios-code-slot';
|
|
579
|
+
if (issued.qrSvg) {
|
|
580
|
+
const qr = document.createElement('div');
|
|
581
|
+
qr.className = 'dsh-mios-qr';
|
|
582
|
+
qr.innerHTML = issued.qrSvg;
|
|
583
|
+
slot.append(qr);
|
|
584
|
+
}
|
|
585
|
+
const code = document.createElement('div');
|
|
586
|
+
code.className = 'code';
|
|
587
|
+
code.textContent = issued.code;
|
|
588
|
+
const addr = document.createElement('div');
|
|
589
|
+
addr.style.cssText = 'margin-top:6px;text-align:center;font-size:12px;color:#3fb27f;';
|
|
590
|
+
addr.textContent = issued.route === 'tunnel'
|
|
591
|
+
? `扫码连接 · 公网固定地址 ${stripUrl(issued.address)}`
|
|
592
|
+
: issued.address
|
|
593
|
+
? `扫码连接 · 局域网 ${issued.address}`
|
|
594
|
+
: '手机填电脑局域网地址 + 此码';
|
|
595
|
+
slot.append(code, addr);
|
|
596
|
+
banner.append(slot);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/** banner 内的出码按钮统一行为:busy → 出码 → 失败在 banner 内显错 */
|
|
600
|
+
function bindCodeBtn(btn, banner, route) {
|
|
601
|
+
btn.addEventListener('click', async () => {
|
|
602
|
+
if (bannerBusy[route]) return;
|
|
603
|
+
bannerBusy[route] = true;
|
|
604
|
+
btn.disabled = true;
|
|
605
|
+
btn.textContent = '生成中…';
|
|
606
|
+
try {
|
|
607
|
+
mountCode(banner, await issuePairingCode(route));
|
|
608
|
+
} catch (err) {
|
|
609
|
+
banner.querySelectorAll('.dsh-mios-code-slot').forEach((e) => e.remove());
|
|
610
|
+
const errEl = document.createElement('div');
|
|
611
|
+
errEl.className = 'err';
|
|
612
|
+
errEl.textContent = '生成失败:' + (err?.body?.error?.message || err.message);
|
|
613
|
+
banner.append(errEl);
|
|
614
|
+
} finally {
|
|
615
|
+
bannerBusy[route] = false;
|
|
616
|
+
btn.textContent = '重新生成配对码';
|
|
617
|
+
renderSummary();
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function showPanel() {
|
|
623
|
+
if (panelEl) return;
|
|
624
|
+
// 打开期间持续轻量刷新:扫码上线、改名、下线都能立刻反映,无需关掉重开。
|
|
625
|
+
if (summaryPollTimer) clearInterval(summaryPollTimer);
|
|
626
|
+
summaryPollTimer = setInterval(refreshSummary, 1200);
|
|
627
|
+
installStyle();
|
|
628
|
+
panelEl = document.createElement('div');
|
|
629
|
+
panelEl.className = 'dsh-mios-panel';
|
|
630
|
+
|
|
631
|
+
const header = document.createElement('h3');
|
|
632
|
+
const dot = document.createElement('span');
|
|
633
|
+
dot.id = 'dsh-mios-panel-dot';
|
|
634
|
+
dot.className = 'dsh-mios-dot down';
|
|
635
|
+
const title = document.createElement('span');
|
|
636
|
+
title.textContent = PANEL_TITLE;
|
|
637
|
+
// 插件更新:放在关闭按钮左边,点击弹出更新对话框(版本对比 + 更新与否由用户选)
|
|
638
|
+
const updBtn = document.createElement('button');
|
|
639
|
+
updBtn.className = 'dsh-mios-upd';
|
|
640
|
+
updBtn.textContent = '插件更新';
|
|
641
|
+
updBtn.addEventListener('click', () => {
|
|
642
|
+
// 点击务必先有可见反馈再联网,避免「无反应」错觉(缓存旧模块时旧逻辑写已移除的内联元素)。
|
|
643
|
+
try { checkPluginUpdate(); } catch (e) {
|
|
644
|
+
console.error('[dsh-mobile-ios] checkPluginUpdate failed:', e);
|
|
645
|
+
showUpdateModal({ title: '插件更新', lines: [], note: `打开更新检查失败:${e?.message || e}`, actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }] });
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
const close = document.createElement('button');
|
|
649
|
+
close.className = 'dsh-mios-close';
|
|
650
|
+
close.textContent = '✕';
|
|
651
|
+
close.setAttribute('aria-label', '关闭面板');
|
|
652
|
+
close.addEventListener('click', removePanel);
|
|
653
|
+
header.append(dot, title, updBtn, close);
|
|
654
|
+
|
|
655
|
+
const body = document.createElement('div');
|
|
656
|
+
body.append(
|
|
657
|
+
row('插件版本', 'dsh-mios-adapter'),
|
|
658
|
+
row('电脑名称', 'dsh-mios-host'),
|
|
659
|
+
deviceRows(),
|
|
660
|
+
);
|
|
661
|
+
|
|
662
|
+
// ── banner 1:局域网连接(在家 · 直连 · 始终可用) ──
|
|
663
|
+
const lanBanner = bannerBox('局域网连接', 'dsh-mios-lan-state', '手机与电脑连同一个 Wi-Fi,直连不走外网。适合在家日常用。');
|
|
664
|
+
const lanCodeBtn = document.createElement('button');
|
|
665
|
+
lanCodeBtn.className = 'pairbtn';
|
|
666
|
+
lanCodeBtn.id = 'dsh-mios-lan-code';
|
|
667
|
+
lanCodeBtn.textContent = '生成配对码';
|
|
668
|
+
bindCodeBtn(lanCodeBtn, lanBanner, 'lan');
|
|
669
|
+
lanBanner.append(lanCodeBtn);
|
|
670
|
+
|
|
671
|
+
// ── banner 2:远程通道连接(出门 · 固定域名 · 一个主按钮走完) ──
|
|
672
|
+
// 「开启远程」与「生成配对码」合并成一个主按钮:没开过→开通;
|
|
673
|
+
// 开过没跑→开启并出码;跑着→直接出码。停止是旁边的小按钮。
|
|
674
|
+
const remoteBanner = bannerBox('远程通道连接', 'dsh-mios-remote-state', '出门在外扫码连固定域名,电脑和手机不必在同一网络。');
|
|
675
|
+
const remoteAddr = document.createElement('div');
|
|
676
|
+
remoteAddr.id = 'dsh-mios-remote-addr';
|
|
677
|
+
remoteAddr.className = 'banner-addr';
|
|
678
|
+
remoteAddr.textContent = '—';
|
|
679
|
+
remoteBanner.append(remoteAddr);
|
|
680
|
+
const mainBtn = document.createElement('button');
|
|
681
|
+
mainBtn.className = 'pairbtn';
|
|
682
|
+
mainBtn.id = 'dsh-mios-remote-main';
|
|
683
|
+
mainBtn.textContent = '开启远程并生成配对码';
|
|
684
|
+
mainBtn.addEventListener('click', () => remoteMainAction(remoteBanner));
|
|
685
|
+
const stopBtn = document.createElement('button');
|
|
686
|
+
stopBtn.className = 'pairbtn stopbtn';
|
|
687
|
+
stopBtn.id = 'dsh-mios-remote-stop';
|
|
688
|
+
stopBtn.textContent = '停止远程';
|
|
689
|
+
stopBtn.addEventListener('click', stopRemoteAction);
|
|
690
|
+
remoteBanner.append(mainBtn, stopBtn);
|
|
691
|
+
|
|
692
|
+
const hint = document.createElement('div');
|
|
693
|
+
hint.id = 'dsh-mios-hint';
|
|
694
|
+
hint.style.cssText = 'margin-top:8px;font-size:11px;color:#e0a03c;line-height:1.5;';
|
|
695
|
+
hint.style.display = 'none';
|
|
696
|
+
|
|
697
|
+
const footer = document.createElement('footer');
|
|
698
|
+
footer.textContent = 'iPhone/iPad App → 连接 → 扫码(约 2 分钟有效)';
|
|
699
|
+
|
|
700
|
+
panelEl.append(header, body, lanBanner, remoteBanner, hint, footer);
|
|
701
|
+
document.body.append(panelEl);
|
|
702
|
+
applyPanelTheme();
|
|
703
|
+
placePanelAboveTrigger();
|
|
704
|
+
renderSummary();
|
|
705
|
+
probe();
|
|
706
|
+
|
|
707
|
+
// 点击面板/触发 banner 以外的任意区域即关闭(✕ 之外的第二条路)
|
|
708
|
+
outsideCloser = (ev) => {
|
|
709
|
+
if (!panelEl) return;
|
|
710
|
+
const t = ev.target;
|
|
711
|
+
if (panelEl.contains(t) || (t.closest && t.closest('.dsh-mios-trigger'))) return;
|
|
712
|
+
removePanel();
|
|
713
|
+
};
|
|
714
|
+
document.addEventListener('pointerdown', outsideCloser);
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/** 面板底边对齐触发 banner 顶边:不遮挡 banner,屏幕放不下就整体上推并内滚。 */
|
|
718
|
+
function placePanelAboveTrigger() {
|
|
719
|
+
const trigger = document.querySelector('.dsh-mios-trigger');
|
|
720
|
+
if (trigger) {
|
|
721
|
+
const rect = trigger.getBoundingClientRect();
|
|
722
|
+
panelEl.style.left = `${Math.max(10, Math.round(rect.left))}px`;
|
|
723
|
+
panelEl.style.bottom = `${Math.max(10, Math.round(window.innerHeight - rect.top + 8))}px`;
|
|
724
|
+
}
|
|
725
|
+
panelEl.style.maxHeight = 'min(72vh, 640px)';
|
|
726
|
+
panelEl.style.overflowY = 'auto';
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/** 更新检查:与「添加插件」同源,读 npm registry 的 latest 比对当前版本。
|
|
730
|
+
* 404 = 包未发布(本地开发版);网络失败 = 如实报不可达,二者不混为一谈。 */
|
|
731
|
+
/** 更新对话框:显示版本对比,更新与否由用户选;随面板销毁清理。 */
|
|
732
|
+
let updateModal = null;
|
|
733
|
+
function closeUpdateModal() {
|
|
734
|
+
updateModal?.remove();
|
|
735
|
+
updateModal = null;
|
|
736
|
+
}
|
|
737
|
+
function showUpdateModal({ title, banner, lines, note, steps, actions }) {
|
|
738
|
+
closeUpdateModal();
|
|
739
|
+
const mask = document.createElement('div');
|
|
740
|
+
mask.className = 'dsh-mios-modal-mask';
|
|
741
|
+
if (detectHostTheme() === 'light') mask.classList.add('light');
|
|
742
|
+
const box = document.createElement('div');
|
|
743
|
+
box.className = 'dsh-mios-modal';
|
|
744
|
+
|
|
745
|
+
const h = document.createElement('h4');
|
|
746
|
+
h.textContent = title;
|
|
747
|
+
const x = document.createElement('button');
|
|
748
|
+
x.className = 'modal-x';
|
|
749
|
+
x.textContent = '✕';
|
|
750
|
+
x.setAttribute('aria-label', '关闭');
|
|
751
|
+
x.addEventListener('click', closeUpdateModal);
|
|
752
|
+
h.append(x);
|
|
753
|
+
box.append(h);
|
|
754
|
+
|
|
755
|
+
// banner:一步更新流程的状态横幅(发现新版 / 更新中 / 成功 / 失败)
|
|
756
|
+
if (banner) {
|
|
757
|
+
const b = document.createElement('div');
|
|
758
|
+
b.className = `modal-banner${banner.tone ? ' ' + banner.tone : ''}`;
|
|
759
|
+
b.textContent = banner.text;
|
|
760
|
+
box.append(b);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
for (const line of lines || []) {
|
|
764
|
+
const row = document.createElement('div');
|
|
765
|
+
row.className = 'modal-line';
|
|
766
|
+
const label = document.createElement('span');
|
|
767
|
+
label.textContent = line.label;
|
|
768
|
+
const value = document.createElement('b');
|
|
769
|
+
value.textContent = line.value;
|
|
770
|
+
if (line.highlight) value.className = 'new';
|
|
771
|
+
row.append(label, value);
|
|
772
|
+
box.append(row);
|
|
773
|
+
}
|
|
774
|
+
if (note) {
|
|
775
|
+
const n = document.createElement('div');
|
|
776
|
+
n.className = 'modal-note';
|
|
777
|
+
n.textContent = note;
|
|
778
|
+
box.append(n);
|
|
779
|
+
}
|
|
780
|
+
// 排障步骤:域名/NS 这类问题光说「异常」没用,得给出下一步点哪里。
|
|
781
|
+
if (steps?.length) {
|
|
782
|
+
const ol = document.createElement('ol');
|
|
783
|
+
ol.className = 'modal-steps';
|
|
784
|
+
for (const s of steps) {
|
|
785
|
+
const li = document.createElement('li');
|
|
786
|
+
li.textContent = s;
|
|
787
|
+
ol.append(li);
|
|
788
|
+
}
|
|
789
|
+
box.append(ol);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
const bar = document.createElement('div');
|
|
793
|
+
bar.className = 'modal-actions';
|
|
794
|
+
for (const action of actions || []) {
|
|
795
|
+
const btn = document.createElement('button');
|
|
796
|
+
btn.textContent = action.label;
|
|
797
|
+
if (action.primary) btn.className = 'primary';
|
|
798
|
+
btn.addEventListener('click', () => action.onClick?.(btn));
|
|
799
|
+
bar.append(btn);
|
|
800
|
+
}
|
|
801
|
+
box.append(bar);
|
|
802
|
+
mask.append(box);
|
|
803
|
+
mask.addEventListener('click', (e) => { if (e.target === mask) closeUpdateModal(); });
|
|
804
|
+
document.body.append(mask);
|
|
805
|
+
updateModal = mask;
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* 复制文本。三级降级,任一级成功即返回 true:
|
|
810
|
+
* 1) navigator.clipboard —— 只在安全上下文可用;dsh-app:// 这类自定义协议
|
|
811
|
+
* 通常不算安全上下文,所以这一级大概率直接缺失/抛错;
|
|
812
|
+
* 2) textarea + execCommand('copy') —— 必须真的可见可焦点、并显式框选,
|
|
813
|
+
* 之前用 opacity:0 且未 focus/未 setSelectionRange,在 Electron 里选不中,
|
|
814
|
+
* execCommand 返回 false(这正是「点了复制没反应」的直接原因);
|
|
815
|
+
* 3) 全选 contenteditable 兜底 —— 仍然失败就交给面板给用户可手动复制的输入框。
|
|
816
|
+
*/
|
|
817
|
+
async function copyToClipboard(text) {
|
|
818
|
+
const value = String(text ?? '');
|
|
819
|
+
if (!value) return false;
|
|
820
|
+
try {
|
|
821
|
+
if (navigator.clipboard?.writeText) {
|
|
822
|
+
await navigator.clipboard.writeText(value);
|
|
823
|
+
return true;
|
|
824
|
+
}
|
|
825
|
+
} catch { /* 落到下一级 */ }
|
|
826
|
+
try {
|
|
827
|
+
const ta = document.createElement('textarea');
|
|
828
|
+
ta.value = value;
|
|
829
|
+
ta.setAttribute('readonly', '');
|
|
830
|
+
ta.setAttribute('aria-hidden', 'true');
|
|
831
|
+
// 用屏幕外定位而不是 opacity:0:透明元素在部分引擎里无法获得选区
|
|
832
|
+
ta.style.cssText = 'position:fixed;top:0;left:-9999px;width:1px;height:1px;'
|
|
833
|
+
+ 'padding:0;border:0;outline:none;box-shadow:none;background:transparent;';
|
|
834
|
+
document.body.append(ta);
|
|
835
|
+
ta.focus();
|
|
836
|
+
ta.select();
|
|
837
|
+
ta.setSelectionRange(0, value.length);
|
|
838
|
+
const ok = document.execCommand('copy');
|
|
839
|
+
ta.remove();
|
|
840
|
+
if (ok) return true;
|
|
841
|
+
} catch { /* 落到下一级 */ }
|
|
842
|
+
try {
|
|
843
|
+
const holder = document.createElement('div');
|
|
844
|
+
holder.contentEditable = 'true';
|
|
845
|
+
holder.textContent = value;
|
|
846
|
+
holder.style.cssText = 'position:fixed;top:0;left:-9999px;white-space:pre;';
|
|
847
|
+
document.body.append(holder);
|
|
848
|
+
const range = document.createRange();
|
|
849
|
+
range.selectNodeContents(holder);
|
|
850
|
+
const sel = window.getSelection();
|
|
851
|
+
sel.removeAllRanges();
|
|
852
|
+
sel.addRange(range);
|
|
853
|
+
const ok = document.execCommand('copy');
|
|
854
|
+
sel.removeAllRanges();
|
|
855
|
+
holder.remove();
|
|
856
|
+
return Boolean(ok);
|
|
857
|
+
} catch {
|
|
858
|
+
return false;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/** 弹窗内结果提示:复制/更新这类动作必须有可见反馈,不能只改按钮文字。 */
|
|
863
|
+
function showModalToast(text, tone = 'info') {
|
|
864
|
+
const box = updateModal?.querySelector?.('.dsh-mios-modal');
|
|
865
|
+
if (!box) return;
|
|
866
|
+
let el = box.querySelector('.modal-toast');
|
|
867
|
+
if (!el) {
|
|
868
|
+
el = document.createElement('div');
|
|
869
|
+
box.insertBefore(el, box.querySelector('.modal-actions'));
|
|
870
|
+
}
|
|
871
|
+
el.className = `modal-toast${tone ? ' ' + tone : ''}`;
|
|
872
|
+
el.textContent = text;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* 复制全线失败时的兜底:在弹窗里放一个已全选的输入框。
|
|
877
|
+
* 用户按 Ctrl/⌘+C 就能拿到,不依赖任何剪贴板 API。
|
|
878
|
+
*/
|
|
879
|
+
function offerManualCopy(text) {
|
|
880
|
+
const box = updateModal?.querySelector?.('.dsh-mios-modal');
|
|
881
|
+
if (!box) return;
|
|
882
|
+
if (box.querySelector('.modal-pick')) return;
|
|
883
|
+
const input = document.createElement('input');
|
|
884
|
+
input.className = 'modal-pick';
|
|
885
|
+
input.type = 'text';
|
|
886
|
+
input.readOnly = true;
|
|
887
|
+
input.value = text;
|
|
888
|
+
box.insertBefore(input, box.querySelector('.modal-actions'));
|
|
889
|
+
try {
|
|
890
|
+
input.focus();
|
|
891
|
+
input.select();
|
|
892
|
+
} catch { /* 焦点拿不到也让用户能手动点选 */ }
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
async function checkPluginUpdate() {
|
|
896
|
+
showUpdateModal({ title: '插件更新', lines: [], note: '正在检查更新…', actions: [] });
|
|
897
|
+
const current = summary?.version || '未知';
|
|
898
|
+
let latest = '';
|
|
899
|
+
let sawNotFound = false;
|
|
900
|
+
let lastError = null;
|
|
901
|
+
for (const base of PLUGIN_REGISTRIES) {
|
|
902
|
+
try {
|
|
903
|
+
const res = await fetch(`${base}/${PLUGIN_NAME}/latest`, { signal: AbortSignal.timeout(6000) });
|
|
904
|
+
if (res.status === 404) { sawNotFound = true; continue; }
|
|
905
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
906
|
+
const meta = await res.json();
|
|
907
|
+
if (meta?.version) { latest = String(meta.version); break; }
|
|
908
|
+
} catch (e) {
|
|
909
|
+
lastError = e;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
if (!latest) {
|
|
913
|
+
showUpdateModal({
|
|
914
|
+
title: '插件更新',
|
|
915
|
+
lines: [{ label: '当前版本', value: `v${current}` }],
|
|
916
|
+
note: sawNotFound
|
|
917
|
+
? '插件尚未发布到 npm,当前为本地开发版。更新方式:拉取仓库最新代码后重启 DSH 桌面端,插件会随源码热重载。'
|
|
918
|
+
: `更新源暂不可达${lastError ? `(${lastError.message})` : ''},无法确认是否有新版本。`,
|
|
919
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
920
|
+
});
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
if (compareVersions(latest, current) > 0) {
|
|
924
|
+
// 更新能力分三档,面板按宿主实际给了什么决定按钮,绝不摆一个按了没反应的按钮:
|
|
925
|
+
// ① updateBundle —— 宿主插件管理器自己的升级入口(与「插件」页那个
|
|
926
|
+
// 「更新到 vX」完全同一条代码路径:先移除旧包再装新包并热加载)。
|
|
927
|
+
// 已安装的包只能走这条,installBundle 会被宿主判为「该插件已安装」。
|
|
928
|
+
// ② installBundle —— 老宿主没有 updateBundle 时的等效能力。
|
|
929
|
+
// ③ 都没有 —— 只给图文指引:去「插件」页点右上角刷新,交给宿主安装。
|
|
930
|
+
const hostUpdate = !!(pluginManager && typeof pluginManager.updateBundle === 'function');
|
|
931
|
+
const hostInstall = !!(pluginManager && typeof pluginManager.installBundle === 'function');
|
|
932
|
+
const canAuto = hostUpdate || hostInstall;
|
|
933
|
+
showUpdateModal({
|
|
934
|
+
title: '新版本更新',
|
|
935
|
+
banner: { text: `⬆️ 发现新版本 v${latest}`, tone: 'info' },
|
|
936
|
+
lines: [
|
|
937
|
+
{ label: '当前版本', value: `v${current}` },
|
|
938
|
+
{ label: '最新版本', value: `v${latest}`, highlight: true },
|
|
939
|
+
],
|
|
940
|
+
note: canAuto
|
|
941
|
+
? '点「立即更新」由 DSH 插件管理器安装并切换到新版本,完成后重启 DSH 生效。也可以到「插件」页点右上角「刷新」手动更新。'
|
|
942
|
+
: '本面板没拿到 DSH 插件管理器接口,请按下面步骤在「插件」页更新,由 DSH 自带的插件更新机制安装。',
|
|
943
|
+
// 没有宿主接口时,把「点哪里」写清楚:只报「可更新」不给路径等于没帮上忙。
|
|
944
|
+
steps: canAuto ? undefined : [
|
|
945
|
+
'在 DSH 左侧点「插件」',
|
|
946
|
+
'点右上角「刷新」',
|
|
947
|
+
`在列表里点 dsh-mobile-ios 的「更新到 v${latest}」`,
|
|
948
|
+
'重启 DSH 桌面端,让新版本生效',
|
|
949
|
+
],
|
|
950
|
+
actions: canAuto
|
|
951
|
+
? [
|
|
952
|
+
{ label: '稍后再说', onClick: closeUpdateModal },
|
|
953
|
+
{ label: `立即更新到 v${latest}`, primary: true, onClick: () => performUpdate(latest) },
|
|
954
|
+
]
|
|
955
|
+
: [
|
|
956
|
+
{
|
|
957
|
+
label: '复制包名',
|
|
958
|
+
onClick: async (btn) => {
|
|
959
|
+
const ok = await copyToClipboard(PLUGIN_NAME);
|
|
960
|
+
if (ok) {
|
|
961
|
+
btn.textContent = '已复制';
|
|
962
|
+
showModalToast(`已复制 ${PLUGIN_NAME},可在「插件 → 添加插件」粘贴`, 'success');
|
|
963
|
+
} else {
|
|
964
|
+
btn.textContent = '请手动复制';
|
|
965
|
+
showModalToast('本机剪贴板不可用,请在下方框内全选复制', 'error');
|
|
966
|
+
offerManualCopy(PLUGIN_NAME);
|
|
967
|
+
}
|
|
968
|
+
},
|
|
969
|
+
},
|
|
970
|
+
{ label: '知道了', primary: true, onClick: closeUpdateModal },
|
|
971
|
+
],
|
|
972
|
+
});
|
|
973
|
+
} else {
|
|
974
|
+
showUpdateModal({
|
|
975
|
+
title: '已是最新版本',
|
|
976
|
+
lines: [
|
|
977
|
+
{ label: '当前版本', value: `v${current}` },
|
|
978
|
+
{ label: 'npm 最新', value: `v${latest}` },
|
|
979
|
+
],
|
|
980
|
+
note: '当前版本已是最新,无需更新。',
|
|
981
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/** 一步更新:经宿主 pluginManager.installBundle 安装 npm spec,
|
|
987
|
+
* 状态机覆盖 更新中 / 成功 / 失败 / 超时对账 / 重复点击。 */
|
|
988
|
+
let UPDATE_TIMEOUT_MS = 180000; // pnpm 安装 + 可能的重启,给足 3 分钟
|
|
989
|
+
function unwrapRemote(result) {
|
|
990
|
+
if (result && typeof result === 'object' && Object.hasOwn(result, 'ok')) {
|
|
991
|
+
if (!result.ok) throw new Error(result.error?.code || result.error?.message || '宿主返回失败');
|
|
992
|
+
return result.value;
|
|
993
|
+
}
|
|
994
|
+
return result;
|
|
995
|
+
}
|
|
996
|
+
function withTimeout(promise, ms, label) {
|
|
997
|
+
return new Promise((resolve, reject) => {
|
|
998
|
+
let settled = false;
|
|
999
|
+
const timer = setTimeout(() => {
|
|
1000
|
+
if (settled) return;
|
|
1001
|
+
settled = true;
|
|
1002
|
+
reject(new Error(`${label}超时(${Math.round(ms / 1000)} 秒)`));
|
|
1003
|
+
}, ms);
|
|
1004
|
+
Promise.resolve(promise).then(
|
|
1005
|
+
(v) => { if (!settled) { settled = true; clearTimeout(timer); resolve(v); } },
|
|
1006
|
+
(e) => { if (!settled) { settled = true; clearTimeout(timer); reject(e); } },
|
|
1007
|
+
);
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
// ── 跨 HMR 重载的更新状态持久化 ─────────────────────────────
|
|
1012
|
+
// 更新「自己」会触发宿主卸载本模块并加载新版,当前 JS 上下文连同弹窗一起销毁。
|
|
1013
|
+
// 因此发起更新前把状态写入 localStorage,新模块 apply 时读取并给出结果反馈,
|
|
1014
|
+
// 避免出现「点一下弹窗消失、什么都没发生」的体验。
|
|
1015
|
+
const UPDATE_STATE_KEY = 'dsh-mios-update-state';
|
|
1016
|
+
function readUpdateState() {
|
|
1017
|
+
try {
|
|
1018
|
+
const raw = globalThis.localStorage?.getItem(UPDATE_STATE_KEY);
|
|
1019
|
+
return raw ? JSON.parse(raw) : null;
|
|
1020
|
+
} catch { return null; }
|
|
1021
|
+
}
|
|
1022
|
+
function writeUpdateState(state) {
|
|
1023
|
+
try { globalThis.localStorage?.setItem(UPDATE_STATE_KEY, JSON.stringify(state)); }
|
|
1024
|
+
catch { /* 存储不可用时静默,功能降级但不崩 */ }
|
|
1025
|
+
}
|
|
1026
|
+
function clearUpdateState() {
|
|
1027
|
+
try { globalThis.localStorage?.removeItem(UPDATE_STATE_KEY); }
|
|
1028
|
+
catch { /* 同上 */ }
|
|
1029
|
+
}
|
|
1030
|
+
/** apply 时调用:若上次更新尚未收尾,依据当前版本推断结果并弹窗告知。 */
|
|
1031
|
+
function restorePendingUpdate() {
|
|
1032
|
+
const st = readUpdateState();
|
|
1033
|
+
if (!st || st.status !== 'updating' || !st.target) return;
|
|
1034
|
+
const current = String(summary?.version || '').trim();
|
|
1035
|
+
const age = Date.now() - (st.at || 0);
|
|
1036
|
+
if (current && compareVersions(current, st.target) >= 0) {
|
|
1037
|
+
clearUpdateState();
|
|
1038
|
+
// 延迟到面板初次渲染后再弹,避免被 apply 后续的初始化覆盖
|
|
1039
|
+
setTimeout(() => showUpdateModal({
|
|
1040
|
+
title: '更新完成',
|
|
1041
|
+
banner: { text: `✅ 已更新到 v${st.target}`, tone: 'success' },
|
|
1042
|
+
lines: [{ label: '当前版本', value: `v${current}` }],
|
|
1043
|
+
note: '插件已更新并重新加载完成。',
|
|
1044
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
1045
|
+
}), 600);
|
|
1046
|
+
} else if (age > 5 * 60 * 1000) {
|
|
1047
|
+
clearUpdateState();
|
|
1048
|
+
setTimeout(() => showUpdateModal({
|
|
1049
|
+
title: '更新结果未知',
|
|
1050
|
+
banner: { text: '⚠️ 上一步更新未能确认', tone: 'warn' },
|
|
1051
|
+
lines: [{ label: '目标版本', value: `v${st.target}` }, { label: '当前版本', value: current ? `v${current}` : '未知' }],
|
|
1052
|
+
note: '更新过程被中断或耗时过长。请到「插件」页确认当前版本,必要时手动更新。',
|
|
1053
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
1054
|
+
}), 600);
|
|
1055
|
+
}
|
|
1056
|
+
// 未超时且版本未变:保持 updating 状态,等下次 apply(轮询重载)再判断
|
|
1057
|
+
}
|
|
1058
|
+
async function performUpdate(latest) {
|
|
1059
|
+
// 重复点击保护:更新中直接忽略再次触发
|
|
1060
|
+
if (updateInFlight) return;
|
|
1061
|
+
updateInFlight = true;
|
|
1062
|
+
const current = summary?.version || '未知';
|
|
1063
|
+
const spec = `${PLUGIN_NAME}@${latest}`;
|
|
1064
|
+
const canHost = !!(pluginManager
|
|
1065
|
+
&& (typeof pluginManager.updateBundle === 'function' || typeof pluginManager.installBundle === 'function'));
|
|
1066
|
+
// 先持久化「更新中」,确保 HMR 卸载本模块后新实例能恢复出结果反馈。
|
|
1067
|
+
writeUpdateState({ status: 'updating', target: String(latest), at: Date.now() });
|
|
1068
|
+
// 状态:更新中。无取消/关闭动作外的操作按钮,防重复点击。
|
|
1069
|
+
showUpdateModal({
|
|
1070
|
+
title: '正在更新',
|
|
1071
|
+
banner: { text: `⏳ 正在更新到 v${latest},请稍候`, tone: 'info' },
|
|
1072
|
+
lines: [
|
|
1073
|
+
{ label: '当前版本', value: `v${current}` },
|
|
1074
|
+
{ label: '目标版本', value: `v${latest}`, highlight: true },
|
|
1075
|
+
],
|
|
1076
|
+
note: canHost
|
|
1077
|
+
? '正在通过 DSH 插件管理器更新(先卸旧版再装新版),约 1–3 分钟。请勿重复点击或关闭窗口。'
|
|
1078
|
+
: '正在经本机网关下载并安装(含完整性校验),请勿重复点击或关闭窗口。',
|
|
1079
|
+
actions: [],
|
|
1080
|
+
});
|
|
1081
|
+
try {
|
|
1082
|
+
let restart = false;
|
|
1083
|
+
const canHostUpdate = !!(pluginManager && typeof pluginManager.updateBundle === 'function');
|
|
1084
|
+
if (canHostUpdate) {
|
|
1085
|
+
// ⓪ 宿主插件管理器的升级入口:与「插件」页「更新到 vX」同一条路径
|
|
1086
|
+
// (内部先 removeBundle 再 installBundle,并触发 HMR 热切换)。
|
|
1087
|
+
// 已安装的包只能走这条 —— installBundle 会被判「该插件已安装」。
|
|
1088
|
+
let requestId = globalThis.crypto?.randomUUID?.();
|
|
1089
|
+
if (typeof requestId !== 'string') throw new Error('无法生成更新请求 ID');
|
|
1090
|
+
const settled = await withTimeout(
|
|
1091
|
+
pluginManager.updateBundle(String(PLUGIN_NAME), String(latest), { requestId }),
|
|
1092
|
+
UPDATE_TIMEOUT_MS, '更新',
|
|
1093
|
+
);
|
|
1094
|
+
const out = unwrapRemote(settled) || {};
|
|
1095
|
+
const result = out.result || {};
|
|
1096
|
+
// 宿主用 application:'failed' + error 表达拒绝/失败,不能当成功吞掉
|
|
1097
|
+
if (result.application === 'failed' || result.error) {
|
|
1098
|
+
const why = result.error?.diagnostic || result.error?.code || out.step || '宿主未说明原因';
|
|
1099
|
+
throw new Error(`DSH 插件管理器更新失败:${why}`);
|
|
1100
|
+
}
|
|
1101
|
+
restart = true;
|
|
1102
|
+
} else if (typeof pluginManager?.installBundle === 'function') {
|
|
1103
|
+
// ① 老宿主安装器:最常见的 Desktop 路径
|
|
1104
|
+
let requestId;
|
|
1105
|
+
requestId = globalThis.crypto?.randomUUID?.();
|
|
1106
|
+
if (typeof requestId !== 'string') throw new Error('无法生成更新请求 ID');
|
|
1107
|
+
let result;
|
|
1108
|
+
try {
|
|
1109
|
+
result = await withTimeout(
|
|
1110
|
+
pluginManager.installBundle(spec, { enabled: true, requestId }),
|
|
1111
|
+
UPDATE_TIMEOUT_MS, '安装',
|
|
1112
|
+
);
|
|
1113
|
+
} catch (err) {
|
|
1114
|
+
// 超时/结果未知:用 requestId 对账,绝不重复提交安装
|
|
1115
|
+
if (typeof pluginManager.waitForInstall === 'function') {
|
|
1116
|
+
const recovered = await withTimeout(pluginManager.waitForInstall(requestId), 60000, '结果对账');
|
|
1117
|
+
if (!recovered) throw err;
|
|
1118
|
+
result = recovered;
|
|
1119
|
+
} else throw err;
|
|
1120
|
+
}
|
|
1121
|
+
const value = unwrapRemote(result);
|
|
1122
|
+
restart = !!(value && (value.restartRequired || value.restart));
|
|
1123
|
+
} else {
|
|
1124
|
+
// ② 网关自更新:宿主没给任何插件管理接口时的等效能力。
|
|
1125
|
+
// 网关从 npm 取 tarball、按 dist.shasum 校验、备份旧目录后覆盖安装。
|
|
1126
|
+
const res = await fetchJson('/mobile/v1/plugin/self-update', {
|
|
1127
|
+
method: 'POST',
|
|
1128
|
+
headers: { 'content-type': 'application/json' },
|
|
1129
|
+
body: JSON.stringify({ target: latest }),
|
|
1130
|
+
}, 180000);
|
|
1131
|
+
restart = res?.restartRequired !== false;
|
|
1132
|
+
}
|
|
1133
|
+
clearUpdateState(); // 安装已返回结果,交由本弹窗反馈,无需跨重载恢复
|
|
1134
|
+
// 状态:成功
|
|
1135
|
+
showUpdateModal({
|
|
1136
|
+
title: '更新完成',
|
|
1137
|
+
banner: { text: `✅ 已安装 v${latest}`, tone: 'success' },
|
|
1138
|
+
lines: [
|
|
1139
|
+
{ label: '目标版本', value: `v${latest}` },
|
|
1140
|
+
],
|
|
1141
|
+
note: restart
|
|
1142
|
+
? '插件已更新,重启 DSH 桌面端后生效(面板版本号会在重启后变成新版本)。'
|
|
1143
|
+
: '插件已更新。若面板未刷新,重开面板或重启 DSH 即可。',
|
|
1144
|
+
actions: [{ label: '知道了', primary: true, onClick: closeUpdateModal }],
|
|
1145
|
+
});
|
|
1146
|
+
} catch (err) {
|
|
1147
|
+
// 状态:失败。不能只报错就完 —— 顺手把「插件页刷新」这条路指出来,
|
|
1148
|
+
// 宿主自己的安装器通常能成功(它走的是 pnpm + 本机 registry 镜像)。
|
|
1149
|
+
clearUpdateState();
|
|
1150
|
+
showUpdateModal({
|
|
1151
|
+
title: '更新失败',
|
|
1152
|
+
banner: { text: '❌ 面板内更新未成功', tone: 'error' },
|
|
1153
|
+
lines: [{ label: '目标版本', value: `v${latest}` }],
|
|
1154
|
+
note: `失败原因:${err?.body?.error?.message || err?.message || String(err)}。可重试;更稳的办法是到「插件」页刷新,由 DSH 自己安装。`,
|
|
1155
|
+
steps: [
|
|
1156
|
+
'在 DSH 左侧点「插件」',
|
|
1157
|
+
'点右上角「刷新」',
|
|
1158
|
+
`在列表里点 dsh-mobile-ios 的「更新到 v${latest}」`,
|
|
1159
|
+
'重启 DSH 桌面端,让新版本生效',
|
|
1160
|
+
],
|
|
1161
|
+
actions: [
|
|
1162
|
+
{ label: '关闭', onClick: closeUpdateModal },
|
|
1163
|
+
{ label: '重试更新', onClick: () => performUpdate(latest) },
|
|
1164
|
+
{
|
|
1165
|
+
label: '复制包名',
|
|
1166
|
+
onClick: async (btn) => {
|
|
1167
|
+
const ok = await copyToClipboard(spec);
|
|
1168
|
+
if (ok) {
|
|
1169
|
+
btn.textContent = '已复制';
|
|
1170
|
+
showModalToast(`已复制 ${spec},去「插件 → 添加插件」粘贴安装`, 'success');
|
|
1171
|
+
} else {
|
|
1172
|
+
btn.textContent = '请手动复制';
|
|
1173
|
+
showModalToast('本机剪贴板不可用,请在下方框内全选复制', 'error');
|
|
1174
|
+
offerManualCopy(spec);
|
|
1175
|
+
}
|
|
1176
|
+
},
|
|
1177
|
+
},
|
|
1178
|
+
{ label: '知道了', primary: true, onClick: closeUpdateModal },
|
|
1179
|
+
],
|
|
1180
|
+
});
|
|
1181
|
+
} finally {
|
|
1182
|
+
updateInFlight = false;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
function compareVersions(a, b) {
|
|
1187
|
+
const parse = (s) => String(s || '').replace(/^v/i, '').split('-')[0].split('.').map((n) => parseInt(n, 10) || 0);
|
|
1188
|
+
const [pa, pb] = [parse(a), parse(b)];
|
|
1189
|
+
for (let i = 0; i < 3; i += 1) {
|
|
1190
|
+
if ((pa[i] || 0) !== (pb[i] || 0)) return (pa[i] || 0) - (pb[i] || 0);
|
|
1191
|
+
}
|
|
1192
|
+
return 0;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
/** 等 summary 反映出「远程已可用」:服务端 start 会等到注册成功才返回,
|
|
1196
|
+
* 但客户端超时/轮询窗口内可能还没看到,这里统一补轮询兜底。 */
|
|
1197
|
+
async function waitForTunnelUp(attempts = 8, delayMs = 1500) {
|
|
1198
|
+
for (let i = 0; i < attempts; i += 1) {
|
|
1199
|
+
await probe();
|
|
1200
|
+
renderSummary();
|
|
1201
|
+
const t = summary?.tunnel;
|
|
1202
|
+
if (t && t.state === 'running' && t.dnsOk !== false) return true;
|
|
1203
|
+
if (i < attempts - 1) await new Promise((resolve) => { setTimeout(resolve, delayMs); });
|
|
1204
|
+
}
|
|
1205
|
+
return false;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/** 自举安装 cloudflared:先弹窗征得同意,确认后经网关从官方源下载到私有目录。
|
|
1209
|
+
* 安装成功即重探状态(发现链已含组件目录),让本次点击继续走开启远程。 */
|
|
1210
|
+
let installBusy = false;
|
|
1211
|
+
let installBanner = null; // 保存触发安装的 banner,供成功后自动继续出码(onClick 只传按钮,不传 banner)
|
|
1212
|
+
let autoContinueTimer = null; // 安装成功后延迟自动出码的定时器,允许手动「立即开启」取消
|
|
1213
|
+
async function installCloudflaredAction(banner) {
|
|
1214
|
+
if (installBusy) return;
|
|
1215
|
+
installBusy = true;
|
|
1216
|
+
installBanner = banner || null;
|
|
1217
|
+
try {
|
|
1218
|
+
showUpdateModal({
|
|
1219
|
+
title: '安装 cloudflared',
|
|
1220
|
+
banner: { text: '⬇️ 远程通道需要 cloudflared', tone: 'info' },
|
|
1221
|
+
lines: [
|
|
1222
|
+
{ label: '来源', value: 'Cloudflare 官方发布' },
|
|
1223
|
+
{ label: '安装位置', value: '本机私有目录(不改系统设置)' },
|
|
1224
|
+
],
|
|
1225
|
+
note: '将从 Cloudflare 官方发布地址下载 cloudflared 到本机,仅用于远程通道。下载期间请保持网络畅通,是否继续?',
|
|
1226
|
+
actions: [
|
|
1227
|
+
{ label: '取消', onClick: () => { installBanner = null; closeUpdateModal(); } },
|
|
1228
|
+
{ label: '确认下载并安装', primary: true, onClick: () => runInstall() },
|
|
1229
|
+
],
|
|
1230
|
+
});
|
|
1231
|
+
} finally {
|
|
1232
|
+
installBusy = false;
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
// 安装成功后接着走「确保隧道起来 → 出码」。抽出来供安装链与手动链共用,
|
|
1237
|
+
// 避免「装完只弹提示、还得再点一次」的伪无反应。
|
|
1238
|
+
async function continueRemoteAfterInstall() {
|
|
1239
|
+
const banner = installBanner;
|
|
1240
|
+
installBanner = null;
|
|
1241
|
+
await remoteMainAction(banner);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
async function runInstall() {
|
|
1245
|
+
if (installBusy) return;
|
|
1246
|
+
installBusy = true;
|
|
1247
|
+
const mainBtn = panelEl?.querySelector('#dsh-mios-remote-main');
|
|
1248
|
+
if (mainBtn) { mainBtn.textContent = '安装中…'; mainBtn.disabled = true; }
|
|
1249
|
+
// 先把确认框换成「正在安装」——用户点确认后立即有可见反馈,不进入静默等待。
|
|
1250
|
+
showUpdateModal({
|
|
1251
|
+
title: '正在安装',
|
|
1252
|
+
banner: { text: '⏳ 正在下载 cloudflared,请稍候', tone: 'info' },
|
|
1253
|
+
lines: [],
|
|
1254
|
+
note: '首次下载约几十 MB,取决于网速,请耐心等待,不要关闭窗口。',
|
|
1255
|
+
actions: [],
|
|
1256
|
+
});
|
|
1257
|
+
let succeeded = false;
|
|
1258
|
+
try {
|
|
1259
|
+
const res = await fetchJson('/mobile/v1/tunnel/install-cloudflared', { method: 'POST' }, 240000);
|
|
1260
|
+
await probe();
|
|
1261
|
+
renderSummary();
|
|
1262
|
+
const nowFound = summary?.tunnel?.executable === 'found';
|
|
1263
|
+
if (!nowFound) throw new Error('安装完成但网关尚未识别到 cloudflared,请重开面板重试');
|
|
1264
|
+
succeeded = true;
|
|
1265
|
+
showUpdateModal({
|
|
1266
|
+
title: '安装完成',
|
|
1267
|
+
banner: { text: '✅ cloudflared 已就绪,正在开启远程…', tone: 'success' },
|
|
1268
|
+
lines: [],
|
|
1269
|
+
note: res?.reused ? '本机已有 cloudflared,无需重复下载。正在为你开启远程并出码。' : 'cloudflared 已安装。正在为你开启远程并出码。',
|
|
1270
|
+
actions: [{ label: '立即开启远程', primary: true, onClick: () => { if (autoContinueTimer) { clearTimeout(autoContinueTimer); autoContinueTimer = null; } closeUpdateModal(); continueRemoteAfterInstall(); } }],
|
|
1271
|
+
});
|
|
1272
|
+
} catch (err) {
|
|
1273
|
+
showUpdateModal({
|
|
1274
|
+
title: '安装失败',
|
|
1275
|
+
banner: { text: '❌ 未能自动安装', tone: 'error' },
|
|
1276
|
+
lines: [],
|
|
1277
|
+
note: `${err?.body?.error?.message || err?.message || String(err)}。可检查网络后重试,或按提示手动安装 cloudflared。`,
|
|
1278
|
+
actions: [
|
|
1279
|
+
{ label: '关闭', onClick: closeUpdateModal },
|
|
1280
|
+
{ label: '重试安装', primary: true, onClick: () => runInstall() },
|
|
1281
|
+
],
|
|
1282
|
+
});
|
|
1283
|
+
} finally {
|
|
1284
|
+
installBusy = false;
|
|
1285
|
+
renderSummary();
|
|
1286
|
+
// 关键修复:安装成功后自动继续出码,无需用户再点一次主按钮。
|
|
1287
|
+
if (succeeded) {
|
|
1288
|
+
if (autoContinueTimer) clearTimeout(autoContinueTimer);
|
|
1289
|
+
autoContinueTimer = setTimeout(() => {
|
|
1290
|
+
autoContinueTimer = null;
|
|
1291
|
+
closeUpdateModal();
|
|
1292
|
+
continueRemoteAfterInstall();
|
|
1293
|
+
}, 900);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
/** 记住最近一次触发远程动作的 banner:弹窗里的「重新检测」要能继续同一条链。 */
|
|
1299
|
+
let lastRemoteBanner = null;
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* 凭据不可用(登录的账号与域名不匹配 / 证书过时)时的唯一正确出口。
|
|
1303
|
+
*
|
|
1304
|
+
* 现场(2026-11-21):新 Mac 上 cert.pem 属于另一个 Cloudflare 账号,
|
|
1305
|
+
* 面板只报「写入 DNS 失败:Command failed: …」,用户以为插件坏了,重装、重启、
|
|
1306
|
+
* 改 NS 全试了一遍——都无效。真正要做的只有一件:用**持有该域名**的账号重跑
|
|
1307
|
+
* `cloudflared tunnel login`。这个弹窗把这句话说清楚,并给出两条兜底:
|
|
1308
|
+
* · 重新检测:login 完之后不用重启插件,点一下就能继续开通;
|
|
1309
|
+
* · 重置本机状态:状态文件被跨机拷贝时的自救入口。
|
|
1310
|
+
*/
|
|
1311
|
+
function showCredentialHelpModal(credentials = {}) {
|
|
1312
|
+
const stateText = credentials.hint || '当前登录的 Cloudflare 账号无法管理这个域名';
|
|
1313
|
+
const exe = 'cloudflared';
|
|
1314
|
+
const lines = [
|
|
1315
|
+
...(credentials.accountName ? [{ label: '当前证书账号', value: credentials.accountName }] : []),
|
|
1316
|
+
...(credentials.zoneName ? [{ label: '固定地址域名', value: credentials.zoneName }] : []),
|
|
1317
|
+
...(credentials.zoneStatus ? [{ label: '域名状态', value: credentials.zoneStatus }] : []),
|
|
1318
|
+
];
|
|
1319
|
+
showUpdateModal({
|
|
1320
|
+
title: '登录的 Cloudflare 账号不对',
|
|
1321
|
+
banner: { text: `⚠️ ${stateText}`, tone: 'warn' },
|
|
1322
|
+
lines,
|
|
1323
|
+
note: '固定地址写在 Cloudflare 的域名下,只有「持有该域名的账号」才写得进去。'
|
|
1324
|
+
+ '换账号登录后,本机会自动重建隧道并沿用同一个地址,手机不需要重新配对。'
|
|
1325
|
+
+ `请在终端执行:${exe} tunnel login(面板无法代做浏览器授权)。`,
|
|
1326
|
+
steps: [
|
|
1327
|
+
`在这台电脑打开「终端」,执行:${exe} tunnel login`,
|
|
1328
|
+
'浏览器里选择「持有该域名」的账号,点授权(会覆盖 ~/.cloudflared/cert.pem)。',
|
|
1329
|
+
'回到面板点「重新检测」——账号一致后会自动重建隧道、保留原地址。',
|
|
1330
|
+
'如果这台电脑是从别的电脑拷过来的(状态文件不是本机生成的),可先点「重置本机状态」再重试。',
|
|
1331
|
+
],
|
|
1332
|
+
actions: [
|
|
1333
|
+
{ label: '关闭', onClick: closeUpdateModal },
|
|
1334
|
+
{ label: '重置本机状态', onClick: () => confirmResetTunnelState() },
|
|
1335
|
+
{
|
|
1336
|
+
label: '重新检测', primary: true,
|
|
1337
|
+
onClick: () => { closeUpdateModal(); recheckDnsAction(lastRemoteBanner); },
|
|
1338
|
+
},
|
|
1339
|
+
],
|
|
1340
|
+
});
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
/** 重置是「清本机状态」,不是删云端隧道:必须讲清后果再执行。 */
|
|
1344
|
+
function confirmResetTunnelState() {
|
|
1345
|
+
showUpdateModal({
|
|
1346
|
+
title: '重置本机固定地址状态',
|
|
1347
|
+
banner: { text: '⚠️ 只清本机状态,不动 Cloudflare', tone: 'warn' },
|
|
1348
|
+
lines: [],
|
|
1349
|
+
note: '将删除本机的固定地址记录与 ingress 配置(不删除 ~/.cloudflared/cert.pem,'
|
|
1350
|
+
+ '也不删除 Cloudflare 上的隧道与 DNS 记录)。重置后需要重新「开启远程」才会分配地址;'
|
|
1351
|
+
+ '如果旧地址仍指向这台机器,新地址会换一个前缀。',
|
|
1352
|
+
steps: [],
|
|
1353
|
+
actions: [
|
|
1354
|
+
{ label: '取消', onClick: showCredentialHelpModal.bind(null, summary?.tunnel?.credentials || {}) },
|
|
1355
|
+
{ label: '确认重置', primary: true, onClick: () => resetTunnelStateAction() },
|
|
1356
|
+
],
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
async function resetTunnelStateAction() {
|
|
1361
|
+
showUpdateModal({
|
|
1362
|
+
title: '正在重置',
|
|
1363
|
+
banner: { text: '⏳ 清理本机固定地址状态', tone: 'info' },
|
|
1364
|
+
lines: [],
|
|
1365
|
+
note: '正在删除本机状态与 ingress 配置,请稍候。',
|
|
1366
|
+
actions: [],
|
|
1367
|
+
});
|
|
1368
|
+
try {
|
|
1369
|
+
const res = await fetchJson('/mobile/v1/tunnel/reset', { method: 'POST' }, 25000);
|
|
1370
|
+
await probe();
|
|
1371
|
+
renderSummary();
|
|
1372
|
+
showUpdateModal({
|
|
1373
|
+
title: '已重置',
|
|
1374
|
+
banner: { text: '✅ 本机固定地址状态已清空', tone: 'success' },
|
|
1375
|
+
lines: res?.removed?.length ? [{ label: '已删除', value: res.removed.join('、') }] : [],
|
|
1376
|
+
note: res?.hadState
|
|
1377
|
+
? 'Cloudflare 上的隧道与 DNS 记录未删除。点「开启远程」会重新分配固定地址;若这台机器的证书属于别的账号,请先执行 cloudflared tunnel login。'
|
|
1378
|
+
: '本机本来就没有固定地址状态。点「开启远程」开始分配。',
|
|
1379
|
+
steps: [],
|
|
1380
|
+
actions: [
|
|
1381
|
+
{ label: '关闭', onClick: closeUpdateModal },
|
|
1382
|
+
{
|
|
1383
|
+
label: '开启远程', primary: true,
|
|
1384
|
+
onClick: () => { closeUpdateModal(); remoteMainAction(lastRemoteBanner); },
|
|
1385
|
+
},
|
|
1386
|
+
],
|
|
1387
|
+
});
|
|
1388
|
+
} catch (err) {
|
|
1389
|
+
showUpdateModal({
|
|
1390
|
+
title: '重置失败',
|
|
1391
|
+
banner: { text: '❌ 未能重置本机状态', tone: 'error' },
|
|
1392
|
+
lines: [],
|
|
1393
|
+
note: `${err?.body?.error?.message || err?.message || String(err)}。可稍后重试,或在终端删除 ~/.dsh/mobile-access/tunnel-named.json。`,
|
|
1394
|
+
steps: [],
|
|
1395
|
+
actions: [
|
|
1396
|
+
{ label: '关闭', onClick: closeUpdateModal },
|
|
1397
|
+
{ label: '重试', primary: true, onClick: () => resetTunnelStateAction() },
|
|
1398
|
+
],
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
/**
|
|
1404
|
+
* 域名未生效时的排障弹窗。
|
|
1405
|
+
*
|
|
1406
|
+
* 关键是把「异常」翻译成用户能做的三件事,并带上公网当前 NS 让他去注册商对照:
|
|
1407
|
+
* 用户不需要理解 zone/pending,只要看名字对不对得上。
|
|
1408
|
+
*/
|
|
1409
|
+
function showDnsHelpModal(res = {}) {
|
|
1410
|
+
const ns = Array.isArray(res.publicNs) ? res.publicNs : [];
|
|
1411
|
+
const zone = res.zone && typeof res.zone === 'object' ? res.zone : null;
|
|
1412
|
+
const expectedNs = Array.isArray(zone?.expectedNs) ? zone.expectedNs : [];
|
|
1413
|
+
const host = stripUrl(summary?.tunnel?.fixedUrl || summary?.tunnel?.hostname || '');
|
|
1414
|
+
const tampered = Boolean(res.tampered);
|
|
1415
|
+
// 归因码决定「修法」:moved(域名被另一个账号接管)/ pending(NS 没指过来)/
|
|
1416
|
+
// ns-mismatch(NS 与账号不匹配)/ unresolved(已生效但还没查到)绝不共用一套话术。
|
|
1417
|
+
const code = tampered ? 'tampered' : (zone?.code || 'unknown');
|
|
1418
|
+
const nsLine = expectedNs.length ? { label: '本账号 NS', value: expectedNs.join(' / ') } : null;
|
|
1419
|
+
const accountLine = zone?.accountName ? { label: '本账号', value: zone.accountName } : null;
|
|
1420
|
+
|
|
1421
|
+
const bodies = {
|
|
1422
|
+
tampered: {
|
|
1423
|
+
title: '固定域名被改指',
|
|
1424
|
+
banner: '⚠️ CNAME 已不指向本机隧道',
|
|
1425
|
+
note: '这条 CNAME 现在指向别处,扫码会把指令发给未知服务器,网关已拒绝下发该地址。',
|
|
1426
|
+
steps: [
|
|
1427
|
+
'到 Cloudflare 该域名的 DNS 页面,检查上面这条 CNAME 指向哪里。',
|
|
1428
|
+
'把它改回本机隧道(或直接删除这条记录)。',
|
|
1429
|
+
'回到面板点「重新检测」,通过后重新生成配对码。',
|
|
1430
|
+
],
|
|
1431
|
+
},
|
|
1432
|
+
moved: {
|
|
1433
|
+
title: '域名已被另一个账号接管',
|
|
1434
|
+
banner: '⚠️ 这个域名不再归本机登录的 Cloudflare 账号',
|
|
1435
|
+
note: 'Cloudflare 把这个域名判成了「已迁移(zone moved)」:公网解析走的是另一个 Cloudflare 账号的那份 zone。'
|
|
1436
|
+
+ '本机写进自己账号的记录一条都不会下发,所以地址永远解析不到——重装插件、重开隧道都没用。',
|
|
1437
|
+
steps: [
|
|
1438
|
+
'先确认两台电脑登录的是不是同一个 Cloudflare 账号:每台都跑一次 cloudflared tunnel login,选到不同账号就会形成「同一域名两份 zone、互相抢」。',
|
|
1439
|
+
expectedNs.length
|
|
1440
|
+
? `在域名注册商处把 NS 改回本账号这一对:${expectedNs.join(' / ')}(当前公网是 ${ns.join(' / ') || '未知'})。隧道与 DNS 记录都在这边,改回即全部恢复。`
|
|
1441
|
+
: '在域名注册商处把 NS 改回本机这个账号分配的一对(Cloudflare 后台该域名页可查)。',
|
|
1442
|
+
'若决定改用现在接管的那份 zone:在那台设备上用对应账号重新授权并重新「开启远程」,本机会重建隧道与新地址。',
|
|
1443
|
+
'两种做法都只能二选一,别在两边同时改;改完等几分钟回到面板点「重新检测」。',
|
|
1444
|
+
],
|
|
1445
|
+
},
|
|
1446
|
+
pending: {
|
|
1447
|
+
title: '域名在本账号未生效',
|
|
1448
|
+
banner: '⚠️ 公网 NS 还没指向本账号',
|
|
1449
|
+
note: '域名已经加进本账号,但注册商处的 NS 还没指向本账号分配的那一对,Cloudflare 因此不下发这里的记录。',
|
|
1450
|
+
steps: [
|
|
1451
|
+
expectedNs.length
|
|
1452
|
+
? `到域名注册商把 NS 改成:${expectedNs.join(' / ')}(当前公网是 ${ns.join(' / ') || '未知'})。`
|
|
1453
|
+
: '到域名注册商把 NS 改成 Cloudflare 给本账号分配的那一对(后台域名页可查)。',
|
|
1454
|
+
'若域名其实已在另一个 Cloudflare 账号里生效,先在那个账号删掉它,否则两边会一直互抢。',
|
|
1455
|
+
'NS 生效通常几分钟到几小时;生效后回到面板点「重新检测」,无需重装插件。',
|
|
1456
|
+
],
|
|
1457
|
+
},
|
|
1458
|
+
'ns-mismatch': {
|
|
1459
|
+
title: 'NS 与本账号不一致',
|
|
1460
|
+
banner: '⚠️ 公网 NS 和本账号分配的对不上',
|
|
1461
|
+
note: '本账号里这个域名显示已生效,但公网解析到的 NS 是另一对,说明注册商那边只改了一半或改错了。',
|
|
1462
|
+
steps: [
|
|
1463
|
+
expectedNs.length ? `确认注册商处的两个 NS 都是:${expectedNs.join(' / ')}。` : '确认注册商处的 NS 与 Cloudflare 后台显示的一致。',
|
|
1464
|
+
`当前公网解析到的是:${ns.join(' / ') || '未知'}。`,
|
|
1465
|
+
'改完后等几分钟,回到面板点「重新检测」。',
|
|
1466
|
+
],
|
|
1467
|
+
},
|
|
1468
|
+
unresolved: {
|
|
1469
|
+
title: '记录还没生效',
|
|
1470
|
+
banner: '⚠️ 域名正常,但查不到这条记录',
|
|
1471
|
+
note: '域名在本账号已生效、NS 也对,只是公网还没查到这台主机的记录:多半是刚写入还在传播,或记录被删了。',
|
|
1472
|
+
steps: [
|
|
1473
|
+
'等 1–2 分钟后点「重新检测」。',
|
|
1474
|
+
'仍不行就点「开启远程」重建记录(会写回本机隧道)。',
|
|
1475
|
+
'如果这个地址刚在另一台电脑上开通过,先确认那台是否把记录改指到了它自己的隧道。',
|
|
1476
|
+
],
|
|
1477
|
+
},
|
|
1478
|
+
};
|
|
1479
|
+
const body = bodies[code] || {
|
|
1480
|
+
title: '域名暂未生效',
|
|
1481
|
+
banner: '⚠️ 公网解析不到这个地址',
|
|
1482
|
+
note: '记录已经写进 Cloudflare,但公网解析不到它。通常是这个域名在当前登录的账号里还没生效。',
|
|
1483
|
+
steps: [
|
|
1484
|
+
'到 Cloudflare 后台看该域名状态是否为「活动 / Active」。',
|
|
1485
|
+
`把域名注册商处的 NS 改成该账号分配的 nameserver${ns.length ? `(当前公网是 ${ns.join('、')})` : ''}。`,
|
|
1486
|
+
'NS 生效后回到面板点「重新检测」即可,无需重装插件。',
|
|
1487
|
+
],
|
|
1488
|
+
};
|
|
1489
|
+
|
|
1490
|
+
showUpdateModal({
|
|
1491
|
+
title: body.title,
|
|
1492
|
+
banner: { text: body.banner, tone: 'warn' },
|
|
1493
|
+
lines: [
|
|
1494
|
+
{ label: '固定域名', value: host || '—' },
|
|
1495
|
+
...(zone?.status ? [{ label: '本账号 zone', value: zone.status }] : []),
|
|
1496
|
+
...(nsLine ? [nsLine] : []),
|
|
1497
|
+
...(ns.length ? [{ label: '公网 NS', value: ns.join(' / ') }] : []),
|
|
1498
|
+
...(accountLine ? [accountLine] : []),
|
|
1499
|
+
],
|
|
1500
|
+
note: body.note,
|
|
1501
|
+
steps: body.steps,
|
|
1502
|
+
actions: [
|
|
1503
|
+
{ label: '关闭', onClick: closeUpdateModal },
|
|
1504
|
+
{
|
|
1505
|
+
label: '重新检测', primary: true,
|
|
1506
|
+
onClick: () => { closeUpdateModal(); recheckDnsAction(lastRemoteBanner); },
|
|
1507
|
+
},
|
|
1508
|
+
],
|
|
1509
|
+
});
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
/**
|
|
1513
|
+
* 域名解析异常时的主按钮出路:强制重探一次。
|
|
1514
|
+
* 通过 → 直接继续「开启 + 出码」,用户不用再点第二次;不通过 → 给排障步骤。
|
|
1515
|
+
*/
|
|
1516
|
+
async function recheckDnsAction(banner) {
|
|
1517
|
+
if (tunnelBusy) return;
|
|
1518
|
+
lastRemoteBanner = banner || lastRemoteBanner;
|
|
1519
|
+
tunnelBusy = true;
|
|
1520
|
+
const mainBtn = panelEl?.querySelector('#dsh-mios-remote-main');
|
|
1521
|
+
if (mainBtn) {
|
|
1522
|
+
mainBtn.disabled = true;
|
|
1523
|
+
mainBtn.textContent = '检测中…';
|
|
1524
|
+
}
|
|
1525
|
+
try {
|
|
1526
|
+
const res = await fetchJson('/mobile/v1/tunnel/recheck-dns', { method: 'POST' }, 25000);
|
|
1527
|
+
await probe();
|
|
1528
|
+
renderSummary();
|
|
1529
|
+
if (res?.ok) {
|
|
1530
|
+
// 解析恢复:把动作链接回去(此时 tunnelBusy 必须释放,否则 remoteMainAction 会直接返回)
|
|
1531
|
+
tunnelBusy = false;
|
|
1532
|
+
if (mainBtn) mainBtn.disabled = false;
|
|
1533
|
+
await remoteMainAction(lastRemoteBanner);
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
// 账号/证书问题与 NS 问题是两码事:重探一万次也不会让跨账号的证书变得可用。
|
|
1537
|
+
// 先把「换账号 login」这条唯一出口摆出来,别让人照着改 NS 白跑。
|
|
1538
|
+
if (res?.credentials?.blocking) {
|
|
1539
|
+
showCredentialHelpModal(res.credentials);
|
|
1540
|
+
return;
|
|
1541
|
+
}
|
|
1542
|
+
showDnsHelpModal(res || {});
|
|
1543
|
+
} catch (err) {
|
|
1544
|
+
showDnsHelpModal({
|
|
1545
|
+
ok: false,
|
|
1546
|
+
warning: err?.body?.error?.message || err?.message || String(err),
|
|
1547
|
+
});
|
|
1548
|
+
} finally {
|
|
1549
|
+
tunnelBusy = false;
|
|
1550
|
+
if (mainBtn) mainBtn.disabled = false;
|
|
1551
|
+
renderSummary();
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
/** 远程主按钮:一条动作链走完「确保在跑 → 出码」。一次点击出码,绝不让人点两下。 */
|
|
1556
|
+
async function remoteMainAction(banner) {
|
|
1557
|
+
if (tunnelBusy || bannerBusy.remote || installBusy) return;
|
|
1558
|
+
lastRemoteBanner = banner || lastRemoteBanner;
|
|
1559
|
+
const mainBtn = panelEl?.querySelector('#dsh-mios-remote-main');
|
|
1560
|
+
// 缺依赖且平台可自动装:先走安装确认,装完再继续开启(避免点了必然失败的 start)
|
|
1561
|
+
if (summary?.tunnel?.executable === 'missing' && summary?.tunnel?.canAutoInstall) {
|
|
1562
|
+
installCloudflaredAction(banner);
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
// 凭据确定不可用(跨账号 / 证书过时):此刻开通必然在写 DNS 处失败。
|
|
1566
|
+
// 已经跑起来的隧道不拦——那种情况用户是在生成配对码,地址本身是通的。
|
|
1567
|
+
const creds = summary?.tunnel?.credentials;
|
|
1568
|
+
if (creds?.blocking && summary?.tunnel?.state !== 'running') {
|
|
1569
|
+
showCredentialHelpModal(creds);
|
|
1570
|
+
return;
|
|
1571
|
+
}
|
|
1572
|
+
// 域名解析异常:先重检,别把人堵在「暂不可开启」上(那就是原来的死锁)
|
|
1573
|
+
if (summary?.tunnel?.dnsOk === false) {
|
|
1574
|
+
// 已经确诊「域名被另一个 Cloudflare 账号接管」:重探改变不了归属,
|
|
1575
|
+
// 直接把原因和两条修法摆出来(弹窗里仍有「重新检测」供改完 NS 后收口)。
|
|
1576
|
+
const zone = summary?.tunnel?.zone;
|
|
1577
|
+
if (zone?.code === 'moved') {
|
|
1578
|
+
showDnsHelpModal({ ok: false, zone, publicNs: zone.publicNs || [] });
|
|
1579
|
+
return;
|
|
1580
|
+
}
|
|
1581
|
+
await recheckDnsAction(banner);
|
|
1582
|
+
return;
|
|
1583
|
+
}
|
|
1584
|
+
const running = summary?.tunnel?.state === 'running' && summary?.tunnel?.dnsOk !== false;
|
|
1585
|
+
try {
|
|
1586
|
+
if (!running) {
|
|
1587
|
+
tunnelBusy = true;
|
|
1588
|
+
if (mainBtn) mainBtn.textContent = '开启中…';
|
|
1589
|
+
let startErr = null;
|
|
1590
|
+
try {
|
|
1591
|
+
await tunnelAction('start');
|
|
1592
|
+
} catch (err) {
|
|
1593
|
+
// 客户端超时 ≠ 服务端失败:cloudflared 可能还在注册路上。
|
|
1594
|
+
// 不直接报错收场,继续轮询等它真的起来(或确认起不来)。
|
|
1595
|
+
startErr = err;
|
|
1596
|
+
}
|
|
1597
|
+
const up = await waitForTunnelUp();
|
|
1598
|
+
if (!up) {
|
|
1599
|
+
// 真没起来才报错;错误信息优先用服务端带回来的
|
|
1600
|
+
const t = summary?.tunnel;
|
|
1601
|
+
const msg = startErr?.body?.error?.message
|
|
1602
|
+
|| (t?.dnsOk === false ? '固定域名尚未在公网生效,请稍后再试' : null)
|
|
1603
|
+
|| startErr?.message || '远程通道未能开启';
|
|
1604
|
+
throw Object.assign(new Error(msg), { body: startErr?.body });
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
bannerBusy.remote = true;
|
|
1608
|
+
if (mainBtn) mainBtn.textContent = '生成中…';
|
|
1609
|
+
mountCode(banner, await issuePairingCode('tunnel'));
|
|
1610
|
+
} catch (err) {
|
|
1611
|
+
// 服务端已经归因到「凭据」时,别把一句 Command failed 摆在提示行上——
|
|
1612
|
+
// 直接把 login 指引弹出来(这是唯一能修好的动作)。
|
|
1613
|
+
if (err?.body?.error?.reason === 'credentials') {
|
|
1614
|
+
showCredentialHelpModal({
|
|
1615
|
+
...(summary?.tunnel?.credentials || {}),
|
|
1616
|
+
hint: err?.body?.error?.message || summary?.tunnel?.credentials?.hint,
|
|
1617
|
+
});
|
|
1618
|
+
} else {
|
|
1619
|
+
const hint = panelEl?.querySelector('#dsh-mios-hint');
|
|
1620
|
+
if (hint) {
|
|
1621
|
+
hint.textContent = `⚠️ ${err?.body?.error?.message || err.message}`;
|
|
1622
|
+
hint.style.display = '';
|
|
1623
|
+
hint.style.color = '#d94f3d';
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
} finally {
|
|
1627
|
+
tunnelBusy = false;
|
|
1628
|
+
bannerBusy.remote = false;
|
|
1629
|
+
renderSummary();
|
|
1630
|
+
}
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
async function stopRemoteAction() {
|
|
1634
|
+
if (tunnelBusy) return;
|
|
1635
|
+
tunnelBusy = true;
|
|
1636
|
+
const stopBtn = panelEl?.querySelector('#dsh-mios-remote-stop');
|
|
1637
|
+
if (stopBtn) stopBtn.textContent = '停止中…';
|
|
1638
|
+
try {
|
|
1639
|
+
await tunnelAction('stop');
|
|
1640
|
+
} catch (err) {
|
|
1641
|
+
const hint = panelEl?.querySelector('#dsh-mios-hint');
|
|
1642
|
+
if (hint) {
|
|
1643
|
+
hint.textContent = `⚠️ ${err?.body?.error?.message || err.message}`;
|
|
1644
|
+
hint.style.display = '';
|
|
1645
|
+
hint.style.color = '#d94f3d';
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
// 停止即收码:远程关了,码还挂在屏幕上只会误导人去扫
|
|
1649
|
+
panelEl?.querySelectorAll('#dsh-mios-remote-main')
|
|
1650
|
+
.forEach((btn) => btn.closest('.dsh-mios-banner')?.querySelectorAll('.dsh-mios-code-slot, .err')
|
|
1651
|
+
.forEach((e) => e.remove()));
|
|
1652
|
+
await probe();
|
|
1653
|
+
renderSummary();
|
|
1654
|
+
tunnelBusy = false;
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
function togglePanel() {
|
|
1658
|
+
if (panelEl) removePanel();
|
|
1659
|
+
else showPanel();
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
// ── banner(slot 渲染,React 元素) ──────────────────────────
|
|
1663
|
+
// 注意:slot 渲染回调不是 React 组件(宿主按回调取元素),不能用 hooks;
|
|
1664
|
+
// 状态点读模块级 summary,新鲜度由 apply 里的轮询保证,
|
|
1665
|
+
// 宿主重渲侧栏时自然取到最新值。面板打开时另有主动 probe()。
|
|
1666
|
+
|
|
1667
|
+
const STATUS_OK = { stroke: '#3fb27f', fill: 'rgba(63,178,127,.18)' };
|
|
1668
|
+
const STATUS_DOWN = { stroke: '#d94f3d', fill: 'rgba(217,79,61,.18)' };
|
|
1669
|
+
|
|
1670
|
+
function PhoneIcon() {
|
|
1671
|
+
return react.createElement('svg', {
|
|
1672
|
+
'aria-hidden': true, focusable: false, width: 16, height: 16,
|
|
1673
|
+
viewBox: '0 0 16 16', fill: 'none', stroke: 'currentColor',
|
|
1674
|
+
strokeWidth: 1.5, strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
1675
|
+
},
|
|
1676
|
+
react.createElement('rect', { x: 4, y: 1, width: 8, height: 14, rx: 2 }),
|
|
1677
|
+
react.createElement('path', { d: 'M7 12h2' }));
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
function apply(ctx) {
|
|
1681
|
+
installStyle();
|
|
1682
|
+
|
|
1683
|
+
// 注入宿主插件管理器:一步更新能力来源。可选能力——用安全读取而非硬依赖,
|
|
1684
|
+
// 拿不到时 pluginManager 保持 null,更新弹窗自动降级为「复制包名」手动安装。
|
|
1685
|
+
try {
|
|
1686
|
+
const remote = (typeof ctx.get === 'function' && ctx.get('remote'))
|
|
1687
|
+
|| ctx.remote || null;
|
|
1688
|
+
pluginManager = remote?.pluginManager || null;
|
|
1689
|
+
} catch { pluginManager = null; }
|
|
1690
|
+
|
|
1691
|
+
// 跨 HMR 重载恢复:一步更新触发插件自更新会卸载本模块,弹窗随之消失。
|
|
1692
|
+
// 发起前把状态写入 localStorage,重载后据此显示「更新完成/进行中」反馈。
|
|
1693
|
+
try { restorePendingUpdate(); } catch { /* 存储不可用时跳过,不影响主流程 */ }
|
|
1694
|
+
|
|
1695
|
+
// 轮询保持 summary 新鲜(banner 状态点 + 面板数据共用)
|
|
1696
|
+
probeUntilUp();
|
|
1697
|
+
const pollTimer = setInterval(probe, 15000);
|
|
1698
|
+
|
|
1699
|
+
// 状态面板打开期间保持轮询:banner 的 useGatewayStatus 只在宿主渲染时活跃
|
|
1700
|
+
const disposeSlot = ctx.slots.inject('sidebar.footer.action', () =>
|
|
1701
|
+
ctx.slots.register(
|
|
1702
|
+
{ name: 'sidebar.footer.action', id: 'dsh-mobile-ios' },
|
|
1703
|
+
({ wide }) => {
|
|
1704
|
+
const ok = !!(summary && summary.ok);
|
|
1705
|
+
const tone = ok ? STATUS_OK : STATUS_DOWN;
|
|
1706
|
+
return react.createElement('button', {
|
|
1707
|
+
className: `dsh-mios-trigger${wide ? '' : ' is-rail'}`,
|
|
1708
|
+
type: 'button',
|
|
1709
|
+
title: PANEL_TITLE,
|
|
1710
|
+
'aria-label': PANEL_TITLE,
|
|
1711
|
+
onClick: togglePanel,
|
|
1712
|
+
},
|
|
1713
|
+
react.createElement('span', {
|
|
1714
|
+
className: 'dsh-mios-dot ' + (ok ? 'ok' : 'down'),
|
|
1715
|
+
style: { background: tone.stroke },
|
|
1716
|
+
}),
|
|
1717
|
+
// 名称左边不再放 SVG 手机图标:wide 模式有「📱」emoji 就够了;
|
|
1718
|
+
// 窄栏(rail)没有名字,图标仍是唯一的视觉标识,保留。
|
|
1719
|
+
wide ? undefined : react.createElement(PhoneIcon),
|
|
1720
|
+
wide ? react.createElement('span', null, BANNER_LABEL) : undefined);
|
|
1721
|
+
}
|
|
1722
|
+
)
|
|
1723
|
+
);
|
|
1724
|
+
|
|
1725
|
+
return () => {
|
|
1726
|
+
clearInterval(pollTimer);
|
|
1727
|
+
disposeSlot();
|
|
1728
|
+
removePanel();
|
|
1729
|
+
document.getElementById(STYLE_ID)?.remove();
|
|
1730
|
+
};
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
// 注意:宿主 inject 数组是「硬依赖」,缺失会让整个模块挂起(pending)。
|
|
1734
|
+
// remote/pluginManager 属可选能力,只声明 slots;安装服务在 apply 内安全获取。
|
|
1735
|
+
const inject = ['slots'];
|
|
1736
|
+
exports.apply = apply;
|
|
1737
|
+
exports.inject = inject;
|
|
1738
|
+
exports.__test = {
|
|
1739
|
+
PORT_CANDIDATES, STATUS_OK, STATUS_DOWN,
|
|
1740
|
+
showUpdateModal, closeUpdateModal, copyToClipboard,
|
|
1741
|
+
showModalToast, offerManualCopy, showDnsHelpModal, recheckDnsAction,
|
|
1742
|
+
showCredentialHelpModal, confirmResetTunnelState, resetTunnelStateAction,
|
|
1743
|
+
performUpdate, checkPluginUpdate, compareVersions, unwrapRemote,
|
|
1744
|
+
setPluginManager: (m) => { pluginManager = m; },
|
|
1745
|
+
probe, remoteMainAction, renderSummary,
|
|
1746
|
+
getSummary: () => summary,
|
|
1747
|
+
setUpdateTimeout: (ms) => { UPDATE_TIMEOUT_MS = ms; },
|
|
1748
|
+
isUpdateInFlight: () => updateInFlight,
|
|
1749
|
+
restorePendingUpdate, readUpdateState, writeUpdateState, clearUpdateState,
|
|
1750
|
+
};
|
|
1751
|
+
|
|
1752
|
+
return module.exports;
|
|
1753
|
+
},
|
|
1754
|
+
});
|