Files
RustProxy/src/admin/web/tls-configurator.js
T
2026-11-28 15:54:56 +01:00

169 lines
11 KiB
JavaScript

window.rustProxyTlsConfigurator = (() => {
"use strict";
const FALLBACK = [
"TLS13_AES_256_GCM_SHA384", "TLS13_AES_128_GCM_SHA256", "TLS13_CHACHA20_POLY1305_SHA256",
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
];
const CLIENTS = [
{ id: "modern", tls13: true, tls12: () => true },
{ id: "safari_new", tls13: true, tls12: () => true },
{ id: "safari_old", tls13: false, tls12: suite => suite.gcm },
{ id: "android_7", tls13: false, tls12: () => true },
{ id: "android_44", tls13: false, tls12: suite => suite.gcm },
{ id: "java_11", tls13: true, tls12: () => true },
{ id: "java_8", tls13: false, tls12: suite => suite.gcm },
{ id: "openssl_111", tls13: true, tls12: () => true },
{ id: "openssl_102", tls13: false, tls12: suite => suite.gcm },
{ id: "ie11_win10", tls13: false, tls12: suite => suite.gcm },
{ id: "ie11_win7", tls13: false, tls12: suite => suite.gcm && suite.auth === "ECDSA", ecdsa: true },
{ id: "legacy", impossible: true },
];
const PRESETS = {
modern: { tls12: false, tls13: true, suites: [] },
recommended: { tls12: true, tls13: true, suites: [] },
strong: { tls12: true, tls13: true, suites: names => names.filter(name => /AES_256|CHACHA20/.test(name)) },
};
function meta(name) {
const tls13 = name.startsWith("TLS13_");
const auth = tls13 ? "*" : name.includes("_ECDSA_") ? "ECDSA" : name.includes("_RSA_") ? "RSA" : "*";
const cipher = name.includes("CHACHA20") ? "ChaCha20-Poly1305" : name.includes("AES_256_GCM") ? "AES-256-GCM" : name.includes("AES_128_GCM") ? "AES-128-GCM" : name;
const bits = cipher === "AES-128-GCM" ? 128 : 256;
return { name, tls13, auth, cipher, bits, gcm: cipher.includes("GCM"), hash: (name.match(/SHA(\d+)$/) || [])[1] ? `SHA-${name.match(/SHA(\d+)$/)[1]}` : "" };
}
function attach() {
const textarea = $("tls-policy-ciphers"), box12 = $("tls-policy-12"), box13 = $("tls-policy-13"), pqc = $("tls-policy-pqc");
if (!textarea || textarea.dataset.tlsConfigurator) return null;
textarea.dataset.tlsConfigurator = "1";
for (const id of ["tls-policy-effective", "tls-policy-available"]) { const node = $(id); if (node) node.hidden = true; }
const holder = textarea.closest("label"); if (holder) holder.hidden = true; else textarea.hidden = true;
const root = el("div", undefined, "tls-configurator");
(holder || textarea).after(root);
const presets = el("div", undefined, "tls-config-presets");
const i18n = (node, key) => { node.dataset.i18n = key; return node; };
const presetButtons = Object.keys(PRESETS).map(id => { const key = `tlsPreset${id[0].toUpperCase()}${id.slice(1)}`, button = i18n(el("button", t(key), "outline"), key); button.type = "button"; button.dataset.preset = id; button.title = t(`${key}Help`); button.dataset.i18nTitle = `${key}Help`; return button; });
const custom = i18n(el("span", t("tlsPresetCustom"), "pill tls-config-custom"), "tlsPresetCustom");
presets.append(i18n(el("span", t("tlsPresets"), "small-label"), "tlsPresets"), ...presetButtons, custom);
const layout = el("div", undefined, "tls-config-layout");
const tableWrap = el("div", undefined, "tls-config-suites"), side = el("div", undefined, "tls-config-side");
layout.append(tableWrap, side);
root.append(presets, layout, i18n(el("p", t("tlsConfiguratorHelp"), "fine"), "tlsConfiguratorHelp"));
const available = () => { const list = latestStatus?.tls?.cipher_suites; return Array.isArray(list) && list.length ? list : FALLBACK; };
const selected = () => String(textarea.value || "").split(/[\n,]+/).map(value => value.trim()).filter(Boolean);
const effective = () => { const names = available(), chosen = selected(); return (chosen.length ? chosen : names).filter(name => names.includes(name)); };
const write = suites => {
const names = available(), all = names.every(name => suites.includes(name));
const value = all ? "" : names.filter(name => suites.includes(name)).join("\n");
if (value === textarea.value) return;
textarea.value = value;
textarea.dispatchEvent(new Event("input", { bubbles: true }));
};
function suiteTable() {
const names = available(), active = new Set(effective());
const table = el("table", undefined, "tls-config-table");
const head = el("thead"), headRow = el("tr");
for (const key of ["", "tlsSuite", "tlsSuiteAuth", "tlsSuiteCipher", "tlsSuiteBits", "tlsSuiteRating"]) headRow.append(el("th", key ? t(key) : ""));
head.append(headRow); table.append(head);
const body = el("tbody");
for (const [label, version, enabled] of [["TLS 1.3", true, box13.checked], ["TLS 1.2", false, box12.checked]]) {
const group = el("tr", undefined, "tls-config-group"), cell = el("td"); cell.colSpan = 6;
cell.append(el("strong", label), el("span", enabled ? t("tlsVersionOn") : t("tlsVersionOff"), `pill ${enabled ? "live" : ""}`.trim()));
group.append(cell); body.append(group);
for (const name of names.filter(item => meta(item).tls13 === version)) {
const info = meta(name), row = el("tr"), check = el("input", undefined, "switch"); check.type = "checkbox"; check.checked = active.has(name); check.disabled = !enabled; check.dataset.suite = name;
check.setAttribute("aria-label", name);
const first = el("td"); first.append(check);
const nameCell = el("td"); nameCell.append(el("code", name));
const rating = info.bits === 256 ? ["tlsRatingStrong", "live"] : ["tlsRatingRecommended", ""];
const ratingCell = el("td"); ratingCell.append(el("span", t(rating[0]), `pill ${rating[1]}`.trim()));
row.classList.toggle("is-off", !enabled || !check.checked);
row.append(first, nameCell, el("td", info.auth === "*" ? t("tlsSuiteAnyCert") : info.auth), el("td", info.cipher), el("td", String(info.bits)), ratingCell);
body.append(row);
}
}
table.append(body);
table.addEventListener("change", event => {
if (!event.target.matches("input[data-suite]")) return;
const chosen = [...table.querySelectorAll("input[data-suite]:checked")].map(input => input.dataset.suite);
const off = [...table.querySelectorAll("input[data-suite]:disabled")].filter(input => active.has(input.dataset.suite)).map(input => input.dataset.suite);
write([...new Set([...chosen, ...off])]);
render();
});
return table;
}
function grade(suites) {
const hsts = envelope?.config?.global_security?.security_headers?.hsts?.enabled === true;
const tls13 = box13.checked && suites.some(name => meta(name).tls13), tls12 = box12.checked && suites.some(name => !meta(name).tls13);
const weakest = Math.min(...suites.filter(name => (meta(name).tls13 ? box13.checked : box12.checked)).map(name => meta(name).bits), 256);
const score = { protocol: tls13 ? (tls12 ? 95 : 100) : 90, exchange: pqc.checked ? 100 : 90, cipher: weakest >= 256 ? 100 : 90 };
const letter = !tls13 && !tls12 ? "-" : tls13 && hsts ? "A+" : "A";
return { letter, score, hsts, tls13, tls12 };
}
function clientState(client, suites) {
if (client.impossible) return "impossible";
const metas = suites.map(meta);
if (client.tls13 && box13.checked && metas.some(item => item.tls13)) return "ok";
if (box12.checked && metas.some(item => !item.tls13 && client.tls12(item))) return client.ecdsa ? "ecdsa" : "ok";
return "fail";
}
function summary() {
const suites = effective(), result = grade(suites);
const card = el("div", undefined, "tls-config-grade");
card.append(el("strong", result.letter, `tls-config-letter ${result.letter === "-" ? "is-bad" : ""}`.trim()));
const bars = el("div", undefined, "tls-config-bars");
for (const [key, value] of [["tlsScoreProtocol", result.score.protocol], ["tlsScoreKeyExchange", result.score.exchange], ["tlsScoreCipher", result.score.cipher]]) {
const bar = el("div", undefined, "tls-config-bar"); const meter = el("i", undefined, "pr-meter"), fill = el("b"); fill.style.width = `${value}%`; meter.append(fill);
bar.append(el("span", t(key)), meter, el("b", String(value))); bars.append(bar);
}
card.append(bars);
const checks = el("ul", undefined, "tls-config-checks");
const item = (ok, key) => { const li = el("li", t(key), ok ? "is-ok" : "is-warn"); li.prepend(el("span", ok ? "✓" : "!", "tls-config-mark")); checks.append(li); };
item(true, "tlsCheckForwardSecrecy"); item(true, "tlsCheckAead"); item(true, "tlsCheckNoLegacy");
item(result.tls13, result.tls13 ? "tlsCheckTls13" : "tlsCheckNoTls13");
item(pqc.checked, pqc.checked ? "tlsCheckPqc" : "tlsCheckNoPqc");
item(result.hsts, result.hsts ? "tlsCheckHsts" : "tlsCheckNoHsts");
const compat = el("div", undefined, "tls-config-compat");
compat.append(el("span", t("tlsCompatibility"), "small-label"));
const list = el("ul");
for (const client of CLIENTS) {
const state = clientState(client, suites), li = el("li", undefined, `is-${state}`);
li.append(el("span", { ok: "✓", ecdsa: "!", fail: "✕", impossible: "✕" }[state], "tls-config-mark"), el("span", t(`tlsClient_${client.id}`)));
if (state !== "ok") li.append(el("small", t(`tlsClientState_${state}`), "fine"));
list.append(li);
}
compat.append(list);
return [card, compat, checks, el("p", t("tlsGradeNote"), "fine")];
}
function syncPresetState() {
const chosen = selected(), names = available();
let match = "";
for (const [id, preset] of Object.entries(PRESETS)) {
const suites = typeof preset.suites === "function" ? preset.suites(names) : preset.suites;
const sameSuites = suites.length ? suites.length === chosen.length && suites.every(name => chosen.includes(name)) : !chosen.length;
if (sameSuites && preset.tls12 === box12.checked && preset.tls13 === box13.checked) match = id;
}
for (const button of presetButtons) { const on = button.dataset.preset === match; button.classList.toggle("primary", on); button.classList.toggle("outline", !on); button.setAttribute("aria-pressed", String(on)); }
custom.hidden = !!match;
}
function render() {
tableWrap.replaceChildren(suiteTable());
side.replaceChildren(...summary());
syncPresetState();
}
for (const button of presetButtons) button.addEventListener("click", () => {
const preset = PRESETS[button.dataset.preset], names = available();
box12.checked = preset.tls12; box13.checked = preset.tls13;
if (!preset.tls13) pqc.checked = false;
const suites = typeof preset.suites === "function" ? preset.suites(names) : preset.suites;
textarea.value = suites.length ? suites.join("\n") : "";
textarea.dispatchEvent(new Event("input", { bubbles: true }));
render();
});
for (const box of [box12, box13, pqc]) box.addEventListener("change", render);
render();
return { refresh: render };
}
let instance = null;
return { refresh() { if (!instance) instance = attach(); else instance.refresh(); }, meta };
})();