Files
magpie/internal/provider/resolve_test.go
T
yetone 97bc02df0d 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.
2026-11-13 14:30:19 +08:00

77 lines
2.9 KiB
Go

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")
}
}