Files
magpie/internal/netproxy/deadsystem_test.go
T
yetone a4bf1a039e netproxy: Auto goes direct while the system's loopback proxy isn't running (@bitssly on X)
@bitssly (X, magpie 0.1.4519, Windows 11 amd64): with the proxy on Auto,
every model failed with 502 "proxy not reachable" once their VPN app was
off, a restart of magpie too, until they set the proxy Off. A VPN or
proxy app that quits without clearing Internet Options leaves
ProxyEnable=1 and ProxyServer=127.0.0.1:port in HKCU, and Auto took the
system proxy as it was: every request went to a port nothing listens on.

Auto now takes a system proxy on loopback only while its port accepts a
connection (liveSystem, through the same listening check the login
shell's proxy already had: 300ms dial, kept 15s while up, 2s while down,
so the VPN coming back is followed within 2 seconds). Every route that
asks Auto goes through it: For (netproxy.Func for the gateway and every
transport, and http.DefaultClient through Install), EnvWith (the CLIs
magpie starts, Bun, and the plugin host, whose proxy check every 15s/2s
restarts it on a change) and Describe. Settings › Network says it:
source "down" reads "Nothing answers at the proxy found, {proxy};
requests go direct until it runs again", in every language.

Not changed, on purpose:
- A proxy set in magpie is never gone around: a dead one still fails.
- The environment's *_PROXY, set for this process by whoever started
  it, is taken as it is, dead or not.
- A system proxy elsewhere than loopback isn't probed: a proxy that is
  down can't be told from a network that drops a probe.
- internal/agent/codex_proxy.go reads the raw System(): it predicts what
  Codex itself does.
- Going direct while the VPN is off means vendors see the computer's own
  address. A user who needs to fail closed sets the proxy in magpie.

Run:
- win-run (xiaomi-gpu, Windows 11) go test ./internal/netproxy: ok.
  TestWindowsDeadSystemProxyGoesDirect writes ProxyEnable/ProxyServer to
  a scratch key that RegOverridePredefKey puts in place of HKCU, so the
  box's own Internet Options are untouched. It reads them through the
  real system() and the real 300ms dial: direct while nothing listens,
  the proxy again once a listener runs.
- win-run go test ./internal/plugin: TestPiPackage,
  TestCachedReadsSavedProvidersOnce (#7197), TestDistReadFailedIsntMissing
  (#6075) and TestPluginLeavesTheUserNetworkDirect (#4115) are red on
  Windows at origin/main 239da9991 too; the last two pass 3 of 3 here.
- wsl-run (magpie-desktop) go test -timeout 45m ./... at ac4da60cb
  (these two commits on origin/main 9bf5848c1): ok except
  TestRequestStateKeptAcrossACatalogFetchedAgain (#6572, red at
  origin/main too), TestSearchQueueKeepsCodexAlive/cancel (#7090) and
  the root, cli and gui packages, which need gtk4 there.
- At the pushed head (rebased on e4e9eb092), mote-run go test
  -timeout 45m of netproxy, gui, plugin, gateway, usage, provider and
  agent: ok except TestOpenCodeGoOwnFreeModelGetsNothingOfOpenCodes
  (#7117, red 3 of 3 at origin/main e4e9eb092 too) and two TempDir
  cleanup reds, TestSetPortServesWhenTheOldOneWasTaken (#7112) and
  TestRequestChunksFromDisk (#4390; 3 of 3 alone).
- go vet ./... and GOOS=windows go vet ./... on mote: ok; vet natively
  on Windows: ok; Linux vet of netproxy, plugin, gateway, agent and
  provider in WSL: ok. gofmt -l clean.
- GUI, wsl-run: proxy-down.test.cjs (5 languages, 1100 and 420px,
  chromium and webkit), i18n-keys and every *proxy* and settings-* file
  at ac4da60cb. Reds: settings-proxy-auth / settings-proxy-remember 4s
  clicks beside a Go suite (#7192, #5562; 30 of 30 alone),
  provider-proxy (#7100) and settings-update-source webkit, red the same
  at origin/main 9bf5848c1. At the pushed head proxy-down, i18n-keys,
  settings-search, settings-proxy-remember, proxy-gaps and
  proxy-own-wording: 94 of 94.

Broken by hand: liveSystem returning the system proxy as it is turns
TestDeadLoopbackSystemProxyGoesDirect red (WSL) and the Windows
registry test red (xiaomi-gpu).

Not tried: magpie.exe against the box's real Internet Options (shared
machine), which source the reporter's proxy was in (registry or env), and
whether a connection dialled direct is kept after the VPN comes back
(#4523 is that class for a changed proxy).
2026-11-19 18:04:19 +08:00

111 lines
3.9 KiB
Go

package netproxy
import (
"testing"
"github.com/yetone/magpie/internal/settings"
)
// downPort stubs the listening check: addr is down until the returned up
// is called; every other port is up. The cache of looks is cleared each
// way, so a test sees the move at once.
func downPort(t *testing.T, addr string) (up func()) {
t.Helper()
isUp := false
old := dialShell
dialShell = func(a string) bool { return a != addr || isUp }
forget := func() {
shellPorts.Lock()
shellPorts.seen = nil
shellPorts.Unlock()
}
forget()
t.Cleanup(func() { dialShell = old; forget() })
return func() { isUp = true; forget() }
}
// @bitssly on X (Windows, proxy Auto): a VPN app turned off left Internet
// Options' proxy at its loopback port, and every request failed with 502
// "proxy not reachable", a restart of magpie too, until Off. A system
// proxy on loopback that nothing listens on is now none: requests, a
// CLI's or a plugin host's env, and the settings page go direct, and
// the page says why. The proxy app coming back is taken again.
func TestDeadLoopbackSystemProxyGoesDirect(t *testing.T) {
clearProxyEnv(t)
shellSays(t, nil)
addr := freePort(t)
up := downPort(t, addr)
sysCache.p = Proxy{URL: "http://" + addr, HTTP: "http://" + addr, Bypass: []string{"<local>"}}
if got := proxyFor(t, "https://api.example/"); got != "" {
t.Fatalf("a dead system proxy was taken: %q", got)
}
if got, from := Describe(); got != "http://"+addr || from != "down" {
t.Fatalf("Describe = %q (%s), want the dead address (down)", got, from)
}
if env := Env([]string{"PATH=/bin"}); envVal(env, "HTTPS_PROXY") != "" {
t.Fatalf("a CLI was given a dead system proxy: %q", env)
}
up()
if got := proxyFor(t, "https://api.example/"); got != "http://"+addr {
t.Fatalf("the proxy app came back and wasn't taken: %q", got)
}
if got, from := Describe(); from != "system" || got != "http://"+addr {
t.Fatalf("Describe = %q (%s), want system", got, from)
}
if env := Env([]string{"PATH=/bin"}); envVal(env, "HTTPS_PROXY") != "http://"+addr {
t.Fatalf("a CLI wasn't given the live system proxy: %q", env)
}
}
// A system proxy elsewhere than loopback is taken as it is: magpie can't
// tell a proxy that is down from a network that drops a probe.
func TestRemoteSystemProxyIsNotProbed(t *testing.T) {
clearProxyEnv(t)
shellSays(t, nil)
old := dialShell
dialShell = func(a string) bool { t.Errorf("probed %s", a); return false }
t.Cleanup(func() { dialShell = old })
sysCache.p = Proxy{URL: "http://10.9.8.7:3128"}
if got := proxyFor(t, "https://api.example/"); got != "http://10.9.8.7:3128" {
t.Fatalf("a remote system proxy was dropped: %q", got)
}
}
// The environment's proxy is set for this process by whoever started it,
// and taken as it is, dead or not.
func TestDeadEnvProxyIsKept(t *testing.T) {
clearProxyEnv(t)
shellSays(t, nil)
dead := freePort(t)
downPort(t, dead)
// ALL_PROXY: Go reads HTTPS_PROXY once per process (ProxyFromEnvironment)
t.Setenv("ALL_PROXY", "http://"+dead)
if got := proxyFor(t, "https://api.example/"); got != "http://"+dead {
t.Fatalf("the environment's proxy was gone around: %q", got)
}
if env := Env([]string{"HTTPS_PROXY=http://" + dead}); envVal(env, "HTTPS_PROXY") != "http://"+dead {
t.Fatalf("a CLI's own proxy was taken out: %q", env)
}
}
// A proxy set in magpie is never gone around, dead or not: requests fail
// through it, so the user sees it, rather than leave from where they said
// not to.
func TestDeadProxySetInMagpieStillFails(t *testing.T) {
clearProxyEnv(t)
shellSays(t, nil)
addr := freePort(t)
downPort(t, addr)
if err := settings.Save(settings.Settings{Proxy: "http://" + addr}); err != nil {
t.Fatal(err)
}
if got := proxyFor(t, "https://api.example/"); got != "http://"+addr {
t.Fatalf("a proxy set in magpie was gone around: %q", got)
}
if env := Env([]string{"PATH=/bin"}); envVal(env, "HTTPS_PROXY") != "http://"+addr {
t.Fatalf("a CLI went around the proxy set in magpie: %q", env)
}
}