dsh-plugin-weapp-gateway 0.8.37 → 0.8.38

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-plugin-weapp-gateway",
3
- "version": "0.8.37",
3
+ "version": "0.8.38",
4
4
  "description": "鲸尾网关助手:DeepSeek Harness 移动客户端连接与会话同步,支持安全配对、远程访问、上下文、编队、额度和语音输入",
5
5
  "main": "lib/index.mjs",
6
6
  "files": [
@@ -192,11 +192,24 @@ class AgentSession {
192
192
  }
193
193
 
194
194
  open(packet) {
195
- if (!this.ready || !packet || packet.length < 9 + 17 || packet[0] !== PACKET_SEALED) return null;
195
+ this.lastOpenFailure = null;
196
+ if (!this.ready) { this.lastOpenFailure = 'not-ready'; return null; }
197
+ if (!packet || packet.length < 9 + 17 || packet[0] !== PACKET_SEALED) {
198
+ this.lastOpenFailure = 'format';
199
+ return null;
200
+ }
196
201
  const counter = readCounter(packet, 1);
197
- if (counter < this.rxCounter) return null;
202
+ // 重复帧(这条管道已经处理过该 counter)不是密钥错误:排队/回放/重连边界
203
+ // 上都可能发生。丢弃即可,绝不能因此关闭整条管道。
204
+ if (counter < this.rxCounter) {
205
+ this.lastOpenFailure = 'duplicate';
206
+ return null;
207
+ }
198
208
  const clear = nacl.secretbox.open(packet.subarray(9), concat(this.rxPrefix, counterBytes(counter)), this.key);
199
- if (!clear || clear.length < 1 || clear[0] !== CLEAR_DATA) return null;
209
+ if (!clear || clear.length < 1 || clear[0] !== CLEAR_DATA) {
210
+ this.lastOpenFailure = 'decrypt';
211
+ return null;
212
+ }
200
213
  this.rxCounter = counter + 1;
201
214
  return utf8Decode(clear.subarray(1));
202
215
  }
@@ -395,7 +408,7 @@ async function handleClientOpen(frame, agentWs) {
395
408
  });
396
409
 
397
410
  gateway.on('close', (code, reason) => {
398
- log(`管道 ${bridgeId}: 本地网关关闭 ${code} ${reason.toString()}`);
411
+ log(`管道 ${bridgeId}: 本地网关关闭 ${code} ${reason ? reason.toString() : ''}`.trimEnd());
399
412
  pipes.delete(bridgeId);
400
413
  if (agentWs.readyState === WebSocket.OPEN) {
401
414
  agentWs.send(JSON.stringify({ kind: 'client-close', bridgeId }), { binary: false });
@@ -471,8 +484,10 @@ function authenticateGateway(pipe, bridgeId, frame) {
471
484
  }
472
485
  sendPipeFrame(pipe.agentWs, bridgeId, pipe.session.seal(text));
473
486
  });
474
- gateway.on('close', () => {
487
+ gateway.on('close', (code, reason) => {
475
488
  clearTimeout(pipe.authTimer); pipes.delete(bridgeId);
489
+ rejectedFrameLog.delete(bridgeId);
490
+ log(`管道 ${bridgeId}: 本地网关关闭 ${code} ${reason ? reason.toString() : ''}`.trimEnd());
476
491
  if (pipe.agentWs.readyState === WebSocket.OPEN) pipe.agentWs.send(JSON.stringify({ kind: 'client-close', bridgeId }));
477
492
  });
478
493
  gateway.on('error', () => {
@@ -502,7 +517,14 @@ function handleRelayBinary(data) {
502
517
  if (pipe.session.ready && packet[0] === PACKET_SEALED) {
503
518
  const text = pipe.session.open(packet);
504
519
  if (text === null) {
505
- log(`管道 ${bridgeId}: 密文解密失败,关闭管道`);
520
+ const reason = pipe.session.lastOpenFailure;
521
+ // 重复帧/残帧只丢弃,不关管道:把它们当“解密失败”会把一条健康连接直接
522
+ // 掐断,手机侧看到 4001 gateway-closed,表现就是打开会话/切页面就掉线。
523
+ if (reason === 'duplicate' || reason === 'format' || reason === 'not-ready') {
524
+ logRejectedFrame(bridgeId, reason, packet.length);
525
+ return;
526
+ }
527
+ log(`管道 ${bridgeId}: 密文解密失败,关闭管道(${reason || 'unknown'})`);
506
528
  try { pipe.gateway.close(4002, 'e2ee-decrypt-failed'); } catch (error) { /* ignore */ }
507
529
  pipes.delete(bridgeId);
508
530
  return;
@@ -576,6 +598,21 @@ function log(message) {
576
598
  console.log(`[connector ${new Date().toISOString()}] ${message}`);
577
599
  }
578
600
 
601
+ // 重复/残帧可能成串出现(例如重连后旧会话的残留帧),按管道限流汇总,
602
+ // 避免日志刷屏同时保留“确实在丢帧”的可见性。
603
+ const rejectedFrameLog = new Map();
604
+ function logRejectedFrame(bridgeId, reason, length) {
605
+ const now = Date.now();
606
+ const entry = rejectedFrameLog.get(bridgeId) || { at: now, count: 0 };
607
+ entry.count += 1;
608
+ if (now - entry.at >= 2000) {
609
+ log(`管道 ${bridgeId}: 丢弃 ${entry.count} 个重复/残帧(${reason},len=${length})`);
610
+ rejectedFrameLog.set(bridgeId, { at: now, count: 0 });
611
+ return;
612
+ }
613
+ rejectedFrameLog.set(bridgeId, entry);
614
+ }
615
+
579
616
  // ---------- 主流程 ----------
580
617
 
581
618
  process.on('SIGINT', () => {