@nestim/koishi-plugin-qqgm-webui 0.2.4 → 0.2.6

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.
Files changed (2) hide show
  1. package/index.js +139 -138
  2. package/package.json +26 -26
package/index.js CHANGED
@@ -1,138 +1,139 @@
1
- /**
2
- * qqgm-webui 0.2.0:静态页面 + 只读 API。
3
- *
4
- * 已接:插件扫描、监控 SQLite、记忆 SQLite、日志文件。
5
- * 未接:配置写回、一键安装(下一阶段,需鉴权/备份/重载)。
6
- */
7
- const fs = require('node:fs');
8
- const path = require('node:path');
9
- const { Schema } = require('koishi');
10
- const { pluginList, monitor, memory } = require('./lib/data');
11
-
12
- const name = 'qqgm-webui';
13
- const inject = ['server'];
14
-
15
- const Config = Schema.object({
16
- enabled: Schema.boolean().default(true).description('启用 WebUI。'),
17
- route: Schema.string().default('/qqgm').description('挂载路径,需以 / 开头。'),
18
- token: Schema.string().default('').description('访问令牌;公网必须设置。'),
19
- allowRemote: Schema.boolean().default(false).description('是否允许非本机访问。'),
20
- });
21
-
22
- const TYPES = {
23
- '.html': 'text/html; charset=utf-8',
24
- '.css': 'text/css; charset=utf-8',
25
- '.js': 'text/javascript; charset=utf-8',
26
- '.png': 'image/png',
27
- '.svg': 'image/svg+xml',
28
- };
29
-
30
- function apply(ctx, config) {
31
- if (config.enabled === false) return;
32
- const logger = ctx.logger('qqgm-webui');
33
- const root = path.join(__dirname, 'public');
34
- const route = String(config.route || '/qqgm').replace(/\/+$/, '') || '/qqgm';
35
-
36
- if (!ctx.server || typeof ctx.server.get !== 'function') {
37
- logger.warn('未找到 Koishi server 插件:请启用 server 后再加载 WebUI');
38
- return;
39
- }
40
-
41
- const isLocal = (koa) => {
42
- const ip = String(koa.request.ip || koa.ip || '');
43
- return ip === '127.0.0.1' || ip === '::1' || ip === '::ffff:127.0.0.1';
44
- };
45
-
46
- const guard = (koa) => {
47
- if (!config.allowRemote && !isLocal(koa)) {
48
- koa.status = 403;
49
- koa.body = 'forbidden: set allowRemote=true and token';
50
- return false;
51
- }
52
- if (config.token) {
53
- const header = String(koa.request.headers.authorization || '');
54
- const bearer = header.startsWith('Bearer ') ? header.slice(7) : '';
55
- const query = String(koa.query && koa.query.token || '');
56
- if (bearer !== config.token && query !== config.token) {
57
- koa.status = 401;
58
- koa.body = 'unauthorized';
59
- return false;
60
- }
61
- }
62
- return true;
63
- };
64
-
65
- const sendFile = (koa, file) => {
66
- if (!guard(koa)) return;
67
- const full = path.join(root, file);
68
- if (!full.startsWith(root) || !fs.existsSync(full)) {
69
- koa.status = 404;
70
- koa.body = 'not found';
71
- return;
72
- }
73
- koa.type = TYPES[path.extname(full)] || 'application/octet-stream';
74
- koa.body = fs.createReadStream(full);
75
- };
76
-
77
- const json = (koa, value, status = 200) => {
78
- if (!guard(koa)) return;
79
- koa.status = status;
80
- koa.type = 'application/json; charset=utf-8';
81
- koa.body = value;
82
- };
83
-
84
- ctx.server.get(route, (koa) => {
85
- if (!guard(koa)) return;
86
- const qs = koa.querystring ? `?${koa.querystring}` : '';
87
- koa.redirect(`${route}/${qs}`);
88
- });
89
- ctx.server.get(`${route}/`, (koa) => sendFile(koa, 'index.html'));
90
- ctx.server.get(`${route}/styles.css`, (koa) => sendFile(koa, 'styles.css'));
91
- ctx.server.get(`${route}/app.js`, (koa) => sendFile(koa, 'app.js'));
92
-
93
- ctx.server.get(`${route}/api/health`, (koa) => json(koa, {
94
- ok: true,
95
- name,
96
- version: '0.2.4',
97
- route,
98
- tokenRequired: !!config.token,
99
- remoteAllowed: !!config.allowRemote,
100
- }));
101
-
102
- ctx.server.get(`${route}/api/overview`, (koa) => {
103
- json(koa, {
104
- plugins: pluginList(ctx),
105
- monitor: monitor(ctx),
106
- generatedAt: Date.now(),
107
- });
108
- });
109
-
110
- ctx.server.get(`${route}/api/plugins`, (koa) => json(koa, pluginList(ctx)));
111
-
112
- ctx.server.get(`${route}/api/memory`, (koa) => {
113
- json(koa, memory(ctx, {
114
- scope: koa.query.scope || '',
115
- query: koa.query.q || '',
116
- limit: koa.query.limit || 50,
117
- }));
118
- });
119
-
120
- ctx.server.get(`${route}/api/logs`, (koa) => {
121
- const file = path.join(ctx.baseDir || process.cwd(), 'data', 'logs', 'qqgm.log');
122
- let lines = [];
123
- try {
124
- lines = fs.readFileSync(file, 'utf8').trim().split(/\r?\n/).slice(-200);
125
- } catch { /* no log file */ }
126
- json(koa, { file, lines });
127
- });
128
-
129
- // 写操作下一阶段:需要 token + 备份 + schema 校验。
130
- ctx.server.post(`${route}/api/install`, (koa) => json(koa, {
131
- ok: false,
132
- error: 'prototype: install API not enabled',
133
- }, 501));
134
-
135
- logger.info(`WebUI 已挂载:${route}(token=${config.token ? '已设置' : '未设置'})`);
136
- }
137
-
138
- module.exports = { name, inject, Config, apply };
1
+ /**
2
+ * qqgm-webui 0.2.0:静态页面 + 只读 API。
3
+ *
4
+ * 已接:插件扫描、监控 SQLite、记忆 SQLite、日志文件。
5
+ * 未接:配置写回、一键安装(下一阶段,需鉴权/备份/重载)。
6
+ */
7
+ const fs = require('node:fs');
8
+ const path = require('node:path');
9
+ const { Schema } = require('koishi');
10
+ const { pluginList, monitor, memory } = require('./lib/data');
11
+
12
+ const name = 'qqgm-webui';
13
+ const inject = ['server'];
14
+
15
+ const Config = Schema.object({
16
+ enabled: Schema.boolean().default(true).description('启用 WebUI。'),
17
+ route: Schema.string().default('/qqgm').description('挂载路径,需以 / 开头。'),
18
+ token: Schema.string().default('').description('访问令牌;公网必须设置。'),
19
+ allowRemote: Schema.boolean().default(false).description('是否允许非本机访问。'),
20
+ });
21
+
22
+ const TYPES = {
23
+ '.html': 'text/html; charset=utf-8',
24
+ '.css': 'text/css; charset=utf-8',
25
+ '.js': 'text/javascript; charset=utf-8',
26
+ '.png': 'image/png',
27
+ '.svg': 'image/svg+xml',
28
+ };
29
+
30
+ function apply(ctx, config) {
31
+ if (config.enabled === false) return;
32
+ const logger = ctx.logger('qqgm-webui');
33
+ const root = path.join(__dirname, 'public');
34
+ const route = String(config.route || '/qqgm').replace(/\/+$/, '') || '/qqgm';
35
+
36
+ if (!ctx.server || typeof ctx.server.get !== 'function') {
37
+ logger.warn('未找到 Koishi server 插件:请启用 server 后再加载 WebUI');
38
+ return;
39
+ }
40
+
41
+ const isLocal = (koa) => {
42
+ const ip = String(koa.request.ip || koa.ip || '');
43
+ return ip === '127.0.0.1' || ip === '::1' || ip === '::ffff:127.0.0.1';
44
+ };
45
+
46
+ const guard = (koa) => {
47
+ if (!config.allowRemote && !isLocal(koa)) {
48
+ koa.status = 403;
49
+ koa.body = 'forbidden: set allowRemote=true and token';
50
+ return false;
51
+ }
52
+ if (config.token) {
53
+ const header = String(koa.request.headers.authorization || '');
54
+ const bearer = header.startsWith('Bearer ') ? header.slice(7) : '';
55
+ const query = String(koa.query && koa.query.token || '');
56
+ if (bearer !== config.token && query !== config.token) {
57
+ koa.status = 401;
58
+ koa.body = 'unauthorized';
59
+ return false;
60
+ }
61
+ }
62
+ return true;
63
+ };
64
+
65
+ const sendFile = (koa, file, needGuard = true) => {
66
+ if (needGuard && !guard(koa)) return;
67
+ const full = path.join(root, file);
68
+ if (!full.startsWith(root) || !fs.existsSync(full)) {
69
+ koa.status = 404;
70
+ koa.body = 'not found';
71
+ return;
72
+ }
73
+ koa.type = TYPES[path.extname(full)] || 'application/octet-stream';
74
+ koa.body = fs.createReadStream(full);
75
+ };
76
+
77
+ const json = (koa, value, status = 200) => {
78
+ if (!guard(koa)) return;
79
+ koa.status = status;
80
+ koa.type = 'application/json; charset=utf-8';
81
+ koa.body = value;
82
+ };
83
+
84
+ ctx.server.get(route, (koa) => {
85
+ if (koa.path !== route) return sendFile(koa, 'index.html');
86
+ if (!guard(koa)) return;
87
+ const qs = koa.querystring ? `?${koa.querystring}` : '';
88
+ koa.redirect(`${route}/${qs}`);
89
+ });
90
+ ctx.server.get(`${route}/`, (koa) => sendFile(koa, 'index.html'));
91
+ ctx.server.get(`${route}/styles.css`, (koa) => sendFile(koa, 'styles.css', false));
92
+ ctx.server.get(`${route}/app.js`, (koa) => sendFile(koa, 'app.js', false));
93
+
94
+ ctx.server.get(`${route}/api/health`, (koa) => json(koa, {
95
+ ok: true,
96
+ name,
97
+ version: '0.2.6',
98
+ route,
99
+ tokenRequired: !!config.token,
100
+ remoteAllowed: !!config.allowRemote,
101
+ }));
102
+
103
+ ctx.server.get(`${route}/api/overview`, (koa) => {
104
+ json(koa, {
105
+ plugins: pluginList(ctx),
106
+ monitor: monitor(ctx),
107
+ generatedAt: Date.now(),
108
+ });
109
+ });
110
+
111
+ ctx.server.get(`${route}/api/plugins`, (koa) => json(koa, pluginList(ctx)));
112
+
113
+ ctx.server.get(`${route}/api/memory`, (koa) => {
114
+ json(koa, memory(ctx, {
115
+ scope: koa.query.scope || '',
116
+ query: koa.query.q || '',
117
+ limit: koa.query.limit || 50,
118
+ }));
119
+ });
120
+
121
+ ctx.server.get(`${route}/api/logs`, (koa) => {
122
+ const file = path.join(ctx.baseDir || process.cwd(), 'data', 'logs', 'qqgm.log');
123
+ let lines = [];
124
+ try {
125
+ lines = fs.readFileSync(file, 'utf8').trim().split(/\r?\n/).slice(-200);
126
+ } catch { /* no log file */ }
127
+ json(koa, { file, lines });
128
+ });
129
+
130
+ // 写操作下一阶段:需要 token + 备份 + schema 校验。
131
+ ctx.server.post(`${route}/api/install`, (koa) => json(koa, {
132
+ ok: false,
133
+ error: 'prototype: install API not enabled',
134
+ }, 501));
135
+
136
+ logger.info(`WebUI 已挂载:${route}(token=${config.token ? '已设置' : '未设置'})`);
137
+ }
138
+
139
+ module.exports = { name, inject, Config, apply };
package/package.json CHANGED
@@ -1,26 +1,26 @@
1
- {
2
- "name": "@nestim/koishi-plugin-qqgm-webui",
3
- "version": "0.2.4",
4
- "description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
5
- "main": "index.js",
6
- "files": [
7
- "index.js",
8
- "lib",
9
- "public",
10
- "README.md"
11
- ],
12
- "license": "MIT",
13
- "keywords": ["koishi", "koishi-plugin", "qqgm", "webui", "admin"],
14
- "peerDependencies": {
15
- "koishi": "^4.18.7",
16
- "@nestim/koishi-plugin-qq-group-manager": ">=1.5.0"
17
- },
18
- "koishi": {
19
- "description": "QQGM 图形化管理台:插件/监控/记忆/配置/日志",
20
- "category": "manage"
21
- },
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/Miseat/koishi-plugin-qqgm-webui.git"
25
- }
26
- }
1
+ {
2
+ "name": "@nestim/koishi-plugin-qqgm-webui",
3
+ "version": "0.2.6",
4
+ "description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "lib",
9
+ "public",
10
+ "README.md"
11
+ ],
12
+ "license": "MIT",
13
+ "keywords": ["koishi", "koishi-plugin", "qqgm", "webui", "admin"],
14
+ "peerDependencies": {
15
+ "koishi": "^4.18.7",
16
+ "@nestim/koishi-plugin-qq-group-manager": ">=1.5.0"
17
+ },
18
+ "koishi": {
19
+ "description": "QQGM 图形化管理台:插件/监控/记忆/配置/日志",
20
+ "category": "manage"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/Miseat/koishi-plugin-qqgm-webui.git"
25
+ }
26
+ }