Files
magpie/internal/gui/proxies.go
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

248 lines
6.4 KiB
Go

package gui
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/netproxy"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// Named proxies (the Network page's list): kept once, chosen by reference
// wherever a proxy can be chosen. The page is sent the address without its
// login, the username and whether a password is set; the password is
// written and never read back, as the GitHub token is.
// savedProxyJSON is a saved proxy as the page sees it.
type savedProxyJSON struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
User string `json:"user,omitempty"`
HasPass bool `json:"hasPass,omitempty"`
// Uses is where it is chosen: Settings' own two, then providers,
// accounts and keys
Uses []proxyUseJSON `json:"uses,omitempty"`
Check *proxyTestJSON `json:"check,omitempty"`
}
type proxyUseJSON struct {
Where string `json:"where,omitempty"` // "global" or "plugins"; else a provider
Provider string `json:"provider,omitempty"`
Account string `json:"account,omitempty"`
}
// proxyTestJSON is one connectivity check: whether a request got through
// the proxy, how long it took, and where it left from.
type proxyTestJSON struct {
OK bool `json:"ok"`
Ms int64 `json:"ms,omitempty"`
IP string `json:"ip,omitempty"`
Loc string `json:"loc,omitempty"`
Error string `json:"error,omitempty"`
At int64 `json:"at"`
}
// proxyTestClient makes the check's request; a test swaps its own.
var proxyTestClient = http.DefaultClient
var (
proxyTestsMu sync.Mutex
proxyTests = map[string]proxyTestJSON{}
)
func savedProxies() []savedProxyJSON {
cur := settings.Load()
var out []savedProxyJSON
proxyTestsMu.Lock()
defer proxyTestsMu.Unlock()
for _, p := range cur.Proxies {
ref := settings.ProxyRef(p.ID)
row := savedProxyJSON{ID: p.ID, Name: p.Name, URL: p.URL, User: p.User, HasPass: p.Pass != ""}
if strings.TrimSpace(cur.Proxy) == ref {
row.Uses = append(row.Uses, proxyUseJSON{Where: "global"})
}
if strings.TrimSpace(cur.PluginProxy) == ref {
row.Uses = append(row.Uses, proxyUseJSON{Where: "plugins"})
}
for _, u := range provider.ProxyUses(p.ID) {
row.Uses = append(row.Uses, proxyUseJSON{Provider: u.Provider, Account: u.Account})
}
if c, ok := proxyTests[p.ID]; ok {
c := c
row.Check = &c
}
out = append(out, row)
}
return out
}
// proxyIn is what the page sends to save or test a proxy. A nil Pass keeps
// the saved password (an edit that left it blank).
type proxyIn struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
User string `json:"user"`
Pass *string `json:"pass"`
}
func (in proxyIn) named(cur []settings.NamedProxy) settings.NamedProxy {
p := settings.NamedProxy{ID: strings.TrimSpace(in.ID), Name: in.Name, URL: in.URL, User: strings.TrimSpace(in.User)}
if in.Pass != nil {
p.Pass = *in.Pass
} else {
for _, c := range cur {
if c.ID == p.ID {
p.Pass = c.Pass
}
}
}
return p
}
func handleProxySave(rw http.ResponseWriter, r *http.Request) {
var in proxyIn
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
s := settings.Load()
p := in.named(s.Proxies)
found := false
if p.ID == "" {
p.ID = settings.NewProxyID()
if in.Pass == nil {
p.Pass = ""
}
}
for i := range s.Proxies {
if s.Proxies[i].ID == p.ID {
s.Proxies[i], found = p, true
}
}
if !found {
s.Proxies = append(s.Proxies, p)
}
if err := settings.Save(s); err != nil {
fail(rw, err)
return
}
provider.ProxyEdited() // Claude Code's env holds an address, not the name
// what it was checked as no longer says anything about it
proxyTestsMu.Lock()
delete(proxyTests, p.ID)
proxyTestsMu.Unlock()
writeJSON(rw, settingsState())
}
func handleProxyDelete(rw http.ResponseWriter, r *http.Request) {
var in struct {
ID string `json:"id"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
s := settings.Load()
var keep []settings.NamedProxy
for _, p := range s.Proxies {
if p.ID != in.ID {
keep = append(keep, p)
}
}
if len(keep) == len(s.Proxies) {
writeJSON(rw, settingsState())
return
}
for _, row := range savedProxies() {
if row.ID == in.ID && len(row.Uses) > 0 {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusConflict)
_ = json.NewEncoder(rw).Encode(map[string]any{"error": "the proxy is in use", "uses": row.Uses})
return
}
}
s.Proxies = keep
if err := settings.Save(s); err != nil {
fail(rw, err)
return
}
proxyTestsMu.Lock()
delete(proxyTests, in.ID)
proxyTestsMu.Unlock()
writeJSON(rw, settingsState())
}
// handleProxyTest asks the trace page through one saved proxy (by id), or
// through one being typed (its address and login, unsaved).
func handleProxyTest(rw http.ResponseWriter, r *http.Request) {
var in proxyIn
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
cur := settings.Load().Proxies
var p settings.NamedProxy
if strings.TrimSpace(in.ID) != "" {
for _, c := range cur {
if c.ID == strings.TrimSpace(in.ID) {
p = c
}
}
if p.ID == "" {
fail(rw, fmt.Errorf("the saved proxy isn't there"))
return
}
if strings.TrimSpace(in.URL) != "" { // an edit not yet saved
p = in.named(cur)
}
} else {
p = in.named(cur)
p.Name = "test"
}
addr, err := p.Address()
if err != nil {
fail(rw, err)
return
}
// a typed address is checked as saving would
if err := settings.CheckProxy(addr); err != nil {
fail(rw, err)
return
}
out := testProxy(r.Context(), addr)
if in.ID != "" && strings.TrimSpace(in.URL) == "" {
proxyTestsMu.Lock()
proxyTests[p.ID] = out
proxyTestsMu.Unlock()
}
writeJSON(rw, out)
}
func testProxy(ctx context.Context, addr string) proxyTestJSON {
leg := throughLeg(netproxy.With(ctx, addr), proxyTestClient)
out := proxyTestJSON{Ms: leg.Ms, IP: leg.IP, Loc: leg.Loc, At: time.Now().Unix()}
switch {
case leg.Error != "":
out.Error = redactProxyErr(leg.Error)
case !leg.Proxied:
out.Error = "the request didn't go to the proxy"
default:
out.OK = true
}
return out
}
func proxyRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /api/proxies/save", handleProxySave)
mux.HandleFunc("POST /api/proxies/delete", handleProxyDelete)
mux.HandleFunc("POST /api/proxies/test", handleProxyTest)
}