From 97bc02df0dbf4a9b6e42623837b76f8a0ca0b1a8 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 10:54:36 +0800 Subject: [PATCH] provider: Resolve, per-host connect addresses on a provider's own base URLs (quantum_lsfc on X) Provider.Resolve is taken where TakesInsecureTLS, TakesClientCert or PinHosts hold (a provider of the user's own, Remote magpie, a preset at a host of the user's), never a subscription's account or a vendor's host; plugins aren't covered (Bun's fetch has no hook for the address). Via puts the rule on the context. The gateway's offline check leaves a provider with a rule out of the hosts it dials by name. Broken by hand: Via without WithResolve turns TestResolveVia red; otherHosts keeping a provider with a rule turns TestOtherHostsSkipResolved red. Run on mote at the head rebased on origin/main b942a872b: go test -count=1 -timeout 45m ./internal/netproxy ./internal/provider ./internal/gateway ./internal/gui, all ok; gofmt -l clean; go vet ./... for darwin, linux (-tags nogui) and windows ok; provider-resolve.test.cjs (chromium and webkit; en zh zh-TW ja de; 1100 and 420) 60 of 60 ok. Rebased again on origin/main dea131174 and run on mote: netproxy, provider and gui (TestCoreScriptBudget) ok, gofmt and the three vets ok, provider-resolve 60 of 60 and i18n-keys ok; internal/gateway was not run again at that head (no new main commit touches the offline check or the dial). The 94 GUI files the editor's Save, Test and certificate rows reach, run at origin/main 436d507f0: 88 reds in 15 files, the same tests red on that main run alone (filed: #6424, and #6010 #6156 #5838 #4621 #4391 #5147 #6030 #6209 #4397 #4673 #6099). Not tried: a real managed network with an L4 SNI gateway (the reporter's), and the rule's dial on Windows or Linux beyond go vet. --- internal/gateway/offline.go | 6 ++- internal/gateway/offline_test.go | 24 ++++++++++ internal/provider/insecure_tls.go | 11 +++++ internal/provider/provider.go | 23 ++++++++++ internal/provider/proxy.go | 5 ++ internal/provider/resolve_test.go | 76 +++++++++++++++++++++++++++++++ 6 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 internal/provider/resolve_test.go diff --git a/internal/gateway/offline.go b/internal/gateway/offline.go index 029723212..c355f7c0b 100644 --- a/internal/gateway/offline.go +++ b/internal/gateway/offline.go @@ -126,7 +126,9 @@ func hostsOf(p provider.Provider) []string { // otherHosts is up to three hosts of providers other than p, reached // directly as p was, that tell whether the network is up: not p's own, -// nor one reached through a proxy, nor a near one. +// nor one reached through a proxy or at addresses of the user's choosing +// (Resolve: dialled by name here, it may not resolve on a managed +// network that works), nor a near one. func otherHosts(p provider.Provider) []string { own := map[string]bool{} for _, h := range hostsOf(p) { @@ -134,7 +136,7 @@ func otherHosts(p provider.Provider) []string { } var out []string for _, o := range provider.All() { - if o.ID == p.ID || o.Proxy != "" { + if o.ID == p.ID || o.Proxy != "" || len(o.Resolve) > 0 { continue } for _, h := range hostsOf(o) { diff --git a/internal/gateway/offline_test.go b/internal/gateway/offline_test.go index ab568d75e..ad3746911 100644 --- a/internal/gateway/offline_test.go +++ b/internal/gateway/offline_test.go @@ -138,3 +138,27 @@ func forgetNetwork() { offlineSeen.at, offlineSeen.off = time.Time{}, false offlineSeen.Unlock() } + +// A provider connected to addresses of the user's choosing (Resolve) is +// dialled by its name here, which a managed network that works may not +// resolve: its host says nothing of the network, as a proxied one's. +func TestOtherHostsSkipResolved(t *testing.T) { + fresh(t) + for _, p := range []provider.Provider{ + {ID: "mine", Name: "Mine", Key: "k", Models: []string{"m"}, Chat: "https://own.example.com/v1"}, + {ID: "gw", Name: "Gw", Key: "k", Models: []string{"m"}, Chat: "https://api.corp.example.com/v1", Resolve: map[string][]string{"api.corp.example.com": {"10.0.0.1"}}}, + {ID: "open", Name: "Open", Key: "k", Models: []string{"m"}, Chat: "https://api.open.example.com/v1"}, + } { + if err := provider.Save(p); err != nil { + t.Fatal(err) + } + } + wasNear := nearHost + t.Cleanup(func() { nearHost = wasNear }) + nearHost = func(string) bool { return false } + me, _ := provider.Find("mine") + got := otherHosts(*me) + if len(got) != 1 || got[0] != "api.open.example.com:443" { + t.Errorf("otherHosts = %v, want only the provider with no rule", got) + } +} diff --git a/internal/provider/insecure_tls.go b/internal/provider/insecure_tls.go index 7e0412cab..269411da7 100644 --- a/internal/provider/insecure_tls.go +++ b/internal/provider/insecure_tls.go @@ -32,6 +32,17 @@ func (p Provider) TakesClientCert() bool { return p.Account == nil && (p.Preset == "" || p.IsRemoteMagpie()) && !p.IsVertex() && hasEndpoint(p) && !subscriptionID(p.ID) } +// TakesResolve is whether the provider may have its hosts connected to +// addresses of the user's choosing (Resolve): one the user added with a base +// URL of its own, as TakesInsecureTLS, and a preset's at a host of its own +// (PinHosts' rule, never the vendor's), so a managed network's gateway can +// stand in for a company's relay. A subscription's or a plugin's account +// never does: a plugin's requests are made by its host, which has no way +// to be told where to connect. +func (p Provider) TakesResolve() bool { + return p.TakesInsecureTLS() || p.TakesClientCert() || len(p.PinHosts()) > 0 +} + // PinHosts are the base URLs a trusted server certificate (ServerPin) is // taken at: every endpoint of a provider that takes a client certificate // (TakesClientCert), and of a preset's only the https:// addresses the user diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 18bc7771d..125dfd8c6 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -513,6 +513,19 @@ type Provider struct { // usual. Taken where TakesClientCert is; see Via and PeekServerCert. ServerPin string `json:"serverPin,omitempty"` + // Resolve connects a host name of the provider's own base URLs to + // the places listed, as curl's --resolve does (quantum_lsfc on X: a + // managed network sends some domains to an internal L4 SNI gateway, + // an IP in the base URL fails, and /etc/hosts can't be edited): only + // the TCP connection moves, the URL's name stays for TLS and Host. A + // place is an IP address (with a port if it has its own) or + // "dns:", looked up at each connection; they are tried in + // order, and when none connects the name is dialled as usual. Not + // used through a proxy, which connects to the name itself. Taken where + // TakesResolve is; a plugin's requests are made by its host, which has + // no way to be told. See netproxy.WithResolve and CheckResolve. + Resolve map[string][]string `json:"resolve,omitempty"` + // Proxy is the proxy magpie's requests to this provider go through // (#237: Codex through one, a vendor at home without): "" follows // the global one (Settings' Proxy, the environment's, the system's), @@ -1308,6 +1321,16 @@ func Save(p Provider) error { if p.InsecureTLS && !p.TakesInsecureTLS() { return fmt.Errorf("%s is a preset's: only a provider you added with your own base URL can skip the certificate check", p.Name) } + if len(p.Resolve) > 0 { + if !p.TakesResolve() { + return fmt.Errorf("%s: a connect address is only for a provider you added with a base URL of its own, or a preset you pointed at your own server", p.ID) + } + r, err := netproxy.CheckResolve(p.Resolve) + if err != nil { + return fmt.Errorf("%s: %w", p.ID, err) + } + p.Resolve = r + } if p.HTTP1 && !p.TakesInsecureTLS() { return fmt.Errorf("%s is a preset's: only a provider you added with your own base URL can be held to HTTP/1.1", p.Name) } diff --git a/internal/provider/proxy.go b/internal/provider/proxy.go index fc97ebf20..7d1547f64 100644 --- a/internal/provider/proxy.go +++ b/internal/provider/proxy.go @@ -18,6 +18,8 @@ import ( // (InsecureTLS) has its own hosts' taken as they are, no other host's, // and one whose server certificate the user trusted (ServerPin) has them // taken with that certificate alone; +// one with connect addresses (Resolve) has its hosts' connections made +// there; // one asked to be spoken HTTP/1.1 to (HTTP1) is, at its own hosts alone; // one whose turns go over the Responses WebSocket (Socket) has them go // there, at its Responses URL alone (SocketTurn). @@ -29,6 +31,9 @@ func (p Provider) Via(ctx context.Context) context.Context { if p.HTTP1 && p.TakesInsecureTLS() { ctx = netproxy.WithHTTP1(ctx, p.UncheckedHosts()...) } + if len(p.Resolve) > 0 && p.TakesResolve() { + ctx = netproxy.WithResolve(ctx, p.Resolve) + } cert, key, ca := p.ClientCert, p.ClientKey, p.ServerCA if !p.TakesClientCert() { cert, key, ca = "", "", "" diff --git a/internal/provider/resolve_test.go b/internal/provider/resolve_test.go new file mode 100644 index 000000000..4c5713f58 --- /dev/null +++ b/internal/provider/resolve_test.go @@ -0,0 +1,76 @@ +package provider + +import ( + "context" + "crypto/tls" + "net" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// A saved rule is held to what netproxy.CheckResolve takes, and only a +// provider the user pointed at their own server keeps one. +func TestResolveSaved(t *testing.T) { + isolate(t) + own := Provider{ID: "corp", Name: "Corp", Key: "k", Models: []string{"m"}, Chat: "https://api.example.com/v1", + Resolve: map[string][]string{" API.example.com ": {"dns:gw.corp.test", "10.0.0.1", "10.0.0.1"}}} + if err := Save(own); err != nil { + t.Fatal(err) + } + got, _ := Find("corp") + if r := got.Resolve["api.example.com"]; len(r) != 2 || r[0] != "dns:gw.corp.test" || r[1] != "10.0.0.1" { + t.Errorf("kept %v", got.Resolve) + } + bad := own + bad.ID = "bad" + bad.Resolve = map[string][]string{"api.example.com": {"gateway.corp"}} + if err := Save(bad); err == nil || !strings.Contains(err.Error(), "resolve: ") { + t.Errorf("a place that is a bare name: %v", err) + } + sub := Provider{ID: "codex", Name: "Codex", Chat: "https://api.example.com/v1", Resolve: map[string][]string{"api.example.com": {"10.0.0.1"}}} + if err := Save(sub); err == nil { + t.Error("a subscription's id took a connect address") + } + vendor := Provider{ID: "v2", Name: "V", Key: "k", Models: []string{"m"}, Preset: "deepseek", Chat: "https://api.deepseek.com/v1", Resolve: map[string][]string{"api.deepseek.com": {"10.0.0.1"}}} + if err := Save(vendor); err == nil { + t.Error("a preset at its vendor's own host took a connect address") + } +} + +// Via carries the rule to the requests made for the provider: the name in +// its base URL is connected to the address the rule gives, and the server +// is still asked for that name (SNI and Host). An account of a subscription +// takes no rule. +func TestResolveVia(t *testing.T) { + outside := dialsOut(t) + var sni, host string + srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { host = r.Host })) + srv.EnableHTTP2 = false + srv.StartTLS() + t.Cleanup(srv.Close) + srv.TLS.GetConfigForClient = func(h *tls.ClientHelloInfo) (*tls.Config, error) { sni = h.ServerName; return nil, nil } + _, port, _ := net.SplitHostPort(srv.Listener.Addr().String()) + p := Provider{ID: "corp", Name: "Corp", Key: "k", InsecureTLS: true, Chat: "https://gw.invalid:" + port + "/v1", + Resolve: map[string][]string{"gw.invalid": {"127.0.0.1"}}} + if !p.TakesResolve() { + t.Fatal("a provider with its own base URL takes a rule") + } + req, _ := http.NewRequestWithContext(p.Via(context.Background()), http.MethodGet, p.Chat, nil) + res, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatal(err) + } + res.Body.Close() + if sni != "gw.invalid" || host != "gw.invalid:"+port { + t.Errorf("SNI %q, Host %q: the URL's name goes to the server", sni, host) + } + if d := outside(); len(d) != 0 { + t.Errorf("dialled %v: the rule's address only", d) + } + p.Account = &Account{} + if p.TakesResolve() { + t.Error("an account of a subscription takes a rule") + } +}