diff --git a/fluxer_docs/src/content/docs/operator/rexctl.mdx b/fluxer_docs/src/content/docs/operator/rexctl.mdx index 056bcf301..eeb8704fb 100644 --- a/fluxer_docs/src/content/docs/operator/rexctl.mdx +++ b/fluxer_docs/src/content/docs/operator/rexctl.mdx @@ -580,7 +580,7 @@ rexctl install --runtime docker --domain chat.example.com --behind-proxy --yes | `--open-firewall` | On | Open the ports Fluxer needs in ufw or firewalld. Turn it off with `--open-firewall=false`. | | `--project string` | `fluxer` | Name for this instance's service, containers and folders. A second Fluxer on one machine gets a free name by itself. | | `--proxy string` | | What is in front of Fluxer (local, remote or tunnel). Implies `--behind-proxy`. | -| `--proxy-from string` | | Address your proxy on another machine connects from, with `--proxy remote`. | +| `--proxy-from string` | | Addresses or ranges your proxy on another machine connects from, or private for any private address, with `--proxy remote`. | | `--registration string` | | Who can create an account (invite, approval, open or closed). | | `--runtime string` | | Where Fluxer runs, systemd for a service on this machine or docker or podman for one container. Without it, rexctl picks systemd on Debian or Ubuntu as root and one container everywhere else. | | `--setup string` | | Where to create your admin account (terminal or browser). | @@ -766,7 +766,7 @@ rexctl proxy on --bind 10.0.0.5:8080 --proxy-from 10.0.0.2 | --- | --- | --- | | `--bind string` | | Address your proxy or tunnel connects to, kept when left out (`127.0.0.1:8080` the first time). | | `--proxy string` | | What is in front of Fluxer (local, remote or tunnel). | -| `--proxy-from string` | | Address your proxy connects from, when other machines can connect to `--bind`. | +| `--proxy-from string` | | Addresses or ranges your proxy connects from, or private for any private address, when other machines can connect to `--bind`. | ## rexctl registration diff --git a/fluxer_rexctl/cmd/install.go b/fluxer_rexctl/cmd/install.go index 0f32fb0d4..5645203b4 100644 --- a/fluxer_rexctl/cmd/install.go +++ b/fluxer_rexctl/cmd/install.go @@ -130,7 +130,7 @@ func newInstallCmd(app *App) *cobra.Command { fl.BoolVar(&f.BehindProxy, "behind-proxy", false, "serve plain HTTP for your own reverse proxy or tunnel") fl.StringVar(&f.Bind, "bind", settings.DefaultBind, "address your proxy or tunnel connects to. Implies --behind-proxy") fl.StringVar(&f.Proxy, "proxy", "", "what is in front of Fluxer (local, remote or tunnel). Implies --behind-proxy") - fl.StringVar(&f.ProxyFrom, "proxy-from", "", "address your proxy on another machine connects from, with --proxy remote") + fl.StringVar(&f.ProxyFrom, "proxy-from", "", "addresses or ranges your proxy on another machine connects from, or private for any private address, with --proxy remote") fl.StringVar(&f.VoiceIP, "voice-ip", "", "public IP address for voice and video (found automatically)") fl.StringVar(&f.MemoryProfile, "memory-profile", "", "use small, medium or large on every start, without sizing Fluxer to this machine") fl.BoolVar(&f.InstallDocker, "install-docker", true, "install Docker if it's missing") @@ -600,14 +600,19 @@ func validateBind(v string) error { } func validateProxyFrom(v string) error { - v = strings.TrimSpace(v) - if _, err := netip.ParseAddr(v); err == nil { - return nil + sources := proxySources(v) + bad := len(sources) == 0 + for _, s := range sources { + _, addrErr := netip.ParseAddr(s) + _, prefixErr := netip.ParsePrefix(s) + if s != proxyFromPrivate && addrErr != nil && prefixErr != nil { + bad = true + } } - if _, err := netip.ParsePrefix(v); err == nil { - return nil + if bad { + return setupInputError("Enter an IP address or range, like 203.0.113.10 or 10.0.0.0/24, or private for any address on your private network. Separate several with commas.") } - return setupInputError("Enter an IP address or range, like 203.0.113.10 or 10.0.0.0/24.") + return nil } var projectPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]*$`) diff --git a/fluxer_rexctl/cmd/install_network.go b/fluxer_rexctl/cmd/install_network.go index a857cb3f9..fdce3aee1 100644 --- a/fluxer_rexctl/cmd/install_network.go +++ b/fluxer_rexctl/cmd/install_network.go @@ -348,16 +348,7 @@ func (in *installer) networkFields() []ui.Field { }) } fields = append(fields, - ui.Field{ - Flag: "proxy-from", - FlagLabel: "--proxy-from", - Title: "What address does your proxy connect from?", - Description: "Fluxer trusts the visitor address your proxy passes on only from here, and the firewall lets only this address in.", - Placeholder: "203.0.113.10", - Value: &n.from, - Validate: validateProxyFrom, - Hide: func() bool { return !remote() }, - }, + proxyFromField(&n.from, func() bool { return !remote() }), ui.Field{ Title: "Do people open this address over plain HTTP?", Description: "Lokinet and .alt addresses often have no certificate.", @@ -442,7 +433,7 @@ func (in *installer) networkSummary() string { case settings.ProxyOverlay: return "Behind your Tor, I2P or Lokinet proxy, Fluxer listens on " + f.Bind case settings.ProxyRemote: - return fmt.Sprintf("Behind a proxy at %s, Fluxer listens on %s", n.from, f.Bind) + return fmt.Sprintf("Behind a proxy at %s, Fluxer listens on %s", proxyFromWords(n.from), f.Bind) case settings.ProxyTunnel: return "Behind a tunnel, Fluxer listens on " + f.Bind } @@ -584,11 +575,24 @@ func privateSource(v string) bool { return a.IsPrivate() || a.IsLoopback() } if p, err := netip.ParsePrefix(v); err == nil { - return p.Addr().IsPrivate() + return p.Addr().IsPrivate() || p.Addr().IsLoopback() } return false } +func proxyFromField(v *string, hide func() bool) ui.Field { + return ui.Field{ + Flag: "proxy-from", + FlagLabel: "--proxy-from", + Title: "What address does your proxy connect from?", + Description: "Fluxer trusts the visitor address your proxy passes on only from here, and the firewall lets only this address in. Enter private for any address on your private network.", + Placeholder: "203.0.113.10", + Value: v, + Validate: validateProxyFrom, + Hide: hide, + } +} + func (in *installer) plainHTTP() bool { d := in.f.Domain if !in.f.BehindProxy { diff --git a/fluxer_rexctl/cmd/install_ports.go b/fluxer_rexctl/cmd/install_ports.go index 08b853633..b96f16f6d 100644 --- a/fluxer_rexctl/cmd/install_ports.go +++ b/fluxer_rexctl/cmd/install_ports.go @@ -235,9 +235,7 @@ func (in *installer) firewallPorts() []host.Port { out = append(out, host.Port{Number: p.Number, Proto: p.Proto}) } if in.f.BehindProxy && in.f.Proxy == settings.ProxyRemote && !settings.BindIsLoopback(in.f.Bind) { - if port := bindPortOf(in.f.Bind); port > 0 { - out = append(out, host.Port{Number: port, Proto: "tcp", Source: in.f.ProxyFrom}) - } + out = append(out, proxyPorts(in.f.Bind, in.f.ProxyFrom)...) } return out } diff --git a/fluxer_rexctl/cmd/migrate_docker_target.go b/fluxer_rexctl/cmd/migrate_docker_target.go index 72e404b15..136733efe 100644 --- a/fluxer_rexctl/cmd/migrate_docker_target.go +++ b/fluxer_rexctl/cmd/migrate_docker_target.go @@ -17,6 +17,7 @@ import ( "time" "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/backup" + "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/checks" "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy" "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine" "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile" @@ -188,9 +189,7 @@ func (t *moveBase) install(ctx context.Context, runtimeFlag string) error { return ui.Wrap(err, "Couldn't keep a copy of the services that run the old containers.", "Run "+migrateCommand+" --verbose to see why.") } args := []string{"install", "--runtime", runtimeFlag, "--for-migrate", "--migrate-from", t.inst.Dir, "--dir", t.dir, "--project", t.inst.Project(), "--domain", t.inst.Domain(), "--setup", "browser", "--no-start", "--yes"} - if mode := t.chosen[settings.EdgeMode]; mode != "" && mode != settings.EdgeBundled { - args = append(args, "--bind", t.chosen[settings.EdgeBind]) - } + args = append(args, installNetworkArgs(t.inst.Domain(), t.chosen)...) if t.app.Flags.Verbose { args = append(args, "--verbose") } @@ -205,6 +204,26 @@ func (t *moveBase) install(ctx context.Context, runtimeFlag string) error { return saveInstance(target) } +func installNetworkArgs(domain string, chosen map[string]string) []string { + mode := chosen[settings.EdgeMode] + if mode == "" || mode == settings.EdgeBundled { + return nil + } + bind := chosen[settings.EdgeBind] + args := []string{"--bind", bind} + kind := chosen[settings.EdgeProxyKind] + if kind == "" && mode == settings.EdgeTunnel { + kind = settings.ProxyTunnel + } + switch { + case kind == settings.ProxyLocal || kind == settings.ProxyTunnel: + return append(args, "--proxy", kind) + case kind == settings.ProxyRemote || !settings.BindIsLoopback(bind) && !checks.OverlayDomain(domain): + return append(args, "--proxy", settings.ProxyRemote, "--proxy-from", migratedProxyFrom(chosen[settings.EdgeTrusted])) + } + return args +} + func (t *moveBase) open(chosen map[string]string) (*instance.Instance, error) { target, err := instance.Find(t.dir) if err != nil { diff --git a/fluxer_rexctl/cmd/proxy.go b/fluxer_rexctl/cmd/proxy.go index 0ce348dfa..e2568bbe4 100644 --- a/fluxer_rexctl/cmd/proxy.go +++ b/fluxer_rexctl/cmd/proxy.go @@ -6,9 +6,12 @@ import ( "cmp" "context" "fmt" + "net" + "net/netip" "os" "os/exec" "path/filepath" + "slices" "strings" "time" @@ -67,7 +70,7 @@ func newProxyCmd(app *App) *cobra.Command { } on.Flags().StringVar(&bind, "bind", "", "address your proxy or tunnel connects to, kept when left out ("+settings.DefaultBind+" the first time)") on.Flags().StringVar(&kind, "proxy", "", "what is in front of Fluxer (local, remote or tunnel)") - on.Flags().StringVar(&from, "proxy-from", "", "address your proxy connects from, when other machines can connect to --bind") + on.Flags().StringVar(&from, "proxy-from", "", "addresses or ranges your proxy connects from, or private for any private address, when other machines can connect to --bind") on.Flags().StringVar(&kind, "kind", "", "") on.Flags().StringVar(&from, "from", "", "") _ = on.Flags().MarkHidden("kind") @@ -183,7 +186,7 @@ func proxyOn(c *cobra.Command, app *App, changes map[string]*string, from string if err != nil || !ok { return err } - if err := app.UI.Ask(c, ui.Field{Flag: "proxy-from", Title: "What address does your proxy connect from?", Placeholder: "203.0.113.10", Value: &from, Validate: validateProxyFrom}); err != nil { + if err := app.UI.Ask(c, proxyFromField(&from, nil)); err != nil { return err } from = strings.TrimSpace(from) @@ -230,10 +233,12 @@ func proxyOff(ctx context.Context, app *App) error { own := "internal" changes[edgeTLSVar] = &own } + var sources []string for _, p := range inst.State.Firewall { - if trusted, ok := trustedFor(p.Source); ok && st[settings.EdgeTrusted] == trusted { - changes[settings.EdgeTrusted] = nil - } + sources = append(sources, p.Source) + } + if trusted, ok := trustedFor(strings.Join(sources, ",")); ok && st[settings.EdgeTrusted] == trusted { + changes[settings.EdgeTrusted] = nil } if err := edgeGuard(inst, changes); err != nil { return err @@ -263,11 +268,93 @@ func proxyOff(ctx context.Context, app *App) error { return nil } +const proxyFromPrivate = "private" + +var privateRanges = []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "fc00::/7"} + +func proxySources(from string) []string { + var out []string + for _, s := range settings.TrustedProxyList(strings.ToLower(from)) { + if !slices.Contains(out, s) { + out = append(out, s) + } + } + return out +} + func trustedFor(from string) (string, bool) { - if from == "" || privateSource(from) { + trusted, named := []string{"private_ranges"}, false + for _, s := range proxySources(from) { + switch { + case s == proxyFromPrivate: + named = true + case !privateSource(s): + trusted = append(trusted, s) + } + } + if len(trusted) == 1 && !named { return "", false } - return "private_ranges," + from, true + return strings.Join(trusted, ","), true +} + +func firewallSources(bind, from string) []string { + var out []string + for _, s := range proxySources(from) { + add := []string{s} + if s == proxyFromPrivate { + add = privateRangesFor(bind) + } + for _, a := range add { + if !slices.Contains(out, a) { + out = append(out, a) + } + } + } + return out +} + +func privateRangesFor(bind string) []string { + host, _, err := net.SplitHostPort(bind) + if err != nil { + return privateRanges + } + a, err := netip.ParseAddr(host) + if err != nil || a.IsUnspecified() && a.Is6() { + return privateRanges + } + return slices.DeleteFunc(slices.Clone(privateRanges), func(r string) bool { + return netip.MustParsePrefix(r).Addr().Is4() != a.Unmap().Is4() + }) +} + +func proxyFromWords(from string) string { + var words []string + for _, s := range proxySources(from) { + if s == proxyFromPrivate { + s = "a private address" + } + words = append(words, s) + } + return ui.JoinOr(words) +} + +func migratedProxyFrom(trusted string) string { + entries := settings.TrustedProxyList(trusted) + private := len(entries) == 0 + var public []string + for _, e := range entries { + switch { + case e == "private_ranges" || privateSource(e): + private = true + case !slices.Contains(public, e): + public = append(public, e) + } + } + if private { + public = append([]string{proxyFromPrivate}, public...) + } + return strings.Join(public, ",") } func applyProxy(ctx context.Context, app *App, inst *instance.Instance, changes map[string]*string, title string) (*deploy.Change, error) { @@ -324,15 +411,15 @@ func applyProxy(ctx context.Context, app *App, inst *instance.Instance, changes } func openProxySource(ctx context.Context, app *App, inst *instance.Instance, bind, from string) error { - port := bindPortOf(bind) - if from == "" || port == 0 { + ports := proxyPorts(bind, from) + if len(ports) == 0 { return nil } fw, err := host.DetectFirewall(ctx) if err != nil || fw == host.FirewallNone { return nil } - closed, err := host.ClosedPorts(ctx, fw, []host.Port{{Number: port, Proto: "tcp", Source: from}}) + closed, err := host.ClosedPorts(ctx, fw, ports) if err != nil || len(closed) == 0 { return nil } @@ -345,7 +432,7 @@ func openProxySource(ctx context.Context, app *App, inst *instance.Instance, bin return nil } if err := host.OpenPorts(ctx, fw, closed); err != nil { - return ui.Wrap(err, "Couldn't open the port for your proxy.", fmt.Sprintf("Open TCP %d from %s in %s yourself.", port, from, fw)) + return ui.Wrap(err, "Couldn't open the port for your proxy.", fmt.Sprintf("Open TCP %d from %s in %s yourself.", bindPortOf(bind), proxyFromWords(from), fw)) } for _, p := range closed { inst.State.Firewall = append(inst.State.Firewall, instance.OpenedPort{Firewall: string(fw), Number: p.Number, Proto: p.Proto, Source: p.Source}) @@ -357,12 +444,24 @@ func openProxySource(ctx context.Context, app *App, inst *instance.Instance, bin return nil } +func proxyPorts(bind, from string) []host.Port { + port := bindPortOf(bind) + if port == 0 { + return nil + } + var out []host.Port + for _, s := range firewallSources(bind, from) { + out = append(out, host.Port{Number: port, Proto: "tcp", Source: s}) + } + return out +} + func closeProxySources(ctx context.Context, app *App, inst *instance.Instance, bind, from string) error { - keep := host.Port{Number: bindPortOf(bind), Proto: "tcp", Source: from} + keep := proxyPorts(bind, from) var stale []instance.OpenedPort var kept []instance.OpenedPort for _, p := range inst.State.Firewall { - if p.Source == "" || (host.Port{Number: p.Number, Proto: p.Proto, Source: p.Source}) == keep { + if p.Source == "" || slices.Contains(keep, host.Port{Number: p.Number, Proto: p.Proto, Source: p.Source}) { kept = append(kept, p) continue } @@ -633,14 +732,14 @@ func (in *installer) waitProxy(ctx context.Context) error { func (in *installer) proxySource() string { if in.f.Proxy == settings.ProxyRemote { - return in.f.ProxyFrom + return strings.Join(slices.DeleteFunc(proxySources(in.f.ProxyFrom), func(s string) bool { return s == proxyFromPrivate }), ",") } return "127.0.0.1" } func (in *installer) clientIPFix() string { if in.f.Proxy == settings.ProxyRemote { - return "Make your proxy set the X-Forwarded-For header, and check that it connects from " + in.f.ProxyFrom + ". Then run rexctl doctor." + return "Make your proxy set the X-Forwarded-For header, and check that it connects from " + proxyFromWords(in.f.ProxyFrom) + ". Then run rexctl doctor." } return "Make your " + proxyNoun(in.f.Proxy) + " set the X-Forwarded-For header. rexctl proxy config " + string(in.net.server) + " shows how." } diff --git a/fluxer_rexctl/internal/checks/proxy.go b/fluxer_rexctl/internal/checks/proxy.go index c9e82eb46..0437a0018 100644 --- a/fluxer_rexctl/internal/checks/proxy.go +++ b/fluxer_rexctl/internal/checks/proxy.go @@ -137,14 +137,17 @@ func judgeClientIP(seen netip.Addr, source string, loopbackDomain bool) (ui.Stat return ui.StateOK, "Fluxer sees this machine as " + seen.String() } -func fromSource(seen netip.Addr, source string) bool { - source = strings.TrimSpace(source) - if a, err := netip.ParseAddr(source); err == nil { - a = a.Unmap() - return seen == a || a.IsLoopback() && seen.IsLoopback() - } - if p, err := netip.ParsePrefix(source); err == nil { - return p.Contains(seen) +func fromSource(seen netip.Addr, sources string) bool { + for _, source := range settings.TrustedProxyList(sources) { + if a, err := netip.ParseAddr(source); err == nil { + a = a.Unmap() + if seen == a || a.IsLoopback() && seen.IsLoopback() { + return true + } + } + if p, err := netip.ParsePrefix(source); err == nil && p.Contains(seen) { + return true + } } return false } diff --git a/fluxer_rexctl/internal/settings/effective.go b/fluxer_rexctl/internal/settings/effective.go index 81441e39f..65d0dea39 100644 --- a/fluxer_rexctl/internal/settings/effective.go +++ b/fluxer_rexctl/internal/settings/effective.go @@ -147,7 +147,7 @@ func namedVars(k Key, value string) map[string]string { case Domain: value = NormalDomain(value) case EdgeTrusted: - value = strings.Join(trustedProxyList(value), " ") + value = strings.Join(TrustedProxyList(value), " ") case StoragePath: if b, err := strconv.ParseBool(value); err == nil { value = strconv.FormatBool(b) diff --git a/fluxer_rexctl/internal/settings/validate.go b/fluxer_rexctl/internal/settings/validate.go index 335c5bb6c..9b16702cb 100644 --- a/fluxer_rexctl/internal/settings/validate.go +++ b/fluxer_rexctl/internal/settings/validate.go @@ -34,7 +34,7 @@ func validText(v string) error { return nil } -func trustedProxyList(v string) []string { +func TrustedProxyList(v string) []string { return strings.FieldsFunc(v, func(r rune) bool { return r == ',' || r == ' ' || r == '\t' }) } @@ -53,7 +53,7 @@ func validSignatureSecrets(v string) error { } func validTrustedProxies(v string) error { - for _, entry := range trustedProxyList(v) { + for _, entry := range TrustedProxyList(v) { if entry == "private_ranges" || net.ParseIP(entry) != nil { continue }