71 lines
2.7 KiB
JavaScript
71 lines
2.7 KiB
JavaScript
// NapCat WebUI 网络配置管理(默认只读;写操作需显式 --apply)
|
||||
|
|
// 密码只从环境变量 NAPCAT_WEBUI_PASSWORD 读取,脚本内不留任何凭据或哈希。
|
|||
|
|
//
|
|||
|
|
// 用法:
|
|||
|
|
// NAPCAT_WEBUI_PASSWORD=xxx node tools/probe/napcat-webui.mjs list
|
|||
|
|
// NAPCAT_WEBUI_PASSWORD=xxx node tools/probe/napcat-webui.mjs version
|
|||
|
|
// NAPCAT_WEBUI_PASSWORD=xxx node tools/probe/napcat-webui.mjs scheme
|
|||
|
|
// NAPCAT_WEBUI_PASSWORD=xxx node tools/probe/napcat-webui.mjs apply <jsonFile> # 需 --apply
|
|||
|
|
const BASE = (process.env.NAPCAT_WEBUI_URL || 'http://192.168.5.32:6099').replace(/\/$/, '');
|
|||
|
|
const PASSWORD = process.env.NAPCAT_WEBUI_PASSWORD;
|
|||
|
|
|
|||
|
|
if (!PASSWORD) {
|
|||
|
|
console.error('缺少环境变量 NAPCAT_WEBUI_PASSWORD。凭据不得硬编码在脚本或仓库中。');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NapCat WebUI 登录方式:hash = SHA256(password + ".napcat"),实测确认
|
|||
|
|
const hash = await sha256Hex(`${PASSWORD}.napcat`);
|
|||
|
|
|
|||
|
|
async function sha256Hex(s) {
|
|||
|
|
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(s));
|
|||
|
|
return [...new Uint8Array(buf)].map(b => b.toString(16).padStart(2, '0')).join('');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async function api(path, { method = 'GET', body, cred } = {}) {
|
|||
|
|
const headers = { 'Content-Type': 'application/json' };
|
|||
|
|
if (cred) headers.Authorization = `Bearer ${cred}`;
|
|||
|
|
const res = await fetch(`${BASE}/api${path}`, {
|
|||
|
|
method, headers, body: body ? JSON.stringify(body) : undefined,
|
|||
|
|
});
|
|||
|
|
const text = await res.text();
|
|||
|
|
let json; try { json = JSON.parse(text); } catch { json = { __raw: text.slice(0, 400) }; }
|
|||
|
|
return { http: res.status, ...json };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const login = await api('/auth/login', { method: 'POST', body: { hash } });
|
|||
|
|
if (login.code !== 0) {
|
|||
|
|
console.error(`WebUI 登录失败: code=${login.code} message=${login.message}`);
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
const CRED = login.data?.Credential;
|
|||
|
|
console.error(`WebUI 登录成功(credential 长度 ${CRED?.length})`);
|
|||
|
|
|
|||
|
|
const cmd = process.argv[2];
|
|||
|
|
|
|||
|
|
switch (cmd) {
|
|||
|
|
case 'version': {
|
|||
|
|
const r = await api('/webui/version', { cred: CRED });
|
|||
|
|
console.log(JSON.stringify(r, null, 2));
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
case 'scheme': {
|
|||
|
|
const r = await api('/webui/scheme', { cred: CRED });
|
|||
|
|
console.log(JSON.stringify(r, null, 2));
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
case 'list': {
|
|||
|
|
const r = await api('/network/list', { method: 'POST', cred: CRED, body: {} });
|
|||
|
|
console.log(JSON.stringify(r, null, 2));
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
case 'apply': {
|
|||
|
|
console.error('apply 未启用:修改 NapCat 网络配置属于对外部运行实例的写操作,');
|
|||
|
|
console.error('需先人工确认新增项与影响范围,再显式实现。');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
default:
|
|||
|
|
console.error('用法: list | version | scheme | apply');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|