Bind stove API keys to configured client IP

This commit is contained in:
anor
2026-11-27 03:26:42 +10:00
parent 086a993e8a
commit be5a29277a
13 changed files with 235 additions and 25 deletions
+11 -3
View File
@@ -13,6 +13,7 @@ export function StoveApiPage() {
const [error, setError] = useState('');
const [name, setName] = useState('');
const [note, setNote] = useState('');
const [allowedIp, setAllowedIp] = useState('');
const [secret, setSecret] = useState('');
const [selected, setSelected] = useState(null);
const load = () => api(base).then((d) => { setData(d); setError(''); })
@@ -25,9 +26,9 @@ export function StoveApiPage() {
const create = async (e) => {
e.preventDefault();
try {
const made = await api(base + '/keys', {method: 'POST', body: JSON.stringify({name, note})});
const made = await api(base + '/keys', {method: 'POST', body: JSON.stringify({name, note, allowed_ip: allowedIp.trim()})});
setSecret(made.secret);
setName(''); setNote(''); await load();
setName(''); setNote(''); setAllowedIp(''); await load();
} catch (err) { setError(err.message || 'Ключ не создан'); }
};
const show = async (row) => {
@@ -47,6 +48,8 @@ export function StoveApiPage() {
<h2>Выдать ключ</h2>
<form onSubmit=${create}>
<label>Название <input value=${name} onInput=${(e) => setName(e.target.value)} required maxLength="100" /></label>
<label>IP сервера или CIDR <input value=${allowedIp} onInput=${(e) => setAllowedIp(e.target.value)} required placeholder="203.0.113.7" /></label>
<p>Укажите внешний IP сервера сайта. С этого адреса ключ сможет вызывать API.</p>
<label>Заметка <input value=${note} onInput=${(e) => setNote(e.target.value)} maxLength="500" /></label>
<button type="submit">Создать ключ</button>
</form>
@@ -58,10 +61,15 @@ export function StoveApiPage() {
<h2>Ключи</h2>
<${Table} columns=${[
{key: 'name', label: 'Название'}, {key: 'key_prefix', label: 'Префикс'},
{key: 'allowed_ip', label: 'IP', render: (r) => r.allowed_ip || 'не задан — доступ закрыт'},
{key: 'calls_24h', label: 'Вызовов/сут'}, {key: 'limit_day', label: 'Лимит/сут'},
{key: 'ai_budget_rub_day', label: 'ИИ ₽/сут'},
{key: 'status', label: 'Статус', render: (r) => r.revoked_at ? 'отозван' : r.disabled ? 'отключён' : 'работает'},
{key: 'status', label: 'Статус', render: (r) => r.revoked_at ? 'отозван' : r.disabled ? 'отключён' : !r.allowed_ip ? 'адрес не задан' : 'работает'},
{key: 'actions', label: 'Действия', render: (r) => r.revoked_at ? '' : html`
<button type="button" onClick=${() => {
const value = prompt('Разрешённый IP сервера или CIDR', r.allowed_ip || '');
if (value && value.trim()) act(base + '/keys/' + r.id + '/allowed-ip', {method: 'PATCH', body: JSON.stringify({allowed_ip: value.trim()})});
}}>IP</button>
<button type="button" onClick=${() => act(base + '/keys/' + r.id + '/disabled?disabled=' + (!r.disabled), {method: 'POST'})}>${r.disabled ? 'Включить' : 'Отключить'}</button>
<button type="button" onClick=${() => {
const limit = Number(prompt('Новый лимит вызовов за сутки', String(r.limit_day)));