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; }
|---|---|---|---|---|
' + 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 |