101 lines
3.7 KiB
JavaScript
101 lines
3.7 KiB
JavaScript
// NapCat 正向 WebSocket 探测:鉴权行为 + 是否可通过 WS 调用 OneBot11 API
|
||||
|
|
// 只调用只读接口,不发送任何消息、不执行任何有副作用的动作。
|
|||
|
|
// 用法: node tools/probe/probe-ws-api.mjs <wsUrl> <access_token>
|
|||
|
|
const base = (process.argv[2] || 'ws://192.168.5.32:16099').replace(/\/$/, '');
|
|||
|
|
const token = process.argv[3];
|
|||
|
|
if (!token) {
|
|||
|
|
console.error('缺少 access_token 参数。凭据不得硬编码在脚本中,请通过命令行参数传入。');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const READ_ONLY_ACTIONS = [
|
|||
|
|
['get_version_info', {}],
|
|||
|
|
['get_login_info', {}],
|
|||
|
|
['get_status', {}],
|
|||
|
|
['get_friend_list', {}],
|
|||
|
|
['get_group_list', {}],
|
|||
|
|
['get_group_system_msg', {}],
|
|||
|
|
['get_online_clients', {}],
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
function connect(target, authHeader) {
|
|||
|
|
return new WebSocket(target, authHeader ? { headers: { Authorization: `Bearer ${authHeader}` } } : {});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function probe(label, target, authHeader) {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
const out = { label, target, open: false, closeCode: null, reason: '', events: [] };
|
|||
|
|
let ws;
|
|||
|
|
try { ws = connect(target, authHeader); } catch (e) { resolve({ ...out, err: String(e) }); return; }
|
|||
|
|
const done = () => { try { ws.close(); } catch {} ; resolve(out); };
|
|||
|
|
const timer = setTimeout(done, 10000);
|
|||
|
|
ws.onopen = () => { out.open = true; };
|
|||
|
|
ws.onmessage = (ev) => {
|
|||
|
|
const s = String(ev.data);
|
|||
|
|
if (s.length < 400) out.events.push(s);
|
|||
|
|
else out.events.push(s.slice(0, 400) + `...(+${s.length - 400})`);
|
|||
|
|
if (out.events.length >= 2) { clearTimeout(timer); done(); }
|
|||
|
|
};
|
|||
|
|
ws.onclose = (e) => { out.closeCode = e.code; out.reason = e.reason || ''; clearTimeout(timer); resolve(out); };
|
|||
|
|
ws.onerror = () => {};
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('### 1. 鉴权行为验证(无 token / 错 token / 正确 token 是否都能连上)');
|
|||
|
|
for (const [label, t, auth] of [
|
|||
|
|
['bare-no-token', base, null],
|
|||
|
|
['query-wrong-token', `${base}/?access_token=WRONG_TOKEN_123456`, null],
|
|||
|
|
['header-wrong-token', base, 'WRONG_TOKEN_123456'],
|
|||
|
|
['query-correct-token', `${base}/?access_token=${token}`, null],
|
|||
|
|
['header-correct-token', base, token],
|
|||
|
|
]) {
|
|||
|
|
const r = await probe(label, t, auth);
|
|||
|
|
console.log(`- ${label}: open=${r.open} close=${r.closeCode} reason="${r.reason}" events=${r.events.length}`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('\n### 2. 通过 WS 调用 OneBot11 API(action + echo)');
|
|||
|
|
const ws = connect(`${base}/?access_token=${token}`);
|
|||
|
|
await new Promise((res, rej) => {
|
|||
|
|
ws.onopen = res;
|
|||
|
|
ws.onerror = (e) => rej(new Error('ws error ' + (e.message || '')));
|
|||
|
|
setTimeout(() => rej(new Error('connect timeout')), 10000);
|
|||
|
|
}).catch((e) => { console.log('连接失败: ' + e.message); process.exit(0); });
|
|||
|
|
|
|||
|
|
const pending = new Map();
|
|||
|
|
ws.onmessage = (ev) => {
|
|||
|
|
let obj;
|
|||
|
|
try { obj = JSON.parse(String(ev.data)); } catch { return; }
|
|||
|
|
if (obj.echo && pending.has(obj.echo)) {
|
|||
|
|
const { resolve } = pending.get(obj.echo);
|
|||
|
|
pending.delete(obj.echo);
|
|||
|
|
resolve(obj);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
function call(action, params, ms = 12000) {
|
|||
|
|
const echo = `probe-${action}-${Date.now()}`;
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
const to = setTimeout(() => {
|
|||
|
|
pending.delete(echo);
|
|||
|
|
resolve({ __timeout: true, action });
|
|||
|
|
}, ms);
|
|||
|
|
pending.set(echo, {
|
|||
|
|
resolve: (o) => { clearTimeout(to); resolve(o); },
|
|||
|
|
});
|
|||
|
|
ws.send(JSON.stringify({ action, params, echo }));
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
for (const [action, params] of READ_ONLY_ACTIONS) {
|
|||
|
|
const r = await call(action, params);
|
|||
|
|
if (r.__timeout) {
|
|||
|
|
console.log(`- ${action}: 无响应(12s 超时)→ 该 WS 通道不支持 API 调用`);
|
|||
|
|
} else {
|
|||
|
|
const s = JSON.stringify(r);
|
|||
|
|
console.log(`- ${action}: status=${r.status} retcode=${r.retcode} → ${s.slice(0, 300)}`);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try { ws.close(); } catch {}
|
|||
|
|
console.log('\n探测结束');
|