dsh-fishpal 0.9.2 → 0.9.5

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/bottle.js CHANGED
@@ -16,13 +16,35 @@
16
16
  import fs from 'node:fs';
17
17
  import path from 'node:path';
18
18
  import crypto from 'node:crypto';
19
- import { signPayload } from './crypto.js';
19
+ import { signPayload, verifyPayload } from './crypto.js';
20
+ import { newBottleKey, bottleKeyOf, sealReplyText, openReplyText } from './bottle-crypto.js';
21
+ import * as pow from './bottle-pow.js';
20
22
 
21
23
  export const MAX_TEXT_CHARS = 500;
22
24
  export const MAX_DRAFTS = 50;
23
25
  /** 瓶子保质期(天)。过了就当它漂走了 —— 不占地方,也不让人看见一句陈年的话。 */
24
26
  export const BOTTLE_TTL_DAYS = 7;
25
27
 
28
+ // ── 安全模型(2026-11-29 岸定的规格)──────────────────────────────────
29
+ // ★ 字段白名单(规格一.3):只认这些,别的字段**丢弃**(不是拒收报文)。
30
+ // 这样以后加新字段,老版本不认识也不至于把整只瓶子扔掉。
31
+ const BOTTLE_FIELDS = new Set([
32
+ 'type',
33
+ 'ver',
34
+ 'bottle_id',
35
+ 'text',
36
+ 'enable_locate',
37
+ 'qq_nick',
38
+ 'anon_pub',
39
+ 'anon_sign_pub',
40
+ 'sent_at',
41
+ 'expire_at',
42
+ 'nonce', // PoW 的解
43
+ 'signature',
44
+ ]);
45
+ /** 时间戳窗口(规格一.4):±15 分钟,防重放 */
46
+ const CLOCK_SKEW_MS = 15 * 60_000;
47
+
26
48
  function keyFile(dataDir) {
27
49
  return path.join(dataDir, 'bottle-key.json');
28
50
  }
@@ -69,6 +91,124 @@ export function ensureAnonIdentity(dataDir) {
69
91
  return id;
70
92
  }
71
93
 
94
+ /**
95
+ * 只读匿名身份:没建过就返回 null,**绝不顺手创建**。
96
+ *
97
+ * ★ 为什么要跟 ensureAnonIdentity 分开(2026-11-29):
98
+ * 收瓶子的时候要用它判断「这条是不是我自己发的」。
99
+ * 如果那里调 ensure 版,一台从没发过瓶子的机器只要收到一只别人的瓶子,
100
+ * 就会凭空生成一把匿名私钥 —— 那是没必要的副作用,也让「有没有发过」这件事失真。
101
+ */
102
+ export function loadAnonIdentity(dataDir) {
103
+ try {
104
+ const parsed = JSON.parse(fs.readFileSync(keyFile(dataDir), 'utf8'));
105
+ if (parsed?.exchange?.public && parsed?.signing?.public) return parsed;
106
+ } catch {
107
+ /* 没有就是没有 */
108
+ }
109
+ return null;
110
+ }
111
+
112
+ /**
113
+ * 校验一只从公共池捡到的瓶子(纯函数,不落盘、不要 dataDir)。
114
+ *
115
+ * ★ 顺序不能换:
116
+ * ① 解析 → ② 结构 → ③ **验签** → ④ 排自己 → ⑤ 排过期。
117
+ * 为什么验签必须在「排自己」之前:不验签的话,谁都能把 anon_sign_pub 伪装成
118
+ * 我的公钥,让这条被当成「自己发的」悄悄丢掉 —— 那是拿丢弃当攻击面。
119
+ * 先验签,再谈内容。
120
+ *
121
+ * @param {string} raw MQTT 收到的原始字符串
122
+ * @param {string|null} mySignPub 本机的匿名签名公钥(没建过身份就传 null)
123
+ * @returns {{ok:true, packet:object} | {ok:false, reason:string}}
124
+ */
125
+ export function verifyBottlePacket(raw, mySignPub, now = Date.now()) {
126
+ let pkt;
127
+ try {
128
+ pkt = JSON.parse(String(raw ?? ''));
129
+ } catch {
130
+ return { ok: false, reason: '不是 JSON' };
131
+ }
132
+ if (!pkt || typeof pkt !== 'object' || Array.isArray(pkt)) return { ok: false, reason: '不是对象' };
133
+ if (pkt.type !== 'bottle') return { ok: false, reason: `type 是 ${pkt.type}` };
134
+ if (pkt.ver !== 1) return { ok: false, reason: `ver 是 ${pkt.ver}` };
135
+
136
+ const { signature, ...rawBody } = pkt;
137
+ if (typeof rawBody.anon_sign_pub !== 'string' || !rawBody.anon_sign_pub) {
138
+ return { ok: false, reason: '没有签名公钥' };
139
+ }
140
+ if (typeof signature !== 'string' || !signature) {
141
+ return { ok: false, reason: '没有签名' };
142
+ }
143
+
144
+ // ① 字段白名单(规格一.3):额外自定义字段**丢弃**,但不丢整条报文。
145
+ // ★ 注意这里是「从 body 里挑」,不是「拒绝报文」—— 以后加新字段,
146
+ // 老版本不认识、也不会因此拒收,兼容性是这么留的。
147
+ const body = {};
148
+ for (const k of BOTTLE_FIELDS) if (k in rawBody) body[k] = rawBody[k];
149
+
150
+ // ② 正文与大小分级(规格一.1 / 一.2):纯文本、64KB 硬上限
151
+ if (typeof body.text !== 'string' || !body.text.trim()) return { ok: false, reason: '没有正文' };
152
+ const size = pow.difficultyFor(body.text);
153
+ if (!size.ok) return { ok: false, reason: size.reason, bytes: size.bytes };
154
+
155
+ // ③ 验签 —— ★ 用**原始**对象验,不能用过滤后的 body。
156
+ // 签名签的是发送端发出去的那个对象,我这边丢掉扩展字段再验就一定对不上。
157
+ // (2026-11-29:这条顺序搞反过一次,删字段和验签谁先谁后是硬约束)
158
+ let sigOk = false;
159
+ try {
160
+ sigOk = verifyPayload(JSON.stringify(rawBody), signature, rawBody.anon_sign_pub);
161
+ } catch (e) {
162
+ // ★ 2026-11-29:这里原来是个空 catch —— 它把「verifyPayload 没 import」这种
163
+ // 程序错误也吞成「签名对不上」。实测代价:函数写完忘了 import,10 条断言里
164
+ // 4 条红,报的全是「签名对不上」,我查了三轮才查到 import 上。
165
+ // 现在把原因带出去:报文坏了和代码坏了,日志里必须分得开。
166
+ return { ok: false, reason: `验签出错:${e?.message ?? e}` };
167
+ }
168
+ if (!sigOk) return { ok: false, reason: '签名对不上' };
169
+
170
+ // 走到这里,这条确实是 anon_sign_pub 那把钥匙发的、中途没被改过
171
+ if (mySignPub && rawBody.anon_sign_pub === mySignPub) {
172
+ return { ok: false, reason: '这是我自己发的' };
173
+ }
174
+
175
+ // ④ 时间戳窗口(规格一.4):±15 分钟,防重放。
176
+ // ★ 只对**带了** sent_at 的报文生效 —— 缺时间戳是「不知道」,
177
+ // 不等于「伪造」;那条交给过期检查。
178
+ const sentMs = Date.parse(body.sent_at || '');
179
+ if (Number.isFinite(sentMs)) {
180
+ const skew = Math.abs(now - sentMs);
181
+ if (skew > CLOCK_SKEW_MS) {
182
+ return {
183
+ ok: false,
184
+ reason: `发送时间偏了 ${Math.round(skew / 60_000)} 分钟(窗口 ±${CLOCK_SKEW_MS / 60_000} 分钟),拒收`,
185
+ skewMs: skew,
186
+ };
187
+ }
188
+ }
189
+
190
+ // ⑤ 过期(老字段,跟时间窗口不重复:这个管「瓶子本身过期了」)
191
+ const exp = body.expire_at ? Date.parse(body.expire_at) : 0;
192
+ if (Number.isFinite(exp) && exp > 0 && exp < now) {
193
+ return { ok: false, reason: '过期了' };
194
+ }
195
+
196
+ // ⑥ PoW(规格三.3)—— ★ 难度由**接收端**按正文长度自己算,
197
+ // 绝不看报文里声明了什么:报文能写「难度 0」,那我这边就等于没设防。
198
+ const pw = pow.verifyPow({
199
+ bottleId: body.bottle_id,
200
+ text: body.text,
201
+ anonPub: body.anon_pub,
202
+ nonce: body.nonce,
203
+ difficulty: size.difficulty,
204
+ });
205
+ if (!pw.ok) {
206
+ return { ok: false, reason: `PoW 没过:${pw.reason}`, powBits: pw.bits, powNeed: size.difficulty };
207
+ }
208
+
209
+ return { ok: true, packet: body, powBits: pw.bits, difficulty: size.difficulty };
210
+ }
211
+
72
212
  export function loadDrafts(dataDir) {
73
213
  try {
74
214
  const parsed = JSON.parse(fs.readFileSync(draftFile(dataDir), 'utf8'));
@@ -401,6 +541,27 @@ export function listRejectedDrafts(dataDir, { limit = 10 } = {}) {
401
541
  .slice(0, limit);
402
542
  }
403
543
 
544
+ /**
545
+ * 把「被主人打回的草稿」排成一段话,塞进鱼每次碰瓶子时的返回里。
546
+ *
547
+ * ★ 为什么不靠投递:DSH 的 tool context 不传 sessionId,插件认不出鱼是在哪个
548
+ * 会话里写的瓶子,所以「投给正在做的那边」做不到。改成「一碰就看见」——
549
+ * 鱼不管在哪个窗口动手,只要调 fishpal_bottle,这段就在它眼前。
550
+ * ★ 空数组返回空串:没被打回时整段不该出现,别留个没有内容的标题。
551
+ */
552
+ export function formatRejectHint(rejected = []) {
553
+ if (!Array.isArray(rejected) || !rejected.length) return '';
554
+ const lines = [`⚠ 有 ${rejected.length} 只瓶子被主人打回了 —— 先看这个,再写新的:`];
555
+ rejected.forEach((d, i) => {
556
+ const said = d.note ? `他说的:${d.note}` : '(他没留意见,只说了不要这条)';
557
+ const full = String(d.text ?? '');
558
+ const body = full.slice(0, 60) + (full.length > 60 ? '…' : '');
559
+ lines.push(`${i + 1}. ${said}`, ` 被打回的是:「${body}」`);
560
+ });
561
+ lines.push('打回 ≠ 丢弃:正文归你,照这个意见重写一只,别去改他只是提意见的地方。');
562
+ return '\n\n' + lines.join('\n');
563
+ }
564
+
404
565
  function decide(dataDir, id, status) {
405
566
  const drafts = pruneDone(loadDrafts(dataDir));
406
567
  const d = drafts.find((x) => x && x.id === id && x.status === 'draft');
@@ -421,12 +582,20 @@ export function dropDraft(dataDir, id) {
421
582
  * ★ 这份报文里**没有** fp_xxx、没有 QQ 群号、没有 IP。
422
583
  * 只有:正文、是否留位置、昵称(可选)、匿名公钥、签名。
423
584
  */
424
- export function buildBottlePacket(dataDir, id, now = new Date().toISOString()) {
585
+ export async function buildBottlePacket(dataDir, id, now = new Date().toISOString()) {
425
586
  const drafts = loadDrafts(dataDir);
426
587
  const d = drafts.find((x) => x && x.id === id && x.status === 'draft');
427
588
  if (!d) return { ok: false, error: '没有这条草稿' };
428
589
 
429
- const anon = ensureAnonIdentity(dataDir);
590
+ // ★ 2026-11-29(岸:「漂流瓶的密码是单独的,用一次换一次」):
591
+ // 每只瓶子一把新钥匙,私钥按 bottle_id 存本地。
592
+ // 原来这里用的是 ensureAnonIdentity 的**长期**密钥 —— 所有瓶子同一把,
593
+ // 别人能把「这些瓶子是同一个人扔的」串起来。
594
+ // 匿名只是「不知道你是谁」,不等于「不能把你干的事连起来」。
595
+ const made = newBottleKey(dataDir, d.id);
596
+ if (!made.ok) return { ok: false, error: made.error };
597
+ const mine = bottleKeyOf(dataDir, d.id);
598
+ if (!mine) return { ok: false, error: '瓶子的钥匙没存下来,这只先别发(存盘失败)' };
430
599
  // ★ 过期时间也写进报文(手册 6.7 的风控):
431
600
  // 收方不用回头问发送方就知道这只瓶子还值不值得看。
432
601
  // 算不出来就别硬编一个 —— 没有 expire_at 的老报文按「不知道过期时间」处理,
@@ -441,20 +610,32 @@ export function buildBottlePacket(dataDir, id, now = new Date().toISOString()) {
441
610
  text: d.text,
442
611
  enable_locate: Boolean(d.enableLocate),
443
612
  qq_nick: d.qqNick || null,
444
- anon_pub: anon.exchange.public,
445
- anon_sign_pub: anon.signing.public,
613
+ // ★ 这只瓶子自己的公钥 —— 不是长期身份那把。
614
+ // 别人拿到它才能加密回信,而且和其他瓶子不可关联。
615
+ anon_pub: mine.exchange.public,
616
+ anon_sign_pub: mine.signing.public,
446
617
  sent_at: now,
447
618
  expire_at: Number.isFinite(sentMs)
448
619
  ? new Date(sentMs + BOTTLE_TTL_DAYS * 86400_000).toISOString()
449
620
  : null,
450
621
  };
622
+ // ★ 2026-11-29(安全模型三.3):发之前先算 PoW。
623
+ // ★ 这步是 async 的 —— 算力证明要跑几百毫秒到几秒,同步算会把界面冻住。
624
+ // ★ nonce 必须在**签名之前**放进去:签名签的是整个 payload,
625
+ // 先签后补 nonce 就等于签名作废。
626
+ const solved = await pow.solvePow(d.text, { bottleId: d.id, anonPub: mine.exchange.public });
627
+ if (!solved.ok) {
628
+ return { ok: false, error: `算力证明没算出来:${solved.reason}。这只先别发。` };
629
+ }
630
+ payload.nonce = solved.nonce;
631
+
451
632
  let signature;
452
633
  try {
453
- signature = signPayload(JSON.stringify(payload), anon.signing.private);
634
+ signature = signPayload(JSON.stringify(payload), mine.signing.private);
454
635
  } catch (e) {
455
636
  return { ok: false, error: '签名失败:' + (e?.message ?? e) };
456
637
  }
457
- return { ok: true, draft: d, packet: { ...payload, signature } };
638
+ return { ok: true, draft: d, packet: { ...payload, signature }, powMs: solved.ms, powTries: solved.tries };
458
639
  }
459
640
 
460
641
  // ── 发送限流(手册 6.7)────────────────────────────────────────────────
@@ -626,8 +807,8 @@ export function nickOf(dataDir, anonSignPub) {
626
807
  }
627
808
 
628
809
  /** 放行:标记 + 返回报文(真正发出去的是调用方) */
629
- export function approveDraft(dataDir, id, now) {
630
- const built = buildBottlePacket(dataDir, id, now);
810
+ export async function approveDraft(dataDir, id, now) {
811
+ const built = await buildBottlePacket(dataDir, id, now);
631
812
  if (!built.ok) return built;
632
813
  const r = decide(dataDir, id, 'approved');
633
814
  if (!r.ok) return r;
@@ -794,21 +975,38 @@ export function checkReplyQuota(
794
975
  * ★ 报文里带 is_temp_channel / has_replied —— 收方一眼看出「这条没有后续」,
795
976
  * 不会把它当成聊天开头继续回。
796
977
  */
797
- export function buildReplyPacket(dataDir, bottleId, now = new Date().toISOString()) {
978
+ export async function buildReplyPacket(dataDir, bottleId, now = new Date().toISOString()) {
798
979
  const key = String(bottleId || '').trim();
799
980
  const replies = loadReplies(dataDir);
800
981
  const rec = replies.find((r) => r && r.bottleId === key && r.status === 'draft');
801
982
  if (!rec) return { ok: false, error: '没有这条回信草稿(可能已经放过或丢掉了)' };
802
983
  const anon = ensureAnonIdentity(dataDir);
984
+ // ★ 2026-11-29:正文加密。拿不到对方公钥就**发不出去**,绝不退化成明文 ——
985
+ // 发一条「看上去加密了其实没有」的回信,比不发更坏。
986
+ const peerPub = String(rec.toAnonPub || '');
987
+ if (!peerPub) {
988
+ return { ok: false, error: '这只瓶子没带对方的公钥,加密不了(旧版瓶子回不了)' };
989
+ }
990
+ const sealed = sealReplyText(
991
+ anon.exchange.private,
992
+ peerPub,
993
+ { text: rec.text, sentAt: now },
994
+ key
995
+ );
996
+ if (!sealed) return { ok: false, error: '加密失败(对方的公钥可能不合法)' };
803
997
  const sentMs = Date.parse(now);
804
998
  const payload = {
805
999
  type: 'bottle_reply',
806
- ver: 1,
1000
+ // ★ 2026-11-29(岸:「别人收到回信是要走私密的,不要让其他人看到」):
1001
+ // ver 2 = 正文加密。用【我的匿名私钥】+【瓶子里对方的公钥】做 ECDH,
1002
+ // 密文只有原丢瓶人拿他自己那只瓶子的私钥才解得开。
1003
+ // ★ 拿不到对方公钥就**发不出去**,绝不退化成明文 —— 那等于骗人。
1004
+ ver: 2,
807
1005
  bottle_id: key,
808
1006
  reply_to_bottle_id: key,
809
1007
  is_temp_channel: true,
810
1008
  has_replied: true,
811
- text: rec.text,
1009
+ sealed, // 密文,代替 ver 1 的明文 text
812
1010
  anon_pub: anon.exchange.public,
813
1011
  anon_sign_pub: anon.signing.public,
814
1012
  sent_at: now,
@@ -816,13 +1014,22 @@ export function buildReplyPacket(dataDir, bottleId, now = new Date().toISOString
816
1014
  ? new Date(sentMs + BOTTLE_TTL_DAYS * 86400_000).toISOString()
817
1015
  : null,
818
1016
  };
1017
+ // ★ 2026-11-29(安全模型四.2):回信也得算 PoW。
1018
+ // ★ base 用**密文** sealed —— 收方在解密之前手里只有密文,
1019
+ // 用明文当 base 的话他根本验不了(拿不到明文)。
1020
+ const solved = await pow.solvePow(sealed, { bottleId: key, anonPub: anon.exchange.public });
1021
+ if (!solved.ok) {
1022
+ return { ok: false, error: `回信的算力证明没算出来:${solved.reason}` };
1023
+ }
1024
+ payload.nonce = solved.nonce;
1025
+
819
1026
  let signature;
820
1027
  try {
821
1028
  signature = signPayload(JSON.stringify(payload), anon.signing.private);
822
1029
  } catch (e) {
823
1030
  return { ok: false, error: '签名失败:' + (e?.message ?? e) };
824
1031
  }
825
- return { ok: true, reply: rec, packet: { ...payload, signature } };
1032
+ return { ok: true, reply: rec, packet: { ...payload, signature }, powMs: solved.ms };
826
1033
  }
827
1034
 
828
1035
  /**
@@ -850,6 +1057,28 @@ export function listRejectedReplies(dataDir, { limit = 10 } = {}) {
850
1057
  .slice(0, limit);
851
1058
  }
852
1059
 
1060
+ /**
1061
+ * 把「被主人打回的回信」排成一段话。
1062
+ *
1063
+ * ★ 跟 formatRejectHint 一个道理(插件不知道鱼在哪个会话,所以靠「一碰就看见」),
1064
+ * 但回信有一处不同:一只瓶子只说一句,通道发完就关 ——
1065
+ * 打回之后那条回信就作废了,鱼得照意见重写一条,而不是「改改原来那句」。
1066
+ * 这句差别必须写出来,不然鱼会以为还能回去编辑。
1067
+ * ★ 空数组返回空串:没被打回时整段不该出现。
1068
+ */
1069
+ export function formatReplyRejectHint(replies = []) {
1070
+ if (!Array.isArray(replies) || !replies.length) return '';
1071
+ const lines = [`⚠ 有 ${replies.length} 条回信被主人打回了 —— 先看这个,再写新的:`];
1072
+ replies.forEach((r, i) => {
1073
+ const said = r.note ? `他说的:${r.note}` : '(他没留意见,只说了不要这条)';
1074
+ const full = String(r.text ?? '');
1075
+ const body = full.slice(0, 60) + (full.length > 60 ? '…' : '');
1076
+ lines.push(`${i + 1}. ${said}`, ` 被打回的是:「${body}」`);
1077
+ });
1078
+ lines.push('回信打回 ≠ 那只瓶子也没了:瓶子还在,但一只瓶子只说一句,照意见重写一条。');
1079
+ return '\n\n' + lines.join('\n');
1080
+ }
1081
+
853
1082
  /** 把一条回信标成「已回」(通道从此关闭)。★ 发成功之后才调它。 */
854
1083
  export function markReplyApproved(dataDir, bottleId, { now = Date.now() } = {}) {
855
1084
  const key = String(bottleId || '');
@@ -864,8 +1093,8 @@ export function markReplyApproved(dataDir, bottleId, { now = Date.now() } = {})
864
1093
  }
865
1094
 
866
1095
  /** 放行:标记 approved + 返回报文(真正发出去的是调用方) */
867
- export function approveReply(dataDir, bottleId, now = new Date().toISOString()) {
868
- const built = buildReplyPacket(dataDir, bottleId, now);
1096
+ export async function approveReply(dataDir, bottleId, now = new Date().toISOString()) {
1097
+ const built = await buildReplyPacket(dataDir, bottleId, now);
869
1098
  if (!built.ok) return built;
870
1099
  const replies = loadReplies(dataDir);
871
1100
  const rec = replies.find((r) => r && r.bottleId === String(bottleId) && r.status === 'draft');
package/lib/config.js CHANGED
@@ -164,7 +164,20 @@ export function createConfig({ me, meFp }) {
164
164
  // 放顶层而不是塞进 loop:loop 是出站闸门(一堆限流参数),
165
165
  // 这一个是「收不收别人的瓶子」,语义不同,也不该跟着闸门一起被批量改。
166
166
  bottle: {
167
+ // ★ 2026-11-30 按手册 §6.1 / §6.4 加的两个开关:
168
+ // enabled = 总开关,收和发一起停(管的是「玩不玩」)
169
+ // safeMode = 校验宽严三档(管的是「关到多死」)
170
+ // 跟 receive 的关系:receive 只管「收不收」,是 11-29 加的旧开关,
171
+ // 留着不动是为了兼容已经存了配置的机器 —— 但 enabled=false 时它是死的。
172
+ enabled: true,
173
+ safeMode: 'default', // default | conservative | off
167
174
  receive: true, // 收:关掉后收到的瓶子在最前面就丢弃,不入隔离区
175
+ // ★ 循环广播(岸 2026-11-30):「隔 1 到 2 个小时去广播一次,就那种尝试的感觉」。
176
+ // loopMinutes 是**下限**,实际每跳在 [loopMinutes, +60] 分钟里随机取 ——
177
+ // 固定 60 分钟是定时器,随机才像「过一阵想起来了又扔一次」。
178
+ // loopTimes = 再循环几次(含首发共 loopTimes+1 次);2 → 共 3 次。
179
+ loopMinutes: 60,
180
+ loopTimes: 2,
168
181
  },
169
182
  proactive: {
170
183
  enabled: false,
@@ -346,9 +359,33 @@ export function setProactive(patch = {}) {
346
359
  export function setBottle(patch = {}) {
347
360
  const cfg = loadConfig();
348
361
  if (!cfg) throw new Error('还没有配置,先生成身份');
349
- const base = { receive: true };
362
+ const base = {
363
+ enabled: true,
364
+ safeMode: 'default',
365
+ receive: true,
366
+ loopMinutes: 60,
367
+ loopTimes: 2,
368
+ };
350
369
  const next = { ...base, ...(cfg.bottle || {}) };
351
370
  if ('receive' in patch) next.receive = patch.receive !== false;
371
+ if ('enabled' in patch) next.enabled = patch.enabled !== false;
372
+ if ('safeMode' in patch) {
373
+ const m = String(patch.safeMode || '').toLowerCase();
374
+ // ★ 认不出来的值不静默吞掉 —— 直接报错。静默变 default 会让人以为档位调了。
375
+ if (!['default', 'conservative', 'off'].includes(m)) {
376
+ throw new Error(`safeMode 只能是 default / conservative / off:${patch.safeMode}`);
377
+ }
378
+ next.safeMode = m;
379
+ }
380
+ for (const k of ['loopMinutes', 'loopTimes']) {
381
+ if (!(k in patch)) continue;
382
+ const n = Number(patch[k]);
383
+ // ★ loopTimes 允许 0(= 不循环,只发一次);loopMinutes 是间隔下限,0 没意义。
384
+ if (!Number.isFinite(n) || (k === 'loopTimes' ? n < 0 : n <= 0)) {
385
+ throw new Error(`${k} 要是一个${k === 'loopTimes' ? '非负' : '正'}数:${patch[k]}`);
386
+ }
387
+ next[k] = n;
388
+ }
352
389
  cfg.bottle = next;
353
390
  saveConfig(cfg);
354
391
  return cfg;
package/lib/guard.js CHANGED
@@ -336,3 +336,52 @@ export function expireQuarantine({ days = 7, now = new Date() } = {}) {
336
336
  fs.renameSync(tmp, file);
337
337
  return { expired: ids.length, ids };
338
338
  }
339
+
340
+ /**
341
+ * ��ҳ����һ�Ѽ�¼���г�һҳ����˵�塸���ж���û��ʾ����
342
+ *
343
+ * �� 2026-11-29 �������յ���Ʈƿ̫������ô�죿��
344
+ * ���������̺ͽ��涼����ƿ����һ���� 570 �ֽڡ�����ֻ�� 40 ������
345
+ * ������������ fishpal_review list �Ѵ���**ȫ��**�̳�����
346
+ * 200 �� �� 200 �� = �����֣�һ�ξͰ���������ijű���Ȼ����˭Ҳ������
347
+ *
348
+ * ��̫�ࡹ�������ݶ��ˣ���һ�θ����ˡ�
349
+ *
350
+ * �߽綼�����������ɢ�����ô���
351
+ * limit �Ƿ� / <= 0 �� Ĭ�� 20
352
+ * limit �������� �� �ⶥ��Ĭ�� 100��
353
+ * offset �Ƿ� / ���� �� 0
354
+ * offset �������� �� page Ϊ�ա�more Ϊ 0��oob=true�����÷�Ҫ˵�˻�������ɸ��ģ�
355
+ *
356
+ * @param {Array} list
357
+ * @param {object} opts
358
+ * @param {number} opts.limit
359
+ * @param {number} opts.offset
360
+ * @param {number} opts.maxLimit
361
+ * @returns {{page: Array, more: number, total: number, offset: number, limit: number, oob: boolean}}
362
+ */
363
+ export function pageOf(list, { limit = 20, offset = 0, maxLimit = 100 } = {}) {
364
+ const all = Array.isArray(list) ? list : [];
365
+ const total = all.length;
366
+
367
+ const lim = Number.isFinite(Number(limit)) && Number(limit) > 0
368
+ ? Math.min(Math.floor(Number(limit)), maxLimit)
369
+ : 20;
370
+ const off = Number.isFinite(Number(offset)) && Number(offset) > 0
371
+ ? Math.floor(Number(offset))
372
+ : 0;
373
+
374
+ if (off >= total) {
375
+ return { page: [], more: 0, total, offset: off, limit: lim, oob: total > 0 };
376
+ }
377
+
378
+ const page = all.slice(off, off + lim);
379
+ return {
380
+ page,
381
+ more: total - (off + page.length),
382
+ total,
383
+ offset: off,
384
+ limit: lim,
385
+ oob: false,
386
+ };
387
+ }