You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/public/cloud/ssh-keys.js

39 lines
1.6 KiB
JavaScript

const runtimeUrl = new URL('/cloud/runtime.js', location.origin);
const assetVersion = new URL(import.meta.url).searchParams.get('v');
if (assetVersion) runtimeUrl.searchParams.set('v', assetVersion);
const {
api, bindPageCleanup, byId, initializeSession, loadKeys, showError, toast,
} = await import(runtimeUrl.href);
export async function initialize() {
try {
if (!await initializeSession('keys')) return;
bindPageCleanup();
byId('ssh-key-form')?.addEventListener('submit', async event => {
event.preventDefault();
const button = byId('ssh-key-save-button');
button.disabled = true;
showError('ssh-key-form-error', '');
try {
await api('/api/v1/ssh-keys', { method: 'POST', body: {
name: byId('ssh-key-name').value.trim(),
public_key: byId('ssh-key-public-key').value.trim(),
} });
byId('ssh-key-form').reset();
await loadKeys();
toast('公钥已保存。', 'success');
} catch (error) { showError('ssh-key-form-error', error.message); }
finally { button.disabled = false; }
});
byId('ssh-key-file')?.addEventListener('change', async event => {
const file = event.target.files?.[0];
if (!file) return;
if (file.size > 16384) return showError('ssh-key-form-error', '公钥文件超过 16 KiB 限制。');
const content = await file.text();
if (/PRIVATE KEY/.test(content)) return showError('ssh-key-form-error', '不能导入私钥,只能导入 .pub 公钥。');
byId('ssh-key-public-key').value = content.trim();
});
await loadKeys();
} catch (error) { showError('cloud-page-error', error.message); }
}