本次提交包含多项安全加固和体验优化: 1. 分离管理密钥与传输凭证,避免持 API Token 者伪造管理员票据 2. 钉钉回调地址增加白名单校验,防止开放重定向钓鱼 3. 初始管理员密码改为随机生成并日志记录,移除硬编码 123456 4. 新增登录防爆破计数表,支持 MySQL 多 worker 共享 5. 统一分页规范,新增 usePagedList composable 和 PagerBar 组件 6. 控制面 CORS 收敛为白名单,避免公网环境下任意站点跨域 7. 插件管理、凭证管理等列表接口支持分页和搜索 8. 强制初始密码修改,未改密前禁止使用管理功能 9. 定时任务触发被拒日志节流,避免授权失效时刷屏 10. 健康检查接口移除环境信息泄露,返回固定响应
201 lines
7.4 KiB
JavaScript
201 lines
7.4 KiB
JavaScript
// 与 FastAPI 后端交互,控制面前端直连 /api/*
|
||
import { store } from './store.js'
|
||
|
||
async function req(url, opts) {
|
||
const res = await fetch(url, opts)
|
||
if (!res.ok) {
|
||
let msg = `HTTP ${res.status}`
|
||
try {
|
||
const detail = (await res.json()).detail
|
||
// FastAPI 422 的 detail 是 [{loc,msg,type},…] 数组;普通错误是字符串/含 msg 的对象
|
||
if (typeof detail === 'string') msg = detail
|
||
else if (Array.isArray(detail)) {
|
||
msg = detail.map((d) => {
|
||
const where = Array.isArray(d.loc) ? d.loc.filter((p) => p !== 'body').join('.') : ''
|
||
return where ? `${where}: ${d.msg}` : (d.msg || JSON.stringify(d))
|
||
}).join(';')
|
||
} else if (detail && typeof detail === 'object') msg = detail.msg || JSON.stringify(detail)
|
||
} catch (_) { /* 非 JSON 响应,保留 HTTP 状态码提示 */ }
|
||
throw new Error(msg)
|
||
}
|
||
return res.json()
|
||
}
|
||
|
||
// 管理接口无状态鉴权:携带登录后签发的签名票据(X-Admin-Token),后端验票解得当前账号
|
||
function adminHeaders(extra = {}) {
|
||
return { 'X-Admin-Token': store.token || '', ...extra }
|
||
}
|
||
|
||
// 统一分页规范:所有列表接口 page(page≥1) + page_size,返回 {items, total}
|
||
function pagedQuery(params = {}) {
|
||
const qs = new URLSearchParams()
|
||
if (params.page) qs.set('page', params.page)
|
||
if (params.page_size) qs.set('page_size', params.page_size)
|
||
return qs.toString()
|
||
}
|
||
|
||
export async function adminLogin(account, password) {
|
||
return req('/api/admin/auth', {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ account, password })
|
||
})
|
||
}
|
||
export async function adminPassword(body) {
|
||
return req('/api/admin/password', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminMe() {
|
||
return req('/api/admin/me', { headers: adminHeaders() })
|
||
}
|
||
export async function adminOverview() {
|
||
return req('/api/admin/overview', { headers: adminHeaders() })
|
||
}
|
||
export async function adminGrantData() {
|
||
return req('/api/admin/grant_data', { headers: adminHeaders() })
|
||
}
|
||
export async function adminAccountGrants(account) {
|
||
return req('/api/admin/account_grants?account=' + encodeURIComponent(account), { headers: adminHeaders() })
|
||
}
|
||
export async function adminPlatforms() {
|
||
return req('/api/admin/platforms', { headers: adminHeaders() })
|
||
}
|
||
export async function adminGrant(body) {
|
||
return req('/api/admin/account_plugins', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminPlatformGrant(body) {
|
||
return req('/api/admin/account_platforms', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccounts(page = 1, pageSize = 20, q = '') {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (q) qs.set('q', q)
|
||
return req('/api/admin/accounts?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminPlugins(page = 1, pageSize = 20, q = '') {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (q) qs.set('q', q)
|
||
return req('/api/admin/plugins?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminAccountCreate(body) {
|
||
return req('/api/admin/account', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccountUpdate(body) {
|
||
return req('/api/admin/account/update', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccountReset(body) {
|
||
return req('/api/admin/account/reset_pwd', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAccountDelete(body) {
|
||
return req('/api/admin/account/delete', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAvatarUpload(account, file) {
|
||
const fd = new FormData()
|
||
fd.append('file', file)
|
||
const qs = new URLSearchParams({ account })
|
||
return req('/api/admin/avatar?' + qs.toString(), { method: 'POST', headers: adminHeaders(), body: fd })
|
||
}
|
||
export async function adminPluginDelete(body) {
|
||
return req('/api/admin/plugin/delete', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminPluginPatch(pluginId, body) {
|
||
const qs = new URLSearchParams({ plugin_id: pluginId })
|
||
return req('/api/admin/plugin/patch?' + qs.toString(), {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminAudit(page = 1, pageSize = 20, filters = {}) {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (filters.action) qs.set('action', filters.action)
|
||
if (filters.q) qs.set('q', filters.q)
|
||
return req('/api/admin/audit?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminReports(page = 1, pageSize = 20, filters = {}) {
|
||
const qs = new URLSearchParams()
|
||
qs.set('page', page); qs.set('page_size', pageSize)
|
||
if (filters.status) qs.set('status', filters.status)
|
||
if (filters.q) qs.set('q', filters.q)
|
||
return req('/api/admin/reports?' + qs.toString(), { headers: adminHeaders() })
|
||
}
|
||
export async function adminStats() {
|
||
return req('/api/admin/stats', { headers: adminHeaders() })
|
||
}
|
||
export async function adminCredsList(page = 1, pageSize = 20) {
|
||
return req('/api/admin/creds/manage?' + pagedQuery({ page, page_size: pageSize }), { headers: adminHeaders() })
|
||
}
|
||
export async function adminCredsSeed(body) {
|
||
return req('/api/admin/creds/seed', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminFileSources(category = '', page = 1, pageSize = 20) {
|
||
const parts = []
|
||
if (category) parts.push('category=' + encodeURIComponent(category))
|
||
const pq = pagedQuery({ page, page_size: pageSize })
|
||
if (pq) parts.push(pq)
|
||
const qs = parts.length ? '?' + parts.join('&') : ''
|
||
return req('/api/admin/file-sources' + qs, { headers: adminHeaders() })
|
||
}
|
||
export async function adminFileSourceCreate(body) {
|
||
return req('/api/admin/file-sources', {
|
||
method: 'POST',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminFileSourcePatch(id, body) {
|
||
return req(`/api/admin/file-sources/${id}`, {
|
||
method: 'PATCH',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' }),
|
||
body: JSON.stringify(body)
|
||
})
|
||
}
|
||
export async function adminFileSourceDelete(id) {
|
||
return req(`/api/admin/file-sources/${id}`, {
|
||
method: 'DELETE',
|
||
headers: adminHeaders({ 'Content-Type': 'application/json' })
|
||
})
|
||
}
|
||
export async function adminUpload(pluginId, name, version, file) {
|
||
const fd = new FormData()
|
||
fd.append('upload', file)
|
||
const qs = new URLSearchParams({ plugin_id: pluginId, name, version })
|
||
return req('/api/admin/pkg?' + qs.toString(), { method: 'POST', headers: adminHeaders(), body: fd })
|
||
} |