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/main9f9bfc160), -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 base7660399a1with no --- FAIL before the deadline (#2544), and passed alone at 100m (2790s). - Rebased onto origin/main17557132c(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).
96 lines
3.4 KiB
Go
96 lines
3.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// #2651 (demystify0137): a Claude account behind a socks5:// proxy was
|
|
// answered 502 after minutes, Claude Code failing every try as
|
|
// UnsupportedProxyProtocol, since Bun, which Claude Code is built with,
|
|
// takes only http:// and https:// proxies. Every Claude Code magpie runs
|
|
// for the subscription — a turn, a model test, /usage — is given magpie's
|
|
// HTTP bridge to the SOCKS proxy instead, never socks5:// itself.
|
|
func TestClaudeCodeGivenTheBridgeForASOCKSProxy(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("a shell script stands in for Claude Code")
|
|
}
|
|
const socks = "socks5://127.0.0.1:1"
|
|
dir := t.TempDir()
|
|
log := filepath.Join(dir, "log")
|
|
// a turn's run answers with its proxy; a test's or /usage's writes it
|
|
// to log
|
|
script := `#!/bin/sh
|
|
proxies="https=$HTTPS_PROXY http=$HTTP_PROXY all=$ALL_PROXY lower=$https_proxy"
|
|
case " $* " in
|
|
*" stream-json "*)
|
|
while read -r line; do
|
|
echo '{"type":"stream_event","event":{"type":"message_start","message":{"id":"m","model":"claude-sonnet-5","usage":{"input_tokens":1}}}}'
|
|
echo '{"type":"stream_event","event":{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'"$proxies"'"}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":3}}}'
|
|
echo '{"type":"stream_event","event":{"type":"message_stop"}}'
|
|
echo '{"type":"result","subtype":"success","is_error":false,"result":""}'
|
|
done ;;
|
|
*)
|
|
echo "$proxies" > ` + log + `
|
|
echo '{"type":"result","is_error":false,"result":"Hi!"}' ;;
|
|
esac
|
|
`
|
|
testenv.Program(t, filepath.Join(dir, "claude"), script)
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
runsEndWithTest(t, dir)
|
|
|
|
bridged := func(what, got string) {
|
|
t.Helper()
|
|
if strings.Contains(got, "socks5") {
|
|
t.Fatalf("%s: Claude Code was given the SOCKS proxy: %s", what, got)
|
|
}
|
|
for _, k := range []string{"https", "http", "all", "lower"} {
|
|
if !strings.Contains(got, k+"=http://127.0.0.1:") {
|
|
t.Fatalf("%s: no bridge in %s: %s", what, k, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
s := New()
|
|
t.Cleanup(s.subscription.abortAll)
|
|
p := provider.Provider{ID: "claude", Proxy: socks, Account: &provider.Account{Agent: "claude", User: "u"}}
|
|
body := `{"model":"claude-sonnet-5","max_tokens":100,"messages":[{"role":"user","content":"hi"}]}`
|
|
rec := httptest.NewRecorder()
|
|
var u Usage
|
|
if code, msg := s.serveClaudeSubscription(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)), provider.Anthropic, p, "claude-sonnet-5", []byte(body), &u); code != 200 {
|
|
t.Fatalf("%d %s", code, msg)
|
|
}
|
|
var res struct {
|
|
Content []struct{ Text string } `json:"content"`
|
|
}
|
|
json.Unmarshal(rec.Body.Bytes(), &res)
|
|
if len(res.Content) == 0 {
|
|
t.Fatalf("no answer: %s", rec.Body)
|
|
}
|
|
bridged("a turn", res.Content[0].Text)
|
|
|
|
ctx := netproxy.With(context.Background(), socks)
|
|
if err := askClaude(ctx, "", "haiku"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := os.ReadFile(log)
|
|
bridged("a model test", string(b))
|
|
os.Remove(log)
|
|
if _, err := claudeUsage(ctx, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ = os.ReadFile(log)
|
|
bridged("/usage", string(b))
|
|
}
|