A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(rexctl): add edge.listen_address setting (#3849)
Hampus99dab64aa310
diff --git a/fluxer_docs/src/content/docs/operator/environment.mdx b/fluxer_docs/src/content/docs/operator/environment.mdxindex 62086c519..b71bb2205 100644--- a/fluxer_docs/src/content/docs/operator/environment.mdx+++ b/fluxer_docs/src/content/docs/operator/environment.mdx@@ -220,6 +220,10 @@ The port Fluxer listens on for HTTPS. If people connect to this port directly, sThe address your reverse proxy or tunnel connects to. The default is `127.0.0.1:8080` with `docker-compose.proxy.yml`, or `127.0.0.1:80` with `tunnel.compose.yml`. With rexctl, use `rexctl config set edge.bind`.+### `FLUXER_EDGE_LISTEN_ADDRESS`++The IP address on this machine that Fluxer listens on for HTTP and HTTPS, or empty for every address. Write an IPv6 address without brackets. `docker-compose.proxy.yml` and `tunnel.compose.yml` ignore it and use `FLUXER_EDGE_BIND`. With rexctl, use `rexctl config set edge.listen_address`.+### `FLUXER_ADMIN_BASE_PATH`The path the admin panel is served under, with a leading slash and no trailing slash, such as `/panel`. The edge serves the admin panel under this path, the admin service builds its links with it, and the default `FLUXER_ADMIN_ENDPOINT` follows it. The default is `/admin`. With rexctl, use `rexctl config set FLUXER_ADMIN_BASE_PATH`.diff --git a/fluxer_rexctl/cmd/app.go b/fluxer_rexctl/cmd/app.goindex f8b62dd77..9a6217100 100644--- a/fluxer_rexctl/cmd/app.go+++ b/fluxer_rexctl/cmd/app.go@@ -24,6 +24,7 @@ import ("github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui")@@ -235,6 +236,9 @@ func (a *App) InstanceRepairing(keys []string) (*instance.Instance, error) {return nil, ui.Refused(fmt.Sprintf("%s holds voice servers that joined %s, not a Fluxer instance.", dir, voiceInstances(inst)), "On a voice machine, use rexctl voice status, rexctl doctor or rexctl upgrade.")}a.warnUnknown(inst)+ if len(keys) == 0 {+ a.saveRepaired(inst)+ }a.rememberFirst(inst)return inst, nilcase errors.Is(err, instance.ErrNotFound):@@ -244,11 +248,36 @@ func (a *App) InstanceRepairing(keys []string) (*instance.Instance, error) {case errors.Is(err, instance.ErrNewerState):return nil, ui.Refused(fmt.Sprintf("A newer rexctl manages the instance in %s.", dir), "Download the latest rexctl, then try again.")case errors.As(err, &se):- return nil, &ui.ExitError{Code: ui.ExitRefused, Msg: fmt.Sprintf("The setting %s in %s isn't valid.", se.Key, dir), Fix: se.Fix(), Err: err}+ return nil, settingRefusal(dir, se)}return nil, ui.Wrap(err, fmt.Sprintf("rexctl can't read the instance in %s.", dir), "Run rexctl as the account that installed Fluxer.")}+func settingRefusal(dir string, se *instance.SettingError) error {+ msg := fmt.Sprintf("The setting %s in %s isn't valid.", se.Key, dir)+ if se.Remedy != "" {+ msg = se.Problem+ }+ return &ui.ExitError{Code: ui.ExitRefused, Msg: msg, Fix: se.Fix(), Err: se}+}++func (a *App) saveRepaired(inst *instance.Instance) {+ if len(inst.State.Repaired) == 0 {+ return+ }+ l, err := inst.Lock(context.Background())+ if err != nil {+ return+ }+ defer l.Release()+ if err := inst.Save(); err != nil {+ a.UI.Detail("%v", err)+ return+ }+ a.UI.Info("rexctl moved the address in %s to %s=%s.", ui.JoinAnd(inst.State.Repaired), settings.EdgeListen, inst.State.Settings[settings.EdgeListen])+ inst.State.Repaired = nil+}+func (a *App) warnUnknown(inst *instance.Instance) {if a.warned || inst.State == nil || len(inst.State.Unknown) == 0 {returndiff --git a/fluxer_rexctl/cmd/doctor.go b/fluxer_rexctl/cmd/doctor.goindex 5a7597d16..2c33b28d7 100644--- a/fluxer_rexctl/cmd/doctor.go+++ b/fluxer_rexctl/cmd/doctor.go@@ -45,9 +45,11 @@ func runDoctor(ctx context.Context, app *App, fix bool) error {env := &checks.Env{Dir: dir}phase := checks.Installedinst, err := instance.Find(dir)+ var se *instance.SettingErrorswitch {case err == nil:env.Instance = inst+ app.saveRepaired(inst)app.UI.Info("Using %s (%s).", inst.Dir, l.Why())if inst.State.IsVoice() {phase = checks.Voice@@ -56,6 +58,8 @@ func runDoctor(ctx context.Context, app *App, fix bool) error {phase = checks.Preinstallcase errors.Is(err, instance.ErrLegacy):return ui.Refused("rexctl doesn't manage the instance in "+dir+" yet.", "Run "+app.takeOverCommand()+" to hand it to rexctl.")+ case errors.As(err, &se):+ return settingRefusal(dir, se)default:return ui.Wrap(err, "rexctl can't read the instance in "+dir+".", "Run rexctl doctor as the account that installed Fluxer.")}diff --git a/fluxer_rexctl/cmd/takeover.go b/fluxer_rexctl/cmd/takeover.goindex caedea85c..a3830bdae 100644--- a/fluxer_rexctl/cmd/takeover.go+++ b/fluxer_rexctl/cmd/takeover.go@@ -17,6 +17,7 @@ import ("github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/release"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/setup""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"@@ -63,11 +64,15 @@ func takeoverDir(ctx context.Context, app *App, dir string, opts takeoverOptionsreturn nil, nil, err}var current *instance.Instance+ var se *instance.SettingErrorswitch found, err := instance.Find(dir); {case err == nil:current = found+ app.saveRepaired(current)case errors.Is(err, instance.ErrNotFound):return nil, nil, ui.Refused(fmt.Sprintf("There's no Fluxer instance in %s.", dir), "Run rexctl install, or pass --dir.")+ case errors.As(err, &se):+ return nil, nil, settingRefusal(dir, se)case !errors.Is(err, instance.ErrLegacy):return nil, nil, ui.Wrap(err, fmt.Sprintf("rexctl can't read the instance in %s.", dir), "Run rexctl as the account that installed Fluxer.")}@@ -167,6 +172,9 @@ func describeTakeover(u *ui.UI, r *instance.ImportReport) {} else if n > 1 {u.Info("%d settings your instance is missing will be added.", n)}+ if r.ListenAddress != "" {+ u.Info("Fluxer keeps listening only on %s, and rexctl keeps that address in %s.", r.ListenAddress, settings.EdgeListen)+ }if r.DedupedEnv {u.Info("Settings that appear twice in .env keep their last value.")}diff --git a/fluxer_rexctl/cmd/voice_ops.go b/fluxer_rexctl/cmd/voice_ops.goindex eb41eaf40..a09dd2842 100644--- a/fluxer_rexctl/cmd/voice_ops.go+++ b/fluxer_rexctl/cmd/voice_ops.go@@ -361,7 +361,7 @@ func voiceProbeTarget(inst *instance.Instance, srv *instance.VoiceServer) (strincase inst.State.IsVoice():return voice.HTTPBase(srv.Endpoint), "127.0.0.1:443"}- return voice.InstanceEdge(srv.Endpoint, inst.State.Settings[settings.EdgeMode], inst.Setting("FLUXER_HTTPS_PORT"), inst.Setting("FLUXER_EDGE_BIND"))+ return voice.InstanceEdge(srv.Endpoint, inst.State.Settings[settings.EdgeMode], inst.Setting("FLUXER_HTTPS_PORT"), inst.Setting("FLUXER_EDGE_BIND"), inst.Setting(settings.EdgeListenVar))}func waitForEndpoint(ctx context.Context, base, edgeAddr string, insecure bool, limit time.Duration) error {diff --git a/fluxer_rexctl/internal/checks/certificate.go b/fluxer_rexctl/internal/checks/certificate.goindex 84f40c18d..55fd4f7a0 100644--- a/fluxer_rexctl/internal/checks/certificate.go+++ b/fluxer_rexctl/internal/checks/certificate.go@@ -29,7 +29,7 @@ func runCertificate(ctx context.Context, e *Env) Result {}dialCtx, cancel := context.WithTimeout(ctx, 15*time.Second)defer cancel()- conn, err := d.DialContext(dialCtx, "tcp", e.localAddr("FLUXER_HTTPS_PORT", "443"))+ conn, err := d.DialContext(dialCtx, "tcp", e.edgeAddr("FLUXER_HTTPS_PORT", "443"))if err != nil {var opErr *net.OpErrorif errors.As(err, &opErr) && opErr.Op == "dial" {diff --git a/fluxer_rexctl/internal/checks/dns.go b/fluxer_rexctl/internal/checks/dns.goindex ae7b71236..ea87d3fab 100644--- a/fluxer_rexctl/internal/checks/dns.go+++ b/fluxer_rexctl/internal/checks/dns.go@@ -48,16 +48,29 @@ func runDNS(ctx context.Context, e *Env) Result {}return pass(fmt.Sprintf("%s points to %s.", e.Domain, addrs[0].IP))}- public, perr := e.publicAddress(ctx)+ var public string+ var perr error+ here := "this machine is"+ listen := e.publicListenIP()+ if listen != nil {+ public, here = listen.String(), "Fluxer listens on"+ } else {+ public, perr = e.publicAddress(ctx)+ }+ record := "A"+ if ip := net.ParseIP(public); ip != nil && ip.To4() == nil {+ record = "AAAA"+ }if len(addrs) == 0 {if perr != nil {return fail(fmt.Sprintf("%s doesn't point to this machine yet.", e.Domain),"Add an A record for it pointing to this machine's public IP address, then run rexctl doctor.")}return fail(fmt.Sprintf("%s doesn't point to this machine yet.", e.Domain),- fmt.Sprintf("Add an A record for it pointing to %s, then run rexctl doctor.", public))+ fmt.Sprintf("Add an %s record for it pointing to %s, then run rexctl doctor.", record, public))}- if matched := matchHere(addrs, public); len(matched) > 0 {+ matched := matchHere(addrs, public, listen == nil)+ if len(matched) > 0 {return pass(fmt.Sprintf("%s points to this machine (%s).", e.Domain, strings.Join(matched, ", ")))}forwards, reason := e.forwardsHere(ctx)@@ -67,18 +80,26 @@ func runDNS(ctx context.Context, e *Env) Result {if perr != nil {return skip(fmt.Sprintf("Couldn't find this machine's public IP address to compare with %s.", e.Domain))}- problem := fmt.Sprintf("%s points to %s and this machine is %s.", e.Domain, addrs[0].IP, public)+ problem := fmt.Sprintf("%s points to %s and %s %s.", e.Domain, addrs[0].IP, here, public)if reason != "" {return fail(problem+" "+reason,- fmt.Sprintf("If a proxy such as Cloudflare is in front of this machine, let requests to https://%s/_edge through without a challenge, then run rexctl doctor. Otherwise, change the A record to %s.", e.Domain, public))+ fmt.Sprintf("If a proxy such as Cloudflare is in front of this machine, let requests to https://%s/_edge through without a challenge, then run rexctl doctor. Otherwise, change the %s record to %s.", e.Domain, record, public))+ }+ return fail(problem, fmt.Sprintf("Change its %s record to %s, then run rexctl doctor.", record, public))+}++func (e *Env) publicListenIP() net.IP {+ ip := net.ParseIP(e.value(settings.EdgeListenVar))+ if ip == nil || !ip.IsGlobalUnicast() || ip.IsPrivate() {+ return nil}- return fail(problem, fmt.Sprintf("Change its A record to %s, then run rexctl doctor.", public))+ return ip}-func matchHere(addrs []net.IPAddr, public string) []string {+func matchHere(addrs []net.IPAddr, public string, anyLocal bool) []string {pub := net.ParseIP(public)local := map[string]bool{}- if ifaces, err := net.InterfaceAddrs(); err == nil {+ if ifaces, err := net.InterfaceAddrs(); anyLocal && err == nil {for _, a := range ifaces {if n, ok := a.(*net.IPNet); ok && !n.IP.IsLoopback() && !n.IP.IsPrivate() && !n.IP.IsLinkLocalUnicast() {local[n.IP.String()] = truediff --git a/fluxer_rexctl/internal/checks/health.go b/fluxer_rexctl/internal/checks/health.goindex b70b2d799..6cc6c7de3 100644--- a/fluxer_rexctl/internal/checks/health.go+++ b/fluxer_rexctl/internal/checks/health.go@@ -3,6 +3,7 @@package checksimport (+ "cmp""context""crypto/tls""net"@@ -10,6 +11,8 @@ import ("slices""strings""time"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings")func init() {@@ -24,6 +27,10 @@ func (e *Env) localAddr(key, def string) string {return localHostPort(v)}+func (e *Env) edgeAddr(key, def string) string {+ return settings.EdgeDialAddr(e.value(settings.EdgeListenVar), cmp.Or(e.value(key), def))+}+func localHostPort(v string) string {host, port, err := net.SplitHostPort(v)if err != nil {@@ -43,11 +50,11 @@ func (e *Env) healthURLs() []string {return []string{"http://" + e.localAddr("FLUXER_HTTP_PORT", "127.0.0.1:80") + "/_health"}}if strings.EqualFold(e.value("FLUXER_PUBLIC_SCHEME"), "http") {- return []string{"http://" + e.localAddr("FLUXER_HTTP_PORT", "80") + "/_health"}+ return []string{"http://" + e.edgeAddr("FLUXER_HTTP_PORT", "80") + "/_health"}}return []string{- "https://" + e.localAddr("FLUXER_HTTPS_PORT", "443") + "/_health",- "http://" + e.localAddr("FLUXER_HTTP_PORT", "80") + "/_health",+ "https://" + e.edgeAddr("FLUXER_HTTPS_PORT", "443") + "/_health",+ "http://" + e.edgeAddr("FLUXER_HTTP_PORT", "80") + "/_health",}}diff --git a/fluxer_rexctl/internal/checks/voice.go b/fluxer_rexctl/internal/checks/voice.goindex 227764c32..5881ff325 100644--- a/fluxer_rexctl/internal/checks/voice.go+++ b/fluxer_rexctl/internal/checks/voice.go@@ -50,7 +50,7 @@ func (e *Env) voiceProbeTarget(s instance.VoiceServer) (string, string) {case e.Instance.State.IsVoice():return voice.HTTPBase(s.Endpoint), "127.0.0.1:443"}- return voice.InstanceEdge(s.Endpoint, e.setting(settings.EdgeMode), e.value("FLUXER_HTTPS_PORT"), e.value("FLUXER_EDGE_BIND"))+ return voice.InstanceEdge(s.Endpoint, e.setting(settings.EdgeMode), e.value("FLUXER_HTTPS_PORT"), e.value("FLUXER_EDGE_BIND"), e.value(settings.EdgeListenVar))}func (e *Env) voiceProject(s instance.VoiceServer) *engine.Project {diff --git a/fluxer_rexctl/internal/deploy/ports.go b/fluxer_rexctl/internal/deploy/ports.goindex f27256eac..5e74a695c 100644--- a/fluxer_rexctl/internal/deploy/ports.go+++ b/fluxer_rexctl/internal/deploy/ports.go@@ -6,6 +6,7 @@ import ("context""fmt""maps"+ "net""slices""strconv""strings"@@ -47,7 +48,11 @@ func portKeyChanged(changed []string) bool {}func portOf(env map[string]string, v portVar) int {- if n, err := strconv.Atoi(strings.TrimSpace(env[v.name])); err == nil && n > 0 && n <= 65535 {+ raw := strings.TrimSpace(env[v.name])+ if _, port, err := net.SplitHostPort(raw); err == nil {+ raw = port+ }+ if n, err := strconv.Atoi(raw); err == nil && n > 0 && n <= 65535 {return n}return v.defdiff --git a/fluxer_rexctl/internal/deploy/set_test.go b/fluxer_rexctl/internal/deploy/set_test.goindex 5623c8f4c..12932a8d6 100644--- a/fluxer_rexctl/internal/deploy/set_test.go+++ b/fluxer_rexctl/internal/deploy/set_test.go@@ -5,6 +5,7 @@ package deployimport ("encoding/base64""maps"+ "slices""testing""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"@@ -54,3 +55,14 @@ func TestSigningSecretsKeepsExistingKeys(t *testing.T) {t.Fatalf("off made a key: got %q, err %v", got[settings.SignatureSecretsVar], err)}}++func TestListenAddressRestartsEdge(t *testing.T) {+ rs := stack.Embedded()+ k, err := Lookup(rs, nil, settings.EdgeListen)+ if err != nil {+ t.Fatal(err)+ }+ if got := Promised(rs, nil, k); !slices.Equal(got, []string{"edge"}) {+ t.Fatalf("Promised = %v, want edge", got)+ }+}diff --git a/fluxer_rexctl/internal/instance/envvars.go b/fluxer_rexctl/internal/instance/envvars.goindex 42c5d5da5..3b080549e 100644--- a/fluxer_rexctl/internal/instance/envvars.go+++ b/fluxer_rexctl/internal/instance/envvars.go@@ -82,6 +82,7 @@ var envVars = []envVar{{"FLUXER_HTTP_PORT", "edge", "docker-compose.proxy.yml and tunnel.compose.yml ignore it and use FLUXER_EDGE_BIND."},{"FLUXER_HTTPS_PORT", "edge", "If people connect to this port directly, set FLUXER_PUBLIC_PORT to the same port. docker-compose.proxy.yml and tunnel.compose.yml ignore it and use FLUXER_EDGE_BIND."},{"FLUXER_EDGE_BIND", "edge", ""},+ {"FLUXER_EDGE_LISTEN_ADDRESS", "edge", "Write an IPv6 address without brackets. docker-compose.proxy.yml and tunnel.compose.yml ignore it and use FLUXER_EDGE_BIND."},{"FLUXER_ADMIN_BASE_PATH", "edge", "The path the admin panel is served under, with a leading slash and no trailing slash, such as /panel. The edge serves the admin panel under this path, the admin service builds its links with it, and the default FLUXER_ADMIN_ENDPOINT follows it."},{"FLUXER_EDGE_SITE_ADDRESS", "edge", "The site address the edge serves in its Caddyfile, including the scheme. When it's empty, Docker Compose uses FLUXER_CADDY_SITE_ADDRESS, or FLUXER_PUBLIC_SCHEME followed by `://` and FLUXER_DOMAIN. docker-compose.proxy.yml and tunnel.compose.yml set their own value."},{"FLUXER_CADDY_SITE_ADDRESS", "edge", "The old name for FLUXER_EDGE_SITE_ADDRESS. Docker Compose uses it only without rexctl and when FLUXER_EDGE_SITE_ADDRESS is empty. rexctl takeover and rexctl upgrade move it to FLUXER_EDGE_SITE_ADDRESS."},diff --git a/fluxer_rexctl/internal/instance/import.go b/fluxer_rexctl/internal/instance/import.goindex 19c3575d6..47d1e2a45 100644--- a/fluxer_rexctl/internal/instance/import.go+++ b/fluxer_rexctl/internal/instance/import.go@@ -23,6 +23,7 @@ import ("github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/secrets"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack")@@ -53,6 +54,7 @@ type ImportReport struct {Changed []stringDedupedEnv boolEngineProxies bool+ ListenAddress stringRemovedEnv boolImages map[string]stringRelease string@@ -291,6 +293,7 @@ func fill(r *ImportReport, vals *valueResult, minted []string) {r.Defaults = vals.defaultsr.AdminKeys = slices.Sorted(maps.Keys(vals.admin))r.EngineProxies = vals.engineProxies+ r.ListenAddress = vals.settings[settings.EdgeListen]}func pin(s *State, e *engine.Engine) {diff --git a/fluxer_rexctl/internal/instance/shipped.json b/fluxer_rexctl/internal/instance/shipped.jsonindex 59fb8c479..e01e8cd7b 100644--- a/fluxer_rexctl/internal/instance/shipped.json+++ b/fluxer_rexctl/internal/instance/shipped.json@@ -111,6 +111,7 @@"f175f1c9e5baab1acbdafccc34c87b0643b701fe256b7ccc58f5b9c88c3c84f2","f3bca683c2978edce9a6545be5694bc6fa522432ba8c4a1e7d4c3b8cdf7563d9","f56b10bf965a1f37c70dc49c6689b607c48b789badcc1d66a56709b4f2211f6f",+ "fe94546098bd46e1c76fbf8796fd969cf82da4a53aca0fa0506737f28adb8f7d","ff2ff25a393387118f692496d6c9eba4c7dd2ed758d0114f71cf058c3dc4e3a4"],"Caddyfile": [@@ -226,6 +227,7 @@"9341b083d98c743766dfa13a1424f9a138c5ba9f2ff03a89caf7590685fa22a0","94bc891e0d91439c447b4faaa9c29bd685878e04ae30e350f086d60173119671","970565bc2040669194a47efa6cd0a78f8d6738f4396012fa55cca4b3022c9b6e",+ "97b456b370d1e781dd519f2bf4a43e546d593b4894fd131eb0dedf2807c2e7ac","9a806be43efafbd6e2fb436c86f82c2f5923dd732fb0a985d95a21874501b95a","9faeccdfa021c0dcfffe63851dce75bad87a86479baea53affc089dcd41ab016","9fd76b713dd66edd2f127e742f1416d3e557b1d2fd8612b5d8222496009acfb9",diff --git a/fluxer_rexctl/internal/instance/state.go b/fluxer_rexctl/internal/instance/state.goindex 10d8abb68..528d1b244 100644--- a/fluxer_rexctl/internal/instance/state.go+++ b/fluxer_rexctl/internal/instance/state.go@@ -6,6 +6,7 @@ import ("encoding/json""errors""fmt"+ "maps""os""path/filepath""runtime"@@ -54,6 +55,7 @@ type State struct {UpdatedAt time.Time `json:"updated_at"`Unknown []string `json:"-"`+ Repaired []string `json:"-"`rootLayout bool}@@ -183,6 +185,7 @@ type SettingError struct {Path stringKey stringProblem string+ Remedy string}func (e *SettingError) Error() string {@@ -190,6 +193,9 @@ func (e *SettingError) Error() string {}func (e *SettingError) Fix() string {+ if e.Remedy != "" {+ return e.Remedy+ }return "Run rexctl config set " + e.Key + " <value>, or rexctl config unset " + e.Key + " to go back to the default."}@@ -252,7 +258,7 @@ func parseState(data []byte, dir string, repairing []string) (*State, error) {unknown = append(unknown, k)}}- values, more, err := checkSettings(raw["settings"], dir, repairing)+ values, more, repaired, err := checkSettings(raw["settings"], dir, repairing)if err != nil {return nil, err}@@ -267,19 +273,20 @@ func parseState(data []byte, dir string, repairing []string) (*State, error) {return nil, fmt.Errorf("instance: reading state in %s: %w", dir, err)}s.Settings = values+ s.Repaired = repairedslices.Sort(unknown)s.Unknown = unknownreturn &s, nil}-func checkSettings(data json.RawMessage, dir string, repairing []string) (map[string]string, []string, error) {+func checkSettings(data json.RawMessage, dir string, repairing []string) (map[string]string, []string, []string, error) {out := map[string]string{}if len(data) == 0 || string(data) == "null" {- return out, nil, nil+ return out, nil, nil, nil}var raw map[string]json.RawMessageif err := json.Unmarshal(data, &raw); err != nil {- return nil, nil, fmt.Errorf("instance: reading settings in %s: %w", dir, err)+ return nil, nil, nil, fmt.Errorf("instance: reading settings in %s: %w", dir, err)}keys := map[string]settings.Key{}for _, k := range settings.Named() {@@ -288,29 +295,35 @@ func checkSettings(data json.RawMessage, dir string, repairing []string) (map[stpath := filepath.Join(dir, StateDirName, StateFileName)var unknown []stringfor name, value := range raw {- k, isNamed := keys[name]+ _, isNamed := keys[name]var v stringif err := json.Unmarshal(value, &v); err != nil {if isNamed {- return nil, nil, &SettingError{Path: path, Key: name, Problem: "the value is not text"}+ return nil, nil, nil, &SettingError{Path: path, Key: name, Problem: "the value is not text"}}unknown = append(unknown, name)continue}- switch {- case isNamed && slices.Contains(repairing, name):- case isNamed:- if k.Validate != nil {- if err := k.Validate(v); err != nil {- return nil, nil, &SettingError{Path: path, Key: name, Problem: err.Error()}- }- }- case !isRaw(name):+ if !isNamed && !isRaw(name) {unknown = append(unknown, name)}out[name] = v}- return out, unknown, nil+ repaired, err := settings.SplitListenAddress(out, repairing)+ var conflict *settings.ListenConflict+ if errors.As(err, &conflict) {+ return nil, nil, nil, &SettingError{Path: path, Key: settings.EdgeListen, Problem: conflict.Error(), Remedy: conflict.Fix()}+ }+ for _, name := range slices.Sorted(maps.Keys(out)) {+ k, isNamed := keys[name]+ if !isNamed || k.Validate == nil || slices.Contains(repairing, name) {+ continue+ }+ if err := k.Validate(out[name]); err != nil {+ return nil, nil, nil, &SettingError{Path: path, Key: name, Problem: err.Error()}+ }+ }+ return out, unknown, repaired, nil}func isRaw(name string) bool {diff --git a/fluxer_rexctl/internal/instance/state_test.go b/fluxer_rexctl/internal/instance/state_test.gonew file mode 100644index 000000000..43fb93583--- /dev/null+++ b/fluxer_rexctl/internal/instance/state_test.go@@ -0,0 +1,31 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package instance++import (+ "errors"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"+)++func TestParseStateSplitsListenAddress(t *testing.T) {+ data := []byte(`{"schema":5,"project":"fluxer","settings":{"edge.http_port":"192.168.24.35:80","edge.https_port":"192.168.24.35:443"}}`)+ s, err := ParseState(data, "/data/fluxer")+ if err != nil {+ t.Fatal(err)+ }+ if s.Settings[settings.EdgeListen] != "192.168.24.35" || s.Settings[settings.EdgeHTTPPort] != "80" || s.Settings[settings.EdgeHTTPSPort] != "443" || len(s.Repaired) != 2 {+ t.Fatalf("settings %v, repaired %v", s.Settings, s.Repaired)+ }++ differ := []byte(`{"schema":5,"project":"fluxer","settings":{"edge.http_port":"192.168.24.35:80","edge.https_port":"192.168.24.36:443"}}`)+ var se *SettingError+ if _, err := ParseState(differ, "/data/fluxer"); !errors.As(err, &se) || se.Remedy == "" {+ t.Fatalf("differing addresses: err %v", err)+ }+ repair := []string{settings.EdgeHTTPPort}+ if _, err := parseStateRepairing(differ, "/data/fluxer", repair); err != nil {+ t.Fatalf("config set is locked out: %v", err)+ }+}diff --git a/fluxer_rexctl/internal/instance/values.go b/fluxer_rexctl/internal/instance/values.goindex 4d1ea4ec1..beaa4c902 100644--- a/fluxer_rexctl/internal/instance/values.go+++ b/fluxer_rexctl/internal/instance/values.go@@ -3,6 +3,7 @@package instanceimport (+ "errors""maps""slices""strconv"@@ -130,6 +131,11 @@ func importValues(in valueInput) (*valueResult, error) {}}+ var conflict *settings.ListenConflict+ if _, err := settings.SplitListenAddress(r.settings, nil); errors.As(err, &conflict) {+ return nil, &AdoptError{Msg: "FLUXER_HTTP_PORT and FLUXER_HTTPS_PORT in .env use different addresses, and rexctl runs Fluxer on one address for both.", Fix: "Give FLUXER_HTTP_PORT and FLUXER_HTTPS_PORT in .env the same address, then run rexctl takeover again."}+ }+for _, o := range in.overlays {switch o {case stack.OverlayProxy, stack.OverlayTunnel:diff --git a/fluxer_rexctl/internal/settings/effective.go b/fluxer_rexctl/internal/settings/effective.goindex b86a3462c..543bff3d2 100644--- a/fluxer_rexctl/internal/settings/effective.go+++ b/fluxer_rexctl/internal/settings/effective.go@@ -3,6 +3,7 @@package settingsimport (+ "cmp""fmt""maps""net"@@ -80,6 +81,13 @@ func Effective(set *stack.Set, chosen, secrets, adminImport map[string]string) (operator[name] = true}}+ if listen := chosen[EdgeListen]; listen != "" && !reads(EdgeListenVar) && cmp.Or(chosen[EdgeMode], EdgeBundled) == EdgeBundled {+ for v, port := range edgePortVars {+ if reads(v) {+ env[v] = net.JoinHostPort(listen, cmp.Or(env[v], port))+ }+ }+ }for adminKey, value := range adminImport {if v, ok := TwinVar(adminKey); ok {diff --git a/fluxer_rexctl/internal/settings/effective_test.go b/fluxer_rexctl/internal/settings/effective_test.goindex c4fe144da..e19c696e7 100644--- a/fluxer_rexctl/internal/settings/effective_test.go+++ b/fluxer_rexctl/internal/settings/effective_test.go@@ -24,3 +24,18 @@ func TestDerivedFollowsPublicScheme(t *testing.T) {}}}++func TestEdgeDialAddrFollowsListenAddress(t *testing.T) {+ cases := map[string]string{+ "": "127.0.0.1:443",+ "0.0.0.0": "127.0.0.1:443",+ "::": "[::1]:443",+ "203.0.113.7": "203.0.113.7:443",+ "2001:db8::7": "[2001:db8::7]:443",+ }+ for listen, want := range cases {+ if got := EdgeDialAddr(listen, "443"); got != want {+ t.Fatalf("EdgeDialAddr(%q) = %q, want %q", listen, got, want)+ }+ }+}diff --git a/fluxer_rexctl/internal/settings/keys.go b/fluxer_rexctl/internal/settings/keys.goindex a8cc5fe17..75ea52fff 100644--- a/fluxer_rexctl/internal/settings/keys.go+++ b/fluxer_rexctl/internal/settings/keys.go@@ -39,6 +39,7 @@ const (Domain = "domain"EdgeMode = "edge.mode"EdgeBind = "edge.bind"+ EdgeListen = "edge.listen_address"EdgeHTTPPort = "edge.http_port"EdgeHTTPSPort = "edge.https_port"EdgePublicPort = "edge.public_port"@@ -91,6 +92,8 @@ var ImageVars = map[string]string{"edge": "FLUXER_CADDY_IMAGE",}+const EdgeListenVar = "FLUXER_EDGE_LISTEN_ADDRESS"+const (SignatureModeVar = "FLUXER_MEDIA_PROXY_ATTACHMENT_SIGNATURE_MODE"SignatureSecretsVar = "FLUXER_MEDIA_PROXY_ATTACHMENT_URL_SECRETS_BASE64"@@ -114,6 +117,8 @@ func buildNamed() []Key {Help: "Whether Fluxer serves HTTPS itself or runs behind your reverse proxy or a tunnel"},{Name: EdgeBind, Type: "address", Vars: []string{"FLUXER_EDGE_BIND"}, Validate: validBind,Help: "The address your reverse proxy or tunnel connects to"},+ {Name: EdgeListen, Type: "ip", Vars: []string{EdgeListenVar}, Validate: validIP,+ Help: "The IP address on this machine that Fluxer listens on for HTTP and HTTPS, or empty for every address"},{Name: EdgeHTTPPort, Type: "port", Vars: []string{"FLUXER_HTTP_PORT"}, Validate: validPort,Help: "The port Fluxer listens on for HTTP"},{Name: EdgeHTTPSPort, Type: "port", Vars: []string{"FLUXER_HTTPS_PORT"}, Validate: validPort,diff --git a/fluxer_rexctl/internal/settings/listen.go b/fluxer_rexctl/internal/settings/listen.gonew file mode 100644index 000000000..4e17a9637--- /dev/null+++ b/fluxer_rexctl/internal/settings/listen.go@@ -0,0 +1,96 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package settings++import (+ "cmp"+ "fmt"+ "maps"+ "net"+ "slices"+ "strings"+)++var edgePortVars = map[string]string{"FLUXER_HTTP_PORT": "80", "FLUXER_HTTPS_PORT": "443"}++func EdgeDialAddr(listen, port string) string {+ if h, p, err := net.SplitHostPort(port); err == nil {+ listen, port = h, p+ }+ host := "127.0.0.1"+ if ip := net.ParseIP(strings.TrimSpace(listen)); ip != nil && !ip.IsUnspecified() {+ host = ip.String()+ } else if ip != nil && ip.To4() == nil {+ host = "::1"+ }+ return net.JoinHostPort(host, port)+}++type ListenConflict struct {+ Addresses map[string]string+ Ports map[string]string+}++func (c *ListenConflict) Error() string {+ var parts []string+ for _, k := range slices.Sorted(maps.Keys(c.Addresses)) {+ parts = append(parts, k+" uses "+c.Addresses[k])+ }+ last := len(parts) - 1+ return strings.Join(parts[:last], ", ") + " and " + parts[last] + ", but Fluxer listens on one address for HTTP and HTTPS."+}++func (c *ListenConflict) Fix() string {+ keep := cmp.Or(c.Addresses[EdgeListen], c.Addresses[EdgeHTTPSPort], c.Addresses[EdgeHTTPPort])+ var pairs []string+ for _, key := range []string{EdgeHTTPPort, EdgeHTTPSPort} {+ if a, ok := c.Addresses[key]; ok && a != keep {+ pairs = append(pairs, key+"="+c.Ports[key])+ }+ }+ return fmt.Sprintf("Run rexctl config set %s to listen on %s for both.", strings.Join(pairs, " "), keep)+}++func SplitListenAddress(values map[string]string, skip []string) ([]string, error) {+ addresses := map[string]string{}+ ports := map[string]string{EdgeHTTPPort: "80", EdgeHTTPSPort: "443"}+ var split []string+ for _, key := range []string{EdgeHTTPPort, EdgeHTTPSPort} {+ v, ok := values[key]+ if !ok || slices.Contains(skip, key) {+ continue+ }+ ports[key] = v+ if validPort(v) == nil {+ continue+ }+ host, port, err := net.SplitHostPort(strings.TrimSpace(v))+ ip := net.ParseIP(host)+ if err != nil || ip == nil || validPort(port) != nil {+ continue+ }+ addresses[key], ports[key] = ip.String(), port+ split = append(split, key)+ }+ if len(split) == 0 {+ return nil, nil+ }+ if listen, ok := values[EdgeListen]; ok && listen != "" && !slices.Contains(skip, EdgeListen) {+ if ip := net.ParseIP(listen); ip != nil {+ addresses[EdgeListen] = ip.String()+ } else {+ addresses[EdgeListen] = listen+ }+ }+ first := addresses[split[0]]+ for _, a := range addresses {+ if a != first {+ return nil, &ListenConflict{Addresses: addresses, Ports: ports}+ }+ }+ for _, key := range split {+ values[key] = ports[key]+ }+ values[EdgeListen] = first+ return split, nil+}diff --git a/fluxer_rexctl/internal/settings/listen_test.go b/fluxer_rexctl/internal/settings/listen_test.gonew file mode 100644index 000000000..c322cd1f8--- /dev/null+++ b/fluxer_rexctl/internal/settings/listen_test.go@@ -0,0 +1,56 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package settings++import (+ "errors"+ "maps"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+)++func TestSplitListenAddress(t *testing.T) {+ cases := []struct {+ name string+ in map[string]string+ want map[string]string+ }{+ {"ipv4 both", map[string]string{EdgeHTTPPort: "192.168.24.35:80", EdgeHTTPSPort: "192.168.24.35:443"},+ map[string]string{EdgeHTTPPort: "80", EdgeHTTPSPort: "443", EdgeListen: "192.168.24.35"}},+ {"ipv6 both", map[string]string{EdgeHTTPPort: "[2001:db8::7]:8080", EdgeHTTPSPort: "[2001:db8::7]:8443"},+ map[string]string{EdgeHTTPPort: "8080", EdgeHTTPSPort: "8443", EdgeListen: "2001:db8::7"}},+ {"one plain", map[string]string{EdgeHTTPPort: "80", EdgeHTTPSPort: "192.168.24.35:443"},+ map[string]string{EdgeHTTPPort: "80", EdgeHTTPSPort: "443", EdgeListen: "192.168.24.35"}},+ {"plain ports", map[string]string{EdgeHTTPPort: "80"}, map[string]string{EdgeHTTPPort: "80"}},+ }+ for _, c := range cases {+ got := maps.Clone(c.in)+ if _, err := SplitListenAddress(got, nil); err != nil || !maps.Equal(got, c.want) {+ t.Fatalf("%s: got %v, err %v", c.name, got, err)+ }+ }++ var conflict *ListenConflict+ differ := map[string]string{EdgeHTTPPort: "192.168.24.35:80", EdgeHTTPSPort: "192.168.24.36:443"}+ if _, err := SplitListenAddress(differ, nil); !errors.As(err, &conflict) {+ t.Fatalf("differing addresses: err %v", err)+ }+ if want := "Run rexctl config set edge.http_port=80 to listen on 192.168.24.36 for both."; conflict.Fix() != want {+ t.Fatalf("fix %q", conflict.Fix())+ }+ if _, err := SplitListenAddress(differ, []string{EdgeHTTPPort}); err != nil || differ[EdgeListen] != "192.168.24.36" {+ t.Fatalf("repairing edge.http_port: got %v, err %v", differ, err)+ }+}++func TestOldReleaseKeepsListenAddressInPorts(t *testing.T) {+ set := &stack.Set{Vars: []string{"FLUXER_HTTP_PORT", "FLUXER_HTTPS_PORT"}}+ env, err := Effective(set, map[string]string{EdgeListen: "2001:db8::7", EdgeHTTPSPort: "8443"}, nil, nil)+ if err != nil {+ t.Fatal(err)+ }+ if env["FLUXER_HTTP_PORT"] != "[2001:db8::7]:80" || env["FLUXER_HTTPS_PORT"] != "[2001:db8::7]:8443" {+ t.Fatalf("got %q and %q", env["FLUXER_HTTP_PORT"], env["FLUXER_HTTPS_PORT"])+ }+}diff --git a/fluxer_rexctl/internal/stack/files/.env.example b/fluxer_rexctl/internal/stack/files/.env.exampleindex 27f96799f..b200f48f9 100644--- a/fluxer_rexctl/internal/stack/files/.env.example+++ b/fluxer_rexctl/internal/stack/files/.env.example@@ -366,6 +366,13 @@ FLUXER_VAPID_PRIVATE_KEY=# rexctl: rexctl config set edge.bind# FLUXER_EDGE_BIND=+# The IP address on this machine that Fluxer listens on for HTTP and HTTPS, or+# empty for every address. Write an IPv6 address without brackets.+# docker-compose.proxy.yml and tunnel.compose.yml ignore it and use+# FLUXER_EDGE_BIND.+# rexctl: rexctl config set edge.listen_address+# FLUXER_EDGE_LISTEN_ADDRESS=+# The path the admin panel is served under, with a leading slash and no# trailing slash, such as /panel. The edge serves the admin panel under this# path, the admin service builds its links with it, and the defaultdiff --git a/fluxer_rexctl/internal/stack/files/docker-compose.yml b/fluxer_rexctl/internal/stack/files/docker-compose.ymlindex 4fb82504a..c0390215f 100644--- a/fluxer_rexctl/internal/stack/files/docker-compose.yml+++ b/fluxer_rexctl/internal/stack/files/docker-compose.yml@@ -272,9 +272,9 @@ services:limits:memory: ${FLUXER_CADDY_MEMORY_LIMIT:-384mb}ports:- - "${FLUXER_HTTP_PORT:-80}:80"- - "${FLUXER_HTTPS_PORT:-443}:443"- - "${FLUXER_HTTPS_PORT:-443}:443/udp"+ - "${FLUXER_EDGE_LISTEN_ADDRESS:+[${FLUXER_EDGE_LISTEN_ADDRESS}]:}${FLUXER_HTTP_PORT:-80}:80"+ - "${FLUXER_EDGE_LISTEN_ADDRESS:+[${FLUXER_EDGE_LISTEN_ADDRESS}]:}${FLUXER_HTTPS_PORT:-443}:443"+ - "${FLUXER_EDGE_LISTEN_ADDRESS:+[${FLUXER_EDGE_LISTEN_ADDRESS}]:}${FLUXER_HTTPS_PORT:-443}:443/udp"environment:FLUXER_EDGE_SITE_ADDRESS: ${FLUXER_EDGE_SITE_ADDRESS:-${FLUXER_CADDY_SITE_ADDRESS:-${FLUXER_PUBLIC_SCHEME:-https}://${FLUXER_DOMAIN:-}}}FLUXER_EDGE_TRUSTED_PROXIES: ${FLUXER_EDGE_TRUSTED_PROXIES:-127.0.0.1/32}diff --git a/fluxer_rexctl/internal/voice/net.go b/fluxer_rexctl/internal/voice/net.goindex 4da9fcccf..dbc2a60c4 100644--- a/fluxer_rexctl/internal/voice/net.go+++ b/fluxer_rexctl/internal/voice/net.go@@ -459,7 +459,7 @@ func dialHost(bind string) string {return a.String()}-func InstanceEdge(endpoint, mode, httpsPort, bind string) (string, string) {+func InstanceEdge(endpoint, mode, httpsPort, bind, listen string) (string, string) {base := HTTPBase(endpoint)switch mode {case settings.EdgeProxy, settings.EdgeTunnel:@@ -480,7 +480,7 @@ func InstanceEdge(endpoint, mode, httpsPort, bind string) (string, string) {if v := usable(httpsPort); v != "" {port = v}- return base, "127.0.0.1:" + port+ return base, settings.EdgeDialAddr(usable(listen), port)}func usable(v string) string {