Commit Graph
9 Commits
Author SHA1 Message Date
ggbdpqandyetone 3ed3605ba0 netproxy: a global proxy switch drops the idle connections of the sync and the icon fetch (#4824)
ggbdpq's patch (gua2100 on Discord), reviewed and landed with test
changes.

What changed
- netproxy.Watch gives the dispatch's per-request check for a moved
  global proxy (#4523) to transports magpie keeps for itself. The sync
  (davsync) uses http.DefaultTransport, so when one of its requests
  finds the proxy moved, it now drops the idle connections itself.
  Before this, only a DefaultClient request dropped them, and a backup
  kept going through the old proxy's HTTP/2 connection.
- FetchIcon now closes the pool of the client that only the fetch used.
  Nothing closed it before.
- Review changes: the davsync test restores settings.json afterwards,
  and the icon test's wait for idle is bounded at 20s, not 2s, to leave
  room under load.

Run at the pushed head (on mote, base 4d55a0a02)
- go test -race passes for netproxy, davsync and provider's touched
  tests.
- go test -tags nogui -count=1 -timeout 45m ./... passes every package
  except two reds already filed and not touched here:
  - internal/cli TestEveryRouteHasACommandOrIsNamed (#4826, red on main
    since e87140242);
  - internal/gateway TestCodexPoolSaysTheClientsVersion (#4807, another
    test's Codex identity, passes alone).
- gofmt -l is clean. go vet ./... passes for darwin, for GOOS=linux
  (-tags nogui) and for GOOS=windows, and so do the linux and windows
  builds.

Broken by hand
- With stall.go's move check off, TestGlobalSwitchDialsTheSyncThroughTheNewProxy
  is red: "1 connections through A, 0 through B".
- With FetchIcon's CloseIdleConnections removed,
  TestFetchIconLeavesNoIdleConnection is red: "the fetch's connection is
  still idle".
- With Watch never reporting a move, TestGlobalSwitchDropsTheOldProxyConnections
  and TestWatchSeesTheGlobalProxyMove are red.

Not tried: a real proxy switch with a real WebDAV/S3 target or icon
host, or Windows/Linux at run time (vet and build only).

Co-authored-by: yetone <yetoneful@gmail.com>
2026-11-02 09:11:30 +08:00
ggbdpq cb35d2fa94 fix(netproxy): a switch of the global proxy drops the old proxy's idle connections (#4523)
What changed
- Dispatch now remembers the proxy setting each request left under and,
  when it moves, closes the idle connections it kept (base's and every
  provider clone's), so the request after a switch dials through the
  proxy in force. A transport pools by target host, and its HTTP/2
  connections by the authority alone, so until now a Test after the
  switch rode the old proxy's connection and left by the old exit while
  the new proxy was the one shown.

Run
- go test ./internal/netproxy/ -run TestGlobalSwitchDropsTheOldProxyConnections
  fails on the parent (1 connection through A, 0 through B after the
  switch), passes here; the package's full suite passes (-tags nogui).

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-02 05:38:10 +08:00
appointatandClaude Opus 5.5 abdc4b113d netproxy, provider, gateway: every request to the ChatGPT backend goes over HTTP/1.1, magpie's own too, not only the gateway's turns (#1700)
f6e1e98e put the gateway's turns to chatgpt.com on HTTP/1.1 through a
wrapper on the gateway's client. magpie's own requests there go through
http.DefaultClient and still spoke HTTP/2: the models list
(codexModels), the warm-up's POST to /backend-api/codex/responses
(warmCodex), the allowance (codexWindows via accountJSON), the reset
credits (codexResets) and spending one (consumeCodexReset). #1700's
reporter, on 0.1.1164, had Codex's /responses fail with PROTOCOL_ERROR
through a local proxy until GODEBUG=http2client=0, set by editing the
app bundle.

The rule now lives where every magpie Go client goes through.
netproxy.Dispatch sends a request netproxy.HTTP1 names through a clone
of its transport kept for HTTP/1.1, beside a provider's own proxy if
any, and the provider package sets HTTP1 to codexBackend (CodexBase's
host). The clone's TLS hello offers no ALPN, as codex-cli's: a transport
that has spoken HTTP/2 carries h2 in its TLS config, which a clone
copies, and a server that took it would be sent HTTP/1.1 on an HTTP/2
connection. The gateway's codexHTTP1 wrapper is folded into this; its
transport file is now upstream_transport.go. Every other vendor is still
offered HTTP/2, and GODEBUG=http2client=0 still turns HTTP/2 off for all.

What was run (Go 1.26.8, darwin/arm64, a sandbox HOME):
- TestCodexBackendOwnRequestsOverHTTP1 (provider): http.DefaultClient
  as netproxy.Install leaves it, a TLS server offering HTTP/2, the five
  requests directly, through Settings' proxy and through an account's
  own CONNECT proxy. On origin/main 6c1c0a74 it fails: all five
  "HTTP/2.0 (h2 offered)" each way. Another vendor's accountJSON read
  stays HTTP/2.0.
- TestDispatchHTTP1 (netproxy): after the transport has spoken HTTP/2
  to the server, a named request arrives HTTP/1.1 with no h2 offered,
  with a proxy choice too. Without the NextProtos reset it fails with
  "malformed HTTP response"; with HTTP1 ignored, with "HTTP/2.0".
- With the provider's hook off, the gateway's TestCodexBackendOverHTTP1
  fails too ("asked over HTTP/2.0, not HTTP/1.1"). TestAccountProxy,
  which stubs http.DefaultClient with a Dispatch of its own, passes; a
  separate Codex client in provider, tried first, failed it.
- go test -race -count=20: ./internal/netproxy TestDispatchHTTP1 and
  TestWith, ./internal/provider TestCodex* (this one included),
  ./internal/gateway TestCodexBackendOverHTTP1 and TestAccountProxy.
  gofmt -l; go vet (darwin, GOOS=linux -tags nogui, GOOS=windows); go
  build (the same three); go test -tags nogui ./... (49 packages) pass.
- Real use, behind Clash on a Mac: Codex's own sign-in copied into a
  sandbox HOME (days left on the token, nothing refreshed), and a
  CONNECT proxy in front of Clash logging each TLS hello's ALPN.
  origin/main: `magpie provider models codex` and one request at the
  gateway's start offered h2 to chatgpt.com, the turn none. This
  branch: no hello to chatgpt.com offered any, models.dev still h2.
  #1700's minimal request on codex/gpt-6-astra answered 200 with
  MAGPIE_CODEX_OK on both.

Not reproduced: PROTOCOL_ERROR itself; chatgpt.com answered HTTP/2 on
this Mac, so whether HTTP/1.1 is what the reporter's path needs is for
their use to say. The warm-up and spending a reset weren't sent for
real.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XSjhwVsPdGd6QUTrq4KvE4
2026-10-13 06:18:27 +08:00
yetone e4134d1fb7 agent: Codex's proxy is worked out from the login shell's NO_PROXY and HTTP_PROXY and macOS' HTTP entry alone, as its reqwest reads them, so "Codex 经代理连 magpie" no longer shows while Codex goes direct (liu_dyj on X)
liu_dyj: with the VPN on, magpie said Codex reached magpie through a
proxy that didn't pass its requests on; versions before f156d9046 had
no such check, which is why upgrading brought it.

Cause: codexProxyFor didn't read the proxy the way Codex does.
- It read magpie's own environment. Codex in a terminal has the login
  shell's, and the ChatGPT app's Codex too (ChatGPT.app 26.1007 runs
  $SHELL -ilc at start and Object.assign()s its env into process.env).
  magpie opened from the Dock has neither, so a NO_PROXY=127.0.0.1 in
  .zshrc, which keeps Codex direct, wasn't seen, and magpie probed the
  system proxy and warned.
- For the system proxy it took netproxy's one URL (HTTPS over HTTP over
  SOCKS). hyper-util 0.1.20's Matcher::from_system, which reqwest
  0.12.28 uses for Codex, takes only macOS' HTTP entry for an http://
  URL; with only the HTTPS or SOCKS entry set (ShadowsocksX-NG's global
  mode is SOCKS only) Codex goes direct and magpie warned. On Windows
  it takes ProxyServer as one address, which a per-protocol list
  ("http=..;https=..") isn't (http's Authority::parse refuses two
  colons), so Codex goes direct there too.
- With respect_system_proxy on macOS, CFNetwork's SOCKS entry is
  UnsupportedProxyScheme, and Codex falls back to HTTP_PROXY/ALL_PROXY
  with NO_PROXY (codex-rs outbound_proxy.rs resolve_env_proxy_route);
  magpie used the SOCKS URL.
- The probe spoke real SOCKS5 to a socks5:// proxy. Codex's reqwest is
  built without SOCKS (no tokio-socks in codex-rs Cargo.lock) and sends
  an absolute-URI HTTP request to it, so the probe now does the same:
  it passes a Clash mixed port as Codex does and fails a SOCKS-only
  port as Codex does.

So netproxy.Proxy carries the HTTP and SOCKS entries alone beside URL
(parseScutil, parseWindows), proc asks the login shell for the
*_PROXY variables with agentenv.Vars in the same probe and only reads
them (proc.LoginEnv; never set in magpie's env, whose requests use
netproxy), and codexProxyFor uses both. Failover's gate
(codexProxyOK) and the drift share codexProxyFor and proxyProbe, so
both follow. The pill's How to fix now names the shell profile for
NO_PROXY. Clash/mihomo rule mode with a 127.0.0.0/8 DIRECT rule never
warned and still doesn't; global mode or a rule set without it still
warns, rightly (real codex 502s there). TUN mode sets no proxy and
never warned.

Run:
- real codex-cli 0.162.1 in a sandbox HOME/CODEX_HOME against a fake
  gateway and mihomo v1.19.32 (rule, no-LAN-rule, global ports):
  direct, rule, and ALL_PROXY=socks5:// on the rule port reach the
  gateway; no-LAN-rule and global 502; global with NO_PROXY=127.0.0.1
  reaches it. The Go probe agrees on every one.
- go test -tags nogui ./internal/agent, ./internal/netproxy,
  ./internal/proc; gofmt -l; GOOS=linux and GOOS=windows go vet
  -tags nogui on the three packages.
- Broke each part by hand: sys.URL back in codexProxyFor (Windows per
  protocol, macOS HTTPS/SOCKS alone red), the probe's socks->http
  (TestRealProxyProbe red), codexEnv without the login shell
  (TestCodexEnvTakesLoginShell red), the respect-path SOCKS fallback
  (three respect cases red), setUnset of every shell var and the probe
  without ProxyVars (TestLoginEnvProxyVarsNotSet red).

Not tried: the macOS system proxy cases with real Codex (changing the
system's proxy settings is off limits here; read from hyper-util,
reqwest and codex-rs sources), the Windows per-protocol ProxyServer
with real Codex (from the http crate's source; no Windows run), and
the reporter's own app and mode, which are asked on X.
2026-10-12 22:33:50 +08:00
yetone 2305ab1fc6 gateway, netproxy: a web search goes through its searcher's proxy, not the direct line of the provider serving the turn (#1522, xixijay1988)
Claude Code on opencode-go (set to "direct"), with Codex as the search
delegate and the proxy on Auto (system proxy 127.0.0.1:7890): every
WebSearch dialed chatgpt.com directly, to the addresses a poisoned DNS
gave, and failed with "no answer in 2m0s". Codex's own requests through
the same gateway went through the proxy.

Cause: attempt() puts the turn provider's proxy choice on the request
context (Provider.Via -> netproxy.With). The search runs inside that
context, and its sub-request to Codex calls Provider.Via again, but
netproxy.With(ctx, "") returned ctx unchanged, so a provider that
follows the global proxy inherited the outer provider's "direct".

- netproxy.With: "" now replaces a choice ctx already names, so any
  request made for one provider while serving another (search delegate,
  vision describer, classifier, Codex titles, all through attempt's
  p.Via) takes its own provider's proxy or the global one.
- webSearch clears the turn's choice up front: the search APIs, which
  have no proxy of their own, follow the global one.
- kimiSearch now applies the Kimi Code plan's own proxy (p.Via); it
  had none and went the turn's way.

Verified:
- New TestSearchFollowsTheSearchersProxy: a fake CONNECT proxy on a free
  port; the turn's provider is reachable only directly, the searcher, a
  Kimi Code search service and a search API only through the proxy.
  Cases: Settings' proxy, Auto (from the environment), a searcher's own
  proxy with the global direct, a search API. On the old code it fails
  with "dial tcp search.test:443: ... (dialed directly)", CONNECTs [];
  with only the With fix the search API case fails; with the kimiSearch
  change reverted the Kimi case fails. -race -count=5 passes.
- TestWith in netproxy gains With(With(ctx, outer), "") follows the
  global proxy; fails on the old With.
- go test -tags nogui: gateway, netproxy, provider, plugin, update pass;
  the full ./... suite under load ~180 passed except
  internal/agent TestDshHomePickSurvivesSyncAndEffort, which passes alone
  (-count=3) and touches no proxy code (not this change; not yet filed).
  After rebasing onto 552c2244d (search MCP, also through webSearch) the
  search, Kimi and proxy tests pass again. gofmt -l clean; go build;
  GOOS=linux and GOOS=windows go vet -tags nogui on gateway and netproxy.
- docs/subsystems/gateway-routing.md: the one-try row says how a
  sub-request picks its proxy; the test is in its run list.
- Not tried against the reporter's real proxy and DNS.
2026-10-11 02:01:32 +08:00
yetone bc141ad834 update: install.sh and magpie update take --proxy and --mirror, and magpie update mirror keeps one for the app's updates too (akic404 on Discord: 安装和update如果没有梯子也是麻烦事,能否在install.sh和magpie update后加子选项?--proxy等可自定义或默认ghproxy) — install.sh had no options and fetched with curl alone, and magpie update took none; without a way out to GitHub both failed. Now both take --proxy <url> (http, https, socks5, socks5h; install.sh also MAGPIE_PROXY=) for the feed and the download, and --mirror <prefix> (or MAGPIE_MIRROR=), a GitHub download mirror the release file is fetched from as <prefix>https://github.com/…; only github.com / githubusercontent.com URLs are put through it. No mirror is used unless one is given: magpie names none and has none by default. The feed with each file's SHA-256 is always fetched from usemagpie.ai, never through the mirror, so a file the mirror changed is refused and deleted ("… from the mirror X does not match its checksum from https://usemagpie.ai; not installed"); macOS still checks the app's signing team. magpie update mirror <prefix>|off saves Settings' updateMirror (http(s) only), which the app's own background update follows too; magpie update --mirror off skips it once; the Settings page's save keeps it. The app's update already followed Settings → Proxy (#294); magpie update --proxy overrides it for one run. The environment's ALL_PROXY / all_proxy is now honoured when HTTPS_PROXY / HTTP_PROXY are unset (Go's ProxyFromEnvironment ignores it), with NO_PROXY as curl reads it. magpie update takes --flag value or --flag=value, and an unknown word is an error instead of an update. The help, README and the docs' FAQ (en/zh/ja) say how. install.test.mjs's proxy, mirror and tampered-mirror tests, TestAllProxyFromTheEnvironment, TestUpdateThroughAMirror, TestUpdateThroughAProxyGiven and TestUpdateFlags fail on the old code and pass; TestSettingsSaveKeepsWhatItDoesNotSend covers updateMirror. A dry run of install.sh as Linux amd64 into a temp HOME installed the real 0.1.773 with its SHA-256 checked, refused a local mirror serving a wrong file, and with --proxy to a closed port failed reaching the site through it. go vet, builds (darwin, GOOS=linux, GOOS=windows), sh -n, node --test site/install.test.mjs and go test -tags nogui -count=1 ./... under a temp HOME pass but TestClaudeIdentityServedWhileAsked, a timing test that passes alone. Not tried through a real proxy server or a real public mirror; no install.ps1 exists for Windows, and the app has no field for the mirror. 2026-10-04 00:19:47 +08:00
yetone 125da38943 providers: each provider, a signed-in account like Codex's among them, goes through its own proxy — the one in Settings, none, or its own address (#237 xosi: 目前似乎是全局设置代理的,codex和国模供应商无法单独区分设置代理) — magpie had one proxy, Settings' (or the environment's, or the system's), for every request it made, so Codex, which needs a proxy from China, and a vendor at home, which is slower or refused through one, couldn't both be right. A provider now has a proxy, "" (following the global one, as before and the default), "direct" (none, whatever Settings says) or an http://, https:// or socks5:// address (host:port meaning http), kept as proxy in providers.json only when set, and for an account provider in its picks entry beside its models. It is checked on save by the same rule Settings' proxy is (settings.CheckProxy, now shared), so ftp:// and the like are refused. netproxy.With puts a provider's choice on a request's context and netproxy.Func honours it; netproxy.Dispatch sends such a request through a clone of the transport kept for that proxy alone (at most 32, cleared past that), since a transport shares an HTTP/2 connection by host whatever proxy dialled it; loopback is never proxied. The gateway's client and http.DefaultClient go through Dispatch, and every request made on a provider's behalf carries its choice: the gateway's turns (attempt, forward, draw, decide, the Codex backend), and the model lists, balances, plan and subscription usage, tests, sign-in, Codex's warm-up and reset, while the Claude subscription's and warm-up's CLI runs get the account's proxy in their *_PROXY (netproxy.EnvWith). The provider editor, a key provider's and a signed-in account's alike, has a Proxy row after Fallback: Global proxy | Direct | Custom (全局代理 | 直连 | 自定义), the address field beside the options only for Custom, its room kept otherwise so a pick never changes the centred dialog's height and moves nothing, and a line under it saying what the pick does; Save posts proxy ("" , "direct" or the trimmed address), a save without it keeps the old one, and Custom with no address is refused before anything is posted ("Proxy: type its address, like http://127.0.0.1:7890"). No border stripes; en and zh strings together. Tested with a new internal/gateway/proxy_test.go in a sandbox HOME: with a global proxy set, provider A's turns and model list go through its own fake proxy, B's (direct) straight to the vendor, C's (following) through the global one, none through another, and A set back to following goes through the global one; ftp:// is refused (fails with With neutered). netproxy_test.go's TestWith covers Func, Dispatch's per-proxy transports and EnvWith. A new internal/gui/tests/provider-proxy.test.cjs in Chromium and WebKit, en and zh: a key provider opens on Global proxy with no address, Custom unfilled is refused and nothing posted, a padded socks5 address saved trimmed, Direct saved as "direct" and Global as ""; Codex's account opens on Custom with its address and saves Direct with its models; every pick leaves the option within 1px; no left border. go build -tags nogui (and GOOS=linux, GOOS=windows), go test -tags nogui ./..., provider-proxy, click-scroll, old-webkit, balance-fix and add-sheet pass (click-scroll's WebKit case failed once alongside the others and passes alone, twice). Not tried against a real proxy or the real Wails window. 2026-09-30 02:15:50 +08:00
yetone 3eed0cdba4 netproxy: an agent's CLI that magpie starts — grok login, cursor-agent, devin, Claude Code, and the runs behind their subscriptions — goes through the proxy magpie's own requests do (#51); the CLIs read only *_PROXY, which an app started from the Dock doesn't have, so a proxy set in magpie or the system alone left grok login trying auth.x.ai directly until it timed out. A proxy set in magpie replaces the environment's, "direct" drops it, the system's is added where the environment names none, and loopback stays direct for the CLIs' calls back to magpie 2026-09-25 12:07:01 +08:00
yetone 1f72ac82f0 net: requests go through a proxy — Settings → Proxy, or the environment's, or the system's (#8) 2026-09-24 12:48:01 +08:00