Files
magpie/internal/wslrun/hostat_test.go
T
yetone cff530079f wsl: a bridged distro reaches Windows on the bridge's adapter, not the LAN router
ecalpal_ on Discord (magpie 0.1.2689, Windows 11, WSL 3.0.3.0, Ubuntu
22.04.5; .wslconfig vmSwitch=WSL_Bridge_Switch, networkingMode=Bridged,
dhcp=false). In bridged networking the distro is a peer on the LAN and its
default route is the router, 192.168.9.1. magpie took that next hop for
Windows: wsl.json got {"net":"bridged","gateway":"192.168.9.1"} and Pi's
models.json a baseUrl on 192.168.9.1:3425, where nothing listens, with the
gateway token rather than the WSL key. serveWSL already listened on
192.168.9.147, Windows' address on vEthernet (WSL_Bridge_Switch).

wslrun.HostAt now picks the address: "" where the distro's 127.0.0.1 is
Windows' (mirrored, consomme, virtioproxy, as before); the next hop as it
is when it is Windows' own address on a WSL adapter (NAT, unchanged);
otherwise Windows' address on the one WSL adapter whose network holds
that next hop (bridged: 192.168.9.1 in 192.168.9.147/24 -> .147). A next
hop on no WSL network is kept as it was. The adapter list moved from
gateway.wslNets to wslrun.Nets (gateway.WSLNets still wraps it), so both
probes use it: agent's parseProbe (distro.Gateway, what place writes into
agents) and wslrun's (Tool.Host, a 127.0.0.1 proxy handed to a WSL CLI),
which had the same bug.

The key follows the address: keyAt(http://192.168.9.147:3425) is on a WSL
net (OnWSLNet), so agents there get the WSL key.

Existing users: on the next start, the first probe of the running distro
writes the new gateway into wsl.json and keeps 192.168.9.1 in Was. The
startup SyncCatalog then rewrites Pi's provider block (baseUrl and apiKey)
at the new address; keptaway treats the old router address as magpie's
because it is in Was. A distro that isn't running at start is fixed at its
first probe after it runs, and its agents' files at the next sync after
that. Codex in WSL, which keeps its own base_url, gets the existing
"unreachable" drift with "Use 192.168.9.147:3425" (wslMovedFrom), not an
automatic rewrite.

Tests (fixtures are the reporter's values):
- wslrun TestHostAt: bridged/nat/mirrored/consomme each with its own route
  and adapters; TestParseProbeBridged (Tool.Host and proxy).
- agent TestWSLBridgedReachesWindowsOnTheBridge (bridged .147, nat
  172.20.0.1 unchanged, mirrored 127.0.0.1) and
  TestWSLBridgedOldRouterWrittenOver: wsl.json and Pi's models.json as
  0.1.2689 wrote them, then a probe and a sync: baseUrl .147, apiKey the
  WSL key, wsl.json rewritten.

Run:
- Broken by hand: probe code put back to HostLoopback only. Both new agent
  tests and TestParseProbeBridged FAIL (Gateway/Host 192.168.9.1).
- mote: gofmt -l empty; go vet, GOOS=windows go vet, GOOS=linux go vet
  -tags nogui for wslrun, gateway, agent; GOOS=windows go build: ok.
- mote: go test -timeout 45m ./internal/wslrun ./internal/agent
  ./internal/gateway: ok (wslrun 1.3s, agent 231s, gateway 855s)
- xiaomi-gpu (win-run): go test ./internal/wslrun ./internal/gateway
  -run 'HostAt|ParseProbe|WSL' ok; ./internal/agent -run
  'WSL|ParseProbe|Omp' ok. A first try with a fallback (bridged + one WSL
  adapter -> that adapter) went red there on TestParseProbe and
  TestWSLNetworkingFromWslinfo, because the box's real NAT adapter
  (vEthernet (WSL (Hyper-V firewall)) 172.17.32.1/20) was taken for the
  bridge; the fallback was dropped.
- xiaomi-gpu, real adapters: HostAt("nat", "172.17.32.1", Nets()) =
  172.17.32.1; magpie-desktop is NAT (wslinfo nat, default via
  172.17.32.1), so its value is unchanged.

Not tried: a real bridged WSL (none on the LAN; magpie-desktop's
networking was not changed), a real magpie start against an existing
wrong config, and the full go test ./... before the push (run after).
2026-11-06 10:49:35 +08:00

76 lines
2.6 KiB
Go

package wslrun
import (
"net"
"testing"
)
func ipNet(t *testing.T, cidr string) *net.IPNet {
t.Helper()
ip, n, err := net.ParseCIDR(cidr)
if err != nil {
t.Fatal(err)
}
n.IP = ip
return n
}
// ecalpal_ on Discord (WSL 3.0.3.0, .wslconfig vmSwitch=WSL_Bridge_Switch,
// networkingMode=Bridged, dhcp=false): the distro's default route is the
// LAN's router, 192.168.9.1, and Windows is at 192.168.9.147/24 on
// vEthernet (WSL_Bridge_Switch). Each mode is checked with its own values.
func TestHostAt(t *testing.T) {
bridge := []*net.IPNet{ipNet(t, "192.168.9.147/24")}
nat := []*net.IPNet{ipNet(t, "172.20.0.1/20")}
both := append(append([]*net.IPNet{}, nat...), bridge...)
for _, c := range []struct {
mode, route string
nets []*net.IPNet
want string
}{
{"bridged", "192.168.9.1", bridge, "192.168.9.147"},
{"bridged", "192.168.9.1", both, "192.168.9.147"},
// a .wslconfig-only bridged WSL (no wslinfo) is told by the network
{"", "192.168.9.1", bridge, "192.168.9.147"},
// the router on no network Windows has on WSL's adapters: nothing
// tells which is Windows, NAT's adapter being up beside a bridge
{"bridged", "10.0.0.1", bridge, "10.0.0.1"},
{"bridged", "10.0.0.1", nat, "10.0.0.1"},
// nothing to tell by: kept
{"bridged", "192.168.9.1", nil, "192.168.9.1"},
// NAT: the next hop is Windows' own address there
{"nat", "172.20.0.1", nat, "172.20.0.1"},
{"nat", "172.20.0.1", both, "172.20.0.1"},
{"nat", "172.20.0.1", nil, "172.20.0.1"},
{"", "172.20.0.1", nat, "172.20.0.1"},
// mirrored and consomme: Windows' 127.0.0.1
{"mirrored", "192.168.9.1", bridge, ""},
{"consomme", "198.18.0.2", nat, ""},
{"bridged", "", bridge, ""},
} {
if got := HostAt(c.mode, c.route, c.nets); got != c.want {
t.Errorf("%s via %s on %v: %q, want %q", c.mode, c.route, c.nets, got, c.want)
}
}
}
// A WSL command found in a bridged distro reaches Windows at its address on
// the bridge, so a proxy at Windows' 127.0.0.1 is handed on there.
func TestParseProbeBridged(t *testing.T) {
old := Nets
t.Cleanup(func() { Nets = old })
Nets = func() []*net.IPNet { return []*net.IPNet{ipNet(t, "192.168.9.147/24")} }
out := "bin:/home/me/.local/bin/claude\nroute:default via 192.168.9.1 dev eth0 proto static\nnet:bridged\n"
tool, ok := parseProbe("Ubuntu-22.04", out)
if !ok || tool.Host != "192.168.9.147" {
t.Fatalf("%+v", tool)
}
if p := tool.proxy("http://127.0.0.1:7890"); p != "http://192.168.9.147:7890" {
t.Errorf("proxy: %s", p)
}
tool, _ = parseProbe("Ubuntu", "bin:/usr/bin/claude\nroute:default via 172.20.0.1 dev eth0\nnet:nat\n")
if tool.Host != "172.20.0.1" {
t.Errorf("nat: %+v", tool)
}
}