Files
magpie/internal/gui/assets/proxies.js
T
yetone 58fb070953 settings: named proxies, chosen as inherit, direct or a saved one anywhere a proxy is (@tsingchao on X)
Settings > Network keeps named proxies (name, address, optional login)
with a Test that says reachable and the latency. Everywhere a proxy can be
chosen (global, plugin downloads, a provider, a key, a Claude account) now
has a Saved mode beside the others; the value is "@<id>". Addresses typed
before are read exactly as before, nothing is migrated or overridden.
netproxy.Parse/Resolve/Redact/EnvWith/Dispatch resolve a reference at the
lowest layer, so a proxy edited once reaches the gateway, usage reads,
sign-ins and the plugin host at the next request; Claude Code's
settings.json env (a snapshot) is rewritten by provider.ProxyEdited.
A reference this computer lacks fails closed (DeadProxy), says the proxy's
name in the request error and as "Proxy missing: {name}" on the provider
row, and never goes direct. The password lives in settings.json (0600), is
never read back by the page, logged, or put in Settings sync. The list
rides with the providers part of a sync (Bundle.Proxies, MergeProxies by
id, passwords only with keys). A proxy in use can't be deleted (409).
The saved list's page code is in proxies.js, loaded when Settings draws it;
app.js grew by Saved-mode helpers only, paid for by 16 long comments cut
(core budget unchanged, no raise).

Run (mote refused ssh all session, so Go and GUI went through win-run on
xiaomi-gpu, Windows 11; gofmt and darwin/linux-nogui/windows go vet were
clean on mote before it went down, at the pre-sync tree):
- go test settings, netproxy, backup, settingsapi, davsync: ok on Windows
  (earlier on mote: also gui, provider, plugin with real Bun, gateway).
- saved-proxies.test.cjs 20 of 20 (chromium+webkit, en zh zh-TW ja de,
  420px) twice more on Windows; provider-proxy-missing.test.cjs 20 of 20.
  One webkit de 420px run of saved-proxies failed once under 4-way
  concurrency and passed 3 of 3 alone; not reproduced.
- TestCoreScriptBudget ok after the move (it was 3.4KB over before).
Red on this tree and on origin/main alike (Windows run, not mine):
settings TestSharedCheckedReadsOnceAndKnowsUnknown; provider
Codex keychain/Git tests; plugin TestHostRunsAsMagpieBun, TestPiPackage;
gui TestGatewayHeldByAContainer and others; old-webkit "compat.js is the
first" (headerpresets.js) and ":has() (Safari 15.4)" on origin/main 245f07b19.
Broken by hand: restore skipping Proxies (TestSavedProxiesTravelWithProviders
red), Parse returning nil for a dangling ref (netproxy tests red), the
Proxy-missing badge disabled (provider-proxy-missing 0 of 20), and the Claude
env snapshot failing open (found by reading, test added).
Not tried: real vendors, the real Claude Code, a real two-computer sync, the
macOS suite and the whole GUI suite on this final tree (mote down; the whole
suite ran to test ~3700 on the earlier tree with reds owned by others).
A proxy missing whose sender never had it is named by its id.
2026-11-11 12:26:43 +08:00

90 lines
4.2 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.
// Settings › Network's saved proxies: their rows, the test and the form.
// app.js imports this when Settings draws them, so it weighs nothing on the core.
let proxyForm = null; // { id, name, url, user, hasPass, err } while one is added or edited
const proxyChecks = {}; // id -> { busy, out }
window.renderSavedProxies = function (s) {
const box = $("#proxiesList");
box.replaceChildren();
const done = (ns) => { prefs = ns; proxyForm = null; renderSettings(); };
for (const p of s.proxies || []) {
if (proxyForm && proxyForm.id === p.id) { box.append(proxyFormRow(done)); continue; }
const r = el("div", "row pref saved-proxy");
const who = el("div", "who");
const sub = el("div", "sub");
who.append(el("div", "name", p.name), sub);
const chk = proxyChecks[p.id];
const out = chk?.out || p.check;
const line = [p.user ? p.user + "@" + p.url : p.url];
if (chk?.busy) line.push(t("Testing…"));
else if (out) line.push(out.ok ? t("Reachable, {ms} ms", { ms: out.ms }) : t("Unreachable: {error}", { error: out.error || "" }));
if (p.uses?.length) line.push(t("Used by {n}", { n: p.uses.length }));
sub.textContent = sub.title = line.join(" · ");
sub.classList.toggle("err", !!out && !out.ok && !chk?.busy);
const val = el("div", "val");
const test = el("button", "text", chk?.busy ? t("Testing…") : t("Test"));
test.disabled = !!chk?.busy;
test.onclick = () => {
proxyChecks[p.id] = { busy: true };
renderSettings();
api("proxies/test", { id: p.id })
.then((o) => { proxyChecks[p.id] = { out: o }; })
.catch((e) => { proxyChecks[p.id] = { out: { ok: false, error: t(e.message) } }; })
.finally(renderSettings);
};
const edit = el("button", "text", t("Edit"));
edit.onclick = () => { proxyForm = { id: p.id, name: p.name, url: p.url, user: p.user || "", hasPass: !!p.hasPass }; renderSettings(); };
const del = el("button", "text", t("Delete"));
del.disabled = !!p.uses?.length;
del.onclick = () => writingPrefs(api("proxies/delete", { id: p.id })).then(done).catch(statusTErr);
val.append(test, edit, del);
r.append(who, val);
box.append(r);
}
if (proxyForm && !proxyForm.id) box.append(proxyFormRow(done));
else if (!proxyForm) {
const r = el("div", "row pref");
const add = el("button", "text", t("Add proxy"));
add.onclick = () => { proxyForm = { id: "", name: "", url: "", user: "", hasPass: false }; renderSettings(); };
r.append(el("div", "who", ""), el("div", "val"));
r.lastChild.append(add);
box.append(r);
}
};
function proxyFormRow(done) {
const f = proxyForm;
const r = el("div", "row pref proxy-form");
const name = input(f.name, t("Name"));
const url = input(f.url, "http://127.0.0.1:7890");
const user = input(f.user, t("Username (optional)"));
const pass = input("", f.hasPass ? "••••••••" : t("Password (optional)"), "password");
name.setAttribute("aria-label", t("Name"));
url.setAttribute("aria-label", t("Proxy address"));
user.setAttribute("aria-label", t("Username (optional)"));
pass.setAttribute("aria-label", t("Password (optional)"));
const err = el("div", "sub err", f.err || "");
const save = el("button", "text", t("Save"));
const cancel = el("button", "text", t("Cancel"));
const body = () => {
const b = { id: f.id, name: name.value.trim(), url: url.value.trim(), user: user.value.trim() };
if (pass.value) b.pass = pass.value; // blank keeps the saved password
return b;
};
save.onclick = () => {
if (!name.value.trim() || !url.value.trim()) return (name.value.trim() ? url : name).focus();
const b = body();
Object.assign(f, { name: b.name, url: b.url, user: b.user });
writingPrefs(api("proxies/save", b)).then(done).catch((e) => { f.err = t(e.message); renderSettings(); });
};
cancel.onclick = () => { proxyForm = null; renderSettings(); };
for (const x of [name, url, user, pass]) {
x.className = "proxy-credential";
x.onkeydown = (e) => { e.stopPropagation(); if (e.key === "Enter") save.onclick(); else if (e.key === "Escape") cancel.onclick(); };
}
const val = el("div", "proxy-controls");
val.append(name, url, user, pass, cancel, save);
r.append(val, err);
err.hidden = !f.err;
return r;
}