Files
magpie/internal/netproxy/localname.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

87 lines
2.4 KiB
Go

package netproxy
import (
"context"
"net"
"strings"
"sync"
"time"
)
// LocalName says whether a name of the user's own DNS
// (http://llm.corp.lan:8000, AcMilan on Discord) resolves only to this
// computer or the local network. The provider editor asks it
// (/api/local-hosts) to tell whether "Send requests unmasked" needs its
// caution; no request is routed or dialled by it.
// localKeep is how long what LocalName found for a name is kept.
const localKeep = time.Minute
var localCache = struct {
sync.Mutex
m map[string]lanSeen
}{m: map[string]lanSeen{}}
// LocalName reports whether host, a name, resolves (in lanWait) to at
// least one address and every address it resolves to is on this computer
// or the local network (LocalIP). A name that doesn't resolve, or doesn't
// in time, is not: what can't be read is unknown, and unknown is masked.
func LocalName(host string) bool {
host = strings.ToLower(strings.TrimSuffix(host, "."))
if host == "" || net.ParseIP(host) != nil {
return false
}
localCache.Lock()
s, ok := localCache.m[host]
localCache.Unlock()
if ok && time.Since(s.at) < localKeep {
return s.lan
}
ctx, cancel := context.WithTimeout(context.Background(), lanWait)
addrs, err := lanLookup(ctx, host)
cancel()
local := err == nil && allLocal(addrs)
localCache.Lock()
if len(localCache.m) >= 256 {
clear(localCache.m)
}
localCache.m[host] = lanSeen{lan: local, at: time.Now()}
localCache.Unlock()
return local
}
// LocalIP says ip is on this computer or the local network: a loopback,
// private (10/8, 172.16/12, 192.168/16, fc00::/7) or link-local address.
// Carrier-grade NAT (100.64/10) is not: it is a Tailscale address, but
// also an ISP's.
func LocalIP(ip net.IP) bool {
return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast()
}
func allLocal(addrs []net.IPAddr) bool {
for _, a := range addrs {
if !LocalIP(a.IP) {
return false
}
}
return len(addrs) > 0
}
// SetLookup has names resolved by f, for a test that must not reach the
// network, and forgets what earlier lookups found; restore puts the
// system's resolver back.
func SetLookup(f func(ctx context.Context, host string) ([]net.IPAddr, error)) (restore func()) {
old := lanLookup
lanLookup = f
forget := func() {
lanCache.Lock()
clear(lanCache.m)
lanCache.Unlock()
localCache.Lock()
clear(localCache.m)
localCache.Unlock()
}
forget()
return func() { lanLookup = old; forget() }
}