A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(rexctl): add --http-bind for voice servers behind a proxy (#3722)
Hampus92a3c67ce8bd
diff --git a/fluxer_docs/src/content/docs/operator/environment.mdx b/fluxer_docs/src/content/docs/operator/environment.mdxindex 116b13bb6..9f5cd8cf4 100644--- a/fluxer_docs/src/content/docs/operator/environment.mdx+++ b/fluxer_docs/src/content/docs/operator/environment.mdx@@ -354,7 +354,11 @@ The host address the media ports are published on, for a host with more than one### `FLUXER_VOICE_HTTP_PORT`-The port on 127.0.0.1 that `voice-http.compose.yml` publishes LiveKit's HTTP and WebSocket port on, for your own proxy.+The port that `voice-http.compose.yml` publishes LiveKit's HTTP and WebSocket port on, for your own proxy.++### `FLUXER_VOICE_HTTP_BIND`++The host address that `voice-http.compose.yml` publishes `FLUXER_VOICE_HTTP_PORT` on. The default is `127.0.0.1`. Use another address of the host, or `0.0.0.0` for every address, when your proxy runs in another network. Write an IPv6 address in brackets, such as `[2001:db8::5]`. The port has no TLS of its own. Let only your proxy's network connect to it in your firewall.### `FLUXER_VOICE_NETWORK`diff --git a/fluxer_docs/src/content/docs/operator/rexctl.mdx b/fluxer_docs/src/content/docs/operator/rexctl.mdxindex 62d95f39e..4e1da2b3f 100644--- a/fluxer_docs/src/content/docs/operator/rexctl.mdx+++ b/fluxer_docs/src/content/docs/operator/rexctl.mdx@@ -1075,9 +1075,13 @@ rexctl voice join <domain> [token] [flags]Hostname for the server's endpoint and certificate.+- `--http-bind string`++ Address the port for your own proxy listens on, with --no-edge (default 127.0.0.1).+- `--http-port int`- Port on 127.0.0.1 for your own proxy, with --no-edge.+ Port for your own proxy, with --no-edge.- `--id string`@@ -1105,7 +1109,7 @@ rexctl voice join <domain> [token] [flags]- `--no-edge`- Start no edge and publish the server on 127.0.0.1 for your own proxy.+ Start no edge and publish the server's HTTP port for your own proxy.- `--no-firewall`diff --git a/fluxer_docs/src/content/docs/operator/voice-compose.mdx b/fluxer_docs/src/content/docs/operator/voice-compose.mdxindex ecb286424..c104839d1 100644--- a/fluxer_docs/src/content/docs/operator/voice-compose.mdx+++ b/fluxer_docs/src/content/docs/operator/voice-compose.mdx@@ -127,8 +127,8 @@ When nginx or another proxy already holds ports 80 and 443 on the machine, skipdocker network create fluxer-voice```-2. Set `FLUXER_VOICE_HTTP_PORT` in the server's settings file to a free port on `127.0.0.1`, such as 7880 for the first server and 7885 for the next.-3. Start the server with `voice-http.compose.yml` as well. It publishes the server's HTTP port on `127.0.0.1` only.+2. Set `FLUXER_VOICE_HTTP_PORT` in the server's settings file to a free port, such as 7880 for the first server and 7885 for the next. When your proxy runs in another network, also set `FLUXER_VOICE_HTTP_BIND` to an address of the machine that the proxy can connect to.+3. Start the server with `voice-http.compose.yml` as well. It publishes the server's HTTP port on `127.0.0.1`, or on `FLUXER_VOICE_HTTP_BIND` when you set it. That port has no TLS of its own. Let only your proxy's network connect to it.```shdocker compose -p fluxer-voice-fra-server-1 --env-file fra-server-1.env -f voice.compose.yml -f voice-http.compose.yml up -ddiff --git a/fluxer_docs/src/content/docs/operator/voice-servers.mdx b/fluxer_docs/src/content/docs/operator/voice-servers.mdxindex 7b0caeb12..a8fd92c35 100644--- a/fluxer_docs/src/content/docs/operator/voice-servers.mdx+++ b/fluxer_docs/src/content/docs/operator/voice-servers.mdx@@ -179,6 +179,12 @@ rexctl voice join chat.example.com fvj_... --no-edge --endpoint wss://voice.examrexctl publishes the server's HTTP port on `127.0.0.1`, from 7880 upwards, or at `--http-port`. Point your proxy at it with WebSocket upgrades and a read timeout of at least 120 seconds.+When your proxy runs in another network or on another machine, pass `--http-bind` with an address of the voice machine that the proxy can connect to, or `0.0.0.0` for every address. This port has no TLS of its own. Let only your proxy's network connect to it in your firewall. To change the address later, remove the server and join it again with a new token.++```sh+rexctl voice join chat.example.com fvj_... --no-edge --endpoint wss://voice.example.com/livekit-fra-server-1 --http-bind 10.0.0.5+```+nginx needs a location block like this one.```nginxdiff --git a/fluxer_rexctl/cmd/voice_add.go b/fluxer_rexctl/cmd/voice_add.goindex e9c2013c1..95f43795d 100644--- a/fluxer_rexctl/cmd/voice_add.go+++ b/fluxer_rexctl/cmd/voice_add.go@@ -25,28 +25,30 @@ import ()type voiceFlags struct {- ID string- Region string- NewRegion string- RegionName string- Lat, Long float64- HasCoords bool- Hostname string- IP string- TCPPort int- UDPPort int- SoftLimit int- Memory string- Image string- Email string- NoEdge bool- Endpoint string- HTTPPort int- TokenStdin bool- NoFirewall bool- DryRun bool- Force bool- Count int+ ID string+ Region string+ NewRegion string+ RegionName string+ Lat, Long float64+ HasCoords bool+ Hostname string+ IP string+ TCPPort int+ UDPPort int+ SoftLimit int+ Memory string+ Image string+ Email string+ NoEdge bool+ Endpoint string+ HTTPPort int+ HTTPBind string+ HTTPBindSet bool+ TokenStdin bool+ NoFirewall bool+ DryRun bool+ Force bool+ Count int}func addVoiceOverrideFlags(c *cobra.Command, f *voiceFlags) {@@ -65,6 +67,24 @@ func addVoiceOverrideFlags(c *cobra.Command, f *voiceFlags) {fl.BoolVar(&f.DryRun, "dry-run", false, "show what would happen and change nothing")}+func checkHTTPBind(f *voiceFlags, local []string) error {+ if f.HTTPBind == "" {+ return nil+ }+ if !f.NoEdge {+ return ui.Usage("Pass --http-bind with --no-edge.")+ }+ bind, ok := voice.NormalHTTPBind(f.HTTPBind)+ if !ok {+ return ui.Usage(fmt.Sprintf("Pass --http-bind as an IPv4 or IPv6 address. %s isn't one.", f.HTTPBind))+ }+ if bind != "" && !voice.HTTPBindOnMachine(bind, local) {+ return ui.Refused(fmt.Sprintf("%s isn't an address of this machine.", f.HTTPBind), "Pass one of its own addresses, or 0.0.0.0 for every address.")+ }+ f.HTTPBind, f.HTTPBindSet = bind, true+ return nil+}+func validateVoiceFlags(c *cobra.Command, f *voiceFlags) error {if err := checkServerID(f.ID); err != nil {return errdiff --git a/fluxer_rexctl/cmd/voice_join.go b/fluxer_rexctl/cmd/voice_join.goindex eb6ffe145..e79fe163a 100644--- a/fluxer_rexctl/cmd/voice_join.go+++ b/fluxer_rexctl/cmd/voice_join.go@@ -68,6 +68,9 @@ func newVoiceJoinCmd(app *App) *cobra.Command {if f.Endpoint != "" && !strings.HasPrefix(f.Endpoint, "wss://") {return ui.Usage("Pass --endpoint as a wss:// URL.")}+ if err := checkHTTPBind(f, voice.InterfaceAddresses()); err != nil {+ return err+ }token, err := joinToken(c, args, f)if err != nil {return err@@ -79,9 +82,10 @@ func newVoiceJoinCmd(app *App) *cobra.Command {fl := c.Flags()fl.StringVar(&f.Hostname, "hostname", "", "hostname for the server's endpoint and certificate")fl.StringVar(&f.Email, "email", "", "email address for Let's Encrypt notices")- fl.BoolVar(&f.NoEdge, "no-edge", false, "start no edge and publish the server on 127.0.0.1 for your own proxy")+ fl.BoolVar(&f.NoEdge, "no-edge", false, "start no edge and publish the server's HTTP port for your own proxy")fl.StringVar(&f.Endpoint, "endpoint", "", "public wss:// URL of the server behind your own proxy")- fl.IntVar(&f.HTTPPort, "http-port", 0, "port on 127.0.0.1 for your own proxy, with --no-edge")+ fl.IntVar(&f.HTTPPort, "http-port", 0, "port for your own proxy, with --no-edge")+ fl.StringVar(&f.HTTPBind, "http-bind", "", "address the port for your own proxy listens on, with --no-edge (default 127.0.0.1)")fl.BoolVar(&f.TokenStdin, "token-stdin", false, "read the join token from standard input")return c}@@ -317,7 +321,7 @@ func planJoin(ctx context.Context, app *App, client *voice.Client, e *engine.Engif plan.edgeInst != nil {network = plan.edgeInst.Project() + "_fluxer"}- httpPort := 0+ httpPort, httpBind := 0, ""if f.NoEdge {httpPort = f.HTTPPortif existing := inst.State.VoiceServer(id); httpPort == 0 && existing != nil {@@ -326,6 +330,10 @@ func planJoin(ctx context.Context, app *App, client *voice.Client, e *engine.Engif httpPort == 0 {httpPort = pickHTTPPort(takenVoicePorts(ctx, e, inst), tcp, freeOnHost)}+ httpBind = f.HTTPBind+ if existing := inst.State.VoiceServer(id); !f.HTTPBindSet && existing != nil {+ httpBind = existing.HTTPBind+ }}plan.srv = instance.VoiceServer{ID: id,@@ -335,6 +343,7 @@ func planJoin(ctx context.Context, app *App, client *voice.Client, e *engine.EngTCPPort: tcp,UDPPort: udp,HTTPPort: httpPort,+ HTTPBind: httpBind,NodeIP: publicIP,BindIP: bindAddress(f.IP, voice.InterfaceAddresses()),Image: plan.image,@@ -639,7 +648,7 @@ func showJoinSummary(app *App, plan *joinPlan, f *voiceFlags) {{"Webhook", s.Webhook},}if s.HTTPPort > 0 {- rows = append(rows, []string{"Local port", fmt.Sprintf("127.0.0.1:%d for your proxy", s.HTTPPort)})+ rows = append(rows, []string{"Local port", voice.HTTPAddress(s.HTTPBind, s.HTTPPort) + " for your proxy"})}u.Info("Voice server for %s", plan.domain)u.Info("")@@ -733,7 +742,7 @@ func finishJoin(ctx context.Context, app *App, client *voice.Client, e *engine.Eif u, perr := url.Parse(srv.Endpoint); perr == nil && u.Path != "" {path = u.Path}- check = fmt.Sprintf("Check that your proxy sends %s to 127.0.0.1:%d with WebSocket upgrades", path, saved.HTTPPort)+ check = fmt.Sprintf("Check that your proxy sends %s to %s with WebSocket upgrades", path, voice.HTTPAddress(saved.HTTPBind, saved.HTTPPort))}return ui.Wrap(err, fmt.Sprintf("%s doesn't answer yet.", srv.Endpoint), check+", then "+strings.ToLower(undo[:1])+undo[1:])}@@ -1003,7 +1012,10 @@ func routeVoiceServer(ctx context.Context, app *App, e *engine.Engine, inst *ins}return reloadCaddy(ctx, e.Named(target.Project(), target.Dir), "edge", "/etc/caddy/Caddyfile")}- app.UI.Info("Point your proxy at 127.0.0.1:%d for %s, with WebSocket upgrades and a read timeout of at least 120 seconds.", srv.HTTPPort, srv.Endpoint)+ app.UI.Info("Point your proxy at %s for %s, with WebSocket upgrades and a read timeout of at least 120 seconds.", voice.HTTPAddress(srv.HTTPBind, srv.HTTPPort), srv.Endpoint)+ if !voice.HTTPBindPrivate(srv.HTTPBind) {+ app.UI.Warn(fmt.Sprintf("%s has no TLS of its own.", voice.HTTPAddress(srv.HTTPBind, srv.HTTPPort)), "Let only your proxy's network connect to it in your firewall.")+ }return nil}diff --git a/fluxer_rexctl/cmd/voice_ops.go b/fluxer_rexctl/cmd/voice_ops.goindex 25e713236..00b4d4ebb 100644--- a/fluxer_rexctl/cmd/voice_ops.go+++ b/fluxer_rexctl/cmd/voice_ops.go@@ -315,7 +315,7 @@ func moveVoicePublicIP(ctx context.Context, app *App, inst *instance.Instance, efunc voiceProbeTarget(inst *instance.Instance, srv *instance.VoiceServer) (string, string) {switch {case srv.Edge == voice.EdgeNone:- return voice.LocalBase(srv.HTTPPort), ""+ return voice.LocalBase(srv.HTTPBind, srv.HTTPPort), ""case inst.State.IsVoice():return voice.HTTPBase(srv.Endpoint), "127.0.0.1:443"}diff --git a/fluxer_rexctl/internal/checks/voice.go b/fluxer_rexctl/internal/checks/voice.goindex f9e54147f..32bd5c965 100644--- a/fluxer_rexctl/internal/checks/voice.go+++ b/fluxer_rexctl/internal/checks/voice.go@@ -46,7 +46,7 @@ func (e *Env) voiceServers() []instance.VoiceServer {func (e *Env) voiceProbeTarget(s instance.VoiceServer) (string, string) {switch {case s.Edge == voice.EdgeNone:- return voice.LocalBase(s.HTTPPort), ""+ return voice.LocalBase(s.HTTPBind, s.HTTPPort), ""case e.Instance.State.IsVoice():return voice.HTTPBase(s.Endpoint), "127.0.0.1:443"}diff --git a/fluxer_rexctl/internal/deploy/voice.go b/fluxer_rexctl/internal/deploy/voice.goindex a1ad762db..81239249a 100644--- a/fluxer_rexctl/internal/deploy/voice.go+++ b/fluxer_rexctl/internal/deploy/voice.go@@ -56,6 +56,7 @@ func VoiceEnv(inst *instance.Instance, srv *instance.VoiceServer) voice.Env {TCPPort: srv.TCPPort,UDPPort: srv.UDPPort,HTTPPort: srv.HTTPPort,+ HTTPBind: srv.HTTPBind,NodeIP: srv.NodeIP,BindIP: srv.BindIP,Image: srv.Image,diff --git a/fluxer_rexctl/internal/instance/envvars.go b/fluxer_rexctl/internal/instance/envvars.goindex 2a708ead5..ad2ce007b 100644--- a/fluxer_rexctl/internal/instance/envvars.go+++ b/fluxer_rexctl/internal/instance/envvars.go@@ -113,7 +113,8 @@ var envVars = []envVar{{"FLUXER_VOICE_NODE_IP", "voice-server", "The public IPv4 address LiveKit gives to clients. Set FLUXER_VOICE_USE_EXTERNAL_IP to `false` with it."},{"FLUXER_VOICE_USE_EXTERNAL_IP", "voice-server", "Whether LiveKit finds its public IP address through STUN. Use `true` or `false`."},{"FLUXER_VOICE_BIND_IP", "voice-server", "The host address the media ports are published on, for a host with more than one public address. When it's empty, Docker publishes them on every address."},- {"FLUXER_VOICE_HTTP_PORT", "voice-server", "The port on 127.0.0.1 that voice-http.compose.yml publishes LiveKit's HTTP and WebSocket port on, for your own proxy."},+ {"FLUXER_VOICE_HTTP_PORT", "voice-server", "The port that voice-http.compose.yml publishes LiveKit's HTTP and WebSocket port on, for your own proxy."},+ {"FLUXER_VOICE_HTTP_BIND", "voice-server", "The host address that voice-http.compose.yml publishes FLUXER_VOICE_HTTP_PORT on. The default is `127.0.0.1`. Use another address of the host, or `0.0.0.0` for every address, when your proxy runs in another network. Write an IPv6 address in brackets, such as `[2001:db8::5]`. The port has no TLS of its own. Let only your proxy's network connect to it in your firewall."},{"FLUXER_VOICE_NETWORK", "voice-server", "The Docker network the server joins. On a voice machine it's the network of the voice edge. On the Fluxer machine it's the network of the instance, such as `fluxer_fluxer`, and Fluxer's own edge routes to the server."},{"FLUXER_VOICE_MEMORY_LIMIT", "voice-server", "The memory limit for the server's container."},{"FLUXER_VOICE_GOMEMLIMIT", "voice-server", "The soft memory limit of the Go runtime in LiveKit. Keep it at about 85% of FLUXER_VOICE_MEMORY_LIMIT."},diff --git a/fluxer_rexctl/internal/instance/shipped.json b/fluxer_rexctl/internal/instance/shipped.jsonindex e71424173..736d80f8b 100644--- a/fluxer_rexctl/internal/instance/shipped.json+++ b/fluxer_rexctl/internal/instance/shipped.json@@ -264,6 +264,9 @@],"voice/Caddyfile.voice": ["193a85faef0174276028f154858bfd98aaaebc2e2ba2d09c1154ff24156d3060"],"voice/voice-edge.compose.yml": ["8c42013b22dc829072af16e1db89a902358cf2c07e1a44bdd6297421ffba397c"],- "voice/voice-http.compose.yml": ["f7a207a03625b080768b57f89a44b5828384d0efb528f92f30aa18034e4cf40d"],+ "voice/voice-http.compose.yml": [+ "80433a0f965a050fcc88dad63d6887cca9dbc06b1d2344c4e8a898049ad55d6b",+ "f7a207a03625b080768b57f89a44b5828384d0efb528f92f30aa18034e4cf40d"+ ],"voice/voice.compose.yml": ["b0ea7cf174eea1b5b882f21f7ab08f6526ecac6e96cb98ab990b3a0e8e6f26d9"]}diff --git a/fluxer_rexctl/internal/instance/state.go b/fluxer_rexctl/internal/instance/state.goindex 7bb5c49b2..9ac9abdd7 100644--- a/fluxer_rexctl/internal/instance/state.go+++ b/fluxer_rexctl/internal/instance/state.go@@ -69,6 +69,7 @@ type VoiceServer struct {TCPPort int `json:"tcp_port"`UDPPort int `json:"udp_port"`HTTPPort int `json:"http_port,omitempty"`+ HTTPBind string `json:"http_bind,omitempty"`NodeIP string `json:"node_ip,omitempty"`IPPinned bool `json:"pinned_node_ip,omitempty"`BindIP string `json:"bind_ip,omitempty"`diff --git a/fluxer_rexctl/internal/stack/files/voice/voice-http.compose.yml b/fluxer_rexctl/internal/stack/files/voice/voice-http.compose.ymlindex fc849516c..857fe5856 100644--- a/fluxer_rexctl/internal/stack/files/voice/voice-http.compose.yml+++ b/fluxer_rexctl/internal/stack/files/voice/voice-http.compose.yml@@ -1,4 +1,4 @@services:livekit:ports:- - "127.0.0.1:${FLUXER_VOICE_HTTP_PORT:-7880}:7880/tcp"+ - "${FLUXER_VOICE_HTTP_BIND:-127.0.0.1}:${FLUXER_VOICE_HTTP_PORT:-7880}:7880/tcp"diff --git a/fluxer_rexctl/internal/voice/net.go b/fluxer_rexctl/internal/voice/net.goindex 91251040d..75cd733d3 100644--- a/fluxer_rexctl/internal/voice/net.go+++ b/fluxer_rexctl/internal/voice/net.go@@ -17,6 +17,7 @@ import ("net/url""os""slices"+ "strconv""strings""time"@@ -207,8 +208,66 @@ func HTTPBase(endpoint string) string {return strings.TrimRight(u.String(), "/")}-func LocalBase(httpPort int) string {- return fmt.Sprintf("http://127.0.0.1:%d", httpPort)+const DefaultHTTPBind = "127.0.0.1"++func LocalBase(bind string, httpPort int) string {+ return "http://" + net.JoinHostPort(dialHost(bind), strconv.Itoa(httpPort))+}++func HTTPAddress(bind string, httpPort int) string {+ if bind == "" {+ bind = DefaultHTTPBind+ }+ return net.JoinHostPort(bind, strconv.Itoa(httpPort))+}++func HTTPBindValue(bind string) string {+ if bind == "" {+ return DefaultHTTPBind+ }+ if a, err := netip.ParseAddr(bind); err == nil && a.Is6() {+ return "[" + a.String() + "]"+ }+ return bind+}++func NormalHTTPBind(bind string) (string, bool) {+ a, err := netip.ParseAddr(strings.TrimSpace(bind))+ if err != nil || a.Zone() != "" {+ return "", false+ }+ a = a.Unmap()+ if a.String() == DefaultHTTPBind {+ return "", true+ }+ return a.String(), true+}++func HTTPBindPrivate(bind string) bool {+ if bind == "" {+ return true+ }+ a, err := netip.ParseAddr(bind)+ return err == nil && a.IsLoopback()+}++func HTTPBindOnMachine(bind string, local []string) bool {+ a, err := netip.ParseAddr(bind)+ if err != nil {+ return false+ }+ if a.IsUnspecified() || a.IsLoopback() {+ return true+ }+ return slices.Contains(local, a.String())+}++func dialHost(bind string) string {+ a, err := netip.ParseAddr(bind)+ if err != nil || a.IsUnspecified() {+ return DefaultHTTPBind+ }+ return a.String()}func InstanceEdge(endpoint, mode, httpsPort, bind string) (string, string) {diff --git a/fluxer_rexctl/internal/voice/voice.go b/fluxer_rexctl/internal/voice/voice.goindex 3b319d53b..458882b10 100644--- a/fluxer_rexctl/internal/voice/voice.go+++ b/fluxer_rexctl/internal/voice/voice.go@@ -96,6 +96,7 @@ type Env struct {TCPPort intUDPPort intHTTPPort int+ HTTPBind stringNodeIP stringBindIP stringImage string@@ -129,6 +130,7 @@ func (e Env) Values() map[string]string {}if e.HTTPPort > 0 {out["FLUXER_VOICE_HTTP_PORT"] = strconv.Itoa(e.HTTPPort)+ out["FLUXER_VOICE_HTTP_BIND"] = HTTPBindValue(e.HTTPBind)}for k, v := range map[string]string{"FLUXER_LIVEKIT_STUN_PRIMARY": e.StunPrimary,