diff --git a/internal/gateway/lan.go b/internal/gateway/lan.go index 7b7413de6..28b28828c 100644 --- a/internal/gateway/lan.go +++ b/internal/gateway/lan.go @@ -78,9 +78,57 @@ func logInvalidPublicURL() { var warnInvalidPublicURL = sync.OnceFunc(logInvalidPublicURL) -// publicURL is the validated MAGPIE_PUBLIC_URL, with a scheme and without -// trailing slashes. An invalid value is treated as unset and warned once. +// publicURL is where other machines reach the gateway when the system +// says so: the validated MAGPIE_PUBLIC_URL, else a LazyCat app's own +// address (lazyCatURL). func publicURL() string { + if u := namedPublicURL(); u != "" { + return u + } + return lazyCatURL() +} + +// lazyCatURL is where a LazyCat (懒猫微服, lazycat.cloud) app is reached: +// LazyCat injects LAZYCAT_APP_DOMAIN (magpie..heiyu.space) into every +// container of an app, and serves the app there over HTTPS. Inside, magpie +// sees only its container's address (172.28.x.x), which nothing outside +// the box reaches, and Settings offered http://172.28.3.65:3425 under +// "Share on local network" (mtoolstec on X). The magpie-lzcapp package +// sets MAGPIE_PUBLIC_URL to the same domain through a manifest template, +// which a system before lzcos 1.3.8 doesn't render, and another package +// or a hand-written manifest may not set it at all. "" outside LazyCat, +// or when the value is no domain. +func lazyCatURL() string { + d := strings.ToLower(strings.TrimSpace(os.Getenv("LAZYCAT_APP_DOMAIN"))) + if !domainName(d) { + return "" + } + return "https://" + d +} + +// domainName: d is a DNS name of two labels or more, each of letters, +// digits and hyphens. +func domainName(d string) bool { + if len(d) > 253 || !strings.Contains(d, ".") { + return false + } + for _, l := range strings.Split(d, ".") { + if l == "" || len(l) > 63 || l[0] == '-' || l[len(l)-1] == '-' { + return false + } + for _, c := range l { + if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-') { + return false + } + } + } + return true +} + +// namedPublicURL is the validated MAGPIE_PUBLIC_URL, with a scheme and +// without trailing slashes. An invalid value is treated as unset and +// warned once. +func namedPublicURL() string { u := strings.TrimSpace(os.Getenv("MAGPIE_PUBLIC_URL")) if u == "" { return "" @@ -102,7 +150,8 @@ func publicURL() string { return u } -// PublicURL is MAGPIE_PUBLIC_URL, with a scheme; "" when it isn't set. +// PublicURL is MAGPIE_PUBLIC_URL, with a scheme, else a LazyCat app's +// address; "" when neither is set. func PublicURL() string { return publicURL() } // OnNetwork: MAGPIE_ADDR puts the gateway beyond loopback (0.0.0.0 in the @@ -120,7 +169,7 @@ func OnNetwork() bool { // gateway key — while the gateway is shared, or listens on the network. func remoteKeyed() bool { return settings.Load().LAN || OnNetwork() } -// PublicHost is MAGPIE_PUBLIC_URL's host, "" when it isn't set. +// PublicHost is PublicURL's host, "" when it isn't set. func PublicHost() string { u, err := url.Parse(publicURL()) if err != nil { @@ -222,11 +271,23 @@ func defaultGateway(table string) string { } // InContainer: this magpie runs in a container (Docker, Podman, a -// Kubernetes pod), whose image is what gets updated, not its binary. -func InContainer() bool { return inContainer("/") } +// Kubernetes pod, a LazyCat app), whose image is what gets updated, not +// its binary. +func InContainer() bool { return lazyCatURL() != "" || inContainer("/") } // inContainer: the system under root is a container's — Docker's or -// Podman's marker file, or a container runtime in PID 1's cgroup. +// Podman's marker file, a container runtime in PID 1's cgroup, a +// container's init (or magpie itself, as the image starts it) as PID 1, +// or the root of a cgroup namespace on an overlay root filesystem. +// +// Under cgroup v2 with a private cgroup namespace, Docker's default +// since 20.10, PID 1's cgroup reads "0::/" and names no runtime, and a +// runtime may write no marker file (a NAS's own, LazyCat's). A host's +// PID 1 is an init system (systemd, in /init.scope), and a host's root +// filesystem is rarely an overlay, so neither alone is taken for a +// container: a read-only overlay root (Raspberry Pi OS, overlayroot) +// sits under systemd in /init.scope, and an init without cgroups puts +// "0::/" on an ext4 or btrfs root. func inContainer(root string) bool { for _, f := range []string{".dockerenv", "run/.containerenv"} { if _, err := os.Stat(filepath.Join(root, f)); err == nil { @@ -239,6 +300,34 @@ func inContainer(root string) bool { return true } } + comm, _ := os.ReadFile(filepath.Join(root, "proc/1/comm")) + switch strings.TrimSpace(string(comm)) { + case "magpie", "tini", "docker-init", "dumb-init", "catatonit": + return true + } + if strings.TrimSpace(string(b)) == "0::/" { + m, _ := os.ReadFile(filepath.Join(root, "proc/1/mountinfo")) + return overlayRoot(string(m)) + } + return false +} + +// overlayRoot: in mountinfo (proc(5)), the filesystem mounted last at "/" +// is an overlay, as a container's image layers are. +func overlayRoot(mountinfo string) bool { + fs := "" + for _, line := range strings.Split(mountinfo, "\n") { + f := strings.Fields(line) + sep := slices.Index(f, "-") + if sep < 5 || sep+1 >= len(f) || f[4] != "/" { + continue + } + fs = f[sep+1] + } + switch fs { + case "overlay", "fuse-overlayfs", "aufs": + return true + } return false } diff --git a/internal/gateway/lan_test.go b/internal/gateway/lan_test.go index 9f952af6b..bc5d7f9e2 100644 --- a/internal/gateway/lan_test.go +++ b/internal/gateway/lan_test.go @@ -172,31 +172,95 @@ func TestPublicURLShared(t *testing.T) { } } -// A container is known by Docker's or Podman's marker file, or a runtime -// in PID 1's cgroup; a plain system has none of them. +// A container is known by Docker's or Podman's marker file, a runtime in +// PID 1's cgroup, a container's init or magpie as PID 1, or a cgroup v2 +// namespace's root on an overlay root filesystem (Docker's default under +// cgroup v2 names no runtime, and a NAS's runtime may write no marker: +// mtoolstec on X, LazyCat). A plain system has none of them, a systemd +// host on an overlay root included. func TestInContainer(t *testing.T) { - mk := func(file, body string) string { + mk := func(files map[string]string) string { root := t.TempDir() - if file != "" { + for file, body := range files { p := filepath.Join(root, file) os.MkdirAll(filepath.Dir(p), 0o755) os.WriteFile(p, []byte(body), 0o644) } return root } + const ( + overlay = "22 1 0:21 / / rw,relatime - ext4 /dev/sda1 rw\n" + + "512 480 0:52 / / rw,relatime master:1 - overlay overlay rw,lowerdir=/l,upperdir=/u,workdir=/w\n" + + "513 512 0:55 / /proc rw,nosuid - proc proc rw\n" + ext4 = "22 1 8:1 / / rw,relatime shared:1 - ext4 /dev/sda1 rw\n23 22 0:5 / /proc rw - proc proc rw\n" + btrfs = "30 1 0:27 /@ / rw,relatime shared:1 - btrfs /dev/nvme0n1p2 rw\n" + ) for _, c := range []struct { - file, body string - want bool + name string + files map[string]string + want bool }{ - {"", "", false}, - {"proc/1/cgroup", "0::/init.scope\n", false}, - {".dockerenv", "", true}, - {"run/.containerenv", "", true}, - {"proc/1/cgroup", "12:pids:/docker/4f1c…\n", true}, - {"proc/1/cgroup", "0::/kubepods/besteffort/pod1\n", true}, + {"nothing", nil, false}, + {"systemd host", map[string]string{"proc/1/cgroup": "0::/init.scope\n", "proc/1/comm": "systemd\n", "proc/1/mountinfo": ext4}, false}, + {"systemd host on overlayroot", map[string]string{"proc/1/cgroup": "0::/init.scope\n", "proc/1/comm": "systemd\n", "proc/1/mountinfo": overlay}, false}, + {"cgroup-less init host", map[string]string{"proc/1/cgroup": "0::/\n", "proc/1/comm": "init\n", "proc/1/mountinfo": btrfs}, false}, + {"WSL", map[string]string{"proc/1/cgroup": "0::/\n", "proc/1/comm": "init\n", "proc/1/mountinfo": ext4}, false}, + {".dockerenv", map[string]string{".dockerenv": ""}, true}, + {".containerenv", map[string]string{"run/.containerenv": ""}, true}, + {"cgroup v1 docker", map[string]string{"proc/1/cgroup": "12:pids:/docker/4f1c…\n"}, true}, + {"kubepods", map[string]string{"proc/1/cgroup": "0::/kubepods/besteffort/pod1\n"}, true}, + {"cgroup v2 namespace on overlay", map[string]string{"proc/1/cgroup": "0::/\n", "proc/1/comm": "sh\n", "proc/1/mountinfo": overlay}, true}, + {"magpie as PID 1", map[string]string{"proc/1/cgroup": "0::/\n", "proc/1/comm": "magpie\n", "proc/1/mountinfo": btrfs}, true}, + {"docker --init", map[string]string{"proc/1/comm": "docker-init\n"}, true}, + {"tini", map[string]string{"proc/1/comm": "tini\n"}, true}, } { - if got := inContainer(mk(c.file, c.body)); got != c.want { - t.Errorf("%s %q: %v", c.file, c.body, got) + if got := inContainer(mk(c.files)); got != c.want { + t.Errorf("%s: %v, want %v", c.name, got, c.want) + } + } +} + +// In a LazyCat (懒猫微服) app magpie sees only its container's address, +// and Settings offered http://172.28.3.65:3425 under "Share on local +// network" (mtoolstec on X). LazyCat names the app's domain in +// LAZYCAT_APP_DOMAIN, and the app is reached there over HTTPS: it is the +// address shared, unless MAGPIE_PUBLIC_URL names another. A package's +// MAGPIE_PUBLIC_URL template a system didn't render is no address, and +// LazyCat's is used instead. +func TestLazyCatPublicURL(t *testing.T) { + t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425") + t.Setenv("MAGPIE_PUBLIC_URL", "") + t.Setenv("LAZYCAT_APP_DOMAIN", "") + if got := PublicURL(); got != "" { + t.Fatalf("outside LazyCat: %q", got) + } + for _, c := range []struct{ public, domain, want string }{ + {"", "magpie.snyht3.heiyu.space", "https://magpie.snyht3.heiyu.space"}, + {"", " Magpie.Snyht3.heiyu.space\n", "https://magpie.snyht3.heiyu.space"}, + {"https://{{.S.AppDomain}}", "magpie.snyht3.heiyu.space", "https://magpie.snyht3.heiyu.space"}, + {"https://ai.example.com", "magpie.snyht3.heiyu.space", "https://ai.example.com"}, + {"", "https://magpie.snyht3.heiyu.space", ""}, + {"", "magpie", ""}, + {"", "magpie.box.heiyu.space/x", ""}, + {"", "-a.heiyu.space", ""}, + } { + t.Setenv("MAGPIE_PUBLIC_URL", c.public) + t.Setenv("LAZYCAT_APP_DOMAIN", c.domain) + if got := PublicURL(); got != c.want { + t.Errorf("MAGPIE_PUBLIC_URL=%q LAZYCAT_APP_DOMAIN=%q: %q, want %q", c.public, c.domain, got, c.want) + continue + } + if c.want == "" { + continue + } + if got := LANURLs(); !slices.Equal(got, []string{c.want}) { + t.Errorf("LAZYCAT_APP_DOMAIN=%q: shared at %v, want %s", c.domain, got, c.want) + } + if ContainerAddrs() { + t.Errorf("LAZYCAT_APP_DOMAIN=%q: the addresses are said to be a container's", c.domain) + } + if !InContainer() { + t.Errorf("LAZYCAT_APP_DOMAIN=%q: not in a container", c.domain) } } }