gateway: a LazyCat app is shared at its own domain, and a cgroup v2 container is known
mtoolstec on X runs magpie in LazyCat (懒猫微服) and, with "Share on local network" on, Settings offered http://172.28.3.65:3425: the container's own address, which nothing outside the box reaches, and no hint that it was a container's. LazyCat injects LAZYCAT_APP_DOMAIN (magpie.<box>.heiyu.space) into every container of an app and serves the app there over HTTPS. The magpie-lzcapp package sets MAGPIE_PUBLIC_URL=https://{{.S.AppDomain}} through a manifest template, which only lzcos 1.3.8+ renders; unrendered, url.Parse refuses the "{" host and publicURL() was "" (the log says "ignoring invalid MAGPIE_PUBLIC_URL"). A hand-written manifest may not set it at all. Then ContainerAddrs() needed inContainer(), which knew only Docker's and Podman's marker files and a runtime's name in PID 1's cgroup. Under cgroup v2 with a private cgroup namespace (Docker's default since 20.10) /proc/1/cgroup is "0::/", and a runtime that writes no marker file got past it, so the container's addresses were listed as the computer's. - publicURL() falls back to https://$LAZYCAT_APP_DOMAIN when MAGPIE_PUBLIC_URL is unset or invalid (a DNS name only; lower-cased). Everything built on PublicURL follows: LANURLs, PublicHost (the rebinding Host check), magpie web's links and trusted origin, the image MCP base, the CLI's advertised URL. InContainer() is true there too (updates go through the image). - inContainer() also takes a container's init as PID 1 (magpie itself, as our image starts it; tini, docker-init, dumb-init, catatonit), and a cgroup namespace's root ("0::/") on an overlay root filesystem (/proc/1/mountinfo). Neither alone catches a host: a systemd host is in /init.scope even on a read-only overlay root, and an init without cgroups on ext4/btrfs is not overlay. hostNetwork is unchanged, so a container on the host's network still lists the host's addresses. What the user sees: in LazyCat, "Share on local network" shows https://magpie.<box>.heiyu.space (and the Gateway URL is that domain). In any other cgroup v2 container on a bridge network without a marker file, Settings shows the container hint to set MAGPIE_PUBLIC_URL. Run: - TestInContainer (13 fake roots: systemd host, systemd on overlayroot, cgroup-less init on btrfs, WSL, markers, cgroup v1, kubepods, cgroup v2 on overlay, magpie/docker-init/tini as PID 1) and TestLazyCatPublicURL (LazyCat domain, mixed case, the unrendered template, MAGPIE_PUBLIC_URL winning, bad domains) pass on Linux (wsl-run, magpie-desktop). - Broken by hand: with origin/main's lan.go both tests are red (4 and 4 cases). - Real systems: the WSL2 distro (systemd host, 0::/init.scope) reads inContainer=false. Docker 28.2.2 on xiaomi-gpu, debian:12-slim with /.dockerenv removed (cgroup "0::/", overlay root, PID 1 sh) reads inContainer=true hostNetwork=false; with --init (docker-init) true; with --network host inContainer=true hostNetwork=true, so it still isn't called a container's addresses. - go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., and the darwin/linux nogui/windows builds pass on mote at the first base (ddd6c649b); after the rebase onto 7 newer main commits, go vet ./internal/..., linux and windows vet of gateway/gui/cli and the build pass on mote. - Linux (wsl-run): internal/gateway ok (1230s) at the rebased head; internal/cli (1707s) and internal/gui (624s), -tags nogui, ok at the first base. - Not run: the full ./... suite. It was queued on mote's suite.lock behind ~15 other sessions' runs for 46 minutes and I stopped it; CI runs it on this push. No GUI file changed. Not tried: a real LazyCat box (none here); the env names and the https domain are from developer.lazycat.cloud/advanced-envs and the lazycat-contrib/magpie-lzcapp manifest. - After a last rebase ontofdd38154b(6 commits in usage, library, providers, settings and docs; none in internal/gateway), nothing was run again.
This commit is contained in:
+96
-7
@@ -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.<box>.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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user