From d1f1a5db7ab8f452b1faf1b4a8a593035db8e9b7 Mon Sep 17 00:00:00 2001 From: sgp Date: Wed, 4 Nov 2026 21:25:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(api):=20Key=20=E6=94=B9=E4=B8=BA=E6=98=8E?= =?UTF-8?q?=E6=96=87=E4=BF=9D=E5=AD=98=E5=8F=AF=E9=9A=8F=E6=97=B6=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=8F=A3=E4=BB=A4=E5=88=97?= =?UTF-8?q?=E4=B8=8E=E4=B8=80=E9=94=AE=E5=A4=8D=E5=88=B6=E6=8E=A5=E5=85=A5?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=EF=BC=88=E5=9C=B0=E5=9D=80+=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=80=85+=E5=8F=A3=E4=BB=A4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api-admin.html | 77 +++++++++++++++++++++++++++++++++++++++++++------- api/README.md | 4 ++- api/admin.js | 2 ++ api/server.js | 13 +++++++-- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/api-admin.html b/api-admin.html index 6040340..febd51b 100644 --- a/api-admin.html +++ b/api-admin.html @@ -129,7 +129,7 @@ button { font-family: inherit; cursor: pointer; } } .tbl-wrap { overflow-x: auto; } -table { width: 100%; border-collapse: collapse; min-width: 860px; } +table { width: 100%; border-collapse: collapse; min-width: 1000px; } th, td { padding: 11px 14px; text-align: left; vertical-align: middle; } th { font-size: 11px; font-weight: 500; color: var(--text-faint); @@ -142,6 +142,18 @@ tbody tr:hover { background: #fafbff; } .sub { margin-top: 2px; font-family: var(--mono); font-size: 10.5px; color: var(--text-faint); } .note { max-width: 200px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; } +.key-cell { white-space: nowrap; } +.key-val { + display: inline-block; margin-right: 6px; padding: 2px 6px; + font-family: var(--mono); font-size: 10.5px; color: #4b5563; + background: #f8fafc; border: 1px solid var(--border); border-radius: 6px; +} +.key-old { font-size: 11px; color: var(--text-faint); } +.tip { + margin: 12px 16px 0; padding: 9px 12px; + font-size: 11.5px; line-height: 1.6; color: #92400e; + background: #fffbeb; border: 1px solid #fde68a; border-radius: 8px; +} .pill { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; font-size: 11px; border-radius: 999px; white-space: nowrap; @@ -244,11 +256,13 @@ tbody tr:hover { background: #fafbff; } +
+ @@ -307,10 +321,11 @@ tbody tr:hover { background: #fafbff; }

✅ API Key 已生成

-
明文只显示这一次。关闭后无法再查看,只能重新生成。请立即复制并妥善保存。
+
内部使用:该口令已明文保存在服务器,可随时在本页复制。请勿发到公开群聊或写进前端代码。
@@ -324,6 +339,10 @@ tbody tr:hover { background: #fafbff; } var token = ''; var keys = []; var editingId = null; + var lastCreated = null; + + // 对外接入地址(复制给调用方用) + var API_ENDPOINT = 'https://drawlogo.zhuig.com/api/v1'; // 本地预览时直连本地 API;线上同源 var IS_LOCAL = /^(localhost|127\.0\.0\.1|\[::1\])$/.test(location.hostname); @@ -335,6 +354,18 @@ tbody tr:hover { background: #fafbff; } }); } + /** 口令掩码:前 10 + 后 6 */ + function maskKey(k) { + if (!k) return ''; + if (k.length <= 20) return k; + return k.slice(0, 10) + '…' + k.slice(-6); + } + + /** 一键复制用的接入信息(地址 + 使用者 + 口令) */ + function infoText(rec) { + return '画个标素材 API\n地址:' + API_ENDPOINT + '\n使用者:' + rec.name + '\n口令:' + rec.key; + } + function api(path, opts) { opts = opts || {}; opts.headers = Object.assign({ 'X-Admin-Token': token }, opts.headers || {}); @@ -422,6 +453,7 @@ tbody tr:hover { background: #fafbff; } return (k.name + ' ' + (k.note || '') + ' ' + k.id).toLowerCase().indexOf(q) !== -1; }); + $('#old-tip').hidden = !keys.some(function (k) { return !k.key; }); $('#empty').hidden = list.length > 0; $('#rows').innerHTML = list.map(function (k) { var expired = isExpired(k); @@ -430,6 +462,12 @@ tbody tr:hover { background: #fafbff; } : '启用中'; return '' + '' + + '' + '' + '' + '
使用者口令 备注 状态 限额
' + esc(k.name) + '
' + esc(k.id) + '
' + + (k.key + ? '' + esc(maskKey(k.key)) + '' + + '' + : '不可查看') + + '' + (k.note ? esc(k.note) : '—') + '' + pill + '' + k.ratePerMin + '/分
' + @@ -499,6 +537,7 @@ tbody tr:hover { background: #fafbff; } req.then(function (d) { closeForm(); if (!editingId && d && d.key) { + lastCreated = d.record; $('#key-for').textContent = '使用者:' + d.record.name + '(' + d.record.id + ')'; $('#key-value').textContent = d.key; $('#key-mask').hidden = false; @@ -545,18 +584,31 @@ tbody tr:hover { background: #fafbff; } $('#btn-save').addEventListener('click', save); $('#btn-close-key').addEventListener('click', function () { $('#key-mask').hidden = true; }); + $('#btn-copy').addEventListener('click', function () { - var text = $('#key-value').textContent; - var done = function () { - $('#btn-copy').textContent = '已复制 ✓'; - setTimeout(function () { $('#btn-copy').textContent = '复制 Key'; }, 1600); - }; + var btn = this; + copyText($('#key-value').textContent, function () { flash(btn, '已复制口令'); }); + }); + + $('#btn-copy-info').addEventListener('click', function () { + var btn = this; + if (!lastCreated || !lastCreated.key) return; + copyText(infoText(lastCreated), function () { flash(btn, '已复制接入信息'); }); + }); + + function copyText(text, done) { if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(text).then(done).catch(function () { fallbackCopy(text, done); }); } else { fallbackCopy(text, done); } - }); + } + + function flash(btn, text) { + var old = btn.textContent; + btn.textContent = text || '已复制'; + setTimeout(function () { btn.textContent = old; }, 1500); + } function fallbackCopy(text, done) { var ta = document.createElement('textarea'); @@ -577,6 +629,11 @@ tbody tr:hover { background: #fafbff; } if (!tr) return; var id = tr.getAttribute('data-id'); var act = btn.getAttribute('data-act'); + if (act === 'copy') { + var r = keys.filter(function (k) { return k.id === id; })[0]; + if (r && r.key) copyText(infoText(r), function () { flash(btn); }); + return; + } if (act === 'toggle') toggle(id); else if (act === 'del') remove(id); else if (act === 'edit') { diff --git a/api/README.md b/api/README.md index 5b90d52..212ca89 100644 --- a/api/README.md +++ b/api/README.md @@ -287,7 +287,9 @@ sudo systemctl restart drawlogo-api - 输入管理口令进入(口令 = 服务器 `config.json` 的 `adminToken`) - 可视化签发 Key:填写**使用者**、**备注**、**每分钟限流**、**每日配额**、**有效期** - 列表可查看今日用量、最近使用时间,支持**启用/禁用/编辑/删除** -- 明文 Key 只在签发时显示一次,页面提供一键复制 +- **内部使用模式**:Key 明文保存在 `data/apikeys.json`(权限 600),后台可随时查看掩码、点「复制」一键复制 **地址 + 使用者 + 口令**,直接发给调用方 +- 早期只存哈希的旧 Key 无法查看明文,重新生成一个即可(旧 Key 可删除) +- 若未来商业化,建议改回「仅存哈希 + 明文只显示一次」 > 管理口令与普通 API Key 完全分离;普通 Key 无法访问管理接口。口令为空时后台接口整体关闭。 diff --git a/api/admin.js b/api/admin.js index ca1889e..6cdc202 100644 --- a/api/admin.js +++ b/api/admin.js @@ -73,6 +73,8 @@ function create() { const rec = { id: 'key_' + crypto.randomBytes(4).toString('hex'), name, + // 内部使用:明文保存,便于后台查看/复制;hash 仅作兼容 + key: raw, hash: crypto.createHash('sha256').update(raw).digest('hex'), enabled: true, ratePerMin: Number(args.rate || CONFIG.defaultRatePerMin || 60), diff --git a/api/server.js b/api/server.js index 5a39434..122eb58 100644 --- a/api/server.js +++ b/api/server.js @@ -151,8 +151,14 @@ function authenticate(req) { return { ok: false, status: 401, code: 'UNAUTHORIZED', message: '缺少 API Key(请求头 X-API-Key 或 Authorization: Bearer )' }; } loadKeys(false); - const hash = sha256(raw.trim()); - const rec = keyState.keys.find(k => k.enabled !== false && k.hash && safeEqual(k.hash, hash)); + const trimmed = raw.trim(); + const hash = sha256(trimmed); + const rec = keyState.keys.find(k => { + if (k.enabled === false) return false; + // 新 Key 存明文,可直接比较;旧 Key 只有 hash,保持兼容 + if (k.key) return safeEqual(k.key, trimmed); + return k.hash && safeEqual(k.hash, hash); + }); if (!rec) { return { ok: false, status: 403, code: 'INVALID_KEY', message: 'API Key 无效或已禁用' }; } @@ -224,6 +230,8 @@ function publicKey(rec) { id: rec.id, name: rec.name, note: rec.note || '', + // 内部使用:明文保存,后台可随时查看/复制;旧 Key 无 key 字段 + key: rec.key || '', enabled: rec.enabled !== false, ratePerMin: rec.ratePerMin, dailyQuota: rec.dailyQuota, @@ -271,6 +279,7 @@ async function handleAdmin(req, res, pathname) { id: 'key_' + crypto.randomBytes(4).toString('hex'), name, note: String(body.note || '').trim(), + key: raw, hash: sha256(raw), enabled: true, ratePerMin: Math.max(1, Number(body.ratePerMin) || CONFIG.defaultRatePerMin),