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.
143 lines
4.6 KiB
Go
143 lines
4.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// A LazyCat (懒猫微服) app's container is on a bridge network of the box's
|
|
// own: its address (172.28.x.x) and the bridge's (the container's default
|
|
// route, 172.28.3.65) are reached from nothing outside the box. LazyCat
|
|
// serves the app at its domain (lazyCatURL), which only devices running
|
|
// LazyCat's client resolve, and publishes no container port on the box's
|
|
// LAN address. So "Share on local network" offered
|
|
// https://magpie.<box>.heiyu.space and http://172.28.3.65:3425, and neither
|
|
// was the http://192.168.0.100:3425 other devices on the home network use
|
|
// (mtoolstec on X, after v0.1.3432).
|
|
//
|
|
// A package whose service runs on the box's network (network_mode: host,
|
|
// as LazyCat's Emby, Lucky and LAN Sheriff do) has the box's interfaces,
|
|
// and the gateway listens on its LAN address itself: those addresses are
|
|
// offered first. On a bridge network they can't be known (LazyCat names
|
|
// the box's domain to a container, never its LAN address), and Settings
|
|
// says how the port gets there.
|
|
|
|
// LazyCatURL is a LazyCat app's own address, served to devices with
|
|
// LazyCat's client; "" outside LazyCat.
|
|
func LazyCatURL() string { return lazyCatURL() }
|
|
|
|
// LazyCatBox: magpie runs in a LazyCat app on the box's own network, so the
|
|
// addresses it finds are the box's, which other devices on its LAN reach.
|
|
func LazyCatBox() bool { return lazyCatURL() != "" && len(lazyCatBoxHosts()) > 0 }
|
|
|
|
// lazyCatBoxHosts are the box's LAN addresses while magpie runs on its
|
|
// network: the IPv4 addresses (as LANHosts takes them) of the interfaces
|
|
// the default route leaves by, which is the box's link to the router, not
|
|
// one of its container bridges or the virtual address other LazyCat apps
|
|
// reach it at (host.lzcapp). nil on a bridge network, where the default
|
|
// route is the container's own eth0.
|
|
func lazyCatBoxHosts() []string {
|
|
if !lazyCatHostNet() {
|
|
return nil
|
|
}
|
|
lzc := lzcHostIPs()
|
|
var ips []net.IP
|
|
for _, name := range defaultIfaces(routeTable()) {
|
|
for _, ip := range ifaceIPs(name) {
|
|
if !slices.ContainsFunc(lzc, ip.Equal) {
|
|
ips = append(ips, ip)
|
|
}
|
|
}
|
|
}
|
|
return lanHostsOf(ips)
|
|
}
|
|
|
|
// lazyCatHostNet: the network magpie is on is the box's: the box runs its
|
|
// apps in containers, so its own network has their bridges and veth pairs
|
|
// (hostNetwork), and host.lzcapp, which LazyCat gives apps for the box, is
|
|
// one of this network's own addresses (on a bridge network it is the
|
|
// bridge's, another machine's). A lookup that fails is asked again a
|
|
// minute on, as LazyCat's DNS may not answer while the app starts.
|
|
func lazyCatHostNet() bool {
|
|
if hostNetwork(ifaceNames()) {
|
|
return true
|
|
}
|
|
own := hostIPs()
|
|
return slices.ContainsFunc(lzcHostIPs(), func(ip net.IP) bool { return slices.ContainsFunc(own, ip.Equal) })
|
|
}
|
|
|
|
// lzcHostIPs are host.lzcapp's addresses; a variable for the tests.
|
|
var lzcHostIPs = func() []net.IP { return lzcLookup.get() }
|
|
|
|
// lzcLookup holds host.lzcapp's addresses: a found answer for the
|
|
// process's life (a box's network doesn't change under a running app), a
|
|
// failure for a minute.
|
|
var lzcLookup = &cachedLookup{name: "host.lzcapp", retry: time.Minute}
|
|
|
|
type cachedLookup struct {
|
|
name string
|
|
retry time.Duration
|
|
mu sync.Mutex
|
|
ips []net.IP
|
|
found bool
|
|
at time.Time
|
|
}
|
|
|
|
func (c *cachedLookup) get() []net.IP {
|
|
if lazyCatURL() == "" {
|
|
return nil
|
|
}
|
|
c.mu.Lock()
|
|
if c.found || !c.at.IsZero() && time.Since(c.at) < c.retry {
|
|
defer c.mu.Unlock()
|
|
return c.ips
|
|
}
|
|
c.mu.Unlock()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, c.name)
|
|
var ips []net.IP
|
|
for _, a := range addrs {
|
|
ips = append(ips, a.IP)
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.ips, c.found, c.at = ips, err == nil && len(ips) > 0, time.Now()
|
|
return c.ips
|
|
}
|
|
|
|
// defaultIfaces are the interfaces of /proc/net/route's default routes
|
|
// (defaultGateway's), in the table's order.
|
|
func defaultIfaces(table string) []string {
|
|
var out []string
|
|
for _, line := range strings.Split(table, "\n") {
|
|
f := strings.Fields(line)
|
|
if len(f) < 4 || f[1] != "00000000" || f[0] == "Iface" || slices.Contains(out, f[0]) {
|
|
continue
|
|
}
|
|
out = append(out, f[0])
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ifaceIPs are the addresses on the interface named, when it is up; a
|
|
// variable for the tests.
|
|
var ifaceIPs = func(name string) []net.IP {
|
|
i, err := net.InterfaceByName(name)
|
|
if err != nil || i.Flags&net.FlagUp == 0 {
|
|
return nil
|
|
}
|
|
addrs, _ := i.Addrs()
|
|
var out []net.IP
|
|
for _, a := range addrs {
|
|
if n, ok := a.(*net.IPNet); ok {
|
|
out = append(out, n.IP)
|
|
}
|
|
}
|
|
return out
|
|
}
|