Commit Graph
4 Commits
Author SHA1 Message Date
yetone f550d3699c claude: a SOCKS proxy reaches Claude Code through magpie's HTTP bridge (#2651)
demystify0137's Claude account sits behind a socks5:// proxy. Every turn
was answered 502 after minutes of retries, Claude Code failing each try as
UnsupportedProxyProtocol: it is a Bun binary, and Bun takes only http://
and https:// proxies. magpie handed it the socks5:// URL as HTTPS_PROXY.

Every Claude Code magpie runs for the subscription now gets its env from
one helper, netproxy.EnvForBunWith, which sets the proxy as before and
puts the loopback HTTP bridge (netproxy.Bridge, #1409) in place of a SOCKS
URL: a turn (startRun), the model test (askClaude), /usage (claudeUsage),
sign-in (startClaudeSignIn) and the sign-in check (claudeSignedIn). Where
the bridge can't be used (it doesn't start, or Claude Code runs in WSL
under NAT, which doesn't reach this computer's loopback) the request fails
at once with that reason, instead of Claude Code retrying for minutes.
Other CLIs magpie runs with a proxy (grok, devin, cursor-agent) aren't Bun
(checked their binaries), and the settings.json env writer already leaves
SOCKS out.

What was run:
- Real Claude Code 2.1.302 in a sandbox HOME with a fake API key, through
  a fake SOCKS5 proxy (temporary test, removed): raw socks5:// gave "API
  Error: Unable to connect to API (UnsupportedProxyProtocol)" with 0
  SOCKS connections; bridged it reached Anthropic, 401 "Invalid API key",
  7 SOCKS connections.
- New tests TestEnvForBunWithBridgesTheProvidersSOCKS,
  TestClaudeCodeGivenTheBridgeForASOCKSProxy (turn, askClaude,
  claudeUsage), TestClaudeSignInBridgesASOCKSProxy. With proxied()
  put back to EnvWith both gateway and provider tests went red ("Claude
  Code was given the SOCKS proxy: https=socks5://127.0.0.1:1...",
  "login-proxy: Claude Code ran with HTTPS_PROXY=\"socks5://127.0.0.1:1\"").
- gofmt -l clean; go vet -tags nogui ./... on darwin, GOOS=linux and
  GOOS=windows at 8b1fad8f5: ok.
- Full suite at 8b1fad8f5 (this and #2649 on origin/main 9f9bfc160),
  -tags nogui -timeout 100m, load ~290: every package ok except three
  timing reds outside this change, each green alone with -count=3:
  TestBurstTellsNoSpeed (#2719), TestFXStateDoesNotWait (fixed on main since, bad4508c4),
  TestTaggedReposSlowRead (#1889). internal/agent passed (3659s); at
  -timeout 45m it timed out twice at the earlier base 7660399a1 with no
  --- FAIL before the deadline (#2544), and passed alone at 100m (2790s).
- Rebased onto origin/main 17557132c (7 commits, none in these files):
  gofmt and vet on all three systems, then internal/gateway,
  internal/provider, internal/netproxy and internal/claudebridge at
  9af3bb98b: ok. The whole suite wasn't run again at this head.

Not tried: Windows, or Claude Code in WSL (mirrored or NAT).
2026-10-17 19:55:49 +08:00
yetone 9a852ae9db netproxy: a bridge handed its SOCKS proxy's just-freed port listens elsewhere, not carrying to itself (TestPluginSOCKSProxy's 60s CI timeouts)
The plugin host gives Bun an HTTP bridge to a SOCKS proxy, listening on
127.0.0.1:0. A port just let go can be handed out again at once, so a
bridge to a SOCKS proxy on loopback that is down could be given that very
port. It then dialled itself, and bridgeConn read the SOCKS greeting as an
HTTP request under its one-minute deadline: every request through it hung
60s instead of failing at once. That is TestPluginSOCKSProxy's 60s timeouts
on ubuntu CI (closedPort frees a port, then the bridge listens), rerun as
load before. A user whose local SOCKS proxy is down could meet it too.

Bridge now listens again while still holding such a port (up to 3 times),
then lets the first go.

Run:
- Injected the reuse (listenLoopback handing out closedPort's port): before
  the fix internal/plugin's SOCKS test failed after 60.03s with "context
  deadline exceeded"; with it, the package passed in 2.4s.
- New TestBridgeNeverCarriesToItself (addr and localhost:<port>): red with
  the loop taken out by hand, green with it.
- go test -race -count=20 -run TestBridge ./internal/netproxy: ok.
- go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean. gofmt -l clean.
- go test -tags nogui -count=1 -p 4 -timeout 45m ./... at d0ccbbebc + this:
  58 ok; internal/gateway red only in TestMaxRPMCountsRetriesAndCounts
  ("a count with no room: 200 after 2.72s"), the open #1791 under load.

Not tried: the Windows box.
2026-10-15 11:42:47 +08:00
yetone 3691c2047d plugin, agent: bun is given a SOCKS proxy through the bridge, so a plugin installs with only a SOCKS system proxy (#1409)
With only a SOCKS system proxy on macOS (127.0.0.1:4401), every plugin
install failed: netproxy.Env put socks5://… in bun add's *_PROXY, and Bun
takes only http:// and https:// proxies, so it stopped with
"UnsupportedProxyProtocol downloading package manifest". The plugin
host's own fetches already went through netproxy.Bridge (hostEnv), but
bunCommand, which runs bun add, update and remove, did not.

- netproxy.EnvForBun rewrites a SOCKS5 HTTPS_PROXY/HTTP_PROXY/ALL_PROXY
  (any case) to the loopback HTTP bridge in front of it; others as they are.
- bunCommand uses it, so add, update, remove and the host's run all get it.
- Sibling: an agent CLI installed with bun is updated by `bun add -g`
  with the same env (updateEnv in cliupdate.go); npm is left as it is.
- provider-plugins.md says so.

Run:
- TestInstallThroughSOCKSProxy (real bun 1.4.2, fake registry, SOCKS
  proxy only): on the old bunCommand it fails with the reporter's exact
  "error: UnsupportedProxyProtocol downloading package manifest"; passes
  with the fix, through the SOCKS proxy.
- TestEnvForBunBridgesSystemSOCKS (system SOCKS proxy via Env) and
  TestUpdateWithBunBridgesSOCKS fail with EnvForBun / the bun branch
  broken ("bun is given …socks5://…"), pass with it.
- Real use: a throwaway test with a real SOCKS5 server and only
  *_PROXY=socks5://… installed @magpie-community/opencode-cursor-auth
  0.2.2 from registry.npmjs.org through it; on the old code the same
  failed as UnsupportedProxyProtocol. Scratch HOME, not committed.
- gofmt -l clean; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...; go build; GOOS=linux go build -tags nogui;
  GOOS=windows go build; go test -tags nogui ./... (scratch HOME) all ok.
2026-10-09 19:35:35 +08:00
yetone 4f9dcb39e5 plugins: a plugin subscription goes through every path a built-in one does (the user: 要保证插件订阅 provider 跟内置的订阅 provider所有逻辑都要一模一样) — a SOCKS5 proxy on a plugin provider or account now carries its requests: Bun speaks http(s) proxies only, so magpie stands a loopback http proxy in front of the SOCKS one, and a proxy that refuses says so as Go's transport does. A plugin's model list and its loader are asked through the account's own proxy, as a built-in's list is fetched through it. The Grok plugin draws and makes videos at Grok's Imagine API through the plugin, and keeps Codex's namespaced tools, as the built-in does; an official plugin beside its built-in (kiro-plugin, zcode-plugin) counts tokens by estimate as the built-in and the moved plugin do. FetchNew no longer re-lists plugin providers every ten minutes, their list being told by the plugin host; a plugin account's usage is cached under one name whichever way it is asked for; and a fetch hook that throws on its sign-in (a refused refresh) marks the account lapsed, as a built-in's refused refresh does. TestPluginSOCKSProxy, TestPluginModelListThroughAccountProxy, TestGrokPluginDraws, TestPluginBesideBuiltinCountsByEstimate, TestFetchNewLeavesListedPlugins, TestPluginUsageReadOnceWhicheverName and TestPluginFetchSignInExpiredLapses fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass (the known load flakes pass alone); internal/plugin, netproxy and the plugin/proxy tests of provider and gateway pass on Linux and Windows. 2026-10-01 19:07:12 +08:00