A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(rexctl): add FLUXER_LIVEKIT_TURN_SERVERS setting (#3695)
Hampus867898103651
diff --git a/fluxer_docs/src/content/docs/operator/environment.mdx b/fluxer_docs/src/content/docs/operator/environment.mdxindex a83688813..65b01d768 100644--- a/fluxer_docs/src/content/docs/operator/environment.mdx+++ b/fluxer_docs/src/content/docs/operator/environment.mdx@@ -295,6 +295,10 @@ The first STUN host LiveKit asks to find its public IP address, as `host:port`.The second STUN host LiveKit asks to find its public IP address, as `host:port`. The default is `stun1.l.google.com:19302`. With rexctl, use `rexctl config set FLUXER_LIVEKIT_STUN_SECONDARY`.+### `FLUXER_LIVEKIT_TURN_SERVERS`++TURN relays LiveKit offers to browsers and apps, as a JSON array of objects with `host`, `port`, `protocol` and optional `username` and `credential`, such as `[{"host":"turn.example.com","port":3478,"protocol":"udp","username":"fluxer","credential":"secret"}]`. Use `udp`, `tcp` or `tls` for `protocol`. Set it when browsers and apps can't connect to the LiveKit ports directly, such as behind Cloudflare Tunnel. When it's empty, LiveKit offers no TURN relay. The default is `[]`. With rexctl, use `rexctl config set FLUXER_LIVEKIT_TURN_SERVERS`.+## Object storageBy default Fluxer keeps files in the bundled object store. To use your own S3-compatible store, set its address and buckets, add `external-object-store.compose.yml` to `COMPOSE_FILE` and put its keys in `FLUXER_S3_ACCESS_KEY` and `FLUXER_S3_SECRET_KEY`.diff --git a/fluxer_docs/src/content/docs/operator/kubernetes.mdx b/fluxer_docs/src/content/docs/operator/kubernetes.mdxindex c3578b127..ba91122ac 100644--- a/fluxer_docs/src/content/docs/operator/kubernetes.mdx+++ b/fluxer_docs/src/content/docs/operator/kubernetes.mdx@@ -69,6 +69,8 @@ The chart's `values.yaml` explains every value. These are the ones most installs| `env` | Fluxer settings as `NAME: value`, listed on [Environment variables](/operator/environment/) || `secrets.extra` | Settings that have a password or a key |+To relay calls through a TURN server when people can't connect to LiveKit directly, add it to `livekit.extraConfig`, as [Voice behind a tunnel](/operator/reverse-proxy/#voice-behind-a-tunnel) shows.+Under Argo CD or `helm template`, set `secrets.generate: false` and give every key in `secrets.values`, or put a Secret you manage in `secrets.existingSecret`.## External servicesdiff --git a/fluxer_docs/src/content/docs/operator/reverse-proxy.mdx b/fluxer_docs/src/content/docs/operator/reverse-proxy.mdxindex 3e030db43..b8cf64cca 100644--- a/fluxer_docs/src/content/docs/operator/reverse-proxy.mdx+++ b/fluxer_docs/src/content/docs/operator/reverse-proxy.mdx@@ -133,4 +133,32 @@ Fluxer sends at most 100 prefixes per request, which is the most Cloudflare acce## Voice and video-Call audio and video don't go through your proxy. They connect directly to the machine on TCP port 7881 and UDP port 7882, and both ports need to be open. A tunnel such as Cloudflare Tunnel can't forward them, and an instance you only serve through a tunnel has no voice or video calls.+Call audio and video don't go through your proxy. They connect directly to the machine on TCP port 7881 and UDP port 7882, and both ports need to be open. A tunnel such as Cloudflare Tunnel can't forward them.++### Voice behind a tunnel++If you only serve Fluxer through a tunnel, calls need a TURN server. It relays call media between people's devices and LiveKit. Run one on a machine with a public address, such as coturn on a small VPS, and open its port there, usually UDP port 3478.++Then give LiveKit the relay as a JSON array in `FLUXER_LIVEKIT_TURN_SERVERS`.++```sh+rexctl config set FLUXER_LIVEKIT_TURN_SERVERS '[{"host":"turn.example.com","port":3478,"protocol":"udp","username":"fluxer","credential":"a-long-password"}]'+```++Without rexctl, put the same value in `.env` between single quotes and run `docker compose up -d`.++LiveKit hands the relay to browsers and apps when they join a call, and they use it when they can't connect to LiveKit directly. Keep `FLUXER_LIVEKIT_USE_EXTERNAL_IP` at `true` so LiveKit tells them its public address. LiveKit opens the path to the relay from its own side, which most routers let through without open ports. If calls still don't connect, your router probably blocks this and you need to open TCP port 7881 and UDP port 7882 after all.++On Kubernetes, add the relay to `livekit.extraConfig` in your values file.++```yaml+livekit:+ extraConfig:+ rtc:+ turn_servers:+ - host: turn.example.com+ port: 3478+ protocol: udp+ username: fluxer+ credential: a-long-password+```diff --git a/fluxer_rexctl/internal/instance/envvars.go b/fluxer_rexctl/internal/instance/envvars.goindex 3c479ca25..dbe228b9b 100644--- a/fluxer_rexctl/internal/instance/envvars.go+++ b/fluxer_rexctl/internal/instance/envvars.go@@ -100,6 +100,7 @@ var envVars = []envVar{{"FLUXER_LIVEKIT_USE_EXTERNAL_IP", "voice", "Whether LiveKit finds its public IP address through STUN. Use `true` or `false`. Set it to `false` when you set FLUXER_LIVEKIT_NODE_IP."},{"FLUXER_LIVEKIT_STUN_PRIMARY", "voice", "The first STUN host LiveKit asks to find its public IP address, as `host:port`. Point it at another STUN host to avoid contacting Google."},{"FLUXER_LIVEKIT_STUN_SECONDARY", "voice", "The second STUN host LiveKit asks to find its public IP address, as `host:port`."},+ {"FLUXER_LIVEKIT_TURN_SERVERS", "voice", "TURN relays LiveKit offers to browsers and apps, as a JSON array of objects with `host`, `port`, `protocol` and optional `username` and `credential`, such as `[{\"host\":\"turn.example.com\",\"port\":3478,\"protocol\":\"udp\",\"username\":\"fluxer\",\"credential\":\"secret\"}]`. Use `udp`, `tcp` or `tls` for `protocol`. Set it when browsers and apps can't connect to the LiveKit ports directly, such as behind Cloudflare Tunnel. When it's empty, LiveKit offers no TURN relay."},{"FLUXER_S3_ENDPOINT", "storage", ""},{"FLUXER_S3_PUBLIC_ENDPOINT", "storage", "When it's empty, the api and worker use FLUXER_S3_ENDPOINT, and the web app doesn't let browsers upload to the object store."},{"FLUXER_S3_REGION", "storage", ""},diff --git a/fluxer_rexctl/internal/instance/shipped.json b/fluxer_rexctl/internal/instance/shipped.jsonindex 455b7d7da..7a8cce007 100644--- a/fluxer_rexctl/internal/instance/shipped.json+++ b/fluxer_rexctl/internal/instance/shipped.json@@ -1,6 +1,7 @@{".env.example": ["013e26017b51e7310d7e044090b50d0b4ae33cd1d91f0ac380a0a6783d9edab1",+ "039e161673ae76f5b8cc5562a7e6acebc1a211ffaf65d488a993f16ca481fb94","0b718956b039a8d8402bd9c6102322fb273159f2834509fdf23a95459c7537b9","0e54ce3bfec7340d76214240c4bb9dc8dd9a33dc9ce57eff70d8d15f909cf7ab","1263f66b22074f380e55198a32d759ae5196bb9b44d8a62473c6f41dfb770905",@@ -239,6 +240,7 @@"f9b7f2fb56c4b6783543e4640841108919d89449da95c47b35198411e6725928","fa4fb2ea2d033ee34fd5203f7acda3bb13f3b65c0f6f8e2003ccdd16e4eed14e","fb336f82038c6054faf90915cf09a7622e67e34be48365f9676b372536e2996f",+ "fc210b0a01624a150751ebd01d00ab4afb42f78cc3d51175067399aae8cc89ef","fcf293ff891bdb0c8dac232bdf2287e66554d691de4db614a1b3d50c13ce641e","fda863ed6260000ebf53aa4e6fe28348a092155fe8d598d7ead1eb2e36c33c03","fdca5099f7d1012ff62b3353d6d79b1044471bd08186ae3ab45ef52042a53857"diff --git a/fluxer_rexctl/internal/stack/files/.env.example b/fluxer_rexctl/internal/stack/files/.env.exampleindex 493969548..cc40a8666 100644--- a/fluxer_rexctl/internal/stack/files/.env.example+++ b/fluxer_rexctl/internal/stack/files/.env.example@@ -475,6 +475,16 @@ FLUXER_VAPID_PRIVATE_KEY=# rexctl: rexctl config set FLUXER_LIVEKIT_STUN_SECONDARY# FLUXER_LIVEKIT_STUN_SECONDARY=+# TURN relays LiveKit offers to browsers and apps, as a JSON array of objects+# with host, port, protocol and optional username and credential, such as+# [{"host":"turn.example.com","port":3478,"protocol":"udp","username":"fluxer","credential":"secret"}].+# Use udp, tcp or tls for protocol. Set it when browsers and apps can't+# connect to the LiveKit ports directly, such as behind Cloudflare Tunnel.+# When it's empty, LiveKit offers no TURN relay.+# default: []+# rexctl: rexctl config set FLUXER_LIVEKIT_TURN_SERVERS+# FLUXER_LIVEKIT_TURN_SERVERS=+# ============================================================================# OBJECT STORAGEdiff --git a/fluxer_rexctl/internal/stack/files/docker-compose.yml b/fluxer_rexctl/internal/stack/files/docker-compose.ymlindex 2470d7828..92aa74c03 100644--- a/fluxer_rexctl/internal/stack/files/docker-compose.yml+++ b/fluxer_rexctl/internal/stack/files/docker-compose.yml@@ -502,6 +502,7 @@ services:stun_servers:- ${FLUXER_LIVEKIT_STUN_PRIMARY:-stun.l.google.com:19302}- ${FLUXER_LIVEKIT_STUN_SECONDARY:-stun1.l.google.com:19302}+ turn_servers: ${FLUXER_LIVEKIT_TURN_SERVERS:-[]}webhook:api_key: ${LIVEKIT_API_KEY:-}urls:diff --git a/fluxer_rexctl/internal/stack/livekit_test.go b/fluxer_rexctl/internal/stack/livekit_test.gonew file mode 100644index 000000000..b94e84b2b--- /dev/null+++ b/fluxer_rexctl/internal/stack/livekit_test.go@@ -0,0 +1,78 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package stack++import (+ "testing"++ "github.com/compose-spec/compose-go/v2/template"+ "go.yaml.in/yaml/v4"+)++type livekitTURNServer struct {+ Host string `yaml:"host"`+ Port int `yaml:"port"`+ Protocol string `yaml:"protocol"`+ Username string `yaml:"username"`+ Credential string `yaml:"credential"`+}++type livekitConfig struct {+ RTC struct {+ UseExternalIP bool `yaml:"use_external_ip"`+ STUNServers []string `yaml:"stun_servers"`+ TURNServers []livekitTURNServer `yaml:"turn_servers"`+ } `yaml:"rtc"`+}++func renderLivekitConfig(t *testing.T, env map[string]string) livekitConfig {+ t.Helper()+ data, err := ReadFile(Embedded(), "docker-compose.yml")+ if err != nil {+ t.Fatal(err)+ }+ var compose struct {+ Services map[string]struct {+ Environment map[string]string `yaml:"environment"`+ } `yaml:"services"`+ }+ if err := yaml.Unmarshal(data, &compose); err != nil {+ t.Fatal(err)+ }+ raw, ok := compose.Services["livekit"].Environment["LIVEKIT_CONFIG"]+ if !ok {+ t.Fatal("livekit service has no LIVEKIT_CONFIG")+ }+ rendered, err := template.Substitute(raw, func(k string) (string, bool) {+ v, ok := env[k]+ return v, ok+ })+ if err != nil {+ t.Fatal(err)+ }+ var cfg livekitConfig+ if err := yaml.Unmarshal([]byte(rendered), &cfg); err != nil {+ t.Fatalf("LIVEKIT_CONFIG is not valid YAML: %v\n%s", err, rendered)+ }+ return cfg+}++func TestLivekitConfigTURNServersFromEnv(t *testing.T) {+ cfg := renderLivekitConfig(t, map[string]string{+ "FLUXER_LIVEKIT_TURN_SERVERS": `[{"host":"turn.example.com","port":3478,"protocol":"udp","username":"u","credential":"p"}]`,+ })+ want := []livekitTURNServer{{Host: "turn.example.com", Port: 3478, Protocol: "udp", Username: "u", Credential: "p"}}+ if len(cfg.RTC.TURNServers) != 1 || cfg.RTC.TURNServers[0] != want[0] {+ t.Errorf("rtc.turn_servers = %+v, want %+v", cfg.RTC.TURNServers, want)+ }+ if !cfg.RTC.UseExternalIP || len(cfg.RTC.STUNServers) != 2 {+ t.Errorf("rtc = %+v, want use_external_ip and two STUN servers kept", cfg.RTC)+ }+}++func TestLivekitConfigNoTURNServersByDefault(t *testing.T) {+ cfg := renderLivekitConfig(t, nil)+ if len(cfg.RTC.TURNServers) != 0 {+ t.Errorf("rtc.turn_servers = %+v, want none", cfg.RTC.TURNServers)+ }+}