chore: 恢复 demo 演示与设计文档入库

This commit is contained in:
2026-11-21 17:21:40 +08:00
parent 2908db1037
commit e6fe82b0c1
10 changed files with 2477 additions and 4 deletions
+210
View File
@@ -0,0 +1,210 @@
/**
* 操作审计 · 模拟数据生成器(种子随机,确定性输出)
*
* 记录字段与产品设计 4.1 表格一一对应;resourceType/resourceName 通过
* AuditCore.resolveResource 从路径解析得出(模拟真实捕获管线)。
* 内置若干"故事线"数据:登录爆破、越权 403、服务端 500 等,便于验证筛选场景。
*/
(function (root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory(require('./audit-core.js'));
} else {
root.MockData = factory(root.AuditCore);
}
})(typeof self !== 'undefined' ? self : this, function (AuditCore) {
'use strict';
var DAY = 86400000;
var MIN = 60000;
var BASE_COUNT = 128; // 随机生成的基础记录条数
// ---------- 种子随机 ----------
function mulberry32(seed) {
var a = seed >>> 0;
return function () {
a |= 0;
a = (a + 0x6D2B79F5) | 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function rint(r, min, max) {
return Math.floor(r() * (max - min + 1)) + min;
}
function pick(r, arr) {
return arr[rint(r, 0, arr.length - 1)];
}
function hex(r, n) {
var s = '';
var chars = '0123456789abcdef';
for (var i = 0; i < n; i++) s += chars[rint(r, 0, 15)];
return s;
}
// ---------- 基础素材 ----------
var UAS = {
chrome: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.2151.97 Safari/537.36',
edge: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.2151.97 Safari/537.36 Edg/119.0.2151.97',
firefox: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0',
safari: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15',
bot: 'python-requests/2.31.0'
};
var USERS = {
'guichumo': { ips: ['192.168.1.23', '116.24.65.187'], uas: ['chrome', 'edge'] },
'editor.zhang': { ips: ['116.24.65.201'], uas: ['edge', 'firefox'] },
'writer.li': { ips: ['120.79.10.45'], uas: ['safari', 'chrome'] }
};
var SLUGS = ['hello-halo', 'self-host-guide', 'weekly-2026-w46', 'reading-notes-11', 'plugin-dev-diary', 'spring-festival-2026'];
var PLUGINS = ['plugin-search-widget', 'plugin-comment-widget', 'plugin-sitemap', 'plugin-links'];
function postPath(r) { return '/apis/content.halo.run/v1alpha1/posts/' + pick(r, SLUGS); }
function publishPath(r) { return '/apis/api.content.halo.run/v1alpha1/posts/' + pick(r, SLUGS) + '/publish'; }
function commentPath(r) { return '/apis/content.halo.run/v1alpha1/comments/comment-' + hex(r, 6); }
function approvePath(r) { return '/apis/api.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/approve'; }
function replyPath(r) { return '/apis/api.halo.run/v1alpha1/comments/comment-' + hex(r, 6) + '/reply'; }
function attachPath(r) { return '/apis/storage.halo.run/v1alpha1/attachments/attachment-' + hex(r, 6); }
// [权重, 操作者, 方法, 路径函数, 状态码]
var ACTIONS = [
[16, 'guichumo', 'PUT', postPath, 200],
[10, 'guichumo', 'POST', publishPath, 200],
[6, 'guichumo', 'POST', function () { return '/apis/content.halo.run/v1alpha1/posts'; }, 201],
[5, 'guichumo', 'PUT', function () { return '/apis/v1alpha1/configmaps/system'; }, 200],
[4, 'guichumo', 'PUT', function (r) { return '/apis/plugin.halo.run/v1alpha1/plugins/' + pick(r, PLUGINS); }, 200],
[4, 'guichumo', 'POST', function () { return '/apis/storage.halo.run/v1alpha1/attachments'; }, 201],
[4, 'guichumo', 'DELETE', attachPath, 204],
[3, 'guichumo', 'DELETE', commentPath, 204],
[3, 'guichumo', 'PUT', function () { return '/apis/v1alpha1/roles/role-template-editor'; }, 200],
[2, 'guichumo', 'POST', function () { return '/login'; }, 200],
[2, 'guichumo', 'POST', function () { return '/logout'; }, 200],
[12, 'editor.zhang', 'PUT', postPath, 200],
[6, 'editor.zhang', 'POST', function () { return '/apis/content.halo.run/v1alpha1/posts'; }, 201],
[2, 'editor.zhang', 'POST', function () { return '/login'; }, 200],
[2, 'editor.zhang', 'POST', function () { return '/apis/storage.halo.run/v1alpha1/attachments'; }, 201],
[8, 'writer.li', 'PUT', postPath, 200],
[6, 'writer.li', 'POST', approvePath, 200],
[3, 'writer.li', 'POST', replyPath, 200],
[2, 'writer.li', 'POST', function () { return '/login'; }, 200]
];
var TOTAL_WEIGHT = ACTIONS.reduce(function (sum, a) { return sum + a[0]; }, 0);
function userCtx(r, name) {
var u = USERS[name];
return {
username: name,
remoteIp: pick(r, u.ips),
userAgent: UAS[pick(r, u.uas)]
};
}
function rec(timestamp, username, method, path, statusCode, remoteIp, userAgent) {
var res = AuditCore.resolveResource(path);
return {
id: null,
timestamp: timestamp,
username: username,
method: AuditCore.normalizeMethod(method),
path: path,
resourceType: res.resourceType,
resourceName: res.resourceName,
statusCode: statusCode,
remoteIp: remoteIp,
userAgent: userAgent
};
}
/**
* 生成模拟审计日志(按时间倒序,id 从旧到新递增)
* @param {number} [now] 当前时间戳(默认 Date.now()),测试传固定值保证确定性
* @returns {Array}
*/
function generateLogs(now) {
if (typeof now !== 'number') now = Date.now();
var r = mulberry32(20261121);
var logs = [];
// 基础记录:均匀散布在近 90 天
for (var i = 0; i < BASE_COUNT; i++) {
var roll = r() * TOTAL_WEIGHT;
var action = ACTIONS[0];
for (var j = 0; j < ACTIONS.length; j++) {
roll -= ACTIONS[j][0];
if (roll <= 0) { action = ACTIONS[j]; break; }
}
var ctx = userCtx(r, action[1]);
var time = now - rint(r, 5 * MIN, 90 * DAY);
logs.push(rec(
new Date(time).toISOString(), ctx.username,
action[2], action[3](r), action[4],
ctx.remoteIp, ctx.userAgent
));
}
// ---- 故事线(固定时间锚点)----
// 1. 攻击者爆破登录:20 天前,同一 IP 6 次失败,UA 为 python-requests
var burstBase = now - 20 * DAY - 3600 * 1000 * 3; // 约 20 天前的凌晨
[0, 41, 88, 135, 210, 400].forEach(function (off) {
logs.push(rec(
new Date(burstBase + off * 1000).toISOString(), '未知用户',
'POST', '/login', 401, '203.0.113.66', UAS.bot
));
});
// 2. 站长忘记密码:12 天前输错两次后登录成功
var b2 = now - 12 * DAY - 3600 * 1000 * 10;
[[0, 401], [97, 401], [180, 200]].forEach(function (pair) {
var ctx = { remoteIp: '192.168.1.23', userAgent: UAS.chrome };
logs.push(rec(
new Date(b2 + pair[0] * 1000).toISOString(),
pair[1] === 200 ? 'guichumo' : '未知用户',
'POST', '/login', pair[1], ctx.remoteIp, ctx.userAgent
));
});
// 3. 越权尝试:8 天前 editor.zhang 尝试修改系统设置被拒绝
logs.push(rec(
new Date(now - 8 * DAY - 3600 * 1000 * 14).toISOString(), 'editor.zhang',
'PUT', '/apis/v1alpha1/configmaps/system', 403, '116.24.65.201', UAS.edge
));
// 4. 权限不足:5 天前 writer.li 尝试删除评论被拒绝
logs.push(rec(
new Date(now - 5 * DAY - 3600 * 1000 * 9).toISOString(), 'writer.li',
'DELETE', '/apis/content.halo.run/v1alpha1/comments/comment-a3f802', 403, '120.79.10.45', UAS.safari
));
// 5. 资源不存在:3 天前删除一个已不存在的附件
logs.push(rec(
new Date(now - 3 * DAY - 3600 * 1000 * 20).toISOString(), 'guichumo',
'DELETE', '/apis/storage.halo.run/v1alpha1/attachments/attachment-dead42', 404, '192.168.1.23', UAS.chrome
));
// 6. 服务端异常:2 天前发布文章返回 500
logs.push(rec(
new Date(now - 2 * DAY - 3600 * 1000 * 6).toISOString(), 'writer.li',
'POST', '/apis/api.content.halo.run/v1alpha1/posts/self-host-guide/publish', 500, '120.79.10.45', UAS.chrome
));
// 7. 误操作定位:1 天前删除了一篇文章
logs.push(rec(
new Date(now - DAY - 3600 * 1000 * 2).toISOString(), 'guichumo',
'DELETE', '/apis/content.halo.run/v1alpha1/posts/weekly-2026-w45', 204, '192.168.1.23', UAS.chrome
));
// 排序 + 分配 id(最新记录 id 数字最大)
logs.sort(function (a, b) {
return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
});
logs.forEach(function (l, idx) {
l.id = 'audit-' + String(logs.length - idx).padStart(4, '0');
});
return logs;
}
return { generateLogs: generateLogs };
});