Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(rexctl): guide podman and reverse proxy installs (#3632)

Hampus2dbdace2f25c
diff --git a/fluxer_docs/src/content/docs/operator/index.mdx b/fluxer_docs/src/content/docs/operator/index.mdx
index 28238c59d..245a9b329 100644
--- a/fluxer_docs/src/content/docs/operator/index.mdx
+++ b/fluxer_docs/src/content/docs/operator/index.mdx
@@ -31,6 +31,14 @@ On Windows, run this in PowerShell:
irm https://fluxer.dev/install.ps1 | iex
```
+On Linux or macOS, start the install with this command to put your own reverse proxy, such as Caddy or nginx, in front of Fluxer.
+
+```sh
+curl -fsSL https://fluxer.dev/install.sh | sh -s -- install --behind-proxy --bind 127.0.0.1:8080
+```
+
+Fluxer then listens on `127.0.0.1:8080` for your proxy. The plain install command also asks whether you want this when another program already uses port 80 or 443 on the machine.
+
The script downloads rexctl and starts it. Answer its questions, and when it's done, rexctl prints your instance's address.
rexctl asks where you want to create your admin account. "Here in the terminal" creates it during the install. "In my browser" prints a setup token at the end, and your instance asks for that token the first time you open it. If you lose the token, run `rexctl setup token` to show it again.
diff --git a/fluxer_rexctl/cmd/install.go b/fluxer_rexctl/cmd/install.go
index d8c7afe4b..63217051c 100644
--- a/fluxer_rexctl/cmd/install.go
+++ b/fluxer_rexctl/cmd/install.go
@@ -3,9 +3,11 @@
package cmd
import (
+ "bytes"
"context"
"errors"
"fmt"
+ "io"
"maps"
"net"
"net/netip"
@@ -60,18 +62,20 @@ type installFlags struct {
}
type installer struct {
- answers *setup.Answers
- storage *storageChoice
- app *App
- c *cobra.Command
- f *installFlags
- dir string
- host *host.Info
- engine *engine.Engine
- found *engine.Engine
- findErr error
- ran map[string]bool
- plan []string
+ answers *setup.Answers
+ storage *storageChoice
+ app *App
+ c *cobra.Command
+ f *installFlags
+ dir string
+ host *host.Info
+ engine *engine.Engine
+ found *engine.Engine
+ findErr error
+ ran map[string]bool
+ plan []string
+ offered bool
+ lowPortsSaid bool
}
func newInstallCmd(app *App) *cobra.Command {
@@ -165,11 +169,16 @@ func runInstall(ctx context.Context, app *App, c *cobra.Command, f *installFlags
if in.runChecks(ctx, checks.Preinstall, "engine", "podman-subids", "windows-linux-containers") {
return notReady()
}
+ } else {
+ in.ran["engine"], in.ran["podman-subids"], in.ran["windows-linux-containers"] = true, true, true
+ }
+ if err := in.offerProxy(); err != nil {
+ return err
+ }
+ if in.engine != nil {
if err := in.preparePodman(ctx); err != nil {
return err
}
- } else {
- in.ran["engine"], in.ran["podman-subids"], in.ran["windows-linux-containers"] = true, true, true
}
if err := app.UI.Ask(c, ui.Field{
Flag: "domain",
@@ -215,7 +224,11 @@ func runInstall(ctx context.Context, app *App, c *cobra.Command, f *installFlags
if err := in.write(ctx, inst, *deleteOld); err != nil {
return err
}
- return in.start(ctx, inst)
+ if err := in.start(ctx, inst); err != nil {
+ return err
+ }
+ in.proxyNote()
+ return nil
})
}
@@ -278,10 +291,8 @@ func validateInstallFlags(f *installFlags) error {
return ui.Usage(fmt.Sprintf("%s isn't an IP address.", f.VoiceIP))
}
}
- if f.BehindProxy {
- if _, _, err := net.SplitHostPort(f.Bind); err != nil {
- return ui.Usage("Pass --bind as an address and port, like 127.0.0.1:8080.")
- }
+ if f.BehindProxy && validateBind(f.Bind) != nil {
+ return ui.Usage("Pass --bind as an address and port, like 127.0.0.1:8080.")
}
for _, p := range []int{f.VoiceTCPPort, f.VoiceUDPPort} {
if p < 0 || p > 65535 {
@@ -305,6 +316,13 @@ func validateInstallFlags(f *installFlags) error {
return nil
}
+func validateBind(v string) error {
+ if _, _, err := net.SplitHostPort(v); err != nil {
+ return setupInputError("Enter an address and port, like 127.0.0.1:8080.")
+ }
+ return nil
+}
+
var projectPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]*$`)
var labelPattern = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$`)
@@ -434,8 +452,8 @@ func (in *installer) ensureEngine(ctx context.Context) error {
}
func dockerInstalled() bool {
- if _, err := exec.LookPath("docker"); err == nil {
- return true
+ if path, err := exec.LookPath("docker"); err == nil {
+ return !podmanDockerWrapper(path)
}
switch runtime.GOOS {
case "windows":
@@ -448,6 +466,23 @@ func dockerInstalled() bool {
return false
}
+func podmanDockerWrapper(path string) bool {
+ if runtime.GOOS != "linux" {
+ return false
+ }
+ if target, err := filepath.EvalSymlinks(path); err == nil && filepath.Base(target) == "podman" {
+ return true
+ }
+ f, err := os.Open(path)
+ if err != nil {
+ return false
+ }
+ defer f.Close()
+ head := make([]byte, 512)
+ n, _ := io.ReadFull(f, head)
+ return bytes.HasPrefix(head[:n], []byte("#!")) && bytes.Contains(head[:n], []byte("podman"))
+}
+
func (in *installer) startPodman(ctx context.Context) error {
rootless := os.Geteuid() != 0
if runtime.GOOS == "linux" && rootless && !host.HasSubIDs(ctx) {
@@ -535,21 +570,125 @@ func (in *installer) allowLowPorts(ctx context.Context) error {
if in.f.BehindProxy || !host.LowPortsBlocked(in.engine.Rootless) {
return nil
}
- ok, err := in.consent("setup-podman", in.f.SetupPodman, "Let Podman use ports 80 and 443 under this account?")
- if err != nil || !ok {
+ if !in.lowPortsSaid {
+ in.app.UI.Info("Rootless Podman needs this to serve Fluxer on ports 80 and 443.")
+ }
+ ok, err := in.consent("setup-podman", in.f.SetupPodman, "Let programs that don't run as root use ports 80 and up on this machine?")
+ if err != nil {
return err
}
+ if !ok {
+ return ui.Refused("Fluxer can't serve HTTPS from rootless Podman without ports 80 and 443.", "Run rexctl install --behind-proxy to use your own reverse proxy instead.")
+ }
if in.f.DryRun {
- in.plan = append(in.plan, "Let Podman use ports 80 and 443.")
+ in.plan = append(in.plan, "Let programs that don't run as root use ports 80 and up.")
return nil
}
if err := host.AllowLowPorts(ctx); err != nil {
- return ui.Wrap(err, "Couldn't let Podman use ports 80 and 443.", "Run rexctl install --verbose to see why.")
+ return ui.Wrap(err, "Couldn't open ports 80 and up to programs that don't run as root.", "Run rexctl install --verbose to see why.")
}
- in.app.UI.OK("Let Podman use ports 80 and 443.")
+ in.app.UI.OK("Programs that don't run as root can use ports 80 and up.")
return nil
}
+type proxyHint struct {
+ Interactive bool
+ Decided bool
+ Busy []int
+ Owner string
+ LowPorts string
+}
+
+func (h proxyHint) offer() (reason string, def bool, ok bool) {
+ if !h.Interactive || h.Decided {
+ return "", false, false
+ }
+ if len(h.Busy) > 0 {
+ nums := make([]string, len(h.Busy))
+ for i, n := range h.Busy {
+ nums[i] = strconv.Itoa(n)
+ }
+ subject := fmt.Sprintf("Port %s on this machine is", nums[0])
+ if len(nums) > 1 {
+ subject = fmt.Sprintf("Ports %s on this machine are", ui.JoinAnd(nums))
+ }
+ switch {
+ case checks.ContainerOwner(h.Owner):
+ return subject + " already in use by a container.", true, true
+ case h.Owner != "":
+ return fmt.Sprintf("%s already in use by %s.", subject, h.Owner), true, true
+ }
+ return subject + " already in use.", true, true
+ }
+ if h.LowPorts != "" {
+ return fmt.Sprintf("Rootless %s can't use ports 80 and 443 without a change to this machine.", h.LowPorts), false, true
+ }
+ return "", false, false
+}
+
+func (in *installer) proxyHint() proxyHint {
+ h := proxyHint{
+ Interactive: in.app.UI.Interactive(),
+ Decided: in.f.BehindProxy || in.c.Flags().Changed("behind-proxy") || in.c.Flags().Changed("bind"),
+ }
+ if !h.Interactive || h.Decided {
+ return h
+ }
+ sameOwner := true
+ for _, port := range []int{80, 443} {
+ used, owner, err := host.PortInUse("tcp", port)
+ if err != nil || !used {
+ continue
+ }
+ if len(h.Busy) == 0 {
+ h.Owner = owner
+ } else if owner != h.Owner {
+ sameOwner = false
+ }
+ h.Busy = append(h.Busy, port)
+ }
+ if !sameOwner {
+ h.Owner = ""
+ }
+ if in.engine != nil && host.LowPortsBlocked(in.engine.Rootless) {
+ h.LowPorts = in.engine.Kind.Name()
+ }
+ return h
+}
+
+func (in *installer) offerProxy() error {
+ h := in.proxyHint()
+ reason, def, ok := h.offer()
+ if !ok {
+ return nil
+ }
+ in.offered, in.lowPortsSaid = true, len(h.Busy) == 0
+ in.app.UI.Info("%s", reason)
+ yes, err := in.app.UI.Confirm("Use your own reverse proxy in front of Fluxer?", def)
+ if err != nil || !yes {
+ return err
+ }
+ if err := in.app.UI.Ask(in.c, ui.Field{
+ Flag: "bind",
+ Title: "Which address should your proxy connect to?",
+ Value: &in.f.Bind,
+ Validate: validateBind,
+ }); err != nil {
+ return err
+ }
+ in.f.BehindProxy = true
+ in.plan = append(in.plan, fmt.Sprintf("Serve Fluxer over plain HTTP on %s for your reverse proxy.", in.f.Bind))
+ return nil
+}
+
+func (in *installer) proxyNote() {
+ if !in.offered || !in.f.BehindProxy {
+ return
+ }
+ in.app.UI.Info("Set up your proxy to forward https://%s to http://%s.", normaliseDomain(in.f.Domain), in.f.Bind)
+ in.app.UI.Info("See https://docs.fluxer.app/operator/reverse-proxy/ for what your proxy needs to do.")
+}
+
func (in *installer) installDocker(ctx context.Context) error {
if in.f.Engine == string(engine.Podman) {
return ui.Refused("Podman isn't installed.", "Install Podman, then run rexctl install again.")
diff --git a/fluxer_rexctl/cmd/install_podman_test.go b/fluxer_rexctl/cmd/install_podman_test.go
new file mode 100644
index 000000000..6bbe413e4
--- /dev/null
+++ b/fluxer_rexctl/cmd/install_podman_test.go
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+)
+
+func TestPodmanDockerWrapper(t *testing.T) {
+ if runtime.GOOS != "linux" {
+ t.Skip("the podman-docker wrapper only exists on Linux")
+ }
+ dir := t.TempDir()
+ write := func(name, body string) string {
+ p := filepath.Join(dir, name)
+ if err := os.WriteFile(p, []byte(body), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ return p
+ }
+ podman := write("podman", "\x7fELF")
+ link := filepath.Join(dir, "docker-link")
+ if err := os.Symlink(podman, link); err != nil {
+ t.Fatal(err)
+ }
+ cases := map[string]bool{
+ write("docker-wrapper", "#!/bin/sh\n[ -e /etc/containers/nodocker ] || echo emulate\nexec /usr/bin/podman \"$@\"\n"): true,
+ link: true,
+ write("docker", "\x7fELF real docker"): false,
+ write("docker-other-script", "#!/bin/sh\nexec /usr/libexec/docker/cli \"$@\"\n"): false,
+ }
+ for path, want := range cases {
+ if got := podmanDockerWrapper(path); got != want {
+ t.Errorf("podmanDockerWrapper(%s) = %v, want %v", filepath.Base(path), got, want)
+ }
+ }
+}
diff --git a/fluxer_rexctl/cmd/install_proxy_test.go b/fluxer_rexctl/cmd/install_proxy_test.go
new file mode 100644
index 000000000..8eb6494a0
--- /dev/null
+++ b/fluxer_rexctl/cmd/install_proxy_test.go
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package cmd
+
+import (
+ "bytes"
+ "strings"
+ "testing"
+
+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"
+)
+
+func TestProxyHintOffer(t *testing.T) {
+ cases := []struct {
+ name string
+ hint proxyHint
+ ok bool
+ def bool
+ reason string
+ }{
+ {"nothing in the way", proxyHint{Interactive: true}, false, false, ""},
+ {"not interactive", proxyHint{Busy: []int{443}, LowPorts: "Podman"}, false, false, ""},
+ {"flag already decides", proxyHint{Interactive: true, Decided: true, Busy: []int{80, 443}}, false, false, ""},
+ {"one port busy", proxyHint{Interactive: true, Busy: []int{443}}, true, true, "Port 443 on this machine is already in use."},
+ {"both ports busy by a program", proxyHint{Interactive: true, Busy: []int{80, 443}, Owner: "caddy"}, true, true, "Ports 80 and 443 on this machine are already in use by caddy."},
+ {"busy by a container", proxyHint{Interactive: true, Busy: []int{80}, Owner: "docker-proxy"}, true, true, "Port 80 on this machine is already in use by a container."},
+ {"busy wins over low ports", proxyHint{Interactive: true, Busy: []int{443}, LowPorts: "Podman"}, true, true, "Port 443 on this machine is already in use."},
+ {"rootless podman", proxyHint{Interactive: true, LowPorts: "Podman"}, true, false, "Rootless Podman can't use ports 80 and 443 without a change to this machine."},
+ }
+ for _, c := range cases {
+ reason, def, ok := c.hint.offer()
+ if ok != c.ok || def != c.def || reason != c.reason {
+ t.Errorf("%s: got (%q, %v, %v), want (%q, %v, %v)", c.name, reason, def, ok, c.reason, c.def, c.ok)
+ }
+ }
+}
+
+func TestValidateBind(t *testing.T) {
+ for v, ok := range map[string]bool{
+ "127.0.0.1:8080": true,
+ "[::1]:8080": true,
+ ":8080": true,
+ "127.0.0.1": false,
+ "": false,
+ } {
+ if err := validateBind(v); (err == nil) != ok {
+ t.Errorf("%q: got %v", v, err)
+ }
+ }
+}
+
+func TestOfferProxyNonInteractive(t *testing.T) {
+ for _, yes := range []bool{false, true} {
+ var out bytes.Buffer
+ app := &App{UI: ui.New(ui.Options{Stdin: strings.NewReader("y\n"), Stdout: &out, Stderr: &out, Yes: yes})}
+ c := newInstallCmd(app)
+ if err := c.ParseFlags(nil); err != nil {
+ t.Fatal(err)
+ }
+ f := &installFlags{Bind: "127.0.0.1:8080"}
+ in := &installer{app: app, c: c, f: f, ran: map[string]bool{}}
+ if err := in.offerProxy(); err != nil {
+ t.Fatalf("yes=%v: %v", yes, err)
+ }
+ if in.offered || f.BehindProxy || len(in.plan) > 0 || out.Len() > 0 {
+ t.Errorf("yes=%v: offered=%v behind=%v plan=%q output=%q", yes, in.offered, f.BehindProxy, in.plan, out.String())
+ }
+ }
+}
diff --git a/fluxer_rexctl/cmd/lifecycle.go b/fluxer_rexctl/cmd/lifecycle.go
index 10cee12a4..f6f5db6cb 100644
--- a/fluxer_rexctl/cmd/lifecycle.go
+++ b/fluxer_rexctl/cmd/lifecycle.go
@@ -7,6 +7,8 @@ import (
"errors"
"fmt"
"net"
+ "os"
+ "os/exec"
"path"
"runtime"
"slices"
@@ -102,6 +104,9 @@ func engineFailureFor(kind engine.Kind, err error) error {
if desktop {
return refused("Docker Desktop isn't running.", "Open Docker Desktop and wait until its engine is running, then try again.")
}
+ if fix := podmanSocketFix(kind); fix != "" {
+ return refused("Podman is installed, but its API socket isn't running.", fix)
+ }
if kind == "" {
return refused("Neither Docker nor Podman answers on this machine.", "Start Docker or Podman, then try again.")
}
@@ -760,3 +765,16 @@ func newRestartCmd(app *App) *cobra.Command {
},
}
}
+
+func podmanSocketFix(kind engine.Kind) string {
+ if runtime.GOOS != "linux" || (kind != "" && kind != engine.Podman) {
+ return ""
+ }
+ if _, err := exec.LookPath("podman"); err != nil {
+ return ""
+ }
+ if os.Geteuid() == 0 {
+ return "Run systemctl enable --now podman.socket, then try again."
+ }
+ return "Run systemctl --user enable --now podman.socket, then try again."
+}
diff --git a/fluxer_rexctl/cmd/proxy.go b/fluxer_rexctl/cmd/proxy.go
index 49c661d67..ae6865400 100644
--- a/fluxer_rexctl/cmd/proxy.go
+++ b/fluxer_rexctl/cmd/proxy.go
@@ -101,8 +101,8 @@ func allowLowPorts(ctx context.Context, app *App, inst *instance.Instance) error
return nil
}
name := e.Kind.Name()
- app.UI.Warn(fmt.Sprintf("%s can't use ports 80 and 443 under this account.", name), "")
- ok, err := app.UI.Confirm(fmt.Sprintf("Let %s use them?", name), true)
+ app.UI.Warn(fmt.Sprintf("Rootless %s can't use ports 80 and 443, because only root can use ports below 1024 on this machine.", name), "")
+ ok, err := app.UI.Confirm("Let programs that don't run as root use ports 80 and up on this machine?", true)
if err != nil {
return err
}
diff --git a/fluxer_rexctl/internal/checks/low_ports.go b/fluxer_rexctl/internal/checks/low_ports.go
index 22f540587..606cce1d6 100644
--- a/fluxer_rexctl/internal/checks/low_ports.go
+++ b/fluxer_rexctl/internal/checks/low_ports.go
@@ -22,11 +22,11 @@ func runLowPorts(_ context.Context, e *Env) Result {
if e.installed() {
proxy = "run rexctl proxy on"
}
- r := fixable(fail(fmt.Sprintf("%s can't use ports 80 and 443 under this account.", e.engineName()), "Run rexctl doctor --fix to allow them, or "+proxy+" to use your own proxy."))
+ r := fixable(fail(fmt.Sprintf("Rootless %s can't use ports 80 and 443, because only root can use ports below 1024 on this machine.", e.engineName()), "Run rexctl doctor --fix to allow them, or "+proxy+" to use your own proxy."))
r.Manual = "Ask an administrator to add " + host.LowPortsSetting + " to " + host.LowPortsFile + " and run sudo sysctl --system, or " + proxy + " to use your own proxy."
return r
}
- return pass(fmt.Sprintf("%s can use ports 80 and 443 under this account.", e.engineName()))
+ return pass(fmt.Sprintf("Rootless %s can use ports 80 and 443.", e.engineName()))
}
func fixLowPorts(ctx context.Context, _ *Env) error {
diff --git a/fluxer_rexctl/internal/checks/ports.go b/fluxer_rexctl/internal/checks/ports.go
index d19ced908..3b72b9228 100644
--- a/fluxer_rexctl/internal/checks/ports.go
+++ b/fluxer_rexctl/internal/checks/ports.go
@@ -31,6 +31,10 @@ var containerOwners = map[string]bool{
"wslrelay": true,
}
+func ContainerOwner(name string) bool {
+ return containerOwners[name]
+}
+
func runPorts(ctx context.Context, e *Env) Result {
wanted := e.wantedPorts()
var numbers, busy []string