qq-bot-roost 1.1.19 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/web/app.js CHANGED
@@ -43,6 +43,17 @@ async function api(path, opts = {}) {
43
43
  err.status = res.status;
44
44
  throw err;
45
45
  }
46
+ /**
47
+ * 凭证(accessKey / loginPassword)刚被改过:服务端已即时吊销旧凭证和所有
48
+ * 会话,当前页面接下来每个请求都会 401。在这里直接跳登录页,别让用户对着
49
+ * 一片「访问密钥无效」猜发生了什么。
50
+ */
51
+ if (data?.credentialsChanged) {
52
+ const msg = data.message || '凭证已更新,请用新凭证重新登录';
53
+ try { sessionStorage.setItem('roostLoginNotice', msg); } catch { /* 隐私模式下忽略 */ }
54
+ location.reload();
55
+ return data;
56
+ }
46
57
  return data;
47
58
  }
48
59
 
@@ -1543,6 +1554,64 @@ window.addEventListener('hashchange', () => {
1543
1554
 
1544
1555
  /* ---------------- 概览 ---------------- */
1545
1556
 
1557
+ /** 宿主协议状态:概览卡与「外接」页的实时行共用同一份 status 快照 */
1558
+ const HOST_STATE_TEXT = {
1559
+ running: '运行中',
1560
+ disabled: '未开启',
1561
+ stopped: '已停止',
1562
+ 'missing-deps': '缺依赖',
1563
+ error: '启动失败',
1564
+ };
1565
+
1566
+ function hostStatusTone(state) {
1567
+ if (state === 'running') return 'var(--ok)';
1568
+ if (state === 'error' || state === 'missing-deps') return 'var(--err)';
1569
+ return 'var(--warn)';
1570
+ }
1571
+
1572
+ function hostDot(color) {
1573
+ return '<span style="display:inline-block;width:8px;height:8px;border-radius:50%;'
1574
+ + `background:${color};margin-right:6px;vertical-align:middle"></span>`;
1575
+ }
1576
+
1577
+ function renderHostOverview(host) {
1578
+ const el = $('#c-host');
1579
+ if (!el) return;
1580
+ const state = host?.state ?? 'disabled';
1581
+ const label = HOST_STATE_TEXT[state] ?? state;
1582
+ const detail = state === 'running'
1583
+ ? `${label} · ${Number(host?.connections ?? 0)} 连接`
1584
+ : label;
1585
+ el.innerHTML = hostDot(hostStatusTone(state)) + escapeHtml(detail);
1586
+ el.title = state === 'running'
1587
+ ? `实际监听 ${host?.host ?? '127.0.0.1'}:${host?.port ?? 0}` +
1588
+ ` · seq ${host?.seq ?? 0} · 点击查看`
1589
+ : escapeHtml(host?.error || '点击查看外接协议');
1590
+ el.onclick = () => { location.hash = 'onebot'; };
1591
+ }
1592
+
1593
+ function renderHostLive(host) {
1594
+ const el = $('#hostLiveStatus');
1595
+ if (!el || !host) return;
1596
+ const state = host.state ?? 'disabled';
1597
+ const label = HOST_STATE_TEXT[state] ?? state;
1598
+ const tr = host.transports ?? {};
1599
+ const channels = [tr.ws ? 'WS' : null, tr.sse ? 'SSE' : null, tr.http ? 'HTTP' : null]
1600
+ .filter(Boolean).join('/') || '—';
1601
+ const text = state === 'running'
1602
+ ? `${label} · ${host.host ?? '127.0.0.1'}:${host.port ?? 0}` +
1603
+ ` · ${Number(host.connections ?? 0)} 个连接 · ${channels} · seq ${host.seq ?? 0}`
1604
+ : label;
1605
+ el.innerHTML = hostDot(hostStatusTone(state)) + escapeHtml(text)
1606
+ + (host.error ? `<div style="color:var(--err);margin-top:4px">${escapeHtml(host.error)}</div>` : '');
1607
+ const hint = $('#hostStateHint');
1608
+ if (hint) {
1609
+ hint.textContent = state === 'running'
1610
+ ? `实时:${label} · ${host.port ?? 0} · ${Number(host.connections ?? 0)} 连接`
1611
+ : `实时:${label}`;
1612
+ }
1613
+ }
1614
+
1546
1615
  function renderStatus(s) {
1547
1616
  // 迟到的别的机器人的状态包,直接丢掉
1548
1617
  if (s && s.botId && CURRENT_BOT && s.botId !== CURRENT_BOT) return;
@@ -1573,6 +1642,9 @@ function renderStatus(s) {
1573
1642
  $('#botName').textContent = online ? s.botName || '已连接' : '未连接';
1574
1643
 
1575
1644
  $('#c-conn').textContent = online ? '在线' : state === 'error' ? '连接失败' : '离线';
1645
+ /** 外接协议状态跟着同一份 5 秒 status 走,概览卡和外接页实时行一起刷 */
1646
+ renderHostOverview(s.host);
1647
+ renderHostLive(s.host);
1576
1648
  $('#c-uptime').textContent = fmtDuration(s.uptimeMs);
1577
1649
  $('#c-cred').textContent = s.credentialExpiresIn
1578
1650
  ? `${Math.floor(s.credentialExpiresIn / 60)} 分`
@@ -1980,9 +2052,13 @@ function fillForm(c) {
1980
2052
 
1981
2053
  f.webuiHost.value = c.webui?.host || '127.0.0.1';
1982
2054
  f.webuiPort.value = c.webui?.port ?? 15207;
1983
- // 密钥不回显(后端只返回 accessKeySet):留空 = 不修改
2055
+ // 两把钥匙都不回显(后端只返回 *Set):留空 = 不修改
1984
2056
  f.webuiAccessKey.value = '';
1985
2057
  f.webuiAccessKey.placeholder = c.webui?.accessKeySet ? '已设置(留空则保持不变)' : '未设置';
2058
+ if (f.webuiLoginPassword) {
2059
+ f.webuiLoginPassword.value = '';
2060
+ f.webuiLoginPassword.placeholder = c.webui?.loginPasswordSet ? '已设置(留空则保持不变)' : '未设置';
2061
+ }
1986
2062
  f.logLevel.value = c.logLevel || 'info';
1987
2063
 
1988
2064
  f.groupInvited.checked = Boolean(c.groupManage?.invited);
@@ -2457,6 +2533,7 @@ function collectAutoPayload(f) {
2457
2533
  host: f.webuiHost.value.trim() || '127.0.0.1',
2458
2534
  port: Number(f.webuiPort.value) || 15207,
2459
2535
  accessKey: f.webuiAccessKey.value.trim(),
2536
+ ...(f.webuiLoginPassword?.value.trim() ? { loginPassword: f.webuiLoginPassword.value.trim() } : {}),
2460
2537
  },
2461
2538
  groupManage: { invited: f.groupInvited.checked },
2462
2539
  media: { placeholderSpace: f.mediaPlaceholderSpace.checked },
@@ -2558,6 +2635,7 @@ async function saveCfgForm({ fromQr = false } = {}) {
2558
2635
  host: f.webuiHost.value.trim() || '127.0.0.1',
2559
2636
  port: Number(f.webuiPort.value) || 15207,
2560
2637
  accessKey: f.webuiAccessKey.value.trim(),
2638
+ ...(f.webuiLoginPassword?.value.trim() ? { loginPassword: f.webuiLoginPassword.value.trim() } : {}),
2561
2639
  },
2562
2640
  groupManage: {
2563
2641
  invited: f.groupInvited.checked,
@@ -2789,11 +2867,15 @@ function bindSetup() {
2789
2867
  // 保存访问方式(webui 全局配置,增量提交;重启才生效)
2790
2868
  $('#setupNetSave')?.addEventListener('click', async () => {
2791
2869
  const key = $('#setupAccessKey')?.value.trim() || '';
2870
+ const loginPass = $('#setupLoginPassword')?.value.trim() || '';
2792
2871
  const host = $('#setupHost')?.value.trim() || '0.0.0.0';
2793
2872
  const port = Number($('#setupPort')?.value) || 15207;
2794
2873
  const err = $('#setupErr');
2795
- if (!key) {
2796
- if (err) { err.textContent = '对外服务必须设置访问密钥'; err.hidden = false; }
2874
+ if (!key || !loginPass) {
2875
+ if (err) {
2876
+ err.textContent = '对外服务要设两把钥匙:登录密码(给人用)和机器密钥(给脚本用),缺一不可';
2877
+ err.hidden = false;
2878
+ }
2797
2879
  return;
2798
2880
  }
2799
2881
  const btn = $('#setupNetSave');
@@ -2802,15 +2884,17 @@ function bindSetup() {
2802
2884
  try {
2803
2885
  await api('/api/config', {
2804
2886
  method: 'PUT',
2805
- body: JSON.stringify({ webui: { host, port, accessKey: key } }),
2887
+ body: JSON.stringify({ webui: { host, port, accessKey: key, loginPassword: loginPass } }),
2806
2888
  });
2807
2889
  if (err) {
2808
- err.textContent = '已保存。重启框架后生效:用局域网 / 公网地址加 ?key=你的密钥 访问面板。';
2890
+ err.textContent = '已保存。重启框架后生效:用局域网 / 公网地址打开面板,在登录页输入登录密码。';
2809
2891
  err.hidden = false;
2810
2892
  err.classList.add('setup-ok');
2811
2893
  }
2812
2894
  const access = $('#setupAccessKey');
2813
2895
  if (access) access.value = '';
2896
+ const passInput = $('#setupLoginPassword');
2897
+ if (passInput) passInput.value = '';
2814
2898
  } catch (e) {
2815
2899
  if (err) { err.textContent = '保存失败:' + errText(e); err.hidden = false; }
2816
2900
  } finally {
@@ -6584,14 +6668,6 @@ async function loadOnebot() {
6584
6668
  }
6585
6669
  }
6586
6670
 
6587
- const HOST_STATE_TEXT = {
6588
- running: '运行中',
6589
- disabled: '未开启',
6590
- stopped: '已停止',
6591
- 'missing-deps': '缺依赖',
6592
- error: '启动失败',
6593
- };
6594
-
6595
6671
  /** 宿主协议那半边:配置表单 + 运行状态 + 方法表 */
6596
6672
  function renderHost(d) {
6597
6673
  const cfg = d?.cfg ?? {};
@@ -6670,9 +6746,12 @@ function renderHost(d) {
6670
6746
  ? '<div class="hint" style="margin-top:6px;color:var(--err)"><b>机器人本体没上线</b>:' +
6671
6747
  '客户端能连上,但发消息、调官方接口会失败——先到「运行」页让它上线</div>'
6672
6748
  : '');
6673
- } else if (st.state === 'missing-deps' || st.state === 'error') {
6674
- hint.innerHTML = `<b>${escapeHtml(st.error || '没起来')}</b>` +
6675
- '。缺依赖就到下面「依赖」卡装,装完会自动重连。';
6749
+ } else if (st.state === 'missing-deps') {
6750
+ hint.innerHTML = `<b>${escapeHtml(st.error || '缺依赖')}</b>` +
6751
+ '。到下面「依赖」卡装,装完会自动重连。';
6752
+ } else if (st.state === 'error') {
6753
+ /** 端口被系统保留 / 被占都不是缺依赖,别再一律让用户去装依赖 */
6754
+ hint.innerHTML = `<b>${escapeHtml(st.error || '没起来')}</b>`;
6676
6755
  } else {
6677
6756
  hint.textContent = '开启后这里会显示连接串和运行状态。';
6678
6757
  }
package/web/index.html CHANGED
@@ -235,6 +235,9 @@
235
235
  <div class="card" title="开着时消息不真发,只记下本来会发什么(只影响内建插件;点击切换)">
236
236
  <div class="k">dry 模式</div><div class="v" id="c-dry">-</div>
237
237
  </div>
238
+ <div class="card" id="hostStatusCard" title="点击查看外接协议状态" style="cursor:pointer">
239
+ <div class="k">外接协议</div><div class="v" id="c-host">-</div>
240
+ </div>
238
241
  </div>
239
242
 
240
243
  <div class="row">
@@ -1044,7 +1047,8 @@ curl 'https://bot.q.qq.com/cgi-bin/v2/info/list_bots' \
1044
1047
  <div class="grid2">
1045
1048
  <label>监听地址<input name="webuiHost" placeholder="127.0.0.1"></label>
1046
1049
  <label>端口<input name="webuiPort" type="number"></label>
1047
- <label>面板访问密钥(留空则不校验;已设置时不会回显,想改就重填)<input name="webuiAccessKey" type="password" autocomplete="new-password"></label>
1050
+ <label>登录密码(给人用,8 位以上;留空则不修改)<input name="webuiLoginPassword" type="password" autocomplete="new-password"></label>
1051
+ <label>机器密钥(给 dsh-roost / 脚本用,16 位以上;留空则不修改)<input name="webuiAccessKey" type="password" autocomplete="new-password"></label>
1048
1052
  <label>日志级别
1049
1053
  <select name="logLevel">
1050
1054
  <option value="debug">debug</option>
@@ -1607,6 +1611,7 @@ curl 'https://bot.q.qq.com/cgi-bin/v2/info/list_bots' \
1607
1611
  <button class="btn small" id="btnHostReload">刷新状态</button>
1608
1612
  </div>
1609
1613
  <div class="hint" id="hostConnHint" style="margin-top:8px"></div>
1614
+ <div class="hint" id="hostLiveStatus" style="margin-top:8px"></div>
1610
1615
  </div>
1611
1616
 
1612
1617
  <div class="box">
@@ -2175,12 +2180,16 @@ curl 'https://bot.q.qq.com/cgi-bin/v2/info/list_bots' \
2175
2180
  </label>
2176
2181
  <div id="setupPublicFields" hidden>
2177
2182
  <p class="setup-note">
2178
- 对外服务必须设置访问密钥,否则任何人都能接管面板(含装插件 = 执行任意代码)。
2179
- 改监听地址和密钥都要<b>重启框架</b>才会生效;重启后用下面的地址加 <code>?key=你的密钥</code> 访问。
2183
+ 对外服务要设两把钥匙:<b>登录密码</b>给你在登录页输(8 位以上),<b>机器密钥</b>给 dsh-roost / 脚本调 API 用(16 位以上)。
2184
+ 不设的话任何人都能接管面板(含装插件 = 执行任意代码)。改监听地址和密钥都要<b>重启框架</b>才生效。
2180
2185
  </p>
2181
2186
  <div class="bot-form">
2182
2187
  <label>
2183
- <span>访问密钥</span>
2188
+ <span>登录密码(给人用,8 位以上)</span>
2189
+ <input id="setupLoginPassword" type="password" placeholder="设一个好记但不常见的密码" autocomplete="new-password">
2190
+ </label>
2191
+ <label>
2192
+ <span>机器密钥(给脚本用,16 位以上)</span>
2184
2193
  <input id="setupAccessKey" type="password" placeholder="设一个长随机串" autocomplete="new-password">
2185
2194
  </label>
2186
2195
  <label>
package/web/login.js ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * 面板登录表单(2026-11-28)。
3
+ *
4
+ * 为什么单独一个文件:主面板 CSP 是 `script-src 'self'`,401 页里不能写内联
5
+ * <script>,否则被 CSP 拦掉、表单按了没反应还查不出原因。同源静态文件不在
6
+ * 鉴权范围内(见 server.js「静态文件」段),所以未登录时也加载得到。
7
+ *
8
+ * 密钥只在 POST body 里交给 /api/login;成功后服务端设 HttpOnly Cookie 并
9
+ * 返回 200,这里 reload 让首页带上 Cookie 走正常会话。
10
+ *
11
+ * 保留兼容:?key= 首跳那条路一行没改,dsh-roost / roost-mcp 照常工作。
12
+ */
13
+ (function () {
14
+ 'use strict';
15
+ var form = document.getElementById('loginForm');
16
+ var input = document.getElementById('panelKey');
17
+ var err = document.getElementById('loginError');
18
+ if (!form || !input || !err) return;
19
+
20
+ /** 改完凭证被踢回登录页时,把原因显示出来(不是普通「没会话」) */
21
+ try {
22
+ var notice = sessionStorage.getItem('roostLoginNotice');
23
+ if (notice) {
24
+ err.textContent = notice;
25
+ err.style.color = '';
26
+ sessionStorage.removeItem('roostLoginNotice');
27
+ }
28
+ } catch (_) {
29
+ /* 隐私模式下 sessionStorage 不可用,忽略 */
30
+ }
31
+
32
+ form.addEventListener('submit', async function (e) {
33
+ e.preventDefault();
34
+ err.textContent = '';
35
+ var key = input.value;
36
+ if (!key) {
37
+ err.textContent = '请输入访问密钥';
38
+ return;
39
+ }
40
+ var btn = form.querySelector('button[type="submit"]');
41
+ if (btn) btn.disabled = true;
42
+ try {
43
+ var res = await fetch('/api/login', {
44
+ method: 'POST',
45
+ headers: { 'Content-Type': 'application/json' },
46
+ body: JSON.stringify({ key: key }),
47
+ });
48
+ if (res.ok) {
49
+ location.reload();
50
+ return;
51
+ }
52
+ var msg = '登录失败(' + res.status + ')';
53
+ try {
54
+ var data = await res.json();
55
+ if (data && data.error) msg = data.error;
56
+ } catch (_) {
57
+ /* 非 JSON 响应:保留状态码文案 */
58
+ }
59
+ err.textContent = msg;
60
+ input.focus();
61
+ input.select();
62
+ } catch (netErr) {
63
+ err.textContent = '网络错误,请确认面板是否还在运行';
64
+ } finally {
65
+ if (btn) btn.disabled = false;
66
+ }
67
+ });
68
+ })();