watch-expiry.mjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env node
  2. /**
  3. * 到期链路定时回归(2026-12-04 用户要求「定时测试,后续给结果」)。
  4. *
  5. * 每轮检查:
  6. * ① 试用容器 we51nkpsaz 是否已自然到期
  7. * ② 到期后:update/usage/report 三个接口是否全部 403
  8. * ③ 到期后:Matrix 原密码是否已登不上(销号生效)
  9. * ④ 回收脚本能否扫到它
  10. *
  11. * 结果追加写到 `.local-backup/tmp-wecom/expiry-watch.log`,同时打印到终端。
  12. * 用法:node scripts/watch-expiry.mjs [--once]
  13. */
  14. import fs from 'node:fs';
  15. import crypto from 'node:crypto';
  16. import { resolve } from 'node:path';
  17. const ROOT = resolve(import.meta.dirname, '..');
  18. const cfg = JSON.parse(fs.readFileSync(resolve(ROOT, '.claude/dev-login.json'), 'utf8'));
  19. const H = { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster', 'X-Parse-Master-Key': cfg.masterKey };
  20. const B = 'https://server.fmode.cn';
  21. const LOG = resolve(ROOT, '.local-backup/tmp-wecom/expiry-watch.log');
  22. /* 从测试记录读目标容器(不在时跳过) */
  23. function target() {
  24. try {
  25. const p = resolve(ROOT, '.local-backup/tmp-wecom/provision-18182.json');
  26. const d = JSON.parse(fs.readFileSync(p, 'utf8'));
  27. return { objId: d.agentObjectId, agentId: d.env.AGENT_ID, acc: d.env.FMODE_ACCESS_TOKEN, sess: d.env.FMODE_SESSION_TOKEN, mxUser: d.env.MATRIX_USER_ID };
  28. } catch { return null; }
  29. }
  30. const fn = async (id, token, params) => {
  31. const r = await fetch(`${B}/api/functions`, {
  32. method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster' },
  33. body: JSON.stringify({ id, token, params }),
  34. });
  35. return { status: r.status, body: await r.json().catch(() => ({})) };
  36. };
  37. async function check() {
  38. const t = target();
  39. const stamp = new Date().toISOString();
  40. const out = [];
  41. const say = (s) => { out.push(s); console.log(s); };
  42. say(`\n===== ${stamp} =====`);
  43. if (!t) { say('⚠️ 未找到测试容器记录,跳过'); return { done: true }; }
  44. const a = await (await fetch(`${B}/parse/classes/FmodeAgent/${t.objId}?keys=accessExpiresAt,accessState,matrixRevokedVer,isDeleted`, { headers: H })).json();
  45. const expMs = a.accessExpiresAt ? Date.parse(a.accessExpiresAt.iso) : 0;
  46. const expired = expMs > 0 && expMs <= Date.now();
  47. const leftMin = expMs ? Math.round((expMs - Date.now()) / 60000) : null;
  48. say(`容器 ${t.agentId}|到期时间 ${a.accessExpiresAt?.iso || '无'}|${expired ? '已到期' : `还剩约 ${leftMin} 分钟`}|状态 ${a.accessState || '-'}`);
  49. if (!expired) return { done: false };
  50. /* 到期后逐项验证 */
  51. const results = [];
  52. const u = await fn('xlosi3m46q', t.sess, { op: 'update', agentId: t.agentId, accessToken: t.acc, field: 'status', value: 'idle' });
  53. results.push(['update 拦截', u.status === 403, `${u.status} ${u.body.reasonCode || u.body.mess || ''}`]);
  54. const us = await fn('xlosi3m46q', t.sess, { op: 'usage', agentId: t.agentId, accessToken: t.acc, usage: [{ sessionId: 'watch', inputTokens: 1 }] });
  55. results.push(['usage 拦截', us.status === 403, `${us.status} ${us.body.reasonCode || us.body.mess || ''}`]);
  56. const rp = await fn('lfYlgU7SkK', t.sess, { action: 'report', agentId: t.agentId, accessToken: t.acc, title: 'watch' });
  57. results.push(['report 拦截', rp.status === 403, `${rp.status} ${rp.body.reasonCode || rp.body.mess || ''}`]);
  58. /* Matrix 销号:用派生密码登录,应 403(密码已被重置为随机串) */
  59. const pw = crypto.createHash('sha256').update('fmode-agent-mx:' + t.objId).digest('hex').slice(0, 24);
  60. let lgStatus = 0;
  61. let lgDetail = '';
  62. try {
  63. const lg = await fetch('https://matrix.fmode.cn/_matrix/client/v3/login', {
  64. method: 'POST', headers: { 'Content-Type': 'application/json' },
  65. body: JSON.stringify({ type: 'm.login.password', identifier: { type: 'm.id.user', user: t.mxUser }, password: pw }),
  66. });
  67. lgStatus = lg.status;
  68. lgDetail = (await lg.text()).slice(0, 80);
  69. } catch (e) {
  70. lgDetail = '请求失败: ' + (e.message || String(e)).slice(0, 60);
  71. }
  72. results.push(['Matrix 销号', lgStatus === 403, `${lgStatus} ${lgStatus === 403 ? '原密码已失效' : lgDetail}`]);
  73. for (const [name, ok, detail] of results) say(` ${ok ? '✅' : '❌'} ${name}: ${detail}`);
  74. const pass = results.filter((r) => r[1]).length;
  75. say(` 合计 ${pass}/${results.length} 通过`);
  76. return { done: pass === results.length };
  77. }
  78. const once = process.argv.includes('--once');
  79. let round = 0;
  80. do {
  81. round++;
  82. const r = await check();
  83. if (once || r.done) break;
  84. /* 每 2 分钟查一次,最多 40 轮(约 80 分钟) */
  85. if (round >= 40) { console.log('达到最大轮次,停止观察'); break; }
  86. await new Promise((res) => setTimeout(res, 120000));
  87. } while (true);