dsh-mobile-ios 0.2.17 → 0.2.19

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/lib/client.js CHANGED
@@ -195,6 +195,11 @@ window.__ModuleLoader__.load({
195
195
  .dsh-mios-modal .modal-actions button.primary { border-color:var(--modal-accent);
196
196
  background:rgba(63,178,127,.16); color:var(--modal-accent); }
197
197
  .dsh-mios-modal .modal-actions button:disabled { opacity:.6; cursor:default; }
198
+ /* 兜底文字链(重置本机状态之类):不抢主按钮的位置,但要能看见、能点 */
199
+ .dsh-mios-modal .modal-link { display:block; width:100%; margin:10px 0 0; padding:2px 0;
200
+ background:none; border:none; color:var(--modal-muted); font-family:inherit; font-size:11px;
201
+ text-align:left; text-decoration:underline; cursor:pointer; }
202
+ .dsh-mios-modal .modal-link:hover { color:var(--modal-fg); }
198
203
  /* 复制结果必须看得见:dsh-app:// 页面里剪贴板可能不可用,只改按钮文字容易被无视。
199
204
  .modal-toast 常驻在动作条上方,成功/失败都留痕。 */
200
205
  .dsh-mios-modal .modal-toast { margin:10px 0 0; padding:7px 10px; border-radius:8px; font-size:11px;
@@ -508,15 +513,20 @@ window.__ModuleLoader__.load({
508
513
  // 按钮永远点不到,NS 改对了也不恢复,只能删状态文件(自锁死)。
509
514
  mainBtn.disabled = tunnelBusy || bannerBusy.remote;
510
515
  const needsInstall = tunnel.executable === 'missing';
516
+ // 凭据确定不可用时先把「换账号登录」摆在前面:此刻点开启必然在写 DNS 处失败,
517
+ // 直接给可执行的出口,比让人反复点一个注定失败的按钮有用。
518
+ const credBlocked = Boolean(tunnel.credentials?.blocking) && !running && !needsInstall;
511
519
  mainBtn.textContent = needsInstall && tunnel.canAutoInstall
512
520
  ? '安装 cloudflared 并开启远程'
513
- : dnsBroken
514
- ? (zoneCode === 'moved' ? '查看原因(域名被占用)' : '重新检测域名解析')
515
- : running
516
- ? (bannerBusy.remote ? '生成中…' : '生成配对码(远程)')
517
- : tunnel.mode === 'named'
518
- ? (tunnel.provisioned ? '开启远程并生成配对码' : '开启远程(分配固定地址)')
519
- : '启动隧道(外网连接)';
521
+ : credBlocked
522
+ ? '开启远程(需重新授权)'
523
+ : dnsBroken
524
+ ? (zoneCode === 'moved' ? '查看原因(域名被占用)' : '重新检测域名解析')
525
+ : running
526
+ ? (bannerBusy.remote ? '生成中…' : '生成配对码(远程)')
527
+ : tunnel.mode === 'named'
528
+ ? (tunnel.provisioned ? '开启远程并生成配对码' : '开启远程(分配固定地址)')
529
+ : '启动隧道(外网连接)';
520
530
  }
521
531
  const stopBtn = panelEl.querySelector('#dsh-mios-remote-stop');
522
532
  if (stopBtn) {
@@ -538,7 +548,10 @@ window.__ModuleLoader__.load({
538
548
  const hint = panelEl.querySelector('#dsh-mios-hint');
539
549
  if (hint) {
540
550
  const t = ok ? summary.tunnel : null;
541
- const msg = t?.warning || t?.error || null;
551
+ // 凭据预检不通过是「确定写不了 DNS」:要在用户点按钮之前就说,
552
+ // 而不是等他点了、写失败了,再看到一句 cloudflared 的命令回显。
553
+ const credBlocked = t?.credentials?.blocking ? t.credentials : null;
554
+ const msg = t?.warning || t?.error || credBlocked?.hint || null;
542
555
  hint.textContent = msg ? `⚠️ ${msg}` : '';
543
556
  hint.style.color = t?.error && !t?.warning ? '#d94f3d' : '#e0a03c';
544
557
  hint.style.display = msg ? '' : 'none';
@@ -726,7 +739,7 @@ window.__ModuleLoader__.load({
726
739
  updateModal?.remove();
727
740
  updateModal = null;
728
741
  }
729
- function showUpdateModal({ title, banner, lines, note, steps, actions }) {
742
+ function showUpdateModal({ title, banner, lines, note, steps, actions, footLink }) {
730
743
  closeUpdateModal();
731
744
  const mask = document.createElement('div');
732
745
  mask.className = 'dsh-mios-modal-mask';
@@ -787,10 +800,35 @@ window.__ModuleLoader__.load({
787
800
  const btn = document.createElement('button');
788
801
  btn.textContent = action.label;
789
802
  if (action.primary) btn.className = 'primary';
790
- btn.addEventListener('click', () => action.onClick?.(btn));
803
+ // 异步动作一律包一层:onClick 里抛异常时,原来等价于「点了没反应」。
804
+ // 现在至少把错误摆到弹窗里(toast),用户能复制给我们。
805
+ btn.addEventListener('click', () => {
806
+ try {
807
+ const ret = action.onClick?.(btn);
808
+ if (ret && typeof ret.catch === 'function') {
809
+ ret.catch((err) => {
810
+ console.error('modal action failed', err);
811
+ showModalToast(`操作失败:${err?.message || err}`, 'error');
812
+ });
813
+ }
814
+ } catch (err) {
815
+ console.error('modal action failed', err);
816
+ showModalToast(`操作失败:${err?.message || err}`, 'error');
817
+ }
818
+ });
791
819
  bar.append(btn);
792
820
  }
793
821
  box.append(bar);
822
+ // 兜底动作(如「重置本机状态」)做成弱化的文字链,避免主按钮挤成四个
823
+ if (footLink) {
824
+ const link = document.createElement('button');
825
+ link.className = 'modal-link';
826
+ link.textContent = footLink.label;
827
+ link.addEventListener('click', () => {
828
+ try { footLink.onClick?.(); } catch (err) { console.error('modal link failed', err); }
829
+ });
830
+ box.append(link);
831
+ }
794
832
  mask.append(box);
795
833
  mask.addEventListener('click', (e) => { if (e.target === mask) closeUpdateModal(); });
796
834
  document.body.append(mask);
@@ -1290,6 +1328,335 @@ window.__ModuleLoader__.load({
1290
1328
  /** 记住最近一次触发远程动作的 banner:弹窗里的「重新检测」要能继续同一条链。 */
1291
1329
  let lastRemoteBanner = null;
1292
1330
 
1331
+ /** 授权轮询句柄(同一时刻只允许一条,重复点不会起两个计时器)。 */
1332
+ let loginPoll = null;
1333
+
1334
+ function stopLoginPoll() {
1335
+ if (loginPoll) {
1336
+ clearInterval(loginPoll);
1337
+ loginPoll = null;
1338
+ }
1339
+ }
1340
+
1341
+ /**
1342
+ * 弹窗按钮的忙碌态。
1343
+ *
1344
+ * 为什么必须有:原来「重新检测 / 重置 / 重新授权」都是 `closeUpdateModal()` 先关窗
1345
+ * 再发请求——请求最长 25 秒,这段时间**界面上什么都不发生**。用户看到的就是
1346
+ * 「点了没反应」,于是反复点、或者以为插件坏了。
1347
+ * 现在:按钮立刻变灰并换文案,同一弹窗内原地出结果,失败也一定看得见。
1348
+ */
1349
+ function setModalBusy(activeBtn, label) {
1350
+ const box = updateModal?.querySelector?.('.dsh-mios-modal');
1351
+ const btns = box?.querySelectorAll ? Array.from(box.querySelectorAll('.modal-actions button')) : [];
1352
+ const prev = new Map();
1353
+ for (const b of btns) {
1354
+ prev.set(b, b.textContent);
1355
+ b.disabled = true;
1356
+ }
1357
+ if (activeBtn && label) activeBtn.textContent = label;
1358
+ return () => {
1359
+ for (const b of btns) {
1360
+ b.disabled = false;
1361
+ if (prev.has(b)) b.textContent = prev.get(b);
1362
+ }
1363
+ };
1364
+ }
1365
+
1366
+ /**
1367
+ * 凭据不可用(登录的账号与域名不匹配 / 证书过时 / 授权错了域名)。
1368
+ *
1369
+ * 现场(2026-11-21,Mac mini):cert.pem 属于另一个 Cloudflare 账号,面板提示
1370
+ * 「账号不对」,用户重装、重启、点重置、点重新检测全都无效——因为这三个动作
1371
+ * 都**不会**换掉 cert.pem。唯一有效的是用持有该域名的账号重新授权。
1372
+ * 所以这里把它做成一步:点「重新授权」→ 浏览器里点允许 → 面板自动继续开通。
1373
+ *
1374
+ * @param {object} credentials 凭据结论(来自 summary.tunnel.credentials)
1375
+ * @param {object} [opts]
1376
+ * @param {boolean} [opts.again] 刚做过一次检测、结论没变
1377
+ * @param {boolean} [opts.afterLogin] 刚到浏览器授权回来的复检
1378
+ */
1379
+ function showCredentialHelpModal(credentials = {}, opts = {}) {
1380
+ const stateText = credentials.hint || '当前登录的 Cloudflare 账号无法管理这个域名';
1381
+ const certAccount = credentials.certAccountName
1382
+ || (credentials.certAccountId ? `账号 ${String(credentials.certAccountId).slice(0, 8)}…` : null);
1383
+ const scopedOther = credentials.state === 'zone-scoped-other' && credentials.certZoneName;
1384
+ const lines = [
1385
+ ...(certAccount ? [{ label: '当前证书账号', value: certAccount }] : []),
1386
+ ...(credentials.zoneName ? [{ label: '固定地址域名', value: credentials.zoneName }] : []),
1387
+ ...(scopedOther ? [{ label: '证书授权域名', value: credentials.certZoneName }] : []),
1388
+ ...(credentials.zoneStatus ? [{ label: '域名状态', value: credentials.zoneStatus }] : []),
1389
+ ];
1390
+ const bannerText = opts.afterLogin
1391
+ ? `⚠️ 授权后仍然不行:${stateText}`
1392
+ : opts.again
1393
+ ? `⚠️ 重新检测结论没变:${stateText}`
1394
+ : `⚠️ ${stateText}`;
1395
+ showUpdateModal({
1396
+ title: opts.afterLogin ? '授权完成,但这个账号不行' : '登录的 Cloudflare 账号不对',
1397
+ banner: { text: bannerText, tone: 'warn' },
1398
+ lines,
1399
+ note: '固定地址写在 Cloudflare 的域名下,只有「持有该域名的账号」才写得进去。'
1400
+ + '重装插件、重启宿主、点重置都不会换掉证书——只有重新授权能修好。'
1401
+ + '授权后本机会自动重建隧道并沿用同一个地址,手机不需要重新配对。',
1402
+ steps: [
1403
+ '点下面的「重新授权(浏览器)」,面板会调起 Cloudflare 的授权页。',
1404
+ scopedOther
1405
+ ? `浏览器里务必选「${credentials.zoneName || '固定地址域名'}」所在的账号(上次选成了 ${credentials.certZoneName})。`
1406
+ : '浏览器里选择「持有该域名」的账号,点「允许 / Authorize」。',
1407
+ '回到面板不用做别的:授权成功后会自动继续开通固定地址。',
1408
+ ],
1409
+ footLink: { label: '重置本机状态(清掉本机的旧地址记录)', onClick: () => confirmResetTunnelState() },
1410
+ actions: [
1411
+ { label: '关闭', onClick: closeUpdateModal },
1412
+ { label: '重新检测', onClick: (btn) => recheckDnsAction(lastRemoteBanner, btn) },
1413
+ { label: '重新授权(浏览器)', primary: true, onClick: (btn) => startTunnelLogin(btn) },
1414
+ ],
1415
+ });
1416
+ }
1417
+
1418
+ /**
1419
+ * 发起浏览器授权:起 `cloudflared tunnel login`,把链接摆到弹窗里,然后轮询结果。
1420
+ *
1421
+ * 为什么要轮询而不是等 POST 返回:cloudflared 会一直挂着等浏览器回调,
1422
+ * 同步等会把 HTTP 请求挂死 5 分钟。POST 只负责「起进程 + 尽早给链接」。
1423
+ */
1424
+ async function startTunnelLogin(btn) {
1425
+ const restore = setModalBusy(btn, '启动中…');
1426
+ stopLoginPoll();
1427
+ let first = null;
1428
+ try {
1429
+ first = await fetchJson('/mobile/v1/tunnel/login', { method: 'POST' }, 20000);
1430
+ } catch (err) {
1431
+ restore();
1432
+ showUpdateModal({
1433
+ title: '无法发起授权',
1434
+ banner: { text: '❌ 没能调起 cloudflared', tone: 'error' },
1435
+ lines: [],
1436
+ note: `${err?.body?.error?.message || err?.message || String(err)}。`
1437
+ + '仍可在终端执行 cloudflared tunnel login 完成授权,然后点「重新检测」。',
1438
+ steps: [],
1439
+ actions: [
1440
+ { label: '关闭', onClick: closeUpdateModal },
1441
+ { label: '重试', primary: true, onClick: (b) => startTunnelLogin(b) },
1442
+ ],
1443
+ });
1444
+ return;
1445
+ }
1446
+ renderLoginModal(first);
1447
+ pollLogin();
1448
+ }
1449
+
1450
+ /** 授权进行中的弹窗:链接、进度、取消。链接拿不到就说明还在等 cloudflared 打印。 */
1451
+ function renderLoginModal(status = {}) {
1452
+ const url = status.url || null;
1453
+ showUpdateModal({
1454
+ title: '等待浏览器授权',
1455
+ banner: {
1456
+ text: status.state === 'ok' ? '✅ 授权完成' : '⏳ 正在等待 Cloudflare 授权…',
1457
+ tone: status.state === 'ok' ? 'success' : 'info',
1458
+ },
1459
+ lines: url ? [{ label: '授权链接', value: url }] : [],
1460
+ note: url
1461
+ ? '浏览器没自动打开的话,点「复制授权链接」用任意浏览器打开;授权完成后回到面板即可,不需要保持那个页面。'
1462
+ : '正在启动 cloudflared 并获取授权链接,通常 1–2 秒。',
1463
+ steps: [
1464
+ '在打开页面里选择持有该域名的 Cloudflare 账号。',
1465
+ '点「允许 / Authorize」,浏览器会跳回本机完成授权。',
1466
+ '面板会自动检测结果并继续开通,不需要再点别的。',
1467
+ ],
1468
+ actions: [
1469
+ { label: '取消授权', onClick: () => cancelTunnelLogin() },
1470
+ ...(url ? [{ label: '复制授权链接', onClick: (btn) => copyLoginUrl(url, btn) }] : []),
1471
+ ],
1472
+ });
1473
+ }
1474
+
1475
+ async function copyLoginUrl(url, btn) {
1476
+ const ok = await copyToClipboard(url);
1477
+ showModalToast(ok ? '授权链接已复制' : '复制失败:请手动选中上面的链接复制', ok ? 'success' : 'error');
1478
+ if (btn) btn.textContent = ok ? '已复制' : '复制失败';
1479
+ }
1480
+
1481
+ async function cancelTunnelLogin() {
1482
+ stopLoginPoll();
1483
+ try { await fetchJson('/mobile/v1/tunnel/login/cancel', { method: 'POST' }, 8000); } catch { /* 已结束 */ }
1484
+ closeUpdateModal();
1485
+ }
1486
+
1487
+ /** 轮询授权结果;成功后自动复检并继续开通。 */
1488
+ function pollLogin() {
1489
+ stopLoginPoll();
1490
+ const startedAt = Date.now();
1491
+ let lastUrl = null;
1492
+ loginPoll = setInterval(async () => {
1493
+ // 服务端 5 分钟超时,这里多留一分钟余量,避免界面上的计时器先死掉
1494
+ if (Date.now() - startedAt > 6 * 60_000) {
1495
+ stopLoginPoll();
1496
+ closeUpdateModal();
1497
+ return;
1498
+ }
1499
+ let st = null;
1500
+ try {
1501
+ st = await fetchJson('/mobile/v1/tunnel/login', {}, 8000);
1502
+ } catch {
1503
+ return; // 网关暂时不可达:继续轮询,不要把一次网络抖动画成失败
1504
+ }
1505
+ if (!st) return;
1506
+ if (st.state === 'waiting') {
1507
+ // 链接晚到:只在第一次拿到时重绘,避免每 1.5 秒闪一次
1508
+ if (st.url && st.url !== lastUrl) {
1509
+ lastUrl = st.url;
1510
+ renderLoginModal(st);
1511
+ }
1512
+ return;
1513
+ }
1514
+ stopLoginPoll();
1515
+ if (st.state === 'ok') {
1516
+ await afterLoginOk();
1517
+ } else if (st.state === 'canceled') {
1518
+ closeUpdateModal();
1519
+ } else {
1520
+ showUpdateModal({
1521
+ title: '授权没有完成',
1522
+ banner: { text: '❌ 浏览器授权未完成', tone: 'error' },
1523
+ lines: [],
1524
+ note: `${st.error || '没有拿到新的证书'}。`
1525
+ + '可以再试一次;如果浏览器打不开,就在这台电脑的终端执行 cloudflared tunnel login。',
1526
+ steps: [],
1527
+ actions: [
1528
+ { label: '关闭', onClick: closeUpdateModal },
1529
+ { label: '重新授权', primary: true, onClick: (b) => startTunnelLogin(b) },
1530
+ ],
1531
+ });
1532
+ }
1533
+ }, 1500);
1534
+ }
1535
+
1536
+ /**
1537
+ * 授权成功之后:强制重检 → 结论变了就继续开通,没变就说清「换了账号还是不行」。
1538
+ *
1539
+ * 关键点:这里必须**强制**重检(force),否则凭据结论还挂在旧证书的 TTL 缓存上,
1540
+ * 用户刚授权完却仍看到「账号不对」,会以为授权白做了。
1541
+ */
1542
+ async function afterLoginOk() {
1543
+ showUpdateModal({
1544
+ title: '授权完成',
1545
+ banner: { text: '✅ 证书已更新,正在验证账号权限', tone: 'success' },
1546
+ lines: [],
1547
+ note: '正在用新证书检查这台电脑能不能写这个域名的 DNS。',
1548
+ steps: [],
1549
+ actions: [],
1550
+ });
1551
+ let res = null;
1552
+ try {
1553
+ res = await fetchJson('/mobile/v1/tunnel/recheck-dns', { method: 'POST' }, 30000);
1554
+ } catch (err) {
1555
+ // 重检失败也要给出下一步,不能只关窗
1556
+ showUpdateModal({
1557
+ title: '授权完成,但检测失败',
1558
+ banner: { text: '⚠️ 新证书已就位,检查没跑完', tone: 'warn' },
1559
+ lines: [],
1560
+ note: `${err?.body?.error?.message || err?.message || String(err)}。可点「重新检测」再试一次。`,
1561
+ steps: [],
1562
+ actions: [
1563
+ { label: '关闭', onClick: closeUpdateModal },
1564
+ { label: '重新检测', primary: true, onClick: (btn) => recheckDnsAction(lastRemoteBanner, btn) },
1565
+ ],
1566
+ });
1567
+ return;
1568
+ }
1569
+ await probe();
1570
+ renderSummary();
1571
+ const creds = res?.credentials || summary?.tunnel?.credentials || null;
1572
+ if (creds?.blocking) {
1573
+ showCredentialHelpModal({ ...creds, hint: creds.hint }, { afterLogin: true });
1574
+ return;
1575
+ }
1576
+ if (res && res.ok === false) {
1577
+ showDnsHelpModal(res);
1578
+ return;
1579
+ }
1580
+ await remoteMainAction(lastRemoteBanner);
1581
+ }
1582
+
1583
+ /** 重置是「清本机状态」,不是删云端隧道:必须讲清后果再执行。 */
1584
+ function confirmResetTunnelState() {
1585
+ showUpdateModal({
1586
+ title: '重置本机固定地址状态',
1587
+ banner: { text: '⚠️ 只清本机状态,不动 Cloudflare', tone: 'warn' },
1588
+ lines: [],
1589
+ note: '将删除本机的固定地址记录与 ingress 配置(不删除 ~/.cloudflared/cert.pem,'
1590
+ + '也不删除 Cloudflare 上的隧道与 DNS 记录)。重置后需要重新「开启远程」才会分配地址;'
1591
+ + '如果旧地址仍指向这台机器,新地址会换一个前缀。',
1592
+ steps: [],
1593
+ actions: [
1594
+ { label: '取消', onClick: showCredentialHelpModal.bind(null, summary?.tunnel?.credentials || {}, {}) },
1595
+ { label: '确认重置', primary: true, onClick: (btn) => resetTunnelStateAction(btn) },
1596
+ ],
1597
+ });
1598
+ }
1599
+
1600
+
1601
+ async function resetTunnelStateAction(btn) {
1602
+ const restore = setModalBusy(btn, '重置中…');
1603
+ showUpdateModal({
1604
+ title: '正在重置',
1605
+ banner: { text: '⏳ 清理本机固定地址状态', tone: 'info' },
1606
+ lines: [],
1607
+ note: '正在停止隧道并删除本机状态与 ingress 配置,请稍候(通常 1–3 秒)。',
1608
+ actions: [],
1609
+ });
1610
+ try {
1611
+ const res = await fetchJson('/mobile/v1/tunnel/reset', { method: 'POST' }, 25000);
1612
+ // 重置只清本机状态,**不会**换掉 cert.pem:如果账号问题还在,
1613
+ // 这里必须当场说清,否则用户点完「开启远程」又看到同一个弹窗,
1614
+ // 只会得出「重置也没用」的结论。所以重置后立刻复检一次。
1615
+ await probe();
1616
+ renderSummary();
1617
+ const creds = summary?.tunnel?.credentials || null;
1618
+ const stillBlocked = Boolean(creds?.blocking);
1619
+ showUpdateModal({
1620
+ title: stillBlocked ? '已重置,但账号问题还在' : '已重置',
1621
+ banner: stillBlocked
1622
+ ? { text: '⚠️ 本机状态已清空,但登录的 Cloudflare 账号仍管不了这个域名', tone: 'warn' }
1623
+ : { text: '✅ 本机固定地址状态已清空', tone: 'success' },
1624
+ lines: res?.removed?.length ? [{ label: '已删除', value: res.removed.join('、') }] : [],
1625
+ note: stillBlocked
1626
+ ? '重置只清本机记录,不会换掉 ~/.cloudflared/cert.pem。要让固定地址生效,必须用持有该域名的账号重新授权。'
1627
+ : res?.hadState
1628
+ ? 'Cloudflare 上的隧道与 DNS 记录未删除。点「开启远程」会重新分配固定地址。'
1629
+ : '本机本来就没有固定地址状态。点「开启远程」开始分配。',
1630
+ steps: [],
1631
+ actions: stillBlocked
1632
+ ? [
1633
+ { label: '关闭', onClick: closeUpdateModal },
1634
+ { label: '重新授权(浏览器)', primary: true, onClick: (b) => startTunnelLogin(b) },
1635
+ ]
1636
+ : [
1637
+ { label: '关闭', onClick: closeUpdateModal },
1638
+ {
1639
+ label: '开启远程', primary: true,
1640
+ onClick: () => { closeUpdateModal(); remoteMainAction(lastRemoteBanner); },
1641
+ },
1642
+ ],
1643
+ });
1644
+ } catch (err) {
1645
+ restore();
1646
+ showUpdateModal({
1647
+ title: '重置失败',
1648
+ banner: { text: '❌ 未能重置本机状态', tone: 'error' },
1649
+ lines: [],
1650
+ note: `${err?.body?.error?.message || err?.message || String(err)}。可稍后重试,或在终端删除 ~/.dsh/mobile-access/tunnel-named.json。`,
1651
+ steps: [],
1652
+ actions: [
1653
+ { label: '关闭', onClick: closeUpdateModal },
1654
+ { label: '重试', primary: true, onClick: (b) => resetTunnelStateAction(b) },
1655
+ ],
1656
+ });
1657
+ }
1658
+ }
1659
+
1293
1660
  /**
1294
1661
  * 域名未生效时的排障弹窗。
1295
1662
  *
@@ -1393,7 +1760,7 @@ window.__ModuleLoader__.load({
1393
1760
  { label: '关闭', onClick: closeUpdateModal },
1394
1761
  {
1395
1762
  label: '重新检测', primary: true,
1396
- onClick: () => { closeUpdateModal(); recheckDnsAction(lastRemoteBanner); },
1763
+ onClick: (btn) => recheckDnsAction(lastRemoteBanner, btn),
1397
1764
  },
1398
1765
  ],
1399
1766
  });
@@ -1403,15 +1770,24 @@ window.__ModuleLoader__.load({
1403
1770
  * 域名解析异常时的主按钮出路:强制重探一次。
1404
1771
  * 通过 → 直接继续「开启 + 出码」,用户不用再点第二次;不通过 → 给排障步骤。
1405
1772
  */
1406
- async function recheckDnsAction(banner) {
1407
- if (tunnelBusy) return;
1773
+ async function recheckDnsAction(banner, btn) {
1774
+ // 之前这里是 `if (tunnelBusy) return` —— 定时轮询/重复点击时界面完全没反应,
1775
+ // 用户只能理解为「按钮坏了」。现在至少给一句「正在检测」。
1776
+ if (tunnelBusy) {
1777
+ showModalToast('正在检测中,请稍候…', 'error');
1778
+ return;
1779
+ }
1408
1780
  lastRemoteBanner = banner || lastRemoteBanner;
1409
1781
  tunnelBusy = true;
1782
+ const restore = setModalBusy(btn, '检测中…');
1410
1783
  const mainBtn = panelEl?.querySelector('#dsh-mios-remote-main');
1411
1784
  if (mainBtn) {
1412
1785
  mainBtn.disabled = true;
1413
1786
  mainBtn.textContent = '检测中…';
1414
1787
  }
1788
+ // 已有结论时的差异感:结论没变要写在弹窗标题上,否则「关掉又弹出一样的窗」
1789
+ // 和「点了没反应」在用户眼里是同一件事。
1790
+ const before = summary?.tunnel?.credentials?.state || null;
1415
1791
  try {
1416
1792
  const res = await fetchJson('/mobile/v1/tunnel/recheck-dns', { method: 'POST' }, 25000);
1417
1793
  await probe();
@@ -1423,6 +1799,13 @@ window.__ModuleLoader__.load({
1423
1799
  await remoteMainAction(lastRemoteBanner);
1424
1800
  return;
1425
1801
  }
1802
+ // 账号/证书问题与 NS 问题是两码事:重探一万次也不会让跨账号的证书变得可用。
1803
+ // 先把「换账号 login」这条唯一出口摆出来,别让人照着改 NS 白跑。
1804
+ if (res?.credentials?.blocking) {
1805
+ const again = Boolean(before && before === res.credentials.state);
1806
+ showCredentialHelpModal(res.credentials, { again });
1807
+ return;
1808
+ }
1426
1809
  showDnsHelpModal(res || {});
1427
1810
  } catch (err) {
1428
1811
  showDnsHelpModal({
@@ -1430,6 +1813,7 @@ window.__ModuleLoader__.load({
1430
1813
  warning: err?.body?.error?.message || err?.message || String(err),
1431
1814
  });
1432
1815
  } finally {
1816
+ restore();
1433
1817
  tunnelBusy = false;
1434
1818
  if (mainBtn) mainBtn.disabled = false;
1435
1819
  renderSummary();
@@ -1438,7 +1822,11 @@ window.__ModuleLoader__.load({
1438
1822
 
1439
1823
  /** 远程主按钮:一条动作链走完「确保在跑 → 出码」。一次点击出码,绝不让人点两下。 */
1440
1824
  async function remoteMainAction(banner) {
1441
- if (tunnelBusy || bannerBusy.remote || installBusy) return;
1825
+ if (tunnelBusy || bannerBusy.remote || installBusy) {
1826
+ // 静默 return 是「点了没反应」的主要来源之一:告诉用户正在做什么。
1827
+ showModalToast(tunnelBusy ? '上一步还没结束,请稍候…' : '正在生成配对码,请稍候…', 'error');
1828
+ return;
1829
+ }
1442
1830
  lastRemoteBanner = banner || lastRemoteBanner;
1443
1831
  const mainBtn = panelEl?.querySelector('#dsh-mios-remote-main');
1444
1832
  // 缺依赖且平台可自动装:先走安装确认,装完再继续开启(避免点了必然失败的 start)
@@ -1446,6 +1834,13 @@ window.__ModuleLoader__.load({
1446
1834
  installCloudflaredAction(banner);
1447
1835
  return;
1448
1836
  }
1837
+ // 凭据确定不可用(跨账号 / 证书过时):此刻开通必然在写 DNS 处失败。
1838
+ // 已经跑起来的隧道不拦——那种情况用户是在生成配对码,地址本身是通的。
1839
+ const creds = summary?.tunnel?.credentials;
1840
+ if (creds?.blocking && summary?.tunnel?.state !== 'running') {
1841
+ showCredentialHelpModal(creds);
1842
+ return;
1843
+ }
1449
1844
  // 域名解析异常:先重检,别把人堵在「暂不可开启」上(那就是原来的死锁)
1450
1845
  if (summary?.tunnel?.dnsOk === false) {
1451
1846
  // 已经确诊「域名被另一个 Cloudflare 账号接管」:重探改变不了归属,
@@ -1485,11 +1880,20 @@ window.__ModuleLoader__.load({
1485
1880
  if (mainBtn) mainBtn.textContent = '生成中…';
1486
1881
  mountCode(banner, await issuePairingCode('tunnel'));
1487
1882
  } catch (err) {
1488
- const hint = panelEl?.querySelector('#dsh-mios-hint');
1489
- if (hint) {
1490
- hint.textContent = `⚠️ ${err?.body?.error?.message || err.message}`;
1491
- hint.style.display = '';
1492
- hint.style.color = '#d94f3d';
1883
+ // 服务端已经归因到「凭据」时,别把一句 Command failed 摆在提示行上——
1884
+ // 直接把 login 指引弹出来(这是唯一能修好的动作)。
1885
+ if (err?.body?.error?.reason === 'credentials') {
1886
+ showCredentialHelpModal({
1887
+ ...(summary?.tunnel?.credentials || {}),
1888
+ hint: err?.body?.error?.message || summary?.tunnel?.credentials?.hint,
1889
+ }, { again: true });
1890
+ } else {
1891
+ const hint = panelEl?.querySelector('#dsh-mios-hint');
1892
+ if (hint) {
1893
+ hint.textContent = `⚠️ ${err?.body?.error?.message || err.message}`;
1894
+ hint.style.display = '';
1895
+ hint.style.color = '#d94f3d';
1896
+ }
1493
1897
  }
1494
1898
  } finally {
1495
1899
  tunnelBusy = false;
@@ -1607,6 +2011,8 @@ window.__ModuleLoader__.load({
1607
2011
  PORT_CANDIDATES, STATUS_OK, STATUS_DOWN,
1608
2012
  showUpdateModal, closeUpdateModal, copyToClipboard,
1609
2013
  showModalToast, offerManualCopy, showDnsHelpModal, recheckDnsAction,
2014
+ showCredentialHelpModal, confirmResetTunnelState, resetTunnelStateAction,
2015
+ startTunnelLogin, renderLoginModal, pollLogin, stopLoginPoll, afterLoginOk, cancelTunnelLogin,
1610
2016
  performUpdate, checkPluginUpdate, compareVersions, unwrapRemote,
1611
2017
  setPluginManager: (m) => { pluginManager = m; },
1612
2018
  probe, remoteMainAction, renderSummary,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-ios",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "type": "module",
5
5
  "description": "DSH iOS移动伴侣:iPhone/iPad 扫码连接 DSH 桌面端,局域网直连 + Cloudflare Tunnel 固定域名远程两种通道",
6
6
  "main": "entries/plugin.mjs",
@@ -29,7 +29,7 @@
29
29
  "scripts": {
30
30
  "start": "node entries/sidecar.mjs --host=mock --dev",
31
31
  "start:lan": "node entries/sidecar.mjs --host=mock --dev --lan=true",
32
- "test": "node test/selftest.mjs && node test/version.mjs && node test/lan-ip.mjs && node test/update-dialog.mjs && node test/ops.mjs && node test/resilience.mjs && node test/borrowed.mjs && node test/mux-table.mjs && node test/live-frames.mjs && node test/artifacts.mjs && node test/attachments.mjs && node test/session-actions.mjs && node test/workspace-actions.mjs && node test/workspace-index.mjs && node test/enhance.mjs && node test/tunnel-named.mjs && node test/tar.mjs && node test/cloudflared-install.mjs && node test/install-continue.mjs && node test/dns-recheck.mjs && node test/plugin-selfupdate.mjs && node test/zone-moved.mjs",
32
+ "test": "node test/selftest.mjs && node test/version.mjs && node test/lan-ip.mjs && node test/update-dialog.mjs && node test/ops.mjs && node test/resilience.mjs && node test/borrowed.mjs && node test/mux-table.mjs && node test/live-frames.mjs && node test/artifacts.mjs && node test/attachments.mjs && node test/session-actions.mjs && node test/workspace-actions.mjs && node test/workspace-index.mjs && node test/enhance.mjs && node test/tunnel-named.mjs && node test/tar.mjs && node test/cloudflared-install.mjs && node test/install-continue.mjs && node test/dns-recheck.mjs && node test/tunnel-credentials.mjs && node test/tunnel-login.mjs && node test/plugin-selfupdate.mjs && node test/zone-moved.mjs",
33
33
  "probe": "node spike0/probe.mjs"
34
34
  },
35
35
  "dsh": {
@@ -18,12 +18,13 @@
18
18
 
19
19
  import crypto from 'node:crypto';
20
20
  import fs from 'node:fs';
21
+ import os from 'node:os';
21
22
  import path from 'node:path';
22
23
  import { fail } from './protocol.mjs';
23
24
  import { deriveSubdomain, isValidLabel } from './dns-label.mjs';
24
25
  import { DNS_PENDING_HINT, DNS_TAMPER_HINT, dnsWarningFor, evaluateDnsAnswer, probeDns } from './dns-probe.mjs';
25
- import { LocalProvisioner, sanitizeZone } from './provision-local.mjs';
26
- import { diagnoseZone, findDnsOwner, ZONE_STATUS_HINT } from './zone-health.mjs';
26
+ import { LocalProvisioner, LocalStateProvisioner, sanitizeZone } from './provision-local.mjs';
27
+ import { diagnoseZone, findDnsOwner, verifyCertAccess, ZONE_STATUS_HINT } from './zone-health.mjs';
27
28
 
28
29
  // 兼容既有调用方与测试:子域规则从 dns-label.mjs 来,这里一并导出
29
30
  export { deriveSubdomain, isValidLabel };
@@ -266,6 +267,26 @@ class BaseProvisioner {
266
267
  get notConfiguredDetail() {
267
268
  return null;
268
269
  }
270
+
271
+ /** 非本地形态没有 cert.pem 可预检;面板统一按「无凭据信息」处理。 */
272
+ get credentials() {
273
+ return null;
274
+ }
275
+
276
+ /** 重置本机状态:token 形态由状态文件承载,删掉即「重新开通」。 */
277
+ async reset() {
278
+ let removed = [];
279
+ try {
280
+ if (this.stateFile && fs.statSync(this.stateFile).isFile()) {
281
+ fs.unlinkSync(this.stateFile);
282
+ removed = [path.basename(this.stateFile)];
283
+ }
284
+ } catch { /* 不存在即目标状态 */ }
285
+ const hadState = Boolean(this.state?.hostname);
286
+ this._state = null;
287
+ this._zone = null;
288
+ return { reset: true, hadState, removed, hostname: null, provisioned: false };
289
+ }
269
290
  }
270
291
 
271
292
  /**
@@ -670,18 +691,34 @@ export function createProvisioner(config = {}, { log = () => {}, stateFile = nul
670
691
  stateFile,
671
692
  configFile: config.configFile || null,
672
693
  seed: config.seed || null,
694
+ // 默认当前用户;显式传入便于测试与「非标准 home」的宿主环境
695
+ homeDir: config.homeDir || os.homedir(),
673
696
  log,
674
697
  // 生产路径才接真实探测:测试直接 new LocalProvisioner 时保持零网络依赖
675
698
  zoneDiagnose: diagnoseZone,
676
699
  ownerLookup: findDnsOwner,
700
+ // 开通前凭据预检:跨账号的 cert.pem 必须在写 DNS **之前**被识别出来
701
+ verifyAccess: verifyCertAccess,
702
+ hostname: os.hostname(),
677
703
  });
678
704
  }
679
705
  if (kind === 'state') {
680
- return new StateProvisioner({
706
+ // 先按 token 形态找;找不到再按**本地管理型**状态找(cert.pem 被删了也能继续用固定地址)。
707
+ // 返回 null 的语义是「这台机器确实没有可复用的固定地址」,而不是「凭据不全就降级成随机地址」。
708
+ const byToken = new StateProvisioner({
709
+ stateFile,
710
+ baseDomain: config.baseDomain || 'matrixboard.cn',
711
+ log,
712
+ });
713
+ if (byToken.configured) return byToken;
714
+ const byLocal = new LocalStateProvisioner({
681
715
  stateFile,
716
+ configFile: config.configFile || null,
682
717
  baseDomain: config.baseDomain || 'matrixboard.cn',
718
+ homeDir: config.homeDir || os.homedir(),
683
719
  log,
684
720
  });
721
+ return byLocal.configured ? byLocal : null;
685
722
  }
686
723
  if (kind === 'hosted') {
687
724
  const provisioner = new HostedProvisioner({