Files
opencode2api/webui/js/pages/config.js
T
Jason Xu b8b1454a0e fix(warp): rotate exits on a live pool and size it to concurrency
The WARP pool could never rotate: its lifetime context was derived from the
startup deadline, so every standby registration and replacement died with
"context canceled" and requests stayed on the first egress forever. Give the
first registration the caller's deadline and the pool a context that only
Close cancels.

- warp.pool_size is an upper bound. The active ring follows concurrency (one
  exit per lease, one per queued request, plus a spare) and trims idle surplus
  after 60 seconds, so a burst expands it and quiet periods shrink it.
- An IPv6 exit is deduplicated and cooled by its /64 for 30 minutes (IPv4 by
  its /32), since the free quota is scoped to that block.
- 429/403 retires an exit immediately and re-registers right away; a free
  model request keeps switching exits and only returns the last 429 after
  retry.max_attempts, on both the anonymous and the Zen-key lane. 429s no
  longer trip the anonymous breaker or cool the key.
- Pin HTTP/1.1 ALPN and TLSNextProto on the WARP transport: a clone of
  DefaultTransport made after its first dial negotiated h2 and fed SETTINGS
  frames to the HTTP/1.x reader, breaking catalog refresh.
- Default to the IPv6 stack and, with warp.source_ipv6, let each exit bind its
  own outer source from a routed prefix via IPV6_FREEBIND. Endpoints try IPv6
  first and fall back to IPv4, so IPv4-only hosts keep working.
- WebUI, config example and all READMEs describe the new pool and rotation
  behaviour.
2026-10-25 06:49:00 +08:00

231 lines
8.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Page: 配置中心 — masked config editing with validation, hot reload and reveal.
import { api } from "../api.js";
import { loadConfig, loadDebugModels } from "../data.js";
import * as store from "../store.js";
import { t, onChange } from "../i18n.js";
import { $, $$, bindTabs, el, openDialog, toast } from "../ui.js";
function renderChips(name) {
const host = $(`#${name}-chips`);
host.replaceChildren();
(store.get("config")?.[name] || []).forEach((item) => {
const remove = el("button", { type: "button", title: t("移除", "common.remove"), text: "×" });
const chip = el("span", { class: "chip", dataset: { id: item.id } });
remove.addEventListener("click", () => chip.remove());
chip.append(el("span", { text: item.display }), remove);
host.append(chip);
});
}
function renderRestartNotice(value) {
const notice = $("#restart-notice");
if (value?.restart_required_fields?.length) {
notice.textContent = t(
`已保存但尚未生效:${value.restart_required_fields.join(", ")}。当前 API ${value.effective.listen}、WebUI ${value.effective.webui_listen}。请重启进程。`,
"config.restartNotice",
value.restart_required_fields.join(", "),
value.effective.listen,
value.effective.webui_listen,
);
notice.classList.remove("hidden");
} else {
notice.classList.add("hidden");
}
}
function secretInputs(name) {
const kept = [...$(`#${name}-chips`).children].map((node) => ({ id: node.dataset.id }));
const added = $(`#${name}-new`)
.value.split(/\r?\n/)
.map((value) => value.trim())
.filter(Boolean)
.map((value) => ({ value }));
return kept.concat(added);
}
const numberValue = (selector) => Number($(selector).value);
function fillConfig(value) {
if (!value) return;
$("#c-listen").value = value.listen;
$("#c-prefer").value = value.prefer;
$("#c-web-listen").value = value.webui.listen;
$("#c-session").value = value.webui.session_ttl_minutes;
$("#c-web-enabled").checked = value.webui.enabled;
$("#c-anonymous").checked = !!value.anonymous;
const warp = value.warp || {};
$("#c-warp-enabled").checked = !!warp.enabled;
$("#c-warp-pool").value = warp.pool_size || 2;
$("#c-warp-stack").value = warp.stack || "ipv6";
$("#c-warp-source").value = warp.source_ipv6 || "";
$("#c-proxyfile").value = value.proxyfile;
$("#c-up-zen").value = value.upstream.zen;
$("#c-up-go").value = value.upstream.go;
$("#c-attempts").value = value.retry.max_attempts;
$("#c-timeout").value = value.retry.timeout_seconds;
$("#c-refresh").value = value.models.refresh_seconds;
$("#c-protocols").value = JSON.stringify(value.models.protocols || {}, null, 2);
$("#c-free-only").checked = !!value.models.free_only;
const reasoning = value.reasoning || {};
$("#c-reasoning-effort").value = reasoning.effort || "";
$("#c-reasoning-by-model").value = JSON.stringify(reasoning.effort_by_model || {}, null, 2);
$("#c-idle").value = value.performance.max_idle_conns;
$("#c-idle-host").value = value.performance.max_idle_conns_per_host;
$("#c-max-host").value = value.performance.max_conns_per_host;
$("#c-idle-timeout").value = value.performance.idle_conn_timeout_seconds;
$("#c-connect").value = value.performance.connect_timeout_seconds;
$("#c-cooldown").value = value.performance.failure_cooldown_seconds;
$("#c-attempt-timeout").value = value.performance.attempt_timeout_seconds;
$("#c-first-event-timeout").value = value.performance.first_event_timeout_seconds || 0;
$("#c-level").value = value.logging.level;
$("#c-ring").value = value.logging.ring_size;
$("#c-dump-bodies").checked = !!value.logging.dump_request_bodies;
["server_keys", "zen_keys", "go_keys", "proxies"].forEach(renderChips);
renderRestartNotice(value);
}
async function save(event) {
event.preventDefault();
const current = store.get("config");
if (!current) return;
try {
const update = {
listen: $("#c-listen").value,
server_keys: secretInputs("server_keys"),
zen_keys: secretInputs("zen_keys"),
go_keys: secretInputs("go_keys"),
anonymous: $("#c-anonymous").checked,
warp: {
enabled: $("#c-warp-enabled").checked,
pool_size: numberValue("#c-warp-pool"),
stack: $("#c-warp-stack").value,
source_ipv6: $("#c-warp-source").value.trim(),
},
proxies: secretInputs("proxies"),
proxyfile: $("#c-proxyfile").value,
prefer: $("#c-prefer").value,
upstream: { zen: $("#c-up-zen").value, go: $("#c-up-go").value },
retry: { max_attempts: numberValue("#c-attempts"), timeout_seconds: numberValue("#c-timeout") },
models: {
refresh_seconds: numberValue("#c-refresh"),
protocols: JSON.parse($("#c-protocols").value || "{}"),
free_only: $("#c-free-only").checked,
},
reasoning: {
effort: $("#c-reasoning-effort").value,
effort_by_model: JSON.parse($("#c-reasoning-by-model").value || "{}"),
},
performance: {
max_idle_conns: numberValue("#c-idle"),
max_idle_conns_per_host: numberValue("#c-idle-host"),
max_conns_per_host: numberValue("#c-max-host"),
idle_conn_timeout_seconds: numberValue("#c-idle-timeout"),
connect_timeout_seconds: numberValue("#c-connect"),
failure_cooldown_seconds: numberValue("#c-cooldown"),
attempt_timeout_seconds: numberValue("#c-attempt-timeout"),
first_event_timeout_seconds: numberValue("#c-first-event-timeout"),
},
logging: {
level: $("#c-level").value,
ring_size: numberValue("#c-ring"),
dump_request_bodies: $("#c-dump-bodies").checked,
},
webui: {
enabled: $("#c-web-enabled").checked,
listen: $("#c-web-listen").value,
username: current.webui.username,
session_ttl_minutes: numberValue("#c-session"),
},
};
const result = await api("/api/config", { method: "PUT", body: JSON.stringify(update) });
store.set("config", result.config);
["server_keys", "zen_keys", "go_keys", "proxies"].forEach(
(name) => ($(`#${name}-new`).value = ""),
);
await loadDebugModels();
toast(
result.result.restart_required
? t(
`已应用;${result.result.restart_fields.join(", ")} 需重启`,
"toast.config.restart",
result.result.restart_fields.join(", "),
)
: t("配置已保存并立即应用", "toast.config.saved"),
);
} catch (error) {
toast(error.message, true);
}
}
async function reload() {
try {
const result = await api("/api/config/reload", { method: "POST", body: "{}" });
await loadConfig();
await loadDebugModels();
toast(
result.restart_required
? t("磁盘配置已重载,监听字段需重启", "toast.config.reloadedRestart")
: t("磁盘配置已重载", "toast.config.reloaded"),
);
} catch (error) {
toast(error.message, true);
}
}
function reveal() {
const input = el("input", { type: "password", autocomplete: "current-password" });
const submit = async (close) => {
try {
const values = await api("/api/config/reveal", {
method: "POST",
body: JSON.stringify({ password: input.value }),
});
close();
openDialog({
title: t("敏感配置", "modal.title"),
size: "dialog-lg",
body: el("pre", { text: JSON.stringify(values, null, 2) }),
actions: [{ label: t("关闭", "modal.close") }],
});
} catch (error) {
toast(error.message, true);
}
};
const dialog = openDialog({
title: t("查看敏感配置", "modal.revealTitle"),
body: el("div", { class: "field" }, [
el("label", { text: t("请再次输入管理密码", "config.revealPrompt") }),
input,
]),
actions: [
{ label: t("取消", "common.cancel") },
{ label: t("查看", "common.view"), kind: "btn-primary", onClick: submit },
],
});
input.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault();
submit(dialog.close);
}
});
}
export default {
id: "config",
title: "配置中心",
sub: "验证后原子保存,并热应用可变字段。",
init() {
store.on("config", fillConfig);
bindTabs($("#page-config"));
$("#config-form").addEventListener("submit", save);
$("#reload-config").addEventListener("click", reload);
$("#reveal").addEventListener("click", reveal);
onChange(() => {
const value = store.get("config");
if (value) renderRestartNotice(value);
// Update chip tooltips in place so pending unsaved chip removals survive a language switch.
$$(".chips .chip button").forEach((button) => (button.title = t("移除", "common.remove")));
});
},
};