Files
magpie/internal/netproxy/localname_test.go
T
yetone 380032cda1 providers: Send requests unmasked is the user's switch at any address, with a caution while on for one not local (AcMilan)
AcMilan (Discord) runs a vLLM at http://llm.corp.lan:8000, and the
editor never offered "Send requests unmasked": the switch showed only
for a localhost, *.local or private-IP address. Asked, he said the
choice should be the user's for each provider.

provider.SkipsRedaction is now: Unredacted, no signed-in account, not
another magpie, and at least one address. Nothing is looked up on the
request path. The editor shows the row for every provider but an
account or remote magpie. While it is ticked, an amber caution under it
names an address not on this computer or the local network (awayHost).
A name of the user's own DNS is asked about, only while ticked, through
POST /api/local-hosts (provider.LocalHost -> netproxy.LocalName: every
address loopback, private or link-local, 400ms lookup, kept 1 minute).
That answer only words the caution. CLI help and `provider show` say
the same. The 'away' i18n line is in zh, zh-TW, ja and de. To stay
inside the core script budget, a few nearby editor comments were cut
down (no raise).

Run (logs in the worktree's logs/):
- Full Go suite on mote at base 024393fbb (go test ./... -timeout 45m).
  Every package is ok except internal/gui. Its reds are the AppKit
  window tests (#4125, #2645, #1936, #3949, #3178) and
  TestRestartHandsTheGatewayOver (#4167), plus TestCoreScriptBudget,
  which was mine and is fixed. At the pushed head (rebased on
  995f84a0f): the gui, provider, netproxy, gateway and cli tests for
  this area, with TestCoreScriptBudget and TestI18n: ok.
- GOOS=linux go vet -tags nogui ./internal/... and GOOS=windows go vet
  on the touched packages: ok. gofmt -l is clean.
- provider-unmasked.test.cjs and decide-endpoint-label.test.cjs: 34/34
  in Chromium and 34/34 in WebKit (Linux, wsl-run), on the final app.js.
- The 155 GUI files that open a provider editor, both engines (WebKit
  on Linux, Chromium on mote). Each file red there was rerun on Linux
  beside the base 024393fbb. The one red this change caused was
  decide-endpoint-label: a /api/local-hosts fetch cut off by navigation
  showed as a console error. That is why names are now asked only while
  the switch is on. The rest are red on the base too and have issues
  (#4431 and others). mote's browsers were crashing (#4125), so
  Chromium was compared on Linux.

Broken by hand:
- The old local-only gate put back in SkipsRedaction: TestSkipsRedaction
  (lan name, vendor, public IP, own proxy) and
  TestLANNameUnredactedProvider went red.
- awayHost returning "": 12 caution tests went red.
- The "only while on" guard removed: 4 "nothing is asked while the
  switch is off" tests went red.

Not tried: a real LAN DNS server, or AcMilan's own setup; WebKit on
macOS (mote's WebKit can't launch, #4125); the Windows app; the whole
GUI suite beyond the provider-editor files.
2026-10-31 00:44:59 +08:00

66 lines
1.9 KiB
Go

package netproxy
import (
"context"
"net"
"sync/atomic"
"testing"
)
// lookups answers names from m, several addresses each, and counts the
// lookups made.
func lookups(t *testing.T, m map[string][]string) *atomic.Int32 {
t.Helper()
n := new(atomic.Int32)
t.Cleanup(SetLookup(func(_ context.Context, host string) ([]net.IPAddr, error) {
n.Add(1)
ips, ok := m[host]
if !ok {
return nil, &net.DNSError{Err: "no such host", Name: host, IsNotFound: true}
}
var out []net.IPAddr
for _, s := range ips {
out = append(out, net.IPAddr{IP: net.ParseIP(s)})
}
return out, nil
}))
return n
}
// AcMilan on Discord: a name of the user's own DNS counts as local when
// every address it resolves to is loopback, private or link-local; one
// resolving to a public address, to a public and a private one, or not at
// all, is not.
func TestLocalName(t *testing.T) {
lookups(t, map[string][]string{
"llm.corp.lan": {"192.168.1.20"},
"gpu.home.arpa": {"10.0.0.7", "fd00::7"},
"loop.test": {"127.0.0.1"},
"linklocal.lan": {"169.254.10.2"},
"public.example": {"203.0.113.7"},
"mixed.example": {"10.0.0.2", "203.0.113.7"},
"cgnat.example": {"100.101.102.103"},
"nothing.example": {},
})
for host, want := range map[string]bool{
"llm.corp.lan": true, "LLM.corp.lan.": true, "gpu.home.arpa": true, "loop.test": true, "linklocal.lan": true,
"public.example": false, "mixed.example": false, "cgnat.example": false, "nothing.example": false,
"gone.lan": false, "192.168.1.20": false, "": false,
} {
if got := LocalName(host); got != want {
t.Errorf("%q: %v, want %v", host, got, want)
}
}
}
// A lookup that doesn't answer in lanWait is unknown, so not local.
func TestLocalNameSlowLookupIsNot(t *testing.T) {
t.Cleanup(SetLookup(func(ctx context.Context, host string) ([]net.IPAddr, error) {
<-ctx.Done()
return nil, ctx.Err()
}))
if LocalName("slow.lan") {
t.Fatal("a lookup that timed out counted as local")
}
}