Files
magpie/internal/gateway/lazycat_test.go
T
yetone 16a2dfac9c lan: a LazyCat app offers the box's LAN address, or says how to forward the port
mtoolstec on X (after v0.1.3432) runs magpie from the magpie-lzcapp
package on a LazyCat box. Settings -> Share on local network offered
https://magpie.<box>.heiyu.space/v1, which only devices with LazyCat's
client reach, and http://172.28.3.65:3425/v1, the container bridge,
named "Other containers on this host", which nothing off the box
reaches. Other devices on his LAN wanted http://192.168.0.100:3425/v1.
The package runs magpie on the app's bridge network, and LazyCat
publishes an app's ports only on heiyu.space, not on the box's LAN
address. Its env names the app's domain, never the box's LAN IP.

- On the box's own network (network_mode: host, as LazyCat's Emby, Lucky
  and LAN Sheriff run), magpie now finds the box's LAN addresses. It sees
  the box's bridges/veths, or host.lzcapp is one of its own addresses, and
  takes the IPv4 of the default route's interface, minus host.lzcapp.
  LANURLs lists them first and the app domain after. A MAGPIE_PUBLIC_URL
  of the user's own (not the domain) stays first, and a pinned
  MAGPIE_ADDR still serves only that address. The sidebar names the box
  address, not the domain.
- On a bridge network that address can't be known. The domain is listed
  alone, named "LazyCat client", and the container-bridge address is no
  longer offered under LazyCat (ContainerHostURL ""). The row under the
  address, in Settings and on the Gateway page, says to forward the port
  with LazyCat's "LAN port forwarding" app (局域网端口转发工具). The CLI
  banner (serve, web) says the same.
- New settings/gateway fields: lanLazyCat, lanLazyCatBox; lazyCat,
  lazyCatBox (and boot.js's sideGateway). Strings are in zh, zh-TW, ja
  and de.
- host.lzcapp is looked up only under LAZYCAT_APP_DOMAIN, with a 2s
  timeout and no lock held. An answer is kept; a failure is retried after
  a minute.

Package side: the magpie-lzcapp package (lazycat-contrib, czyt) needs
network_mode: host plus host.lzcapp upstreams and the net.* permissions,
or its usage needs the port-forward steps. An issue is filed there.

Run (mote, macOS, at this tree on origin/main ce6ae593c; then rebased
onto ab084fdf2; api.go, providers.go and i18n.js changed there too and
merged clean):
- gofmt -l: clean. go vet: darwin for gateway/gui/cli; CGO_ENABLED=0
  GOOS=linux -tags nogui ./... and GOOS=windows ./...: ok.
- go test -timeout 45m ./...: ok (before the string reword; after it,
  internal/cli and internal/gateway again: ok).
- wsl-run (Ubuntu 24.04): the LazyCat/default-route/LAN tests in
  internal/gateway and internal/cli: ok.
- GUI: lan-lazycat (new: 2 engines x 5 languages x 1100/420px, 20 tests),
  i18n-keys, i18n-en-no-cjk, gui-de/ja/zh-tw, lan-docker and
  side-gateway-lan: 85 pass, 0 fail. A wider set (lan-*, gateway-*, connect-*,
  key-page, sidebar, phone-*, remote-magpie and others; 835 tests) was
  run before the reword. Its reds were the i18n placeholder check
  (fixed: no <box IP> in the string), and phone-connect-menu 420 (#4706,
  #4707) and remote-magpie (#4398). Those last two are red the same alone
  on origin/main b03a37476 on mote.
- Real Linux containers (Docker on xiaomi-gpu), with a test binary
  printing what the real functions find. LAZYCAT_APP_DOMAIN was set and
  host.lzcapp added as host-gateway.
  - bridge: ifaces [lo eth0], LANURLs [https://magpie.snyht3.heiyu.space],
    ContainerHostURL "".
  - --network host: ifaces [lo services1 eth0 docker0], defaultIfaces
    [eth0], LANURLs [http://192.168.65.3:3425 https://magpie...],
    ContainerHostURL "".
- Broken by hand: LANURLs without the box branch and ContainerHostURL
  without the LazyCat check turn TestLazyCatOffersTheBoxAddress red (6
  failures). The old settings.js turns lan-lazycat red (10 of 10,
  chromium).

Not tried: no LazyCat box is at hand. Not checked on a real box: lzcos's
interface names on the host network, where host.lzcapp points in host
mode, and which interface holds the default route.

At the rebased head: gofmt clean; go vet of internal/gui, gateway and
cli; their Budget/LazyCat/LAN/Container/PublicURL/Parity tests ok; GUI
lan-lazycat, i18n-keys, i18n-en-no-cjk, lan-docker, side-gateway-lan:
76 pass, 0 fail.
2026-11-10 07:34:29 +08:00

162 lines
6.3 KiB
Go

package gateway
import (
"net"
"slices"
"testing"
)
// lazyCatNet holds what a LazyCat box's network looks like to magpie for a
// test: the interfaces' names, their addresses, the route table and
// host.lzcapp's answer; nothing is looked up.
func lazyCatNet(t *testing.T, names []string, addrs map[string][]string, table string, lzc ...string) {
t.Helper()
t.Cleanup(func(ifn func() []string, ips func(string) []net.IP, rt func() string, lz func() []net.IP, hi func() []net.IP, in func() bool) func() {
return func() {
ifaceNames, ifaceIPs, routeTable, lzcHostIPs, hostIPs, inThisContainer = ifn, ips, rt, lz, hi, in
}
}(ifaceNames, ifaceIPs, routeTable, lzcHostIPs, hostIPs, inThisContainer))
parse := func(ss []string) []net.IP {
var out []net.IP
for _, s := range ss {
out = append(out, net.ParseIP(s))
}
return out
}
ifaceNames = func() []string { return names }
ifaceIPs = func(name string) []net.IP { return parse(addrs[name]) }
routeTable = func() string { return table }
lzcHostIPs = func() []net.IP { return parse(lzc) }
hostIPs = func() []net.IP {
var out []net.IP
for _, n := range names {
out = append(out, parse(addrs[n])...)
}
return out
}
inThisContainer = func() bool { return true }
}
const (
// the box: its LAN link enp2s0 holds the default route, beside the
// apps' bridges
boxRoutes = "Iface\tDestination\tGateway\tFlags\nenp2s0\t00000000\t0100A8C0\t0003\nenp2s0\t0000A8C0\t00000000\t0001\nlzc-br0\t00001CAC\t00000000\t0001\n"
// an app's container on the box's bridge: eth0 to 172.28.3.65
bridgeRoutes = "Iface\tDestination\tGateway\tFlags\neth0\t00000000\t41031CAC\t0003\neth0\t00001CAC\t00000000\t0001\n"
)
// mtoolstec on X: magpie in a LazyCat app offered the app's domain, which
// only devices with LazyCat's client reach, and the box's bridge
// (172.28.3.65), which nothing off the box reaches; other devices on the
// home network wanted http://192.168.0.100:3425/v1. On the box's network
// (network_mode: host) the box's LAN address is offered first, then the
// domain; a MAGPIE_PUBLIC_URL of the user's own stays first. On a bridge
// network the box's address can't be known: the domain alone, and no
// bridge address for other containers.
func TestLazyCatOffersTheBoxAddress(t *testing.T) {
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
t.Setenv("MAGPIE_PUBLIC_URL", "")
t.Setenv("LAZYCAT_APP_DOMAIN", "magpie.snyht3.heiyu.space")
const lc = "https://magpie.snyht3.heiyu.space"
box := Scheme() + "://192.168.0.100:3425"
// host network: the box's interfaces, docker-style bridges among them
lazyCatNet(t, []string{"lo", "enp2s0", "lzc-br0", "veth1a2b"}, map[string][]string{
"lo": {"127.0.0.1"}, "enp2s0": {"192.168.0.100", "fe80::1"}, "lzc-br0": {"172.28.0.1"},
}, boxRoutes, "172.28.0.1")
if got, want := LANURLs(), []string{box, lc}; !slices.Equal(got, want) {
t.Errorf("host network: %v, want %v", got, want)
}
if !LazyCatBox() {
t.Error("host network: not the box's")
}
if got := ContainerHostURL(); got != "" {
t.Errorf("host network: other containers offered %q", got)
}
// the box's network found by host.lzcapp alone, with no bridge named
// as Docker's
lazyCatNet(t, []string{"lo", "enp2s0", "lzc0"}, map[string][]string{
"enp2s0": {"192.168.0.100"}, "lzc0": {"198.18.0.1"},
}, boxRoutes, "198.18.0.1")
if got, want := LANURLs(), []string{box, lc}; !slices.Equal(got, want) {
t.Errorf("host.lzcapp on this network: %v, want %v", got, want)
}
// host.lzcapp on the default route's interface is not the LAN's
lazyCatNet(t, []string{"lo", "enp2s0", "veth0"}, map[string][]string{
"enp2s0": {"192.168.0.100", "172.28.0.1"},
}, boxRoutes, "172.28.0.1")
if got, want := LANURLs(), []string{box, lc}; !slices.Equal(got, want) {
t.Errorf("host.lzcapp on the LAN link: %v, want %v", got, want)
}
// the user's own address first
t.Setenv("MAGPIE_PUBLIC_URL", "https://ai.example.com")
if got, want := LANURLs(), []string{"https://ai.example.com", box, lc}; !slices.Equal(got, want) {
t.Errorf("MAGPIE_PUBLIC_URL: %v, want %v", got, want)
}
// the package's MAGPIE_PUBLIC_URL, the domain, is said once
t.Setenv("MAGPIE_PUBLIC_URL", lc+"/")
if got, want := LANURLs(), []string{box, lc}; !slices.Equal(got, want) {
t.Errorf("MAGPIE_PUBLIC_URL the domain: %v, want %v", got, want)
}
t.Setenv("MAGPIE_PUBLIC_URL", "")
// MAGPIE_ADDR pinned to one address: that one alone is served
t.Setenv("MAGPIE_ADDR", "192.168.0.100:3425")
if got, want := LANURLs(), []string{lc}; !slices.Equal(got, want) {
t.Errorf("pinned: %v, want %v", got, want)
}
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
// bridge network: the container's eth0 alone, host.lzcapp the bridge's
lazyCatNet(t, []string{"lo", "eth0"}, map[string][]string{
"eth0": {"172.28.3.66"},
}, bridgeRoutes, "172.28.3.65")
if got, want := LANURLs(), []string{lc}; !slices.Equal(got, want) {
t.Errorf("bridge network: %v, want %v", got, want)
}
if LazyCatBox() {
t.Error("bridge network: said to be the box's")
}
if got := ContainerHostURL(); got != "" {
t.Errorf("bridge network: other containers offered %q", got)
}
if LazyCatURL() != lc {
t.Errorf("LazyCatURL: %q", LazyCatURL())
}
// outside LazyCat nothing changes: a NAS's host network offers its own
t.Setenv("LAZYCAT_APP_DOMAIN", "")
lazyCatNet(t, []string{"lo", "enp2s0", "docker0"}, map[string][]string{
"enp2s0": {"192.168.0.100"},
}, boxRoutes)
if LazyCatBox() || LazyCatURL() != "" {
t.Error("outside LazyCat: said to be one")
}
}
func TestDefaultIfaces(t *testing.T) {
for _, c := range []struct {
table string
want []string
}{
{boxRoutes, []string{"enp2s0"}},
{bridgeRoutes, []string{"eth0"}},
{"Iface\tDestination\tGateway\tFlags\nwlan0\t00000000\t0101A8C0\t0003\neth0\t00000000\t0100A8C0\t0003\nwlan0\t00000000\t0101A8C0\t0003\n", []string{"wlan0", "eth0"}},
{"Iface\tDestination\tGateway\tFlags\neth0\t0000A8C0\t00000000\t0001\n", nil},
{"", nil},
} {
if got := defaultIfaces(c.table); !slices.Equal(got, c.want) {
t.Errorf("%q: %v, want %v", c.table, got, c.want)
}
}
}
// A lookup's answer is kept; a failure is asked again only after retry;
// outside LazyCat nothing is asked.
func TestCachedLookupOutsideLazyCat(t *testing.T) {
t.Setenv("LAZYCAT_APP_DOMAIN", "")
c := &cachedLookup{name: "host.lzcapp.invalid", retry: 0}
if got := c.get(); got != nil || !c.at.IsZero() {
t.Errorf("outside LazyCat: %v, asked at %v", got, c.at)
}
}