| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #!/usr/bin/env node
- /**
- * 到期链路定时回归(2026-12-04 用户要求「定时测试,后续给结果」)。
- *
- * 每轮检查:
- * ① 试用容器 we51nkpsaz 是否已自然到期
- * ② 到期后:update/usage/report 三个接口是否全部 403
- * ③ 到期后:Matrix 原密码是否已登不上(销号生效)
- * ④ 回收脚本能否扫到它
- *
- * 结果追加写到 `.local-backup/tmp-wecom/expiry-watch.log`,同时打印到终端。
- * 用法:node scripts/watch-expiry.mjs [--once]
- */
- import fs from 'node:fs';
- import crypto from 'node:crypto';
- import { resolve } from 'node:path';
- const ROOT = resolve(import.meta.dirname, '..');
- const cfg = JSON.parse(fs.readFileSync(resolve(ROOT, '.claude/dev-login.json'), 'utf8'));
- const H = { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster', 'X-Parse-Master-Key': cfg.masterKey };
- const B = 'https://server.fmode.cn';
- const LOG = resolve(ROOT, '.local-backup/tmp-wecom/expiry-watch.log');
- /* 从测试记录读目标容器(不在时跳过) */
- function target() {
- try {
- const p = resolve(ROOT, '.local-backup/tmp-wecom/provision-18182.json');
- const d = JSON.parse(fs.readFileSync(p, 'utf8'));
- 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 };
- } catch { return null; }
- }
- const fn = async (id, token, params) => {
- const r = await fetch(`${B}/api/functions`, {
- method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster' },
- body: JSON.stringify({ id, token, params }),
- });
- return { status: r.status, body: await r.json().catch(() => ({})) };
- };
- async function check() {
- const t = target();
- const stamp = new Date().toISOString();
- const out = [];
- const say = (s) => { out.push(s); console.log(s); };
- say(`\n===== ${stamp} =====`);
- if (!t) { say('⚠️ 未找到测试容器记录,跳过'); return { done: true }; }
- const a = await (await fetch(`${B}/parse/classes/FmodeAgent/${t.objId}?keys=accessExpiresAt,accessState,matrixRevokedVer,isDeleted`, { headers: H })).json();
- const expMs = a.accessExpiresAt ? Date.parse(a.accessExpiresAt.iso) : 0;
- const expired = expMs > 0 && expMs <= Date.now();
- const leftMin = expMs ? Math.round((expMs - Date.now()) / 60000) : null;
- say(`容器 ${t.agentId}|到期时间 ${a.accessExpiresAt?.iso || '无'}|${expired ? '已到期' : `还剩约 ${leftMin} 分钟`}|状态 ${a.accessState || '-'}`);
- if (!expired) return { done: false };
- /* 到期后逐项验证 */
- const results = [];
- const u = await fn('xlosi3m46q', t.sess, { op: 'update', agentId: t.agentId, accessToken: t.acc, field: 'status', value: 'idle' });
- results.push(['update 拦截', u.status === 403, `${u.status} ${u.body.reasonCode || u.body.mess || ''}`]);
- const us = await fn('xlosi3m46q', t.sess, { op: 'usage', agentId: t.agentId, accessToken: t.acc, usage: [{ sessionId: 'watch', inputTokens: 1 }] });
- results.push(['usage 拦截', us.status === 403, `${us.status} ${us.body.reasonCode || us.body.mess || ''}`]);
- const rp = await fn('lfYlgU7SkK', t.sess, { action: 'report', agentId: t.agentId, accessToken: t.acc, title: 'watch' });
- results.push(['report 拦截', rp.status === 403, `${rp.status} ${rp.body.reasonCode || rp.body.mess || ''}`]);
- /* Matrix 销号:用派生密码登录,应 403(密码已被重置为随机串) */
- const pw = crypto.createHash('sha256').update('fmode-agent-mx:' + t.objId).digest('hex').slice(0, 24);
- let lgStatus = 0;
- let lgDetail = '';
- try {
- const lg = await fetch('https://matrix.fmode.cn/_matrix/client/v3/login', {
- method: 'POST', headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ type: 'm.login.password', identifier: { type: 'm.id.user', user: t.mxUser }, password: pw }),
- });
- lgStatus = lg.status;
- lgDetail = (await lg.text()).slice(0, 80);
- } catch (e) {
- lgDetail = '请求失败: ' + (e.message || String(e)).slice(0, 60);
- }
- results.push(['Matrix 销号', lgStatus === 403, `${lgStatus} ${lgStatus === 403 ? '原密码已失效' : lgDetail}`]);
- for (const [name, ok, detail] of results) say(` ${ok ? '✅' : '❌'} ${name}: ${detail}`);
- const pass = results.filter((r) => r[1]).length;
- say(` 合计 ${pass}/${results.length} 通过`);
- return { done: pass === results.length };
- }
- const once = process.argv.includes('--once');
- let round = 0;
- do {
- round++;
- const r = await check();
- if (once || r.done) break;
- /* 每 2 分钟查一次,最多 40 轮(约 80 分钟) */
- if (round >= 40) { console.log('达到最大轮次,停止观察'); break; }
- await new Promise((res) => setTimeout(res, 120000));
- } while (true);
|