|
|
|
|
|
const root = document.querySelector('[data-platform-test-policy]');
|
|
|
|
|
|
|
|
|
|
|
|
if (root) {
|
|
|
|
|
|
const state = {
|
|
|
|
|
|
policy: window.PLATFORM_TEST_POLICY_BOOTSTRAP || { revision: 0, rules: [] },
|
|
|
|
|
|
canManage: root.dataset.canManage === '1',
|
|
|
|
|
|
busy: false,
|
|
|
|
|
|
frozen: false,
|
|
|
|
|
|
editing: '',
|
|
|
|
|
|
};
|
|
|
|
|
|
const body = document.getElementById('platform-test-policy-body');
|
|
|
|
|
|
const form = document.getElementById('platform-test-policy-form');
|
|
|
|
|
|
const message = document.getElementById('platform-test-policy-message');
|
|
|
|
|
|
const revision = document.getElementById('platform-test-policy-revision');
|
|
|
|
|
|
const available = document.getElementById('platform-test-policy-available');
|
|
|
|
|
|
const fields = {
|
|
|
|
|
|
id: document.getElementById('platform-test-policy-rule-id'),
|
|
|
|
|
|
service: document.getElementById('platform-test-service'),
|
|
|
|
|
|
protocol: document.getElementById('platform-test-protocol'),
|
|
|
|
|
|
port: document.getElementById('platform-test-port'),
|
|
|
|
|
|
direction: document.getElementById('platform-test-direction'),
|
|
|
|
|
|
reason: document.getElementById('platform-test-reason'),
|
|
|
|
|
|
submit: document.getElementById('platform-test-submit'),
|
|
|
|
|
|
cancel: document.getElementById('platform-test-cancel'),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function showMessage(text, kind = 'danger') {
|
|
|
|
|
|
if (!message) return;
|
|
|
|
|
|
message.textContent = text;
|
|
|
|
|
|
message.className = `alert alert-${kind}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function clearMessage() {
|
|
|
|
|
|
if (!message) return;
|
|
|
|
|
|
message.textContent = '';
|
|
|
|
|
|
message.className = 'alert d-none';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setBusy(value) {
|
|
|
|
|
|
state.busy = value;
|
|
|
|
|
|
root.querySelectorAll('button, input, select').forEach((element) => {
|
|
|
|
|
|
element.disabled = Boolean(value || state.frozen);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function cell(row, value, className = '') {
|
|
|
|
|
|
const node = document.createElement('td');
|
|
|
|
|
|
if (className) node.className = className;
|
|
|
|
|
|
const text = document.createTextNode(String(value ?? ''));
|
|
|
|
|
|
if (className.includes('platform-test-service')) {
|
|
|
|
|
|
const strong = document.createElement('strong');
|
|
|
|
|
|
strong.appendChild(text);
|
|
|
|
|
|
node.appendChild(strong);
|
|
|
|
|
|
} else if (className.includes('platform-test-port')) {
|
|
|
|
|
|
const code = document.createElement('code');
|
|
|
|
|
|
code.appendChild(text);
|
|
|
|
|
|
node.appendChild(code);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
node.appendChild(text);
|
|
|
|
|
|
}
|
|
|
|
|
|
row.appendChild(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function actionButton(label, action, className) {
|
|
|
|
|
|
const button = document.createElement('button');
|
|
|
|
|
|
button.type = 'button';
|
|
|
|
|
|
button.textContent = label;
|
|
|
|
|
|
button.dataset.platformTestAction = action;
|
|
|
|
|
|
button.className = className;
|
|
|
|
|
|
button.disabled = state.busy || state.frozen;
|
|
|
|
|
|
return button;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
|
|
if (!body) return;
|
|
|
|
|
|
body.replaceChildren();
|
|
|
|
|
|
const rules = Array.isArray(state.policy.rules) ? state.policy.rules : [];
|
|
|
|
|
|
if (rules.length === 0) {
|
|
|
|
|
|
const row = document.createElement('tr');
|
|
|
|
|
|
row.dataset.platformTestEmpty = '';
|
|
|
|
|
|
const empty = document.createElement('td');
|
|
|
|
|
|
empty.colSpan = state.canManage ? 6 : 5;
|
|
|
|
|
|
empty.className = 'text-muted text-center py-4';
|
|
|
|
|
|
empty.textContent = '当前没有手动测试配置。';
|
|
|
|
|
|
row.appendChild(empty);
|
|
|
|
|
|
body.appendChild(row);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
rules.forEach((rule) => {
|
|
|
|
|
|
const row = document.createElement('tr');
|
|
|
|
|
|
row.dataset.ruleId = String(rule.id || '');
|
|
|
|
|
|
cell(row, rule.service, 'platform-test-service');
|
|
|
|
|
|
cell(row, rule.protocol_label);
|
|
|
|
|
|
cell(row, rule.port, 'platform-test-port');
|
|
|
|
|
|
cell(row, rule.direction_label);
|
|
|
|
|
|
cell(row, rule.reason, 'text-muted');
|
|
|
|
|
|
if (state.canManage) {
|
|
|
|
|
|
const actions = document.createElement('td');
|
|
|
|
|
|
const group = document.createElement('div');
|
|
|
|
|
|
group.className = 'd-flex gap-2';
|
|
|
|
|
|
group.appendChild(actionButton('编辑', 'edit', 'btn btn-sm btn-light'));
|
|
|
|
|
|
group.appendChild(actionButton('删除', 'delete', 'btn btn-sm btn-outline-danger'));
|
|
|
|
|
|
actions.appendChild(group);
|
|
|
|
|
|
row.appendChild(actions);
|
|
|
|
|
|
}
|
|
|
|
|
|
body.appendChild(row);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (revision) revision.textContent = `目录版本 ${Number(state.policy.revision || 0)}`;
|
|
|
|
|
|
if (available) available.textContent = String(state.policy.available_expanded_rules ?? '—');
|
|
|
|
|
|
setBusy(state.busy);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resetForm() {
|
|
|
|
|
|
state.editing = '';
|
|
|
|
|
|
if (!form) return;
|
|
|
|
|
|
form.reset();
|
|
|
|
|
|
if (fields.id) fields.id.value = '';
|
|
|
|
|
|
if (fields.submit) fields.submit.textContent = '增加';
|
|
|
|
|
|
if (fields.cancel) fields.cancel.classList.add('d-none');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function edit(rule) {
|
|
|
|
|
|
if (!form || !rule || state.busy || state.frozen) return;
|
|
|
|
|
|
state.editing = String(rule.id || '');
|
|
|
|
|
|
fields.id.value = state.editing;
|
|
|
|
|
|
fields.service.value = String(rule.service || '');
|
|
|
|
|
|
fields.protocol.value = String(rule.protocol || 'tcp');
|
|
|
|
|
|
fields.port.value = String(rule.port || '').replace('–', '-');
|
|
|
|
|
|
fields.direction.value = String(rule.direction || 'ingress');
|
|
|
|
|
|
fields.reason.value = String(rule.reason || '');
|
|
|
|
|
|
fields.submit.textContent = '保存';
|
|
|
|
|
|
fields.cancel.classList.remove('d-none');
|
|
|
|
|
|
fields.service.focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function requireSecureTransport() {
|
|
|
|
|
|
if (location.protocol === 'https:'
|
|
|
|
|
|
|| ['localhost', '127.0.0.1', '[::1]'].includes(location.hostname)) return;
|
|
|
|
|
|
throw new Error('请通过 HTTPS 修改平台测试策略。');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function api(path, options = {}) {
|
|
|
|
|
|
requireSecureTransport();
|
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
|
const timer = window.setTimeout(() => controller.abort(), 15000);
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await fetch(path, {
|
|
|
|
|
|
method: options.method || 'GET',
|
|
|
|
|
|
credentials: 'same-origin',
|
|
|
|
|
|
cache: 'no-store',
|
|
|
|
|
|
signal: controller.signal,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
'X-CSRF-Token': window.ADMIN_CSRF_TOKEN || '',
|
|
|
|
|
|
},
|
|
|
|
|
|
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
|
|
|
|
});
|
|
|
|
|
|
let payload = null;
|
|
|
|
|
|
try { payload = await response.json(); } catch (_error) { /* handled below */ }
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
throw new Error(payload?.detail || payload?.error || `请求失败(HTTP ${response.status})`);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!payload || !payload.test_policy) {
|
|
|
|
|
|
throw new Error('服务返回的数据格式无效,请刷新页面。');
|
|
|
|
|
|
}
|
|
|
|
|
|
return payload.test_policy;
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
window.clearTimeout(timer);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function mutate(path, method, payload, success) {
|
|
|
|
|
|
if (state.busy || state.frozen) return;
|
|
|
|
|
|
clearMessage();
|
|
|
|
|
|
setBusy(true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
state.policy = await api(path, { method, body: payload });
|
|
|
|
|
|
resetForm();
|
|
|
|
|
|
showMessage(success, 'success');
|
|
|
|
|
|
render();
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
if (error?.name === 'AbortError') {
|
|
|
|
|
|
state.frozen = true;
|
|
|
|
|
|
showMessage('请求结果暂时未知。为避免重复修改,本页已停止操作,请刷新后核对目录版本。', 'warning');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
showMessage(error instanceof Error ? error.message : '操作失败,请重试。');
|
|
|
|
|
|
}
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setBusy(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (form && state.canManage) {
|
|
|
|
|
|
form.addEventListener('submit', (event) => {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
const id = state.editing;
|
|
|
|
|
|
const payload = {
|
|
|
|
|
|
expected_revision: Number(state.policy.revision || 0),
|
|
|
|
|
|
service: fields.service.value.trim(),
|
|
|
|
|
|
protocol: fields.protocol.value,
|
|
|
|
|
|
port: fields.port.value.trim(),
|
|
|
|
|
|
direction: fields.direction.value,
|
|
|
|
|
|
reason: fields.reason.value.trim(),
|
|
|
|
|
|
};
|
|
|
|
|
|
mutate(
|
|
|
|
|
|
id ? `/api/v1/admin/platform-test-policy/rules/${encodeURIComponent(id)}`
|
|
|
|
|
|
: '/api/v1/admin/platform-test-policy/rules',
|
|
|
|
|
|
id ? 'PUT' : 'POST', payload,
|
|
|
|
|
|
id ? '测试规则已更新;只影响之后进入开通队列的新实例。'
|
|
|
|
|
|
: '测试规则已增加;只影响之后进入开通队列的新实例。',
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
fields.cancel?.addEventListener('click', () => {
|
|
|
|
|
|
resetForm();
|
|
|
|
|
|
clearMessage();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
body?.addEventListener('click', (event) => {
|
|
|
|
|
|
const button = event.target.closest('[data-platform-test-action]');
|
|
|
|
|
|
const row = button?.closest('[data-rule-id]');
|
|
|
|
|
|
if (!button || !row || !state.canManage || state.busy || state.frozen) return;
|
|
|
|
|
|
const rule = (state.policy.rules || []).find((item) => String(item.id) === row.dataset.ruleId);
|
|
|
|
|
|
if (!rule) return;
|
|
|
|
|
|
if (button.dataset.platformTestAction === 'edit') {
|
|
|
|
|
|
edit(rule);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
mutate(
|
|
|
|
|
|
`/api/v1/admin/platform-test-policy/rules/${encodeURIComponent(rule.id)}`,
|
|
|
|
|
|
'DELETE', { expected_revision: Number(state.policy.revision || 0) },
|
|
|
|
|
|
'测试规则已从未来配置中删除;已排队和已创建实例保持原策略。',
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
render();
|
|
|
|
|
|
}
|