dsh-mobile-ios 0.2.11 → 0.2.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-ios",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "type": "module",
5
5
  "description": "DSH iOS移动伴侣:iPhone/iPad 扫码连接 DSH 桌面端,局域网直连 + Cloudflare Tunnel 固定域名远程两种通道",
6
6
  "main": "entries/plugin.mjs",
@@ -252,46 +252,78 @@ export class LocalProvisioner {
252
252
  if (!fs.existsSync(cert)) throw fail(4021, this.notConfiguredMessage, { detail: this.notConfiguredDetail });
253
253
  }
254
254
 
255
- async #findTunnelByName(label) {
255
+ /** 当前授权账号下的隧道列表;拿不到就返回 null(调用方必须当「未知」处理,不能当「没有」)。 */
256
+ async #listTunnels() {
256
257
  let raw;
257
258
  try {
258
259
  raw = await this.run(['tunnel', 'list', '--output', 'json']);
259
260
  } catch {
260
- return null; // 老版本不支持 --output;交给 create 的报错去说明
261
+ return null; // 老版本不支持 --output,或网络/凭据问题;交给 create 的报错去说明
261
262
  }
262
263
  const start = raw.indexOf('[');
263
264
  if (start < 0) return null;
264
- const list = JSON.parse(raw.slice(start));
265
- const hit = Array.isArray(list) ? list.find((t) => t && t.name === label && !t.deleted_at) : null;
265
+ try {
266
+ const list = JSON.parse(raw.slice(start));
267
+ return Array.isArray(list) ? list : null;
268
+ } catch {
269
+ return null;
270
+ }
271
+ }
272
+
273
+ async #findTunnelByName(label) {
274
+ const list = await this.#listTunnels();
275
+ if (!list) return null; // 老版本不支持 --output;交给 create 的报错去说明
276
+ const hit = list.find((t) => t && t.name === label && !t.deleted_at);
266
277
  return hit?.id ? { id: hit.id } : null;
267
278
  }
268
279
 
280
+ /**
281
+ * 这条 tunnel 是否还在当前授权账号里。
282
+ * true / false 之外还有 null(查不到)——null 必须当作「可能还在」,别因为一次网络抖动
283
+ * 就把用户正常的隧道判死重建。返回 undefined 表示根本没 id 可查。
284
+ */
285
+ async #tunnelInAccount(tunnelId) {
286
+ if (!tunnelId) return undefined;
287
+ const list = await this.#listTunnels();
288
+ if (!list) return null;
289
+ return list.some((t) => t && t.id === tunnelId && !t.deleted_at);
290
+ }
291
+
269
292
  async ensure({ port, force = false } = {}) {
270
293
  if (!Number.isInteger(port) || port <= 0) throw fail(4400, '开通隧道需要有效端口');
271
- // 授权账号可能变过(域名迁移到另一个 Cloudflare 账号后重新 login 就是这种情况):
272
- // 旧 tunnel 属于上一个账号,继续拿它的 id 去 route dns 只会得到
273
- // "tunnel not found" 或更糟的静默失败。账号变了就当新装:重建隧道、保留同一个子域
274
- // (地址不变,用户不用重新记/重新扫)。
294
+ // 授权账号可能变过(域名迁到另一个 Cloudflare 账号后重新 login 就是这种情况):
295
+ // 旧 tunnel 属于上一个账号,继续拿它的 id 去 route dns 只会得到 "tunnel not found",
296
+ // 或者更糟——静默写不进去却以为自己开通成功。真判定有两条线:
297
+ // 1) 状态文件记了 account 且与当前 cert 不一致 → 确知换了账号;
298
+ // 2) 没记(老状态文件)或记不清 → 直接问 Cloudflare 这条 tunnel 还在不在当前账号。
299
+ // 只靠 1) 不够:0.2.11 之前写下的状态文件根本没有 account 字段,那才是升级后的真实起点。
300
+ // 账号变了就当新装:重建隧道、保留同一个子域(地址不变,用户不用重新记/重新扫码)。
275
301
  const account = readZoneCert({ homeDir: this.homeDir })?.accountId || null;
276
302
  let cached = this.state;
277
- let accountChanged = false;
278
- if (cached?.account && account && cached.account !== account) {
279
- this.log(`tunnel 授权账号已变更(${cached.account.slice(0, 8)}… → ${account.slice(0, 8)}…),重建隧道`);
280
- accountChanged = true;
281
- cached = {
282
- ...cached,
283
- account,
284
- tunnelId: null,
285
- credentialsFile: null,
286
- configFile: null,
287
- dnsOk: null,
288
- dnsTampered: false,
289
- zone: null,
290
- };
303
+ let rebuild = false;
304
+ if (cached?.tunnelId) {
305
+ const knownMismatch = Boolean(cached.account && account && cached.account !== account);
306
+ const alive = knownMismatch ? false : await this.#tunnelInAccount(cached.tunnelId);
307
+ if (knownMismatch || alive === false) {
308
+ this.log(knownMismatch
309
+ ? `tunnel 授权账号已变更(${String(cached.account).slice(0, 8)}… → ${String(account).slice(0, 8)}…),重建隧道`
310
+ : `tunnel ${String(cached.tunnelId).slice(0, 8)}… 不在当前授权账号里,重建隧道`);
311
+ rebuild = true;
312
+ cached = {
313
+ ...cached,
314
+ account,
315
+ tunnelId: null,
316
+ credentialsFile: null,
317
+ configFile: null,
318
+ dnsOk: null,
319
+ dnsTampered: false,
320
+ zone: null,
321
+ };
322
+ }
291
323
  }
292
- // 账号变了就必须走完整开通:缓存里的 port/hostname 看着都可用,
293
- // 但它们描述的是上一个账号的隧道,跳过缓存才不会「解析通了就以为没事」。
294
- if (!force && !accountChanged && cached?.hostname && cached.port === port) {
324
+ // 走完整开通:缓存里的 port/hostname 看着都可用,但它们描述的是上一个账号的隧道,
325
+ // 跳过缓存才不会「解析通了就以为没事」。
326
+ if (!force && !rebuild && cached?.hostname && cached.port === port) {
295
327
  // 与 BaseProvisioner 同策略:没确认过就现场验一次(含防篡改评估)
296
328
  if (cached.dnsOk !== true) {
297
329
  const { verdict, zone } = await this.#evaluate(cached.hostname, cached.tunnelId);