fde-console 0.0.0-stage → 2.9.68
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/LICENSE +21 -0
- package/README.md +128 -2
- package/README.zh-CN.md +130 -0
- package/assets/icon.svg +9 -0
- package/cordis.patch.yml +7 -0
- package/lib/agentActivity.js +1180 -0
- package/lib/agentContext.js +146 -0
- package/lib/agentTranscript.js +1398 -0
- package/lib/analyze.js +625 -0
- package/lib/attribute.js +205 -0
- package/lib/backup.js +366 -0
- package/lib/desktopAgents.js +123 -0
- package/lib/diary.js +333 -0
- package/lib/dingtalk.js +412 -0
- package/lib/feishu.js +580 -0
- package/lib/giteaClient.js +810 -0
- package/lib/giteeClient.js +372 -0
- package/lib/githubClient.js +361 -0
- package/lib/heatmap.js +99 -0
- package/lib/imNotify.js +378 -0
- package/lib/inbox.js +438 -0
- package/lib/instanceLock.js +122 -0
- package/lib/jobs.js +56 -0
- package/lib/learn.js +1388 -0
- package/lib/learnImport.js +283 -0
- package/lib/learnSeeds.js +97 -0
- package/lib/learnSeedsAiEng.js +77 -0
- package/lib/mcpCore.js +135 -0
- package/lib/members.js +160 -0
- package/lib/migration.js +403 -0
- package/lib/overview.js +376 -0
- package/lib/postmortem.js +148 -0
- package/lib/radarIgnore.js +134 -0
- package/lib/redact.js +76 -0
- package/lib/router.js +246 -0
- package/lib/routes-gitea.js +262 -0
- package/lib/routes-setup.js +40 -0
- package/lib/scorecard.js +111 -0
- package/lib/sessionRef.js +227 -0
- package/lib/sessionSnapshot.js +67 -0
- package/lib/sessionSuggest.js +186 -0
- package/lib/settings.js +783 -0
- package/lib/setupCheck.js +330 -0
- package/lib/ssrfGuard.js +145 -0
- package/lib/store.js +564 -0
- package/lib/storeDb.js +418 -0
- package/lib/taskEvents.js +243 -0
- package/lib/webhookOut.js +125 -0
- package/lib/weekly.js +439 -0
- package/lib/workSearch.js +220 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +46 -4
- package/plugin/adapter.js +157 -0
- package/plugin/client.js +282 -0
- package/plugin/index.mjs +116 -0
- package/plugin/static.js +99 -0
- package/scripts/backup-data.js +41 -0
- package/scripts/mcp-server.mjs +68 -0
- package/scripts/migrate-data.js +301 -0
- package/scripts/migrate-storage.js +47 -0
- package/scripts/seed-demo.js +200 -0
- package/scripts/write-agent-context.js +43 -0
- package/test/helpers/appAssembler.js +1078 -0
- package/web/client/fde-client.js +292 -0
- package/web/css/app.css +1510 -0
- package/web/index.html +69 -0
- package/web/js/api.js +72 -0
- package/web/js/app.js +313 -0
- package/web/js/icons.js +48 -0
- package/web/js/mock.js +1150 -0
- package/web/js/state.js +7 -0
- package/web/js/ui.js +276 -0
- package/web/js/views/activity.js +410 -0
- package/web/js/views/agents.js +892 -0
- package/web/js/views/board.js +1656 -0
- package/web/js/views/learn.js +530 -0
- package/web/js/views/members.js +344 -0
- package/web/js/views/onboarding.js +103 -0
- package/web/js/views/overview.js +508 -0
- package/web/js/views/radar.js +1359 -0
- package/web/js/views/search.js +149 -0
- package/web/js/views/settings.js +1351 -0
- package/web/js/views/topology.js +471 -0
- package/web/mock.json +2363 -0
package/lib/redact.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// redact.js — 凭据脱敏单一出口(SPEC §5-③)。
|
|
3
|
+
// 规则:所有日志/错误/落盘文本必经 redact(),按两个维度过滤:
|
|
4
|
+
// a) 按 key(config.security.redactKeys = ["Authorization","extraheader"],大小写不敏感):
|
|
5
|
+
// 覆盖 HTTP 头 `Key: value`、JSON `"key":"value"`、git-config `key=value` 三种形态;
|
|
6
|
+
// b) 按凭据原值:registerSecret 注册的值与 extraSecrets 参数,每次出现都替换。
|
|
7
|
+
// 敏感值仅存本模块内存 Set;本模块绝不写任何文件、绝不打印自身内容。
|
|
8
|
+
|
|
9
|
+
const DEFAULT_KEYS = ['Authorization', 'extraheader'];
|
|
10
|
+
|
|
11
|
+
/** @type {Set<string>} 仅内存的凭据原值集合 */
|
|
12
|
+
const secrets = new Set();
|
|
13
|
+
|
|
14
|
+
/** @type {string[]} 按 key 过滤的键名(大小写不敏感) */
|
|
15
|
+
let keys = DEFAULT_KEYS.slice();
|
|
16
|
+
|
|
17
|
+
function escapeRegExp(s) {
|
|
18
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 注册凭据原值(仅内存)。短于 4 字符的值忽略(无脱敏意义)。
|
|
23
|
+
* @param {string|null|undefined} value
|
|
24
|
+
*/
|
|
25
|
+
function registerSecret(value) {
|
|
26
|
+
if (typeof value !== 'string') return;
|
|
27
|
+
const v = value.trim();
|
|
28
|
+
if (v.length >= 4) secrets.add(v);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 清空内存凭据集合(仅测试用) */
|
|
32
|
+
function clearSecrets() {
|
|
33
|
+
secrets.clear();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 设置按 key 过滤的键名列表(来自 config.security.redactKeys)。
|
|
38
|
+
* @param {string[]|undefined} list
|
|
39
|
+
*/
|
|
40
|
+
function setRedactKeys(list) {
|
|
41
|
+
if (Array.isArray(list) && list.length) keys = list.map(String);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 脱敏任意文本(错误 message、日志行、JSON 串等)。
|
|
46
|
+
* @param {unknown} text 任意可字符串化的值
|
|
47
|
+
* @param {string[]} [extraSecrets] 额外要按值过滤的凭据
|
|
48
|
+
* @returns {string}
|
|
49
|
+
*/
|
|
50
|
+
function redact(text, extraSecrets = []) {
|
|
51
|
+
let out = text == null ? '' : String(text);
|
|
52
|
+
|
|
53
|
+
// b) 按凭据原值过滤(先做,保证整串凭据优先被整体抹除)
|
|
54
|
+
const pool = extraSecrets.concat([...secrets]);
|
|
55
|
+
for (const s of pool) {
|
|
56
|
+
if (typeof s === 'string' && s.length >= 4) {
|
|
57
|
+
out = out.split(s).join('[REDACTED]');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// a) 按 key 过滤
|
|
62
|
+
for (const key of keys) {
|
|
63
|
+
const k = escapeRegExp(key);
|
|
64
|
+
// 形态 1:`Key: value` / `key=value`(HTTP 头、git-config、一般键值)。
|
|
65
|
+
// 值允许多段(如 "Basic xxx"),到行尾或分隔符为止
|
|
66
|
+
out = out.replace(new RegExp(`(${k}\\s*[:=]\\s*)[^\\r\\n,;&"'}\\]]+`, 'gi'), '$1[REDACTED]');
|
|
67
|
+
// 形态 2:JSON 双引号 `"key":"value"`
|
|
68
|
+
out = out.replace(new RegExp(`("${k}"\\s*:\\s*")([^"]*)"`, 'gi'), '$1[REDACTED]"');
|
|
69
|
+
// 形态 3:JSON 单引号 `'key':'value'`
|
|
70
|
+
out = out.replace(new RegExp(`('${k}'\\s*:\\s*')([^']*)'`, 'gi'), "$1[REDACTED]'");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = { redact, registerSecret, clearSecrets, setRedactKeys, getRedactKeys: () => keys.slice() };
|
package/lib/router.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* router.js — 方法+路径分发、统一 JSON 错误出口、gitea 路由挂载兼容
|
|
5
|
+
* 零依赖;路径模式支持 :param 段;未知 /api/* 统一 JSON 404。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
class HttpError extends Error {
|
|
9
|
+
constructor(status, code, message) {
|
|
10
|
+
super(message || code);
|
|
11
|
+
this.name = 'HttpError';
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function sendJSON(res, status, obj) {
|
|
18
|
+
const body = JSON.stringify(obj);
|
|
19
|
+
res.writeHead(status, {
|
|
20
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
21
|
+
'Content-Length': Buffer.byteLength(body),
|
|
22
|
+
'Cache-Control': 'no-store',
|
|
23
|
+
});
|
|
24
|
+
res.end(body);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 统一错误体:{"error":{"code":"<大写蛇形>","message":"<中文>"}} */
|
|
28
|
+
function sendError(res, status, code, message) {
|
|
29
|
+
sendJSON(res, status, { error: { code, message: message || '' } });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function noContent(res) {
|
|
33
|
+
res.statusCode = 204;
|
|
34
|
+
res.setHeader('Content-Length', 0);
|
|
35
|
+
res.end();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const BODY_LIMIT = 1024 * 1024; // 1MB
|
|
39
|
+
|
|
40
|
+
/** CSRF 防线①(qoder P0-1):带请求体的写方法必须 Content-Type: application/json。
|
|
41
|
+
* 跨源 text/plain 简单请求被 400 拒绝 → 迫使浏览器走预检,而本服务不处理 OPTIONS(404),预检必败。 */
|
|
42
|
+
const WRITE_METHODS = new Set(['POST', 'PATCH', 'PUT', 'DELETE']);
|
|
43
|
+
|
|
44
|
+
function contentTypeError(req) {
|
|
45
|
+
if (!WRITE_METHODS.has(req.method)) return null;
|
|
46
|
+
const hasBody =
|
|
47
|
+
(Number(req.headers['content-length']) > 0 || req.headers['transfer-encoding'] !== undefined);
|
|
48
|
+
if (!hasBody) return null;
|
|
49
|
+
const ct = String(req.headers['content-type'] || '').split(';')[0].trim().toLowerCase();
|
|
50
|
+
if (ct !== 'application/json') {
|
|
51
|
+
return new HttpError(400, 'CONTENT_TYPE_REQUIRED', '写请求必须携带 Content-Type: application/json');
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** #9:错误堆栈输出前过 redact()(redact.js 缺位时原样输出,绝不因日志崩溃) */
|
|
57
|
+
let redactFn = null;
|
|
58
|
+
let redactTried = false;
|
|
59
|
+
function redactText(text) {
|
|
60
|
+
if (!redactTried) {
|
|
61
|
+
redactTried = true;
|
|
62
|
+
try {
|
|
63
|
+
redactFn = require('./redact.js').redact;
|
|
64
|
+
} catch (e) {
|
|
65
|
+
redactFn = null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
return redactFn ? redactFn(String(text)) : String(text);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
return String(text);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 读取请求体并按 JSON 解析;空体 → {};非法 JSON → 400 INVALID_JSON */
|
|
76
|
+
function readBody(req) {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
const chunks = [];
|
|
79
|
+
let size = 0;
|
|
80
|
+
req.on('data', (ch) => {
|
|
81
|
+
size += ch.length;
|
|
82
|
+
if (size > BODY_LIMIT) {
|
|
83
|
+
reject(new HttpError(413, 'BODY_TOO_LARGE', '请求体超过 1MB 上限'));
|
|
84
|
+
req.destroy();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
chunks.push(ch);
|
|
88
|
+
});
|
|
89
|
+
req.on('end', () => {
|
|
90
|
+
if (chunks.length === 0) return resolve({});
|
|
91
|
+
const raw = Buffer.concat(chunks).toString('utf8').trim();
|
|
92
|
+
if (!raw) return resolve({});
|
|
93
|
+
try {
|
|
94
|
+
const parsed = JSON.parse(raw);
|
|
95
|
+
resolve(parsed === null || parsed === undefined ? {} : parsed);
|
|
96
|
+
} catch (e) {
|
|
97
|
+
reject(new HttpError(400, 'INVALID_JSON', '请求体不是合法 JSON'));
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
req.on('error', reject);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
class Router {
|
|
105
|
+
constructor() {
|
|
106
|
+
this.routes = [];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* add('GET', '/api/tasks/:id', handler)
|
|
111
|
+
* options.rawBody=true:跳过统一 readBody 预读,由 handler 自行消费请求体
|
|
112
|
+
* (用于挂载自读 body 的外部 handler,如 routes-gitea.js 的 handleGiteaTest)
|
|
113
|
+
*/
|
|
114
|
+
add(method, pattern, handler, options) {
|
|
115
|
+
const segs = pattern
|
|
116
|
+
.split('/')
|
|
117
|
+
.filter(Boolean)
|
|
118
|
+
.map((s) => (s.startsWith(':') ? { name: s.slice(1) } : { literal: s.toLowerCase() }));
|
|
119
|
+
this.routes.push({
|
|
120
|
+
method: method.toUpperCase(),
|
|
121
|
+
segs,
|
|
122
|
+
pattern,
|
|
123
|
+
handler,
|
|
124
|
+
rawBody: !!(options && options.rawBody),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
match(method, pathname) {
|
|
129
|
+
const parts = pathname.split('/').filter(Boolean);
|
|
130
|
+
for (const r of this.routes) {
|
|
131
|
+
if (r.method !== method || r.segs.length !== parts.length) continue;
|
|
132
|
+
const params = {};
|
|
133
|
+
let ok = true;
|
|
134
|
+
for (let i = 0; i < r.segs.length; i++) {
|
|
135
|
+
const seg = r.segs[i];
|
|
136
|
+
if (seg.name) {
|
|
137
|
+
try {
|
|
138
|
+
params[seg.name] = decodeURIComponent(parts[i]);
|
|
139
|
+
} catch (e) {
|
|
140
|
+
ok = false;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
} else if (seg.literal !== parts[i].toLowerCase()) {
|
|
144
|
+
ok = false;
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (ok) return { handler: r.handler, params, route: r };
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** 分发一个 /api/* 请求;统一 JSON 错误出口 */
|
|
154
|
+
async dispatch(ctx, req, res) {
|
|
155
|
+
let url;
|
|
156
|
+
try {
|
|
157
|
+
url = new URL(req.url, 'http://127.0.0.1');
|
|
158
|
+
} catch (e) {
|
|
159
|
+
return sendError(res, 400, 'BAD_REQUEST', '非法请求路径');
|
|
160
|
+
}
|
|
161
|
+
const pathname = url.pathname;
|
|
162
|
+
let found;
|
|
163
|
+
try {
|
|
164
|
+
found = this.match(req.method, pathname);
|
|
165
|
+
} catch (e) {
|
|
166
|
+
return sendError(res, 400, 'BAD_REQUEST', '非法请求路径');
|
|
167
|
+
}
|
|
168
|
+
if (!found) return sendError(res, 404, 'NOT_FOUND', '接口不存在');
|
|
169
|
+
try {
|
|
170
|
+
const ctErr = contentTypeError(req);
|
|
171
|
+
if (ctErr) throw ctErr;
|
|
172
|
+
const body = found.route.rawBody ? {} : await readBody(req);
|
|
173
|
+
await found.handler({
|
|
174
|
+
req,
|
|
175
|
+
res,
|
|
176
|
+
params: found.params,
|
|
177
|
+
query: url.searchParams,
|
|
178
|
+
body,
|
|
179
|
+
config: ctx.config,
|
|
180
|
+
ctx,
|
|
181
|
+
});
|
|
182
|
+
} catch (e) {
|
|
183
|
+
if (e instanceof HttpError) return sendError(res, e.status, e.code, e.message);
|
|
184
|
+
console.error('[api] 未捕获错误:', redactText((e && e.stack) || e));
|
|
185
|
+
if (!res.headersSent) return sendError(res, 500, 'INTERNAL', '服务器内部错误');
|
|
186
|
+
try {
|
|
187
|
+
res.end();
|
|
188
|
+
} catch (_) { /* 已断开 */ }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* gitea 路由挂载兼容(另一岗 mcode 负责 lib/routes-gitea.js,本侧绝不创建该文件)。
|
|
195
|
+
* 识别两种导出形态:
|
|
196
|
+
* ① register/mount(router, ctx) 函数;
|
|
197
|
+
* ② 裸 handler 对 {handleActivities(req,res), handleGiteaTest(req,res), setConfigProvider(fn)}——
|
|
198
|
+
* 由本侧代为挂到 GET /api/activities 与 POST /api/gitea/test,
|
|
199
|
+
* rawBody 挂载(对方自读请求体),并接 setConfigProvider(() => ctx.config) 使设置 PATCH 即时生效。
|
|
200
|
+
* WP6 焊死(业主代决):插件态无独立端口即无 stub 场景——routes-gitea-stub.js 已删除,
|
|
201
|
+
* 模块不存在/形态不符/加载失败时不再兜底 502,两路径自然 404(路由未挂载即不存在)。
|
|
202
|
+
*/
|
|
203
|
+
function mountGiteaRoutes(router, ctx) {
|
|
204
|
+
let mounted = false;
|
|
205
|
+
try {
|
|
206
|
+
const mod = require('./routes-gitea.js');
|
|
207
|
+
if (typeof mod === 'function' || typeof (mod && (mod.register || mod.mount)) === 'function') {
|
|
208
|
+
const reg = typeof mod === 'function' ? mod : mod.register || mod.mount;
|
|
209
|
+
reg(router, ctx);
|
|
210
|
+
mounted = true;
|
|
211
|
+
console.log('[gitea] routes-gitea.js 已以 register 形态挂载');
|
|
212
|
+
} else if (mod && (typeof mod.handleActivities === 'function' || typeof mod.handleGiteaTest === 'function')) {
|
|
213
|
+
if (typeof mod.setConfigProvider === 'function') {
|
|
214
|
+
mod.setConfigProvider(() => ctx.config);
|
|
215
|
+
}
|
|
216
|
+
if (typeof mod.handleActivities === 'function') {
|
|
217
|
+
router.add('GET', '/api/activities', (c) => mod.handleActivities(c.req, c.res), { rawBody: true });
|
|
218
|
+
}
|
|
219
|
+
if (typeof mod.handleGiteaTest === 'function') {
|
|
220
|
+
router.add('POST', '/api/gitea/test', (c) => mod.handleGiteaTest(c.req, c.res), { rawBody: true });
|
|
221
|
+
}
|
|
222
|
+
if (typeof mod.handleSourceTest === 'function') {
|
|
223
|
+
router.add('POST', '/api/sources/test', (c) => mod.handleSourceTest(c.req, c.res), { rawBody: true });
|
|
224
|
+
}
|
|
225
|
+
mounted = true;
|
|
226
|
+
console.log('[gitea] routes-gitea.js 已以 handler 形态挂载 /api/activities、/api/gitea/test 与 /api/sources/test');
|
|
227
|
+
} else {
|
|
228
|
+
console.error('[gitea] routes-gitea.js 存在但无已知导出形态,路由未挂载(WP6:stub 兜底已删)');
|
|
229
|
+
}
|
|
230
|
+
} catch (e) {
|
|
231
|
+
if (e && e.code !== 'MODULE_NOT_FOUND') {
|
|
232
|
+
console.error('[gitea] routes-gitea.js 加载失败,路由未挂载:', e.message);
|
|
233
|
+
}
|
|
234
|
+
mounted = false;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
module.exports = {
|
|
239
|
+
HttpError,
|
|
240
|
+
sendJSON,
|
|
241
|
+
sendError,
|
|
242
|
+
noContent,
|
|
243
|
+
readBody,
|
|
244
|
+
Router,
|
|
245
|
+
mountGiteaRoutes,
|
|
246
|
+
};
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// routes-gitea.js — §3 动态/连接测试两个 handler(原生 http,供主路由挂载)。
|
|
3
|
+
// 职责边界:只写 handler,不建服务器、不做 Host 头校验(主路由/服务骨架职责)。
|
|
4
|
+
// matchedTaskIds 在读取响应时由本 handler 现算(§3 读取口径),绝不落盘(§4)。
|
|
5
|
+
|
|
6
|
+
const fs = require('node:fs');
|
|
7
|
+
const path = require('node:path');
|
|
8
|
+
const { redact } = require('./redact.js');
|
|
9
|
+
const giteaClient = require('./giteaClient.js');
|
|
10
|
+
const githubClient = require('./githubClient.js');
|
|
11
|
+
const giteeClient = require('./giteeClient.js');
|
|
12
|
+
const store = require('./store.js');
|
|
13
|
+
|
|
14
|
+
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
|
15
|
+
|
|
16
|
+
/** 测试可注入 cfg 提供器(默认 loadConfig(),每次请求现读,设置 PATCH 后即时生效) */
|
|
17
|
+
let configProvider = null;
|
|
18
|
+
function setConfigProvider(fn) {
|
|
19
|
+
configProvider = typeof fn === 'function' ? fn : null;
|
|
20
|
+
}
|
|
21
|
+
function currentCfg() {
|
|
22
|
+
return configProvider ? configProvider() : giteaClient.loadConfig();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function dataPath(cfg, name) {
|
|
26
|
+
const dir = path.isAbsolute(cfg.dataDir) ? cfg.dataDir : path.join(PROJECT_ROOT, cfg.dataDir);
|
|
27
|
+
return path.join(dir, name);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function sendJson(res, status, obj) {
|
|
31
|
+
const body = JSON.stringify(obj);
|
|
32
|
+
res.writeHead(status, {
|
|
33
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
34
|
+
'Content-Length': Buffer.byteLength(body)
|
|
35
|
+
});
|
|
36
|
+
res.end(body);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function sendError(res, status, code, message) {
|
|
40
|
+
sendJson(res, status, { error: { code, message } });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** SSRF 守卫错误 → §3 的 400 响应(协议违规/host 违规统一 code=HOST_NOT_ALLOWED) */
|
|
44
|
+
function guardErrorResponse(res, err) {
|
|
45
|
+
if (err && err.code === 'HOST_NOT_ALLOWED') {
|
|
46
|
+
sendError(res, 400, 'HOST_NOT_ALLOWED', err.message || 'host 不在白名单');
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
if (err && err.code === 'DANGEROUS_WHITELIST_ENTRY') {
|
|
50
|
+
sendError(res, 400, 'HOST_NOT_ALLOWED', 'host 不在白名单(白名单配置含禁止地址)');
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readBody(req, limit = 1024 * 1024) {
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
const chunks = [];
|
|
59
|
+
let size = 0;
|
|
60
|
+
req.on('data', (c) => {
|
|
61
|
+
size += c.length;
|
|
62
|
+
if (size > limit) {
|
|
63
|
+
const err = new Error('请求体过大');
|
|
64
|
+
err.code = 'PAYLOAD_TOO_LARGE';
|
|
65
|
+
reject(err);
|
|
66
|
+
req.destroy();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
chunks.push(c);
|
|
70
|
+
});
|
|
71
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
72
|
+
req.on('error', reject);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ------------------------------------------------------- matchedTaskIds 现算
|
|
77
|
+
|
|
78
|
+
/** 读任务/动态:v2.9.33 批次 2 收敛到 store 路由(存储迁移后 fs 直读会静默读空——
|
|
79
|
+
* 本地 fs 版 loader 已删除;cfg 形状换为 store 需要的 ctx 形状;返回 .tasks 数组保持调用方契约) */
|
|
80
|
+
function loadTasks(cfg) {
|
|
81
|
+
return store.loadTasks({ tasksPath: dataPath(cfg, 'tasks.json') }).tasks;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 互为 ≥4 字子串(双向 includes,两边都须 ≥4 字符) */
|
|
85
|
+
function mutualSubstring(a, b) {
|
|
86
|
+
const x = String(a || '').trim();
|
|
87
|
+
const y = String(b || '').trim();
|
|
88
|
+
if (x.length < 4 || y.length < 4) return false;
|
|
89
|
+
return x.includes(y) || y.includes(x);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** §3 口径:仓库护栏(跨仓同名 branch/标题不匹配,attribute.repoMismatch 单一事实源)→ branch 精确相等,或 title/branch 互为 ≥4 字子串 */
|
|
93
|
+
function matchTaskIds(activity, tasks) {
|
|
94
|
+
const ids = [];
|
|
95
|
+
for (const t of tasks) {
|
|
96
|
+
if (!t || !t.id) continue;
|
|
97
|
+
if (require('./attribute.js').repoMismatch(activity, t)) continue;
|
|
98
|
+
if (t.branch && activity.branch && String(t.branch) === String(activity.branch)) {
|
|
99
|
+
ids.push(t.id);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (
|
|
103
|
+
mutualSubstring(t.title, activity.title) ||
|
|
104
|
+
mutualSubstring(t.title, activity.branch) ||
|
|
105
|
+
mutualSubstring(t.branch, activity.title) ||
|
|
106
|
+
mutualSubstring(t.branch, activity.branch)
|
|
107
|
+
) {
|
|
108
|
+
ids.push(t.id);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return ids;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function loadActivities(cfg) {
|
|
115
|
+
return store.loadActivities({ activitiesPath: dataPath(cfg, 'activities.json') });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ------------------------------------------------------------------ handlers
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* GET /api/activities?refresh=1(§3)
|
|
122
|
+
* refresh=1 强制拉取 Gitea;成功/缺省返回 200 数组(响应带 matchedTaskIds,不落盘)。
|
|
123
|
+
* 拉取失败:HOST_NOT_ALLOWED → 400;其他 → 502 GITEA_ERROR(message 已脱敏)。
|
|
124
|
+
* @param {import('node:http').IncomingMessage} req
|
|
125
|
+
* @param {import('node:http').ServerResponse} res
|
|
126
|
+
*/
|
|
127
|
+
async function handleActivities(req, res) {
|
|
128
|
+
try {
|
|
129
|
+
const url = new URL(req.url || '/', 'http://127.0.0.1'); // 仅用于解析 query,host 无关
|
|
130
|
+
const refresh = url.searchParams.get('refresh') === '1';
|
|
131
|
+
const cfg = currentCfg();
|
|
132
|
+
|
|
133
|
+
if (refresh) {
|
|
134
|
+
// v2.9 多源编排:Gitea 恒启用 + GitHub 可选;全部启用源都失败才报错,
|
|
135
|
+
// 单源失败不拖垮其余源的数据回流(各源自落 status 文件)
|
|
136
|
+
const errors = [];
|
|
137
|
+
const githubOn = !!(cfg.github && cfg.github.enabled === true);
|
|
138
|
+
try {
|
|
139
|
+
await giteaClient.pollActivities(cfg);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
if (guardErrorResponse(res, err)) return; // 守卫类维持 400 快速失败(config 级错误,重试无意义)
|
|
142
|
+
errors.push(`gitea: ${redact((err && err.message) || String(err))}`);
|
|
143
|
+
}
|
|
144
|
+
if (githubOn) {
|
|
145
|
+
try {
|
|
146
|
+
await githubClient.pollActivities(cfg);
|
|
147
|
+
} catch (err) {
|
|
148
|
+
if (err && (err.code === 'HOST_NOT_ALLOWED' || err.code === 'DANGEROUS_WHITELIST_ENTRY')) {
|
|
149
|
+
return sendError(res, 400, err.code, redact((err && err.message) || String(err)));
|
|
150
|
+
}
|
|
151
|
+
errors.push(`github: ${redact((err && err.message) || String(err))}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// v2.9.18 Gitee 源:同款独立 try/catch——单源失败不拖垮其他源
|
|
155
|
+
const giteeOn = !!(cfg.gitee && cfg.gitee.enabled === true);
|
|
156
|
+
if (giteeOn) {
|
|
157
|
+
try {
|
|
158
|
+
await giteeClient.pollActivities(cfg);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
if (err && (err.code === 'HOST_NOT_ALLOWED' || err.code === 'DANGEROUS_WHITELIST_ENTRY')) {
|
|
161
|
+
return sendError(res, 400, err.code, redact((err && err.message) || String(err)));
|
|
162
|
+
}
|
|
163
|
+
errors.push(`gitee: ${redact((err && err.message) || String(err))}`);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const enabledSources = 1 + (githubOn ? 1 : 0) + (giteeOn ? 1 : 0);
|
|
167
|
+
if (errors.length > 0 && errors.length === enabledSources) {
|
|
168
|
+
return sendError(res, 502, 'GITEA_ERROR', errors.join('; '));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const tasks = loadTasks(cfg);
|
|
173
|
+
const out = loadActivities(cfg).map((a) => ({ ...a, matchedTaskIds: matchTaskIds(a, tasks) }));
|
|
174
|
+
return sendJson(res, 200, out);
|
|
175
|
+
} catch (err) {
|
|
176
|
+
return sendError(res, 500, 'INTERNAL_ERROR', redact((err && err.message) || String(err)));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* POST /api/gitea/test(§3):{"baseUrl"} 省略用 config 值。
|
|
182
|
+
* 白名单外 → 400 HOST_NOT_ALLOWED;否则 200 {ok,latencyMs,apiVersion}。
|
|
183
|
+
* @param {import('node:http').IncomingMessage} req
|
|
184
|
+
* @param {import('node:http').ServerResponse} res
|
|
185
|
+
*/
|
|
186
|
+
async function handleGiteaTest(req, res) {
|
|
187
|
+
try {
|
|
188
|
+
const cfg = currentCfg();
|
|
189
|
+
let body = {};
|
|
190
|
+
const raw = await readBody(req);
|
|
191
|
+
if (raw && raw.trim()) {
|
|
192
|
+
try {
|
|
193
|
+
body = JSON.parse(raw);
|
|
194
|
+
} catch {
|
|
195
|
+
return sendError(res, 400, 'INVALID_JSON', '请求体不是合法 JSON');
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) {
|
|
199
|
+
return sendError(res, 400, 'INVALID_JSON', '请求体必须是 JSON 对象');
|
|
200
|
+
}
|
|
201
|
+
const baseUrl = typeof body.baseUrl === 'string' && body.baseUrl.trim() ? body.baseUrl.trim() : cfg.gitea.baseUrl;
|
|
202
|
+
|
|
203
|
+
let result;
|
|
204
|
+
try {
|
|
205
|
+
result = await giteaClient.testConnection(baseUrl, cfg);
|
|
206
|
+
} catch (err) {
|
|
207
|
+
if (guardErrorResponse(res, err)) return;
|
|
208
|
+
return sendError(res, 502, 'GITEA_ERROR', redact((err && err.message) || String(err)));
|
|
209
|
+
}
|
|
210
|
+
const payload = { ok: result.ok, latencyMs: result.latencyMs, apiVersion: result.apiVersion };
|
|
211
|
+
if (!result.ok) payload.error = result.error;
|
|
212
|
+
return sendJson(res, 200, payload);
|
|
213
|
+
} catch (err) {
|
|
214
|
+
const code = err && err.code;
|
|
215
|
+
if (code === 'PAYLOAD_TOO_LARGE') return sendError(res, 400, 'INVALID_JSON', '请求体过大');
|
|
216
|
+
return sendError(res, 500, 'INTERNAL_ERROR', redact((err && err.message) || String(err)));
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* POST /api/sources/test(v2.9.19 设置页「测试连接」扩展到 GitHub/Gitee 源):
|
|
222
|
+
* {"source":"github"|"gitee", "org"?, "baseUrl"?}——org/baseUrl 缺省用 config 值(表单当前值可覆盖,
|
|
223
|
+
* 保存前先测)。探测只拉 events 第 1 页 per_page=1,与 enabled 开关无关、不改任何状态文件。
|
|
224
|
+
* 白名单/守卫类错误 → 400;其余失败 → 200 {ok:false,error}(测试语义不 5xx)。
|
|
225
|
+
*/
|
|
226
|
+
async function handleSourceTest(req, res) {
|
|
227
|
+
try {
|
|
228
|
+
let body = {};
|
|
229
|
+
const raw = await readBody(req);
|
|
230
|
+
if (raw && raw.trim()) {
|
|
231
|
+
try { body = JSON.parse(raw); } catch { return sendError(res, 400, 'INVALID_JSON', '请求体不是合法 JSON'); }
|
|
232
|
+
}
|
|
233
|
+
if (body === null || typeof body !== 'object' || Array.isArray(body)) {
|
|
234
|
+
return sendError(res, 400, 'INVALID_JSON', '请求体必须是 JSON 对象');
|
|
235
|
+
}
|
|
236
|
+
const source = String(body.source || '');
|
|
237
|
+
if (source !== 'github' && source !== 'gitee') {
|
|
238
|
+
return sendError(res, 400, 'SOURCE_INVALID', 'source 必须为 github/gitee');
|
|
239
|
+
}
|
|
240
|
+
const override = {};
|
|
241
|
+
for (const k of ['org', 'baseUrl']) {
|
|
242
|
+
if (body[k] !== undefined) {
|
|
243
|
+
if (typeof body[k] !== 'string') return sendError(res, 400, 'SETTINGS_INVALID', `github.${k} 必须为字符串`);
|
|
244
|
+
override[k] = body[k];
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const cfg = currentCfg();
|
|
248
|
+
const result = source === 'github'
|
|
249
|
+
? await githubClient.testConnection(cfg, override)
|
|
250
|
+
: await giteeClient.testConnection(cfg, override);
|
|
251
|
+
if (result.code === 'HOST_NOT_ALLOWED' || result.code === 'DANGEROUS_WHITELIST_ENTRY') {
|
|
252
|
+
return sendError(res, 400, result.code, result.error || '出站地址被守卫拒绝');
|
|
253
|
+
}
|
|
254
|
+
return sendJson(res, 200, result);
|
|
255
|
+
} catch (err) {
|
|
256
|
+
const code = err && err.code;
|
|
257
|
+
if (code === 'PAYLOAD_TOO_LARGE') return sendError(res, 400, 'INVALID_JSON', '请求体过大');
|
|
258
|
+
return sendError(res, 500, 'INTERNAL_ERROR', redact((err && err.message) || String(err)));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
module.exports = { handleActivities, handleGiteaTest, handleSourceTest, setConfigProvider, matchTaskIds };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* routes-setup.js — 环境体检聚合端点(分发部署自检;探测实现唯一见 lib/setupCheck.js)
|
|
5
|
+
* - GET /api/setup/check → {allOk, items:[{id,name,ok,detail,hint}], checkedAt}
|
|
6
|
+
* 五项(node/dws/dsh/llmRoute/gitea)互不阻塞(v2.2 起 cliGateway 项随网关集成移除);
|
|
7
|
+
* detail 过 redact,hint 为静态中文修复指引(前端直出);凭据永不回显。
|
|
8
|
+
* - GET /api/repos → 配置 Gitea 组织下全部仓库 [{fullName,name,private,updatedAt}]
|
|
9
|
+
* (只读:任务表单仓库下拉数据源;拉取实现唯一见 lib/giteaClient.js listOrgRepos)。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { sendJSON, sendError } = require('./router');
|
|
13
|
+
const { runSetupCheck } = require('./setupCheck');
|
|
14
|
+
const { redact } = require('./redact');
|
|
15
|
+
const { listOrgRepos } = require('./giteaClient');
|
|
16
|
+
|
|
17
|
+
function registerSetupRoutes(router, ctx) {
|
|
18
|
+
router.add('GET', '/api/setup/check', async (c) => {
|
|
19
|
+
sendJSON(c.res, 200, await runSetupCheck(ctx.config));
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
// GET /api/repos(只读端点:绝不写 data/ 任何文件、不写 config)
|
|
23
|
+
// 守卫码(HOST_NOT_ALLOWED/DANGEROUS_WHITELIST_ENTRY)→ 400;其余失败 → 502 GITEA_ERROR
|
|
24
|
+
//(message 必经 redact,映射风格与 routes-gitea.js guardErrorResponse 一致)
|
|
25
|
+
router.add('GET', '/api/repos', async (c) => {
|
|
26
|
+
try {
|
|
27
|
+
sendJSON(c.res, 200, await listOrgRepos(c.config || ctx.config));
|
|
28
|
+
} catch (err) {
|
|
29
|
+
if (err && err.code === 'HOST_NOT_ALLOWED') {
|
|
30
|
+
return sendError(c.res, 400, 'HOST_NOT_ALLOWED', err.message || 'host 不在白名单');
|
|
31
|
+
}
|
|
32
|
+
if (err && err.code === 'DANGEROUS_WHITELIST_ENTRY') {
|
|
33
|
+
return sendError(c.res, 400, 'HOST_NOT_ALLOWED', 'host 不在白名单(白名单配置含禁止地址)');
|
|
34
|
+
}
|
|
35
|
+
return sendError(c.res, 502, 'GITEA_ERROR', redact((err && err.message) || String(err)));
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = { registerSetupRoutes };
|