|
|
|
|
@ -9,6 +9,8 @@ const state = {
|
|
|
|
|
statusRequest: null, statusFailures: 0, detailRequest: null, detailId: null,
|
|
|
|
|
opener: null, scroll: null, networkRequest: null, networkOptionsRequest: null,
|
|
|
|
|
networkPollRequest: null, networkPollTimer: null, networkPollResolve: null, networkSerial: 0,
|
|
|
|
|
settingsRequest: null, settingsPollRequest: null, settingsPollTimer: null,
|
|
|
|
|
settingsPollResolve: null, settingsSerial: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function el(tag, className = '', text) {
|
|
|
|
|
@ -179,7 +181,7 @@ async function api(path, controller) {
|
|
|
|
|
|
|
|
|
|
async function mutate(path, body, controller) {
|
|
|
|
|
if (location.protocol !== 'https:' && !['localhost', '127.0.0.1', '[::1]'].includes(location.hostname)) {
|
|
|
|
|
throw new Error('请通过 HTTPS 修改实例网络配置。');
|
|
|
|
|
throw new Error('请通过 HTTPS 修改实例配置。');
|
|
|
|
|
}
|
|
|
|
|
const timeout = setTimeout(() => controller.abort('timeout'), 30000);
|
|
|
|
|
try {
|
|
|
|
|
@ -499,6 +501,209 @@ function cancelNetworkActivity(reason = 'cancelled') {
|
|
|
|
|
state.networkSerial += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setSettingsFeedback(section, message, tone = 'muted') {
|
|
|
|
|
const node = section?.querySelector('[data-settings-feedback]');
|
|
|
|
|
if (!node) return;
|
|
|
|
|
node.hidden = !message;
|
|
|
|
|
node.className = `admin-instance-settings-feedback text-${tone} fs-13 mb-2`;
|
|
|
|
|
node.textContent = message || '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cancelSettingsActivity(reason = 'cancelled') {
|
|
|
|
|
clearTimeout(state.settingsPollTimer);
|
|
|
|
|
state.settingsPollTimer = null;
|
|
|
|
|
state.settingsPollResolve?.(false);
|
|
|
|
|
state.settingsPollResolve = null;
|
|
|
|
|
state.settingsPollRequest?.abort(reason);
|
|
|
|
|
state.settingsRequest?.abort(reason);
|
|
|
|
|
state.settingsPollRequest = null;
|
|
|
|
|
state.settingsRequest = null;
|
|
|
|
|
state.settingsSerial += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function waitForSettingsPoll(ms, serial) {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
clearTimeout(state.settingsPollTimer);
|
|
|
|
|
state.settingsPollResolve = resolve;
|
|
|
|
|
state.settingsPollTimer = setTimeout(() => {
|
|
|
|
|
state.settingsPollTimer = null;
|
|
|
|
|
state.settingsPollResolve = null;
|
|
|
|
|
resolve(serial === state.settingsSerial);
|
|
|
|
|
}, ms);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function settingsJobState(job) {
|
|
|
|
|
return String(job?.status || job?.job?.status || '').toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function settingsStatusPath(jobId, statusUrl) {
|
|
|
|
|
return typeof statusUrl === 'string' && statusUrl.startsWith('/api/v1/')
|
|
|
|
|
? statusUrl : `/api/v1/jobs/${encodeURIComponent(jobId)}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function settingsFormValues(form) {
|
|
|
|
|
const hostname = String(form.elements.namedItem('hostname')?.value || '').trim().toLowerCase();
|
|
|
|
|
const password = String(form.elements.namedItem('root_password')?.value || '');
|
|
|
|
|
const confirmation = String(form.elements.namedItem('root_password_confirmation')?.value || '');
|
|
|
|
|
const publicKey = String(form.elements.namedItem('ssh_public_key')?.value || '').trim();
|
|
|
|
|
if (!hostname && !password && !publicKey) throw new Error('请至少填写一项要修改的实例设置。');
|
|
|
|
|
if (hostname && !/^(?=.{1,253}$)[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/.test(hostname)) {
|
|
|
|
|
throw new Error('主机名只能包含英文、数字、连字符和点,每段不能以连字符开头或结尾,最多 253 个字符。');
|
|
|
|
|
}
|
|
|
|
|
if (password || confirmation) {
|
|
|
|
|
if (!password) throw new Error('请输入新的实例密码。');
|
|
|
|
|
if (password !== confirmation) throw new Error('两次输入的实例密码不一致。');
|
|
|
|
|
if (password.length < 12 || password.length > 128 || !/^[\x21-\x7e]+$/.test(password)
|
|
|
|
|
|| !/[A-Z]/.test(password) || !/[a-z]/.test(password)
|
|
|
|
|
|| !/[0-9]/.test(password) || !/[^A-Za-z0-9]/.test(password)) {
|
|
|
|
|
throw new Error('实例密码需为 12 至 128 位可打印字符,并包含大写、小写、数字和符号。');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (publicKey) {
|
|
|
|
|
if (publicKey.length > 16384 || /[\r\n\x00-\x1f\x7f]/.test(publicKey)) {
|
|
|
|
|
throw new Error('SSH 公钥必须是一行且不能包含控制字符。');
|
|
|
|
|
}
|
|
|
|
|
if (!/^(?:ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp256|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521)\s+[A-Za-z0-9+/]+={0,3}(?:\s+[^\r\n]*)?$/.test(publicKey)) {
|
|
|
|
|
throw new Error('请输入有效的单行 OpenSSH 公钥,不要粘贴私钥。');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const body = { confirm_restart: true };
|
|
|
|
|
if (hostname) body.hostname = hostname;
|
|
|
|
|
if (password) body.root_password = password;
|
|
|
|
|
if (publicKey) body.ssh_public_key = publicKey;
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function settingsSetBusy(form, busy) {
|
|
|
|
|
for (const control of form.querySelectorAll('input, textarea, button')) {
|
|
|
|
|
control.disabled = busy;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unlockSettingsForm(form, activeJob = false) {
|
|
|
|
|
if (!form) return;
|
|
|
|
|
for (const control of form.querySelectorAll('input, textarea, button')) {
|
|
|
|
|
control.disabled = activeJob;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function pollSettingsJob(vm, section, jobId, statusUrl) {
|
|
|
|
|
const serial = state.settingsSerial;
|
|
|
|
|
const path = settingsStatusPath(jobId, statusUrl);
|
|
|
|
|
for (let attempt = 0; attempt < 150; attempt += 1) {
|
|
|
|
|
if (serial !== state.settingsSerial || state.detailId !== String(vm.id)) return;
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
state.settingsPollRequest = controller;
|
|
|
|
|
try {
|
|
|
|
|
const job = await api(path, controller);
|
|
|
|
|
if (serial !== state.settingsSerial || state.detailId !== String(vm.id)) return;
|
|
|
|
|
const status = settingsJobState(job);
|
|
|
|
|
const phase = job?.phase || job?.job?.phase;
|
|
|
|
|
if (['succeeded', 'completed', 'done'].includes(status)) {
|
|
|
|
|
setSettingsFeedback(section, `任务 #${jobId} 已完成,正在刷新实例设置…`, 'success');
|
|
|
|
|
detailNotice(`实例设置任务 #${jobId} 已完成,正在刷新详情。`);
|
|
|
|
|
state.settingsPollRequest = null;
|
|
|
|
|
clearTimeout(state.settingsPollTimer);
|
|
|
|
|
state.settingsPollTimer = null;
|
|
|
|
|
await refreshDetail(vm.id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (['failed', 'cancelled'].includes(status)) {
|
|
|
|
|
setSettingsFeedback(section, `任务 #${jobId}${phase ? `:${phase}` : '执行失败'},请修正设置后重试。`, 'danger');
|
|
|
|
|
section.dataset.settingsRetry = 'true';
|
|
|
|
|
unlockSettingsForm(section.querySelector('[data-instance-settings-form]'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (status === 'uncertain') {
|
|
|
|
|
setSettingsFeedback(section, `任务 #${jobId} 状态待核对,不能重复提交;请先在任务记录确认结果。`, 'warning');
|
|
|
|
|
unlockSettingsForm(section.querySelector('[data-instance-settings-form]'), true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setSettingsFeedback(section, `任务 #${jobId} ${jobStatus(status || 'queued')}${phase ? ` · ${phase}` : ''}…`, 'muted');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (controller.signal.aborted && controller.signal.reason !== 'timeout') return;
|
|
|
|
|
if (serial !== state.settingsSerial) return;
|
|
|
|
|
setSettingsFeedback(section, `${error.message},将继续等待任务结果。`, 'warning');
|
|
|
|
|
} finally {
|
|
|
|
|
if (state.settingsPollRequest === controller) state.settingsPollRequest = null;
|
|
|
|
|
}
|
|
|
|
|
if (!await waitForSettingsPoll(Math.min(10000, 2000 + attempt * 250), serial)) return;
|
|
|
|
|
}
|
|
|
|
|
setSettingsFeedback(section, `任务 #${jobId} 仍在执行,请稍后刷新任务记录;不要重复提交。`, 'warning');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function settingsEditor(vm) {
|
|
|
|
|
const template = byId('admin-instance-settings-controls');
|
|
|
|
|
const controls = template?.content?.cloneNode(true);
|
|
|
|
|
const section = controls?.querySelector('[data-instance-settings]');
|
|
|
|
|
if (!section) return el('section');
|
|
|
|
|
const form = section.querySelector('[data-instance-settings-form]');
|
|
|
|
|
const current = section.querySelector('[data-settings-current-hostname]');
|
|
|
|
|
if (current) current.textContent = `当前主机名:${text(vm.guest_hostname, vm.hostname, '未记录')}`;
|
|
|
|
|
if (!form) return section;
|
|
|
|
|
const activeJob = Boolean(vm.active_job && ['queued', 'running', 'uncertain'].includes(String(vm.active_job.status)));
|
|
|
|
|
if (activeJob) {
|
|
|
|
|
unlockSettingsForm(form, true);
|
|
|
|
|
setSettingsFeedback(section, `实例已有任务 #${vm.active_job.id}(${jobStatus(vm.active_job.status)}),完成前不能修改实例设置。`, 'warning');
|
|
|
|
|
}
|
|
|
|
|
const submit = async event => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (state.settingsRequest || (activeJob && section.dataset.settingsRetry !== 'true')) return;
|
|
|
|
|
let body;
|
|
|
|
|
try {
|
|
|
|
|
body = settingsFormValues(form);
|
|
|
|
|
if (!form.elements.namedItem('confirm_restart')?.checked) {
|
|
|
|
|
throw new Error('请先确认实例会重启。');
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
setSettingsFeedback(section, error.message, 'danger');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
state.settingsRequest = controller;
|
|
|
|
|
delete section.dataset.settingsRetry;
|
|
|
|
|
settingsSetBusy(form, true);
|
|
|
|
|
setSettingsFeedback(section, '正在创建实例设置任务,请勿重复提交…', 'muted');
|
|
|
|
|
try {
|
|
|
|
|
const result = await mutate(`/api/v1/admin/instances/${encodeURIComponent(vm.id)}/settings`, body, controller);
|
|
|
|
|
const jobId = result?.job_id ?? result?.job?.id;
|
|
|
|
|
const resultStatus = settingsJobState(result);
|
|
|
|
|
form.elements.namedItem('root_password').value = '';
|
|
|
|
|
form.elements.namedItem('root_password_confirmation').value = '';
|
|
|
|
|
form.elements.namedItem('ssh_public_key').value = '';
|
|
|
|
|
form.elements.namedItem('confirm_restart').checked = false;
|
|
|
|
|
if (!jobId && ['succeeded', 'completed', 'done'].includes(resultStatus)) {
|
|
|
|
|
setSettingsFeedback(section, '实例设置已应用,正在刷新详情…', 'success');
|
|
|
|
|
detailNotice('实例设置已应用,正在刷新详情。');
|
|
|
|
|
await refreshDetail(vm.id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!jobId) throw new Error('服务器未返回实例设置任务编号,请在任务记录中核对。');
|
|
|
|
|
setSettingsFeedback(section, `任务 #${jobId} 已创建,运行中的实例将在后台重启并应用设置…`, 'muted');
|
|
|
|
|
detailNotice(`实例设置任务 #${jobId} 已创建,正在等待执行结果。`);
|
|
|
|
|
void pollSettingsJob(vm, section, jobId, result?.status_url);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (controller.signal.aborted && controller.signal.reason !== 'timeout') return;
|
|
|
|
|
setSettingsFeedback(section, error.message, 'danger');
|
|
|
|
|
unlockSettingsForm(form);
|
|
|
|
|
} finally {
|
|
|
|
|
if (state.settingsRequest === controller) state.settingsRequest = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
form.addEventListener('submit', submit);
|
|
|
|
|
form.addEventListener('reset', () => {
|
|
|
|
|
window.setTimeout(() => setSettingsFeedback(section, ''), 0);
|
|
|
|
|
});
|
|
|
|
|
for (const control of form.querySelectorAll('input, textarea')) {
|
|
|
|
|
control.addEventListener('input', () => {
|
|
|
|
|
const feedback = section.querySelector('[data-settings-feedback]');
|
|
|
|
|
if (feedback && feedback.classList.contains('text-danger')) setSettingsFeedback(section, '');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return section;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function networkJobState(job) {
|
|
|
|
|
return String(job?.status || job?.job?.status || '').toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
@ -643,11 +848,12 @@ async function refreshDetail(id) {
|
|
|
|
|
state.detailRequest?.abort('refresh');
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
state.detailRequest = controller;
|
|
|
|
|
detailNotice('正在刷新网络配置…');
|
|
|
|
|
detailNotice('正在刷新实例详情…');
|
|
|
|
|
try {
|
|
|
|
|
const payload = await api(`/api/v1/admin/instances/${encodeURIComponent(id)}`, controller);
|
|
|
|
|
if (controller.signal.aborted || state.detailId !== String(id)) return;
|
|
|
|
|
cancelNetworkActivity('detail-refresh');
|
|
|
|
|
cancelSettingsActivity('detail-refresh');
|
|
|
|
|
renderDetail(payload);
|
|
|
|
|
detailNotice('');
|
|
|
|
|
const vm = payload.instance;
|
|
|
|
|
@ -664,7 +870,7 @@ async function refreshDetail(id) {
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (controller.signal.aborted && controller.signal.reason !== 'timeout') return;
|
|
|
|
|
if (state.detailId === String(id)) detailNotice(`${error.message} 当前网络任务结果请在任务记录中核对。`, true);
|
|
|
|
|
if (state.detailId === String(id)) detailNotice(`${error.message} 当前任务结果请在任务记录中核对。`, true);
|
|
|
|
|
} finally {
|
|
|
|
|
if (state.detailRequest === controller) state.detailRequest = null;
|
|
|
|
|
}
|
|
|
|
|
@ -883,7 +1089,7 @@ function renderDetail(payload) {
|
|
|
|
|
['DNS', Array.isArray(vm.dns_servers) ? text(vm.dns_servers.join('、'), '未记录') : text(vm.dns_servers, '未记录')],
|
|
|
|
|
['带宽上限', vm.bandwidth_mbps ? `${vm.bandwidth_mbps} Mbps` : '未记录'],
|
|
|
|
|
['创建时间', date(vm.created_at)],
|
|
|
|
|
]), networkEditor(vm));
|
|
|
|
|
]), networkEditor(vm), settingsEditor(vm));
|
|
|
|
|
byId('instance-panel-service').replaceChildren(infoList([
|
|
|
|
|
['套餐', text(vm.plan_name, '无关联套餐')], ['服务状态', vm.display?.service], ['网络状态', vm.display?.network],
|
|
|
|
|
['服务到期', vm.service_status === 'exempt' ? '免计费' : date(vm.service_expires_at)],
|
|
|
|
|
@ -944,6 +1150,7 @@ function closeDetail() {
|
|
|
|
|
state.detailRequest?.abort();
|
|
|
|
|
state.detailRequest = null;
|
|
|
|
|
cancelNetworkActivity('closed');
|
|
|
|
|
cancelSettingsActivity('closed');
|
|
|
|
|
state.detailId = null;
|
|
|
|
|
const dialog = byId('instance-detail');
|
|
|
|
|
if (dialog.open) dialog.close();
|
|
|
|
|
@ -961,6 +1168,7 @@ async function openDetail(id, opener) {
|
|
|
|
|
if (!/^[1-9][0-9]*$/.test(id)) return;
|
|
|
|
|
state.detailRequest?.abort();
|
|
|
|
|
cancelNetworkActivity('detail-switch');
|
|
|
|
|
cancelSettingsActivity('detail-switch');
|
|
|
|
|
state.detailId = id;
|
|
|
|
|
state.opener = opener;
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
@ -1056,7 +1264,7 @@ if (root) {
|
|
|
|
|
// Native modal dialog traps focus; background scrolling is locked separately for iOS.
|
|
|
|
|
window.addEventListener('popstate', () => { closeDetail(); void loadPage(new URL(location.href), 'none'); });
|
|
|
|
|
document.addEventListener('visibilitychange', () => { if (document.hidden) stopStatuses(); else void refreshStatuses(); });
|
|
|
|
|
window.addEventListener('pagehide', () => { state.active = false; stopStatuses(); state.request?.abort(); state.detailRequest?.abort(); cancelNetworkActivity('pagehide'); });
|
|
|
|
|
window.addEventListener('pagehide', () => { state.active = false; stopStatuses(); state.request?.abort(); state.detailRequest?.abort(); cancelNetworkActivity('pagehide'); cancelSettingsActivity('pagehide'); });
|
|
|
|
|
window.addEventListener('pageshow', event => { state.active = true; if (event.persisted) { closeDetail(); void loadPage(new URL(location.href), 'none'); } });
|
|
|
|
|
if (bootstrapped) void refreshStatuses();
|
|
|
|
|
else void loadPage(new URL(location.href), 'none');
|
|
|
|
|
|