Files
magpie/internal/netproxy/netproxy_test.go
T
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

221 lines
7.8 KiB
Go

package netproxy
import (
"context"
"net/http"
"net/url"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/settings"
)
func TestParseScutil(t *testing.T) {
out := `<dictionary> {
ExceptionsList : <array> {
0 : 127.0.0.1
1 : *.local
}
HTTPEnable : 1
HTTPPort : 7890
HTTPProxy : 127.0.0.1
HTTPSEnable : 1
HTTPSPort : 7891
HTTPSProxy : 127.0.0.1
SOCKSEnable : 1
SOCKSPort : 7892
SOCKSProxy : 127.0.0.1
}`
p := parseScutil(out)
if p.URL != "http://127.0.0.1:7891" || len(p.Bypass) != 2 || p.Bypass[1] != "*.local" {
t.Fatalf("%+v", p)
}
// each entry alone, for Codex's reqwest, which takes the HTTP one for
// an http:// URL and never the HTTPS or SOCKS one
if p.HTTP != "http://127.0.0.1:7890" || p.SOCKS != "socks5://127.0.0.1:7892" {
t.Fatalf("entries: %+v", p)
}
if p := parseScutil("<dictionary> {\n SOCKSEnable : 1\n SOCKSPort : 1080\n SOCKSProxy : 10.0.0.2\n}"); p.URL != "socks5://10.0.0.2:1080" || p.HTTP != "" || p.SOCKS != p.URL {
t.Fatalf("socks: %+v", p)
}
if p := parseScutil("<dictionary> {\n HTTPSEnable : 1\n HTTPSPort : 7890\n HTTPSProxy : 127.0.0.1\n}"); p.URL != "http://127.0.0.1:7890" || p.HTTP != "" {
t.Fatalf("https only: %+v", p)
}
if p := parseScutil("<dictionary> {\n HTTPEnable : 0\n HTTPProxy : x\n}"); p.URL != "" {
t.Fatalf("off: %+v", p)
}
}
func TestParseWindows(t *testing.T) {
for server, want := range map[string]string{
"127.0.0.1:7890": "http://127.0.0.1:7890",
"http=127.0.0.1:1;https=127.0.0.1:2": "http://127.0.0.1:2",
"socks=127.0.0.1:1080": "socks5://127.0.0.1:1080",
"": "",
} {
if got := parseWindows(server, "").URL; got != want {
t.Errorf("%q: %q, want %q", server, got, want)
}
}
// reqwest reads ProxyServer as one address: a list per protocol isn't one
if p := parseWindows("127.0.0.1:7890", ""); p.HTTP != "http://127.0.0.1:7890" {
t.Fatalf("one for all: %+v", p)
}
if p := parseWindows("http=127.0.0.1:1;https=127.0.0.1:2;socks=127.0.0.1:3", ""); p.HTTP != "" || p.SOCKS != "socks5://127.0.0.1:3" {
t.Fatalf("per protocol: %+v", p)
}
p := parseWindows("127.0.0.1:7890", "*.corp;<local>")
if !bypassed("git.corp", p.Bypass) || !bypassed("intranet", p.Bypass) || bypassed("chatgpt.com", p.Bypass) {
t.Fatalf("bypass: %+v", p.Bypass)
}
}
func TestFor(t *testing.T) {
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
sysCache.at, sysCache.p = farFuture, Proxy{URL: "http://127.0.0.1:7890", Bypass: []string{"*.corp"}}
t.Cleanup(func() { sysCache.at = zero })
u, _ := url.Parse("https://chatgpt.com/backend-api/codex/responses")
if p, _ := For(u); p == nil || p.String() != "http://127.0.0.1:7890" {
t.Fatalf("system: %v", p)
}
corp, _ := url.Parse("https://git.corp/x")
if p, _ := For(corp); p != nil {
t.Fatalf("bypass: %v", p)
}
local := &http.Request{URL: &url.URL{Scheme: "http", Host: "127.0.0.1:3425"}}
if p, _ := Func(local); p != nil {
t.Fatalf("loopback: %v", p)
}
// magpie's own setting wins; "direct" turns it off
settings.Save(settings.Settings{Proxy: "socks5://127.0.0.1:1080"})
if p, _ := For(u); p == nil || p.String() != "socks5://127.0.0.1:1080" {
t.Fatalf("setting: %v", p)
}
settings.Save(settings.Settings{Proxy: "direct"})
if p, _ := For(u); p != nil {
t.Fatalf("direct: %v", p)
}
if err := settings.Save(settings.Settings{Proxy: "ftp://x"}); err == nil {
t.Fatal("ftp proxy accepted")
}
if settings.Save(settings.Settings{Proxy: "127.0.0.1:7890"}) != nil || filepath.Dir(settings.Path()) != filepath.Join(dir, "magpie") {
t.Fatal("host:port proxy refused")
}
}
func TestEnv(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
sysCache.at, sysCache.p = farFuture, Proxy{URL: "http://127.0.0.1:7890", Bypass: []string{"*.corp", "<local>"}}
t.Cleanup(func() { sysCache.at = zero })
get := func(env []string, k string) string {
v := ""
for _, e := range env {
if key, val, _ := strings.Cut(e, "="); key == k {
v = val
}
}
return v
}
// the system's proxy, where the environment names none
env := Env([]string{"PATH=/bin", "HOME=/h"})
if get(env, "HTTPS_PROXY") != "http://127.0.0.1:7890" || get(env, "all_proxy") != "http://127.0.0.1:7890" ||
get(env, "NO_PROXY") != "localhost,127.0.0.1,::1,.corp" || get(env, "PATH") != "/bin" {
t.Fatalf("system: %v", env)
}
// one the environment names is kept
env = Env([]string{"https_proxy=http://10.0.0.1:1", "NO_PROXY=x"})
if len(env) != 2 || get(env, "https_proxy") != "http://10.0.0.1:1" {
t.Fatalf("environment: %v", env)
}
// magpie's own replaces the environment's
settings.Save(settings.Settings{Proxy: "127.0.0.1:6152"})
env = Env([]string{"HTTPS_PROXY=http://10.0.0.1:1", "PATH=/bin"})
if get(env, "HTTPS_PROXY") != "http://127.0.0.1:6152" || get(env, "http_proxy") != "http://127.0.0.1:6152" ||
get(env, "NO_PROXY") != "localhost,127.0.0.1,::1" || len(env) != 9 {
t.Fatalf("setting: %v", env)
}
// and "direct" leaves none
settings.Save(settings.Settings{Proxy: "direct"})
if env = Env([]string{"HTTPS_PROXY=http://10.0.0.1:1", "PATH=/bin"}); len(env) != 1 {
t.Fatalf("direct: %v", env)
}
}
var farFuture = time.Now().Add(time.Hour)
var zero time.Time
// A provider's own proxy (#237) wins over the global one, "direct" over
// both, and each proxy is given one transport of its own, kept.
func TestWith(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
settings.Save(settings.Settings{Proxy: "127.0.0.1:6152"})
u := &url.URL{Scheme: "https", Host: "api.openai.com"}
req := func(choice string) *http.Request {
return (&http.Request{URL: u}).WithContext(With(context.Background(), choice))
}
if p, _ := Func(req("")); p == nil || p.String() != "http://127.0.0.1:6152" {
t.Fatalf("follow: %v", p)
}
if p, _ := Func(req("socks5://127.0.0.1:1080")); p == nil || p.String() != "socks5://127.0.0.1:1080" {
t.Fatalf("own: %v", p)
}
if p, _ := Func(req("direct")); p != nil {
t.Fatalf("direct: %v", p)
}
// a request for a provider that follows the global proxy, made while
// serving one set to its own or to none, follows the global one
// (#1522: Codex's search inside a turn of a provider set to direct)
for _, outer := range []string{"direct", "socks5://127.0.0.1:1080"} {
inner := (&http.Request{URL: u}).WithContext(With(With(context.Background(), outer), ""))
if p, _ := Func(inner); p == nil || p.String() != "http://127.0.0.1:6152" {
t.Fatalf("follow inside %s: %v", outer, p)
}
if c := Choice(inner.Context()); c != "" {
t.Fatalf("follow inside %s: choice %q", outer, c)
}
}
local := (&http.Request{URL: &url.URL{Scheme: "http", Host: "127.0.0.1:3425"}}).WithContext(With(context.Background(), "127.0.0.1:1"))
if p, _ := Func(local); p != nil {
t.Fatalf("loopback: %v", p)
}
d := Dispatch(&http.Transport{Proxy: Func}).(*dispatch)
a, b := d.transport("127.0.0.1:1"), d.transport("direct")
if a == b || d.transport("127.0.0.1:1") != a || len(d.own) != 2 {
t.Fatalf("transports: %d kept", len(d.own))
}
if p, _ := a.Proxy(req("")); p == nil || p.Host != "127.0.0.1:1" {
t.Fatalf("own transport: %v", p)
}
if p, _ := b.Proxy(req("")); p != nil {
t.Fatalf("direct transport: %v", p)
}
get := func(env []string, k string) string {
for _, e := range env {
if key, val, _ := strings.Cut(e, "="); key == k {
return val
}
}
return ""
}
if env := EnvWith("socks5://127.0.0.1:1080", []string{"PATH=/bin"}); get(env, "HTTPS_PROXY") != "socks5://127.0.0.1:1080" {
t.Fatalf("own env: %v", env)
}
if env := EnvWith("direct", []string{"HTTPS_PROXY=http://x:1", "PATH=/bin"}); len(env) != 1 {
t.Fatalf("direct env: %v", env)
}
if env := EnvWith("", []string{"PATH=/bin"}); get(env, "HTTPS_PROXY") != "http://127.0.0.1:6152" {
t.Fatalf("follow env: %v", env)
}
}