Files
RustProxy/src/admin/web/management.js
T
2026-11-28 20:38:38 +01:00

692 lines
53 KiB
JavaScript

"use strict";
(() => {
const label = (tag, key, cls) => { const node = el(tag, t(key), cls); node.dataset.i18n = key; return node; };
const option = (value, key) => { const node = label("option", key); node.value = value; return node; };
const codes = [400, 401, 403, 404, 408, 413, 429, 500, 502, 503, 504];
const STATUS_NAMES = { 200: "OK", 204: "No Content", 400: "Bad Request", 401: "Unauthorized", 403: "Forbidden", 404: "Not Found", 408: "Request Timeout", 410: "Gone", 413: "Payload Too Large", 429: "Too Many Requests", 500: "Internal Server Error", 502: "Bad Gateway", 503: "Service Unavailable", 504: "Gateway Timeout" };
const statusLabel = code => `${code} ${STATUS_NAMES[code] || ""}`.trim();
const CLOUDFLARE_KINDS = ["cloudflare", "cloudflare_welcome", "cloudflare_placeholder"];
const PAGE_KIND_CARDS = [["", "pageInherit", "pageKindInheritHelp"], ["welcome", "pageWelcome", "notfoundKindWelcomeHelp"], ["builtin", "pageBuiltin", "notfoundKindBuiltinHelp"], ["cloudflare", "notfoundKindCloudflare", "notfoundKindCloudflareHelp"], ["empty", "pageEmpty", "notfoundKindEmptyHelp"], ["html", "pageHtml", "notfoundKindHtmlHelp"]];
let pageCardSerial = 0;
function pageEditor(parent, key, value = null, options = {}) {
const row = el("div", undefined, "page-editor"), heading = options.title ? el("b", options.title, "page-label") : label("b", key, "page-label");
const summary = el("button", undefined, "page-editor-summary"); summary.type = "button"; summary.setAttribute("aria-expanded", "false");
const copy = el("span", undefined, "page-editor-head"); copy.append(heading); if (options.whenKey) copy.append(label("small", options.whenKey, "page-when"));
const kindPill = el("span", "", "pill page-kind-pill"), statusPill = el("span", "", "pill page-status-pill");
const badges = el("span", undefined, "page-editor-badges"); badges.append(kindPill, statusPill); summary.append(el("span", undefined, "page-editor-dot"), copy, badges);
row.append(summary); row.dataset.pageSlot = options.slot || "";
const mode = el("select"), status = el("select"), html = el("textarea"), file = el("input");
mode.setAttribute("aria-label", t(key)); mode.className = "page-mode";
mode.append(option("", "pageInherit"), option("welcome", "pageWelcome"), option("builtin", "pageBuiltin"), option("cloudflare", "pageCloudflare"), option("cloudflare_welcome", "pageCloudflareWelcome"), option("cloudflare_placeholder", "pageCloudflarePlaceholder"), option("empty", "pageEmpty"), option("html", "pageHtml"));
mode.value = value?.kind || "";
status.className = "page-status"; status.setAttribute("aria-label", t("pageStatus"));
status.append(option("", "pageOriginalStatus"));
for (const code of [200, 204, 400, 401, 403, 404, 408, 410, 413, 429, 500, 502, 503, 504]) { const o = el("option", statusLabel(code)); o.value = code; status.append(o); }
status.value = value?.status ?? ""; html.value = value?.html || ""; html.rows = 5; html.maxLength = 32768; html.spellcheck = false; html.className = "page-html"; html.setAttribute("aria-label", t("pageHtmlSource"));
file.type = "file"; file.accept = ".html,.htm,text/html,text/plain"; file.setAttribute("aria-label", t("pageUploadHtml"));
const source = el("div", undefined, "page-source"); source.append(label("span", "pageUploadHtml", "fine"), file, html);
const controls = el("div", undefined, "page-editor-controls"); controls.hidden = true;
const name = `page-kind-${++pageCardSerial}`, chips = el("div", undefined, "page-kind-chips"); chips.setAttribute("role", "radiogroup"); chips.setAttribute("aria-label", t(key));
for (const [kind, titleKey, helpKey] of PAGE_KIND_CARDS) {
const chip = el("label", undefined, "page-kind-chip"), radio = el("input"); radio.type = "radio"; radio.name = name; radio.value = kind;
chip.append(radio, label("span", titleKey)); chip.title = t(helpKey); chip.dataset.i18nTitle = helpKey; chips.append(chip);
radio.addEventListener("change", () => { if (!radio.checked) return; mode.value = kind === "cloudflare" ? variant.value : kind; mode.dispatchEvent(new Event("change", { bubbles: true })); });
}
const help = el("p", "", "fine page-kind-help");
const variant = el("select"); variant.className = "page-variant"; variant.append(option("cloudflare", "notfoundVariantError"), option("cloudflare_welcome", "notfoundVariantWelcome"), option("cloudflare_placeholder", "notfoundVariantPlaceholder"));
variant.addEventListener("change", () => { mode.value = variant.value; mode.dispatchEvent(new Event("change", { bubbles: true })); });
const variantField = el("label", undefined, "page-variant-field"); variantField.append(label("span", "notfoundCloudflareVariant"), variant);
const statusField = el("label", undefined, "page-status-field"); statusField.append(label("span", "pageStatus"), status);
const fields = el("div", undefined, "page-editor-fields"); fields.append(variantField, statusField);
mode.hidden = true; controls.append(chips, help, mode, fields, source);
row.append(controls);
function sync() {
if (mode.value && status.value === "204") mode.value = "empty"; status.disabled = !mode.value; source.hidden = mode.value !== "html";
const group = CLOUDFLARE_KINDS.includes(mode.value) ? "cloudflare" : mode.value;
for (const radio of chips.querySelectorAll("input")) { radio.checked = radio.value === group; radio.closest(".page-kind-chip").classList.toggle("is-selected", radio.checked); }
variantField.hidden = group !== "cloudflare"; if (group === "cloudflare") variant.value = mode.value;
const helpKey = PAGE_KIND_CARDS.find(([kind]) => kind === group)?.[2] || "pageKindInheritHelp"; help.dataset.i18n = helpKey; help.textContent = t(helpKey);
kindPill.textContent = mode.selectedOptions[0]?.textContent || ""; kindPill.classList.toggle("live", !!mode.value);
statusPill.textContent = mode.value && status.value ? statusLabel(Number(status.value)) : ""; statusPill.hidden = !statusPill.textContent;
row.classList.toggle("is-custom", !!mode.value);
options.onChange?.(api);
}
for (const event of ["change", "input"]) { mode.addEventListener(event, sync); status.addEventListener(event, sync); html.addEventListener(event, () => options.onChange?.(api)); }
file.addEventListener("change", async () => { try { const chosen = file.files[0]; if (!chosen) return; if (chosen.size > 32768) throw new Error(t("pageTooLarge")); html.value = await chosen.text(); html.dispatchEvent(new Event("input", { bubbles: true })); } catch (error) { toast(error.message, true); } });
summary.addEventListener("click", () => { if (!controls.hidden && row.classList.contains("is-previewed")) { api.setOpen(false); return; } options.onSelect?.(api); api.setOpen(true); });
controls.addEventListener("focusin", () => options.onSelect?.(api));
parent.append(row);
function setMode(kind) {
if (mode.value === kind && (kind || !status.value)) return;
mode.value = kind;
if (!kind) status.value = "";
mode.dispatchEvent(new Event("change", { bubbles: true }));
}
const styled = () => CLOUDFLARE_KINDS.includes(mode.value);
const bodyless = () => mode.value === "empty" && status.value === "204";
const api = {
row, key, slot: options.slot || "", scope: options.scope || "global", vhostId: options.vhostId || null, naturalStatus: options.naturalStatus || 404,
title: () => heading.textContent,
setOpen(open) { controls.hidden = !open; row.classList.toggle("is-open", open); summary.setAttribute("aria-expanded", String(open)); },
state: () => ({ kind: mode.value, status: status.value ? Number(status.value) : null, html: html.value }),
setCloudflare: () => {
if (bodyless() || styled()) return;
if (mode.value === "welcome" && key !== "customErrorPages") {
setMode(key === "pageUnknownHost" || key === "pageMissingPath" ? "cloudflare_placeholder" : "cloudflare_welcome");
} else setMode("cloudflare");
}, reset: () => setMode(""),
isCloudflare: () => styled() || bodyless(),
read() { if (!mode.value) return null; const markup = mode.value === "html" ? html.value : ""; if (new TextEncoder().encode(markup).length > 32768) { api.setOpen(true); throw new Error(t("pageTooLarge")); } return { kind: mode.value, status: status.value ? Number(status.value) : null, html: markup }; }
};
sync();
return api;
}
function previewPane(parent, savedPage) {
const pane = el("aside", undefined, "page-preview-pane"), headRow = el("div", undefined, "page-preview-head"), titleNode = el("strong", ""), open = el("a", t("notfoundPreviewOpen"), "page-preview-open");
open.dataset.i18n = "notfoundPreviewOpen"; open.target = "_blank"; open.rel = "noopener noreferrer";
headRow.append(label("span", "notfoundPreview", "small-label"), titleNode, open);
const frame = el("iframe"); frame.setAttribute("sandbox", ""); frame.referrerPolicy = "no-referrer"; frame.title = t("notfoundPreview"); frame.loading = "lazy";
const note = el("p", "", "fine page-preview-note");
pane.append(headRow, frame, note); parent.append(pane);
let current = null, timer = null;
const inheritKey = editor => editor.scope === "vhost" ? "pagePreviewInheritVhost" : { direct_ip: "pagePreviewInheritDirectIp", unknown_host: "pagePreviewInheritUnknown" }[editor.slot] || "pagePreviewInheritError";
function render() {
if (!current) return;
const state = current.state(), status = state.status || current.naturalStatus;
titleNode.textContent = current.title();
for (const node of parent.querySelectorAll(".page-editor.is-previewed")) node.classList.remove("is-previewed");
current.row.classList.add("is-previewed");
let url = "", noteKey = "";
if (!state.kind) noteKey = inheritKey(current);
else if (state.kind === "empty") noteKey = "notfoundPreviewEmpty";
else if (state.kind === "html") {
const saved = savedPage(current);
if (saved?.kind !== "html") noteKey = "pagePreviewHtmlUnsaved";
else {
const params = new URLSearchParams({ kind: "html", status: String(status), slot: current.slot }); if (current.vhostId) params.set("vhost", current.vhostId);
url = `/api/pages/preview?${params}`; if (saved.html !== state.html) noteKey = "notfoundPreviewHtmlSaved";
}
} else url = `/api/pages/preview?${new URLSearchParams({ kind: state.kind, status: String(status) })}`;
note.hidden = !noteKey; if (noteKey) { note.dataset.i18n = noteKey; note.textContent = t(noteKey); }
frame.hidden = !url; open.hidden = !url;
if (url) { open.href = url; if (frame.dataset.src !== url) { frame.dataset.src = url; frame.src = url; } }
}
return {
select(editor) { current = editor; render(); },
changed(editor) { if (editor === current) { clearTimeout(timer); timer = setTimeout(render, 150); } }
};
}
function errorsEditor(parent, values = {}, context = {}) {
const details = el("details", undefined, "pages-errors"), summary = el("summary"), count = el("span", "", "pill");
summary.append(label("span", "customErrorPages"), count); details.append(summary, label("p", "pageErrorsWhen", "fine page-when")); parent.append(details);
const list = el("div", undefined, "page-editor-list"); details.append(list);
const rows = new Map(); for (const code of codes) { const editor = pageEditor(list, "customErrorPages", values[code], { ...context, slot: `error-${code}`, naturalStatus: code, title: statusLabel(code) }); rows.set(code, editor); }
const updateCount = () => { const custom = [...rows.values()].filter(editor => editor.state().kind).length; count.textContent = `${custom} / ${codes.length}`; count.classList.toggle("live", custom > 0); };
details.addEventListener("change", updateCount); updateCount();
if ([...rows.values()].some(editor => editor.state().kind)) details.open = true;
return {
setAllCloudflare() { for (const editor of rows.values()) editor.setCloudflare(); details.open = true; updateCount(); },
resetAll() { for (const editor of rows.values()) editor.reset(); details.open = false; updateCount(); },
allCloudflare: () => [...rows.values()].every(editor => editor.isCloudflare()),
read() { return Object.fromEntries([...rows].map(([code, editor]) => [code, editor.read()]).filter(([, page]) => page)); }
};
}
function bulkCloudflareSwitch(parent, apply) {
const wrapper = el("div", undefined, "page-bulk-actions");
const control = el("label", undefined, "check page-bulk-switch-label");
const toggle = document.createElement("input");
toggle.type = "checkbox";
toggle.className = "switch";
control.append(toggle, label("span", "pageCloudflareAll"));
toggle.addEventListener("change", () => apply(toggle.checked));
wrapper.append(control);
parent.append(wrapper);
return toggle;
}
function workspace(parent, savedPage) {
const box = el("div", undefined, "pages-workspace"), editors = el("div", undefined, "pages-editors"); box.append(editors); parent.append(box);
const preview = previewPane(box, savedPage); let open = null;
const select = editor => { if (open && open !== editor) open.setOpen(false); open = editor; preview.select(editor); };
return { editors, context: { onSelect: select, onChange: editor => preview.changed(editor) }, preview: { select(editor) { select(editor); editor.setOpen(true); }, changed: preview.changed } };
}
function vhostEditor(outer, values = {}, options = {}) {
const vhostId = options.vhostId || "";
const savedVhostPage = editor => { const pages = (envelope?.config.vhosts || []).find(item => item.id === vhostId)?.pages; if (!pages || !vhostId) return null; return editor.slot.startsWith("error-") ? pages.errors?.[Number(editor.slot.slice(6))] : pages[editor.slot]; };
const space = workspace(outer, savedVhostPage), parent = space.editors, context = { ...space.context, scope: "vhost", vhostId };
let applying = false;
const bulk = bulkCloudflareSwitch(parent, checked => {
applying = true;
try {
if (checked) {
response.setCloudflare(); fallback.setCloudflare(); errors.setAllCloudflare();
enabled.checked = true;
statuses.value = codes.join(", ");
} else {
response.reset(); fallback.reset(); errors.resetAll();
enabled.checked = false;
statuses.value = "404";
}
enabled.dispatchEvent(new Event("change", { bubbles: true }));
} finally { applying = false; }
syncBulk();
});
const response = pageEditor(parent, "pageStaticHost", values.response, { ...context, slot: "response", naturalStatus: 200, whenKey: "pageStaticHostWhen" }), fallback = pageEditor(parent, "pageMissingPath", values.fallback, { ...context, slot: "fallback", naturalStatus: 404, whenKey: "pageMissingPathWhen" });
const interception = el("section", undefined, "php-origin-errors");
const toggle = el("label", undefined, "check"), enabled = document.createElement("input");
enabled.type = "checkbox"; enabled.className = "switch"; enabled.checked = (values.intercept_statuses || []).length > 0;
toggle.append(enabled, label("span", "vhostInterceptOriginErrors"));
const codesLabel = el("label"), codesCaption = label("span", "vhostInterceptOriginStatuses"), statuses = document.createElement("input");
statuses.type = "text"; statuses.maxLength = 64;
statuses.value = (values.intercept_statuses?.length ? values.intercept_statuses : [404]).join(", ");
statuses.placeholder = "404, 500, 502, 503, 504";
codesLabel.append(codesCaption, statuses);
const hint = label("p", "vhostInterceptOriginErrorsHelp", "fine");
const sync = () => { statuses.disabled = !enabled.checked; };
enabled.addEventListener("change", sync); sync();
interception.append(toggle, codesLabel, hint); parent.append(interception);
const errors = errorsEditor(parent, values.errors, context);
const syncBulk = () => {
const selected = statuses.value.split(/[\s,]+/).filter(Boolean);
bulk.checked = response.isCloudflare() && fallback.isCloudflare() && errors.allCloudflare()
&& enabled.checked && selected.length === codes.length && codes.every(code => selected.includes(String(code)));
};
const onEditorChange = event => { if (!applying && event.target !== bulk) syncBulk(); };
parent.addEventListener("change", onEditorChange);
parent.addEventListener("input", onEditorChange);
syncBulk();
parent.append(label("p", "pagesSafetyHint", "fine"));
space.preview.select(response);
return {
read: () => {
const selectedTokens = enabled.checked ? statuses.value.split(/[\s,]+/).filter(Boolean) : [];
const selected = selectedTokens.map(Number);
if (enabled.checked && (!selected.length || selectedTokens.some(token => !/^\d{3}$/.test(token)) || selected.length > codes.length || new Set(selected).size !== selected.length || selected.some(code => !codes.includes(code)))) {
throw new Error(t("vhostInterceptOriginInvalid"));
}
return { response: response.read(), fallback: fallback.read(), errors: errors.read(), intercept_statuses: selected };
}
};
}
let global = null, globalSync = null;
function render() {
if (!envelope || settingsDirty.has("pages-form")) return; const values = envelope.config.pages || {}, parent = $("global-pages-fields");
if (globalSync) { parent.removeEventListener("change", globalSync); parent.removeEventListener("input", globalSync); }
parent.replaceChildren();
let applying = false;
const bulk = bulkCloudflareSwitch(parent, checked => {
applying = true;
try {
if (checked) { ip.setCloudflare(); unknown.setCloudflare(); errors.setAllCloudflare(); }
else { ip.reset(); unknown.reset(); errors.resetAll(); }
} finally { applying = false; }
globalSync();
});
const savedGlobalPage = editor => { const pages = envelope?.config.pages || {}; return editor.slot.startsWith("error-") ? pages.errors?.[Number(editor.slot.slice(6))] : pages[editor.slot]; };
const space = workspace(parent, savedGlobalPage), context = space.context;
const ip = pageEditor(space.editors, "pageDirectIp", values.direct_ip, { ...context, slot: "direct_ip", naturalStatus: 200, whenKey: "pageDirectIpWhen" }), unknown = pageEditor(space.editors, "pageUnknownHost", values.unknown_host, { ...context, slot: "unknown_host", naturalStatus: 404, whenKey: "pageUnknownHostWhen" }), errors = errorsEditor(space.editors, values.errors, context);
space.preview.select(ip);
globalSync = event => { if (!applying && event?.target !== bulk) bulk.checked = ip.isCloudflare() && unknown.isCloudflare() && errors.allCloudflare(); };
parent.addEventListener("change", globalSync);
parent.addEventListener("input", globalSync);
globalSync();
global = { read: () => ({ direct_ip: ip.read(), unknown_host: unknown.read(), errors: errors.read() }) };
}
window.rustProxyPages = { render, vhostEditor };
bindSettingsForm("pages-form", next => { next.pages = global.read(); }, "pages");
$("settings-security-open").addEventListener("click", () => setPanel("security"));
$("settings-vhost-open").addEventListener("click", () => setPanel("vhosts"));
if (envelope) render();
})();
(() => {
const dialog = $("logs-dialog"); let paused = null, expanded = null, docked = false, pinning = false, preferencesLoaded = false, preferencesLoading = null, preferenceSaveRunning = false, preferenceSaveQueued = false;
const filters = ["log-vhost", "log-method", "log-status", "log-outcome", "log-filter"];
const columns = [
{ id: "time", label: "time", value: e => formatTime(e.time_ms) },
{ id: "request", label: "request", value: e => `${e.method} ${e.path}` },
{ id: "host", label: "logHost", value: e => e.host },
{ id: "source_ip", label: "sourceIp", value: e => e.client_ip || "-" },
{ id: "vhost", label: "vhVhost", value: e => e.vhost || "-" },
{ id: "backend", label: "backend", value: e => e.backend || "-" },
{ id: "status", label: "status", value: e => e.status },
{ id: "duration", label: "duration", value: e => `${e.duration_ms} ms` },
{ id: "cache", label: "cache", value: e => e.cache || "-" },
{ id: "outcome", label: "outcome", value: e => t(e.outcome) },
{ id: "transport", label: "logTransport", value: e => e.transport ? `${e.transport}${e.http_version ? ` / HTTP ${e.http_version}` : ""}` : "-" },
{ id: "websocket", label: "logWebSocket", value: e => [e.websocket_bootstrap, e.websocket_phase, e.websocket_error, e.h3_reset_code].filter(Boolean).join(" / ") || "-" },
{ id: "upstream_error", label: "logUpstreamError", value: e => e.upstream_error || "-" },
];
const defaultColumns = () => new Set(columns.map(column => column.id));
let visibleColumns = defaultColumns();
function active() { return dialog.open && !paused && !expanded; }
function syncLivePolling(fetchNow = false) {
window.rustProxyRefresh?.sync?.();
if (fetchNow && active()) void window.rustProxyRefresh?.manual?.("logs-fallback");
}
function source() { return paused || expanded || lastLogs || { entries: [] }; }
function mergeExpanded(data) {
if (!expanded || paused || !data?.entries) return;
const seen = new Set();
const entries = [];
for (const entry of [...data.entries, ...(expanded.entries || [])]) {
const key = entry.id ?? `${entry.time_ms}:${entry.method}:${entry.host}:${entry.path}:${entry.status}`;
if (seen.has(key)) continue;
seen.add(key); entries.push(entry);
if (entries.length >= 4096) break;
}
expanded = { ...expanded, ...data, entries, capacity: 4096, window: 4096 };
}
function syncExpandedButton() {
const button = $("logs-load-history");
button.textContent = t(expanded ? "logExpandedBuffer" : "logExpandBuffer");
button.dataset.i18n = expanded ? "logExpandedBuffer" : "logExpandBuffer";
button.disabled = Boolean(expanded);
}
function filtered() {
const query = $("log-filter").value.trim().toLowerCase(), outcome = $("log-outcome").value;
return source().entries.filter(e => (!$("log-vhost").value || e.vhost === $("log-vhost").value)
&& (!$("log-method").value || e.method === $("log-method").value)
&& (!$("log-status").value || String(e.status).startsWith($("log-status").value))
&& (outcome === "all" || (outcome === "errors" ? e.status >= 400 : e.cache === outcome))
&& `${e.method} ${e.path} ${e.host} ${e.client_ip || ""} ${e.vhost || ""} ${e.backend || ""} ${e.transport || ""} ${e.http_version || ""} ${e.websocket_bootstrap || ""} ${e.websocket_phase || ""} ${e.websocket_error || ""} ${e.h3_reset_code || ""} ${e.upstream_error || ""}`.toLowerCase().includes(query));
}
function syncHeaders() {
dialog.querySelectorAll("th[data-log-column]").forEach(header => { header.hidden = !visibleColumns.has(header.dataset.logColumn); });
}
function syncColumnPicker() {
const picker = $("log-column-options");
for (const column of columns) {
let input = [...picker.querySelectorAll("input[data-column]")].find(node => node.dataset.column === column.id);
if (!input) {
const label = el("label", undefined, "check log-column-option");
input = document.createElement("input"); input.type = "checkbox"; input.dataset.column = column.id;
const caption = el("span"); caption.dataset.i18n = column.label;
input.addEventListener("change", () => {
if (!input.checked && visibleColumns.size === 1) { input.checked = true; toast(t("logColumnsRequired"), true); return; }
if (input.checked) visibleColumns.add(column.id); else visibleColumns.delete(column.id);
syncHeaders(); render(); void savePreferences();
});
label.append(input, caption); picker.append(label);
}
input.checked = visibleColumns.has(column.id);
const caption = input.nextElementSibling, label = t(column.label);
if (caption.textContent !== label) caption.textContent = label;
}
}
function applyPreferences(preferences) {
if (!preferences) return;
const selected = Array.isArray(preferences.live_log_columns) ? preferences.live_log_columns.filter(id => columns.some(column => column.id === id)) : [];
visibleColumns = selected.length ? new Set(selected) : defaultColumns(); preferencesLoaded = true;
syncColumnPicker(); syncHeaders(); render();
}
async function loadPreferences(force = false) {
if (!authenticated || (preferencesLoaded && !force)) return;
if (preferencesLoading) return preferencesLoading;
preferencesLoading = (async () => {
try { applyPreferences(await window.rustProxyPreferences.load(force)); }
catch (error) { if (error.status !== 401) toast(error.message, true); }
finally { preferencesLoading = null; }
})();
return preferencesLoading;
}
async function savePreferences() {
if (!authenticated) return;
preferenceSaveQueued = true;
if (preferenceSaveRunning) return;
preferenceSaveRunning = true;
try {
while (preferenceSaveQueued && authenticated) {
preferenceSaveQueued = false;
const selected = columns.filter(column => visibleColumns.has(column.id)).map(column => column.id);
const preferences = await window.rustProxyPreferences.update({ live_log_columns: selected });
if (!authenticated) return;
if (!preferenceSaveQueued) applyPreferences(preferences);
}
} catch (error) { if (error.status !== 401) toast(error.message, true); }
finally { preferenceSaveRunning = false; }
}
function render(data = null) {
if (data) mergeExpanded(data);
if (!dialog.open) return;
const selector = $("log-vhost"), selected = selector.value, ids = new Set([...(envelope?.config.vhosts || []).map(v => v.id), ...source().entries.map(e => e.vhost).filter(Boolean)]);
if (selected) ids.add(selected);
const all = el("option", t("logAllVhosts")); all.value = ""; selector.replaceChildren(all, ...[...ids].sort().map(id => { const o = el("option", id); o.value = id; return o; })); if (ids.has(selected)) selector.value = selected;
const entries = filtered(), rows = entries.map(e => {
const row = el("tr"); const hints = [];
if (e.cache_reason) hints.push(window.rustProxyCachePanel?.reason(e.cache_reason) || e.cache_reason);
if (e.upstream_error) hints.push(`upstream: ${e.upstream_error}`);
row.title = hints.join(" · ");
for (const column of columns) {
if (!visibleColumns.has(column.id)) continue;
const value = column.value(e), cell = el("td", String(value)); cell.title = String(value);
if (column.id === "status") cell.className = e.status < 400 ? "status-good" : "status-error";
if (column.id === "cache" && ["HIT", "REVALIDATED"].includes(e.cache)) cell.className = "status-good";
if (column.id === "cache" && e.cache === "STALE") cell.className = "status-stale";
row.append(cell);
}
return row;
});
syncHeaders(); $("logs").replaceChildren(...rows); $("empty-logs").hidden = rows.length > 0; $("log-count").textContent = `${entries.length} / ${source().entries.length} | ${t(paused ? "logPaused" : "live")}`;
$("logs-pause").textContent = t(paused ? "logResume" : "logPause"); $("logs-pin").textContent = t(docked ? "logUndock" : "logPin"); syncExpandedButton();
}
function open(vhost) {
if (!authenticated) return;
if (!preferencesLoaded) void loadPreferences();
if (vhost) { const opt = el("option", vhost); opt.value = vhost; $("log-vhost").append(opt); $("log-vhost").value = vhost; }
if (!dialog.open) { if (docked) dialog.show(); else dialog.showModal(); }
render(); syncLivePolling(true);
}
syncColumnPicker(); syncHeaders();
$("logs-open").addEventListener("click", () => { $("logs-menu").open = false; open(); }); $("logs-close").addEventListener("click", () => dialog.close());
$("logs-load-history").addEventListener("click", async () => {
const button = $("logs-load-history"); button.disabled = true;
try {
const snapshot = await api("/api/logs?limit=4096");
expanded = { ...snapshot, capacity: 4096, window: 4096 }; render();
toast(t("logBufferExpanded").replace("{count}", String(snapshot.entries?.length || 0)));
} catch (error) { toast(error.message, true); }
finally { button.disabled = Boolean(expanded); syncLivePolling(); }
});
$("logs-pause").addEventListener("click", () => { paused = paused ? null : structuredClone(source()); render(); syncLivePolling(!paused); });
$("logs-pin").addEventListener("click", () => { pinning = true; dialog.close(); docked = !docked; dialog.classList.toggle("docked", docked); if (docked) dialog.show(); else dialog.showModal(); pinning = false; render(); syncLivePolling(true); });
dialog.addEventListener("close", () => { if (pinning) return; expanded = null; paused = null; syncExpandedButton(); syncLivePolling(); });
for (const id of filters) $(id).addEventListener(id === "log-filter" ? "input" : "change", render);
$("export-logs").addEventListener("click", () => { const url = URL.createObjectURL(new Blob([JSON.stringify({ entries: filtered(), capacity: source().capacity, filtered: true }, null, 2) + "\n"], { type: "application/json" })); const a = el("a"); a.href = url; a.download = "RustProxy-access-log.json"; a.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); });
window.rustProxyLogs = { render, open, active, loadPreferences, applyPreferences, reset() { dialog.close(); paused = null; expanded = null; preferencesLoaded = false; preferencesLoading = null; preferenceSaveQueued = false; visibleColumns = defaultColumns(); syncColumnPicker(); syncHeaders(); for (const id of filters) $(id).value = id === "log-outcome" ? "all" : ""; syncExpandedButton(); syncLivePolling(); } };
})();
(() => {
let editing = null, dirty = false;
const form = $("acl-profile-form");
const split = value => value.split(/[\s,]+/).map(v => v.trim()).filter(Boolean);
const aclDefault = $("acl-profile-default"), countryMode = $("acl-profile-country-mode"), countriesInput = $("acl-profile-countries");
const countryEnabled = $("acl-profile-country-enabled"), countryField = $("acl-profile-country-field"), customControls = $("acl-profile-custom-controls");
const rules = $("acl-profile-rules"), ruleRows = $("acl-profile-rule-rows"), bulk = $("acl-profile-bulk");
const addButton = $("acl-profile-rule-add"), importButton = $("acl-profile-import-rules");
const previewDefault = $("acl-profile-preview-default"), previewFlow = $("acl-profile-preview-flow"), previewNote = $("acl-profile-preview-note");
const countryHelp = $("acl-profile-country-help"), countryLabel = $("acl-profile-country-label"), ipHelp = $("acl-profile-ip-help");
const i18n = (tag, key, cls) => { const node = el(tag, t(key), cls); node.dataset.i18n = key; return node; };
const dnsHint = i18n("p", "aclDnsHelp", "fine"); ipHelp?.after(dnsHint);
const dnsStatus = $("acl-dns-status"), dnsStatusList = $("acl-dns-status-list");
async function refreshDnsStatus() {
const button = $("acl-dns-status-refresh");
button.disabled = true;
try {
const snapshot = await api("/api/acl/dns-status");
const names = snapshot.hostnames || [];
const labels = { pending: "aclDnsPending", resolved: "aclDnsResolved", failed: "aclDnsFailed", expired: "aclDnsExpired" };
const rows = names.map(item => {
const card = el("div", undefined, "entity");
const title = el("div", undefined, "entity-top");
title.append(el("strong", item.hostname), el("span", t(labels[item.state] || "aclDnsPending")));
card.append(title);
const addresses = (item.addresses || []).join(", ");
card.append(el("p", `${t("aclDnsAddresses")}: ${addresses || "-"}`, "entity-details"));
const checked = item.last_checked_unix_ms ? new Date(item.last_checked_unix_ms).toLocaleString() : "-";
card.append(el("p", `${t("aclDnsChecked")}: ${checked} · TTL: ${item.valid_for_seconds ?? "-"} s`, "fine"));
if (item.error) card.append(el("p", `${t("aclDnsError")}: ${item.error}`, "fine"));
return card;
});
dnsStatusList.replaceChildren(...(rows.length ? rows : [el("p", t("aclDnsNoNames"), "fine")]));
} catch (error) {
dnsStatusList.replaceChildren(el("p", error.message, "fine"));
} finally { button.disabled = false; }
}
dnsStatus.addEventListener("toggle", () => { if (dnsStatus.open) void refreshDnsStatus(); });
$("acl-dns-status-refresh").addEventListener("click", () => void refreshDnsStatus());
const modeInputs = [...form.querySelectorAll('input[name="acl-profile-mode"]')];
const selectedMode = () => modeInputs.find(node => node.checked)?.value || "allow_exceptions";
const setMode = value => { for (const node of modeInputs) node.checked = node.value === value; };
const guidedAction = mode => mode === "allow_exceptions" ? "allow" : "deny";
const guidedDefault = mode => mode === "allow_exceptions" ? "deny" : "allow";
const guidedCountryMode = mode => mode === "allow_exceptions" ? "allow" : "block";
const activeCountryMode = () => selectedMode() === "custom" ? countryMode.value : (countryEnabled.checked ? guidedCountryMode(selectedMode()) : "off");
function normalizeNetwork(value) {
const network = value.trim();
if (!network || network.includes("/")) return network;
if (/^(?:\d{1,3}\.){3}\d{1,3}$/.test(network)) {
const octets = network.split(".").map(Number);
if (octets.every(value => value >= 0 && value <= 255)) return `${network}/32`;
}
if (network.includes(":")) return `${network}/128`;
return network;
}
const ruleKind = value => /[a-z]/i.test(value) && value.includes(".") && !value.includes(":") && !value.includes("/") ? "dns" : "ip";
function syncRuleKind(row) {
const kind = row.querySelector(".acl-profile-rule-type").value;
const network = row.querySelector(".acl-profile-rule-network");
network.maxLength = kind === "dns" ? 253 : 64;
network.placeholder = kind === "dns" ? "office.example.ddns.net" : "10.0.0.0/8 or 203.0.113.5";
}
function addRule(rule = null, mark = true) {
if (ruleRows.children.length >= 128) { toast(t("vhRowLimit"), true); return; }
const mode = selectedMode(), fallbackAction = mode === "custom" ? "allow" : guidedAction(mode);
const value = rule || { action: fallbackAction, network: "" };
const row = el("div", undefined, "acl-rule acl-profile-rule");
const actionLabel = el("label", undefined, "acl-profile-rule-action-field"); actionLabel.append(i18n("span", "aclAction"));
const actionSelect = el("select"); actionSelect.className = "acl-profile-rule-action";
for (const actionValue of ["allow", "deny"]) { const option = el("option", t(actionValue === "allow" ? "aclAllow" : "aclDeny")); option.value = actionValue; actionSelect.append(option); }
actionSelect.value = value.action || fallbackAction; actionLabel.append(actionSelect);
const actionBadge = el("span", "", "acl-profile-rule-action-badge"); actionLabel.append(actionBadge);
const typeLabel = el("label"); typeLabel.append(i18n("span", "aclRuleType"));
const kind = el("select"); kind.className = "acl-profile-rule-type";
for (const [id, key] of [["ip", "aclRuleIp"], ["dns", "aclRuleDns"]]) {
const item = i18n("option", key); item.value = id; kind.append(item);
}
kind.value = ruleKind(value.network || ""); typeLabel.append(kind);
const networkLabel = el("label"); networkLabel.append(i18n("span", "aclNetwork"));
const network = el("input"); network.className = "acl-profile-rule-network"; network.type = "text"; network.required = true; network.autocomplete = "off"; network.spellcheck = false; network.value = value.network || ""; networkLabel.append(network);
network.addEventListener("blur", () => { const normalized = normalizeNetwork(network.value); if (normalized !== network.value) { network.value = normalized; syncRulesText(); renderPreview(); } });
kind.addEventListener("change", () => { network.value = ""; syncRuleKind(row); syncRulesText(); dirty = true; syncPolicy(); network.focus(); });
const remove = action(t("remove"), () => { row.remove(); syncRulesText(); dirty = true; syncPolicy(); }); remove.classList.add("acl-profile-rule-remove");
row.append(actionLabel, typeLabel, networkLabel, remove); ruleRows.append(row); syncRuleKind(row);
if (mark) { syncRulesText(); dirty = true; }
syncRuleRows();
}
function collectRuleRows() {
return [...ruleRows.children].map(row => ({
action: row.querySelector(".acl-profile-rule-action").value,
network: normalizeNetwork(row.querySelector(".acl-profile-rule-network").value),
}));
}
function syncRulesText() { rules.value = collectRuleRows().map(rule => `${rule.action} ${rule.network}`.trimEnd()).join("\n"); }
function renderRuleRows(values) { ruleRows.replaceChildren(); for (const rule of values || []) addRule(rule, false); syncRulesText(); syncRuleRows(); }
function parseBulkRules(value, target) {
if (selectedMode() === "custom") return parseRules(value, target);
const expected = guidedAction(selectedMode());
return normalizeAclRuleText(value).split(/\r?\n/).map((line, index) => ({ line: line.trim(), number: index + 1 })).filter(item => item.line).map(item => {
const match = item.line.match(/^(?:(allow|deny)\s+)?(\S+)$/i);
if (!match) throw fieldError(target, `${t("ruleSyntaxLine")} ${item.number}: ${item.line}`);
return { action: (match[1] || expected).toLowerCase(), network: normalizeNetwork(match[2]) };
});
}
function importRules() {
try {
const parsed = parseBulkRules(rules.value, rules), mode = selectedMode();
if (mode !== "custom" && parsed.some(rule => rule.action !== guidedAction(mode))) setMode("custom");
renderRuleRows(parsed); dirty = true; syncPolicy(); toast(t("aclProfileRulesImported"));
} catch (error) { toast(error.message, true); }
}
function inferMode(profile) {
const cMode = profile.country_access?.mode || "off";
const defaultAction = profile.acl?.default || (cMode === "allow" ? "deny" : "allow");
const actions = (profile.acl?.rules || []).map(rule => rule.action);
if (defaultAction === "deny" && actions.every(action => action === "allow") && ["off", "allow"].includes(cMode)) return "allow_exceptions";
if (defaultAction === "allow" && actions.every(action => action === "deny") && ["off", "block"].includes(cMode)) return "block_exceptions";
return "custom";
}
function syncRuleRows(convert = false) {
const mode = selectedMode(), custom = mode === "custom", expected = custom ? null : guidedAction(mode);
for (const row of ruleRows.children) {
const select = row.querySelector(".acl-profile-rule-action"), badge = row.querySelector(".acl-profile-rule-action-badge");
if (!custom && convert) select.value = expected;
select.hidden = !custom; badge.hidden = custom;
if (!custom) { badge.textContent = t(expected === "allow" ? "aclAllow" : "aclDeny"); badge.classList.toggle("is-allow", expected === "allow"); badge.classList.toggle("is-deny", expected === "deny"); }
}
syncRulesText();
}
function conditionChip(key, value, kind) {
const chip = el("span", undefined, `acl-policy-condition ${kind}`); chip.append(i18n("b", key), el("span", value)); return chip;
}
function renderPreview() {
const mode = selectedMode(), custom = mode === "custom", defaultAction = custom ? aclDefault.value : guidedDefault(mode);
previewDefault.textContent = t(defaultAction === "deny" ? "aclProfilePreviewDefaultDeny" : "aclProfilePreviewDefaultAllow");
previewDefault.className = defaultAction === "deny" ? "is-deny" : "is-allow";
const countries = [...new Set(split(countriesInput.value).map(value => value.toUpperCase()))];
const ipRules = collectRuleRows().filter(rule => rule.network);
if (custom) {
const modeValue = countryMode.value, items = [];
items.push(conditionChip("aclProfilePreviewIpRules", String(ipRules.length), "neutral"));
if (modeValue !== "off") items.push(conditionChip(modeValue === "allow" ? "aclProfilePreviewAllowedCountries" : "aclProfilePreviewBlockedCountries", countries.join(", ") || "—", modeValue === "allow" ? "allow" : "deny"));
previewFlow.replaceChildren(...items);
previewNote.textContent = t("aclProfilePreviewCustomNote");
return;
}
const allowMode = mode === "allow_exceptions", items = [];
if (countryEnabled.checked) items.push(conditionChip(allowMode ? "aclProfilePreviewAllowedCountries" : "aclProfilePreviewBlockedCountries", countries.join(", ") || "—", allowMode ? "allow" : "deny"));
if (ipRules.length) items.push(conditionChip(allowMode ? "aclProfilePreviewAllowedIps" : "aclProfilePreviewBlockedIps", ipRules.map(rule => rule.network).join(", "), allowMode ? "allow" : "deny"));
if (!items.length) items.push(el("span", t("aclProfilePreviewNoExceptions"), "acl-policy-condition neutral"));
const flow = [];
items.forEach((item, index) => { if (index) flow.push(el("span", t("aclProfileOr"), "acl-policy-or")); flow.push(item); });
previewFlow.replaceChildren(...flow);
previewNote.textContent = t(items.length === 1 && !countryEnabled.checked && !ipRules.length ? (allowMode ? "aclProfilePreviewBlocksAll" : "aclProfilePreviewAllowsAll") : (allowMode ? "aclProfilePreviewAllowNote" : "aclProfilePreviewBlockNote"));
}
function syncPolicy(convertRules = false) {
const mode = selectedMode(), custom = mode === "custom";
customControls.hidden = !custom;
if (!custom) {
aclDefault.value = guidedDefault(mode);
countryMode.value = countryEnabled.checked ? guidedCountryMode(mode) : "off";
countryLabel.textContent = t(mode === "allow_exceptions" ? "aclProfileAllowedCountries" : "aclProfileBlockedCountries");
countryLabel.dataset.i18n = mode === "allow_exceptions" ? "aclProfileAllowedCountries" : "aclProfileBlockedCountries";
countryHelp.textContent = t(mode === "allow_exceptions" ? "aclProfileAllowedCountriesHelp" : "aclProfileBlockedCountriesHelp");
ipHelp.textContent = t(mode === "allow_exceptions" ? "aclProfileAllowedIpsHelp" : "aclProfileBlockedIpsHelp");
} else {
countryLabel.textContent = t("vhCountryList"); countryLabel.dataset.i18n = "vhCountryList";
countryHelp.textContent = t("aclProfileCustomCountryHelp"); ipHelp.textContent = t("aclProfileCustomIpHelp");
countryEnabled.checked = countryMode.value !== "off";
}
countryField.classList.toggle("is-disabled", !countryEnabled.checked); countriesInput.disabled = !countryEnabled.checked;
if (custom) { countriesInput.disabled = countryMode.value === "off"; countryEnabled.checked = countryMode.value !== "off"; countryEnabled.disabled = true; }
else countryEnabled.disabled = false;
bulk.querySelector("summary").textContent = t(custom ? "aclProfileBulkRules" : "aclProfileBulkExceptions");
rules.placeholder = custom ? "allow 10.0.0.0/8\ndeny 10.21.0.0/16" : "10.0.0.0/8\n203.0.113.5";
syncRuleRows(convertRules); renderPreview();
}
function attachAclProfileId() { window.rustProxyAutoId.attach($("acl-profile-id"), { elements: [$("acl-profile-name")], taken: () => (envelope?.config.acl_profiles || []).map(value => value.id).filter(value => value !== editing), original: editing, inUse: id => configReferences(id, ["acl_profile_id", "acl_profile_ids"]), renameKey: "idRenameHelpAclProfile" }); }
function clear() {
editing = null; dirty = false; form.reset(); setMode("allow_exceptions"); aclDefault.value = "deny"; countryMode.value = "off"; countryEnabled.checked = false; countriesInput.value = ""; $("acl-profile-id").readOnly = false; attachAclProfileId(); $("acl-profile-editing").textContent = t("aclProfileNew"); renderRuleRows([]); rules.value = ""; syncPolicy();
}
function edit(profile) {
editing = profile.id; dirty = false; $("acl-profile-id").value = profile.id; $("acl-profile-id").readOnly = false; $("acl-profile-name").value = profile.name || profile.id; attachAclProfileId();
const cMode = profile.country_access?.mode || "off"; aclDefault.value = profile.acl?.default || (cMode === "allow" ? "deny" : "allow"); countryMode.value = cMode;
countryEnabled.checked = cMode !== "off"; countriesInput.value = (profile.country_access?.countries || []).join("\n");
setMode(inferMode(profile)); renderRuleRows(profile.acl?.rules || []); $("acl-profile-editing").textContent = t("aclProfileEdit"); dirty = false; syncPolicy();
}
function copyProfile(profile) {
edit(profile); editing = null; $("acl-profile-id").readOnly = false; $("acl-profile-id").value = ""; $("acl-profile-name").value = ""; attachAclProfileId();
$("acl-profile-editing").textContent = t("aclProfileNew"); dirty = true; $("acl-profile-name").focus();
}
function refreshTranslations() {
$("acl-profile-editing").textContent = t(editing ? "aclProfileEdit" : "aclProfileNew");
for (const row of ruleRows.children) {
const network = row.querySelector(".acl-profile-rule-network");
const kind = row.querySelector(".acl-profile-rule-type");
if (kind) { kind.value = ruleKind(network?.value || ""); syncRuleKind(row); }
const select = row.querySelector(".acl-profile-rule-action");
if (select?.options?.length >= 2) {
select.options[0].textContent = t("aclAllow");
select.options[1].textContent = t("aclDeny");
}
const remove = row.querySelector(".acl-profile-rule-remove");
if (remove) remove.textContent = t("remove");
}
syncPolicy();
render();
}
function render() {
if (!envelope) return;
const profiles = envelope.config.acl_profiles || [], list = $("acl-profile-list");
const query = ($("acl-profile-search")?.value || "").trim().toLowerCase();
const usersOf = profile => window.rustProxyConfigReferences(profile.id, ["acl_profile_id", "acl_profile_ids"]);
const shown = profiles.filter(profile => !query || [profile.id, profile.name, ...(profile.acl?.rules || []).map(rule => rule.network), ...(profile.country_access?.countries || []), ...usersOf(profile)].join(" ").toLowerCase().includes(query));
const cards = shown.map(profile => {
const box = el("article", undefined, "vhost-card acl-profile-card acl-profile-row"); box.dataset.aclProfile = profile.id;
const mode = inferMode(profile), users = usersOf(profile), rulesList = profile.acl?.rules || [], countries = profile.country_access?.countries || [];
const head = el("div", undefined, "vhost-card-head"), identity = el("div", undefined, "vhost-card-identity"), title = el("div", undefined, "vhost-title-line");
const modePill = el("span", t(mode === "allow_exceptions" ? "aclProfileModeAllowExceptions" : mode === "block_exceptions" ? "aclProfileModeBlockExceptions" : "aclProfileModeCustom"), `pill${profile.acl?.default === "deny" ? " warn" : " live"}`);
title.append(el("strong", profile.name || profile.id, "entity-title"), modePill, el("span", users.length ? `${t("aclProfileAttached")}: ${users.length}` : t("aclProfileNotUsed"), users.length ? "pill live" : "pill"));
identity.append(title, el("div", profile.id, "vhost-hosts")); head.append(identity);
const actions = el("div", undefined, "entity-actions"); actions.append(action(t("edit"), () => { edit(profile); openEditor(); }), action(t("copyEntry"), () => { copyProfile(profile); openEditor(); }), action(t("remove"), async () => {
if (users.length) { toast(`${t("aclProfileInUse")}: ${users.join(", ")}`, true); return; }
if (!(await appConfirm(t("confirmDelete"), { danger: true }))) return; const next = structuredClone(envelope.config); next.acl_profiles = (next.acl_profiles || []).filter(item => item.id !== profile.id); if (await saveSection("acl-profiles", next.acl_profiles || [])) { if (editing === profile.id && aclDialog?.open) aclDialog.close(); clear(); }
}));
head.append(actions); box.append(head);
const details = el("div", undefined, "vhost-detail-strip");
const countryMode = profile.country_access?.mode || "off";
for (const [key, value] of [
["aclDefault", t(profile.acl?.default === "deny" ? "deny" : "allow")],
["aclProfileIpRulesShort", rulesList.length ? rulesList.map(rule => `${t(rule.action === "deny" ? "aclDeny" : "aclAllow")} ${rule.network}`).join(", ") : "-"],
["vhCountryMode", countryMode === "off" || !countries.length ? "-" : `${t(countryMode === "block" ? "vhCountryBlock" : "vhCountryAllow")}: ${countries.join(", ")}`],
["aclProfileAttached", users.join(", ") || "-"]
]) { const cell = el("div", undefined, "vhost-detail"); const valueNode = el("b", value); valueNode.title = value; cell.append(el("span", t(key), "small-label"), valueNode); details.append(cell); }
box.append(details); return box;
});
list.replaceChildren(...(cards.length ? cards : [el("p", t(query && profiles.length ? "aclProfileSearchEmpty" : "aclProfilesEmpty"), "empty")]));
if (!dirty && editing) { const current = profiles.find(p => p.id === editing); if (current) edit(current); else clear(); }
}
form.addEventListener("input", event => { dirty = true; if (event.target !== rules) { syncRulesText(); syncPolicy(); } });
form.addEventListener("change", event => {
dirty = true;
if (event.target.matches('input[name="acl-profile-mode"]')) syncPolicy(true);
else if (event.target === countryEnabled || event.target === countryMode || event.target === aclDefault) syncPolicy();
else { syncRulesText(); syncPolicy(); }
});
ruleRows.addEventListener("input", () => { syncRulesText(); renderPreview(); }); ruleRows.addEventListener("change", () => { syncRulesText(); renderPreview(); });
addButton.addEventListener("click", () => { addRule(); syncPolicy(); }); importButton.addEventListener("click", importRules);
form.addEventListener("submit", async event => {
event.preventDefault(); if (!envelope || busy) return;
try {
await withFormSubmitState(form, event.submitter, async () => {
const id = $("acl-profile-id").value.trim(), name = $("acl-profile-name").value.trim(), mode = selectedMode();
const countries = [...new Set(split(countriesInput.value).map(v => v.toUpperCase()))];
if (countries.some(v => !/^[A-Z]{2}$/.test(v))) throw fieldError(countriesInput, t("aclProfileCountryInvalid"));
const cMode = activeCountryMode();
if (cMode !== "off" && !countries.length) throw fieldError(countriesInput, t("aclProfileCountryRequired"));
const visualText = collectRuleRows().map(rule => `${rule.action} ${rule.network}`.trimEnd()).join("\n");
if (normalizeAclRuleText(rules.value).trim() !== normalizeAclRuleText(visualText).trim()) {
const parsed = parseBulkRules(rules.value, rules);
if (mode !== "custom" && parsed.some(rule => rule.action !== guidedAction(mode))) { setMode("custom"); syncPolicy(); }
renderRuleRows(parsed);
}
const finalMode = selectedMode(), expectedAction = finalMode === "custom" ? null : guidedAction(finalMode);
const aclRules = collectRuleRows().map(rule => ({ ...rule, action: expectedAction || rule.action }));
if (aclRules.some(rule => rule.action === "deny" && ruleKind(rule.network) === "dns"))
throw fieldError(ruleRows, t("aclDnsAllowOnly"));
if (aclRules.some(rule => !rule.network)) { const input = [...ruleRows.querySelectorAll(".acl-profile-rule-network")].find(node => !node.value.trim()); throw fieldError(input || rules, t("ruleSyntax")); }
for (const [row, rule] of [...ruleRows.children].map((row, index) => [row, aclRules[index]])) row.querySelector(".acl-profile-rule-network").value = rule.network;
const finalDefault = finalMode === "custom" ? aclDefault.value : guidedDefault(finalMode);
const finalCountryMode = finalMode === "custom" ? countryMode.value : (countryEnabled.checked ? guidedCountryMode(finalMode) : "off");
const profile = { id, name, acl: { default: finalDefault, rules: aclRules }, country_access: { mode: finalCountryMode, countries } };
const next = structuredClone(envelope.config); next.acl_profiles ||= [];
const index = next.acl_profiles.findIndex(item => item.id === editing);
if (next.acl_profiles.some(item => item.id === id && item.id !== editing)) throw fieldError($("acl-profile-id"), t("aclProfileDuplicate")); if (index < 0) next.acl_profiles.push(profile); else next.acl_profiles[index] = profile;
if (await saveSection("acl-profiles", next.acl_profiles || [])) { dirty = false; editing = null; if (aclDialog?.open) aclDialog.close(); clear(); render(); }
});
} catch (error) { toast(error.message, true); if (error.fieldTarget?.focus) error.fieldTarget.focus(); }
});
const aclDialog = $("acl-profile-dialog");
function openEditor() { if (aclDialog && !aclDialog.open) aclDialog.showModal(); }
async function closeEditor() {
if (dirty && !(await appConfirm(t("aclProfileUnsaved"), { danger: true, confirmLabel: t("discardAction") }))) return;
dirty = false; if (aclDialog?.open) aclDialog.close(); clear();
}
$("acl-profile-new").addEventListener("click", () => { clear(); openEditor(); $("acl-profile-name").focus(); });
$("acl-profile-search")?.addEventListener("input", () => render());
$("acl-profile-reset").addEventListener("click", () => void closeEditor());
$("acl-profile-close")?.addEventListener("click", () => void closeEditor());
aclDialog?.addEventListener("cancel", event => { event.preventDefault(); void closeEditor(); });
window.rustProxyAclProfiles = { render, reset: () => { dirty = false; if (aclDialog?.open) aclDialog.close(); clear(); }, refreshTranslations };
clear(); if (envelope) render();
})();