60 lines
2.2 KiB
JavaScript
60 lines
2.2 KiB
JavaScript
// 安全验证:未授权客户端能否通过 NapCat WebSocket 调用 API(只调用只读接口)
|
|||
|
|
// 用法: node tools/probe/probe-ws-unauth.mjs <wsUrl>
|
||
|
|
const base = (process.argv[2] || 'ws://192.168.5.32:16099').replace(/\/$/, '');
|
||
|
|
|
||
|
|
function run(label, target) {
|
||
|
|
return new Promise((resolve) => {
|
||
|
|
const res = { label, open: false, apiWorked: null, detail: '' };
|
||
|
|
const ws = new WebSocket(target);
|
||
|
|
const pending = new Map();
|
||
|
|
const finish = () => { try { ws.close(); } catch {} ; resolve(res); };
|
||
|
|
const hardTimer = setTimeout(finish, 20000);
|
||
|
|
|
||
|
|
ws.onopen = () => { res.open = true; };
|
||
|
|
ws.onmessage = (ev) => {
|
||
|
|
let o; try { o = JSON.parse(String(ev.data)); } catch { return; }
|
||
|
|
if (o.echo && pending.has(o.echo)) {
|
||
|
|
const p = pending.get(o.echo); pending.delete(o.echo);
|
||
|
|
res.apiWorked = o.status === 'ok';
|
||
|
|
res.detail = `retcode=${o.retcode} data=${JSON.stringify(o.data).slice(0, 160)}`;
|
||
|
|
clearTimeout(p.to); clearTimeout(hardTimer);
|
||
|
|
finish();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
ws.onclose = () => {
|
||
|
|
if (!res.open) { res.detail = `握手即被关闭 close`; clearTimeout(hardTimer); resolve(res); }
|
||
|
|
};
|
||
|
|
ws.onerror = () => {};
|
||
|
|
|
||
|
|
ws.addEventListener?.('open', () => {
|
||
|
|
const echo = `unauth-${Date.now()}`;
|
||
|
|
const to = setTimeout(() => {
|
||
|
|
pending.delete(echo);
|
||
|
|
res.apiWorked = false;
|
||
|
|
res.detail = '发送 action 后 15s 无任何响应';
|
||
|
|
clearTimeout(hardTimer);
|
||
|
|
finish();
|
||
|
|
}, 15000);
|
||
|
|
pending.set(echo, { to });
|
||
|
|
try {
|
||
|
|
ws.send(JSON.stringify({ action: 'get_version_info', params: {}, echo }));
|
||
|
|
} catch (e) {
|
||
|
|
res.detail = 'send 失败: ' + e.message;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('测试:完全不带任何凭据的客户端,能否调用 NapCat API');
|
||
|
|
console.log('(若返回 status=ok,说明该 WebSocket 端口对任意内网主机开放全部 QQ 账号操作能力)\n');
|
||
|
|
for (const [label, t] of [
|
||
|
|
['bare', base],
|
||
|
|
['wrong-token', `${base}/?access_token=definitely_wrong`],
|
||
|
|
]) {
|
||
|
|
const r = await run(label, t);
|
||
|
|
console.log(`- ${label}`);
|
||
|
|
console.log(` 握手成功 : ${r.open}`);
|
||
|
|
console.log(` API 可调用 : ${r.apiWorked}`);
|
||
|
|
console.log(` 详情 : ${r.detail}`);
|
||
|
|
}
|