A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
feat(rexctl): run and connect standalone voice servers (#3714)
Hampusc24cbaed74eb
diff --git a/.github/workflows/helm.yaml b/.github/workflows/helm.yamlindex 4c20d9e69..f6a9a9555 100644--- a/.github/workflows/helm.yaml+++ b/.github/workflows/helm.yaml@@ -147,6 +147,30 @@ jobs:expect_failure "Set livekit.nodeCIDRs" --set $d --set networkPolicy.ingressNamespaceSelector.matchLabels.name=ingressexpect_failure "set livekit.external.publicUrl" --set $d --set livekit.enabled=false --set livekit.external.url=http://livekit.voice.svc:7880expect_failure "reserve a public address" --set $d --set livekit.expose=loadBalancer+ expect_failure "livekit.joined keeps voice on" --set $d --set livekit.joined=true++ - name: Check that joined voice keeps voice on without LiveKit+ run: |+ set -euo pipefail+ helm template fluxer "$CHART" -n fluxer -f "$CHART/ci/joined-voice-values.yaml" > rendered.yaml+ python3 - rendered.yaml <<'PY'+ import sys, yaml+ docs = [d for d in yaml.safe_load_all(open(sys.argv[1])) if d]+ workloads = [d['metadata']['name'] for d in docs if d.get('kind') in ('Deployment', 'StatefulSet', 'DaemonSet')]+ if any('livekit' in name for name in workloads):+ sys.exit('joined voice renders a LiveKit workload: ' + ', '.join(workloads))+ config = {}+ for d in docs:+ if d.get('kind') == 'ConfigMap':+ config.update(d.get('data') or {})+ want = {'FLUXER_LIVEKIT_ENABLED': 'true', 'FLUXER_LIVEKIT_BUNDLED': 'false'}+ for key, value in want.items():+ if config.get(key) != value:+ sys.exit(f'{key} is {config.get(key)!r}, want {value!r}')+ if 'FLUXER_LIVEKIT_INTERNAL_URL' in config or 'FLUXER_LIVEKIT_DEFAULT_REGION' not in config:+ sys.exit('joined voice sets the internal URL or leaves out the default region')+ print('joined voice keeps voice on with no LiveKit workload')+ PY- name: Check that a chart version bump restarts nothingrun: |diff --git a/deploy/helm/fluxer/ci/joined-voice-values.yaml b/deploy/helm/fluxer/ci/joined-voice-values.yamlnew file mode 100644index 000000000..0d73a71c2--- /dev/null+++ b/deploy/helm/fluxer/ci/joined-voice-values.yaml@@ -0,0 +1,4 @@+domain: chat.example.test+livekit:+ enabled: false+ joined: truediff --git a/deploy/helm/fluxer/templates/_config.tpl b/deploy/helm/fluxer/templates/_config.tplindex c98be12a6..b86492d02 100644--- a/deploy/helm/fluxer/templates/_config.tpl+++ b/deploy/helm/fluxer/templates/_config.tpl@@ -69,6 +69,9 @@{{- $_ := set $c "FLUXER_LIVEKIT_INTERNAL_URL" $v.livekit.external.url }}{{- $_ := set $c "FLUXER_LIVEKIT_DEFAULT_REGION" $v.livekit.defaultRegion }}{{- $_ := set $c "FLUXER_LIVEKIT_URL" $v.livekit.external.publicUrl }}+{{- else if eq $livekit "joined" }}+{{- $_ := set $c "FLUXER_LIVEKIT_BUNDLED" "false" }}+{{- $_ := set $c "FLUXER_LIVEKIT_DEFAULT_REGION" $v.livekit.defaultRegion }}{{- end }}{{- $_ := set $c "FLUXER_API_PRESIGNED_HARVEST_DOWNLOADS_ENABLED" "false" }}{{- $_ := set $c "FLUXER_EMAIL_FROM_EMAIL" "noreply@localhost" }}diff --git a/deploy/helm/fluxer/templates/_helpers.tpl b/deploy/helm/fluxer/templates/_helpers.tplindex 7acfc6635..a778e221f 100644--- a/deploy/helm/fluxer/templates/_helpers.tpl+++ b/deploy/helm/fluxer/templates/_helpers.tpl@@ -156,7 +156,7 @@ public: {{ substr 56 121 $der | b64enc | replace "+" "-" | replace "/" "_" | tri{{- end }}{{- define "fluxer.livekitMode" -}}-{{- if .Values.livekit.enabled }}bundled{{ else if .Values.livekit.external.url }}external{{ else }}off{{ end }}+{{- if .Values.livekit.enabled }}bundled{{ else if .Values.livekit.external.url }}external{{ else if .Values.livekit.joined }}joined{{ else }}off{{ end }}{{- end }}{{- define "fluxer.noGenerateKeys" -}}@@ -182,7 +182,7 @@ public: {{ substr 56 121 $der | b64enc | replace "+" "-" | replace "/" "_" | tri{{- $extra := .Values.secrets.extra | default dict }}{{- range $k, $_ := .Values.secrets.values }}{{- if and (eq $k "FLUXER_POSTGRES_PASSWORD") (get $extra "FLUXER_POSTGRES_URL") }}-{{- else if and (hasPrefix "FLUXER_LIVEKIT_" $k) (eq (include "fluxer.livekitMode" $) "off") }}+{{- else if and (hasPrefix "FLUXER_LIVEKIT_" $k) (has (include "fluxer.livekitMode" $) (list "off" "joined")) }}{{- else if and (eq $k "FLUXER_VALKEY_PASSWORD") (not $.Values.valkey.enabled) }}{{- else if and (eq $k "FLUXER_NATS_AUTH_TOKEN") (not $.Values.nats.enabled) }}{{- else if and (has $k (list "FLUXER_SETUP_TOKEN" "FLUXER_ACTIVITYPUB_SIGNING_KEY_BASE64")) (or $.Values.secrets.existingSecret (and (not $.Values.secrets.generate) (not (include "fluxer.string" (get $.Values.secrets.values $k | default ""))))) }}diff --git a/deploy/helm/fluxer/templates/_validate.tpl b/deploy/helm/fluxer/templates/_validate.tplindex 9c4728978..bc8509bf7 100644--- a/deploy/helm/fluxer/templates/_validate.tpl+++ b/deploy/helm/fluxer/templates/_validate.tpl@@ -41,6 +41,9 @@{{- if not (has $v.livekit.expose (list "hostNetwork" "loadBalancer" "nodePort")) }}{{- fail "livekit.expose must be hostNetwork, loadBalancer or nodePort" }}{{- end }}+{{- if and $v.livekit.joined (or $v.livekit.enabled $v.livekit.external.url) }}+{{- fail "livekit.joined keeps voice on with only the servers you add, and it needs livekit.enabled false and no livekit.external.url" }}+{{- end }}{{- if and $v.livekit.enabled $v.livekit.external.url }}{{- fail "livekit.external.url names a LiveKit server of your own, so set livekit.enabled to false" }}{{- end }}diff --git a/deploy/helm/fluxer/values.schema.json b/deploy/helm/fluxer/values.schema.jsonindex d9cd3c5be..041e32cf7 100644--- a/deploy/helm/fluxer/values.schema.json+++ b/deploy/helm/fluxer/values.schema.json@@ -633,6 +633,9 @@"enabled": {"type": "boolean"},+ "joined": {+ "type": "boolean"+ },"image": {"$ref": "#/definitions/thirdPartyImage"},diff --git a/deploy/helm/fluxer/values.yaml b/deploy/helm/fluxer/values.yamlindex 9029deea2..c1fa791b3 100644--- a/deploy/helm/fluxer/values.yaml+++ b/deploy/helm/fluxer/values.yaml@@ -358,8 +358,9 @@ s3:harvests: fluxer-harvestslivekit:- # Run LiveKit for voice and video. false with no external.url turns voice off.+ # Run LiveKit for voice and video. false turns voice off unless external.url or joined is set.enabled: true+ joined: false# A LiveKit server of your own, used when enabled is false. Its key and# secret go in secrets.values.FLUXER_LIVEKIT_API_KEY and FLUXER_LIVEKIT_API_SECRET.# Point its webhook at <origin>/api/webhooks/livekit, signed with the same key.diff --git a/fluxer_admin/openapi-admin.json b/fluxer_admin/openapi-admin.jsonindex 3e4548239..f73d42b2d 100644--- a/fluxer_admin/openapi-admin.json+++ b/fluxer_admin/openapi-admin.json@@ -12099,6 +12099,179 @@]}},+ "/admin/voice/join-tokens": {+ "post": {+ "operationId": "create_admin_voice_join_token",+ "summary": "Create a voice join token",+ "tags": ["Admin"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/CreateVoiceJoinTokenResponse"}}}+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "401": {+ "description": "Unauthorized - Authentication is required or the token is invalid",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "403": {+ "description": "Forbidden - You do not have permission to perform this action",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Creates a single use token that lets one machine register one voice server. The token appears in this response only. Creates audit log entry. Requires VOICE_JOIN_TOKEN_CREATE permission.",+ "security": [{"adminApiKey": []}],+ "requestBody": {+ "required": false,+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/CreateVoiceJoinTokenRequest"}}}+ }+ },+ "get": {+ "operationId": "list_admin_voice_join_tokens",+ "summary": "List voice join tokens",+ "tags": ["Admin"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ListVoiceJoinTokensResponse"}}}+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "401": {+ "description": "Unauthorized - Authentication is required or the token is invalid",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "403": {+ "description": "Forbidden - You do not have permission to perform this action",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Lists voice join tokens from the last 7 days with their status, without the token values. Requires VOICE_JOIN_TOKEN_LIST permission.",+ "security": [{"adminApiKey": []}]+ }+ },+ "/admin/voice/join-tokens/{token_id}": {+ "delete": {+ "operationId": "revoke_admin_voice_join_token",+ "summary": "Revoke a voice join token",+ "tags": ["Admin"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/DeleteVoiceResponse"}}}+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "401": {+ "description": "Unauthorized - Authentication is required or the token is invalid",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "403": {+ "description": "Forbidden - You do not have permission to perform this action",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Stops a pending voice join token from working. Creates audit log entry. Requires VOICE_JOIN_TOKEN_REVOKE permission.",+ "security": [{"adminApiKey": []}],+ "parameters": [+ {+ "name": "token_id",+ "in": "path",+ "required": true,+ "schema": {+ "description": "ID of the join token",+ "allOf": [{"$ref": "#/components/schemas/SnowflakeType"}]+ },+ "description": "ID of the join token"+ }+ ]+ }+ },"/admin/voice/regions": {"get": {"operationId": "list_admin_voice_regions",@@ -12749,6 +12922,77 @@}]}+ },+ "/admin/voice/regions/{region_id}/servers/{server_id}/test-token": {+ "post": {+ "operationId": "create_admin_voice_server_test_token",+ "summary": "Create a voice server test token",+ "tags": ["Admin"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {+ "application/json": {"schema": {"$ref": "#/components/schemas/CreateVoiceServerTestTokenResponse"}}+ }+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "401": {+ "description": "Unauthorized - Authentication is required or the token is invalid",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "403": {+ "description": "Forbidden - You do not have permission to perform this action",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Signs a 60 second LiveKit token for a hidden, audio only participant in a throwaway room on one server, for a connection test from outside its network. Fluxer ignores webhooks for these rooms. Creates audit log entry. Requires VOICE_SERVER_UPDATE permission.",+ "security": [{"adminApiKey": []}],+ "parameters": [+ {+ "name": "region_id",+ "in": "path",+ "required": true,+ "schema": {"description": "ID of the region the server belongs to", "type": "string"},+ "description": "ID of the region the server belongs to"+ },+ {+ "name": "server_id",+ "in": "path",+ "required": true,+ "schema": {"description": "ID of the voice server", "type": "string"},+ "description": "ID of the voice server"+ }+ ]+ }}},"components": {@@ -12807,6 +13051,19 @@"required": ["code", "message"],"additionalProperties": {}},+ "CreateVoiceServerTestTokenResponse": {+ "type": "object",+ "properties": {+ "token": {+ "type": "string",+ "description": "LiveKit token for a hidden audio only participant, valid for 60 seconds"+ },+ "endpoint": {"type": "string", "description": "Endpoint of the server"},+ "room": {"type": "string", "description": "Name of the throwaway test room"}+ },+ "required": ["token", "endpoint", "room"],+ "additionalProperties": false+ },"DeleteVoiceResponse": {"type": "object","properties": {"success": {"type": "boolean", "description": "Whether the deletion was successful"}},@@ -12865,6 +13122,14 @@"maxItems": 1000,"type": "array","items": {"$ref": "#/components/schemas/SnowflakeType"}+ },+ "internal_endpoint": {+ "nullable": true,+ "allOf": [{"$ref": "#/components/schemas/VoiceInternalEndpointType"}]+ },+ "draining": {+ "description": "Whether placement sends new calls elsewhere while calls on this server finish",+ "type": "boolean"}}},@@ -12872,6 +13137,81 @@"type": "object","properties": {"server": {+ "properties": {+ "region_id": {"type": "string", "description": "ID of the region this server belongs to"},+ "server_id": {"type": "string", "description": "Unique identifier for the voice server"},+ "endpoint": {+ "type": "string",+ "format": "uri",+ "description": "Client signal WebSocket endpoint URL for the voice server"+ },+ "latitude": {+ "nullable": true,+ "description": "Optional geographic latitude override for this server",+ "type": "number"+ },+ "longitude": {+ "nullable": true,+ "description": "Optional geographic longitude override for this server",+ "type": "number"+ },+ "is_active": {"type": "boolean", "description": "Whether the server is currently active"},+ "soft_connection_limit": {+ "nullable": true,+ "description": "Connection count above which placement prefers another server, or null when the server has no limit",+ "type": "integer",+ "minimum": 1,+ "maximum": 2147483647+ },+ "notes": {+ "nullable": true,+ "description": "Free-form admin notes about this server, or null when there are none",+ "type": "string"+ },+ "vip_only": {"type": "boolean", "description": "Whether this server is restricted to VIP users"},+ "required_guild_features": {+ "maxItems": 100,+ "type": "array",+ "items": {"type": "string"},+ "description": "Guild features required to use this server"+ },+ "allowed_guild_ids": {+ "maxItems": 1000,+ "type": "array",+ "items": {"$ref": "#/components/schemas/SnowflakeStringType"},+ "description": "Guild IDs explicitly allowed to use this server"+ },+ "allowed_user_ids": {+ "maxItems": 1000,+ "type": "array",+ "items": {"$ref": "#/components/schemas/SnowflakeStringType"},+ "description": "User IDs explicitly allowed to use this server"+ },+ "internal_endpoint": {+ "nullable": true,+ "description": "URL the API uses for the control API and health checks, or null to use the endpoint",+ "type": "string"+ },+ "draining": {+ "type": "boolean",+ "description": "Whether placement sends new calls elsewhere while calls on this server finish"+ },+ "health": {+ "description": "Health of the server as this API node last saw it",+ "allOf": [{"$ref": "#/components/schemas/VoiceServerHealthResponse"}]+ },+ "duplicate_api_key": {"type": "boolean", "description": "Whether another server uses the same API key"},+ "created_at": {+ "nullable": true,+ "description": "ISO 8601 timestamp when the server was created",+ "type": "string"+ },+ "updated_at": {+ "nullable": true,+ "description": "ISO 8601 timestamp when the server was last updated",+ "type": "string"+ }+ },"description": "Updated voice server","allOf": [{"$ref": "#/components/schemas/VoiceServerAdminResponse"}]}@@ -12950,6 +13290,16 @@"maxItems": 1000,"type": "array","items": {"$ref": "#/components/schemas/SnowflakeType"}+ },+ "internal_endpoint": {+ "default": null,+ "nullable": true,+ "allOf": [{"$ref": "#/components/schemas/VoiceInternalEndpointType"}]+ },+ "draining": {+ "default": false,+ "description": "Whether placement sends new calls elsewhere while calls on this server finish",+ "type": "boolean"}},"required": ["server_id", "endpoint", "api_key", "api_secret"]@@ -12958,6 +13308,81 @@"type": "object","properties": {"server": {+ "properties": {+ "region_id": {"type": "string", "description": "ID of the region this server belongs to"},+ "server_id": {"type": "string", "description": "Unique identifier for the voice server"},+ "endpoint": {+ "type": "string",+ "format": "uri",+ "description": "Client signal WebSocket endpoint URL for the voice server"+ },+ "latitude": {+ "nullable": true,+ "description": "Optional geographic latitude override for this server",+ "type": "number"+ },+ "longitude": {+ "nullable": true,+ "description": "Optional geographic longitude override for this server",+ "type": "number"+ },+ "is_active": {"type": "boolean", "description": "Whether the server is currently active"},+ "soft_connection_limit": {+ "nullable": true,+ "description": "Connection count above which placement prefers another server, or null when the server has no limit",+ "type": "integer",+ "minimum": 1,+ "maximum": 2147483647+ },+ "notes": {+ "nullable": true,+ "description": "Free-form admin notes about this server, or null when there are none",+ "type": "string"+ },+ "vip_only": {"type": "boolean", "description": "Whether this server is restricted to VIP users"},+ "required_guild_features": {+ "maxItems": 100,+ "type": "array",+ "items": {"type": "string"},+ "description": "Guild features required to use this server"+ },+ "allowed_guild_ids": {+ "maxItems": 1000,+ "type": "array",+ "items": {"$ref": "#/components/schemas/SnowflakeStringType"},+ "description": "Guild IDs explicitly allowed to use this server"+ },+ "allowed_user_ids": {+ "maxItems": 1000,+ "type": "array",+ "items": {"$ref": "#/components/schemas/SnowflakeStringType"},+ "description": "User IDs explicitly allowed to use this server"+ },+ "internal_endpoint": {+ "nullable": true,+ "description": "URL the API uses for the control API and health checks, or null to use the endpoint",+ "type": "string"+ },+ "draining": {+ "type": "boolean",+ "description": "Whether placement sends new calls elsewhere while calls on this server finish"+ },+ "health": {+ "description": "Health of the server as this API node last saw it",+ "allOf": [{"$ref": "#/components/schemas/VoiceServerHealthResponse"}]+ },+ "duplicate_api_key": {"type": "boolean", "description": "Whether another server uses the same API key"},+ "created_at": {+ "nullable": true,+ "description": "ISO 8601 timestamp when the server was created",+ "type": "string"+ },+ "updated_at": {+ "nullable": true,+ "description": "ISO 8601 timestamp when the server was last updated",+ "type": "string"+ }+ },"description": "Created voice server","allOf": [{"$ref": "#/components/schemas/VoiceServerAdminResponse"}]}@@ -13208,6 +13633,123 @@"anyOf": [{"type": "string"}, {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991}],"format": "snowflake"},+ "ListVoiceJoinTokensResponse": {+ "type": "object",+ "properties": {+ "tokens": {+ "type": "array",+ "items": {"$ref": "#/components/schemas/VoiceJoinTokenAdminResponse"},+ "description": "Join tokens, newest first"+ }+ },+ "required": ["tokens"],+ "additionalProperties": false+ },+ "CreateVoiceJoinTokenRequest": {+ "type": "object",+ "properties": {+ "region_id": {+ "default": null,+ "description": "Region for the new server, or null to let the API pick the nearest existing region",+ "nullable": true,+ "type": "string",+ "minLength": 1,+ "maxLength": 64,+ "pattern": "^[a-z0-9][a-z0-9-]*$"+ },+ "region_name": {+ "default": null,+ "description": "Name for a new region with the id in region_id, created when the server joins",+ "nullable": true,+ "type": "string"+ },+ "region_emoji": {+ "default": null,+ "description": "Emoji for the new region in region_name",+ "nullable": true,+ "type": "string"+ },+ "region_latitude": {+ "default": null,+ "description": "Latitude of the new region in region_name. Placement sends people to the nearest region",+ "nullable": true,+ "type": "number",+ "minimum": -90,+ "maximum": 90+ },+ "region_longitude": {+ "default": null,+ "description": "Longitude of the new region in region_name",+ "nullable": true,+ "type": "number",+ "minimum": -180,+ "maximum": 180+ },+ "server_id": {+ "default": null,+ "description": "Id for the new server",+ "nullable": true,+ "type": "string",+ "minLength": 1,+ "maxLength": 64,+ "pattern": "^[a-z0-9][a-z0-9-]*$"+ },+ "hostname": {+ "default": null,+ "description": "Hostname the server must use for its endpoint",+ "nullable": true,+ "type": "string",+ "maxLength": 253,+ "pattern": "^[A-Za-z0-9.-]+$"+ },+ "internal_endpoint": {+ "default": null,+ "nullable": true,+ "allOf": [{"$ref": "#/components/schemas/VoiceInternalEndpointType"}]+ },+ "soft_connection_limit": {+ "default": null,+ "nullable": true,+ "type": "integer",+ "minimum": 1,+ "maximum": 2147483647+ },+ "notes": {"nullable": true, "type": "string", "maxLength": 2000},+ "vip_only": {"default": false, "type": "boolean"},+ "required_guild_features": {"default": [], "maxItems": 100, "type": "array", "items": {"type": "string"}},+ "allowed_guild_ids": {+ "maxItems": 1000,+ "type": "array",+ "items": {"$ref": "#/components/schemas/SnowflakeType"}+ },+ "allowed_user_ids": {+ "maxItems": 1000,+ "type": "array",+ "items": {"$ref": "#/components/schemas/SnowflakeType"}+ },+ "expires_in_seconds": {+ "default": 3600,+ "description": "Seconds until the token stops working, from 60 to 86400",+ "type": "integer",+ "minimum": 60,+ "maximum": 86400+ }+ }+ },+ "CreateVoiceJoinTokenResponse": {+ "type": "object",+ "properties": {+ "token_id": {+ "description": "ID of the token",+ "allOf": [{"$ref": "#/components/schemas/SnowflakeStringType"}]+ },+ "token": {"type": "string", "description": "The token. It appears in this response only"},+ "expires_at": {"type": "string", "description": "ISO 8601 timestamp when the token stops working"},+ "command": {"type": "string", "description": "Command to run on the voice machine"}+ },+ "required": ["token_id", "token", "expires_at", "command"],+ "additionalProperties": false+ },"WebAuthnCredentialListResponse": {"type": "array","items": {"$ref": "#/components/schemas/WebAuthnCredentialResponse"}@@ -14395,7 +14937,7 @@"type": "object","properties": {"acls": {- "maxItems": 116,+ "maxItems": 119,"type": "array","items": {"$ref": "#/components/schemas/AdminAclType"},"description": "List of access control permissions to assign"@@ -14560,6 +15102,9 @@"user:update:profile","user:update:traits","user:update:username",+ "voice:join_token:create",+ "voice:join_token:list",+ "voice:join_token:revoke","voice:region:create","voice:region:delete","voice:region:list",@@ -19344,7 +19889,7 @@},"acls": {"description": "Replacement list of access control permissions for the key",- "maxItems": 116,+ "maxItems": 119,"type": "array","items": {"$ref": "#/components/schemas/AdminAclType"}}@@ -19362,7 +19907,7 @@"type": "string"},"acls": {- "maxItems": 116,+ "maxItems": 119,"type": "array","items": {"type": "string"},"description": "List of access control permissions for the key"@@ -19392,7 +19937,7 @@"maximum": 365},"acls": {- "maxItems": 116,+ "maxItems": 119,"type": "array","items": {"$ref": "#/components/schemas/AdminAclType"},"description": "List of access control permissions for the key"@@ -19413,7 +19958,7 @@"type": "string"},"acls": {- "maxItems": 116,+ "maxItems": 119,"type": "array","items": {"type": "string"},"description": "List of access control permissions for the key"@@ -19426,7 +19971,7 @@"type": "object","properties": {"acls": {- "maxItems": 116,+ "maxItems": 119,"type": "array","items": {"type": "string", "minLength": 1, "maxLength": 64},"description": "Every admin access control permission the admin API recognises"@@ -23768,7 +24313,7 @@"description": "The enforcement decision behind the current suspension or closure, null when none owns it","allOf": [{"$ref": "#/components/schemas/SnowflakeStringType"}]},- "acls": {"maxItems": 116, "type": "array", "items": {"type": "string"}},+ "acls": {"maxItems": 119, "type": "array", "items": {"type": "string"}},"traits": {"maxItems": 100, "type": "array", "items": {"type": "string"}},"badge_ids": {"allOf": [{"$ref": "#/components/schemas/CustomBadgeIdListResponse"}]},"has_totp": {"type": "boolean"},@@ -24850,6 +25395,59 @@"required": ["id", "name", "created_at", "last_used_at", "rp_id"],"additionalProperties": false},+ "VoiceInternalEndpointType": {+ "type": "string",+ "maxLength": 512,+ "format": "uri",+ "description": "URL the API uses for the control API and health checks of this server, such as http://voice-fra:7880"+ },+ "VoiceJoinTokenAdminResponse": {+ "type": "object",+ "properties": {+ "token_id": {+ "description": "ID of the token",+ "allOf": [{"$ref": "#/components/schemas/SnowflakeStringType"}]+ },+ "status": {+ "description": "pending, used, expired or revoked",+ "allOf": [{"$ref": "#/components/schemas/VoiceJoinTokenStatus"}]+ },+ "created_at": {"type": "string", "description": "ISO 8601 timestamp when the token was created"},+ "expires_at": {"type": "string", "description": "ISO 8601 timestamp when the token stops working"},+ "used_at": {+ "nullable": true,+ "description": "ISO 8601 timestamp when a server joined with it",+ "type": "string"+ },+ "used_server_id": {"nullable": true, "description": "ID of the server that joined with it", "type": "string"},+ "revoked_at": {"nullable": true, "description": "ISO 8601 timestamp when it was revoked", "type": "string"},+ "region_id": {"nullable": true, "description": "Region preset, or null", "type": "string"},+ "region_name": {+ "nullable": true,+ "description": "Name of the new region it creates, or null",+ "type": "string"+ },+ "server_id": {"nullable": true, "description": "Server id preset, or null", "type": "string"},+ "hostname": {"nullable": true, "description": "Hostname preset, or null", "type": "string"},+ "notes": {"nullable": true, "description": "Notes for the new server, or null", "type": "string"}+ },+ "required": [+ "token_id",+ "status",+ "created_at",+ "expires_at",+ "used_at",+ "used_server_id",+ "revoked_at",+ "region_id",+ "region_name",+ "server_id",+ "hostname",+ "notes"+ ],+ "additionalProperties": false+ },+ "VoiceJoinTokenStatus": {"type": "string", "enum": ["pending", "used", "expired", "revoked"]},"VoiceServerAdminResponse": {"type": "object","properties": {@@ -24902,6 +25500,20 @@"items": {"$ref": "#/components/schemas/SnowflakeStringType"},"description": "User IDs explicitly allowed to use this server"},+ "internal_endpoint": {+ "nullable": true,+ "description": "URL the API uses for the control API and health checks, or null to use the endpoint",+ "type": "string"+ },+ "draining": {+ "type": "boolean",+ "description": "Whether placement sends new calls elsewhere while calls on this server finish"+ },+ "health": {+ "description": "Health of the server as this API node last saw it",+ "allOf": [{"$ref": "#/components/schemas/VoiceServerHealthResponse"}]+ },+ "duplicate_api_key": {"type": "boolean", "description": "Whether another server uses the same API key"},"created_at": {"nullable": true,"description": "ISO 8601 timestamp when the server was created",@@ -24926,11 +25538,37 @@"required_guild_features","allowed_guild_ids","allowed_user_ids",+ "internal_endpoint",+ "draining",+ "health",+ "duplicate_api_key","created_at","updated_at"],"additionalProperties": false},+ "VoiceServerHealthResponse": {+ "type": "object",+ "properties": {+ "status": {+ "type": "string",+ "enum": ["up", "down", "unknown"],+ "description": "up, down, or unknown before the first check"+ },+ "checked_at": {+ "nullable": true,+ "description": "ISO 8601 timestamp of the last check, or null before the first",+ "type": "string"+ },+ "error": {+ "nullable": true,+ "description": "Status code or short transport error of the last failed check",+ "type": "string"+ }+ },+ "required": ["status", "checked_at", "error"],+ "additionalProperties": false+ },"VoiceRegionAdminResponse": {"type": "object","properties": {@@ -25330,6 +25968,16 @@"USER_OWNS_GUILDS","VALIDATION_ERROR","VOICE_CHANNEL_FULL",+ "VOICE_JOIN_ENDPOINT_INVALID",+ "VOICE_JOIN_ENDPOINT_UNREACHABLE",+ "VOICE_JOIN_KEY_MISMATCH",+ "VOICE_JOIN_TOKEN_BUSY",+ "VOICE_JOIN_TOKEN_INVALID",+ "VOICE_NODE_SIGNATURE_INVALID",+ "VOICE_NODE_TOKEN_EXPIRED",+ "VOICE_NODE_UNKNOWN",+ "VOICE_SERVER_API_KEY_IN_USE",+ "VOICE_SERVER_ID_IN_USE","WEBAUTHN_CREDENTIAL_LIMIT_REACHED","ACCOUNT_VERIFICATION_REQUIRED","DATE_OF_BIRTH_REQUIRED",@@ -25695,6 +26343,16 @@"This user owns communities.","Validation failed.","Voice channel is full.",+ "The voice server endpoint isn't allowed.",+ "Fluxer couldn't connect to the voice server endpoint.",+ "The voice server rejected its API key and secret.",+ "Another voice server is joining with this token right now.",+ "The voice join token is not valid.",+ "The voice server's signature doesn't match its secret.",+ "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "No voice server has this API key.",+ "Another voice server already uses this API key.",+ "Another voice server already uses this ID.","You've reached the maximum of {count, plural, one {# WebAuthn credential} other {# WebAuthn credentials}}.","{need, select, add_email {{action, select, message {Add an email to your account to send messages.} typing {Add an email to your account to send messages.} reaction {Add an email to your account to add reactions.} direct_message {Add an email to your account to send direct messages.} direct_message_open {Add an email to your account to start direct messages.} group_dm {Add an email to your account to join group chats.} friend_request {Add an email to your account to send friend requests.} friend_accept {Add an email to your account to accept friend requests.} invite_create {Add an email to your account to create invites.} guild_join {Add an email to your account to join this community.} guild_create {Add an email to your account to create a community.} voice {Add an email to your account to join voice channels.} call {Add an email to your account to start calls.} group_call {Add an email to your account to join group calls.} thread_join {Add an email to your account to join threads.} channel_follow {Add an email to your account to follow channels.} ownership_transfer {This person needs to add an email to their account before they can own a community.} application_create {Add an email to your account to create applications.} gift_redeem {Add an email to your account to redeem gifts.} profile_customize {Add an email to your account to change your profile.} report {Add an email to your account to submit reports.} mfa {Add an email to your account to turn on two-factor authentication.} guild_verification {Add an email to your account to talk in this community.} other {Add an email to your account to do this.}}} confirm_email {{action, select, message {Confirm your email to send messages.} typing {Confirm your email to send messages.} reaction {Confirm your email to add reactions.} direct_message {Confirm your email to send direct messages.} direct_message_open {Confirm your email to start direct messages.} group_dm {Confirm your email to join group chats.} friend_request {Confirm your email to send friend requests.} friend_accept {Confirm your email to accept friend requests.} invite_create {Confirm your email to create invites.} guild_join {Confirm your email to join this community.} guild_create {Confirm your email to create a community.} voice {Confirm your email to join voice channels.} call {Confirm your email to start calls.} group_call {Confirm your email to join group calls.} thread_join {Confirm your email to join threads.} channel_follow {Confirm your email to follow channels.} ownership_transfer {This person needs to confirm their email before they can own a community.} application_create {Confirm your email to create applications.} gift_redeem {Confirm your email to redeem gifts.} profile_customize {Confirm your email to change your profile.} report {Confirm your email to submit reports.} mfa {Confirm your email to turn on two-factor authentication.} guild_verification {Confirm your email to talk in this community.} other {Confirm your email to do this.}}} password {{action, select, message {Set a password to send messages.} typing {Set a password to send messages.} reaction {Set a password to add reactions.} direct_message {Set a password to send direct messages.} direct_message_open {Set a password to start direct messages.} group_dm {Set a password to join group chats.} friend_request {Set a password to send friend requests.} friend_accept {Set a password to accept friend requests.} invite_create {Set a password to create invites.} guild_join {Set a password to join this community.} guild_create {Set a password to create a community.} voice {Set a password to join voice channels.} call {Set a password to start calls.} group_call {Set a password to join group calls.} thread_join {Set a password to join threads.} channel_follow {Set a password to follow channels.} ownership_transfer {This person needs to set a password before they can own a community.} application_create {Set a password to create applications.} gift_redeem {Set a password to redeem gifts.} profile_customize {Set a password to change your profile.} report {Set a password to submit reports.} mfa {Set a password to turn on two-factor authentication.} guild_verification {Set a password to talk in this community.} other {Set a password to do this.}}} other {{action, select, message {Claim your account to send messages.} typing {Claim your account to send messages.} reaction {Claim your account to add reactions.} direct_message {Claim your account to send direct messages.} direct_message_open {Claim your account to start direct messages.} group_dm {Claim your account to join group chats.} friend_request {Claim your account to send friend requests.} friend_accept {Claim your account to accept friend requests.} invite_create {Claim your account to create invites.} guild_join {Claim your account to join this community.} guild_create {Claim your account to create a community.} voice {Claim your account to join voice channels.} call {Claim your account to start calls.} group_call {Claim your account to join group calls.} thread_join {Claim your account to join threads.} channel_follow {Claim your account to follow channels.} ownership_transfer {This person needs to claim their account before they can own a community.} application_create {Claim your account to create applications.} gift_redeem {Claim your account to redeem gifts.} profile_customize {Claim your account to change your profile.} report {Claim your account to submit reports.} mfa {Claim your account to turn on two-factor authentication.} guild_verification {Claim your account to talk in this community.} other {Claim your account to do this.}}}}","Enter your date of birth to continue.",diff --git a/fluxer_admin/src/acl.rs b/fluxer_admin/src/acl.rsindex d3d68dddd..ec6ea650b 100644--- a/fluxer_admin/src/acl.rs+++ b/fluxer_admin/src/acl.rs@@ -108,6 +108,9 @@ pub const USER_UPDATE_MFA: &str = "user:update:mfa";pub const USER_UPDATE_PROFILE: &str = "user:update:profile";pub const USER_UPDATE_TRAITS: &str = "user:update:traits";pub const USER_UPDATE_USERNAME: &str = "user:update:username";+pub const VOICE_JOIN_TOKEN_CREATE: &str = "voice:join_token:create";+pub const VOICE_JOIN_TOKEN_LIST: &str = "voice:join_token:list";+pub const VOICE_JOIN_TOKEN_REVOKE: &str = "voice:join_token:revoke";pub const VOICE_REGION_CREATE: &str = "voice:region:create";pub const VOICE_REGION_DELETE: &str = "voice:region:delete";pub const VOICE_REGION_LIST: &str = "voice:region:list";@@ -225,6 +228,9 @@ pub const ALL_ACLS: &[&str] = &[USER_UPDATE_PROFILE,USER_UPDATE_TRAITS,USER_UPDATE_USERNAME,+ VOICE_JOIN_TOKEN_CREATE,+ VOICE_JOIN_TOKEN_LIST,+ VOICE_JOIN_TOKEN_REVOKE,VOICE_REGION_CREATE,VOICE_REGION_DELETE,VOICE_REGION_LIST,diff --git a/fluxer_admin/src/api/types/voice.rs b/fluxer_admin/src/api/types/voice.rsindex a51c44401..c6344cc89 100644--- a/fluxer_admin/src/api/types/voice.rs+++ b/fluxer_admin/src/api/types/voice.rs@@ -37,6 +37,48 @@ pub struct VoiceServer {pub allowed_guild_ids: Vec<String>,#[serde(default)]pub allowed_user_ids: Vec<String>,+ pub internal_endpoint: Option<String>,+ #[serde(default)]+ pub draining: bool,+ pub health: Option<VoiceServerHealth>,+ #[serde(default)]+ pub duplicate_api_key: bool,+}++#[derive(Clone, Debug, Deserialize, Serialize)]+pub struct VoiceServerHealth {+ pub status: String,+ pub checked_at: Option<String>,+ pub error: Option<String>,+}++#[derive(Clone, Debug, Deserialize, Serialize)]+pub struct VoiceJoinToken {+ pub token_id: String,+ pub status: String,+ pub created_at: String,+ pub expires_at: String,+ pub used_at: Option<String>,+ pub used_server_id: Option<String>,+ pub revoked_at: Option<String>,+ pub region_id: Option<String>,+ pub region_name: Option<String>,+ pub server_id: Option<String>,+ pub hostname: Option<String>,+ pub notes: Option<String>,+}++#[derive(Clone, Debug, Deserialize, Serialize)]+pub struct ListVoiceJoinTokensResponse {+ pub tokens: Vec<VoiceJoinToken>,+}++#[derive(Clone, Debug, Deserialize, Serialize)]+pub struct CreateVoiceJoinTokenResponse {+ pub token_id: String,+ pub token: String,+ pub expires_at: String,+ pub command: String,}#[derive(Clone, Debug, Deserialize, Serialize)]diff --git a/fluxer_admin/src/api/voice.rs b/fluxer_admin/src/api/voice.rsindex 66ad1f734..7d985eb8e 100644--- a/fluxer_admin/src/api/voice.rs+++ b/fluxer_admin/src/api/voice.rs@@ -8,9 +8,10 @@ use crate::api::generated::types as generated_types;use super::client::{AdminApiClient, ApiError, ApiResult};use super::types::{- CreateVoiceRegionResponse, CreateVoiceServerResponse, DeleteVoiceResponse,- GetVoiceRegionResponse, ListVoiceRegionsResponse, ListVoiceServersResponse,- UpdateVoiceRegionResponse, UpdateVoiceServerResponse,+ CreateVoiceJoinTokenResponse, CreateVoiceRegionResponse, CreateVoiceServerResponse,+ DeleteVoiceResponse, GetVoiceRegionResponse, ListVoiceJoinTokensResponse,+ ListVoiceRegionsResponse, ListVoiceServersResponse, UpdateVoiceRegionResponse,+ UpdateVoiceServerResponse,};impl AdminApiClient {@@ -128,6 +129,34 @@ impl AdminApiClient {.await}+ pub async fn create_voice_join_token(+ &self,+ params: &serde_json::Value,+ ) -> ApiResult<CreateVoiceJoinTokenResponse> {+ validate_against::<generated_types::CreateVoiceJoinTokenRequest>(params)?;+ self.send(Method::POST, "/admin/voice/join-tokens", Some(params), None)+ .await+ }++ pub async fn list_voice_join_tokens(&self) -> ApiResult<ListVoiceJoinTokensResponse> {+ let response = self+ .generated()+ .list_admin_voice_join_tokens()+ .await+ .map_err(|e| self.generated_error(e))?;+ self.generated_value(response.into_inner())+ }++ pub async fn revoke_voice_join_token(&self, token_id: &str) -> ApiResult<DeleteVoiceResponse> {+ self.send(+ Method::DELETE,+ &format!("/admin/voice/join-tokens/{}", urlencoding::encode(token_id)),+ None::<&serde_json::Value>,+ None,+ )+ .await+ }+pub async fn delete_voice_server(&self,region_id: &str,diff --git a/fluxer_admin/src/routes/voice.rs b/fluxer_admin/src/routes/voice.rsindex f840435d7..a00e6fe3d 100644--- a/fluxer_admin/src/routes/voice.rs+++ b/fluxer_admin/src/routes/voice.rs@@ -105,6 +105,8 @@ async fn voice_servers_page(region_id: None,region_name: None,servers: None,+ regions: &[],+ join_tokens: None,connection_counts: &HashMap::new(),error: None,csrf_token: &csrf_token,@@ -117,16 +119,26 @@ async fn voice_servers_page(let client = AdminApiClient::new(state.http_client(), config, &auth.0.session);let connection_counts = load_server_connection_counts(&client).await;- let region_name = match client.get_voice_region(region_id, false).await {- Ok(resp) => resp- .region- .and_then(|r| r.name)- .unwrap_or_else(|| region_id.to_string()),+ let regions = match client.list_voice_regions(false).await {+ Ok(resp) => resp.regions,Err(error) => {- tracing::warn!(%error, region_id, "admin API request failed: load voice region");- region_id.to_string()+ tracing::warn!(%error, "admin API request failed: load voice regions");+ Vec::new()}};+ let region_name = regions+ .iter()+ .find(|r| r.id == region_id)+ .and_then(|r| r.name.clone())+ .unwrap_or_else(|| region_id.to_string());+ let join_tokens = client+ .list_voice_join_tokens()+ .await+ .map(|resp| resp.tokens)+ .inspect_err(+ |error| tracing::warn!(%error, "admin API request failed: load voice join tokens"),+ )+ .ok();match client.list_voice_servers(region_id).await {Ok(resp) => {@@ -137,6 +149,8 @@ async fn voice_servers_page(region_id: Some(region_id),region_name: Some(®ion_name),servers: Some(&resp.servers),+ regions: ®ions,+ join_tokens: join_tokens.as_deref(),connection_counts: &connection_counts,error: None,csrf_token: &csrf_token,@@ -153,6 +167,8 @@ async fn voice_servers_page(region_id: Some(region_id),region_name: Some(®ion_name),servers: None,+ regions: ®ions,+ join_tokens: join_tokens.as_deref(),connection_counts: &connection_counts,error: Some(&msg),csrf_token: &csrf_token,diff --git a/fluxer_admin/src/routes/voice_actions.rs b/fluxer_admin/src/routes/voice_actions.rsindex b1e0a9e73..bd501266c 100644--- a/fluxer_admin/src/routes/voice_actions.rs+++ b/fluxer_admin/src/routes/voice_actions.rs@@ -104,6 +104,63 @@ pub(crate) fn build_server_body(form: &MultiValueForm) -> serde_json::Value {},);}+ if let Some(raw) = form.first("internal_endpoint") {+ let trimmed = raw.trim();+ body.insert(+ "internal_endpoint".into(),+ if trimmed.is_empty() {+ serde_json::Value::Null+ } else {+ trimmed.into()+ },+ );+ }+ if form.contains_key("draining_field") {+ body.insert("draining".into(), form.bool_value("draining").into());+ }+ body.insert("vip_only".into(), form.bool_value("vip_only").into());+ insert_submitted_list(&mut body, form, "required_guild_features");+ insert_submitted_list(&mut body, form, "allowed_guild_ids");+ serde_json::Value::Object(body)+}++pub(crate) fn build_join_token_body(form: &MultiValueForm) -> serde_json::Value {+ let mut body = serde_json::Map::new();+ match form.clean("region_choice").as_deref() {+ Some("new") => {+ for (field, key) in [+ ("new_region_id", "region_id"),+ ("new_region_name", "region_name"),+ ("new_region_emoji", "region_emoji"),+ ] {+ if let Some(value) = form.clean(field) {+ body.insert(key.into(), value.into());+ }+ }+ }+ Some("auto") | None => {}+ Some(region) => {+ body.insert("region_id".into(), region.into());+ }+ }+ for field in ["server_id", "hostname"] {+ if let Some(value) = form.clean(field) {+ body.insert(field.into(), value.into());+ }+ }+ if let Some(limit) = form+ .clean("soft_connection_limit")+ .and_then(|value| value.parse::<i64>().ok())+ .filter(|limit| *limit > 0)+ {+ body.insert("soft_connection_limit".into(), limit.into());+ }+ let hours = form+ .clean("expires_hours")+ .and_then(|value| value.parse::<i64>().ok())+ .unwrap_or(1)+ .clamp(1, 24);+ body.insert("expires_in_seconds".into(), (hours * 3600).into());body.insert("vip_only".into(), form.bool_value("vip_only").into());insert_submitted_list(&mut body, form, "required_guild_features");insert_submitted_list(&mut body, form, "allowed_guild_ids");@@ -183,7 +240,36 @@ pub(crate) async fn voice_servers_post(let client = AdminApiClient::new(state.http_client(), config, &auth.0.session);let action = aq.action.as_deref().unwrap_or("");+ if action == "join_token" {+ let body = build_join_token_body(&form);+ let region_id = form.clean("region_id");+ return match client.create_voice_join_token(&body).await {+ Ok(created) => axum::response::Html(+ crate::templates::pages::voice_servers::join_token_created_page(+ config,+ &auth.0,+ region_id.as_deref(),+ &created,+ )+ .into_string(),+ )+ .into_response(),+ Err(e) => flash::redirect_with_flash(+ &servers_url(base, region_id.as_deref()),+ FlashData::error(format!("Failed to create a join token: {e:?}")),+ config.secure_cookies(),+ ),+ };+ }+let (msg, level) = match action {+ "revoke_token" => match form.clean("token_id") {+ Some(token_id) => match client.revoke_voice_join_token(&token_id).await {+ Ok(_) => ("Join token revoked".to_string(), "success"),+ Err(e) => (format!("Failed to revoke join token: {e:?}"), "error"),+ },+ None => ("Token ID is required".to_string(), "error"),+ },"create" => {let body = build_server_body(&form);let has_ids = body.get("region_id").and_then(|v| v.as_str()).is_some()@@ -225,17 +311,81 @@ pub(crate) async fn voice_servers_post(_ => ("Unknown action".to_string(), "error"),};- let redirect_url = if let Some(region_id) = form.clean("region_id") {- format!(- "{base}/voice-servers?region_id={}",- urlencoding::encode(®ion_id)- )- } else {- format!("{base}/voice-servers")- };flash::redirect_with_flash(- &redirect_url,+ &servers_url(base, form.clean("region_id").as_deref()),flash_from_level(level, &msg),config.secure_cookies(),)}++fn servers_url(base: &str, region_id: Option<&str>) -> String {+ match region_id {+ Some(region_id) => format!(+ "{base}/voice-servers?region_id={}",+ urlencoding::encode(region_id)+ ),+ None => format!("{base}/voice-servers"),+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn form(pairs: &[(&str, &str)]) -> MultiValueForm {+ let encoded = pairs+ .iter()+ .map(|(k, v)| format!("{}={}", urlencoding::encode(k), urlencoding::encode(v)))+ .collect::<Vec<_>>()+ .join("&");+ MultiValueForm::parse(encoded.as_bytes())+ }++ #[test]+ fn join_token_body_for_a_new_region_and_presets() {+ let body = build_join_token_body(&form(&[+ ("region_choice", "new"),+ ("new_region_id", "sgp"),+ ("new_region_name", "Singapore"),+ ("new_region_emoji", "🇸🇬"),+ ("server_id", "sgp-server-1"),+ ("hostname", " voice-sgp.chat.example.com "),+ ("soft_connection_limit", "300"),+ ("expires_hours", "48"),+ ]));+ assert_eq!(body["region_id"], "sgp");+ assert_eq!(body["region_name"], "Singapore");+ assert_eq!(body["server_id"], "sgp-server-1");+ assert_eq!(body["hostname"], "voice-sgp.chat.example.com");+ assert_eq!(body["soft_connection_limit"], 300);+ assert_eq!(body["expires_in_seconds"], 86400);+ }++ #[test]+ fn join_token_body_with_automatic_region_has_no_region() {+ let body = build_join_token_body(&form(&[("region_choice", "auto")]));+ assert!(body.get("region_id").is_none());+ assert_eq!(body["expires_in_seconds"], 3600);+ let picked = build_join_token_body(&form(&[("region_choice", "fra")]));+ assert_eq!(picked["region_id"], "fra");+ }++ #[test]+ fn server_body_keeps_draining_unless_the_form_shows_it() {+ let toggle = build_server_body(&form(&[+ ("region_id", "fra"),+ ("server_id", "a"),+ ("is_active", "false"),+ ]));+ assert!(toggle.get("draining").is_none());+ assert!(toggle.get("internal_endpoint").is_none());+ let edit = build_server_body(&form(&[+ ("region_id", "fra"),+ ("server_id", "a"),+ ("draining_field", "1"),+ ("internal_endpoint", ""),+ ]));+ assert_eq!(edit["draining"], false);+ assert!(edit["internal_endpoint"].is_null());+ }+}diff --git a/fluxer_admin/src/templates/pages/voice_servers.rs b/fluxer_admin/src/templates/pages/voice_servers.rsindex ce2d2a1cd..ad658f450 100644--- a/fluxer_admin/src/templates/pages/voice_servers.rs+++ b/fluxer_admin/src/templates/pages/voice_servers.rs@@ -1,7 +1,7 @@// SPDX-License-Identifier: AGPL-3.0-or-lateruse crate::{- api::types::VoiceServer,+ api::types::{CreateVoiceJoinTokenResponse, VoiceJoinToken, VoiceRegion, VoiceServer},config::AdminConfig,middleware::auth::AuthContext,templates::{@@ -10,9 +10,13 @@ use crate::{data_field::data_field_text,empty_state::empty_state_full,error_display::error_alert,- form::csrf_input,+ form::{csrf_input, form_field_with_id},page_container::{card, page_header_full},- voice::{voice_features_list, voice_guild_ids_list, voice_status_badges},+ table::data_table,+ voice::{+ voice_features_list, voice_guild_ids_list, voice_restriction_fields,+ voice_status_badges,+ },},layout::{LayoutOptions, admin_layout_ext},},@@ -26,6 +30,8 @@ pub struct VoiceServersPageParams<'a> {pub region_id: Option<&'a str>,pub region_name: Option<&'a str>,pub servers: Option<&'a [VoiceServer]>,+ pub regions: &'a [VoiceRegion],+ pub join_tokens: Option<&'a [VoiceJoinToken]>,pub connection_counts: &'a HashMap<String, i64>,pub error: Option<&'a str>,pub csrf_token: &'a str,@@ -41,6 +47,8 @@ pub fn voice_servers_page(region_id,region_name,servers,+ regions,+ join_tokens,connection_counts,error,csrf_token,@@ -65,6 +73,16 @@ pub fn voice_servers_page(&title,None,Some(html! {+ a href="#join" class="mr-2" {+ button type="button"+ class="inline-flex items-center justify-center gap-2 font-medium \+ rounded-lg transition-all duration-150 focus:outline-none \+ focus:ring-2 focus:ring-offset-2 bg-neutral-900 text-white \+ hover:bg-neutral-800 w-fit px-4 py-2 text-base \+ focus:ring-offset-white" {+ span { "Add a voice server" }+ }+ }a href="#create" {button type="button"class="inline-flex items-center justify-center gap-2 font-medium \@@ -83,6 +101,14 @@ pub fn voice_servers_page(@if let Some(servers) = servers {(servers_list(config, rid, servers, connection_counts, csrf_token))}+ div id="join" class="mt-8" {+ (join_token_form(config, rid, regions, csrf_token))+ }+ @if let Some(tokens) = join_tokens {+ div class="mt-8" {+ (join_tokens_table(config, rid, tokens, csrf_token))+ }+ }div id="create" class="mt-8" {(create_server_form(config, rid, csrf_token))}@@ -166,6 +192,24 @@ fn server_card(.map_or_else(|| "No limit".to_string(), |v| v.to_string());let connections_str =connection_count.map_or_else(|| "Unavailable".to_string(), |v| v.to_string());+ let health = server+ .health+ .as_ref()+ .map_or("unknown", |h| h.status.as_str());+ let health_str = match (+ health,+ server.health.as_ref().and_then(|h| h.error.as_deref()),+ ) {+ ("up", _) => "Up".to_string(),+ ("down", Some(err)) => format!("Down ({err})"),+ ("down", None) => "Down".to_string(),+ (_, Some(err)) => format!("Not checked ({err})"),+ _ => "Not checked yet".to_string(),+ };+ let internal_str = server+ .internal_endpoint+ .clone()+ .unwrap_or_else(|| "Same as the endpoint".to_string());let at_soft_limit = matches!((server.soft_connection_limit, connection_count),(Some(limit), Some(count)) if limit > 0 && count >= limit@@ -183,6 +227,17 @@ fn server_card(@if at_soft_limit {(badge("AT SOFT LIMIT", BadgeVariant::Warning))}+ @if server.draining {+ (badge("DRAINING", BadgeVariant::Warning))+ }+ @if health == "down" {+ (badge("DOWN", BadgeVariant::Danger))+ } @else if health == "up" {+ (badge("UP", BadgeVariant::Success))+ }+ @if server.duplicate_api_key {+ (badge("SHARED API KEY", BadgeVariant::Danger))+ }(voice_status_badges(vip_only, has_features, has_guild_ids))}p class="text-sm text-neutral-500" { (endpoint) }@@ -194,6 +249,14 @@ fn server_card((data_field_text("Longitude", &lng_str))(data_field_text("Soft connection limit", &soft_limit_str))(data_field_text("Live connections", &connections_str))+ (data_field_text("Health", &health_str))+ (data_field_text("Draining", if server.draining { "Yes" } else { "No" }))+ (data_field_text("Internal URL", &internal_str))+ }+ @if server.duplicate_api_key {+ p class="mb-4 text-sm text-red-700" {+ "Another server uses the same API key. Fluxer checks webhooks from only one of them. Give each server its own key."+ }}@if let Some(notes) = server.notes.as_deref() {div class="mb-4" {@@ -252,3 +315,240 @@ fn server_card(}})}++const TRUST_NOTE: &str = "A voice server handles the audio and video of every call placed on it, and whoever runs it can record those calls. Only give this token to a machine you trust.";++fn join_token_form(+ config: &AdminConfig,+ region_id: &str,+ regions: &[VoiceRegion],+ csrf_token: &str,+) -> Markup {+ let base = &config.base_path;+ card(html! {+ h2 class="mb-1 font-semibold text-base text-neutral-900" { "Add a voice server" }+ p class="mb-4 text-sm text-neutral-600" {+ "Create a join token, then run the command it shows on the voice machine. rexctl installs LiveKit there, gets a certificate and registers the server here."+ }+ form method="post" action={(base) "/voice-servers?action=join_token"} class="space-y-4" {+ (csrf_input(csrf_token))+ input type="hidden" name="region_id" value=(region_id);+ div class="space-y-1" {+ label for="join-region" class="block text-sm font-medium text-neutral-700" { "Region" }+ select id="join-region" name="region_choice"+ class="block w-full rounded-md border border-neutral-300 px-3 py-2 text-sm shadow-sm" {+ option value="auto" { "Automatic, the nearest existing region" }+ @for region in regions {+ option value=(region.id) selected[region.id == region_id] {+ (region.name.as_deref().unwrap_or(®ion.id))+ }+ }+ option value="new" { "A new region" }+ }+ }+ div class="grid grid-cols-1 gap-4 md:grid-cols-3" {+ (form_field_with_id("New region ID", "join-new-region-id", "new_region_id", "text", "", "sgp", false))+ (form_field_with_id("New region name", "join-new-region-name", "new_region_name", "text", "", "Singapore", false))+ (form_field_with_id("New region emoji", "join-new-region-emoji", "new_region_emoji", "text", "", "🇸🇬", false))+ }+ div class="grid grid-cols-1 gap-4 md:grid-cols-2" {+ (form_field_with_id("Server ID (optional)", "join-server-id", "server_id", "text", "", "fra-server-2", false))+ (form_field_with_id("Hostname (optional)", "join-hostname", "hostname", "text", "", "voice-fra.chat.example.com", false))+ (form_field_with_id("Soft connection limit (optional)", "join-soft-limit", "soft_connection_limit", "number", "", "300", false))+ (form_field_with_id("Hours until the token expires", "join-expires", "expires_hours", "number", "1", "1", false))+ }+ (voice_restriction_fields("join", false, "", ""))+ p class="text-sm text-neutral-600" { (TRUST_NOTE) }+ button type="submit"+ class="w-full inline-flex items-center justify-center gap-2 font-medium \+ rounded-lg transition-all duration-150 focus:outline-none \+ focus:ring-2 focus:ring-offset-2 bg-neutral-900 text-white \+ hover:bg-neutral-800 px-4 py-2 text-base focus:ring-offset-white" {+ span { "Create join token" }+ }+ }+ })+}++fn join_tokens_table(+ config: &AdminConfig,+ region_id: &str,+ tokens: &[VoiceJoinToken],+ csrf_token: &str,+) -> Markup {+ let base = &config.base_path;+ card(html! {+ h2 class="mb-4 font-semibold text-base text-neutral-900" { "Join tokens" }+ @if tokens.is_empty() {+ p class="text-sm text-neutral-600" { "No join tokens from the last 7 days." }+ } @else {+ (data_table(&["Token", "Status", "Region", "Server", "Expires", ""], html! {+ @for token in tokens {+ tr {+ td class="px-4 py-2 text-sm" { (token.token_id) }+ td class="px-4 py-2 text-sm" {+ @match token.status.as_str() {+ "pending" => (badge("PENDING", BadgeVariant::Info)),+ "used" => (badge("USED", BadgeVariant::Success)),+ "revoked" => (badge("REVOKED", BadgeVariant::Default)),+ _ => (badge("EXPIRED", BadgeVariant::Default)),+ }+ }+ td class="px-4 py-2 text-sm" {+ (token.region_name.as_deref().or(token.region_id.as_deref()).unwrap_or("Automatic"))+ }+ td class="px-4 py-2 text-sm" {+ (token.used_server_id.as_deref().or(token.server_id.as_deref()).unwrap_or(""))+ }+ td class="px-4 py-2 text-sm" { (token.expires_at) }+ td class="px-4 py-2 text-sm" {+ @if token.status == "pending" {+ form method="post" action={(base) "/voice-servers?action=revoke_token"} {+ (csrf_input(csrf_token))+ input type="hidden" name="region_id" value=(region_id);+ input type="hidden" name="token_id" value=(token.token_id);+ button type="submit" class="text-red-600 hover:underline" { "Revoke" }+ }+ }+ }+ }+ }+ }))+ }+ })+}++pub fn join_token_created_page(+ config: &AdminConfig,+ auth: &AuthContext,+ region_id: Option<&str>,+ created: &CreateVoiceJoinTokenResponse,+) -> Markup {+ let base = &config.base_path;+ let back = match region_id {+ Some(rid) => format!(+ "{base}/voice-servers?region_id={}",+ urlencoding::encode(rid)+ ),+ None => format!("{base}/voice-servers"),+ };+ let content = html! {+ (page_header_full("Join token created", None, None, Some(&back), Some("Back to servers"), html! {}))+ (card(html! {+ p class="mb-3 text-sm text-neutral-700" {+ "Run this on the voice machine. This page shows the token once."+ }+ pre class="mb-3 overflow-x-auto whitespace-pre-wrap break-all rounded bg-neutral-900 p-3 text-sm text-white" {+ code { (created.command) }+ }+ button type="button"+ class="mb-4 cursor-pointer text-blue-600 hover:underline"+ data-copy-value=(created.command)+ onclick="window.__adminCopyToClipboard && window.__adminCopyToClipboard(this.dataset.copyValue, this, 'Copied')" {+ "Copy command"+ }+ p class="text-sm text-neutral-700" {+ "The token works once and expires at " (created.expires_at) "."+ }+ p class="mt-2 text-sm text-neutral-600" { (TRUST_NOTE) }+ }))+ };+ admin_layout_ext(+ config,+ auth,+ "Voice Servers",+ "voice-servers",+ None,+ content,+ LayoutOptions::default(),+ )+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::{config::ProxyConfig, session::Session};++ fn test_config() -> AdminConfig {+ AdminConfig {+ host: String::new(),+ port: 0,+ secret_key_base: "k".repeat(64),+ base_path: "/admin".to_owned(),+ api_endpoint: String::new(),+ api_public_endpoint: String::new(),+ media_endpoint: String::new(),+ static_cdn_endpoint: String::new(),+ reports_bucket_origin: String::new(),+ admin_endpoint: String::new(),+ web_app_endpoint: String::new(),+ oauth_client_id: String::new(),+ oauth_client_secret: String::new(),+ oauth_redirect_uri: String::new(),+ build_version: String::new(),+ self_hosted: true,+ proxy: ProxyConfig {+ trust_client_ip_header: false,+ client_ip_header_name: String::new(),+ },+ }+ }++ fn test_auth() -> AuthContext {+ AuthContext {+ session: Session {+ user_id: "1".to_owned(),+ access_token: String::new(),+ created_at: 0,+ },+ admin_user: None,+ pending_registration_count: None,+ branding: None,+ }+ }++ fn token(status: &str) -> VoiceJoinToken {+ VoiceJoinToken {+ token_id: format!("1{status}"),+ status: status.to_owned(),+ created_at: "2026-10-29T20:00:00Z".to_owned(),+ expires_at: "2026-10-29T21:00:00Z".to_owned(),+ used_at: None,+ used_server_id: None,+ revoked_at: None,+ region_id: Some("fra".to_owned()),+ region_name: None,+ server_id: None,+ hostname: None,+ notes: None,+ }+ }++ #[test]+ fn token_table_shows_each_status_and_revoke_only_for_pending() {+ let config = test_config();+ let tokens = ["pending", "used", "expired", "revoked"].map(token);+ let html = join_tokens_table(&config, "fra", &tokens, "csrf").into_string();+ for label in ["PENDING", "USED", "EXPIRED", "REVOKED"] {+ assert!(html.contains(label), "{label} missing");+ }+ assert_eq!(html.matches("action=revoke_token").count(), 1);+ assert!(!html.contains("fvj_"));+ }++ #[test]+ fn created_page_shows_the_command_once() {+ let config = test_config();+ let created = CreateVoiceJoinTokenResponse {+ token_id: "1".to_owned(),+ token: "fvj_1_secret".to_owned(),+ expires_at: "2026-10-29T21:00:00Z".to_owned(),+ command: "curl -fsSL https://fluxer.dev/install.sh | sh -s -- voice chat.example.com fvj_1_secret".to_owned(),+ };+ let html =+ join_token_created_page(&config, &test_auth(), Some("fra"), &created).into_string();+ assert_eq!(html.matches("<code>curl -fsSL").count(), 1);+ assert!(html.contains("Copy command"));+ assert!(html.contains("record those calls"));+ }+}diff --git a/fluxer_admin/src/templates/pages/voice_servers_forms.rs b/fluxer_admin/src/templates/pages/voice_servers_forms.rsindex 846eb9ae4..bf3ee33e3 100644--- a/fluxer_admin/src/templates/pages/voice_servers_forms.rs+++ b/fluxer_admin/src/templates/pages/voice_servers_forms.rs@@ -32,6 +32,7 @@ pub fn edit_server_form(.soft_connection_limit.map_or_else(String::new, |v| v.to_string());let notes = server.notes.as_deref().unwrap_or("");+ let internal_endpoint = server.internal_endpoint.as_deref().unwrap_or("");let vip_only = server.vip_only.unwrap_or(false);let features_csv = server.required_guild_features.join(", ");let guild_ids_csv = server.allowed_guild_ids.join(", ");@@ -72,6 +73,15 @@ pub fn edit_server_form("Leave empty for no limit","Placement prefers another server once this server holds this many connections",))+ (form_field_with_helper(+ "Internal URL",+ &format!("{id_prefix}-internal-endpoint"),+ "internal_endpoint",+ "url",+ internal_endpoint,+ "http://voice-fra-server-1:7880",+ INTERNAL_URL_HELPER,+ ))(notes_field(&format!("{id_prefix}-notes"), notes))(form_field_with_helper("API Key",@@ -99,6 +109,14 @@ pub fn edit_server_form(checked: is_active,enabled: true,}.render())+ input type="hidden" name="draining_field" value="1";+ (Checkbox {+ name: "draining",+ value: "true",+ label: "Draining. New calls go to other servers and calls already here keep going.",+ checked: server.draining,+ enabled: true,+ }.render())}(voice_restriction_fields(&id_prefix, vip_only, &features_csv, &guild_ids_csv))button type="submit"@@ -123,7 +141,10 @@ pub fn create_server_form(config: &AdminConfig, region_id: &str, csrf_token: &stdiv class="grid grid-cols-1 gap-4 md:grid-cols-2" {(form_field_with_id("Server ID", "new-server-id", "server_id", "text", "", "livekit-us-east-1", true))(form_field_with_id("Endpoint", "new-server-endpoint", "endpoint", "url", "", "wss://livekit.example.com", true))- (form_field_with_id("API Key", "new-server-api-key", "api_key", "text", "", "LiveKit API key", true))+ div class="space-y-1" {+ (form_field_with_id("API Key", "new-server-api-key", "api_key", "text", "", "LiveKit API key", true))+ p class="text-xs text-neutral-500" { "Each server needs its own key." }+ }(form_field_with_id("API Secret", "new-server-api-secret", "api_secret", "password", "", "LiveKit API secret", true))(form_field_with_id("Latitude (optional)", "new-server-latitude", "latitude", "number", "", "40.7128", false))(form_field_with_id("Longitude (optional)", "new-server-longitude", "longitude", "number", "", "-74.0060", false))@@ -136,6 +157,15 @@ pub fn create_server_form(config: &AdminConfig, region_id: &str, csrf_token: &st"Leave empty for no limit","Placement prefers another server once this server holds this many connections",))+ (form_field_with_helper(+ "Internal URL (optional)",+ "new-server-internal-endpoint",+ "internal_endpoint",+ "url",+ "",+ "http://voice-fra-server-1:7880",+ INTERNAL_URL_HELPER,+ ))}(notes_field("new-server-notes", ""))div class="space-y-3" {@@ -159,6 +189,8 @@ pub fn create_server_form(config: &AdminConfig, region_id: &str, csrf_token: &st})}+const INTERNAL_URL_HELPER: &str = "Fluxer uses this address for the LiveKit control API and health checks. Leave it empty to use the endpoint.";+fn form_field_with_helper(label: &str,id: &str,diff --git a/fluxer_api/src/api/Config.ts b/fluxer_api/src/api/Config.tsindex ace6b5b4d..dfaef0c85 100644--- a/fluxer_api/src/api/Config.ts+++ b/fluxer_api/src/api/Config.ts@@ -453,8 +453,11 @@ export function buildAPIConfigFromMaster(master: MasterConfig): APIConfig {apiSecret: master.integrations.voice.api_secret,url: master.integrations.voice.url,internalUrl: master.integrations.voice.internal_url,+ bundled: master.integrations.voice.bundled ?? true,defaultRegion: master.integrations.voice.default_region,p2pStunUrls: master.integrations.voice.p2p_stun_urls,+ joinAllowPrivateEndpoints: master.integrations.voice.join_allow_private_endpoints ?? false,+ healthIntervalMs: master.integrations.voice.health_interval_ms ?? 15000,},stripe: {enabled: master.integrations.stripe.enabled,diff --git a/fluxer_api/src/api/admin/AdminAuditActions.ts b/fluxer_api/src/api/admin/AdminAuditActions.tsindex 3ead031db..174731d0b 100644--- a/fluxer_api/src/api/admin/AdminAuditActions.ts+++ b/fluxer_api/src/api/admin/AdminAuditActions.ts@@ -64,6 +64,7 @@ export const AdminAuditReadActions = {LIST_USER_SESSIONS: 'list_user_sessions',LIST_USER_SSO_IDENTITIES: 'list_user_sso_identities',LIST_USER_STORE_PURCHASES: 'list_user_store_purchases',+ LIST_VOICE_JOIN_TOKENS: 'list_voice_join_tokens',LIST_VOICE_REGIONS: 'list_voice_regions',LIST_VOICE_SERVERS: 'list_voice_servers',LIST_WEBAUTHN_CREDENTIALS: 'list_webauthn_credentials',diff --git a/fluxer_api/src/api/admin/AdminService.ts b/fluxer_api/src/api/admin/AdminService.tsindex 92fcb36a8..42fb35764 100644--- a/fluxer_api/src/api/admin/AdminService.ts+++ b/fluxer_api/src/api/admin/AdminService.ts@@ -53,6 +53,7 @@ import type {ReportService} from '@app/api/report/ReportService';import type {StoreEntitlementService} from '@app/api/store_billing/StoreEntitlementService';import {systemDmsHidden} from '@app/api/user/services/SystemDmMessage';import type {UserService} from '@app/api/user/services/UserService';+import {VoiceJoinTokenRepository} from '@app/api/voice/VoiceJoinTokenRepository';import type {VoiceRepository} from '@app/api/voice/VoiceRepository';import {DirectMessagesDisabledError} from '@fluxer/errors/src/domains/channel/DirectMessagesDisabledError';import type {SendSystemDmResponse} from '@fluxer/schema/src/domains/admin/AdminSchemas';@@ -232,6 +233,8 @@ export class AdminService {this.voiceService = new AdminVoiceService({apiContext: this.apiContext,voiceRepository: this.voiceRepository,+ joinTokenRepository: new VoiceJoinTokenRepository(),+ snowflakeService: snowflake,auditService: this.auditService,});this.badgeService = new AdminBadgeService({diff --git a/fluxer_api/src/api/admin/controllers/VoiceAdminController.ts b/fluxer_api/src/api/admin/controllers/VoiceAdminController.tsindex 07310037b..c8ce1a81c 100644--- a/fluxer_api/src/api/admin/controllers/VoiceAdminController.ts+++ b/fluxer_api/src/api/admin/controllers/VoiceAdminController.ts@@ -11,15 +11,19 @@ import {Validator} from '@app/api/Validator';import {AdminACLs} from '@fluxer/constants/src/AdminACLs';import {isRecord} from '@fluxer/record_utils/src/RecordGuards';import {+ CreateVoiceJoinTokenRequest,+ CreateVoiceJoinTokenResponse,CreateVoiceRegionRequest,CreateVoiceRegionResponse,CreateVoiceServerRequest,CreateVoiceServerRequestBody,CreateVoiceServerResponse,+ CreateVoiceServerTestTokenResponse,DeleteVoiceResponse,GetVoiceRegionQuery,GetVoiceRegionResponse,GetVoiceServerResponse,+ ListVoiceJoinTokensResponse,ListVoiceRegionsQuery,ListVoiceRegionsResponse,ListVoiceServersResponse,@@ -29,6 +33,7 @@ import {UpdateVoiceServerRequest,UpdateVoiceServerRequestBody,UpdateVoiceServerResponse,+ VoiceJoinTokenIdParam,VoiceRegionIdParam,VoiceServerIdParam,} from '@fluxer/schema/src/domains/admin/AdminVoiceSchemas';@@ -320,4 +325,110 @@ export function VoiceAdminController(app: HonoApp) {);},);+ app.post(+ '/admin/voice/regions/:region_id/servers/:server_id/test-token',+ RateLimitMiddleware(RateLimitConfigs.ADMIN_GUILD_MODIFY),+ requireAdminACL(AdminACLs.VOICE_SERVER_UPDATE),+ Validator('param', VoiceServerIdParam),+ OpenAPI({+ operationId: 'create_admin_voice_server_test_token',+ summary: 'Create a voice server test token',+ responseSchema: CreateVoiceServerTestTokenResponse,+ statusCode: 200,+ security: 'adminApiKey',+ tags: 'Admin',+ description:+ 'Signs a 60 second LiveKit token for a hidden, audio only participant in a throwaway room on one server, for a connection test from outside its network. Fluxer ignores webhooks for these rooms. Creates audit log entry. Requires VOICE_SERVER_UPDATE permission.',+ }),+ async (ctx) => {+ const adminService = ctx.get('adminService');+ const {region_id, server_id} = ctx.req.valid('param');+ return ctx.json(+ await adminService.voiceService.createServerTestToken(+ {region_id, server_id},+ ctx.get('adminUserId'),+ ctx.get('auditLogReason'),+ ),+ );+ },+ );+ app.post(+ '/admin/voice/join-tokens',+ RateLimitMiddleware(RateLimitConfigs.ADMIN_GUILD_MODIFY),+ requireAdminACL(AdminACLs.VOICE_JOIN_TOKEN_CREATE),+ Validator('json', CreateVoiceJoinTokenRequest),+ OpenAPI({+ operationId: 'create_admin_voice_join_token',+ summary: 'Create a voice join token',+ requestSchema: CreateVoiceJoinTokenRequest,+ responseSchema: CreateVoiceJoinTokenResponse,+ statusCode: 200,+ security: 'adminApiKey',+ tags: 'Admin',+ description:+ 'Creates a single use token that lets one machine register one voice server. The token appears in this response only. Creates audit log entry. Requires VOICE_JOIN_TOKEN_CREATE permission.',+ }),+ async (ctx) => {+ const adminService = ctx.get('adminService');+ return ctx.json(+ await adminService.voiceService.createJoinToken(+ ctx.req.valid('json'),+ ctx.get('adminUserId'),+ ctx.get('auditLogReason'),+ ),+ );+ },+ );+ app.get(+ '/admin/voice/join-tokens',+ RateLimitMiddleware(RateLimitConfigs.ADMIN_LOOKUP),+ requireAdminACL(AdminACLs.VOICE_JOIN_TOKEN_LIST),+ OpenAPI({+ operationId: 'list_admin_voice_join_tokens',+ summary: 'List voice join tokens',+ responseSchema: ListVoiceJoinTokensResponse,+ statusCode: 200,+ security: 'adminApiKey',+ tags: 'Admin',+ description:+ 'Lists voice join tokens from the last 7 days with their status, without the token values. Requires VOICE_JOIN_TOKEN_LIST permission.',+ }),+ async (ctx) => {+ const adminService = ctx.get('adminService');+ const response = await adminService.voiceService.listJoinTokens();+ await recordAdminRead(ctx, {+ targetType: 'voice_join_token',+ targetId: 0n,+ action: AdminAuditReadActions.LIST_VOICE_JOIN_TOKENS,+ metadata: {result_count: response.tokens.length},+ });+ return ctx.json(response);+ },+ );+ app.delete(+ '/admin/voice/join-tokens/:token_id',+ RateLimitMiddleware(RateLimitConfigs.ADMIN_GUILD_MODIFY),+ requireAdminACL(AdminACLs.VOICE_JOIN_TOKEN_REVOKE),+ Validator('param', VoiceJoinTokenIdParam),+ OpenAPI({+ operationId: 'revoke_admin_voice_join_token',+ summary: 'Revoke a voice join token',+ responseSchema: DeleteVoiceResponse,+ statusCode: 200,+ security: 'adminApiKey',+ tags: 'Admin',+ description:+ 'Stops a pending voice join token from working. Creates audit log entry. Requires VOICE_JOIN_TOKEN_REVOKE permission.',+ }),+ async (ctx) => {+ const adminService = ctx.get('adminService');+ return ctx.json(+ await adminService.voiceService.revokeJoinToken(+ ctx.req.valid('param').token_id,+ ctx.get('adminUserId'),+ ctx.get('auditLogReason'),+ ),+ );+ },+ );}diff --git a/fluxer_api/src/api/admin/services/AdminVoiceService.ts b/fluxer_api/src/api/admin/services/AdminVoiceService.tsindex bcd51a13f..e73d4494e 100644--- a/fluxer_api/src/api/admin/services/AdminVoiceService.ts+++ b/fluxer_api/src/api/admin/services/AdminVoiceService.ts@@ -1,14 +1,35 @@// SPDX-License-Identifier: AGPL-3.0-or-later+import {randomBytes} from 'node:crypto';import type {ApiContext} from '@app/api/ApiContext';import type {AdminAuditService} from '@app/api/admin/services/AdminAuditService';import {createGuildIDSet, createUserIDSet, type UserID} from '@app/api/BrandedTypes';+import {Config} from '@app/api/Config';+import type {VoiceJoinTokenRow} from '@app/api/database/types/VoiceTypes';+import type {ISnowflakeService} from '@app/api/infrastructure/ISnowflakeService';+import {REXCTL_TEST_ROOM_PREFIX} from '@app/api/infrastructure/LiveKitWebhookService';+import {getVoiceServerHealth} from '@app/api/middleware/ServiceRegistry';import {VOICE_CONFIGURATION_CHANNEL} from '@app/api/voice/VoiceConstants';+import type {VoiceJoinTokenRepository} from '@app/api/voice/VoiceJoinTokenRepository';import type {VoiceRegionRecord, VoiceRegionWithServers, VoiceServerRecord} from '@app/api/voice/VoiceModel';+import {+ hashJoinSecret,+ joinTokenStatus,+ VOICE_JOIN_TOKEN_PREFIX,+ voiceJoinCommand,+} from '@app/api/voice/VoiceNodeService';import type {VoiceRepository} from '@app/api/voice/VoiceRepository';+import {bundledVoiceServerId} from '@app/api/voice/VoiceServerEndpoints';+import {healthOf, toHealthResponse} from '@app/api/voice/VoiceServerHealth';+import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';import {UnknownVoiceRegionError} from '@fluxer/errors/src/domains/voice/UnknownVoiceRegionError';import {UnknownVoiceServerError} from '@fluxer/errors/src/domains/voice/UnknownVoiceServerError';+import {VoiceJoinTokenInvalidError} from '@fluxer/errors/src/domains/voice/VoiceJoinTokenInvalidError';+import {VoiceServerApiKeyInUseError} from '@fluxer/errors/src/domains/voice/VoiceServerApiKeyInUseError';+import {VoiceServerIdInUseError} from '@fluxer/errors/src/domains/voice/VoiceServerIdInUseError';import type {+ CreateVoiceJoinTokenRequest,+ CreateVoiceJoinTokenResponse,CreateVoiceRegionRequest,CreateVoiceServerRequest,DeleteVoiceRegionRequest,@@ -19,16 +40,22 @@ import type {ListVoiceServersRequest,UpdateVoiceRegionRequest,UpdateVoiceServerRequest,+ VoiceJoinTokenAdminResponse,VoiceRegionAdminResponse,VoiceServerAdminResponse,} from '@fluxer/schema/src/domains/admin/AdminVoiceSchemas';+import {AccessToken, TrackSource} from 'livekit-server-sdk';interface AdminVoiceServiceDeps {apiContext: ApiContext;voiceRepository: VoiceRepository;+ joinTokenRepository: VoiceJoinTokenRepository;+ snowflakeService: ISnowflakeService;auditService: AdminAuditService;}+const TEST_TOKEN_TTL_SECONDS = 60;+interface VoiceRestrictionData {vip_only?: boolean;required_guild_features?: Array<string>;@@ -85,6 +112,7 @@ export class AdminVoiceService {? await voiceRepository.listRegionsWithServers(): await voiceRepository.listRegions();regions.sort((a, b) => a.name.localeCompare(b.name));+ const duplicates = await this.duplicateApiKeys();if (data.include_servers) {const regionsWithServers = regions as Array<VoiceRegionWithServers>;return {@@ -92,7 +120,7 @@ export class AdminVoiceService {...this.mapVoiceRegionToAdminResponse(region),servers: region.servers.sort((a, b) => a.serverId.localeCompare(b.serverId))- .map((server) => this.mapVoiceServerToAdminResponse(server)),+ .map((server) => this.mapVoiceServerToAdminResponse(server, duplicates)),})),};}@@ -112,10 +140,11 @@ export class AdminVoiceService {}if (data.include_servers && 'servers' in region) {const regionWithServers = region as VoiceRegionWithServers;+ const duplicates = await this.duplicateApiKeys();return {region: {...this.mapVoiceRegionToAdminResponse(regionWithServers),- servers: regionWithServers.servers.map((server) => this.mapVoiceServerToAdminResponse(server)),+ servers: regionWithServers.servers.map((server) => this.mapVoiceServerToAdminResponse(server, duplicates)),},};}@@ -206,8 +235,9 @@ export class AdminVoiceService {async listVoiceServers(data: ListVoiceServersRequest) {const {voiceRepository} = this.deps;const servers = await voiceRepository.listServers(data.region_id);+ const duplicates = await this.duplicateApiKeys();return {- servers: servers.map((server) => this.mapVoiceServerToAdminResponse(server)),+ servers: servers.map((server) => this.mapVoiceServerToAdminResponse(server, duplicates)),};}@@ -215,7 +245,7 @@ export class AdminVoiceService {const {voiceRepository} = this.deps;const server = await voiceRepository.getServer(data.region_id, data.server_id);return {- server: server ? this.mapVoiceServerToAdminResponse(server) : null,+ server: server ? this.mapVoiceServerToAdminResponse(server, await this.duplicateApiKeys()) : null,};}@@ -225,6 +255,16 @@ export class AdminVoiceService {if (!region) {throw new UnknownVoiceRegionError();}+ const existing = await voiceRepository.listAllServers();+ if (existing.some((server) => server.regionId === data.region_id && server.serverId === data.server_id)) {+ throw new VoiceServerIdInUseError();+ }+ if (data.server_id === bundledVoiceServerId() && data.api_key !== Config.voice.apiKey) {+ throw new VoiceServerIdInUseError();+ }+ if (existing.some((server) => server.apiKey === data.api_key)) {+ throw new VoiceServerApiKeyInUseError();+ }const server = await voiceRepository.createServer({regionId: data.region_id,serverId: data.server_id,@@ -236,6 +276,10 @@ export class AdminVoiceService {apiSecret: data.api_secret ?? null,latitude: data.latitude ?? null,longitude: data.longitude ?? null,+ internalEndpoint: data.internal_endpoint ?? null,+ draining: data.draining ?? false,+ pinnedHostname: null,+ pinnedSoftLimit: false,restrictions: buildVoiceRestrictions(data),});await this.publishVoiceConfiguration({type: 'server_created', regionId: data.region_id, serverId: data.server_id});@@ -252,7 +296,7 @@ export class AdminVoiceService {]),});return {- server: this.mapVoiceServerToAdminResponse(server),+ server: this.mapVoiceServerToAdminResponse(server, await this.duplicateApiKeys()),};}@@ -264,13 +308,21 @@ export class AdminVoiceService {}const updates: VoiceServerRecord = {...existing};if (data.endpoint !== undefined) updates.endpoint = data.endpoint;- if (data.api_key !== undefined && data.api_key !== '') updates.apiKey = data.api_key;+ if (data.api_key !== undefined && data.api_key !== '' && data.api_key !== existing.apiKey) {+ const servers = await voiceRepository.listAllServers();+ if (servers.some((server) => server.apiKey === data.api_key)) {+ throw new VoiceServerApiKeyInUseError();+ }+ updates.apiKey = data.api_key;+ }if (data.api_secret !== undefined && data.api_secret !== '') updates.apiSecret = data.api_secret;if (data.latitude !== undefined) updates.latitude = data.latitude;if (data.longitude !== undefined) updates.longitude = data.longitude;if (data.is_active !== undefined) updates.isActive = data.is_active;if (data.soft_connection_limit !== undefined) updates.softConnectionLimit = data.soft_connection_limit;if (data.notes !== undefined) updates.notes = data.notes;+ if (data.internal_endpoint !== undefined) updates.internalEndpoint = data.internal_endpoint;+ if (data.draining !== undefined) updates.draining = data.draining;updates.restrictions = patchVoiceRestrictions(existing.restrictions, data);updates.updatedAt = new Date();await voiceRepository.upsertServer(updates);@@ -287,7 +339,7 @@ export class AdminVoiceService {]),});return {- server: this.mapVoiceServerToAdminResponse(updates),+ server: this.mapVoiceServerToAdminResponse(updates, await this.duplicateApiKeys()),};}@@ -332,7 +384,17 @@ export class AdminVoiceService {};}- private mapVoiceServerToAdminResponse(server: VoiceServerRecord): VoiceServerAdminResponse {+ private async duplicateApiKeys(): Promise<Set<string>> {+ const seen = new Set<string>();+ const duplicates = new Set<string>();+ for (const server of await this.deps.voiceRepository.listAllServers()) {+ if (seen.has(server.apiKey)) duplicates.add(server.apiKey);+ seen.add(server.apiKey);+ }+ return duplicates;+ }++ private mapVoiceServerToAdminResponse(server: VoiceServerRecord, duplicates: Set<string>): VoiceServerAdminResponse {const allowedGuildIds = Array.from(server.restrictions.allowedGuildIds).map((id) => id.toString());return {region_id: server.regionId,@@ -347,11 +409,163 @@ export class AdminVoiceService {required_guild_features: Array.from(server.restrictions.requiredGuildFeatures),allowed_guild_ids: allowedGuildIds,allowed_user_ids: Array.from(server.restrictions.allowedUserIds).map((id) => id.toString()),+ internal_endpoint: server.internalEndpoint,+ draining: server.draining,+ health: toHealthResponse(healthOf(getVoiceServerHealth(), server)),+ duplicate_api_key: duplicates.has(server.apiKey),created_at: server.createdAt?.toISOString() ?? null,updated_at: server.updatedAt?.toISOString() ?? null,};}+ async createJoinToken(+ data: CreateVoiceJoinTokenRequest,+ adminUserId: UserID,+ auditLogReason: string | null,+ ): Promise<CreateVoiceJoinTokenResponse> {+ if (data.region_name && !data.region_id) {+ throw new UnknownVoiceRegionError();+ }+ if ((data.region_latitude === null) !== (data.region_longitude === null)) {+ throw InputValidationError.create('region_latitude', 'Pass region_latitude and region_longitude together.');+ }+ if (data.region_latitude !== null && !data.region_name) {+ throw InputValidationError.create(+ 'region_latitude',+ 'Region coordinates only apply to a new region in region_name.',+ );+ }+ if (data.region_id && !data.region_name && !(await this.deps.voiceRepository.getRegion(data.region_id))) {+ throw new UnknownVoiceRegionError();+ }+ if (data.server_id && data.server_id === bundledVoiceServerId()) {+ throw new VoiceServerIdInUseError();+ }+ const tokenId = await this.deps.snowflakeService.generate();+ const secret = randomBytes(32).toString('base64url');+ const now = new Date();+ const row: VoiceJoinTokenRow = {+ token_id: tokenId,+ secret_hash: hashJoinSecret(secret),+ created_by: adminUserId,+ created_at: now,+ expires_at: new Date(now.getTime() + data.expires_in_seconds * 1000),+ claim_id: null,+ claimed_at: null,+ used_at: null,+ used_by_ip: null,+ used_server_id: null,+ revoked_at: null,+ region_id: data.region_id,+ region_name: data.region_name,+ region_emoji: data.region_emoji,+ region_latitude: data.region_latitude,+ region_longitude: data.region_longitude,+ server_id: data.server_id,+ hostname: data.hostname,+ internal_endpoint: data.internal_endpoint,+ soft_connection_limit: data.soft_connection_limit,+ notes: data.notes,+ vip_only: data.vip_only,+ required_guild_features: new Set(data.required_guild_features),+ allowed_guild_ids: new Set(data.allowed_guild_ids.map(BigInt)),+ allowed_user_ids: new Set(data.allowed_user_ids.map(BigInt)),+ };+ await this.deps.joinTokenRepository.create(row);+ await this.deps.auditService.createAuditLog({+ targetId: 0n,+ adminUserId,+ targetType: 'voice_join_token',+ action: 'create_voice_join_token',+ auditLogReason,+ metadata: new Map([+ ['token_id', tokenId.toString()],+ ['region_id', data.region_id ?? ''],+ ]),+ });+ const token = `${VOICE_JOIN_TOKEN_PREFIX}${tokenId}_${secret}`;+ return {+ token_id: tokenId.toString(),+ token,+ expires_at: row.expires_at.toISOString(),+ command: voiceJoinCommand(Config.endpoints.webApp, Config.instance.baseDomain, token),+ };+ }++ async listJoinTokens(): Promise<{tokens: Array<VoiceJoinTokenAdminResponse>}> {+ const rows = await this.deps.joinTokenRepository.list();+ rows.sort((a, b) => b.created_at.getTime() - a.created_at.getTime());+ return {+ tokens: rows.map((row) => ({+ token_id: row.token_id.toString(),+ status: joinTokenStatus(row),+ created_at: row.created_at.toISOString(),+ expires_at: row.expires_at.toISOString(),+ used_at: row.used_at?.toISOString() ?? null,+ used_server_id: row.used_server_id,+ revoked_at: row.revoked_at?.toISOString() ?? null,+ region_id: row.region_id,+ region_name: row.region_name,+ server_id: row.server_id,+ hostname: row.hostname,+ notes: row.notes,+ })),+ };+ }++ async revokeJoinToken(tokenId: bigint, adminUserId: UserID, auditLogReason: string | null) {+ const row = await this.deps.joinTokenRepository.get(tokenId);+ if (!row || joinTokenStatus(row) !== 'pending' || !(await this.deps.joinTokenRepository.revoke(row, new Date()))) {+ throw new VoiceJoinTokenInvalidError();+ }+ await this.deps.auditService.createAuditLog({+ targetId: 0n,+ adminUserId,+ targetType: 'voice_join_token',+ action: 'revoke_voice_join_token',+ auditLogReason,+ metadata: new Map([['token_id', tokenId.toString()]]),+ });+ return {success: true};+ }++ async createServerTestToken(+ data: GetVoiceServerRequest,+ adminUserId: UserID,+ auditLogReason: string | null,+ ): Promise<{token: string; endpoint: string; room: string}> {+ const server = await this.deps.voiceRepository.getServer(data.region_id, data.server_id);+ if (!server) {+ throw new UnknownVoiceServerError();+ }+ const room = `${REXCTL_TEST_ROOM_PREFIX}${randomBytes(6).toString('hex')}`;+ const token = new AccessToken(server.apiKey, server.apiSecret, {+ identity: `rexctl-test-${randomBytes(4).toString('hex')}`,+ ttl: TEST_TOKEN_TTL_SECONDS,+ });+ token.addGrant({+ room,+ roomJoin: true,+ hidden: true,+ canPublish: true,+ canSubscribe: true,+ canPublishData: false,+ canPublishSources: [TrackSource.MICROPHONE],+ });+ await this.deps.auditService.createAuditLog({+ targetId: 0n,+ adminUserId,+ targetType: 'voice_server',+ action: 'create_voice_server_test_token',+ auditLogReason,+ metadata: new Map([+ ['region_id', server.regionId],+ ['server_id', server.serverId],+ ]),+ });+ return {token: await token.toJwt(), endpoint: server.endpoint, room};+ }+private async publishVoiceConfiguration(message: VoiceConfigurationMessage): Promise<void> {await this.deps.apiContext.services.cache.publish(VOICE_CONFIGURATION_CHANNEL, JSON.stringify(message));}diff --git a/fluxer_api/src/api/admin/tests/VoiceAdminController.test.ts b/fluxer_api/src/api/admin/tests/VoiceAdminController.test.tsindex 9de0fdc49..4fb97f728 100644--- a/fluxer_api/src/api/admin/tests/VoiceAdminController.test.ts+++ b/fluxer_api/src/api/admin/tests/VoiceAdminController.test.ts@@ -7,6 +7,7 @@ import {HTTP_STATUS} from '@app/api/test/TestConstants';import {createBuilder} from '@app/api/test/TestRequestBuilder';import {VoiceRepository} from '@app/api/voice/VoiceRepository';import {AdminACLs} from '@fluxer/constants/src/AdminACLs';+import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';import type {CreateVoiceRegionResponse,CreateVoiceServerResponse,@@ -168,4 +169,74 @@ describe('VoiceAdminController', () => {.execute();expect((await voiceRepository.getServer(fixture.regionId, fixture.serverId))?.notes).toBeNull();});+ test('rejects a second server with an api key that is already in use', async () => {+ const admin = await createAdminWithAcls(harness, [+ AdminACLs.VOICE_REGION_CREATE,+ AdminACLs.VOICE_SERVER_CREATE,+ AdminACLs.VOICE_SERVER_UPDATE,+ ]);+ await createVoiceFixture(harness, admin, {+ regionId: 'voice-region-keys',+ serverId: 'voice-server-keys-1',+ endpoint: 'wss://voice-keys-1.example.com/livekit',+ apiKey: 'shared-key',+ apiSecret: 'secret-1',+ });+ await createBuilder(harness, admin.token)+ .post('/admin/voice/regions/voice-region-keys/servers')+ .body({server_id: 'voice-server-keys-2', endpoint: 'wss://b.example.com', api_key: 'shared-key', api_secret: 's'})+ .expect(HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_SERVER_API_KEY_IN_USE)+ .execute();+ await createBuilder(harness, admin.token)+ .post('/admin/voice/regions/voice-region-keys/servers')+ .body({server_id: 'voice-server-keys-1', endpoint: 'wss://b.example.com', api_key: 'other-key', api_secret: 's'})+ .expect(HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_SERVER_ID_IN_USE)+ .execute();+ await createBuilder(harness, admin.token)+ .post('/admin/voice/regions/voice-region-keys/servers')+ .body({server_id: 'voice-server-keys-2', endpoint: 'wss://b.example.com', api_key: 'other-key', api_secret: 's'})+ .execute();+ await createBuilder(harness, admin.token)+ .patch('/admin/voice/regions/voice-region-keys/servers/voice-server-keys-2')+ .body({api_key: 'shared-key'})+ .expect(HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_SERVER_API_KEY_IN_USE)+ .execute();+ await createBuilder(harness, admin.token)+ .patch('/admin/voice/regions/voice-region-keys/servers/voice-server-keys-2')+ .body({api_key: 'other-key', notes: 'same key again is fine'})+ .execute();+ });++ test('flags existing servers that share a key and keeps internal endpoint and draining', async () => {+ const admin = await createAdminWithAcls(harness, [+ AdminACLs.VOICE_REGION_CREATE,+ AdminACLs.VOICE_SERVER_CREATE,+ AdminACLs.VOICE_SERVER_UPDATE,+ AdminACLs.VOICE_SERVER_LIST,+ ]);+ const fixture = await createVoiceFixture(harness, admin, {+ regionId: 'voice-region-flags',+ serverId: 'voice-server-flags-1',+ endpoint: 'wss://voice-flags.example.com/livekit',+ apiKey: 'flag-key',+ apiSecret: 'flag-secret',+ });+ const first = await voiceRepository.getServer(fixture.regionId, fixture.serverId);+ await voiceRepository.upsertServer({...first!, serverId: 'voice-server-flags-2'});+ const updated = await createBuilder<UpdateVoiceServerResponse>(harness, admin.token)+ .patch(`/admin/voice/regions/${fixture.regionId}/servers/${fixture.serverId}`)+ .body({internal_endpoint: 'http://voice-flags:7880', draining: true})+ .execute();+ expect(updated.server).toMatchObject({+ internal_endpoint: 'http://voice-flags:7880',+ draining: true,+ duplicate_api_key: true,+ health: {status: 'unknown', checked_at: null, error: null},+ });+ await createBuilder(harness, admin.token)+ .patch(`/admin/voice/regions/${fixture.regionId}/servers/${fixture.serverId}`)+ .body({internal_endpoint: 'ftp://voice-flags'})+ .expect(HTTP_STATUS.BAD_REQUEST)+ .execute();+ });});diff --git a/fluxer_api/src/api/admin/tests/audit_coverage/VoiceAdminAuditCases.ts b/fluxer_api/src/api/admin/tests/audit_coverage/VoiceAdminAuditCases.tsindex fb70c182c..4921d25ff 100644--- a/fluxer_api/src/api/admin/tests/audit_coverage/VoiceAdminAuditCases.ts+++ b/fluxer_api/src/api/admin/tests/audit_coverage/VoiceAdminAuditCases.ts@@ -5,6 +5,7 @@ import type {TestAccount} from '@app/api/auth/tests/AuthTestUtils';import type {ApiTestHarness} from '@app/api/test/ApiTestHarness';import {HTTP_STATUS} from '@app/api/test/TestConstants';import {createBuilder} from '@app/api/test/TestRequestBuilder';+import {expect} from 'vitest';const REGION_ID = 'coverage-region';const REGION_NAME = 'Coverage Region';@@ -239,4 +240,71 @@ export const VoiceAdminAuditCases: ReadonlyArray<AdminAuditCoverageCase> = [};},},+ {+ method: 'POST',+ route: '/admin/voice/regions/:region_id/servers/:server_id/test-token',+ async prepare({harness, admin}) {+ await createRegionWithServer(harness, admin);+ return {+ request: {path: `/admin/voice/regions/${REGION_ID}/servers/${SERVER_ID}/test-token`},+ expected: {+ action: 'create_voice_server_test_token',+ targetType: 'voice_server',+ targetId: '0',+ metadata: {region_id: REGION_ID, server_id: SERVER_ID},+ },+ };+ },+ },+ {+ method: 'POST',+ route: '/admin/voice/join-tokens',+ async prepare({harness, admin}) {+ await createRegion(harness, admin);+ return {+ request: {path: '/admin/voice/join-tokens', body: {region_id: REGION_ID}},+ expected: {+ action: 'create_voice_join_token',+ targetType: 'voice_join_token',+ targetId: '0',+ metadata: {token_id: expect.any(String), region_id: REGION_ID},+ },+ };+ },+ },+ {+ method: 'GET',+ route: '/admin/voice/join-tokens',+ async prepare() {+ return {+ request: {path: '/admin/voice/join-tokens'},+ expected: {+ action: 'list_voice_join_tokens',+ targetType: 'voice_join_token',+ targetId: '0',+ metadata: {result_count: '0'},+ },+ };+ },+ },+ {+ method: 'DELETE',+ route: '/admin/voice/join-tokens/:token_id',+ async prepare({harness, admin}) {+ const created = await createBuilder<{token_id: string}>(harness, admin.token)+ .post('/admin/voice/join-tokens')+ .body({})+ .expect(HTTP_STATUS.OK)+ .execute();+ return {+ request: {path: `/admin/voice/join-tokens/${created.token_id}`},+ expected: {+ action: 'revoke_voice_join_token',+ targetType: 'voice_join_token',+ targetId: '0',+ metadata: {token_id: created.token_id},+ },+ };+ },+ },];diff --git a/fluxer_api/src/api/app/ControllerRegistry.ts b/fluxer_api/src/api/app/ControllerRegistry.tsindex a01559756..bc52a1d70 100644--- a/fluxer_api/src/api/app/ControllerRegistry.ts+++ b/fluxer_api/src/api/app/ControllerRegistry.ts@@ -40,6 +40,7 @@ import {ThemeController} from '@app/api/theme/ThemeController';import type {HonoApp} from '@app/api/types/HonoEnv';import {UnfurlController} from '@app/api/unfurl/UnfurlController';import {UserController} from '@app/api/user/controllers/UserController';+import {VoiceNodeController} from '@app/api/voice/VoiceNodeController';import {VoiceP2pConnectionReportController} from '@app/api/voice/VoiceP2pConnectionReportController';import {WebhookController} from '@app/api/webhook/WebhookController';@@ -78,6 +79,7 @@ export function registerControllers(routes: HonoApp, config: APIConfig): void {}UserController(routes);VoiceP2pConnectionReportController(routes);+ VoiceNodeController(routes);StoreBillingController(routes);WebhookController(routes);OAuth2Controller(routes);diff --git a/fluxer_api/src/api/config/APIConfig.ts b/fluxer_api/src/api/config/APIConfig.tsindex 8b4c74a4d..5414ed63f 100644--- a/fluxer_api/src/api/config/APIConfig.ts+++ b/fluxer_api/src/api/config/APIConfig.ts@@ -211,6 +211,7 @@ export interface APIConfig {apiSecret?: string;url?: string;internalUrl?: string;+ bundled: boolean;defaultRegion?: {id: string;name: string;@@ -219,6 +220,8 @@ export interface APIConfig {longitude: number;};p2pStunUrls?: Array<string>;+ joinAllowPrivateEndpoints: boolean;+ healthIntervalMs: number;};stripe: {enabled: boolean;diff --git a/fluxer_api/src/api/database/types/VoiceTypes.ts b/fluxer_api/src/api/database/types/VoiceTypes.tsindex 721fc6108..3e76d77b8 100644--- a/fluxer_api/src/api/database/types/VoiceTypes.ts+++ b/fluxer_api/src/api/database/types/VoiceTypes.ts@@ -47,6 +47,10 @@ export interface VoiceServerRow {required_guild_features: Set<string> | null;allowed_guild_ids: Set<bigint> | null;allowed_user_ids: Set<bigint> | null;+ internal_endpoint: string | null;+ draining: boolean | null;+ pinned_hostname: string | null;+ pinned_soft_limit: boolean | null;created_at: Date | null;updated_at: Date | null;}@@ -66,10 +70,70 @@ export const VOICE_SERVER_COLUMNS = ['required_guild_features','allowed_guild_ids','allowed_user_ids',+ 'internal_endpoint',+ 'draining',+ 'pinned_hostname',+ 'pinned_soft_limit','created_at','updated_at',] as const satisfies ReadonlyArray<keyof VoiceServerRow>;+export interface VoiceJoinTokenRow {+ token_id: bigint;+ secret_hash: string;+ created_by: bigint;+ created_at: Date;+ expires_at: Date;+ claim_id: string | null;+ claimed_at: Date | null;+ used_at: Date | null;+ used_by_ip: string | null;+ used_server_id: string | null;+ revoked_at: Date | null;+ region_id: string | null;+ region_name: string | null;+ region_emoji: string | null;+ region_latitude: number | null;+ region_longitude: number | null;+ server_id: string | null;+ hostname: string | null;+ internal_endpoint: string | null;+ soft_connection_limit: number | null;+ notes: string | null;+ vip_only: boolean | null;+ required_guild_features: Set<string> | null;+ allowed_guild_ids: Set<bigint> | null;+ allowed_user_ids: Set<bigint> | null;+}++export const VOICE_JOIN_TOKEN_COLUMNS = [+ 'token_id',+ 'secret_hash',+ 'created_by',+ 'created_at',+ 'expires_at',+ 'claim_id',+ 'claimed_at',+ 'used_at',+ 'used_by_ip',+ 'used_server_id',+ 'revoked_at',+ 'region_id',+ 'region_name',+ 'region_emoji',+ 'region_latitude',+ 'region_longitude',+ 'server_id',+ 'hostname',+ 'internal_endpoint',+ 'soft_connection_limit',+ 'notes',+ 'vip_only',+ 'required_guild_features',+ 'allowed_guild_ids',+ 'allowed_user_ids',+] as const satisfies ReadonlyArray<keyof VoiceJoinTokenRow>;+export interface VoiceP2pConnectionReportRow {user_id: UserID;report_id: bigint;diff --git a/fluxer_api/src/api/infrastructure/LiveKitService.ts b/fluxer_api/src/api/infrastructure/LiveKitService.tsindex 7793725cb..4ce9c76a0 100644--- a/fluxer_api/src/api/infrastructure/LiveKitService.ts+++ b/fluxer_api/src/api/infrastructure/LiveKitService.ts@@ -14,6 +14,7 @@ import {} from '@app/api/infrastructure/ILiveKitService';import {Logger} from '@app/api/Logger';import type {VoiceRegionMetadata, VoiceServerRecord} from '@app/api/voice/VoiceModel';+import {resolveRoomServiceEndpoint, toHttpUrl} from '@app/api/voice/VoiceServerEndpoints';import type {VoiceTopology} from '@app/api/voice/VoiceTopology';import {AccessToken, RoomServiceClient, type TrackInfo, TrackSource} from 'livekit-server-sdk';@@ -49,10 +50,6 @@ export function computeLiveKitPublishSources(permissions: LiveKitPublishPermissireturn sources;}-function toHttpUrl(wsUrl: string): string {- return wsUrl.replace(/^wss:\/\//, 'https://').replace(/^ws:\/\//, 'http://');-}-function createRoomServiceClient(endpoint: string, apiKey: string, apiSecret: string): RoomServiceClient {const httpUrl = toHttpUrl(endpoint);const parsed = new URL(httpUrl);@@ -67,18 +64,6 @@ function createRoomServiceClient(endpoint: string, apiKey: string, apiSecret: streturn client;}-function resolveRoomServiceEndpoint(server: VoiceServerRecord): string {- const defaultRegion = Config.voice.defaultRegion;- const internalUrl = Config.voice.internalUrl;- if (!defaultRegion || !internalUrl) {- return server.endpoint;- }- if (server.regionId !== defaultRegion.id || server.serverId !== `${defaultRegion.id}-server-1`) {- return server.endpoint;- }- return internalUrl;-}-export class LiveKitService extends ILiveKitService {private serverClients: Map<string, Map<string, ServerClientConfig>> = new Map();private topology: VoiceTopology;diff --git a/fluxer_api/src/api/infrastructure/LiveKitWebhookService.ts b/fluxer_api/src/api/infrastructure/LiveKitWebhookService.tsindex f0e52501a..f9c53d162 100644--- a/fluxer_api/src/api/infrastructure/LiveKitWebhookService.ts+++ b/fluxer_api/src/api/infrastructure/LiveKitWebhookService.ts@@ -11,6 +11,15 @@ import type {VoiceTopology} from '@app/api/voice/VoiceTopology';import type {WebhookEvent} from 'livekit-server-sdk';import {TrackSource, WebhookReceiver} from 'livekit-server-sdk';+const WEBHOOK_CLOCK_TOLERANCE_SECONDS = 60;++interface SourceServer {+ regionId: string;+ serverId: string;+}++export const REXCTL_TEST_ROOM_PREFIX = 'rexctl-test-';+interface VoiceWebhookParticipantContext {readonly type: 'dm' | 'guild';readonly channelId: ChannelID;@@ -19,13 +28,7 @@ interface VoiceWebhookParticipantContext {export class LiveKitWebhookService {private receivers: Map<string, WebhookReceiver>;- private serverMap: Map<- string,- {- regionId: string;- serverId: string;- }- >;+ private serversByKey: Map<string, Array<SourceServer>>;constructor(private voiceRoomStore: IVoiceRoomStore,@@ -34,7 +37,7 @@ export class LiveKitWebhookService {private voiceTopology: VoiceTopology,) {this.receivers = new Map();- this.serverMap = new Map();+ this.serversByKey = new Map();this.rebuildReceivers();this.voiceTopology.registerSubscriber(() => this.rebuildReceivers());}@@ -52,7 +55,7 @@ export class LiveKitWebhookService {let lastError: Error | null = null;for (const [apiKey, receiver] of this.receivers.entries()) {try {- const event = await receiver.receive(body, authHeader);+ const event = await receiver.receive(body, authHeader, false, WEBHOOK_CLOCK_TOLERANCE_SECONDS);return {event: event as WebhookEvent, apiKey};} catch (error) {lastError = error as Error;@@ -106,23 +109,19 @@ export class LiveKitWebhookService {private rebuildReceivers(): void {const newReceivers = new Map<string, WebhookReceiver>();- const newServerMap = new Map<- string,- {- regionId: string;- serverId: string;- }- >();+ const newServersByKey = new Map<string, Array<SourceServer>>();const regions = this.voiceTopology.getAllRegions();for (const region of regions) {const servers = this.voiceTopology.getServersForRegion(region.id);for (const server of servers) {newReceivers.set(server.apiKey, new WebhookReceiver(server.apiKey, server.apiSecret));- newServerMap.set(server.apiKey, {regionId: region.id, serverId: server.serverId});+ const sharing = newServersByKey.get(server.apiKey) ?? [];+ sharing.push({regionId: region.id, serverId: server.serverId});+ newServersByKey.set(server.apiKey, sharing);}}this.receivers = newReceivers;- this.serverMap = newServerMap;+ this.serversByKey = newServersByKey;Logger.debug({regionCount: regions.length,@@ -132,6 +131,44 @@ export class LiveKitWebhookService {);}+ private resolveSource(+ apiKey: string,+ raw: {region_id?: string; server_id?: string} | undefined,+ ): SourceServer | undefined {+ const servers = this.serversByKey.get(apiKey) ?? [];+ if (servers.length === 1) {+ return servers[0];+ }+ if (!raw?.region_id || !raw.server_id) {+ return undefined;+ }+ return servers.find((server) => server.regionId === raw.region_id && server.serverId === raw.server_id);+ }++ private async isFromPinnedServer(+ guildId: GuildID | undefined,+ channelId: ChannelID,+ source: SourceServer | undefined,+ ): Promise<boolean> {+ if (!source) {+ return false;+ }+ const pinned = await this.voiceRoomStore.getPinnedRoomServer(guildId, channelId);+ if (pinned && (pinned.regionId !== source.regionId || pinned.serverId !== source.serverId)) {+ Logger.debug(+ {+ guildId: guildId?.toString(),+ channelId: channelId.toString(),+ sourceServer: source.serverId,+ pinnedServer: pinned.serverId,+ },+ 'Ignoring LiveKit webhook from a server the room is not pinned to',+ );+ return false;+ }+ return true;+ }+async handleRoomFinished(event: WebhookEvent, apiKey: string): Promise<void> {if (event.event !== 'room_finished' || !event.room) {return;@@ -142,54 +179,34 @@ export class LiveKitWebhookService {Logger.warn({roomName}, 'Unknown room name format');return;}+ const guildId = isDMRoom(context) ? undefined : context.guildId;Logger.debug(- {- roomName,- contextType: context.type,- guildId: isDMRoom(context) ? undefined : context.guildId.toString(),- channelId: context.channelId.toString(),- },+ {roomName, contextType: context.type, guildId: guildId?.toString(), channelId: context.channelId.toString()},'Processing LiveKit room_finished event',);- const sourceServer = this.serverMap.get(apiKey);- if (isDMRoom(context)) {- const pinned = await this.voiceRoomStore.getPinnedRoomServer(undefined, context.channelId);- if (pinned && sourceServer && pinned.serverId !== sourceServer.serverId) {- Logger.debug(- {- channelId: context.channelId.toString(),- finishedServer: sourceServer.serverId,- pinnedServer: pinned.serverId,- },- 'Ignoring room_finished from stale server — room has moved to a different server',- );- return;- }- await this.voiceRoomStore.deleteRoomServer(undefined, context.channelId);- Logger.debug({channelId: context.channelId.toString()}, 'Cleared DM voice room server pinning');- } else {- const pinned = await this.voiceRoomStore.getPinnedRoomServer(context.guildId, context.channelId);- if (pinned && sourceServer && pinned.serverId !== sourceServer.serverId) {- Logger.debug(- {- guildId: context.guildId.toString(),- channelId: context.channelId.toString(),- finishedServer: sourceServer.serverId,- pinnedServer: pinned.serverId,- },- 'Ignoring room_finished from stale server — room has moved to a different server',- );- return;- }- await this.voiceRoomStore.deleteRoomServer(context.guildId, context.channelId);+ const servers = this.serversByKey.get(apiKey) ?? [];+ if (servers.length === 0) {+ return;+ }+ const pinned = await this.voiceRoomStore.getPinnedRoomServer(guildId, context.channelId);+ if (+ pinned &&+ !servers.some((server) => server.regionId === pinned.regionId && server.serverId === pinned.serverId)+ ) {Logger.debug(- {guildId: context.guildId.toString(), channelId: context.channelId.toString()},- 'Cleared guild voice room server pinning',+ {guildId: guildId?.toString(), channelId: context.channelId.toString(), pinnedServer: pinned.serverId},+ 'Ignoring LiveKit webhook from a server the room is not pinned to',);+ return;}+ await this.voiceRoomStore.deleteRoomServer(guildId, context.channelId);+ Logger.debug(+ {guildId: guildId?.toString(), channelId: context.channelId.toString()},+ 'Cleared voice room server pinning',+ );}- private async handleParticipantJoined(event: WebhookEvent): Promise<void> {+ private async handleParticipantJoined(event: WebhookEvent, apiKey: string): Promise<void> {if (event.event !== 'participant_joined') {return;}@@ -205,6 +222,17 @@ export class LiveKitWebhookService {}const {context, raw} = parsed;const tokenNonce = raw.token_nonce;+ const source = this.resolveSource(apiKey, raw);+ if (+ !source ||+ !(await this.isFromPinnedServer(+ context.type === 'guild' ? context.guildId : undefined,+ context.channelId,+ source,+ ))+ ) {+ return;+ }Logger.debug({type: context.type,@@ -278,8 +306,8 @@ export class LiveKitWebhookService {channelId: context.channelId,userId: context.userId,connectionId: context.connectionId,- regionId: raw.region_id ?? '',- serverId: raw.server_id ?? '',+ regionId: source.regionId,+ serverId: source.serverId,});} catch (disconnectError) {Logger.error({error: disconnectError}, 'Failed to disconnect rejected participant');@@ -294,23 +322,11 @@ export class LiveKitWebhookService {private async isParticipantStillInRoom(params: {participantIdentity: string;context: VoiceWebhookParticipantContext;- regionId?: string;- serverId?: string;+ source: SourceServer;}): Promise<'present' | 'absent' | 'unknown'> {const {participantIdentity, context} = params;const guildId = context.type === 'guild' ? context.guildId : undefined;- let regionId = params.regionId;- let serverId = params.serverId;- if (!regionId || !serverId) {- const pinnedServer = await this.voiceRoomStore.getPinnedRoomServer(guildId, context.channelId);- if (pinnedServer) {- regionId = pinnedServer.regionId;- serverId = pinnedServer.serverId;- }- }- if (!regionId || !serverId) {- return 'unknown';- }+ const {regionId, serverId} = params.source;const result = await this.liveKitService.listParticipants({guildId,channelId: context.channelId,@@ -327,7 +343,7 @@ export class LiveKitWebhookService {return result.participants.some((p) => p.identity === participantIdentity) ? 'present' : 'absent';}- private async handleParticipantLeft(event: WebhookEvent): Promise<void> {+ private async handleParticipantLeft(event: WebhookEvent, apiKey: string): Promise<void> {if (event.event !== 'participant_left' && event.event !== 'participant_connection_aborted') {return;}@@ -342,6 +358,7 @@ export class LiveKitWebhookService {return;}const {context, raw} = parsed;+ const source = this.resolveSource(apiKey, raw);Logger.debug({type: context.type,@@ -355,32 +372,14 @@ export class LiveKitWebhookService {`Processing LiveKit ${event.event} event`,);try {- if (raw.region_id && raw.server_id) {- const guildId = context.type === 'guild' ? context.guildId : undefined;- const pinnedServer = await this.voiceRoomStore.getPinnedRoomServer(guildId, context.channelId);- if (pinnedServer && (pinnedServer.regionId !== raw.region_id || pinnedServer.serverId !== raw.server_id)) {- Logger.debug(- {- type: context.type,- participantIdentity: participant.identity,- channelId: context.channelId.toString(),- guildId: context.type === 'guild' ? context.guildId.toString() : undefined,- connectionId: context.connectionId,- eventRegionId: raw.region_id,- eventServerId: raw.server_id,- currentRegionId: pinnedServer.regionId,- currentServerId: pinnedServer.serverId,- },- 'Ignoring participant_left from stale server - room has migrated to a different server',- );- return;- }+ const guildIdForPin = context.type === 'guild' ? context.guildId : undefined;+ if (!source || !(await this.isFromPinnedServer(guildIdForPin, context.channelId, source))) {+ return;}const presenceStatus = await this.isParticipantStillInRoom({participantIdentity: participant.identity,context,- regionId: raw.region_id,- serverId: raw.server_id,+ source,});if (presenceStatus === 'present') {Logger.warn(@@ -441,7 +440,7 @@ export class LiveKitWebhookService {}}- private async handleTrackPublished(event: WebhookEvent): Promise<void> {+ private async handleTrackPublished(event: WebhookEvent, apiKey: string): Promise<void> {if (event.event !== 'track_published') {return;}@@ -458,11 +457,9 @@ export class LiveKitWebhookService {return;}const {context, raw} = parsed;- if (!raw.region_id || !raw.server_id) {- Logger.warn(- {participantIdentity: participant.identity, trackSid: track.sid},- 'Cannot mute published microphone without a region and server',- );+ const source = this.resolveSource(apiKey, raw);+ const guildId = context.type === 'guild' ? context.guildId : undefined;+ if (!source || !(await this.isFromPinnedServer(guildId, context.channelId, source))) {return;}Logger.debug(@@ -470,12 +467,12 @@ export class LiveKitWebhookService {'Muting microphone published while server muted',);await this.liveKitService.muteMicrophoneTrack({- guildId: context.type === 'guild' ? context.guildId : undefined,+ guildId,channelId: context.channelId,userId: context.userId,connectionId: context.connectionId,- regionId: raw.region_id,- serverId: raw.server_id,+ regionId: source.regionId,+ serverId: source.serverId,trackSid: track.sid,});}@@ -483,19 +480,22 @@ export class LiveKitWebhookService {async processEvent(data: {event: WebhookEvent; apiKey: string}): Promise<void> {const {event, apiKey} = data;Logger.debug({event: event.event, apiKey}, 'Dispatching LiveKit webhook event');+ if (event.room?.name.startsWith(REXCTL_TEST_ROOM_PREFIX)) {+ return;+ }switch (event.event) {case 'participant_joined':- await this.handleParticipantJoined(event);+ await this.handleParticipantJoined(event, apiKey);break;case 'participant_left':case 'participant_connection_aborted':- await this.handleParticipantLeft(event);+ await this.handleParticipantLeft(event, apiKey);break;case 'room_finished':await this.handleRoomFinished(event, apiKey);break;case 'track_published':- await this.handleTrackPublished(event);+ await this.handleTrackPublished(event, apiKey);break;default:Logger.debug({event: event.event}, 'Ignoring LiveKit webhook event');diff --git a/fluxer_api/src/api/infrastructure/tests/LiveKitWebhookPinning.test.ts b/fluxer_api/src/api/infrastructure/tests/LiveKitWebhookPinning.test.tsnew file mode 100644index 000000000..0819e1803--- /dev/null+++ b/fluxer_api/src/api/infrastructure/tests/LiveKitWebhookPinning.test.ts@@ -0,0 +1,194 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {createHash, createHmac} from 'node:crypto';+import type {IGatewayService} from '@app/api/infrastructure/IGatewayService';+import type {ILiveKitService} from '@app/api/infrastructure/ILiveKitService';+import type {IVoiceRoomStore} from '@app/api/infrastructure/IVoiceRoomStore';+import {LiveKitWebhookService} from '@app/api/infrastructure/LiveKitWebhookService';+import type {VoiceRegionRecord, VoiceServerRecord} from '@app/api/voice/VoiceModel';+import type {VoiceTopology} from '@app/api/voice/VoiceTopology';+import type {WebhookEvent} from 'livekit-server-sdk';+import {describe, expect, it} from 'vitest';++const GUILD = '100';+const CHANNEL = '200';+const ROOM = `guild_${GUILD}_channel_${CHANNEL}`;++function server(serverId: string, apiKey: string): VoiceServerRecord {+ return {+ regionId: 'fra',+ serverId,+ endpoint: `wss://${serverId}.example.com`,+ apiKey,+ apiSecret: `${apiKey}-secret`,+ latitude: null,+ longitude: null,+ isActive: true,+ softConnectionLimit: null,+ notes: null,+ internalEndpoint: null,+ pinnedHostname: null,+ pinnedSoftLimit: false,+ draining: false,+ restrictions: {+ vipOnly: false,+ requiredGuildFeatures: new Set(),+ allowedGuildIds: new Set(),+ allowedUserIds: new Set(),+ },+ createdAt: null,+ updatedAt: null,+ };+}++function setup(servers: Array<VoiceServerRecord> = [server('pinned', 'key-pinned'), server('other', 'key-other')]) {+ const calls: Array<string> = [];+ let pinned: {regionId: string; serverId: string} | null = {regionId: 'fra', serverId: 'pinned'};+ const topology = {+ getAllRegions: () => [{id: 'fra'} as VoiceRegionRecord],+ getServersForRegion: () => servers,+ registerSubscriber: () => {},+ } as unknown as VoiceTopology;+ const roomStore = {+ getPinnedRoomServer: async () => pinned,+ deleteRoomServer: async () => {+ calls.push('delete_pin');+ pinned = null;+ },+ pinRoomServer: async () => {},+ } as unknown as IVoiceRoomStore;+ const gateway = {+ confirmVoiceConnection: async () => {+ calls.push('confirm');+ return {success: true};+ },+ disconnectVoiceUserIfInChannel: async () => {+ calls.push('disconnect');+ return {success: true};+ },+ } as unknown as IGatewayService;+ const liveKit = {+ listParticipants: async ({serverId}: {serverId: string}) => {+ calls.push(`list:${serverId}`);+ return {status: 'ok', participants: []};+ },+ disconnectParticipant: async () => calls.push('kick'),+ muteMicrophoneTrack: async () => calls.push('mute'),+ } as unknown as ILiveKitService;+ const service = new LiveKitWebhookService(roomStore, gateway, liveKit, topology);+ return {service, calls};+}++function participantEvent(event: string, metadataServer: string): WebhookEvent {+ return {+ event,+ room: {name: ROOM},+ participant: {+ identity: 'user_1_conn',+ metadata: JSON.stringify({+ user_id: '1',+ channel_id: CHANNEL,+ guild_id: GUILD,+ connection_id: 'conn',+ token_nonce: 'nonce',+ issued_at: '1',+ region_id: 'fra',+ server_id: metadataServer,+ }),+ attributes: {},+ },+ } as unknown as WebhookEvent;+}++describe('LiveKit webhooks from a server the room is not pinned to', () => {+ it('ignores forged participant events and room_finished', async () => {+ const {service, calls} = setup();+ await service.processEvent({event: participantEvent('participant_left', 'pinned'), apiKey: 'key-other'});+ await service.processEvent({event: participantEvent('participant_joined', 'pinned'), apiKey: 'key-other'});+ await service.processEvent({+ event: {event: 'room_finished', room: {name: ROOM}} as WebhookEvent,+ apiKey: 'key-other',+ });+ expect(calls).toEqual([]);+ });++ it('acts on events from the pinned server and checks presence on that server', async () => {+ const {service, calls} = setup();+ await service.processEvent({event: participantEvent('participant_joined', 'other'), apiKey: 'key-pinned'});+ await service.processEvent({event: participantEvent('participant_left', 'other'), apiKey: 'key-pinned'});+ await service.processEvent({+ event: {event: 'room_finished', room: {name: ROOM}} as WebhookEvent,+ apiKey: 'key-pinned',+ });+ expect(calls).toEqual(['confirm', 'list:pinned', 'disconnect', 'delete_pin']);+ });++ it('ignores rooms made by rexctl voice test', async () => {+ const {service, calls} = setup();+ await service.processEvent({+ event: {event: 'room_finished', room: {name: 'rexctl-test-abc'}} as WebhookEvent,+ apiKey: 'key-pinned',+ });+ expect(calls).toEqual([]);+ });+});++describe('LiveKit webhooks from servers that share an API key', () => {+ const shared = () => setup([server('pinned', 'fluxer'), server('other', 'fluxer')]);++ it('clears the voice state when a participant leaves the server that is not last in the list', async () => {+ const {service, calls} = shared();+ await service.processEvent({event: participantEvent('participant_left', 'pinned'), apiKey: 'fluxer'});+ expect(calls).toEqual(['list:pinned', 'disconnect']);+ });++ it('confirms joins on either server', async () => {+ const {service, calls} = shared();+ await service.processEvent({event: participantEvent('participant_joined', 'pinned'), apiKey: 'fluxer'});+ expect(calls).toEqual(['confirm']);+ });++ it('ignores events whose metadata points at a server the room is not pinned to', async () => {+ const {service, calls} = shared();+ await service.processEvent({event: participantEvent('participant_left', 'other'), apiKey: 'fluxer'});+ expect(calls).toEqual([]);+ });++ it('ignores metadata that points at a server with another key', async () => {+ const {service, calls} = setup([server('pinned', 'fluxer'), server('other', 'fluxer'), server('third', 'own')]);+ await service.processEvent({event: participantEvent('participant_left', 'third'), apiKey: 'fluxer'});+ expect(calls).toEqual([]);+ });++ it('clears the pin when the pinned server finishes the room', async () => {+ const {service, calls} = shared();+ await service.processEvent({event: {event: 'room_finished', room: {name: ROOM}} as WebhookEvent, apiKey: 'fluxer'});+ expect(calls).toEqual(['delete_pin']);+ });+});++describe('LiveKit webhook signatures', () => {+ function signed(body: string, offsetSeconds: number): string {+ const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url');+ const now = Math.floor(Date.now() / 1000) + offsetSeconds;+ const claims = {+ iss: 'key-pinned',+ nbf: now - 10,+ exp: now + 60,+ sha256: createHash('sha256').update(body).digest('base64'),+ };+ const input = `${encode({alg: 'HS256', typ: 'JWT'})}.${encode(claims)}`;+ return `${input}.${createHmac('sha256', 'key-pinned-secret').update(input).digest('base64url')}`;+ }++ it('accepts a voice machine whose clock is up to a minute off', async () => {+ const {service} = setup();+ const body = JSON.stringify({event: 'room_started', id: 'EV_1', room: {name: ROOM}});+ for (const offset of [45, -45]) {+ await expect(service.verifyAndParse(body, signed(body, offset))).resolves.toMatchObject({apiKey: 'key-pinned'});+ }+ for (const offset of [180, -180]) {+ await expect(service.verifyAndParse(body, signed(body, offset))).rejects.toThrow();+ }+ });+});diff --git a/fluxer_api/src/api/instance/InstanceController.ts b/fluxer_api/src/api/instance/InstanceController.tsindex f1e6a669d..1caa2f2a1 100644--- a/fluxer_api/src/api/instance/InstanceController.ts+++ b/fluxer_api/src/api/instance/InstanceController.ts@@ -14,11 +14,13 @@ import {assertSetupToken} from '@app/api/instance/SetupToken';import {getStatusPageDetector} from '@app/api/instance/StatusPageDetector';import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';+import {getVoiceTopology} from '@app/api/middleware/ServiceRegistry';import {RateLimitConfigs} from '@app/api/RateLimitConfig';import {isBillingActive, isPremiumTieringActive, isStripeServiceable} from '@app/api/stripe/BillingConfigCache';import type {HonoEnv} from '@app/api/types/HonoEnv';import {requireConfiguredClientIp} from '@app/api/utils/RequestClientIp';import {Validator} from '@app/api/Validator';+import {endpointOrigin} from '@app/api/voice/VoiceServerEndpoints';import {type EmailDeliveryState,EmailDeliveryStates,@@ -139,6 +141,19 @@ function buildDiscoveryStaticInput(};}+function listVoiceOrigins(): Array<string> {+ const topology = getVoiceTopology();+ if (!topology) return [];+ const origins = new Set<string>();+ for (const region of topology.getAllRegions()) {+ for (const server of topology.getServersForRegion(region.id)) {+ const origin = endpointOrigin(server.endpoint);+ if (origin) origins.add(origin);+ }+ }+ return Array.from(origins).sort();+}+function mapGuildBadgesForDiscovery(badges: ReadonlyArray<CustomBadgeDefinition>): Array<InstanceGuildBadge> {return badges.filter((badge) => badge.type === 'guild')@@ -247,6 +262,7 @@ export function InstanceController(app: Hono<HonoEnv>) {notice,domain_migration: toDomainMigrationDiscovery(domainMigration),guild_badges: mapGuildBadgesForDiscovery(customBadges),+ voice_origins: listVoiceOrigins(),};discoveryValidators = nextDiscoveryValidators(response, discoveryValidators);ctx.header('ETag', discoveryValidators.etag);diff --git a/fluxer_api/src/api/middleware/ServiceRegistry.ts b/fluxer_api/src/api/middleware/ServiceRegistry.tsindex 599857799..57f97fdbc 100644--- a/fluxer_api/src/api/middleware/ServiceRegistry.ts+++ b/fluxer_api/src/api/middleware/ServiceRegistry.ts@@ -23,6 +23,7 @@ import {singleton} from '@app/api/middleware/Singleton';import {readOptionalEnv, readOptionalIntegerEnv} from '@app/api/utils/IntegerOptions';import {VoiceAvailabilityService} from '@app/api/voice/VoiceAvailabilityService';import {VoiceRepository} from '@app/api/voice/VoiceRepository';+import {VoiceServerHealthMonitor} from '@app/api/voice/VoiceServerHealth';import {VoiceServerLoadTracker} from '@app/api/voice/VoiceServerLoad';import {VoiceTopology} from '@app/api/voice/VoiceTopology';import type {WorkerTaskName} from '@app/api/worker/WorkerLaneConfig';@@ -224,6 +225,7 @@ interface VoiceResources {readonly availability: VoiceAvailabilityService | null;readonly liveKit: ILiveKitService;readonly roomStore: IVoiceRoomStore;+ readonly health: VoiceServerHealthMonitor | null;}class VoiceResourceSession {@@ -263,6 +265,7 @@ class VoiceResourceSession {}shutdown(): Promise<void> {+ this.current?.health?.stop();this.current = null;this.stopCompletion ??= this.close();return this.stopCompletion;@@ -284,17 +287,23 @@ function createVoiceSession(): VoiceResourceSession {availability: null,liveKit: new DisabledLiveKitService(),roomStore: new InMemoryVoiceRoomStore(),+ health: null,}));}const kvClient = getKVClient();const gatewayService = getGatewayService();const topology = new VoiceTopology(new VoiceRepository(), kvClient);- return new VoiceResourceSession(topology, () => ({- topology,- availability: new VoiceAvailabilityService(topology, new VoiceServerLoadTracker({gatewayService})),- liveKit: new LiveKitService(topology),- roomStore: new VoiceRoomStore(kvClient),- }));+ return new VoiceResourceSession(topology, () => {+ const health = new VoiceServerHealthMonitor(topology);+ health.start();+ return {+ topology,+ availability: new VoiceAvailabilityService(topology, new VoiceServerLoadTracker({gatewayService}), health),+ liveKit: new LiveKitService(topology),+ roomStore: new VoiceRoomStore(kvClient),+ health,+ };+ });}export async function ensureVoiceResourcesInitialized(): Promise<void> {@@ -319,6 +328,10 @@ export function getVoiceTopology(): VoiceTopology | null {return voiceSession?.resources?.topology ?? null;}+export function getVoiceServerHealth(): VoiceServerHealthMonitor | null {+ return voiceSession?.resources?.health ?? null;+}+export function getVoiceAvailabilityService(): VoiceAvailabilityService | null {return voiceSession?.resources?.availability ?? null;}diff --git a/fluxer_api/src/api/middleware/UserMiddleware.ts b/fluxer_api/src/api/middleware/UserMiddleware.tsindex 39dbb6af0..86c9b8623 100644--- a/fluxer_api/src/api/middleware/UserMiddleware.ts+++ b/fluxer_api/src/api/middleware/UserMiddleware.ts@@ -23,6 +23,9 @@ interface ParsedAuthHeader {const SKIP_PATHS = new Set(['/_health','/webhooks/livekit',+ '/voice/join/preview',+ '/voice/join',+ '/voice/node','/webhooks/sweego','/webhooks/app-store','/webhooks/google-play',diff --git a/fluxer_api/src/api/openapi/openapi.json b/fluxer_api/src/api/openapi/openapi.jsonindex 3eabf283f..0d509fbf2 100644--- a/fluxer_api/src/api/openapi/openapi.json+++ b/fluxer_api/src/api/openapi/openapi.json@@ -24473,6 +24473,231 @@}}},+ "/voice/join": {+ "post": {+ "operationId": "create_voice_join",+ "summary": "Join a voice server to the instance",+ "tags": ["Voice"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/VoiceJoinResponse"}}}+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Registers a new voice server with a join token. Send Authorization: VoiceJoin with the token. Fluxer checks that the endpoint answers and that the key and secret match before it saves the server, then the token stops working.",+ "requestBody": {+ "required": true,+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/VoiceJoinRequest"}}}+ }+ }+ },+ "/voice/join/preview": {+ "post": {+ "operationId": "preview_voice_join",+ "summary": "Preview joining a voice server",+ "tags": ["Voice"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/VoiceJoinPreviewResponse"}}}+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Shows the server id, region, coordinates and webhook URL a new voice server would get from a join token. Send Authorization: VoiceJoin with the token. The call changes nothing and does not use the token.",+ "requestBody": {+ "required": false,+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/VoiceJoinPreviewRequest"}}}+ }+ }+ },+ "/voice/node": {+ "get": {+ "operationId": "get_voice_node",+ "summary": "Get the calling voice server",+ "tags": ["Voice"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/VoiceNodeResponse"}}}+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Returns the voice server that signed the request, with its health as the API sees it. Send Authorization: Bearer with an HS256 JWT signed with the API secret of the server, with iss set to its API key, aud set to fluxer-voice-node, method and path of the request, an exp at most 5 minutes ahead and sha256 holding the base64 SHA-256 of the body, or an empty string for a request without a body."+ },+ "patch": {+ "operationId": "update_voice_node",+ "summary": "Update the calling voice server",+ "tags": ["Voice"],+ "responses": {+ "200": {+ "description": "Success",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/VoiceNodeResponse"}}}+ },+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Changes the endpoint, draining flag, coordinates or soft connection limit of the voice server that signed the request. A new endpoint is checked before it is saved. Send Authorization: Bearer with an HS256 JWT signed with the API secret of the server, with iss set to its API key, aud set to fluxer-voice-node, method and path of the request, an exp at most 5 minutes ahead and sha256 holding the base64 SHA-256 of the body, or an empty string for a request without a body.",+ "requestBody": {+ "required": true,+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UpdateVoiceNodeRequest"}}}+ }+ },+ "delete": {+ "operationId": "delete_voice_node",+ "summary": "Remove the calling voice server",+ "tags": ["Voice"],+ "responses": {+ "204": {"description": "No Content"},+ "400": {+ "description": "Bad Request - The request was malformed or contained invalid data",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ },+ "429": {+ "description": "Too Many Requests - You are being rate limited",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ThrottledError"}}},+ "headers": {+ "Retry-After": {+ "description": "Number of seconds to wait before retrying (only on 429)",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Limit": {+ "description": "The number of requests that can be made in the current window",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Remaining": {+ "description": "The number of remaining requests that can be made",+ "schema": {"type": "integer"}+ },+ "X-RateLimit-Reset": {+ "description": "Unix timestamp when the rate limit resets",+ "schema": {"type": "integer"}+ }+ }+ },+ "500": {+ "description": "Internal Server Error - An unexpected error occurred",+ "content": {"application/json": {"schema": {"$ref": "#/components/schemas/Error"}}}+ }+ },+ "description": "Deletes the voice server that signed the request. Send Authorization: Bearer with an HS256 JWT signed with the API secret of the server, with iss set to its API key, aud set to fluxer-voice-node, method and path of the request, an exp at most 5 minutes ahead and sha256 holding the base64 SHA-256 of the body, or an empty string for a request without a body."+ }+ },"/voice/p2p/connection-reports": {"post": {"operationId": "create_voice_p2p_connection_reports",@@ -27025,6 +27250,203 @@},"required": ["reports"]},+ "UpdateVoiceNodeRequest": {+ "type": "object",+ "properties": {+ "endpoint": {"description": "New public wss endpoint", "type": "string", "maxLength": 512, "format": "uri"},+ "draining": {"description": "Whether placement sends new calls elsewhere", "type": "boolean"},+ "latitude": {"anyOf": [{"type": "number", "minimum": -90, "maximum": 90}, {"type": "null"}]},+ "longitude": {"anyOf": [{"type": "number", "minimum": -180, "maximum": 180}, {"type": "null"}]},+ "soft_connection_limit": {+ "anyOf": [{"type": "integer", "minimum": 1, "maximum": 2147483647}, {"type": "null"}]+ }+ },+ "additionalProperties": false+ },+ "VoiceNodeResponse": {+ "type": "object",+ "properties": {+ "region_id": {"type": "string", "description": "Region of the server"},+ "server_id": {"type": "string", "description": "ID of the server"},+ "endpoint": {"type": "string", "description": "Endpoint of the server"},+ "latitude": {"description": "Server latitude", "type": ["number", "null"]},+ "longitude": {"description": "Server longitude", "type": ["number", "null"]},+ "is_active": {"type": "boolean", "description": "Whether an admin keeps the server turned on"},+ "draining": {"type": "boolean", "description": "Whether placement sends new calls elsewhere"},+ "soft_connection_limit": {"description": "Soft connection limit, or null", "type": ["number", "null"]},+ "health": {+ "description": "Health of the server as this API node last saw it",+ "$ref": "#/components/schemas/VoiceServerHealthResponse"+ }+ },+ "required": [+ "region_id",+ "server_id",+ "endpoint",+ "latitude",+ "longitude",+ "is_active",+ "draining",+ "soft_connection_limit",+ "health"+ ],+ "additionalProperties": false+ },+ "VoiceJoinPreviewRequest": {+ "type": "object",+ "properties": {+ "public_ipv4": {+ "description": "Public IPv4 address of the voice machine",+ "anyOf": [+ {+ "type": "string",+ "format": "ipv4",+ "pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$"+ },+ {"type": "null"}+ ]+ },+ "region_id": {+ "description": "Existing region the operator asked for",+ "type": "string",+ "minLength": 1,+ "maxLength": 64,+ "pattern": "^[a-z0-9][a-z0-9-]*$"+ },+ "cpu_count": {+ "description": "CPU count of the voice machine",+ "type": "integer",+ "minimum": 1,+ "maximum": 4096+ },+ "memory_bytes": {+ "description": "Memory of the voice machine in bytes",+ "type": "integer",+ "minimum": 0,+ "maximum": 9007199254740991+ },+ "rexctl_version": {"description": "Version of rexctl on the voice machine", "type": "string", "maxLength": 64}+ }+ },+ "VoiceJoinPreviewResponse": {+ "type": "object",+ "properties": {+ "server_id": {"type": "string", "description": "Suggested server id, or the preset of the token"},+ "server_id_pinned": {+ "type": "boolean",+ "description": "Whether the token sets the server id, which the join then always uses"+ },+ "region": {+ "type": "object",+ "properties": {+ "id": {"type": "string", "description": "Region id"},+ "name": {"type": "string", "description": "Region name"},+ "distance_km": {+ "description": "Distance from the voice machine, or null without GeoIP data",+ "type": ["number", "null"]+ },+ "new": {"type": "boolean", "description": "Whether the region is created when the server joins"},+ "source": {+ "type": "string",+ "enum": ["token", "requested", "nearest", "default"],+ "description": "Why Fluxer picked the region, from the token, region_id, the nearest region or the default region"+ }+ },+ "required": ["id", "name", "distance_km", "new", "source"],+ "additionalProperties": false+ },+ "latitude": {"description": "Server latitude from GeoIP, rounded to 0.1 degrees", "type": ["number", "null"]},+ "longitude": {+ "description": "Server longitude from GeoIP, rounded to 0.1 degrees",+ "type": ["number", "null"]+ },+ "hostname": {"description": "Hostname preset of the token, or null", "type": ["string", "null"]},+ "suggested_hostname": {+ "type": "string",+ "description": "A hostname under the instance domain the operator may use"+ },+ "soft_connection_limit": {+ "description": "Soft limit preset of the token, or null",+ "type": ["number", "null"]+ },+ "webhook_url": {"type": "string", "description": "URL the server sends LiveKit webhooks to"},+ "api_code_version": {+ "type": "integer",+ "minimum": -9007199254740991,+ "maximum": 9007199254740991,+ "description": "Version of the API server code"+ },+ "release": {+ "type": "string",+ "description": "Fluxer release of the API, or an empty string for a development build"+ },+ "expires_at": {"type": "string", "description": "ISO 8601 timestamp when the token stops working"}+ },+ "required": [+ "server_id",+ "server_id_pinned",+ "region",+ "latitude",+ "longitude",+ "hostname",+ "suggested_hostname",+ "soft_connection_limit",+ "webhook_url",+ "api_code_version",+ "release",+ "expires_at"+ ],+ "additionalProperties": false+ },+ "VoiceJoinRequest": {+ "type": "object",+ "properties": {+ "server_id": {+ "description": "Server id, ignored when the token has a preset",+ "type": "string",+ "minLength": 1,+ "maxLength": 64,+ "pattern": "^[a-z0-9][a-z0-9-]*$"+ },+ "region_id": {+ "description": "Existing region, ignored when the token has a preset",+ "type": "string",+ "minLength": 1,+ "maxLength": 64,+ "pattern": "^[a-z0-9][a-z0-9-]*$"+ },+ "endpoint": {+ "type": "string",+ "maxLength": 512,+ "format": "uri",+ "description": "Public wss endpoint of the server"+ },+ "api_key": {"description": "LiveKit API key of the server", "type": "string"},+ "api_secret": {"description": "LiveKit API secret of the server", "type": "string"},+ "latitude": {"type": "number", "minimum": -90, "maximum": 90},+ "longitude": {"type": "number", "minimum": -180, "maximum": 180},+ "soft_connection_limit": {"type": "integer", "minimum": 1, "maximum": 2147483647},+ "public_ipv4": {+ "description": "Public IPv4 address of the voice machine",+ "type": "string",+ "format": "ipv4",+ "pattern": "^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$"+ }+ },+ "required": ["endpoint", "api_key", "api_secret"]+ },+ "VoiceJoinResponse": {+ "type": "object",+ "properties": {+ "server_id": {"type": "string", "description": "ID of the new server"},+ "region_id": {"type": "string", "description": "Region of the new server"},+ "endpoint": {"type": "string", "description": "Endpoint of the new server"},+ "webhook_url": {"type": "string", "description": "URL the server sends LiveKit webhooks to"},+ "node_url": {"type": "string", "description": "URL of the node routes for this server"}+ },+ "required": ["server_id", "region_id", "endpoint", "webhook_url", "node_url"],+ "additionalProperties": false+ },"EntranceSoundPlayRequest": {"type": "object","properties": {"sound_id": {"$ref": "#/components/schemas/SnowflakeType"}},@@ -37431,6 +37853,12 @@"maxItems": 100,"type": "array","items": {"$ref": "#/components/schemas/InstanceGuildBadgeSchema"}+ },+ "voice_origins": {+ "description": "Origins of every voice server endpoint, such as wss://voice.example.com, for content security policies",+ "maxItems": 1000,+ "type": "array",+ "items": {"type": "string"}}},"required": [@@ -44780,6 +45208,26 @@"enum": ["online", "dnd", "idle", "invisible"],"type": "string"},+ "VoiceServerHealthResponse": {+ "type": "object",+ "properties": {+ "status": {+ "type": "string",+ "enum": ["up", "down", "unknown"],+ "description": "up, down, or unknown before the first check"+ },+ "checked_at": {+ "description": "ISO 8601 timestamp of the last check, or null before the first",+ "type": ["string", "null"]+ },+ "error": {+ "description": "Status code or short transport error of the last failed check",+ "type": ["string", "null"]+ }+ },+ "required": ["status", "checked_at", "error"],+ "additionalProperties": false+ },"Int64Type": {"anyOf": [{"type": "string"}, {"type": "integer", "minimum": -9007199254740991, "maximum": 9007199254740991}],"format": "int64"@@ -45128,6 +45576,16 @@"USER_OWNS_GUILDS","VALIDATION_ERROR","VOICE_CHANNEL_FULL",+ "VOICE_JOIN_ENDPOINT_INVALID",+ "VOICE_JOIN_ENDPOINT_UNREACHABLE",+ "VOICE_JOIN_KEY_MISMATCH",+ "VOICE_JOIN_TOKEN_BUSY",+ "VOICE_JOIN_TOKEN_INVALID",+ "VOICE_NODE_SIGNATURE_INVALID",+ "VOICE_NODE_TOKEN_EXPIRED",+ "VOICE_NODE_UNKNOWN",+ "VOICE_SERVER_API_KEY_IN_USE",+ "VOICE_SERVER_ID_IN_USE","WEBAUTHN_CREDENTIAL_LIMIT_REACHED","ACCOUNT_VERIFICATION_REQUIRED","DATE_OF_BIRTH_REQUIRED",@@ -45493,6 +45951,16 @@"This user owns communities.","Validation failed.","Voice channel is full.",+ "The voice server endpoint isn't allowed.",+ "Fluxer couldn't connect to the voice server endpoint.",+ "The voice server rejected its API key and secret.",+ "Another voice server is joining with this token right now.",+ "The voice join token is not valid.",+ "The voice server's signature doesn't match its secret.",+ "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "No voice server has this API key.",+ "Another voice server already uses this API key.",+ "Another voice server already uses this ID.","You've reached the maximum of {count, plural, one {# WebAuthn credential} other {# WebAuthn credentials}}.","{need, select, add_email {{action, select, message {Add an email to your account to send messages.} typing {Add an email to your account to send messages.} reaction {Add an email to your account to add reactions.} direct_message {Add an email to your account to send direct messages.} direct_message_open {Add an email to your account to start direct messages.} group_dm {Add an email to your account to join group chats.} friend_request {Add an email to your account to send friend requests.} friend_accept {Add an email to your account to accept friend requests.} invite_create {Add an email to your account to create invites.} guild_join {Add an email to your account to join this community.} guild_create {Add an email to your account to create a community.} voice {Add an email to your account to join voice channels.} call {Add an email to your account to start calls.} group_call {Add an email to your account to join group calls.} thread_join {Add an email to your account to join threads.} channel_follow {Add an email to your account to follow channels.} ownership_transfer {This person needs to add an email to their account before they can own a community.} application_create {Add an email to your account to create applications.} gift_redeem {Add an email to your account to redeem gifts.} profile_customize {Add an email to your account to change your profile.} report {Add an email to your account to submit reports.} mfa {Add an email to your account to turn on two-factor authentication.} guild_verification {Add an email to your account to talk in this community.} other {Add an email to your account to do this.}}} confirm_email {{action, select, message {Confirm your email to send messages.} typing {Confirm your email to send messages.} reaction {Confirm your email to add reactions.} direct_message {Confirm your email to send direct messages.} direct_message_open {Confirm your email to start direct messages.} group_dm {Confirm your email to join group chats.} friend_request {Confirm your email to send friend requests.} friend_accept {Confirm your email to accept friend requests.} invite_create {Confirm your email to create invites.} guild_join {Confirm your email to join this community.} guild_create {Confirm your email to create a community.} voice {Confirm your email to join voice channels.} call {Confirm your email to start calls.} group_call {Confirm your email to join group calls.} thread_join {Confirm your email to join threads.} channel_follow {Confirm your email to follow channels.} ownership_transfer {This person needs to confirm their email before they can own a community.} application_create {Confirm your email to create applications.} gift_redeem {Confirm your email to redeem gifts.} profile_customize {Confirm your email to change your profile.} report {Confirm your email to submit reports.} mfa {Confirm your email to turn on two-factor authentication.} guild_verification {Confirm your email to talk in this community.} other {Confirm your email to do this.}}} password {{action, select, message {Set a password to send messages.} typing {Set a password to send messages.} reaction {Set a password to add reactions.} direct_message {Set a password to send direct messages.} direct_message_open {Set a password to start direct messages.} group_dm {Set a password to join group chats.} friend_request {Set a password to send friend requests.} friend_accept {Set a password to accept friend requests.} invite_create {Set a password to create invites.} guild_join {Set a password to join this community.} guild_create {Set a password to create a community.} voice {Set a password to join voice channels.} call {Set a password to start calls.} group_call {Set a password to join group calls.} thread_join {Set a password to join threads.} channel_follow {Set a password to follow channels.} ownership_transfer {This person needs to set a password before they can own a community.} application_create {Set a password to create applications.} gift_redeem {Set a password to redeem gifts.} profile_customize {Set a password to change your profile.} report {Set a password to submit reports.} mfa {Set a password to turn on two-factor authentication.} guild_verification {Set a password to talk in this community.} other {Set a password to do this.}}} other {{action, select, message {Claim your account to send messages.} typing {Claim your account to send messages.} reaction {Claim your account to add reactions.} direct_message {Claim your account to send direct messages.} direct_message_open {Claim your account to start direct messages.} group_dm {Claim your account to join group chats.} friend_request {Claim your account to send friend requests.} friend_accept {Claim your account to accept friend requests.} invite_create {Claim your account to create invites.} guild_join {Claim your account to join this community.} guild_create {Claim your account to create a community.} voice {Claim your account to join voice channels.} call {Claim your account to start calls.} group_call {Claim your account to join group calls.} thread_join {Claim your account to join threads.} channel_follow {Claim your account to follow channels.} ownership_transfer {This person needs to claim their account before they can own a community.} application_create {Claim your account to create applications.} gift_redeem {Claim your account to redeem gifts.} profile_customize {Claim your account to change your profile.} report {Claim your account to submit reports.} mfa {Claim your account to turn on two-factor authentication.} guild_verification {Claim your account to talk in this community.} other {Claim your account to do this.}}}}","Enter your date of birth to continue.",diff --git a/fluxer_api/src/api/rate_limit_configs/MiscRateLimitConfig.ts b/fluxer_api/src/api/rate_limit_configs/MiscRateLimitConfig.tsindex 143904069..bcdbc13d5 100644--- a/fluxer_api/src/api/rate_limit_configs/MiscRateLimitConfig.ts+++ b/fluxer_api/src/api/rate_limit_configs/MiscRateLimitConfig.ts@@ -8,6 +8,14 @@ export const MiscRateLimitConfigs = {bucket: 'instance:info',config: {limit: 60, windowMs: ms('1 minute')},} as RouteRateLimitConfig,+ VOICE_JOIN: {+ bucket: 'voice:join',+ config: {limit: 10, windowMs: ms('1 minute')},+ } as RouteRateLimitConfig,+ VOICE_NODE: {+ bucket: 'voice:node',+ config: {limit: 60, windowMs: ms('1 minute')},+ } as RouteRateLimitConfig,ACTIVITY_PUB_DOCUMENT: {bucket: 'activity_pub:document',config: {limit: 60, windowMs: ms('1 minute')},diff --git a/fluxer_api/src/api/voice/VoiceAvailabilityService.ts b/fluxer_api/src/api/voice/VoiceAvailabilityService.tsindex 91d1c6605..93f9f458a 100644--- a/fluxer_api/src/api/voice/VoiceAvailabilityService.ts+++ b/fluxer_api/src/api/voice/VoiceAvailabilityService.ts@@ -3,6 +3,7 @@import type {GuildID, UserID} from '@app/api/BrandedTypes';import type {VoiceRegionAvailability, VoiceRegionRecord, VoiceServerRecord} from '@app/api/voice/VoiceModel';import {preferServersUnderSoftLimit} from '@app/api/voice/VoiceRegionSelection';+import type {VoiceServerHealthSource} from '@app/api/voice/VoiceServerHealth';import type {VoiceServerLoadSource} from '@app/api/voice/VoiceServerLoad';import type {VoiceTopology} from '@app/api/voice/VoiceTopology';import {GuildFeatures} from '@fluxer/constants/src/GuildConstants';@@ -58,8 +59,25 @@ export class VoiceAvailabilityService {constructor(private topology: VoiceTopology,private loadSource: VoiceServerLoadSource | null = null,+ private healthSource: VoiceServerHealthSource | null = null,) {}+ isServerDown(server: VoiceServerRecord): boolean {+ return this.healthSource?.get(server).status === 'down';+ }++ isServerPlaceable(server: VoiceServerRecord): boolean {+ return !server.draining && !this.isServerDown(server);+ }++ preferPlaceableServers(servers: Array<VoiceServerRecord>): Array<VoiceServerRecord> {+ const open = servers.filter((server) => !server.draining);+ const up = open.filter((server) => !this.isServerDown(server));+ if (up.length > 0) return up;+ if (open.length > 0) return open;+ return servers;+ }+getServerConnectionCounts(): ReadonlyMap<string, number> {return this.loadSource?.getConnectionCounts() ?? EMPTY_CONNECTION_COUNTS;}@@ -113,10 +131,42 @@ export class VoiceAvailabilityService {if (accessibleServers.length === 0) {return null;}- const candidateServers = preferServersUnderSoftLimit(accessibleServers, this.getServerConnectionCounts());+ const candidateServers = preferServersUnderSoftLimit(+ this.preferPlaceableServers(accessibleServers),+ this.getServerConnectionCounts(),+ );const index = this.rotationIndex.get(regionId) ?? 0;const server = candidateServers[index % candidateServers.length];this.rotationIndex.set(regionId, (index + 1) % candidateServers.length);return server;}++ selectServerOrFallback(+ regionId: string,+ context: VoiceAccessContext,+ accessibleRegions: Array<VoiceRegionAvailability>,+ ): {regionId: string; server: VoiceServerRecord} | null {+ const initialServer = this.selectServer(regionId, context);+ if (initialServer && this.isServerPlaceable(initialServer)) {+ return {regionId, server: initialServer};+ }+ let fallback: {regionId: string; server: VoiceServerRecord} | null = null;+ for (const region of accessibleRegions) {+ if (region.id === regionId) {+ continue;+ }+ const server = this.selectServer(region.id, context);+ if (!server) {+ continue;+ }+ if (this.isServerPlaceable(server)) {+ return {regionId: region.id, server};+ }+ fallback ??= {regionId: region.id, server};+ }+ if (initialServer) {+ return {regionId, server: initialServer};+ }+ return fallback;+ }}diff --git a/fluxer_api/src/api/voice/VoiceDataInitializer.ts b/fluxer_api/src/api/voice/VoiceDataInitializer.tsindex 9af7c7d60..b8a1ee8b4 100644--- a/fluxer_api/src/api/voice/VoiceDataInitializer.ts+++ b/fluxer_api/src/api/voice/VoiceDataInitializer.ts@@ -3,6 +3,7 @@import {Config} from '@app/api/Config';import {Logger} from '@app/api/Logger';import {VoiceRepository} from '@app/api/voice/VoiceRepository';+import {bundledVoiceServerId} from '@app/api/voice/VoiceServerEndpoints';export function resolveLivekitEndpoint(configuredUrl: string | undefined, apiPublicUrl: string): string {if (configuredUrl) {@@ -21,13 +22,13 @@ export class VoiceDataInitializer {const defaultRegion = Config.voice.defaultRegion;const livekitApiKey = Config.voice.apiKey;const livekitApiSecret = Config.voice.apiSecret;- if (!livekitApiKey || !livekitApiSecret) {+ const serverId = bundledVoiceServerId();+ if (serverId && (!livekitApiKey || !livekitApiSecret)) {Logger.warn('[VoiceDataInitializer] LiveKit API key/secret not configured, cannot create default region');return;}try {const repository = new VoiceRepository();- const serverId = `${defaultRegion.id}-server-1`;const livekitEndpoint = resolveLivekitEndpoint(Config.voice.url, Config.endpoints.apiPublic);const existingRegions = await repository.listRegions();if (existingRegions.length === 0) {@@ -47,6 +48,12 @@ export class VoiceDataInitializer {},});Logger.info(`[VoiceDataInitializer] Created region: ${defaultRegion.name} (${defaultRegion.id})`);+ if (!serverId || !livekitApiKey || !livekitApiSecret) {+ Logger.info(+ '[VoiceDataInitializer] No bundled voice server, add servers with rexctl voice join or in the admin panel',+ );+ return;+ }await repository.createServer({regionId: defaultRegion.id,serverId,@@ -58,6 +65,10 @@ export class VoiceDataInitializer {isActive: true,softConnectionLimit: null,notes: null,+ internalEndpoint: null,+ draining: false,+ pinnedHostname: null,+ pinnedSoftLimit: false,restrictions: {vipOnly: false,requiredGuildFeatures: new Set(),@@ -76,6 +87,10 @@ export class VoiceDataInitializer {);return;}+ if (!serverId || !livekitApiKey || !livekitApiSecret) {+ await this.removeBundledServer(repository, defaultRegion.id, livekitApiKey);+ return;+ }const configuredServer = await repository.getServer(defaultRegion.id, serverId);if (!configuredServer) {Logger.info(@@ -90,6 +105,12 @@ export class VoiceDataInitializer {Logger.info(`[VoiceDataInitializer] Default voice server ${serverId} already matches config credentials`);return;}+ if (endpointChanged && apiKeyChanged) {+ Logger.warn(+ `[VoiceDataInitializer] Server ${serverId} has another endpoint and API key than the bundled LiveKit, leaving it unchanged`,+ );+ return;+ }await repository.upsertServer({...configuredServer,endpoint: livekitEndpoint,@@ -102,4 +123,18 @@ export class VoiceDataInitializer {Logger.error({error}, '[VoiceDataInitializer] Failed to initialise config-managed voice topology');}}++ private async removeBundledServer(+ repository: VoiceRepository,+ regionId: string,+ bundledApiKey: string | undefined,+ ): Promise<void> {+ const serverId = `${regionId}-server-1`;+ const server = await repository.getServer(regionId, serverId);+ if (!server || !bundledApiKey || server.apiKey !== bundledApiKey) {+ return;+ }+ await repository.deleteServer(regionId, serverId);+ Logger.info(`[VoiceDataInitializer] Removed ${serverId}, the bundled voice server is turned off`);+ }}diff --git a/fluxer_api/src/api/voice/VoiceJoinTokenRepository.ts b/fluxer_api/src/api/voice/VoiceJoinTokenRepository.tsnew file mode 100644index 000000000..a8d7e854a--- /dev/null+++ b/fluxer_api/src/api/voice/VoiceJoinTokenRepository.ts@@ -0,0 +1,92 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {executeConditional, fetchMany, fetchOne} from '@app/api/database/CassandraQueryExecution';+import {defineTable} from '@app/api/database/CassandraTableDsl';+import {Db} from '@app/api/database/CassandraTypes';+import {VOICE_JOIN_TOKEN_COLUMNS, type VoiceJoinTokenRow} from '@app/api/database/types/VoiceTypes';++const KEEP_AFTER_EXPIRY_SECONDS = 7 * 24 * 60 * 60;++const VoiceJoinTokens = defineTable<VoiceJoinTokenRow, 'token_id'>({+ name: 'voice_join_tokens',+ columns: VOICE_JOIN_TOKEN_COLUMNS,+ primaryKey: ['token_id'],+});++const GET_TOKEN_CQL = VoiceJoinTokens.selectCql({where: VoiceJoinTokens.where.eq('token_id')});+const LIST_TOKENS_CQL = VoiceJoinTokens.selectCql();++function ttlFor(row: Pick<VoiceJoinTokenRow, 'expires_at'>): number {+ return Math.max(1, Math.floor((row.expires_at.getTime() - Date.now()) / 1000) + KEEP_AFTER_EXPIRY_SECONDS);+}++export class VoiceJoinTokenRepository {+ async create(row: VoiceJoinTokenRow): Promise<void> {+ const applied = await executeConditional(VoiceJoinTokens.insertIfNotExistsWithTtl(row, ttlFor(row)));+ if (!applied) {+ throw new Error(`Voice join token ${row.token_id} already exists`);+ }+ }++ async get(tokenId: bigint): Promise<VoiceJoinTokenRow | null> {+ return fetchOne<VoiceJoinTokenRow>(GET_TOKEN_CQL, {token_id: tokenId});+ }++ async list(): Promise<Array<VoiceJoinTokenRow>> {+ return fetchMany<VoiceJoinTokenRow>(LIST_TOKENS_CQL, {});+ }++ async claim(row: VoiceJoinTokenRow, claimId: string, now: Date): Promise<boolean> {+ return executeConditional(+ VoiceJoinTokens.conditionalPatchByPkWithTtl(+ {token_id: row.token_id},+ {claim_id: Db.set(claimId), claimed_at: Db.set(now)},+ {+ secret_hash: row.secret_hash,+ used_at: null,+ revoked_at: null,+ claim_id: row.claim_id,+ claimed_at: row.claimed_at,+ },+ ttlFor(row),+ ),+ );+ }++ async release(row: VoiceJoinTokenRow, claimId: string): Promise<boolean> {+ return executeConditional(+ VoiceJoinTokens.conditionalPatchByPkWithTtl(+ {token_id: row.token_id},+ {claim_id: Db.clear(), claimed_at: Db.clear()},+ {claim_id: claimId},+ ttlFor(row),+ ),+ );+ }++ async markUsed(+ row: VoiceJoinTokenRow,+ claimId: string,+ used: {at: Date; ip: string | null; serverId: string},+ ): Promise<boolean> {+ return executeConditional(+ VoiceJoinTokens.conditionalPatchByPkWithTtl(+ {token_id: row.token_id},+ {used_at: Db.set(used.at), used_by_ip: Db.set(used.ip), used_server_id: Db.set(used.serverId)},+ {claim_id: claimId},+ ttlFor(row),+ ),+ );+ }++ async revoke(row: VoiceJoinTokenRow, now: Date): Promise<boolean> {+ return executeConditional(+ VoiceJoinTokens.conditionalPatchByPkWithTtl(+ {token_id: row.token_id},+ {revoked_at: Db.set(now)},+ {secret_hash: row.secret_hash, used_at: null, revoked_at: null},+ ttlFor(row),+ ),+ );+ }+}diff --git a/fluxer_api/src/api/voice/VoiceModel.ts b/fluxer_api/src/api/voice/VoiceModel.tsindex 08e59aaf5..56d7949b3 100644--- a/fluxer_api/src/api/voice/VoiceModel.ts+++ b/fluxer_api/src/api/voice/VoiceModel.ts@@ -33,6 +33,10 @@ export interface VoiceServerRecord {softConnectionLimit: number | null;notes: string | null;restrictions: VoiceRestriction;+ internalEndpoint: string | null;+ draining: boolean;+ pinnedHostname: string | null;+ pinnedSoftLimit: boolean;createdAt: Date | null;updatedAt: Date | null;}diff --git a/fluxer_api/src/api/voice/VoiceNodeController.ts b/fluxer_api/src/api/voice/VoiceNodeController.tsnew file mode 100644index 000000000..abdb6bc2e--- /dev/null+++ b/fluxer_api/src/api/voice/VoiceNodeController.ts@@ -0,0 +1,155 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';+import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';+import {getVoiceServerHealth} from '@app/api/middleware/ServiceRegistry';+import {RateLimitConfigs} from '@app/api/RateLimitConfig';+import type {HonoApp, HonoEnv} from '@app/api/types/HonoEnv';+import {getRequestClientIp} from '@app/api/utils/RequestClientIp';+import {Validator} from '@app/api/Validator';+import {VOICE_CONFIGURATION_CHANNEL} from '@app/api/voice/VoiceConstants';+import {VoiceJoinTokenRepository} from '@app/api/voice/VoiceJoinTokenRepository';+import {VOICE_NODE_PATH, VoiceNodeService} from '@app/api/voice/VoiceNodeService';+import {VoiceRepository} from '@app/api/voice/VoiceRepository';+import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';+import {+ UpdateVoiceNodeRequest,+ VoiceJoinPreviewRequest,+ VoiceJoinPreviewResponse,+ VoiceJoinRequest,+ VoiceJoinResponse,+ VoiceNodeResponse,+} from '@fluxer/schema/src/domains/voice/VoiceNodeSchemas';+import type {Context} from 'hono';++function nodeService(ctx: Context<HonoEnv>): VoiceNodeService {+ const cache = ctx.get('apiContext').services.cache;+ return new VoiceNodeService({+ voiceRepository: new VoiceRepository(),+ joinTokenRepository: new VoiceJoinTokenRepository(),+ auditService: ctx.get('adminService').auditService,+ health: getVoiceServerHealth(),+ publish: (message) => cache.publish(VOICE_CONFIGURATION_CHANNEL, message),+ });+}++function parseNodeBody(text: string): UpdateVoiceNodeRequest {+ let value: unknown;+ try {+ value = text === '' ? {} : JSON.parse(text);+ } catch {+ throw InputValidationError.create('body', 'The body is not JSON');+ }+ const parsed = UpdateVoiceNodeRequest.safeParse(value);+ if (!parsed.success) {+ throw InputValidationError.createMultiple(+ parsed.error.issues.map((issue) => ({path: issue.path.join('.') || 'body', message: issue.message})),+ );+ }+ return parsed.data;+}++const NODE_AUTH_DESCRIPTION =+ 'Send Authorization: Bearer with an HS256 JWT signed with the API secret of the server, with iss set to its API key, aud set to fluxer-voice-node, method and path of the request, an exp at most 5 minutes ahead and sha256 holding the base64 SHA-256 of the body, or an empty string for a request without a body.';++export function VoiceNodeController(app: HonoApp) {+ app.post(+ '/voice/join/preview',+ RateLimitMiddleware(RateLimitConfigs.VOICE_JOIN),+ Validator('json', VoiceJoinPreviewRequest),+ OpenAPI({+ operationId: 'preview_voice_join',+ summary: 'Preview joining a voice server',+ description:+ 'Shows the server id, region, coordinates and webhook URL a new voice server would get from a join token. Send Authorization: VoiceJoin with the token. The call changes nothing and does not use the token.',+ requestSchema: VoiceJoinPreviewRequest,+ responseSchema: VoiceJoinPreviewResponse,+ statusCode: 200,+ security: [],+ tags: ['Voice'],+ }),+ async (ctx) => {+ const service = nodeService(ctx);+ const row = await service.authenticateJoin(ctx.req.header('Authorization'));+ return ctx.json(await service.preview(row, ctx.req.valid('json'), getRequestClientIp(ctx)));+ },+ );+ app.post(+ '/voice/join',+ RateLimitMiddleware(RateLimitConfigs.VOICE_JOIN),+ Validator('json', VoiceJoinRequest),+ OpenAPI({+ operationId: 'create_voice_join',+ summary: 'Join a voice server to the instance',+ description:+ 'Registers a new voice server with a join token. Send Authorization: VoiceJoin with the token. Fluxer checks that the endpoint answers and that the key and secret match before it saves the server, then the token stops working.',+ requestSchema: VoiceJoinRequest,+ responseSchema: VoiceJoinResponse,+ statusCode: 200,+ security: [],+ tags: ['Voice'],+ }),+ async (ctx) => {+ const service = nodeService(ctx);+ const row = await service.authenticateJoin(ctx.req.header('Authorization'));+ return ctx.json(await service.join(row, ctx.req.valid('json'), getRequestClientIp(ctx)));+ },+ );+ app.get(+ VOICE_NODE_PATH,+ RateLimitMiddleware(RateLimitConfigs.VOICE_NODE),+ OpenAPI({+ operationId: 'get_voice_node',+ summary: 'Get the calling voice server',+ description: `Returns the voice server that signed the request, with its health as the API sees it. ${NODE_AUTH_DESCRIPTION}`,+ responseSchema: VoiceNodeResponse,+ statusCode: 200,+ security: [],+ tags: ['Voice'],+ }),+ async (ctx) => {+ const service = nodeService(ctx);+ const server = await service.authenticateNode(ctx.req.header('Authorization'), 'GET', '');+ return ctx.json(service.describe(server));+ },+ );+ app.patch(+ VOICE_NODE_PATH,+ RateLimitMiddleware(RateLimitConfigs.VOICE_NODE),+ OpenAPI({+ operationId: 'update_voice_node',+ summary: 'Update the calling voice server',+ description: `Changes the endpoint, draining flag, coordinates or soft connection limit of the voice server that signed the request. A new endpoint is checked before it is saved. ${NODE_AUTH_DESCRIPTION}`,+ requestSchema: UpdateVoiceNodeRequest,+ responseSchema: VoiceNodeResponse,+ statusCode: 200,+ security: [],+ tags: ['Voice'],+ }),+ async (ctx) => {+ const service = nodeService(ctx);+ const text = await ctx.req.text();+ const server = await service.authenticateNode(ctx.req.header('Authorization'), 'PATCH', text);+ return ctx.json(await service.update(server, parseNodeBody(text)));+ },+ );+ app.delete(+ VOICE_NODE_PATH,+ RateLimitMiddleware(RateLimitConfigs.VOICE_NODE),+ OpenAPI({+ operationId: 'delete_voice_node',+ summary: 'Remove the calling voice server',+ description: `Deletes the voice server that signed the request. ${NODE_AUTH_DESCRIPTION}`,+ responseSchema: null,+ statusCode: 204,+ security: [],+ tags: ['Voice'],+ }),+ async (ctx) => {+ const service = nodeService(ctx);+ const server = await service.authenticateNode(ctx.req.header('Authorization'), 'DELETE', '');+ await service.remove(server);+ return ctx.body(null, 204);+ },+ );+}diff --git a/fluxer_api/src/api/voice/VoiceNodeService.ts b/fluxer_api/src/api/voice/VoiceNodeService.tsnew file mode 100644index 000000000..5809cec76--- /dev/null+++ b/fluxer_api/src/api/voice/VoiceNodeService.ts@@ -0,0 +1,505 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {createHash, createHmac, randomUUID, timingSafeEqual} from 'node:crypto';+import type {AdminAuditService} from '@app/api/admin/services/AdminAuditService';+import {createGuildIDSet, createUserIDSet, type UserID} from '@app/api/BrandedTypes';+import {Config} from '@app/api/Config';+import type {VoiceJoinTokenRow} from '@app/api/database/types/VoiceTypes';+import {Logger} from '@app/api/Logger';+import {calculateDistance} from '@app/api/utils/GeoUtils';+import type {VoiceJoinTokenRepository} from '@app/api/voice/VoiceJoinTokenRepository';+import type {VoiceRegionRecord, VoiceServerRecord} from '@app/api/voice/VoiceModel';+import type {VoiceRepository} from '@app/api/voice/VoiceRepository';+import {+ bundledVoiceServerId,+ checkLiveKitKeys,+ parseNodeEndpoint,+ probeLiveKit,+ VoiceEndpointError,+} from '@app/api/voice/VoiceServerEndpoints';+import {healthOf, toHealthResponse, type VoiceServerHealthSource} from '@app/api/voice/VoiceServerHealth';+import {API_CODE_VERSION} from '@fluxer/constants/src/AppConstants';+import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';+import {UnknownVoiceRegionError} from '@fluxer/errors/src/domains/voice/UnknownVoiceRegionError';+import {VoiceJoinEndpointInvalidError} from '@fluxer/errors/src/domains/voice/VoiceJoinEndpointInvalidError';+import {VoiceJoinEndpointUnreachableError} from '@fluxer/errors/src/domains/voice/VoiceJoinEndpointUnreachableError';+import {VoiceJoinKeyMismatchError} from '@fluxer/errors/src/domains/voice/VoiceJoinKeyMismatchError';+import {VoiceJoinTokenBusyError} from '@fluxer/errors/src/domains/voice/VoiceJoinTokenBusyError';+import {VoiceJoinTokenInvalidError} from '@fluxer/errors/src/domains/voice/VoiceJoinTokenInvalidError';+import {VoiceNodeSignatureInvalidError} from '@fluxer/errors/src/domains/voice/VoiceNodeSignatureInvalidError';+import {VoiceNodeTokenExpiredError} from '@fluxer/errors/src/domains/voice/VoiceNodeTokenExpiredError';+import {VoiceNodeUnknownError} from '@fluxer/errors/src/domains/voice/VoiceNodeUnknownError';+import {VoiceServerApiKeyInUseError} from '@fluxer/errors/src/domains/voice/VoiceServerApiKeyInUseError';+import {VoiceServerIdInUseError} from '@fluxer/errors/src/domains/voice/VoiceServerIdInUseError';+import type {+ UpdateVoiceNodeRequest,+ VoiceJoinPreviewRequest,+ VoiceJoinPreviewResponse,+ VoiceJoinRequest,+ VoiceJoinResponse,+ VoiceNodeResponse,+} from '@fluxer/schema/src/domains/voice/VoiceNodeSchemas';+import {lookupGeoipByIp} from '@pkgs/geoip/src/GeoipLookup';++export const VOICE_JOIN_TOKEN_PREFIX = 'fvj_';++export function voiceJoinCommand(webApp: string, baseDomain: string, token: string): string {+ const instance = URL.parse(webApp)?.host || baseDomain;+ return `curl -fsSL https://fluxer.dev/install.sh | sh -s -- voice ${instance} ${token}`;+}++export const VOICE_NODE_AUDIENCE = 'fluxer-voice-node';+export const VOICE_NODE_PATH = '/voice/node';++const CLAIM_STALE_MS = 2 * 60 * 1000;+const NODE_TOKEN_MAX_AHEAD_SECONDS = 5 * 60;+const NODE_TOKEN_LEEWAY_SECONDS = 60;+const PROBE_TIMEOUT_MS = 10_000;++export function hashJoinSecret(secret: string): string {+ return createHash('sha256').update(secret).digest('hex');+}++function parseJoinToken(header: string | undefined): {tokenId: bigint; secret: string} | null {+ const value = header?.startsWith('VoiceJoin ') ? header.slice('VoiceJoin '.length) : null;+ const match = value ? /^fvj_(\d{1,20})_([A-Za-z0-9_-]{43})$/.exec(value) : null;+ if (!match) return null;+ return {tokenId: BigInt(match[1]!), secret: match[2]!};+}++function sameHash(a: string, b: string): boolean {+ const left = Buffer.from(a, 'hex');+ const right = Buffer.from(b, 'hex');+ return left.length === right.length && timingSafeEqual(left, right);+}++export type JoinTokenStatus = 'pending' | 'used' | 'expired' | 'revoked';++export function joinTokenStatus(row: VoiceJoinTokenRow, now = new Date()): JoinTokenStatus {+ if (row.used_at) return 'used';+ if (row.revoked_at) return 'revoked';+ if (row.expires_at.getTime() <= now.getTime()) return 'expired';+ return 'pending';+}++function round(value: number): number {+ return Math.round(value * 10) / 10;+}++function bodyHash(body: string): string {+ return body === '' ? '' : createHash('sha256').update(body).digest('base64');+}++function decodeSegment(segment: string): Record<string, unknown> | null {+ try {+ const value: unknown = JSON.parse(Buffer.from(segment, 'base64url').toString('utf8'));+ return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : null;+ } catch {+ return null;+ }+}++interface NodeTokenParts {+ issuer: string;+ signingInput: string;+ signature: Buffer;+ claims: Record<string, unknown>;+}++function splitNodeToken(header: string | undefined): NodeTokenParts | null {+ const token = header?.startsWith('Bearer ') ? header.slice('Bearer '.length) : null;+ const parts = token?.split('.');+ if (parts?.length !== 3) return null;+ const [head, payload, signature] = parts as [string, string, string];+ const headerJson = decodeSegment(head);+ const claims = decodeSegment(payload);+ if (headerJson?.['alg'] !== 'HS256' || !claims || typeof claims['iss'] !== 'string') return null;+ return {+ issuer: claims['iss'],+ signingInput: `${head}.${payload}`,+ signature: Buffer.from(signature, 'base64url'),+ claims,+ };+}++function signatureMatches(parts: NodeTokenParts, secret: string): boolean {+ const expected = createHmac('sha256', secret).update(parts.signingInput).digest();+ return expected.length === parts.signature.length && timingSafeEqual(expected, parts.signature);+}++export type EndpointVerifier = (+ endpoint: string,+ keys: {apiKey: string; apiSecret: string},+ internalEndpoint: string | null,+ probePublic: boolean,+) => Promise<void>;++const verifyNodeEndpoint: EndpointVerifier = async (endpoint, keys, internalEndpoint, probePublic) => {+ const base = internalEndpoint ?? endpoint;+ const options = {+ checkAddress: internalEndpoint === null,+ allowPrivate: Config.voice.joinAllowPrivateEndpoints,+ timeoutMs: PROBE_TIMEOUT_MS,+ };+ if (probePublic && internalEndpoint !== null) {+ await probeLiveKit(endpoint, {...options, checkAddress: true}).catch((error: unknown) => {+ Logger.warn(+ {endpoint, error: error instanceof Error ? error.message : String(error)},+ "The API can't connect to the new public endpoint of a voice server with an internal endpoint",+ );+ });+ }+ await probeLiveKit(base, options);+ await checkLiveKitKeys(base, keys, options);+};++let endpointVerifier: EndpointVerifier = verifyNodeEndpoint;++export function setVoiceNodeEndpointVerifierForTesting(verifier: EndpointVerifier | null): void {+ endpointVerifier = verifier ?? verifyNodeEndpoint;+}++function asApiError(error: unknown): Error {+ if (!(error instanceof VoiceEndpointError)) return error as Error;+ switch (error.reason) {+ case 'invalid':+ case 'private':+ return new VoiceJoinEndpointInvalidError(error.message);+ case 'key_mismatch':+ return new VoiceJoinKeyMismatchError();+ default:+ return new VoiceJoinEndpointUnreachableError(error.message);+ }+}++interface VoiceNodeServiceDeps {+ voiceRepository: VoiceRepository;+ joinTokenRepository: VoiceJoinTokenRepository;+ auditService: AdminAuditService;+ health: VoiceServerHealthSource | null;+ publish: (message: string) => Promise<void>;+}++type RegionSource = 'token' | 'requested' | 'nearest' | 'default';++interface ChosenRegion {+ id: string;+ name: string;+ isNew: boolean;+ distanceKm: number | null;+ emoji: string;+ source: RegionSource;+}++export class VoiceNodeService {+ constructor(private readonly deps: VoiceNodeServiceDeps) {}++ async authenticateJoin(header: string | undefined): Promise<VoiceJoinTokenRow> {+ const parsed = parseJoinToken(header);+ if (!parsed) throw new VoiceJoinTokenInvalidError();+ const row = await this.deps.joinTokenRepository.get(parsed.tokenId);+ if (!row || !sameHash(row.secret_hash, hashJoinSecret(parsed.secret)) || joinTokenStatus(row) !== 'pending') {+ throw new VoiceJoinTokenInvalidError();+ }+ return row;+ }++ async preview(row: VoiceJoinTokenRow, body: VoiceJoinPreviewRequest, clientIp: string | null) {+ const point = await this.locate(body.public_ipv4 ?? clientIp);+ const region = await this.chooseRegion(row, body.region_id, point);+ const servers = await this.deps.voiceRepository.listAllServers();+ const response: VoiceJoinPreviewResponse = {+ server_id: row.server_id ?? this.suggestServerId(region.id, servers),+ server_id_pinned: row.server_id !== null,+ region: {+ id: region.id,+ name: region.name,+ distance_km: region.distanceKm,+ new: region.isNew,+ source: region.source,+ },+ latitude: point ? round(point.latitude) : null,+ longitude: point ? round(point.longitude) : null,+ hostname: row.hostname,+ suggested_hostname: `voice-${region.id}.${Config.instance.baseDomain}`,+ soft_connection_limit: row.soft_connection_limit,+ webhook_url: `${Config.endpoints.apiPublic}/webhooks/livekit`,+ api_code_version: API_CODE_VERSION,+ release: process.env.BUILD_VERSION?.trim() ?? '',+ expires_at: row.expires_at.toISOString(),+ };+ return response;+ }++ async join(row: VoiceJoinTokenRow, body: VoiceJoinRequest, clientIp: string | null): Promise<VoiceJoinResponse> {+ const {joinTokenRepository, voiceRepository} = this.deps;+ const now = new Date();+ if (row.claim_id && row.claimed_at && now.getTime() - row.claimed_at.getTime() < CLAIM_STALE_MS) {+ throw new VoiceJoinTokenBusyError();+ }+ const claimId = randomUUID();+ if (!(await joinTokenRepository.claim(row, claimId, now))) {+ throw new VoiceJoinTokenBusyError();+ }+ try {+ const point =+ body.latitude !== undefined && body.longitude !== undefined+ ? {latitude: body.latitude, longitude: body.longitude}+ : await this.locate(body.public_ipv4 ?? clientIp);+ const region = await this.chooseRegion(row, body.region_id, point);+ const servers = await voiceRepository.listAllServers();+ const serverId = row.server_id ?? body.server_id ?? this.suggestServerId(region.id, servers);+ if (serverId === bundledVoiceServerId() || servers.some((server) => server.serverId === serverId)) {+ throw new VoiceServerIdInUseError();+ }+ if (servers.some((server) => server.apiKey === body.api_key)) throw new VoiceServerApiKeyInUseError();+ const endpoint = this.checkEndpoint(body.endpoint, row.hostname);+ await endpointVerifier(+ endpoint,+ {apiKey: body.api_key, apiSecret: body.api_secret},+ row.internal_endpoint,+ false,+ ).catch((error: unknown) => {+ throw asApiError(error);+ });+ if (region.isNew) {+ await voiceRepository.createRegion({+ id: region.id,+ name: region.name,+ emoji: region.emoji,+ latitude: row.region_latitude ?? point?.latitude ?? 0,+ longitude: row.region_longitude ?? point?.longitude ?? 0,+ isDefault: false,+ restrictions: emptyRestrictions(),+ });+ }+ const created = await voiceRepository.createServerIfAbsent({+ regionId: region.id,+ serverId,+ endpoint,+ apiKey: body.api_key,+ apiSecret: body.api_secret,+ latitude: point ? round(point.latitude) : null,+ longitude: point ? round(point.longitude) : null,+ isActive: true,+ draining: false,+ internalEndpoint: row.internal_endpoint,+ pinnedHostname: row.hostname ?? null,+ pinnedSoftLimit: row.soft_connection_limit != null,+ softConnectionLimit: row.soft_connection_limit ?? body.soft_connection_limit ?? null,+ notes: row.notes ?? `Joined with a join token on ${now.toISOString().slice(0, 10)}`,+ restrictions: {+ vipOnly: row.vip_only ?? false,+ requiredGuildFeatures: new Set(row.required_guild_features ?? []),+ allowedGuildIds: createGuildIDSet(new Set(row.allowed_guild_ids ?? [])),+ allowedUserIds: createUserIDSet(new Set(row.allowed_user_ids ?? [])),+ },+ });+ if (!created) throw new VoiceServerIdInUseError();+ await joinTokenRepository.markUsed(row, claimId, {at: new Date(), ip: clientIp, serverId});+ await this.deps.publish(JSON.stringify({type: 'server_created', regionId: region.id, serverId}));+ await this.deps.auditService.createAuditLog({+ targetId: 0n,+ adminUserId: row.created_by as UserID,+ targetType: 'voice_server',+ action: 'create_voice_server',+ auditLogReason: null,+ metadata: new Map([+ ['region_id', region.id],+ ['server_id', serverId],+ ['endpoint', endpoint],+ ['via', `voice_join:${row.token_id}`],+ ['node_ip', clientIp ?? ''],+ ]),+ });+ return {+ server_id: serverId,+ region_id: region.id,+ endpoint,+ webhook_url: `${Config.endpoints.apiPublic}/webhooks/livekit`,+ node_url: `${Config.endpoints.apiPublic}${VOICE_NODE_PATH}`,+ };+ } catch (error) {+ await joinTokenRepository.release(row, claimId).catch(() => false);+ throw error;+ }+ }++ async authenticateNode(header: string | undefined, method: string, body: string): Promise<VoiceServerRecord> {+ const parts = splitNodeToken(header);+ if (!parts) throw new VoiceNodeSignatureInvalidError();+ const candidates = (await this.deps.voiceRepository.listAllServers()).filter(+ (server) => server.apiKey === parts.issuer,+ );+ if (candidates.length === 0) throw new VoiceNodeUnknownError();+ const server = candidates.find((candidate) => signatureMatches(parts, candidate.apiSecret));+ const {claims} = parts;+ const valid =+ claims['aud'] === VOICE_NODE_AUDIENCE &&+ claims['method'] === method &&+ claims['path'] === VOICE_NODE_PATH &&+ claims['sha256'] === bodyHash(body) &&+ claims['video'] === undefined &&+ typeof claims['exp'] === 'number';+ if (!valid || !server) throw new VoiceNodeSignatureInvalidError();+ const now = Math.floor(Date.now() / 1000);+ const exp = claims['exp'] as number;+ const iat = typeof claims['iat'] === 'number' ? claims['iat'] : null;+ const current =+ exp > now - NODE_TOKEN_LEEWAY_SECONDS &&+ exp <= now + NODE_TOKEN_MAX_AHEAD_SECONDS + NODE_TOKEN_LEEWAY_SECONDS &&+ (iat === null || iat <= now + NODE_TOKEN_LEEWAY_SECONDS);+ if (!current) throw new VoiceNodeTokenExpiredError();+ return server;+ }++ describe(server: VoiceServerRecord): VoiceNodeResponse {+ return {+ region_id: server.regionId,+ server_id: server.serverId,+ endpoint: server.endpoint,+ latitude: server.latitude,+ longitude: server.longitude,+ is_active: server.isActive,+ draining: server.draining,+ soft_connection_limit: server.softConnectionLimit,+ health: toHealthResponse(healthOf(this.deps.health, server)),+ };+ }++ async update(server: VoiceServerRecord, body: UpdateVoiceNodeRequest): Promise<VoiceNodeResponse> {+ const updated: VoiceServerRecord = {...server, updatedAt: new Date()};+ if (body.endpoint !== undefined && body.endpoint !== server.endpoint) {+ updated.endpoint = this.checkEndpoint(body.endpoint, server.pinnedHostname);+ await endpointVerifier(updated.endpoint, server, server.internalEndpoint, true).catch((error: unknown) => {+ throw asApiError(error);+ });+ }+ if (+ server.pinnedSoftLimit &&+ body.soft_connection_limit !== undefined &&+ body.soft_connection_limit !== server.softConnectionLimit+ ) {+ throw InputValidationError.create(+ 'soft_connection_limit',+ 'The join token set the soft limit of this server. Change it in the admin panel.',+ );+ }+ if (body.draining !== undefined) updated.draining = body.draining;+ if (body.latitude !== undefined) updated.latitude = body.latitude;+ if (body.longitude !== undefined) updated.longitude = body.longitude;+ if (body.soft_connection_limit !== undefined) updated.softConnectionLimit = body.soft_connection_limit;+ await this.deps.voiceRepository.upsertServer(updated);+ await this.deps.publish(+ JSON.stringify({type: 'server_updated', regionId: server.regionId, serverId: server.serverId}),+ );+ await this.audit(server, 'update_voice_server', Object.keys(body).sort().join(','));+ return this.describe(updated);+ }++ async remove(server: VoiceServerRecord): Promise<void> {+ await this.deps.voiceRepository.deleteServer(server.regionId, server.serverId);+ await this.deps.publish(+ JSON.stringify({type: 'server_deleted', regionId: server.regionId, serverId: server.serverId}),+ );+ await this.audit(server, 'delete_voice_server', '');+ }++ private async audit(server: VoiceServerRecord, action: string, fields: string): Promise<void> {+ const metadata = new Map([+ ['region_id', server.regionId],+ ['server_id', server.serverId],+ ['via', `voice_node:${server.serverId}`],+ ]);+ if (fields) metadata.set('fields', fields);+ await this.deps.auditService.createAuditLog({+ targetId: 0n,+ adminUserId: 0n as UserID,+ targetType: 'voice_server',+ action,+ auditLogReason: null,+ metadata,+ });+ }++ private checkEndpoint(endpoint: string, hostname: string | null): string {+ try {+ const url = parseNodeEndpoint(endpoint);+ if (hostname && url.hostname.toLowerCase() !== hostname.toLowerCase()) {+ throw new VoiceEndpointError('invalid', `The endpoint must use the hostname ${hostname}`);+ }+ return url.toString().replace(/\/$/, '');+ } catch (error) {+ throw asApiError(error);+ }+ }++ private async locate(ip: string | null): Promise<{latitude: number; longitude: number} | null> {+ if (!ip || !Config.geoip.maxmindDbPath) return null;+ const result = await lookupGeoipByIp(ip, Config.geoip.maxmindDbPath).catch(() => null);+ if (result?.latitude == null || result.longitude == null) return null;+ return {latitude: result.latitude, longitude: result.longitude};+ }++ private async chooseRegion(+ row: VoiceJoinTokenRow,+ requested: string | undefined,+ point: {latitude: number; longitude: number} | null,+ ): Promise<ChosenRegion> {+ const regions = await this.deps.voiceRepository.listRegions();+ const distance = (region: VoiceRegionRecord) =>+ point ? Math.round(calculateDistance(point.latitude, point.longitude, region.latitude, region.longitude)) : null;+ const pick = (region: VoiceRegionRecord, source: RegionSource): ChosenRegion => ({+ id: region.id,+ name: region.name,+ emoji: region.emoji,+ isNew: false,+ distanceKm: distance(region),+ source,+ });+ const wanted = row.region_id ?? requested ?? null;+ if (wanted) {+ const source = row.region_id ? 'token' : 'requested';+ const existing = regions.find((region) => region.id === wanted);+ if (existing) return pick(existing, source);+ if (row.region_id && row.region_name) {+ return {+ id: row.region_id,+ name: row.region_name,+ emoji: row.region_emoji ?? '🌍',+ isNew: true,+ distanceKm: 0,+ source,+ };+ }+ throw new UnknownVoiceRegionError();+ }+ if (regions.length === 0) throw new UnknownVoiceRegionError();+ if (point) {+ const nearest = regions.reduce((best, region) =>+ (distance(region) ?? 0) < (distance(best) ?? 0) ? region : best,+ );+ return pick(nearest, 'nearest');+ }+ return pick(regions.find((region) => region.isDefault) ?? regions[0]!, 'default');+ }++ private suggestServerId(regionId: string, servers: Array<VoiceServerRecord>): string {+ const used = new Set(servers.map((server) => server.serverId));+ const bundled = bundledVoiceServerId();+ if (bundled) used.add(bundled);+ for (let n = 1; ; n++) {+ const id = `${regionId}-server-${n}`;+ if (!used.has(id)) return id;+ }+ }+}++function emptyRestrictions(): VoiceRegionRecord['restrictions'] {+ return {+ vipOnly: false,+ requiredGuildFeatures: new Set(),+ allowedGuildIds: createGuildIDSet(new Set()),+ allowedUserIds: createUserIDSet(new Set()),+ };+}diff --git a/fluxer_api/src/api/voice/VoiceRepository.ts b/fluxer_api/src/api/voice/VoiceRepository.tsindex 7f17b6fcd..967106d1f 100644--- a/fluxer_api/src/api/voice/VoiceRepository.ts+++ b/fluxer_api/src/api/voice/VoiceRepository.ts@@ -1,7 +1,14 @@// SPDX-License-Identifier: AGPL-3.0-or-laterimport {createGuildIDSet, createUserIDSet} from '@app/api/BrandedTypes';-import {BatchBuilder, deleteOneOrMany, fetchMany, fetchOne, upsertOne} from '@app/api/database/CassandraQueryExecution';+import {+ BatchBuilder,+ deleteOneOrMany,+ executeConditional,+ fetchMany,+ fetchOne,+ upsertOne,+} from '@app/api/database/CassandraQueryExecution';import {defineTable} from '@app/api/database/CassandraTableDsl';import {VOICE_REGION_COLUMNS,@@ -136,8 +143,24 @@ export class VoiceRepository {return fullServer;}+ async createServerIfAbsent(server: Omit<VoiceServerRecord, 'createdAt' | 'updatedAt'>): Promise<boolean> {+ const now = new Date();+ return executeConditional(+ VoiceServers.insertIfNotExists(this.toServerRow({...server, createdAt: now, updatedAt: now})),+ );+ }++ async listAllServers(): Promise<Array<VoiceServerRecord>> {+ const regions = await this.listRegionsWithServers();+ return regions.flatMap((region) => region.servers);+ }+async upsertServer(server: VoiceServerRecord): Promise<void> {- const row: VoiceServerRow = {+ await upsertOne(VoiceServers.upsertAll(this.toServerRow(server)));+ }++ private toServerRow(server: VoiceServerRecord): VoiceServerRow {+ return {region_id: server.regionId,server_id: server.serverId,endpoint: server.endpoint,@@ -152,10 +175,13 @@ export class VoiceRepository {required_guild_features: new Set(server.restrictions.requiredGuildFeatures),allowed_guild_ids: new Set(Array.from(server.restrictions.allowedGuildIds).map((id) => BigInt(id))),allowed_user_ids: new Set(Array.from(server.restrictions.allowedUserIds).map((id) => BigInt(id))),+ internal_endpoint: server.internalEndpoint,+ draining: server.draining,+ pinned_hostname: server.pinnedHostname,+ pinned_soft_limit: server.pinnedSoftLimit,created_at: server.createdAt ?? new Date(),updated_at: server.updatedAt ?? new Date(),};- await upsertOne(VoiceServers.upsertAll(row));}async deleteServer(regionId: string, serverId: string): Promise<void> {@@ -199,6 +225,10 @@ export class VoiceRepository {allowedGuildIds: createGuildIDSet(new Set(toIterable<bigint>(row.allowed_guild_ids))),allowedUserIds: createUserIDSet(new Set(toIterable<bigint>(row.allowed_user_ids))),},+ internalEndpoint: row.internal_endpoint ?? null,+ draining: row.draining ?? false,+ pinnedHostname: row.pinned_hostname ?? null,+ pinnedSoftLimit: row.pinned_soft_limit ?? false,createdAt: row.created_at,updatedAt: row.updated_at,};diff --git a/fluxer_api/src/api/voice/VoiceServerEndpoints.ts b/fluxer_api/src/api/voice/VoiceServerEndpoints.tsnew file mode 100644index 000000000..5ded297aa--- /dev/null+++ b/fluxer_api/src/api/voice/VoiceServerEndpoints.ts@@ -0,0 +1,216 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {lookup} from 'node:dns/promises';+import http from 'node:http';+import https from 'node:https';+import {isIP, type LookupFunction} from 'node:net';+import {Config} from '@app/api/Config';+import type {VoiceServerRecord} from '@app/api/voice/VoiceModel';+import {isPublicIpAddress} from '@fluxer/ip_utils/src/IpAddress';+import {AccessToken} from 'livekit-server-sdk';++export function toHttpUrl(wsUrl: string): string {+ return wsUrl.replace(/^wss:\/\//, 'https://').replace(/^ws:\/\//, 'http://');+}++export function bundledVoiceServerId(): string | null {+ const defaultRegion = Config.voice.defaultRegion;+ if (!defaultRegion || !Config.voice.bundled) {+ return null;+ }+ return `${defaultRegion.id}-server-1`;+}++function isConfiguredInternalServer(server: Pick<VoiceServerRecord, 'regionId' | 'serverId'>): boolean {+ const serverId = bundledVoiceServerId();+ if (!serverId || !Config.voice.internalUrl) {+ return false;+ }+ return server.regionId === Config.voice.defaultRegion?.id && server.serverId === serverId;+}++export function resolveRoomServiceEndpoint(server: VoiceServerRecord): string {+ if (server.internalEndpoint) {+ return server.internalEndpoint;+ }+ if (isConfiguredInternalServer(server)) {+ return Config.voice.internalUrl!;+ }+ return server.endpoint;+}++export function usesPublicEndpoint(server: VoiceServerRecord): boolean {+ return !server.internalEndpoint && !isConfiguredInternalServer(server);+}++export function endpointOrigin(endpoint: string): string | null {+ try {+ const url = new URL(endpoint);+ if (url.protocol !== 'wss:' && url.protocol !== 'ws:') {+ return null;+ }+ return `${url.protocol}//${url.host}`;+ } catch {+ return null;+ }+}++export class VoiceEndpointError extends Error {+ constructor(+ readonly reason: 'invalid' | 'private' | 'unreachable' | 'key_mismatch',+ message: string,+ ) {+ super(message);+ this.name = 'VoiceEndpointError';+ }+}++type Resolve = (host: string) => Promise<Array<string>>;++let resolveHost: Resolve = async (host) => (await lookup(host, {all: true, verbatim: true})).map((r) => r.address);++export function setVoiceEndpointResolverForTesting(resolver: Resolve | null): void {+ resolveHost = resolver ?? (async (host) => (await lookup(host, {all: true, verbatim: true})).map((r) => r.address));+}++function bareHost(url: URL): string {+ return url.hostname.replace(/^\[|\]$/g, '');+}++export function parseNodeEndpoint(endpoint: string): URL {+ let url: URL;+ try {+ url = new URL(endpoint);+ } catch {+ throw new VoiceEndpointError('invalid', 'The endpoint is not a URL');+ }+ if (url.protocol !== 'wss:') {+ throw new VoiceEndpointError('invalid', 'The endpoint must start with wss://');+ }+ if (url.username || url.password || url.search || url.hash) {+ throw new VoiceEndpointError('invalid', 'The endpoint must not have credentials, a query or a fragment');+ }+ if (isIP(bareHost(url)) === 6) {+ throw new VoiceEndpointError('invalid', 'An endpoint that is an IP address must be IPv4');+ }+ return url;+}++export async function resolveCheckedAddress(url: URL, allowPrivate: boolean): Promise<string> {+ const host = bareHost(url);+ const addresses = isIP(host) ? [host] : await resolveHost(host).catch(() => []);+ if (addresses.length === 0) {+ throw new VoiceEndpointError('unreachable', `${host} does not resolve`);+ }+ if (!allowPrivate && addresses.some((address) => !isPublicIpAddress(address))) {+ throw new VoiceEndpointError('private', `${host} resolves to an address that is not public`);+ }+ return addresses[0]!;+}++interface ProbeResponse {+ status: number;+ body: string;+}++function pinnedLookup(address: string): LookupFunction {+ const family = isIP(address);+ return (_hostname, opts, callback) => {+ if (opts.all) {+ callback(null, [{address, family}]);+ return;+ }+ callback(null, address, family);+ };+}++function request(+ url: URL,+ options: {method: string; address: string | null; headers?: Record<string, string>; body?: string; timeoutMs: number},+): Promise<ProbeResponse> {+ const client = url.protocol === 'https:' ? https : http;+ const host = bareHost(url);+ return new Promise((resolve, reject) => {+ const req = client.request(+ url,+ {+ method: options.method,+ headers: {...options.headers, Host: url.host},+ servername: isIP(host) ? undefined : host,+ timeout: options.timeoutMs,+ lookup: options.address ? pinnedLookup(options.address) : undefined,+ },+ (res) => {+ let body = '';+ res.setEncoding('utf8');+ res.on('data', (chunk: string) => {+ if (body.length < 4096) body += chunk;+ });+ res.on('end', () => resolve({status: res.statusCode ?? 0, body}));+ res.on('error', reject);+ },+ );+ req.on('timeout', () => req.destroy(new Error('timed out')));+ req.on('error', reject);+ req.end(options.body);+ });+}++function withPath(base: string, path: string): URL {+ const url = new URL(toHttpUrl(base));+ url.pathname = `${url.pathname.replace(/\/+$/, '')}${path}`;+ return url;+}++function transportError(error: unknown): string {+ if (error && typeof error === 'object' && 'code' in error && typeof error.code === 'string') {+ return error.code;+ }+ return error instanceof Error ? error.message : 'connection failed';+}++export async function probeLiveKit(+ base: string,+ options: {checkAddress: boolean; allowPrivate: boolean; timeoutMs: number},+): Promise<void> {+ const url = withPath(base, '/');+ const address = options.checkAddress ? await resolveCheckedAddress(url, options.allowPrivate) : null;+ let response: ProbeResponse;+ try {+ response = await request(url, {method: 'GET', address, timeoutMs: options.timeoutMs});+ } catch (error) {+ throw new VoiceEndpointError('unreachable', transportError(error));+ }+ if (response.status !== 200 || response.body.trim() !== 'OK') {+ throw new VoiceEndpointError('unreachable', `HTTP ${response.status}`);+ }+}++export async function checkLiveKitKeys(+ base: string,+ keys: {apiKey: string; apiSecret: string},+ options: {checkAddress: boolean; allowPrivate: boolean; timeoutMs: number},+): Promise<void> {+ const url = withPath(base, '/twirp/livekit.RoomService/ListRooms');+ const address = options.checkAddress ? await resolveCheckedAddress(url, options.allowPrivate) : null;+ const token = new AccessToken(keys.apiKey, keys.apiSecret, {ttl: 60});+ token.addGrant({roomList: true});+ let response: ProbeResponse;+ try {+ response = await request(url, {+ method: 'POST',+ address,+ timeoutMs: options.timeoutMs,+ headers: {Authorization: `Bearer ${await token.toJwt()}`, 'Content-Type': 'application/json'},+ body: '{}',+ });+ } catch (error) {+ throw new VoiceEndpointError('unreachable', transportError(error));+ }+ if (response.status === 401 || response.status === 403) {+ throw new VoiceEndpointError('key_mismatch', `HTTP ${response.status}`);+ }+ if (response.status !== 200) {+ throw new VoiceEndpointError('unreachable', `HTTP ${response.status}`);+ }+}diff --git a/fluxer_api/src/api/voice/VoiceServerHealth.ts b/fluxer_api/src/api/voice/VoiceServerHealth.tsnew file mode 100644index 000000000..ece4a22b3--- /dev/null+++ b/fluxer_api/src/api/voice/VoiceServerHealth.ts@@ -0,0 +1,129 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {Config} from '@app/api/Config';+import {Logger} from '@app/api/Logger';+import type {VoiceServerRecord} from '@app/api/voice/VoiceModel';+import {+ probeLiveKit,+ resolveRoomServiceEndpoint,+ usesPublicEndpoint,+ VoiceEndpointError,+} from '@app/api/voice/VoiceServerEndpoints';+import type {VoiceTopology} from '@app/api/voice/VoiceTopology';++export type VoiceServerHealthStatus = 'up' | 'down' | 'unknown';++export interface VoiceServerHealthState {+ status: VoiceServerHealthStatus;+ checkedAt: Date | null;+ error: string | null;+}++export interface VoiceServerHealthSource {+ get(server: Pick<VoiceServerRecord, 'regionId' | 'serverId'>): VoiceServerHealthState;+}++const FAILURES_BEFORE_DOWN = 2;+const PROBE_TIMEOUT_MS = 3000;+const UNKNOWN: VoiceServerHealthState = {status: 'unknown', checkedAt: null, error: null};++type Probe = (server: VoiceServerRecord) => Promise<void>;++const probeVoiceServer: Probe = (server) =>+ probeLiveKit(resolveRoomServiceEndpoint(server), {+ checkAddress: usesPublicEndpoint(server),+ allowPrivate: Config.voice.joinAllowPrivateEndpoints,+ timeoutMs: PROBE_TIMEOUT_MS,+ });++interface Entry extends VoiceServerHealthState {+ failures: number;+}++function key(server: Pick<VoiceServerRecord, 'regionId' | 'serverId'>): string {+ return `${server.regionId}\u0000${server.serverId}`;+}++export class VoiceServerHealthMonitor implements VoiceServerHealthSource {+ private entries = new Map<string, Entry>();+ private timer: NodeJS.Timeout | null = null;+ private running = false;++ constructor(+ private readonly topology: Pick<VoiceTopology, 'getAllRegions' | 'getServersForRegion'>,+ private readonly probe: Probe = probeVoiceServer,+ private readonly intervalMs: number = Config.voice.healthIntervalMs,+ ) {}++ get(server: Pick<VoiceServerRecord, 'regionId' | 'serverId'>): VoiceServerHealthState {+ const entry = this.entries.get(key(server));+ return entry ? {status: entry.status, checkedAt: entry.checkedAt, error: entry.error} : UNKNOWN;+ }++ start(): void {+ if (this.timer || this.intervalMs <= 0) return;+ this.timer = setInterval(() => void this.checkAll(), this.intervalMs);+ this.timer.unref();+ void this.checkAll();+ }++ stop(): void {+ if (this.timer) clearInterval(this.timer);+ this.timer = null;+ }++ async checkAll(): Promise<void> {+ if (this.running) return;+ this.running = true;+ try {+ const servers = this.topology+ .getAllRegions()+ .flatMap((region) => this.topology.getServersForRegion(region.id))+ .filter((server) => server.isActive);+ const live = new Set(servers.map(key));+ for (const k of this.entries.keys()) {+ if (!live.has(k)) this.entries.delete(k);+ }+ await Promise.all(servers.map((server) => this.check(server)));+ } finally {+ this.running = false;+ }+ }++ private async check(server: VoiceServerRecord): Promise<void> {+ const k = key(server);+ const previous = this.entries.get(k) ?? {...UNKNOWN, failures: 0};+ const checkedAt = new Date();+ try {+ await this.probe(server);+ this.entries.set(k, {status: 'up', checkedAt, error: null, failures: 0});+ } catch (error) {+ const message = error instanceof Error ? error.message : 'probe failed';+ if (error instanceof VoiceEndpointError && error.reason === 'private') {+ this.entries.set(k, {status: 'unknown', checkedAt, error: message, failures: 0});+ return;+ }+ const failures = previous.failures + 1;+ const status = failures >= FAILURES_BEFORE_DOWN ? 'down' : previous.status;+ if (status === 'down' && previous.status !== 'down') {+ Logger.warn({regionId: server.regionId, serverId: server.serverId, error: message}, 'Voice server is down');+ }+ this.entries.set(k, {status, checkedAt, error: message, failures});+ }+ }+}++export function toHealthResponse(state: VoiceServerHealthState): {+ status: VoiceServerHealthStatus;+ checked_at: string | null;+ error: string | null;+} {+ return {status: state.status, checked_at: state.checkedAt?.toISOString() ?? null, error: state.error};+}++export function healthOf(+ source: VoiceServerHealthSource | null,+ server: Pick<VoiceServerRecord, 'regionId' | 'serverId'>,+): VoiceServerHealthState {+ return source?.get(server) ?? UNKNOWN;+}diff --git a/fluxer_api/src/api/voice/VoiceService.ts b/fluxer_api/src/api/voice/VoiceService.tsindex 787b281fd..10ad68e3f 100644--- a/fluxer_api/src/api/voice/VoiceService.ts+++ b/fluxer_api/src/api/voice/VoiceService.ts@@ -193,11 +193,11 @@ export class VoiceService {if (!regionId) {throw new FeatureTemporarilyDisabledError();}- const serverSelection = this.selectServerForRegion({+ const serverSelection = this.voiceAvailabilityService.selectServerOrFallback(regionId,context,accessibleRegions,- });+ );if (!serverSelection) {throw new FeatureTemporarilyDisabledError();}@@ -321,7 +321,11 @@ export class VoiceService {return null;}const serverRecord = this.liveKitService.getServer(pinnedServer.regionId, pinnedServer.serverId);- if (serverRecord && this.voiceAvailabilityService.isServerAccessible(serverRecord, context)) {+ if (+ serverRecord &&+ this.voiceAvailabilityService.isServerAccessible(serverRecord, context) &&+ !this.voiceAvailabilityService.isServerDown(serverRecord)+ ) {return {regionId: pinnedServer.regionId,serverId: pinnedServer.serverId,@@ -332,35 +336,6 @@ export class VoiceService {return null;}- private selectServerForRegion({- regionId,- context,- accessibleRegions,- }: {- regionId: string;- context: VoiceAccessContext;- accessibleRegions: Array<VoiceRegionAvailability>;- }): {- regionId: string;- server: VoiceServerRecord;- } | null {- const initialServer = this.voiceAvailabilityService.selectServer(regionId, context);- if (initialServer) {- return {regionId, server: initialServer};- }- const fallbackRegion = accessibleRegions.find((region) => region.id !== regionId);- if (fallbackRegion) {- const fallbackServer = this.voiceAvailabilityService.selectServer(fallbackRegion.id, context);- if (fallbackServer) {- return {- regionId: fallbackRegion.id,- server: fallbackServer,- };- }- }- return null;- }-private getAccessibleServersForRegions(accessibleRegions: Array<VoiceRegionAvailability>,context: VoiceAccessContext,@@ -369,7 +344,7 @@ export class VoiceService {for (const region of accessibleRegions) {servers.push(...this.voiceAvailabilityService.getAccessibleServersForRegion(region.id, context));}- return servers;+ return this.voiceAvailabilityService.preferPlaceableServers(servers);}async updateParticipant(params: {diff --git a/fluxer_api/src/api/voice/tests/VoiceAvailabilityService.test.ts b/fluxer_api/src/api/voice/tests/VoiceAvailabilityService.test.tsindex 551aa6ea5..e1cf75169 100644--- a/fluxer_api/src/api/voice/tests/VoiceAvailabilityService.test.ts+++ b/fluxer_api/src/api/voice/tests/VoiceAvailabilityService.test.ts@@ -47,6 +47,10 @@ function createServer(overrides: Partial<VoiceServerRecord> = {}): VoiceServerReisActive: true,softConnectionLimit: null,notes: null,+ internalEndpoint: null,+ pinnedHostname: null,+ pinnedSoftLimit: false,+ draining: false,restrictions: {vipOnly: false,requiredGuildFeatures: new Set(),@@ -437,6 +441,76 @@ describe('VoiceAvailabilityService', () => {});});describe('selectServer', () => {+ const context: VoiceAccessContext = {requestingUserId: createUserID(123n)};+ const downOnly = (ids: Array<string>) => ({+ get: (server: {serverId: string}) => ({+ status: ids.includes(server.serverId) ? ('down' as const) : ('up' as const),+ checkedAt: null,+ error: null,+ }),+ });+ it('sends new calls past draining and down servers', async () => {+ const servers = [+ createServer({serverId: 'server-1', apiKey: 'k1', draining: true}),+ createServer({serverId: 'server-2', apiKey: 'k2'}),+ createServer({serverId: 'server-3', apiKey: 'k3'}),+ ];+ const topology = await createTopology([createRegion()], new Map([['us-default', servers]]));+ service = new VoiceAvailabilityService(topology, null, downOnly(['server-2']));+ for (let i = 0; i < 4; i++) {+ expect(service.selectServer('us-default', context)?.serverId).toBe('server-3');+ }+ expect(service.isServerAccessible(servers[0]!, context)).toBe(true);+ expect(service.isServerDown(servers[1]!)).toBe(true);+ });+ it('still places calls when every server is down or draining', async () => {+ const servers = [+ createServer({serverId: 'server-1', apiKey: 'k1', draining: true}),+ createServer({serverId: 'server-2', apiKey: 'k2'}),+ ];+ const topology = await createTopology([createRegion()], new Map([['us-default', servers]]));+ service = new VoiceAvailabilityService(topology, null, downOnly(['server-2']));+ expect(service.selectServer('us-default', context)?.serverId).toBe('server-2');+ service = new VoiceAvailabilityService(topology, null, downOnly(['server-1', 'server-2']));+ expect(service.selectServer('us-default', context)?.serverId).toBe('server-2');+ const drainingOnly = await createTopology(+ [createRegion({id: 'eu-draining', isDefault: false})],+ new Map([+ [+ 'eu-draining',+ [createServer({regionId: 'eu-draining', serverId: 'server-9', apiKey: 'k9', draining: true})],+ ],+ ]),+ );+ service = new VoiceAvailabilityService(drainingOnly);+ expect(service.selectServer('eu-draining', context)?.serverId).toBe('server-9');+ });+ it('moves a call to another region when every server in its region is down', async () => {+ const fallbackRegion = createRegion({id: 'eu-fallback', isDefault: false});+ const topology = await createTopology(+ [createRegion(), fallbackRegion],+ new Map([+ ['us-default', [createServer({serverId: 'default-server-1', apiKey: 'k1'})]],+ ['eu-fallback', [createServer({regionId: 'eu-fallback', serverId: 'eu-server-1', apiKey: 'k2'})]],+ ]),+ );+ service = new VoiceAvailabilityService(topology, null, downOnly(['default-server-1']));+ const regions = service.getAvailableRegions(context).filter((region) => region.isAccessible);+ expect(service.selectServerOrFallback('us-default', context, regions)).toMatchObject({+ regionId: 'eu-fallback',+ server: {serverId: 'eu-server-1'},+ });+ service = new VoiceAvailabilityService(topology, null, downOnly(['default-server-1', 'eu-server-1']));+ expect(service.selectServerOrFallback('us-default', context, regions)).toMatchObject({+ regionId: 'us-default',+ server: {serverId: 'default-server-1'},+ });+ service = new VoiceAvailabilityService(topology, null, downOnly([]));+ expect(service.selectServerOrFallback('us-default', context, regions)).toMatchObject({+ regionId: 'us-default',+ server: {serverId: 'default-server-1'},+ });+ });it('returns server from specified region', async () => {const region = createRegion();const server = createServer();diff --git a/fluxer_api/src/api/voice/tests/VoiceDataInitializer.test.ts b/fluxer_api/src/api/voice/tests/VoiceDataInitializer.test.tsnew file mode 100644index 000000000..998cb426a--- /dev/null+++ b/fluxer_api/src/api/voice/tests/VoiceDataInitializer.test.ts@@ -0,0 +1,115 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {Config} from '@app/api/Config';+import {resolveLivekitEndpoint, VoiceDataInitializer} from '@app/api/voice/VoiceDataInitializer';+import type {VoiceServerRecord} from '@app/api/voice/VoiceModel';+import {VoiceRepository} from '@app/api/voice/VoiceRepository';+import {afterEach, beforeEach, describe, expect, it} from 'vitest';++const BUNDLED_KEY = 'bundled-key';+const BUNDLED_SECRET = 's'.repeat(64);++function joinedServer(overrides: Partial<VoiceServerRecord> = {}): VoiceServerRecord {+ return {+ regionId: 'default',+ serverId: 'default-server-1',+ endpoint: 'wss://203.0.113.7/livekit-default-server-1',+ apiKey: 'joined-key',+ apiSecret: 'j'.repeat(64),+ latitude: null,+ longitude: null,+ isActive: true,+ softConnectionLimit: null,+ notes: null,+ internalEndpoint: null,+ pinnedHostname: null,+ pinnedSoftLimit: false,+ draining: false,+ restrictions: {+ vipOnly: false,+ requiredGuildFeatures: new Set(),+ allowedGuildIds: new Set(),+ allowedUserIds: new Set(),+ },+ createdAt: new Date(),+ updatedAt: new Date(),+ ...overrides,+ };+}++describe('VoiceDataInitializer', () => {+ let before: typeof Config.voice;++ beforeEach(() => {+ before = {...Config.voice};+ Config.voice.enabled = true;+ Config.voice.apiKey = BUNDLED_KEY;+ Config.voice.apiSecret = BUNDLED_SECRET;+ Config.voice.defaultRegion = {id: 'default', name: 'Default', emoji: ':earth_africa:', latitude: 0, longitude: 0};+ Config.voice.bundled = true;+ });++ afterEach(() => {+ Object.assign(Config.voice, before);+ });++ it('seeds the default region and the bundled server', async () => {+ await new VoiceDataInitializer().initialize();+ const repository = new VoiceRepository();+ expect(await repository.getRegion('default')).toMatchObject({id: 'default', isDefault: true});+ expect(await repository.getServer('default', 'default-server-1')).toMatchObject({+ apiKey: BUNDLED_KEY,+ endpoint: resolveLivekitEndpoint(Config.voice.url, Config.endpoints.apiPublic),+ });+ });++ it('seeds only the default region when the bundled server is off', async () => {+ Config.voice.bundled = false;+ await new VoiceDataInitializer().initialize();+ const repository = new VoiceRepository();+ expect(await repository.getRegion('default')).toMatchObject({id: 'default', isDefault: true});+ expect(await repository.listServers('default')).toEqual([]);+ });++ it('seeds the region without LiveKit keys when the bundled server is off', async () => {+ Config.voice.bundled = false;+ Config.voice.apiKey = undefined;+ Config.voice.apiSecret = undefined;+ await new VoiceDataInitializer().initialize();+ expect(await new VoiceRepository().getRegion('default')).not.toBeNull();+ });++ it('removes the bundled server once the bundled LiveKit is turned off', async () => {+ await new VoiceDataInitializer().initialize();+ Config.voice.bundled = false;+ await new VoiceDataInitializer().initialize();+ const repository = new VoiceRepository();+ expect(await repository.getServer('default', 'default-server-1')).toBeNull();+ expect(await repository.getRegion('default')).not.toBeNull();+ });++ it('leaves a joined server with the bundled ID unchanged', async () => {+ await new VoiceDataInitializer().initialize();+ const repository = new VoiceRepository();+ const joined = joinedServer();+ await repository.upsertServer(joined);+ await new VoiceDataInitializer().initialize();+ expect(await repository.getServer('default', 'default-server-1')).toMatchObject({+ endpoint: joined.endpoint,+ apiKey: joined.apiKey,+ apiSecret: joined.apiSecret,+ });+ Config.voice.bundled = false;+ await new VoiceDataInitializer().initialize();+ expect(await repository.getServer('default', 'default-server-1')).toMatchObject({apiKey: joined.apiKey});+ });++ it('syncs new LiveKit keys onto the bundled server', async () => {+ await new VoiceDataInitializer().initialize();+ Config.voice.apiKey = 'rotated-key';+ await new VoiceDataInitializer().initialize();+ expect(await new VoiceRepository().getServer('default', 'default-server-1')).toMatchObject({+ apiKey: 'rotated-key',+ });+ });+});diff --git a/fluxer_api/src/api/voice/tests/VoiceJoin.test.ts b/fluxer_api/src/api/voice/tests/VoiceJoin.test.tsnew file mode 100644index 000000000..0952739ba--- /dev/null+++ b/fluxer_api/src/api/voice/tests/VoiceJoin.test.ts@@ -0,0 +1,504 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {createHash, createHmac} from 'node:crypto';+import {createAdminWithAcls} from '@app/api/admin/tests/AdminTestUtils';+import type {TestAccount} from '@app/api/auth/tests/AuthTestUtils';+import {Config} from '@app/api/Config';+import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';+import {server} from '@app/api/test/msw/server';+import {HTTP_STATUS} from '@app/api/test/TestConstants';+import {createBuilder, createBuilderWithoutAuth} from '@app/api/test/TestRequestBuilder';+import {VoiceJoinTokenRepository} from '@app/api/voice/VoiceJoinTokenRepository';+import {+ hashJoinSecret,+ setVoiceNodeEndpointVerifierForTesting,+ VOICE_NODE_AUDIENCE,+ VOICE_NODE_PATH,+ voiceJoinCommand,+} from '@app/api/voice/VoiceNodeService';+import {VoiceRepository} from '@app/api/voice/VoiceRepository';+import {setVoiceEndpointResolverForTesting, VoiceEndpointError} from '@app/api/voice/VoiceServerEndpoints';+import {AdminACLs} from '@fluxer/constants/src/AdminACLs';+import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import type {+ CreateVoiceJoinTokenResponse,+ ListVoiceJoinTokensResponse,+} from '@fluxer/schema/src/domains/admin/AdminVoiceSchemas';+import type {+ VoiceJoinPreviewResponse,+ VoiceJoinResponse,+ VoiceNodeResponse,+} from '@fluxer/schema/src/domains/voice/VoiceNodeSchemas';+import {AccessToken} from 'livekit-server-sdk';+import {HttpResponse, http} from 'msw';+import {afterEach, beforeEach, describe, expect, test} from 'vitest';++const SECRET = 'a'.repeat(64);++function joinBody(overrides: Record<string, unknown> = {}) {+ return {+ endpoint: 'wss://203.0.113.7/livekit-fra-server-1',+ api_key: 'fluxer-fra-server-1',+ api_secret: SECRET,+ ...overrides,+ };+}++function nodeJwt(claims: Record<string, unknown>, secret = SECRET): string {+ const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url');+ const input = `${encode({alg: 'HS256', typ: 'JWT'})}.${encode(claims)}`;+ return `Bearer ${input}.${createHmac('sha256', secret).update(input).digest('base64url')}`;+}++function nodeClaims(method: string, body: string, overrides: Record<string, unknown> = {}) {+ return {+ iss: 'fluxer-fra-server-1',+ aud: VOICE_NODE_AUDIENCE,+ method,+ path: VOICE_NODE_PATH,+ exp: Math.floor(Date.now() / 1000) + 60,+ sha256: body === '' ? '' : createHash('sha256').update(body).digest('base64'),+ ...overrides,+ };+}++describe('voice join tokens and node routes', () => {+ let harness: ApiTestHarness;+ let admin: TestAccount;+ let verified: Array<string>;++ async function createRegion(id: string) {+ await createBuilder(harness, admin.token)+ .post('/admin/voice/regions')+ .body({id, name: `Region ${id}`, emoji: ':earth_africa:', latitude: 50, longitude: 8})+ .execute();+ }++ async function createToken(body: Record<string, unknown> = {}) {+ return createBuilder<CreateVoiceJoinTokenResponse>(harness, admin.token)+ .post('/admin/voice/join-tokens')+ .body(body)+ .execute();+ }++ async function join(token: string, body = joinBody(), status: number = HTTP_STATUS.OK, code?: string) {+ return createBuilder<VoiceJoinResponse>(harness, `VoiceJoin ${token}`)+ .post('/voice/join')+ .body(body)+ .expect(status, code)+ .execute();+ }++ beforeEach(async () => {+ harness = await createApiTestHarness();+ admin = await createAdminWithAcls(harness, [+ AdminACLs.VOICE_REGION_CREATE,+ AdminACLs.VOICE_REGION_LIST,+ AdminACLs.VOICE_SERVER_CREATE,+ AdminACLs.VOICE_SERVER_LIST,+ AdminACLs.VOICE_SERVER_UPDATE,+ AdminACLs.VOICE_SERVER_DELETE,+ AdminACLs.VOICE_JOIN_TOKEN_CREATE,+ AdminACLs.VOICE_JOIN_TOKEN_LIST,+ AdminACLs.VOICE_JOIN_TOKEN_REVOKE,+ ]);+ verified = [];+ setVoiceNodeEndpointVerifierForTesting(async (endpoint) => {+ verified.push(endpoint);+ });+ await createRegion('fra');+ });++ afterEach(async () => {+ setVoiceNodeEndpointVerifierForTesting(null);+ await harness?.shutdown();+ });++ test('creates a token that previews without being used and joins once', async () => {+ const created = await createToken({region_id: 'fra'});+ expect(created.token).toMatch(/^fvj_\d+_[A-Za-z0-9_-]{43}$/);+ expect(created.command).toContain(`voice `);+ expect(created.command).toContain(created.token);+ const preview = await createBuilder<VoiceJoinPreviewResponse>(harness, `VoiceJoin ${created.token}`)+ .post('/voice/join/preview')+ .body({public_ipv4: '203.0.113.7'})+ .execute();+ expect(preview.region.id).toBe('fra');+ expect(preview.region.source).toBe('token');+ expect(preview.server_id).toBe('fra-server-1');+ expect(preview.server_id_pinned).toBe(false);+ expect(preview.webhook_url).toMatch(/\/webhooks\/livekit$/);+ const joined = await join(created.token);+ expect(joined).toMatchObject({server_id: 'fra-server-1', region_id: 'fra'});+ expect(verified).toEqual(['wss://203.0.113.7/livekit-fra-server-1']);+ const server = await new VoiceRepository().getServer('fra', 'fra-server-1');+ expect(server).toMatchObject({apiKey: 'fluxer-fra-server-1', isActive: true, draining: false});+ await join(+ created.token,+ joinBody({api_key: 'other'}),+ HTTP_STATUS.UNAUTHORIZED,+ APIErrorCodes.VOICE_JOIN_TOKEN_INVALID,+ );+ const list = await createBuilder<ListVoiceJoinTokensResponse>(harness, admin.token)+ .get('/admin/voice/join-tokens')+ .execute();+ expect(list.tokens[0]).toMatchObject({token_id: created.token_id, status: 'used', used_server_id: 'fra-server-1'});+ expect(JSON.stringify(list)).not.toContain(created.token);+ });++ test('keeps the bundled server ID for the bundled LiveKit even after it is deleted', async () => {+ const before = {defaultRegion: Config.voice.defaultRegion, bundled: Config.voice.bundled};+ Config.voice.defaultRegion = {id: 'fra', name: 'Region fra', emoji: ':earth_africa:', latitude: 50, longitude: 8};+ Config.voice.bundled = true;+ try {+ const created = await createToken({region_id: 'fra'});+ const preview = await createBuilder<VoiceJoinPreviewResponse>(harness, `VoiceJoin ${created.token}`)+ .post('/voice/join/preview')+ .body({public_ipv4: '203.0.113.7'})+ .execute();+ expect(preview.server_id).toBe('fra-server-2');+ await join(+ created.token,+ joinBody({server_id: 'fra-server-1'}),+ HTTP_STATUS.CONFLICT,+ APIErrorCodes.VOICE_SERVER_ID_IN_USE,+ );+ const joined = await join(created.token);+ expect(joined).toMatchObject({server_id: 'fra-server-2', region_id: 'fra'});+ expect(await new VoiceRepository().getServer('fra', 'fra-server-1')).toBeNull();+ await createBuilder(harness, admin.token)+ .post('/admin/voice/join-tokens')+ .body({region_id: 'fra', server_id: 'fra-server-1'})+ .expect(HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_SERVER_ID_IN_USE)+ .execute();+ Config.voice.bundled = false;+ const unbundled = await createToken({region_id: 'fra', server_id: 'fra-server-1'});+ expect(await join(unbundled.token, joinBody({api_key: 'joined-fra-1'}))).toMatchObject({+ server_id: 'fra-server-1',+ });+ } finally {+ Config.voice.defaultRegion = before.defaultRegion;+ Config.voice.bundled = before.bundled;+ }+ });++ test('rejects revoked, expired, malformed and misplaced tokens', async () => {+ const revoked = await createToken();+ await createBuilder(harness, admin.token).delete(`/admin/voice/join-tokens/${revoked.token_id}`).execute();+ await join(revoked.token, joinBody(), HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_JOIN_TOKEN_INVALID);+ const secret = 'b'.repeat(43);+ await new VoiceJoinTokenRepository().create({+ token_id: 42n,+ secret_hash: hashJoinSecret(secret),+ created_by: 1n,+ created_at: new Date(Date.now() - 7200_000),+ expires_at: new Date(Date.now() - 3600_000),+ claim_id: null,+ claimed_at: null,+ used_at: null,+ used_by_ip: null,+ used_server_id: null,+ revoked_at: null,+ region_id: null,+ region_name: null,+ region_emoji: null,+ region_latitude: null,+ region_longitude: null,+ server_id: null,+ hostname: null,+ internal_endpoint: null,+ soft_connection_limit: null,+ notes: null,+ vip_only: false,+ required_guild_features: null,+ allowed_guild_ids: null,+ allowed_user_ids: null,+ });+ await join(`fvj_42_${secret}`, joinBody(), HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_JOIN_TOKEN_INVALID);+ await join('fvj_1_short', joinBody(), HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_JOIN_TOKEN_INVALID);+ const pending = await createToken();+ await createBuilder(harness, `VoiceJoin ${pending.token}`)+ .get('/admin/voice/join-tokens')+ .expect(HTTP_STATUS.UNAUTHORIZED)+ .execute();+ await createBuilder(harness, `Bearer ${pending.token}`)+ .get('/users/@me')+ .expect(HTTP_STATUS.UNAUTHORIZED)+ .execute();+ });++ test('releases the claim when the endpoint check fails so the token works again', async () => {+ const created = await createToken();+ setVoiceNodeEndpointVerifierForTesting(async () => {+ throw new VoiceEndpointError('unreachable', 'HTTP 502');+ });+ await join(created.token, joinBody(), HTTP_STATUS.BAD_REQUEST, APIErrorCodes.VOICE_JOIN_ENDPOINT_UNREACHABLE);+ setVoiceNodeEndpointVerifierForTesting(async () => {+ throw new VoiceEndpointError('key_mismatch', 'HTTP 401');+ });+ await join(created.token, joinBody(), HTTP_STATUS.BAD_REQUEST, APIErrorCodes.VOICE_JOIN_KEY_MISMATCH);+ setVoiceNodeEndpointVerifierForTesting(async () => {});+ await join(created.token);+ });++ test('refuses an endpoint that is not wss, an IPv6 literal or another hostname than the preset', async () => {+ const created = await createToken({hostname: 'voice.example.com'});+ for (const endpoint of [+ 'https://voice.example.com/livekit-x',+ 'wss://[2001:db8::1]/livekit-x',+ 'wss://other.example.com/livekit-x',+ ]) {+ await createBuilder(harness, `VoiceJoin ${created.token}`)+ .post('/voice/join')+ .body(joinBody({endpoint}))+ .expect(HTTP_STATUS.BAD_REQUEST)+ .execute();+ }+ await join(created.token, joinBody({endpoint: 'wss://voice.example.com/livekit-fra-server-1'}));+ });++ test('rejects a duplicate server id or api key and two joins with one token', async () => {+ const first = await createToken({server_id: 'fra-server-1'});+ const pinned = await createBuilder<VoiceJoinPreviewResponse>(harness, `VoiceJoin ${first.token}`)+ .post('/voice/join/preview')+ .body({})+ .execute();+ expect(pinned).toMatchObject({+ server_id: 'fra-server-1',+ server_id_pinned: true,+ region: {id: 'fra', source: 'default'},+ });+ const requested = await createBuilder<VoiceJoinPreviewResponse>(harness, `VoiceJoin ${first.token}`)+ .post('/voice/join/preview')+ .body({region_id: 'fra'})+ .execute();+ expect(requested.region.source).toBe('requested');+ await join(first.token);+ const second = await createToken({server_id: 'fra-server-1'});+ await join(second.token, joinBody({api_key: 'fresh'}), HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_SERVER_ID_IN_USE);+ await join(second.token, joinBody({server_id: 'x'}), HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_SERVER_ID_IN_USE);+ const third = await createToken();+ await join(third.token, joinBody(), HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_SERVER_API_KEY_IN_USE);+ const racing = await createToken();+ let release: () => void = () => {};+ setVoiceNodeEndpointVerifierForTesting(() => new Promise<void>((resolve) => (release = resolve)));+ const winner = join(racing.token, joinBody({api_key: 'race-a', server_id: 'fra-race'}));+ await new Promise((resolve) => setTimeout(resolve, 50));+ await join(racing.token, joinBody({api_key: 'race-b'}), HTTP_STATUS.CONFLICT, APIErrorCodes.VOICE_JOIN_TOKEN_BUSY);+ release();+ await winner;+ expect(await new VoiceRepository().getServer('fra', 'fra-race')).toMatchObject({apiKey: 'race-a'});+ });++ test('creates a new region only from a token preset', async () => {+ const created = await createToken({+ region_id: 'sgp',+ region_name: 'Singapore',+ region_emoji: '🇸🇬',+ region_latitude: 1.35,+ region_longitude: 103.8,+ });+ const joined = await join(created.token);+ expect(joined.region_id).toBe('sgp');+ expect(await new VoiceRepository().getRegion('sgp')).toMatchObject({+ name: 'Singapore',+ latitude: 1.35,+ longitude: 103.8,+ });+ await createBuilder(harness, admin.token)+ .post('/admin/voice/join-tokens')+ .body({region_id: 'ams', region_name: 'Amsterdam', region_latitude: 52.4})+ .expect(HTTP_STATUS.BAD_REQUEST)+ .execute();+ await createBuilder(harness, admin.token)+ .post('/admin/voice/join-tokens')+ .body({region_id: 'fra', region_latitude: 50, region_longitude: 8})+ .expect(HTTP_STATUS.BAD_REQUEST)+ .execute();+ const other = await createToken();+ await join(other.token, joinBody({api_key: 'k2', region_id: 'nowhere'}), HTTP_STATUS.NOT_FOUND);+ });++ describe('node routes', () => {+ beforeEach(async () => {+ const created = await createToken({server_id: 'fra-server-1'});+ await join(created.token);+ });++ async function nodeRequest(method: 'GET' | 'PATCH' | 'DELETE', auth: string, body?: unknown) {+ const builder = createBuilder<VoiceNodeResponse>(harness, auth);+ const routed = {+ GET: () => builder.get(VOICE_NODE_PATH),+ PATCH: () => builder.patch(VOICE_NODE_PATH),+ DELETE: () => builder.delete(VOICE_NODE_PATH),+ }[method]();+ return body === undefined ? routed : routed.body(body);+ }++ test('reads and updates its own row', async () => {+ const node = await (await nodeRequest('GET', nodeJwt(nodeClaims('GET', '')))).execute();+ expect(node).toMatchObject({server_id: 'fra-server-1', draining: false, is_active: true});+ const body = {draining: true};+ const updated = await (+ await nodeRequest('PATCH', nodeJwt(nodeClaims('PATCH', JSON.stringify(body))), body)+ ).execute();+ expect(updated.draining).toBe(true);+ });++ test('allows a minute of clock skew and reports a larger one', async () => {+ const now = Math.floor(Date.now() / 1000);+ const ok = [+ {iat: now - 170, exp: now - 50},+ {iat: now + 50, exp: now + 170},+ ];+ for (const times of ok) {+ await (await nodeRequest('GET', nodeJwt(nodeClaims('GET', '', times)))).execute();+ }+ const skewed = [{iat: now - 300, exp: now - 180}, {iat: now + 180, exp: now + 300}, {exp: now + 3600}];+ for (const times of skewed) {+ await (await nodeRequest('GET', nodeJwt(nodeClaims('GET', '', times))))+ .expect(HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_NODE_TOKEN_EXPIRED)+ .execute();+ }+ await (await nodeRequest('GET', nodeJwt(nodeClaims('GET', '', {exp: now - 300}), 'b'.repeat(64))))+ .expect(HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID)+ .execute();+ });++ test('rejects tokens that are not node tokens', async () => {+ const cases: Array<[string, string]> = [+ [nodeJwt(nodeClaims('GET', ''), 'b'.repeat(64)), APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID],+ [nodeJwt(nodeClaims('DELETE', '')), APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID],+ [nodeJwt(nodeClaims('GET', '', {path: '/voice/other'})), APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID],+ [nodeJwt(nodeClaims('GET', '', {aud: undefined})), APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID],+ [nodeJwt(nodeClaims('GET', '', {video: {roomJoin: true}})), APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID],+ [nodeJwt(nodeClaims('GET', '', {iss: 'nobody'})), APIErrorCodes.VOICE_NODE_UNKNOWN],+ ];+ for (const [auth, code] of cases) {+ await (await nodeRequest('GET', auth)).expect(HTTP_STATUS.UNAUTHORIZED, code).execute();+ }+ const participant = new AccessToken('fluxer-fra-server-1', SECRET, {identity: 'user_1_x'});+ participant.addGrant({roomJoin: true, room: 'guild_1_channel_2'});+ await (await nodeRequest('GET', `Bearer ${await participant.toJwt()}`))+ .expect(HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID)+ .execute();+ const body = {draining: true};+ await (await nodeRequest('PATCH', nodeJwt(nodeClaims('PATCH', '{"draining":false}')), body))+ .expect(HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID)+ .execute();+ });++ test('cannot turn itself on or change its region', async () => {+ for (const body of [{is_active: true}, {region_id: 'other'}, {internal_endpoint: 'http://10.0.0.1:7880'}]) {+ await (await nodeRequest('PATCH', nodeJwt(nodeClaims('PATCH', JSON.stringify(body))), body))+ .expect(HTTP_STATUS.BAD_REQUEST)+ .execute();+ }+ });++ test('tells a deleted server from a changed secret', async () => {+ await createBuilder(harness, admin.token)+ .patch('/admin/voice/regions/fra/servers/fra-server-1')+ .body({api_secret: 'c'.repeat(64)})+ .execute();+ await (await nodeRequest('GET', nodeJwt(nodeClaims('GET', ''))))+ .expect(HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID)+ .execute();+ await (await nodeRequest('DELETE', nodeJwt(nodeClaims('DELETE', ''), 'c'.repeat(64))))+ .expect(HTTP_STATUS.NO_CONTENT)+ .execute();+ await (await nodeRequest('GET', nodeJwt(nodeClaims('GET', ''), 'c'.repeat(64))))+ .expect(HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_NODE_UNKNOWN)+ .execute();+ });+ });++ describe('node routes on a server that joined with a hostname and soft limit', () => {+ const pinnedClaims = (method: string, body: string) => nodeClaims(method, body, {iss: 'fluxer-pinned'});++ beforeEach(async () => {+ const created = await createToken({+ server_id: 'fra-pinned',+ hostname: 'voice.example.com',+ soft_connection_limit: 100,+ internal_endpoint: 'http://10.0.0.5:7880',+ });+ await join(+ created.token,+ joinBody({api_key: 'fluxer-pinned', endpoint: 'wss://voice.example.com/livekit-fra-pinned'}),+ );+ });++ async function patch(body: Record<string, unknown>) {+ return createBuilder<VoiceNodeResponse>(harness, nodeJwt(pinnedClaims('PATCH', JSON.stringify(body))))+ .patch(VOICE_NODE_PATH)+ .body(body);+ }++ test('keeps the hostname of the token', async () => {+ await (await patch({endpoint: 'wss://other.example.com/livekit-fra-pinned'}))+ .expect(HTTP_STATUS.BAD_REQUEST, APIErrorCodes.VOICE_JOIN_ENDPOINT_INVALID)+ .execute();+ const moved = await (await patch({endpoint: 'wss://voice.example.com/livekit-moved'})).execute();+ expect(moved.endpoint).toBe('wss://voice.example.com/livekit-moved');+ });++ test('keeps the soft limit of the token', async () => {+ await (await patch({soft_connection_limit: 5000})).expect(HTTP_STATUS.BAD_REQUEST).execute();+ const same = await (await patch({soft_connection_limit: 100})).execute();+ expect(same.soft_connection_limit).toBe(100);+ });++ test('checks the new public endpoint even with an internal endpoint', async () => {+ const probes: Array<[string, boolean]> = [];+ setVoiceNodeEndpointVerifierForTesting(async (endpoint, _keys, internal, probePublic) => {+ probes.push([endpoint, probePublic && internal !== null]);+ });+ await (await patch({endpoint: 'wss://voice.example.com/livekit-new'})).execute();+ expect(probes).toEqual([['wss://voice.example.com/livekit-new', true]]);+ });++ test('moves when the public hostname resolves to a private address for the API', async () => {+ setVoiceNodeEndpointVerifierForTesting(null);+ setVoiceEndpointResolverForTesting(async () => ['10.0.0.9']);+ const seen: Array<string> = [];+ server.use(+ http.get('http://10.0.0.5:7880/', () => {+ seen.push('probe');+ return HttpResponse.text('OK');+ }),+ http.post('http://10.0.0.5:7880/twirp/livekit.RoomService/ListRooms', () => {+ seen.push('keys');+ return HttpResponse.json({rooms: []});+ }),+ );+ try {+ const moved = await (await patch({endpoint: 'wss://voice.example.com/livekit-split'})).execute();+ expect(moved.endpoint).toBe('wss://voice.example.com/livekit-split');+ expect(seen).toEqual(['probe', 'keys']);+ } finally {+ setVoiceEndpointResolverForTesting(null);+ }+ });+ });++ test('puts the public port of the instance in the join command', () => {+ expect(voiceJoinCommand('https://chat.example.com:8443', 'chat.example.com', 'fvj_1_x')).toBe(+ 'curl -fsSL https://fluxer.dev/install.sh | sh -s -- voice chat.example.com:8443 fvj_1_x',+ );+ expect(voiceJoinCommand('https://chat.example.com', 'chat.example.com', 'fvj_1_x')).toBe(+ 'curl -fsSL https://fluxer.dev/install.sh | sh -s -- voice chat.example.com fvj_1_x',+ );+ expect(voiceJoinCommand('https://chat.example.com:443/', 'chat.example.com', 'fvj_1_x')).toContain(+ ' voice chat.example.com fvj_1_x',+ );+ });++ test('needs a token for preview', async () => {+ await createBuilderWithoutAuth(harness)+ .post('/voice/join/preview')+ .body({})+ .expect(HTTP_STATUS.UNAUTHORIZED, APIErrorCodes.VOICE_JOIN_TOKEN_INVALID)+ .execute();+ });+});diff --git a/fluxer_api/src/api/voice/tests/VoiceRegionSelection.test.ts b/fluxer_api/src/api/voice/tests/VoiceRegionSelection.test.tsindex 8cb8844ed..7bb1ce02d 100644--- a/fluxer_api/src/api/voice/tests/VoiceRegionSelection.test.ts+++ b/fluxer_api/src/api/voice/tests/VoiceRegionSelection.test.ts@@ -65,6 +65,10 @@ function createVoiceServer({isActive: true,softConnectionLimit,notes: null,+ internalEndpoint: null,+ pinnedHostname: null,+ pinnedSoftLimit: false,+ draining: false,restrictions: {vipOnly: false,requiredGuildFeatures: new Set(),diff --git a/fluxer_api/src/api/voice/tests/VoiceServerEndpoints.test.ts b/fluxer_api/src/api/voice/tests/VoiceServerEndpoints.test.tsnew file mode 100644index 000000000..40c818216--- /dev/null+++ b/fluxer_api/src/api/voice/tests/VoiceServerEndpoints.test.ts@@ -0,0 +1,73 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {server} from '@app/api/test/msw/server';+import {+ checkLiveKitKeys,+ endpointOrigin,+ parseNodeEndpoint,+ probeLiveKit,+ resolveCheckedAddress,+ setVoiceEndpointResolverForTesting,+} from '@app/api/voice/VoiceServerEndpoints';+import {HttpResponse, http} from 'msw';+import {afterEach, describe, expect, it} from 'vitest';++describe('voice server endpoints', () => {+ afterEach(() => setVoiceEndpointResolverForTesting(null));++ it('keeps only the scheme, host and port as the origin', () => {+ expect(endpointOrigin('wss://voice.example.com/livekit-fra')).toBe('wss://voice.example.com');+ expect(endpointOrigin('wss://203.0.113.7:8443/livekit-x')).toBe('wss://203.0.113.7:8443');+ expect(endpointOrigin('https://voice.example.com')).toBeNull();+ });++ it('accepts only wss endpoints without IPv6 literals, credentials or queries', () => {+ expect(parseNodeEndpoint('wss://voice.example.com/livekit-a').hostname).toBe('voice.example.com');+ for (const bad of [+ 'ws://a.example.com',+ 'wss://[2001:db8::1]/x',+ 'wss://u:[email protected]',+ 'wss://a.example.com/?x=1',+ ]) {+ expect(() => parseNodeEndpoint(bad)).toThrow();+ }+ });++ it('refuses a host when any address is private, even after a public one', async () => {+ setVoiceEndpointResolverForTesting(async () => ['1.1.1.1']);+ await expect(resolveCheckedAddress(new URL('https://voice.example.com'), false)).resolves.toBe('1.1.1.1');+ setVoiceEndpointResolverForTesting(async () => ['1.1.1.1', '10.0.0.5']);+ await expect(resolveCheckedAddress(new URL('https://voice.example.com'), false)).rejects.toMatchObject({+ reason: 'private',+ });+ await expect(resolveCheckedAddress(new URL('https://voice.example.com'), true)).resolves.toBe('1.1.1.1');+ await expect(resolveCheckedAddress(new URL('https://127.0.0.1'), false)).rejects.toMatchObject({reason: 'private'});+ });++ it('probes the root of the path prefix and checks the keys against the room service', async () => {+ const seen: Array<string> = [];+ server.use(+ http.get('http://voice.test/livekit-a/', () => {+ seen.push('probe');+ return HttpResponse.text('OK');+ }),+ http.get('http://voice.test/other/', () => HttpResponse.text('down', {status: 502})),+ http.post('http://voice.test/livekit-a/twirp/livekit.RoomService/ListRooms', ({request}) => {+ seen.push('list');+ const auth = request.headers.get('authorization') ?? '';+ return HttpResponse.json({}, {status: auth.startsWith('Bearer ') ? 200 : 401});+ }),+ );+ setVoiceEndpointResolverForTesting(async () => ['1.1.1.1']);+ const options = {checkAddress: true, allowPrivate: false, timeoutMs: 2000};+ await probeLiveKit('ws://voice.test/livekit-a', options);+ await checkLiveKitKeys('ws://voice.test/livekit-a', {apiKey: 'k', apiSecret: 's'.repeat(32)}, options);+ await expect(probeLiveKit('ws://voice.test/other', options)).rejects.toMatchObject({+ reason: 'unreachable',+ message: 'HTTP 502',+ });+ setVoiceEndpointResolverForTesting(async () => ['192.168.1.2']);+ await expect(probeLiveKit('ws://voice.test/livekit-a', options)).rejects.toMatchObject({reason: 'private'});+ expect(seen).toEqual(['probe', 'list']);+ });+});diff --git a/fluxer_api/src/api/voice/tests/VoiceServerHealth.test.ts b/fluxer_api/src/api/voice/tests/VoiceServerHealth.test.tsnew file mode 100644index 000000000..4c2a2bdca--- /dev/null+++ b/fluxer_api/src/api/voice/tests/VoiceServerHealth.test.ts@@ -0,0 +1,80 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import type {VoiceRegionRecord, VoiceServerRecord} from '@app/api/voice/VoiceModel';+import {VoiceEndpointError} from '@app/api/voice/VoiceServerEndpoints';+import {VoiceServerHealthMonitor} from '@app/api/voice/VoiceServerHealth';+import {describe, expect, it} from 'vitest';++function server(serverId: string, isActive = true): VoiceServerRecord {+ return {+ regionId: 'fra',+ serverId,+ endpoint: `wss://${serverId}.example.com`,+ apiKey: serverId,+ apiSecret: 'secret',+ latitude: null,+ longitude: null,+ isActive,+ softConnectionLimit: null,+ notes: null,+ internalEndpoint: null,+ pinnedHostname: null,+ pinnedSoftLimit: false,+ draining: false,+ restrictions: {+ vipOnly: false,+ requiredGuildFeatures: new Set(),+ allowedGuildIds: new Set(),+ allowedUserIds: new Set(),+ },+ createdAt: null,+ updatedAt: null,+ };+}++function topology(servers: Array<VoiceServerRecord>) {+ return {+ getAllRegions: () => [{id: 'fra'} as VoiceRegionRecord],+ getServersForRegion: () => servers,+ };+}++describe('VoiceServerHealthMonitor', () => {+ it('marks a server down after two failures and up after one success', async () => {+ const failing = new Set(['a']);+ const servers = [server('a'), server('b')];+ const monitor = new VoiceServerHealthMonitor(+ topology(servers),+ async (s) => {+ if (failing.has(s.serverId)) throw new VoiceEndpointError('unreachable', 'HTTP 502');+ },+ 0,+ );+ expect(monitor.get(servers[0]!).status).toBe('unknown');+ await monitor.checkAll();+ expect(monitor.get(servers[0]!)).toMatchObject({status: 'unknown', error: 'HTTP 502'});+ expect(monitor.get(servers[1]!).status).toBe('up');+ await monitor.checkAll();+ expect(monitor.get(servers[0]!).status).toBe('down');+ failing.clear();+ await monitor.checkAll();+ expect(monitor.get(servers[0]!)).toMatchObject({status: 'up', error: null});+ });++ it('never marks a server down for a refused address and skips inactive servers', async () => {+ const probed: Array<string> = [];+ const servers = [server('private'), server('off', false)];+ const monitor = new VoiceServerHealthMonitor(+ topology(servers),+ async (s) => {+ probed.push(s.serverId);+ throw new VoiceEndpointError('private', 'not public');+ },+ 0,+ );+ await monitor.checkAll();+ await monitor.checkAll();+ expect(monitor.get(servers[0]!)).toMatchObject({status: 'unknown', error: 'not public'});+ expect(probed).toEqual(['private', 'private']);+ });+});diff --git a/fluxer_app_proxy/src/branding_images.rs b/fluxer_app_proxy/src/branding_images.rsindex 9f805e4e0..6ef26e632 100644--- a/fluxer_app_proxy/src/branding_images.rs+++ b/fluxer_app_proxy/src/branding_images.rs@@ -105,6 +105,11 @@ fn instance_sources_from_discovery(discovery: &serde_json::Value) -> InstanceCsp{connect_origins.push(origin);}+ for origin in voice_origins(discovery) {+ if !connect_origins.contains(&origin) {+ connect_origins.push(origin);+ }+ }InstanceCspSources {static_cdn_endpoint: http_origin("static_cdn"),media_endpoint: http_origin("media"),@@ -112,6 +117,33 @@ fn instance_sources_from_discovery(discovery: &serde_json::Value) -> InstanceCsp}}+fn voice_origins(discovery: &serde_json::Value) -> Vec<String> {+ let Some(values) = discovery+ .pointer("/voice_origins")+ .and_then(serde_json::Value::as_array)+ else {+ return Vec::new();+ };+ let mut out = Vec::new();+ for origin in values+ .iter()+ .filter_map(serde_json::Value::as_str)+ .filter_map(|value| connect_origin(value.trim()))+ {+ let twin = origin+ .strip_prefix("wss://")+ .map(|rest| format!("https://{rest}"))+ .or_else(|| {+ origin+ .strip_prefix("ws://")+ .map(|rest| format!("http://{rest}"))+ });+ out.push(origin);+ out.extend(twin);+ }+ out+}+fn connect_origin(value: &str) -> Option<String> {let url = Url::parse(value).ok()?;if !matches!(url.scheme(), "http" | "https" | "ws" | "wss") || url.host_str().is_none() {@@ -351,3 +383,56 @@ impl InstanceBranding {});}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn voice_origins_from_discovery_join_connect_sources_with_their_https_twins() {+ let discovery = serde_json::json!({+ "endpoints": {"api": "https://chat.example.com/api"},+ "voice_origins": [+ "wss://voice-fra.chat.example.com",+ "wss://203.0.113.7:8443",+ "not a url",+ 42+ ]+ });+ let sources = instance_sources_from_discovery(&discovery);+ for origin in [+ "wss://voice-fra.chat.example.com",+ "https://voice-fra.chat.example.com",+ "wss://203.0.113.7:8443",+ "https://203.0.113.7:8443",+ ] {+ assert!(+ sources.connect_origins.iter().any(|o| o == origin),+ "{origin} missing from {:?}",+ sources.connect_origins+ );+ }+ assert!(+ !sources+ .connect_origins+ .iter()+ .any(|o| o.contains("not a url"))+ );+ }++ #[test]+ fn a_server_that_leaves_discovery_leaves_the_connect_sources() {+ let with = serde_json::json!({"voice_origins": ["wss://voice-old.example.com"]});+ let without = serde_json::json!({"voice_origins": []});+ assert!(+ instance_sources_from_discovery(&with)+ .connect_origins+ .contains(&"wss://voice-old.example.com".to_owned())+ );+ assert!(+ instance_sources_from_discovery(&without)+ .connect_origins+ .is_empty()+ );+ }+}diff --git a/fluxer_docs/astro.config.ts b/fluxer_docs/astro.config.tsindex a8d503ac9..547b10394 100644--- a/fluxer_docs/astro.config.ts+++ b/fluxer_docs/astro.config.ts@@ -164,10 +164,12 @@ export default defineConfig({items: ['operator','operator/running',+ 'operator/voice-servers','operator/reverse-proxy','operator/sign-in','operator/kubernetes','operator/docker-compose',+ 'operator/voice-compose','operator/environment','operator/rexctl',],diff --git a/fluxer_docs/src/content/docs/admin-api/index.mdx b/fluxer_docs/src/content/docs/admin-api/index.mdxindex 8997e5248..c1bf115ef 100644--- a/fluxer_docs/src/content/docs/admin-api/index.mdx+++ b/fluxer_docs/src/content/docs/admin-api/index.mdx@@ -143,6 +143,9 @@ Each route needs one or more ACLs, and the caller needs at least one of them. Th| user:update:profile | Clears profile fields || user:update:traits | Replaces account traits || user:update:username | Updates usernames and discriminators |+| voice:join_token:create | Creates join tokens for new voice servers |+| voice:join_token:list | Reads join tokens |+| voice:join_token:revoke | Revokes join tokens || voice:region:create | Creates [voice](/admin-api/voice/) regions || voice:region:delete | Deletes voice regions || voice:region:list | Reads voice regions |@@ -355,6 +358,7 @@ These entries have `access` set to `read`.| list_user_relationships | The relationships of an account were read || list_user_sessions | An account's login sessions were read || list_user_sso_identities | The single sign-on identities of an account were read |+| list_voice_join_tokens | Voice join tokens were listed || list_voice_regions | Voice regions were listed || list_voice_servers | The voice servers of a region were listed || list_webauthn_credentials | An account's WebAuthn credentials were read |@@ -408,8 +412,10 @@ These entries have `access` set to `write`.| create_registration_url | A registration URL was issued || create_sign_in_link | A sign-in link was created for an admin account || create_user | An account was created by an Admin |+| create_voice_join_token | A voice join token was created || create_voice_region | A voice region was created || create_voice_server | A voice server was registered |+| create_voice_server_test_token | A test token for a voice server was signed || delete_all_user_messages | Deletion of every message an account sent was queued || delete_all_user_messages_dry_run | Deletion of every message an account sent was previewed with `dry_run`, and nothing was deleted || delete_badge | A custom badge was deleted |@@ -449,6 +455,7 @@ These entries have `access` set to `write`.| revoke_admin_api_key | An Admin API key was revoked || revoke_recovery_kit | The recovery kit of an account was revoked || revoke_registration_url | A registration URL was revoked |+| revoke_voice_join_token | A voice join token was revoked || schedule_deletion | Account deletion was scheduled || send_password_reset | A password reset message was requested for an account || set_acls | The Admin ACL set of an account was replaced |diff --git a/fluxer_docs/src/content/docs/admin-api/voice.mdx b/fluxer_docs/src/content/docs/admin-api/voice.mdxindex d37f10b69..e98d2ea3f 100644--- a/fluxer_docs/src/content/docs/admin-api/voice.mdx+++ b/fluxer_docs/src/content/docs/admin-api/voice.mdx@@ -84,9 +84,27 @@ A voice server is one LiveKit deployment in one region, identified by its `regio| required_guild_features | array[string] | [Guild features](/http-api/guilds/#guild-features) that admit a guild (max 100) || allowed_guild_ids | array[snowflake] | The guilds admitted without other guild checks (max 1000) || allowed_user_ids | array[snowflake] | The only accounts allowed to use the server, when not empty (max 1000) |+| internal_endpoint | ?string | The `http://` or `https://` URL the API uses for the LiveKit control API and health checks, or null to use `endpoint` |+| draining | boolean | Whether new calls go to other servers while calls already on this one keep going |+| health | [health](#voice-server-health-object) object | The server's health as the API node that answered last saw it |+| duplicate_api_key | boolean | Whether another server uses the same `api_key` || created_at | ?ISO8601 timestamp | When the server was created || updated_at | ?ISO8601 timestamp | When the server last changed |+### Voice server health object++| Field | Type | Description |+| --- | --- | --- |+| status | string | `up`, `down`, or `unknown` before the first check |+| checked_at | ?ISO8601 timestamp | When the last check ran, or null before the first |+| error | ?string | The status code or short transport error of the last failed check |++Every API node checks every active server every `FLUXER_VOICE_HEALTH_INTERVAL_MS`, 15 seconds by default, with `GET` on `internal_endpoint` or `endpoint`. Two failed checks in a row mark a server down, and one passing check marks it up. Placement sends no new calls to a server that is down, unless every server it could pick is down. A call pinned to a server that is down moves on its next join. A check of a public endpoint that resolves to a private address doesn't run, and the status stays `unknown`.++## Draining++A server with `draining` set gets no new calls. Calls already on it keep going, and people who join those calls still land on it. `rexctl` sets it while it upgrades or removes a server. Unlike `is_active` false, draining never splits a call in two.+## Soft connection limits`soft_connection_limit` is the connection count at which placement starts to prefer the region's other servers, and it never refuses a session. The count comes from [Get voice state counts](/admin-api/gateway/#get-voice-state-counts). Each API node refreshes it every 15 seconds, and a burst of joins between refreshes can go past the limit. The limit is checked when a channel's first session is placed, and later joins to that channel use the same server.@@ -282,7 +300,7 @@ The rate limit is 200 requests per minute, on the `admin:lookup` bucket.<RouteHeader method="POST" path="/v1/admin/voice/regions/{region_id}/servers" auditReason />-Adds a server to a region, and a `server_id` already in the region overwrites that server. Requires `voice:server:create`.+Adds a server to a region. Requires `voice:server:create`.### JSON body@@ -301,6 +319,8 @@ Adds a server to a region, and a `server_id` already in the region overwrites th| required_guild_features? | array[string] | [Guild features](/http-api/guilds/#guild-features) that admit a guild (max 100) || allowed_guild_ids? | array[snowflake] | The guilds admitted without other guild checks (max 1000) || allowed_user_ids? | array[snowflake] | The only accounts allowed to use the server, when not empty (max 1000) |+| internal_endpoint? | ?string | The `http://` or `https://` URL the API uses for the control API and health checks (max 512 characters) |+| draining? | boolean | Whether new calls go to other servers (default false) |<sup>1</sup> Send both coordinates or neither@@ -316,6 +336,7 @@ Adds a server to a region, and a `server_id` already in the region overwrites th| --- | --- | --- || 200 | response body | The server was created || 404 | [error response](/admin-api/#error-response) | `UNKNOWN_VOICE_REGION` when no region has the ID |+| 409 | [error response](/admin-api/#error-response) | `VOICE_SERVER_ID_IN_USE` for a taken ID, or `VOICE_SERVER_API_KEY_IN_USE` for a taken key |The rate limit is 100 requests per minute, on the `admin:guild:modify` bucket.@@ -341,6 +362,8 @@ Updates a server, and an array you send replaces the stored one. Requires `voice| required_guild_features? | array[string] | [Guild features](/http-api/guilds/#guild-features) that admit a guild (max 100) || allowed_guild_ids? | array[snowflake] | The guilds admitted without other guild checks (max 1000) || allowed_user_ids? | array[snowflake] | The only accounts allowed to use the server, when not empty (max 1000) |+| internal_endpoint? | ?string | The `http://` or `https://` URL the API uses for the control API and health checks, or null to use `endpoint` |+| draining? | boolean | Whether new calls go to other servers |<sup>1</sup> Send both coordinates or neither, and send both as null to use the region's coordinates@@ -356,6 +379,7 @@ Updates a server, and an array you send replaces the stored one. Requires `voice| --- | --- | --- || 200 | response body | The server was updated || 404 | [error response](/admin-api/#error-response) | `UNKNOWN_VOICE_SERVER` when no server matches |+| 409 | [error response](/admin-api/#error-response) | `VOICE_SERVER_API_KEY_IN_USE` when another server uses the new key |The rate limit is 100 requests per minute, on the `admin:guild:modify` bucket.@@ -379,3 +403,202 @@ Deletes a server and its stored credentials. Sessions on it stay connected, but| 404 | [error response](/admin-api/#error-response) | `UNKNOWN_VOICE_SERVER` when no server matches |The rate limit is 100 requests per minute, on the `admin:guild:modify` bucket.++## Create voice server test token++<RouteHeader method="POST" path="/v1/admin/voice/regions/{region_id}/servers/{server_id}/test-token" auditReason />++Signs a LiveKit token for a hidden, audio only participant in a throwaway room whose name starts with `rexctl-test-`. The token works for 60 seconds. `rexctl voice test` uses it to test a server from outside its network, and Fluxer ignores webhooks for these rooms. Requires `voice:server:update`.++### Response body++| Field | Type | Description |+| --- | --- | --- |+| token | string | The LiveKit token |+| endpoint | string | The server's endpoint |+| room | string | The name of the throwaway room |++### Response++| Status | Body | Condition |+| --- | --- | --- |+| 200 | response body | The token was signed |+| 404 | [error response](/admin-api/#error-response) | `UNKNOWN_VOICE_SERVER` when no server matches |++The rate limit is 100 requests per minute, on the `admin:guild:modify` bucket.++## Voice join tokens++A join token lets one machine register one voice server with [Join a voice server](#join-a-voice-server). It looks like `fvj_` followed by its ID, an underscore and a 43 character secret. Fluxer stores only a hash of the secret. A token works once, for 60 seconds to 24 hours, and the list keeps it for 7 days after it expires. Whoever runs the server can record the calls placed on it. A token trusts that machine with calls, and it gives no other access.++### Voice join token object++| Field | Type | Description |+| --- | --- | --- |+| token_id | snowflake | The ID of the token |+| status | string | `pending`, `used`, `expired` or `revoked` |+| created_at | ISO8601 timestamp | When the token was created |+| expires_at | ISO8601 timestamp | When the token stops working |+| used_at | ?ISO8601 timestamp | When a server joined with it |+| used_server_id | ?string | The server that joined with it |+| revoked_at | ?ISO8601 timestamp | When it was revoked |+| region_id | ?string | The region preset, or null to let Fluxer pick |+| region_name | ?string | The name of the new region the join creates, or null |+| server_id | ?string | The server ID preset, or null |+| hostname | ?string | The hostname the endpoint must use, or null |+| notes | ?string | The notes for the new server, or null |++## Create voice join token++<RouteHeader method="POST" path="/v1/admin/voice/join-tokens" auditReason />++Requires `voice:join_token:create`. Every preset wins over what the joining machine sends.++### JSON body++| Field | Type | Description |+| --- | --- | --- |+| region_id? | ?string | An existing region, or the ID of a new region with `region_name` (lowercase letters, digits and dashes, 1-64 characters) |+| region_name? | ?string | The name of a new region the join creates (1-100 characters) |+| region_emoji? | ?string | The emoji of the new region (1-64 characters) |+| region_latitude? | ?number | The latitude of the new region, with `region_longitude` (-90 to 90) |+| region_longitude? | ?number | The longitude of the new region, with `region_latitude` (-180 to 180) |+| server_id? | ?string | The server ID (lowercase letters, digits and dashes, 1-64 characters) |+| hostname? | ?string | The hostname the endpoint must use |+| internal_endpoint? | ?string | The URL the API uses for the control API and health checks of the new server |+| soft_connection_limit? | ?integer | The soft connection limit of the new server |+| notes? | ?string | The notes of the new server (max 2000 characters) |+| vip_only? | boolean | Restricts the new server to `VIP_VOICE` guilds (default false) |+| required_guild_features? | array[string] | Guild features that admit a guild to the new server (max 100) |+| allowed_guild_ids? | array[snowflake] | Guilds admitted to the new server (max 1000) |+| allowed_user_ids? | array[snowflake] | The only accounts allowed on the new server (max 1000) |+| expires_in_seconds? | integer | Seconds until the token stops working (60-86400, default 3600) |++A new region from `region_name` gets `region_latitude` and `region_longitude`. Without them, it gets the location of the server that joins.++### Response body++| Field | Type | Description |+| --- | --- | --- |+| token_id | snowflake | The ID of the token |+| token | string | The token, which no other response returns |+| expires_at | ISO8601 timestamp | When the token stops working |+| command | string | The install command to run on the voice machine |++### Response++| Status | Body | Condition |+| --- | --- | --- |+| 200 | response body | The token was created |+| 400 | [error response](/admin-api/#error-response) | `region_latitude` or `region_longitude` comes without the other, or without `region_name` |+| 404 | [error response](/admin-api/#error-response) | `UNKNOWN_VOICE_REGION` when no region has the ID in `region_id` and `region_name` is empty |++The rate limit is 100 requests per minute, on the `admin:guild:modify` bucket.++## List voice join tokens++<RouteHeader method="GET" path="/v1/admin/voice/join-tokens" />++Lists the tokens of the last 7 days, newest first, without their secrets. Requires `voice:join_token:list`.++### Response body++| Field | Type | Description |+| --- | --- | --- |+| tokens | array[[voice join token](#voice-join-token-object) object] | The tokens |++The rate limit is on the `admin:lookup` bucket.++## Revoke voice join token++<RouteHeader method="DELETE" path="/v1/admin/voice/join-tokens/{token_id}" auditReason />++Stops a pending token from working. Requires `voice:join_token:revoke`.++### Response body++| Field | Type | Description |+| --- | --- | --- |+| success | boolean | Always true |++### Response++| Status | Body | Condition |+| --- | --- | --- |+| 200 | `{"success": true}` | The token was revoked |+| 401 | [error response](/admin-api/#error-response) | `VOICE_JOIN_TOKEN_INVALID` when the token is unknown, used, expired or revoked |++## Join a voice server++These routes are for the voice machine, not for admins. They take `Authorization: VoiceJoin` followed by a join token, and the rate limit is 10 requests per minute for each address.++<RouteHeader method="POST" path="/v1/voice/join/preview" unauthenticated />++Shows what a join would choose, without using the token. The JSON body has `public_ipv4`, `region_id`, `cpu_count`, `memory_bytes` and `rexctl_version`, all optional. The response has the suggested `server_id`, `server_id_pinned` set to true when the token sets the server ID, the `region` with its `id`, `name`, `distance_km`, whether it's `new` and its `source`, the `latitude` and `longitude` from GeoIP rounded to 0.1 degrees, the `hostname` preset, a `suggested_hostname`, the `soft_connection_limit` preset, the `webhook_url`, the `api_code_version`, the `release` of the API and `expires_at`.++Fluxer picks the token's region first, then an existing region in `region_id`, then the existing region nearest to the machine when the instance has a GeoIP database, and the default region otherwise. It never creates a region unless the token has a new one. The `source` says which of these it used, as `token`, `requested`, `nearest` or `default`.++<RouteHeader method="POST" path="/v1/voice/join" unauthenticated />++Registers the server and uses the token.++| Field | Type | Description |+| --- | --- | --- |+| endpoint | string | The public `wss://` URL of the server. A host that is an IP address must be IPv4 |+| api_key | string | A LiveKit API key no other server uses (1-256 characters) |+| api_secret | string | Its secret (32-256 characters) |+| server_id? | string | The server ID, ignored when the token has one |+| region_id? | string | An existing region, ignored when the token has one |+| latitude?<sup>1</sup> | number | The server's latitude |+| longitude?<sup>1</sup> | number | The server's longitude |+| soft_connection_limit? | integer | Ignored when the token has one |+| public_ipv4? | string | The machine's public address, for GeoIP |++<sup>1</sup> Send both coordinates or neither++Fluxer resolves the endpoint's host, refuses it when any address is private unless `FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS` is true, and connects to the address it checked. `GET` on the endpoint's path must answer `OK` within 10 seconds with a certificate Fluxer trusts, and the room service must accept the key and secret. Then Fluxer saves the server, active and not draining, and the token stops working. The response has `server_id`, `region_id`, `endpoint`, `webhook_url` and `node_url`.++| Status | Code | Condition |+| --- | --- | --- |+| 400 | `VOICE_JOIN_ENDPOINT_INVALID` | The endpoint isn't `wss://`, uses an IPv6 address, a private address or another hostname than the token's |+| 400 | `VOICE_JOIN_ENDPOINT_UNREACHABLE` | The endpoint didn't answer `OK`, with the status or error in `detail` |+| 400 | `VOICE_JOIN_KEY_MISMATCH` | The server refused the key and secret |+| 401 | `VOICE_JOIN_TOKEN_INVALID` | The token is unknown, used, expired or revoked |+| 404 | `UNKNOWN_VOICE_REGION` | No region has the ID in `region_id` |+| 409 | `VOICE_JOIN_TOKEN_BUSY` | Another join with the token is running |+| 409 | `VOICE_SERVER_ID_IN_USE` | Another server has the ID |+| 409 | `VOICE_SERVER_API_KEY_IN_USE` | Another server uses the key |++A failed join leaves the token unused. The same machine can try again.++## Voice node routes++A server that joined manages its own row with `GET`, `PATCH` and `DELETE` on `/v1/voice/node`. The request takes `Authorization: Bearer` followed by an HS256 JWT signed with the server's API secret, with these claims:++- `iss` set to the server's API key.+- `aud` set to `fluxer-voice-node`.+- `method` and `path` of the request, with `path` set to `/voice/node`.+- `exp` in the future and at most 5 minutes ahead, and `iat` when present. Fluxer allows 60 seconds of clock difference either way.+- `sha256` set to the base64 SHA-256 of the body, or an empty string for `GET` and `DELETE`.++Fluxer refuses tokens with a `video` grant. LiveKit participant tokens and webhooks signed with the same secret don't work here.++<RouteHeader method="GET" path="/v1/voice/node" unauthenticated />++Returns the server's `region_id`, `server_id`, `endpoint`, `latitude`, `longitude`, `is_active`, `draining`, `soft_connection_limit` and `health`.++<RouteHeader method="PATCH" path="/v1/voice/node" unauthenticated />++Takes `endpoint`, `draining`, `latitude`, `longitude` and `soft_connection_limit`, and returns the server like `GET`. Fluxer checks a new endpoint the same way as a join. When the server has an internal endpoint, Fluxer also tries the new public endpoint and logs a warning when it doesn't answer, without refusing the change. A new endpoint must keep the token's hostname, and a server can't change a soft connection limit the token set. A server can't change its region, its restrictions, `internal_endpoint` or `is_active`. A server an admin turned off stays off.++<RouteHeader method="DELETE" path="/v1/voice/node" unauthenticated />++Removes the server and answers 204.++Every node route can fail with these errors.++| Status | Code | Condition |+| --- | --- | --- |+| 401 | `VOICE_NODE_UNKNOWN` | No server has the key in `iss`, usually because an admin deleted it |+| 401 | `VOICE_NODE_SIGNATURE_INVALID` | The signature or a claim is wrong, or an admin changed the server's secret |+| 401 | `VOICE_NODE_TOKEN_EXPIRED` | The signature matches, and `exp` or `iat` is outside the allowed time. The clock of the voice machine is usually off |diff --git a/fluxer_docs/src/content/docs/http-api/errors.md b/fluxer_docs/src/content/docs/http-api/errors.mdindex 48ed0bfef..76f12c307 100644--- a/fluxer_docs/src/content/docs/http-api/errors.md+++ b/fluxer_docs/src/content/docs/http-api/errors.md@@ -389,6 +389,16 @@ These are the values of the top-level `code`, and the [OpenAPI document](/http-a| `USE_CLAIM_ROUTE` | Add an email address to your account first || `VALIDATION_ERROR` | Validation failed || `VOICE_CHANNEL_FULL` | Voice channel is full |+| `VOICE_JOIN_ENDPOINT_INVALID` | The voice server endpoint isn't allowed |+| `VOICE_JOIN_ENDPOINT_UNREACHABLE` | Fluxer couldn't connect to the voice server endpoint |+| `VOICE_JOIN_KEY_MISMATCH` | The voice server rejected its API key and secret |+| `VOICE_JOIN_TOKEN_BUSY` | Another voice server is joining with this token right now |+| `VOICE_JOIN_TOKEN_INVALID` | The voice join token is not valid |+| `VOICE_NODE_SIGNATURE_INVALID` | The voice server's signature doesn't match its secret |+| `VOICE_NODE_TOKEN_EXPIRED` | The voice server's request is expired or not valid yet. Check the clock on that machine |+| `VOICE_NODE_UNKNOWN` | No voice server has this API key |+| `VOICE_SERVER_API_KEY_IN_USE` | Another voice server already uses this API key |+| `VOICE_SERVER_ID_IN_USE` | Another voice server already uses this ID || `WEBAUTHN_CREDENTIAL_LIMIT_REACHED` | You've reached the maximum of {count} WebAuthn credentials || `WEBHOOK_FORUM_TARGET_CONFLICT` | You can't specify both `thread_id` and `thread_name` || `WEBHOOK_FORUM_TARGET_REQUIRED` | Webhooks posting to this channel need a `thread_id` or a `thread_name` |diff --git a/fluxer_docs/src/content/docs/http-api/instance.mdx b/fluxer_docs/src/content/docs/http-api/instance.mdxindex 6d7d66536..66d247aca 100644--- a/fluxer_docs/src/content/docs/http-api/instance.mdx+++ b/fluxer_docs/src/content/docs/http-api/instance.mdx@@ -32,6 +32,7 @@ The public description of one instance.| age_policy? | [age policy](#age-policy-object) object | Regional age policy || notice? | ?[instance notice](#instance-notice-object) object | The current notice from the instance admins, or null || guild_badges? | array[[guild badge](#guild-badge-object) object] | The custom badges that guilds can show next to their name, in display order (at most 10) |+| voice_origins? | array[string] | The origin of every voice server endpoint, such as `wss://voice.example.com`, for content security policies (at most 1000) |## Guild badge objectdiff --git a/fluxer_docs/src/content/docs/operator/docker-compose.mdx b/fluxer_docs/src/content/docs/operator/docker-compose.mdxindex df6fd2319..485b190b9 100644--- a/fluxer_docs/src/content/docs/operator/docker-compose.mdx+++ b/fluxer_docs/src/content/docs/operator/docker-compose.mdx@@ -61,6 +61,10 @@ FLUXER_EDGE_BIND=127.0.0.1:8080When every service is healthy, open your domain in a browser and enter the `FLUXER_SETUP_TOKEN` from `.env` to create your admin account.+## Add voice servers++The `voice` folder of the bundle runs extra LiveKit servers on this machine or on others. See [Run a voice server with Docker Compose](/operator/voice-compose/).+## UpgradeDownload the new bundle into the same folder and unpack it, which replaces every file except `.env`. If the new `.env.example` has an uncommented line your `.env` doesn't have yet, add it, then run `docker compose up -d`.diff --git a/fluxer_docs/src/content/docs/operator/environment.mdx b/fluxer_docs/src/content/docs/operator/environment.mdxindex b37952008..116b13bb6 100644--- a/fluxer_docs/src/content/docs/operator/environment.mdx+++ b/fluxer_docs/src/content/docs/operator/environment.mdx@@ -110,6 +110,7 @@ The optional compose files change how Fluxer runs. To use one, list it after `do- `tunnel.compose.yml` for a tunnel- `external-object-store.compose.yml` for your own object store- `ipv6.compose.yml` for IPv6 on Fluxer's network+- `external-voice.compose.yml` for voice servers of your own without the bundled LiveKit### `COMPOSE_FILE`@@ -249,7 +250,7 @@ The arguments of a Caddy tls directive for the edge, such as `internal` for a ce## Voice and video-Calls connect directly to this machine on `FLUXER_LIVEKIT_TCP_PORT` and `FLUXER_LIVEKIT_UDP_PORT`, even when Fluxer is behind your own proxy.+Calls connect directly to this machine on `FLUXER_LIVEKIT_TCP_PORT` and `FLUXER_LIVEKIT_UDP_PORT`, even when Fluxer is behind your own proxy. To run without the bundled LiveKit, add `external-voice.compose.yml` to `COMPOSE_FILE` and set `FLUXER_LIVEKIT_BUNDLED` to `false`.### `FLUXER_LIVEKIT_ENABLED`@@ -257,7 +258,11 @@ Whether voice and video calls are turned on. Use `true` or `false`. The default### `FLUXER_LIVEKIT_INTERNAL_URL`-The URL the API uses for the LiveKit control API of the bundled LiveKit, the first server in the default voice region. Servers added in the admin panel use their own address. The default is `http://livekit:7880`. With rexctl, use `rexctl config set FLUXER_LIVEKIT_INTERNAL_URL`.+The URL the API uses for the LiveKit control API of the bundled LiveKit, the first server in the default voice region. Servers added in the admin panel use their own address. Fluxer ignores it when `FLUXER_LIVEKIT_BUNDLED` is `false`. The default is `http://livekit:7880`. With rexctl, use `rexctl config set FLUXER_LIVEKIT_INTERNAL_URL`.++### `FLUXER_LIVEKIT_BUNDLED`++Whether Fluxer runs its own LiveKit voice server on this machine. Use `true` or `false`. The bundled LiveKit is the first server in the default voice region. With `false`, Fluxer creates the default region with no server in it, removes the server it made for the bundled LiveKit and keeps calls on the servers you add with `rexctl voice add`, `rexctl voice join` or the admin panel. With Docker Compose, also add `external-voice.compose.yml` to `COMPOSE_FILE`. The default is `true`. With rexctl, use `rexctl config set voice.bundled`.### `FLUXER_LIVEKIT_DEFAULT_REGION`@@ -271,6 +276,14 @@ The URL browsers and apps use for voice signalling. When it's empty, Fluxer usesSTUN URLs for peer-to-peer calls, separated by commas, such as `stun:stun.example.com:3478`. When it's empty, peer-to-peer calls work only between devices with a direct route to each other, such as on one local network. With rexctl, use `rexctl config set FLUXER_VOICE_P2P_STUN_URLS`.+### `FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS`++Whether the API accepts a voice server joining with an endpoint that resolves to a private address. Use `true` or `false`. Set it to `true` only for tests and private networks. With rexctl, use `rexctl config set FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS`.++### `FLUXER_VOICE_HEALTH_INTERVAL_MS`++How often each API node checks every active voice server, in milliseconds. A server that fails two checks in a row gets no new calls until a check passes. Set it to `0` to turn the checks off. When it's empty, the API checks every 15 seconds. With rexctl, use `rexctl config set FLUXER_VOICE_HEALTH_INTERVAL_MS`.+### `FLUXER_LIVEKIT_TCP_PORT`The TCP port for voice and video. The default is `7881`. With rexctl, use `rexctl config set voice.tcp_port`.@@ -299,6 +312,78 @@ The second STUN host LiveKit asks to find its public IP address, as `host:port`.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`.+## Standalone voice servers++These go in the settings file of a voice server from the voice folder of the compose bundle, such as `fra-server-1.env`, not in Fluxer's `.env`. `rexctl voice add` and `rexctl voice join` write them for you.++### `FLUXER_VOICE_ID`++The ID of the voice server, such as `fra-server-2`. Use lowercase letters, digits and dashes. The container answers as `voice-` followed by this ID on the voice network, and the edge routes `/livekit-` followed by this ID to it.++### `FLUXER_VOICE_API_KEY`++The LiveKit API key of this server. Every voice server of an instance needs its own key, such as `fluxer-` followed by the server ID.++### `FLUXER_VOICE_API_SECRET`++The LiveKit API secret that goes with `FLUXER_VOICE_API_KEY`, at least 32 characters, such as the output of `openssl rand -hex 32`.++### `FLUXER_VOICE_WEBHOOK_URL`++The URL LiveKit sends its webhooks to. On the Fluxer machine it's `http://api:8080/webhooks/livekit`. On another machine it's the public API address followed by `/webhooks/livekit`.++### `FLUXER_VOICE_TCP_PORT`++The TCP port for media on this host. LiveKit listens on the same port inside the container, and each server on a host needs its own.++### `FLUXER_VOICE_UDP_PORT`++The UDP port for media on this host. LiveKit listens on the same port inside the container, and each server on a host needs its own.++### `FLUXER_VOICE_NODE_IP`++The public IPv4 address LiveKit gives to clients. Set `FLUXER_VOICE_USE_EXTERNAL_IP` to `false` with it.++### `FLUXER_VOICE_USE_EXTERNAL_IP`++Whether LiveKit finds its public IP address through STUN. Use `true` or `false`.++### `FLUXER_VOICE_BIND_IP`++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`++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_NETWORK`++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`++The memory limit for the server's container.++### `FLUXER_VOICE_GOMEMLIMIT`++The soft memory limit of the Go runtime in LiveKit. Keep it at about 85% of `FLUXER_VOICE_MEMORY_LIMIT`.++### `FLUXER_VOICE_STOP_GRACE`++How long Docker waits for the calls on the server to end when it stops or replaces the container, before it stops LiveKit anyway.++## Voice edge++These go in `edge.env` next to `voice-edge.compose.yml` in the voice folder of the compose bundle. `rexctl voice join` writes them for you.++### `FLUXER_VOICE_EDGE_IP`++The public IPv4 address of the voice machine, when an endpoint uses that address and not a hostname. The edge then answers clients that send no server name with the certificate for that address, which browsers need.++### `FLUXER_VOICE_EDGE_EMAIL`++The email address the voice edge gives Let's Encrypt for notices about its certificates. When it's empty, the edge gets certificates without one.+## 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`.@@ -1780,6 +1865,10 @@ The file form doesn't work for the docs site variables either, or for variablesrexctl and its install scripts look these up in the shell you run them from. A value in `.env` has no effect.+### `FLUXER_VOICE_JOIN_TOKEN`++The join token for `rexctl voice join` when the command line doesn't have one. Other accounts on the machine can't see it in the process list.+### `REXCTL_RELEASE_URL`The URL of the release index that rexctl and its install scripts download releases from. When it's empty, they use `https://fluxer.dev/rexctl/release.json`. Another server must answer the same queries as that address, including `version` and `format=sh`.diff --git a/fluxer_docs/src/content/docs/operator/kubernetes.mdx b/fluxer_docs/src/content/docs/operator/kubernetes.mdxindex 4669c3b27..ea74430ee 100644--- a/fluxer_docs/src/content/docs/operator/kubernetes.mdx+++ b/fluxer_docs/src/content/docs/operator/kubernetes.mdx@@ -90,6 +90,10 @@ The Postgres database and role, and the S3 buckets in `s3.buckets`, must exist bPoint your LiveKit server's webhook at `https://<your domain>/api/webhooks/livekit`, signed with the key in `FLUXER_LIVEKIT_API_KEY`.+Give every LiveKit server of an instance its own API key. Fluxer finds the server that sent a webhook by its key. To add more voice servers, create a join token in the admin panel and run [`rexctl voice join`](/operator/voice-servers/#on-another-machine) on another machine, or follow [Run a voice server with Docker Compose](/operator/voice-compose/). Joining talks only to your instance's public API, and it works with the Helm chart too.++Joined servers work next to the chart's LiveKit and next to a LiveKit of your own in `livekit.external.url`. To run with only joined servers and no LiveKit of the chart's, set `livekit.enabled: false` and `livekit.joined: true`. Voice stays on, the default region starts with no server in it and calls go to the servers you join. `livekit.enabled: false` without `livekit.external.url` or `livekit.joined` turns voice off.+## Trust your own CAWhen your email server or another service the api calls has a certificate from your own CA, mount that CA's PEM file into the api and worker and set `NODE_EXTRA_CA_CERTS` to its path. This example reads `ca.pem` from a ConfigMap called `smtp-ca`.diff --git a/fluxer_docs/src/content/docs/operator/reverse-proxy.mdx b/fluxer_docs/src/content/docs/operator/reverse-proxy.mdxindex b8cf64cca..555cad74c 100644--- a/fluxer_docs/src/content/docs/operator/reverse-proxy.mdx+++ b/fluxer_docs/src/content/docs/operator/reverse-proxy.mdx@@ -135,6 +135,8 @@ Fluxer sends at most 100 prefixes per request, which is the most Cloudflare acceCall 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.+Extra voice servers on this machine from [`rexctl voice add`](/operator/voice-servers/#on-the-fluxer-machine) use paths on your domain, such as `/livekit-default-server-2/`. A proxy that forwards every path already passes them. Their media ports skip the proxy too. Open each server's TCP and UDP port as well.+### Voice behind a tunnelIf 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.diff --git a/fluxer_docs/src/content/docs/operator/rexctl.mdx b/fluxer_docs/src/content/docs/operator/rexctl.mdxindex 26b829fe1..5abe86a97 100644--- a/fluxer_docs/src/content/docs/operator/rexctl.mdx+++ b/fluxer_docs/src/content/docs/operator/rexctl.mdx@@ -54,7 +54,7 @@ rexctl admins sign-in-link <user>## rexctl auto-upgrade-Show whether rexctl upgrades Fluxer by itself, when it runs next and how the last run went. Each run is rexctl upgrade --yes and works like an upgrade you run yourself.+Show whether rexctl upgrades Fluxer, or the voice servers on a voice machine, by itself, when it runs next and how the last run went. Each run is rexctl upgrade --yes and works like an upgrade you run yourself.```shrexctl auto-upgrade [flags]@@ -382,6 +382,10 @@ rexctl install [flags]Your instance's name.+- `--no-bundled-voice`++ Run without the bundled LiveKit and use only voice servers you add later.+- `--no-start`Write the files without starting Fluxer.@@ -960,3 +964,289 @@ rexctl version [flags]- `--json`Print JSON.++## rexctl voice++Run standalone LiveKit voice servers next to Fluxer with rexctl voice add, or on other machines with rexctl voice token and rexctl voice join.++To run without the bundled voice server, run rexctl config set voice.bundled false, or install with rexctl install --no-bundled-voice. Calls then go only to the servers you add. rexctl config set voice.bundled true brings the bundled server back.++### rexctl voice add++Add standalone LiveKit voice servers next to your Fluxer instance on this machine. Each one gets its own ID, ports, keys and path on your domain, and rexctl registers it with your instance.++```sh+rexctl voice add [flags]+```++- `--count int`++ How many servers to add. The default is `1`.++- `--dry-run`++ Show what would happen and change nothing.++- `--force`++ Add servers even when Fluxer runs through a tunnel.++- `--id string`++ Server ID, such as fra-server-2.++- `--image string`++ LiveKit image to run.++- `--ip string`++ Public IPv4 address LiveKit gives to clients, also the address the ports bind to when it's on this machine.++- `--lat float`++ Latitude of the server.++- `--long float`++ Longitude of the server.++- `--memory string`++ Memory limit for the server, such as 2g.++- `--new-region string`++ Create a voice region with this ID for the servers.++- `--no-firewall`++ Leave the firewall on this machine alone.++- `--region string`++ Existing voice region for the server.++- `--region-name string`++ Name of the new region.++- `--soft-limit int`++ Connections above which placement prefers another server.++- `--tcp-port int`++ TCP port for media.++- `--udp-port int`++ UDP port for media.++### rexctl voice hostname++Move a voice server on this machine to a hostname once its DNS record points here. rexctl gets a certificate and updates the server's endpoint on the instance.++```sh+rexctl voice hostname <id> <host>+```++### rexctl voice join++Install a standalone LiveKit voice server on this machine and register it with the Fluxer instance at \<domain>. Get the token with rexctl voice token on the Fluxer machine, or with Add a voice server in the admin panel. The token works once. rexctl also reads it from FLUXER_VOICE_JOIN_TOKEN or, with --token-stdin, from standard input.++```sh+rexctl voice join <domain> [token] [flags]+```++- `--dry-run`++ Show what would happen and change nothing.++- `--email string`++ Email address for Let's Encrypt notices.++- `--endpoint string`++ Public wss:// URL of the server behind your own proxy.++- `--hostname string`++ Hostname for the server's endpoint and certificate.++- `--http-port int`++ Port on 127.0.0.1 for your own proxy, with --no-edge.++- `--id string`++ Server ID, such as fra-server-2.++- `--image string`++ LiveKit image to run.++- `--ip string`++ Public IPv4 address LiveKit gives to clients, also the address the ports bind to when it's on this machine.++- `--lat float`++ Latitude of the server.++- `--long float`++ Longitude of the server.++- `--memory string`++ Memory limit for the server, such as 2g.++- `--no-edge`++ Start no edge and publish the server on 127.0.0.1 for your own proxy.++- `--no-firewall`++ Leave the firewall on this machine alone.++- `--region string`++ Existing voice region for the server.++- `--soft-limit int`++ Connections above which placement prefers another server.++- `--tcp-port int`++ TCP port for media.++- `--token-stdin`++ Read the join token from standard input.++- `--udp-port int`++ UDP port for media.++### rexctl voice list++List voice servers.++```sh+rexctl voice list [flags]+```++- `--json`++ Print JSON.++### rexctl voice remove++Drain, unregister and delete a voice server.++```sh+rexctl voice remove [id] [flags]+```++- `--all`++ Remove every voice server and the edge on this machine.++- `--drain-timeout duration`++ How long to wait for calls to end. The default is `15m0s`.++- `--now`++ Don't wait for calls to end.++### rexctl voice status++Show the voice servers on this machine.++```sh+rexctl voice status [id]+```++### rexctl voice test++Join a throwaway room on a voice server and report which transports connect. On the Fluxer machine it can test any server of the instance, including servers on other machines.++```sh+rexctl voice test [id]+```++### rexctl voice token++Create a join token and print the command that installs a voice server with it. The token works once.++```sh+rexctl voice token [flags]+```++- `--expires string`++ How long the token works, at most 24h. The default is `1h`.++- `--hostname string`++ Hostname the server must use.++- `--id string`++ Server ID.++- `--internal-endpoint string`++ URL the API uses to connect to the server, such as http://10.0.0.5:7880.++- `--lat float`++ Latitude of the new region.++- `--long float`++ Longitude of the new region.++- `--name string`++ A note for the server.++- `--new-region string`++ Create a region with this ID when the server joins.++- `--region string`++ Existing region for the server.++- `--region-name string`++ Name of the new region.++- `--soft-limit int`++ Connections above which placement prefers another server.++### rexctl voice token list++List join tokens.++```sh+rexctl voice token list [flags]+```++- `--json`++ Print JSON.++### rexctl voice token revoke++Stop a join token from working.++```sh+rexctl voice token revoke <token id>+```diff --git a/fluxer_docs/src/content/docs/operator/running.mdx b/fluxer_docs/src/content/docs/operator/running.mdxindex 820017460..50cd6a7dd 100644--- a/fluxer_docs/src/content/docs/operator/running.mdx+++ b/fluxer_docs/src/content/docs/operator/running.mdx@@ -96,7 +96,7 @@ You can run more than one instance on the same machine. Each one needs three thirexctl install --dir /path/to/second-instance --project blue --behind-proxy --bind 127.0.0.1:8081```-rexctl chooses free voice and video ports for each instance and prints the ones it picked. `rexctl doctor` shows which instance uses which ports.+rexctl chooses free voice and video ports for each instance and prints the ones it picked. `rexctl doctor` shows which instance uses which ports. To run more voice servers for one instance, on this machine or on others, see [Add voice servers](/operator/voice-servers/).rexctl remembers where it installed each instance. Outside an instance folder, commands use the default instance, which is the first one you installed. Run `rexctl use <folder>` to pick another default, or pass `--dir`. Each account has its own default.@@ -182,7 +182,7 @@ To change the Compose setup, such as labels, images, ports or volumes, run `rexcrexctl config edit```-To add your own Caddy directives, such as extra routes or headers, put them in a `.caddy` file in the `caddy.d` folder of your instance. Fluxer's Caddyfile loads every file there inside your site, and upgrades keep them. To serve a whole extra site, such as another domain on the same machine, put its `.caddy` file in `caddy.d/sites`. Fluxer loads those files next to its own site, and `rexctl upgrade` moves a file of whole sites from `caddy.d` to `caddy.d/sites` for you. To use your own Caddyfile instead, mount it over `/etc/caddy/Caddyfile` on the `edge` service in `custom.compose.yml`.+To add your own Caddy directives, such as extra routes or headers, put them in a `.caddy` file in the `caddy.d` folder of your instance. Fluxer's Caddyfile loads every file there inside your site, and upgrades keep them. `rexctl voice add` writes a `voice-` file there for each voice server it adds, and `rexctl voice remove` deletes it again. To serve a whole extra site, such as another domain on the same machine, put its `.caddy` file in `caddy.d/sites`. Fluxer loads those files next to its own site, and `rexctl upgrade` moves a file of whole sites from `caddy.d` to `caddy.d/sites` for you. To use your own Caddyfile instead, mount it over `/etc/caddy/Caddyfile` on the `edge` service in `custom.compose.yml`.## When something is wrongdiff --git a/fluxer_docs/src/content/docs/operator/voice-compose.mdx b/fluxer_docs/src/content/docs/operator/voice-compose.mdxnew file mode 100644index 000000000..ecb286424--- /dev/null+++ b/fluxer_docs/src/content/docs/operator/voice-compose.mdx@@ -0,0 +1,239 @@+---+# SPDX-License-Identifier: AGPL-3.0-or-later+title: Run a voice server with Docker Compose+description: Run a standalone LiveKit voice server from the compose bundle and connect it to any Fluxer instance by hand.+sidebar:+ label: Voice server with Compose+---++Use this page to run a voice server on a machine where you can't run rexctl, or next to an instance on Kubernetes or another platform. When you can run rexctl, [`rexctl voice add`](/operator/voice-servers/#on-the-fluxer-machine) and [`rexctl voice join`](/operator/voice-servers/#on-another-machine) do all of this for you.++## Get the files++The `voice` folder of the [compose bundle](/operator/docker-compose/#get-the-files) has everything for one machine:++- `voice.compose.yml` runs one LiveKit server.+- `voice-http.compose.yml` publishes the server's HTTP port on `127.0.0.1` for your own proxy.+- `voice-edge.compose.yml` and `Caddyfile.voice` run Caddy on ports 80 and 443 for every server on a voice machine.+- `sites.d/voice.caddy.example` is a site with one server.+- `voice.env.example` lists every setting of a server.+- `edge.env.example` lists the settings of the edge.++```sh+curl -fsSL https://fluxer.dev/compose/latest -o fluxer-compose.tar.gz+tar -xzf fluxer-compose.tar.gz+cd voice+```++## Ask the instance first++With a join token, ask your instance what the join would choose before you fill in anything. Create the token on the Fluxer machine with `rexctl voice token`, or with **Add a voice server** on the admin panel's voice servers page. The preview doesn't use the token.++```sh+curl -fsS https://chat.example.com/api/voice/join/preview \+ -H "Authorization: VoiceJoin fvj_..." \+ -H "Content-Type: application/json" \+ -d '{"public_ipv4": "203.0.113.7"}'+```++Use the `server_id` from the answer as the server's ID, and its `webhook_url` as the webhook address. A token can set the server ID, and the join then uses the token's ID whatever you send. The answer also shows the region, the hostname the token wants and the soft limit.++Without a token, pick an ID that no other server of your instance uses, such as `fra-server-1`. The webhook address is your instance's public API address followed by `/webhooks/livekit`, such as `https://chat.example.com/api/webhooks/livekit`.++## Fill in the settings++Each server needs its own settings file, Compose project name, key and ports. Make one file for each server, with its ID in the file name.++```sh+cp voice.env.example fra-server-1.env+```++Set at least these values in it:++- `FLUXER_VOICE_ID` to the server's ID.+- `FLUXER_VOICE_API_KEY` to a key no other server of your instance uses, such as `fluxer-fra-server-1`.+- `FLUXER_VOICE_API_SECRET` to the output of `openssl rand -hex 32`.+- `FLUXER_VOICE_WEBHOOK_URL` to the webhook address.+- `FLUXER_VOICE_NODE_IP` to the machine's public IPv4 address, and `FLUXER_VOICE_USE_EXTERNAL_IP` to `false`.++A second server on the same machine needs another `FLUXER_VOICE_TCP_PORT` and `FLUXER_VOICE_UDP_PORT`, such as 7883 and 7884.++## Raise the UDP buffers++LiveKit needs larger UDP buffers for many calls. Raise them once on each machine that runs a voice server, as `rexctl voice join` does.++```sh+printf 'net.core.rmem_max = 16777216\nnet.core.wmem_max = 16777216\n' | sudo tee /etc/sysctl.d/61-fluxer-voice.conf+sudo sysctl -p /etc/sysctl.d/61-fluxer-voice.conf+```++## On another machine++### Set up the edge++Copy the example site and change the address and the server ID in it.++```sh+cp sites.d/voice.caddy.example sites.d/voice-fra.chat.example.com.caddy+cp edge.env.example edge.env+```++The site address is a hostname that points at the machine, or the machine's public IPv4 address. For an address, add this inside the site. Caddy then gets a certificate for the address from Let's Encrypt.++```caddy+tls {+ issuer acme {+ profile shortlived+ }+}+```++Then set `FLUXER_VOICE_EDGE_IP` to that address in `edge.env`. Browsers send no server name for an address, and the edge then answers them with that certificate. Set `FLUXER_VOICE_EDGE_EMAIL` there to get notices from Let's Encrypt.++Each server on the machine gets a `handle_path` block of its own in the site, with its ID in the path and in the upstream. The server's endpoint is `wss://` followed by the site address and `/livekit-` with the ID, such as `wss://voice-fra.chat.example.com/livekit-fra-server-1`.++### Open the ports++Open the server's TCP and UDP ports, and TCP ports 80 and 443, in the firewall on the machine and in your provider's firewall. Behind a router or another NAT, forward each port to the same port number on the machine. [Ports and firewalls](/operator/voice-servers/#ports-and-firewalls) has the details.++### Start it++Start the edge first. It creates the `fluxer-voice` network that the servers join.++```sh+docker compose -p fluxer-voice-edge --env-file edge.env -f voice-edge.compose.yml up -d+docker compose -p fluxer-voice-fra-server-1 --env-file fra-server-1.env -f voice.compose.yml up -d+```++After you change a site, reload Caddy.++```sh+docker compose -p fluxer-voice-edge --env-file edge.env -f voice-edge.compose.yml exec edge caddy reload --config /etc/caddy/Caddyfile+```++Check that the server answers through the edge. It answers `OK`. The first certificate can take a minute.++```sh+curl https://voice-fra.chat.example.com/livekit-fra-server-1/+```++### Behind your own proxy++When nginx or another proxy already holds ports 80 and 443 on the machine, skip the edge and let your proxy serve the server.++1. Create the network that the servers join. Do this once on the machine.++ ```sh+ docker 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.++ ```sh+ docker compose -p fluxer-voice-fra-server-1 --env-file fra-server-1.env -f voice.compose.yml -f voice-http.compose.yml up -d+ ```++4. Point a path of your proxy at that port with WebSocket upgrades and a read timeout of at least 120 seconds. [The nginx and Caddy examples](/operator/voice-servers/#machines-with-something-else-on-ports-80-and-443) show how.+5. Open only the server's TCP and UDP ports in the firewall on the machine and in your provider's firewall. Your proxy already has 80 and 443.++The server's endpoint is the `wss://` address of that path on your proxy, such as `wss://voice.example.com/livekit-fra-server-1`.++## On the Fluxer machine++A server next to a Fluxer instance from the compose bundle uses Fluxer's network and edge. It needs no edge of its own and no DNS record. Use ports other than the bundled server's 7881 and 7882, such as 7883 and 7884.++Find the name of Fluxer's network first. It's the Compose project name followed by `_fluxer`, such as `fluxer_fluxer` for a plain `docker compose up`. Portainer, TrueNAS and Unraid use the stack name as the project name.++```sh+docker network ls --filter name=_fluxer+```++Set these values in the server's settings file as well:++- `FLUXER_VOICE_NETWORK` to that network, such as `fluxer_fluxer`.+- `FLUXER_VOICE_WEBHOOK_URL` to `http://api:8080/webhooks/livekit`.++Start the server from the `voice` folder.++```sh+docker compose -p fluxer-voice-fra-server-1 --env-file fra-server-1.env -f voice.compose.yml up -d+```++Add a route for it to Fluxer's edge. Put this in a file in the `caddy.d` folder next to Fluxer's `docker-compose.yml`, such as `caddy.d/voice-fra-server-1.caddy`.++```caddy+handle_path /livekit-fra-server-1/* {+ import upstream voice-fra-server-1:7880+}+```++Then reload Fluxer's edge from the folder of its `docker-compose.yml`. People connected to Fluxer reconnect once.++```sh+docker compose exec edge caddy reload --config /etc/caddy/Caddyfile+```++The server's endpoint is `wss://` followed by your domain and the path, such as `wss://chat.example.com/livekit-fra-server-1`. Open its TCP and UDP ports in the firewall on the machine and in your provider's firewall.++## Register the server++### With a join token++Call the join route of your instance's API with the token.++```sh+curl -fsS https://chat.example.com/api/voice/join \+ -H "Authorization: VoiceJoin fvj_..." \+ -H "Content-Type: application/json" \+ -d '{+ "server_id": "fra-server-1",+ "region_id": "fra",+ "public_ipv4": "203.0.113.7",+ "endpoint": "wss://voice-fra.chat.example.com/livekit-fra-server-1",+ "api_key": "fluxer-fra-server-1",+ "api_secret": "<the secret from fra-server-1.env>"+ }'+```++Set `region_id` to the region from the preview's answer, and `public_ipv4` to the voice machine's address. These fields set where the server goes. The curl can then run on any machine. Without them, Fluxer places the server by the address the request comes from. Fluxer checks that the endpoint answers and that the key and secret work before it saves the server. Add `latitude` with `longitude` to place the server on the map yourself. The [voice admin API](/admin-api/voice/#join-a-voice-server) lists every field and error.++### In the admin panel++Open **Voice servers** for the region in the admin panel and fill in **Add Voice Server** with these values:++| Field | Value |+| --- | --- |+| Server ID | The value of `FLUXER_VOICE_ID` |+| Endpoint | The server's endpoint |+| API Key | The value of `FLUXER_VOICE_API_KEY` |+| API Secret | The value of `FLUXER_VOICE_API_SECRET` |+| Internal URL | The address the API connects to, or nothing |++On the Fluxer machine, set **Internal URL** to `http://voice-` followed by the ID and `:7880`, such as `http://voice-fra-server-1:7880`. On another machine, leave it empty unless the API connects to the server on a private address.++## Upgrade++The bundle of each Fluxer release sets the LiveKit image of that release in `voice.compose.yml`, and the Caddy image of the edge in `voice-edge.compose.yml`.++1. Download the bundle of the new release.+2. Copy `voice.compose.yml`, `voice-http.compose.yml`, `voice-edge.compose.yml` and `Caddyfile.voice` from its `voice` folder over yours. Keep `sites.d` and your settings files as they are.+3. On a machine with the edge, run the edge's `up -d` command first.+4. Run the `up -d` command of each server.++A `FLUXER_LIVEKIT_IMAGE` in the server's settings file overrides the release's image. Remove that line to follow the release.++Docker waits up to `FLUXER_VOICE_STOP_GRACE`, 15 minutes by default, for calls on the server to end. Turn on **Draining** for the server in the admin panel first. New calls then go elsewhere while it waits. Turn it off when the server is back.++## Remove a server++1. Turn on **Draining** for the server in the admin panel, and wait for its calls to end.+2. Delete the server in the admin panel. A server that joined with a token can also delete itself with `DELETE /v1/voice/node`, as the [voice admin API](/admin-api/voice/) describes.+3. Stop it and delete its container.++ ```sh+ docker compose -p fluxer-voice-fra-server-1 --env-file fra-server-1.env -f voice.compose.yml down -v+ ```++4. Delete its `handle_path` block, or its file in `caddy.d` on the Fluxer machine, and reload Caddy.+5. Close its TCP and UDP ports in the firewall on the machine and in your provider's firewall.diff --git a/fluxer_docs/src/content/docs/operator/voice-servers.mdx b/fluxer_docs/src/content/docs/operator/voice-servers.mdxnew file mode 100644index 000000000..7b0caeb12--- /dev/null+++ b/fluxer_docs/src/content/docs/operator/voice-servers.mdx@@ -0,0 +1,329 @@+---+# SPDX-License-Identifier: AGPL-3.0-or-later+title: Add voice servers+description: Run extra LiveKit voice servers next to Fluxer or on other machines, and connect them to your instance with rexctl.+sidebar:+ label: Voice servers+---++A Fluxer instance comes with one LiveKit voice server on the same machine, the bundled server. You can add more, or [run without it](#run-without-the-bundled-voice-server). rexctl installs each one, gives it its own keys and ports, sets up HTTPS, registers it with your instance and checks that calls can connect to it.++## When to add one++Add a voice server to:++- Put calls closer to people in another part of the world, in a voice region of its own.+- Spread calls over more machines when one machine runs out of CPU or bandwidth.+- Give a server its own public IP address, or keep it for some communities with restrictions in the admin panel.+- Keep large calls away from the rest.++A call always stays on one server. Fluxer picks the server when the first person joins, and everyone who joins later goes to the same one. A voice server on the same machine as Fluxer shares its CPU and adds no capacity. It helps when the machine has more than one public address, or when you want to keep some calls apart.++## On the Fluxer machine++```sh+rexctl voice add+```++rexctl picks every value and shows them in one summary before it changes anything:++- **ID.** The region followed by `-server-` and the first free number, such as `default-server-2`. rexctl skips IDs that another voice server on the machine uses, whatever instance it belongs to. While the bundled server is on, `-server-1` in the default region stays free for it, even after you delete it in the admin panel.+- **Region.** The default region of your instance. Pass `--region` for another existing region, or `--new-region` with `--region-name`, `--lat` and `--long` to create one.+- **Ports.** The next free TCP and UDP pair from 7881 and 7882 upwards, skipping the ports of every other server and instance on the machine.+- **Endpoint.** A path on your domain, such as `wss://chat.example.com/livekit-default-server-2`. It needs no DNS record, no new certificate and no change to a proxy that forwards every path.+- **Keys.** The key `fluxer-` followed by the ID, and a new random secret.+- **Address, memory and image.** The same public address, LiveKit release and memory limit as the bundled server.+- **Soft limit.** 75 connections per CPU. Placement prefers another server once a server holds this many.++Add several at once with `--count`.++```sh+rexctl voice add --count 3+```++When the machine has more than one public IPv4 address, pass `--ip` to give a server its own. LiveKit gives that address to clients and Docker publishes the server's ports on it alone.++Behind 1:1 NAT, as on AWS, Google Cloud, Oracle Cloud or a home router, the public address isn't on the machine. Pass it with `--ip` all the same. LiveKit gives it to clients, and Docker publishes the ports on every address of the machine.++```sh+rexctl voice add --ip 198.51.100.8+```++A server added without `--ip` follows `voice.public_ip`. When you change that setting with `rexctl config set`, rexctl gives these servers the new address and restarts them one at a time, as an upgrade does. When Fluxer is stopped, they get it at the next `rexctl start`. A server added with `--ip` keeps its address.++After it adds a server, rexctl prints its TCP and UDP ports. Open them in your provider's firewall as well. The test at the end runs on the same machine and can't see that firewall.++Adding a server writes a route to `caddy.d` and reloads the edge. People connected to Fluxer reconnect once, and calls keep going.++When you change the domain or the public HTTPS port with `rexctl config set`, rexctl moves these servers to the new address in your instance. When Fluxer was stopped at the time, it moves them at the next `rexctl start`.++## On another machine++On the Fluxer machine, create a join token.++```sh+rexctl voice token+```++It prints the command to run on the voice machine.++```sh+curl -fsSL https://fluxer.dev/install.sh | sh -s -- voice chat.example.com fvj_...+```++With rexctl already on the voice machine, this does the same.++```sh+rexctl voice join chat.example.com fvj_...+```++The token works once and expires after an hour. Pass `--expires` to `rexctl voice token` for up to 24 hours. The **Add a voice server** button on the admin panel's voice servers page creates the same token and shows the same command.++The join command does this:++1. Reads your instance's public settings from its domain and stops when voice is off there.+2. Installs Docker when the machine has neither Docker nor Podman, as `rexctl install` does. On a machine with Podman older than 5.8.2 and no Fluxer instance, it stops. That Podman doesn't start voice servers again after a restart.+3. Finds the machine's public IPv4 address, its CPU count, its memory and its firewall.+4. Asks your instance for the server's ID, region and webhook address, without using the token.+5. Picks free ports and a hostname or IP address for the endpoint.+6. Shows a summary and asks once. Pass `--yes` to go ahead without asking.+7. Raises the UDP buffer limits in `/etc/sysctl.d/61-fluxer-voice.conf`, which LiveKit needs for many calls. This needs root. Without root, rexctl shows the two commands to run with sudo. Then it starts an edge on ports 80 and 443 with Caddy, and the LiveKit server behind it.+8. Opens the server's ports, and ports 80 and 443, in ufw or firewalld.+9. Waits for a certificate, then registers the server with your instance. The token stops working once it's registered.+10. Checks that browsers on your instance can connect to the server, that calls connect over UDP and TCP, and that your instance accepts the server's webhooks.++When a step fails, run the same command again with the same token. rexctl picks up where it stopped, because the token is used only once the server is registered.++A machine can run more than one voice server. Each one needs a token of its own and a `rexctl voice join` of its own. They share the machine's edge and get their own ports.++rexctl keeps the voice servers of a machine in the voice folder. That's `/opt/fluxer-voice` when rexctl runs as root, and `~/fluxer-voice` for any other account. Pass `--dir` to use another folder.++The token is a command line argument, which other accounts on the machine can see while the command runs. rexctl also reads it from `FLUXER_VOICE_JOIN_TOKEN`, or from standard input with `--token-stdin`.++### Join tokens and trust++A voice server handles the audio and video of every call placed on it, and whoever runs it can record those calls. A join token lets one machine add one server. Give it only to a machine you trust. The token gives no other access to your instance.++```sh+rexctl voice token list+rexctl voice token revoke <id>+```++A token can set the region, the server ID, the hostname, a soft limit and the restrictions of the new server. The machine that joins can't change those.++## Hostnames and certificates++With no hostname, the endpoint uses the machine's public IPv4 address, such as `wss://203.0.113.7/livekit-fra-server-1`. Caddy gets a short-lived Let's Encrypt certificate for that address. The certificate lasts about 6 days, and Caddy renews it before it expires. This needs ports 80 and 443 open to the internet and no DNS record.++To use a hostname, add an `A` record that points at the machine first.++```+voice-fra.chat.example.com. A 203.0.113.7+```++Then pass it when you join.++```sh+rexctl voice join chat.example.com fvj_... --hostname voice-fra.chat.example.com+```++When `voice-` followed by the region and your domain already points at the machine, rexctl uses it without being asked. When the hostname doesn't point at the machine yet, rexctl prints the record and waits up to 10 minutes. After that it uses the IP address and prints the command to switch later. With `--yes`, or without a terminal, it uses the IP address straight away.++A token made with `--hostname` works only with that hostname. rexctl stops when you pass another one. It waits up to 10 minutes for the name to point at the machine, also with `--yes`, and stops before it starts anything when the name still doesn't. Add the record, then run the same command again.++```sh+rexctl voice hostname fra-server-1 voice-fra.chat.example.com+```++On Cloudflare, set this record to **DNS only**. `rexctl voice join` checks that the name points at the machine before the edge asks Let's Encrypt for a certificate, and a proxied name points at Cloudflare. Remove any `AAAA` record that points somewhere else, because Let's Encrypt checks IPv6 first.++A server from `rexctl voice add` uses your instance's domain, which can stay proxied. Only signalling goes through Cloudflare. Audio and video go straight to the machine's IP address, which LiveKit gives to clients.++Voice servers serve IPv4 only. A machine with only an IPv6 address can't run one.++## Ports and firewalls++Each server needs these ports.++| Port | Use |+| --- | --- |+| TCP port of the server, 7881 for the first | Media over TCP, for networks that block UDP |+| UDP port of the server, 7882 for the first | Media |+| TCP 80 and 443, on a voice machine | The edge, for signalling and certificates |++rexctl opens them in ufw or firewalld when one of them is on. Your provider's firewall, such as a Hetzner Cloud Firewall or an AWS security group, is outside the machine, and you need to open the same ports there yourself. Docker publishes ports past ufw. An open port in ufw doesn't prove that the port is open from the internet.++A provider firewall that follows connections, such as a Hetzner Cloud Firewall, can let call audio over UDP through with the UDP port closed. It lets replies in once the server has sent packets to a client. TCP still fails. Open both ports.++After you change a firewall, run `rexctl voice test <id>` on the Fluxer machine for a server on another machine. It connects from outside that machine and reports UDP and TCP on their own. A server from `rexctl voice add` runs on the Fluxer machine, and its test can't see your provider's firewall. Check that firewall by hand, or join a call from another network.++Behind a router or another NAT, forward each port to the same port number on the machine. LiveKit tells clients the port it listens on, and a different outside port doesn't work.++## Networks a voice server can't serve++- **Shared address NAT (CGNAT, 100.64.0.0/10).** A machine with an address from 100.64.0.0/10 on its main network usually can't take incoming calls, and rexctl stops. Some providers use that range for private networks behind 1:1 NAT. When the machine has a public IPv4 address of its own, pass it with `--ip`. CGNAT behind a home router doesn't show on the machine. Run `rexctl voice test <id>` on the Fluxer machine to find it.+- **IPv6 only.** Voice servers need a public IPv4 address.+- **Networks that allow only port 443.** Some office and school networks block everything but HTTPS. Calls from there need TURN over TLS on port 443, which rexctl doesn't set up. See LiveKit's guide to [TURN](https://docs.livekit.io/transport/self-hosting/deployment/#improving-connectivity-with-turn).++## Machines with something else on ports 80 and 443++When another rexctl Fluxer instance holds ports 80 and 443 on the voice machine, the server uses that instance's domain and edge, such as `wss://other.example.com/livekit-fra-server-1`. You can't pass `--hostname` then.++The server's files go in the voice folder, and the Fluxer instance stays the default folder. `rexctl voice status`, `rexctl voice test` and `rexctl voice remove` find the server by its ID. The server uses that instance's domain, and `rexctl voice hostname` doesn't apply to it. `rexctl upgrade` on the Fluxer instance upgrades it too. Other commands for it need `--dir` with the voice folder.++When nginx or another program holds them, rexctl stops. Pass `--no-edge` and the public address of your own proxy.++```sh+rexctl voice join chat.example.com fvj_... --no-edge --endpoint wss://voice.example.com/livekit-fra-server-1+```++rexctl 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.++nginx needs a location block like this one.++```nginx+location /livekit-fra-server-1/ {+ proxy_pass http://127.0.0.1:7880/;+ proxy_http_version 1.1;+ proxy_set_header Host $host;+ proxy_set_header Upgrade $http_upgrade;+ proxy_set_header Connection "upgrade";+ proxy_read_timeout 120s;+}+```++Caddy needs a route like this one.++```caddy+voice.example.com {+ handle_path /livekit-fra-server-1/* {+ reverse_proxy 127.0.0.1:7880+ }+}+```++## Regions and placement++On another machine, the server goes to the region in the token. Without one, it goes to the existing region nearest to the machine, when your instance has a GeoIP database, and to the default region otherwise. The summary shows the region and the distance. Pass `--region` to pick another existing region. Fluxer never creates a region by itself, because GeoIP data for cloud addresses is often wrong. Create a region with a token for it, or with `rexctl voice add` on the Fluxer machine. Give the region's location, because placement sends people to the nearest region.++```sh+rexctl voice token --new-region fra --region-name Frankfurt --lat 50.1 --long 8.7+rexctl voice add --new-region fra --region-name Frankfurt --lat 50.1 --long 8.7+```++A token without `--lat` and `--long` puts the new region where your instance's GeoIP database places the voice machine, or at the place you pass to `rexctl voice join` with `--lat` and `--long`. Without either, the region gets latitude 0 and longitude 0. Fix that in the admin panel.++A channel with a region set in its settings uses that region. Other calls go to the nearest region of the person who starts them. Within a region, Fluxer takes turns between the servers and prefers servers below their soft limit. Restrictions on a server or region in the admin panel apply to new calls on it.++Every API node checks every active server every 15 seconds. A server that fails two checks in a row gets no new calls, and calls that reconnect move to another server. When every server in a region is down or draining, new calls for that region go to a server in another region. When every server is down, Fluxer places calls as before.++Fluxer lists the address of every voice server in its discovery document, and the web app allows browsers to connect to them. Anyone can read that list, as anyone who joins a call already sees the address of its server.++The web app reads that list about once a minute. `rexctl voice join` and `rexctl voice hostname` wait until browsers can connect to a new address. A page opened before then can't join calls on that server until it's reloaded.++## Run without the bundled voice server++Calls can go only to the servers you add, with no LiveKit next to Fluxer. Install that way from the start, or turn the bundled server off later.++```sh+rexctl install --no-bundled-voice+rexctl config set voice.bundled false+```++Fluxer then:++- Stops and removes the bundled LiveKit container, and no longer needs ports 7881 and 7882. `rexctl config set` offers to close them in the firewall.+- Creates the default region with no server in it, or removes `default-server-1` from it when that server is the bundled one. Servers you added stay as they are.+- Places every call on the servers you add with `rexctl voice add`, `rexctl voice join` or the admin panel. Until you add one, calls can't start.++`rexctl config set voice.bundled true` starts the bundled server again, opens its ports and adds `default-server-1` back to the default region.++With Docker Compose and no rexctl, add `external-voice.compose.yml` to `COMPOSE_FILE` and set `FLUXER_LIVEKIT_BUNDLED=false` in `.env`. To turn it back on, take both out again and add `default-server-1` in the admin panel, with the key and secret from `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET`.++## Check a server++```sh+rexctl voice list+rexctl voice status+rexctl voice test+rexctl doctor+```++`rexctl voice list` on the Fluxer machine shows every server of your instance with its health, and marks the ones that run on that machine. On a voice machine it shows the servers there.++`rexctl voice test` joins a throwaway room and reports whether UDP and TCP connect. A test on the machine that runs the server can't see your provider's firewall or your router. On the Fluxer machine, `rexctl voice test <id>` tests a server on another machine from outside its network, with a token your instance signs. When the server rejects that token, your instance has another secret for it than the server has.++## Upgrade++`rexctl upgrade` on the Fluxer machine upgrades Fluxer first. Then it moves each voice server on the machine to the new LiveKit release, one at a time. It marks the server as draining. New calls then go elsewhere, and calls on it keep going. Docker then replaces the container, and LiveKit waits until those calls end, for up to 15 minutes. The server then takes new calls again, also when the upgrade fails or you stop it. A server you turned off or marked as draining in the admin panel stays that way.++On a voice machine, `rexctl upgrade` updates rexctl to the release your instance runs, then upgrades the servers the same way. It never goes past your instance's release, and it refuses `--prefetch` and `--to`. It also updates the edge.++rexctl upgrades a server when the release has a new LiveKit image, and also when the release changes the server's files or settings. A server added with `--image` keeps that image.++A voice machine doesn't upgrade with the Fluxer machine. Run `rexctl upgrade` there after you upgrade Fluxer, or run `rexctl auto-upgrade on` on it to upgrade it every day.++## Stop, restart and back up++`rexctl stop` on the Fluxer machine stops the voice servers there too, and calls on them end. `rexctl start` starts them again. `rexctl restart` restarts Fluxer and leaves running voice servers alone.++`rexctl backup` keeps the voice servers of the instance, with their keys. `rexctl restore` writes their files again, starts them and opens their ports in the firewall. A server added after the backup is removed by the restore.++Changing `network.ipv6` with `rexctl config set` rebuilds Fluxer's network. rexctl stops the voice servers on the machine first and starts them again afterwards, and calls on them end.++On a voice machine, `rexctl stop`, `rexctl restart`, `rexctl logs` and `rexctl backup` don't apply. Docker runs each server as `fluxer-voice-<id>-livekit-1`, and the edge as `fluxer-voice-edge-edge-1`.++```sh+docker logs fluxer-voice-fra-server-1-livekit-1+docker logs fluxer-voice-edge-edge-1+docker restart fluxer-voice-fra-server-1-livekit-1+```++A voice machine has nothing to back up. When you lose one, remove the server in the admin panel and join a new machine with a new token.++## Remove a server++Run it on the machine that runs the server. For a server on a machine you no longer have, delete it in the admin panel.++```sh+rexctl voice remove fra-server-1+```++rexctl marks the server as draining, waits up to 15 minutes for its calls to end, removes it from your instance, and deletes its container, files, route and the ports it opened. Pass `--now` to skip the wait.++On a voice machine, `rexctl voice remove --all` or `rexctl uninstall` removes every server and the edge. `rexctl uninstall` on the Fluxer machine removes its voice servers too.++## Troubleshooting++`rexctl doctor` checks voice servers with these checks.++The secret of a server is `FLUXER_VOICE_API_SECRET` in `voice/<id>/.env` in the rexctl folder. On a voice machine that's the voice folder.++| Check | What's wrong | What to do |+| --- | --- | --- |+| `voice-servers` | A container isn't running, or the endpoint doesn't answer through the edge. | Run `rexctl voice status <id>`. |+| `voice-endpoints` | A server from `rexctl voice add` still uses an old domain or port of your instance. | Run `rexctl doctor --fix`. |+| `voice-ports` | A port isn't published, another container publishes it, or the firewall on the machine closes it. | Open the port, and check your provider's firewall. |+| `voice-node-ip` | The server gives clients another address than the machine's public address. | Set the right address. The steps follow this table. |+| `voice-tls` | The certificate of an endpoint is missing or expires within 2 days. | Open ports 80 and 443. `docker logs fluxer-voice-edge-edge-1` shows the error from Let's Encrypt. |+| `voice-registration` | The instance deleted the server, has another secret for it, or rejects the time on its requests. | Run `rexctl voice remove <id>` and join with a new token, put the secret back in the admin panel, or fix the clock. |+| `voice-webhook` | The instance doesn't accept a signed test webhook. A rejection means it has another secret for the server. | Put the server's secret back in the admin panel, or check that the webhook address answers. |+| `voice-keys` | Two servers share an API key. | Give each server its own key. Remove the newer server and add it again. |+| `voice-health` | The API can't connect to a server, or a server has been draining for more than an hour. | Run `rexctl voice status` on the machine that runs it. For a server stuck draining, clear Draining in the admin panel. |++When `voice-node-ip` warns, set the address the server gives clients. On the Fluxer machine, run `rexctl config set voice.public_ip <address>`. A server added with `--ip` keeps its address. Remove it and add it again with the right `--ip`. On a voice machine, run `rexctl voice remove <id>`, then join again with a new token and the right `--ip`.++When `voice-registration` says the clock on a voice machine is off, turn on time sync there with `sudo timedatectl set-ntp true`. Fluxer allows a minute of difference.++People on the same network as a voice machine at home may fail to connect while everyone else can. That happens when the router can't send traffic for its own public address back inside. The router needs NAT loopback, also called hairpin NAT, for those clients. A local DNS record isn't enough, because LiveKit gives clients the server's public address for audio and video. Turn on NAT loopback in the router, or run the server elsewhere.++When the instance is behind a firewall or bot protection, such as Cloudflare's, let these requests through from your voice machines:++- `/.well-known/fluxer` and `/` on your domain.+- `voice/join`, `voice/join/preview`, `voice/node` and `webhooks/livekit` under your instance's public API address. With the API on your domain, these are `/api/voice/` and `/api/webhooks/livekit`. With the API on a host of its own, such as `api.example.com`, they have no `/api` in front.++rexctl and LiveKit send them without a browser, and bot protection can block them. A voice machine that can't send them can't join, can't drain its servers for an upgrade and can't remove them cleanly. Webhooks tell Fluxer when people leave calls.++## Without rexctl++To run a voice server with Docker Compose by hand, see [Run a voice server with Docker Compose](/operator/voice-compose/). A join token also adds a voice server to an instance on [Kubernetes](/operator/kubernetes/), because joining talks only to the instance's public API.diff --git a/fluxer_docs/src/content/docs/voice/index.md b/fluxer_docs/src/content/docs/voice/index.mdindex 50f575448..5a45be10d 100644--- a/fluxer_docs/src/content/docs/voice/index.md+++ b/fluxer_docs/src/content/docs/voice/index.md@@ -79,7 +79,7 @@ A refused join sends no Voice Server Update. The client gets a [Gateway Error](/### Regions-[List RTC regions](/http-api/channels/#list-rtc-regions) returns the regions the caller can pick for a channel, and the list can be empty. [Modify channel](/http-api/channels/#modify-channel) sets `rtc_region`, which needs UPDATE_RTC_REGION, and a null region lets Fluxer pick one. Instance admins manage regions and voice servers through the [Admin voice resource](/admin-api/voice/).+[List RTC regions](/http-api/channels/#list-rtc-regions) returns the regions the caller can pick for a channel, and the list can be empty. [Modify channel](/http-api/channels/#modify-channel) sets `rtc_region`, which needs UPDATE_RTC_REGION, and a null region lets Fluxer pick one. Instance admins manage regions and voice servers through the [Admin voice resource](/admin-api/voice/), and run more servers with [Add voice servers](/operator/voice-servers/).## Private callsdiff --git a/fluxer_docs/src/installer/install.sh b/fluxer_docs/src/installer/install.shindex da53abff0..c3a64d24f 100644--- a/fluxer_docs/src/installer/install.sh+++ b/fluxer_docs/src/installer/install.sh@@ -137,6 +137,22 @@ map_legacy_flags() {[ "$help" -eq 0 ] || add --help}+voice_join_shorthand() {+ [ "${1:-}" = voice ] || return 1+ case ${2:-} in+ '' | -* | add | join | token | list | status | test | hostname | remove | help) return 1 ;;+ esac+ case ${3:-} in+ fvj_*) return 0 ;;+ '' | -*)+ case $2 in+ *.*) return 0 ;;+ esac+ ;;+ esac+ return 1+}+self_dir=''case $0 in*/install.sh) self_dir=$(cd "$(dirname "$0")" 2>/dev/null && pwd) || self_dir='' ;;@@ -146,7 +162,12 @@ trial=0case ${1:-} in'' | -*) map_legacy_flags "$@" ;;*)- add "$@"+ if voice_join_shorthand "$@"; then+ shift+ add voice join "$@"+ else+ add "$@"+ fifor arg in "$@"; docase $arg in--dry-run | --check) trial=1 ;;diff --git a/fluxer_rexctl/cmd/app.go b/fluxer_rexctl/cmd/app.goindex 60451999a..a7e45dcba 100644--- a/fluxer_rexctl/cmd/app.go+++ b/fluxer_rexctl/cmd/app.go@@ -52,11 +52,13 @@ type App struct {engine *engine.Enginewarned boollocated *instance.Located+ command stringkeepLayout bool}func (a *App) init(c *cobra.Command) {+ a.command = c.CommandPath()a.UI = ui.New(ui.Options{Context: c.Context(),Yes: a.Flags.Yes,@@ -228,6 +230,9 @@ func (a *App) InstanceRepairing(keys []string) (*instance.Instance, error) {if err := rootlessGuard(inst); err != nil {return nil, err}+ if inst.State.IsVoice() && !voiceCommandAllowed(a.command) {+ 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)a.rememberFirst(inst)return inst, nildiff --git a/fluxer_rexctl/cmd/autoupgrade.go b/fluxer_rexctl/cmd/autoupgrade.goindex 236c486f7..0ee15efe5 100644--- a/fluxer_rexctl/cmd/autoupgrade.go+++ b/fluxer_rexctl/cmd/autoupgrade.go@@ -60,7 +60,7 @@ func newAutoUpgradeCmd(app *App) *cobra.Command {c := &cobra.Command{Use: "auto-upgrade",Short: "Show whether rexctl upgrades Fluxer by itself",- Long: "Show whether rexctl upgrades Fluxer by itself, when it runs next and how the last run went. Each run is rexctl upgrade --yes and works like an upgrade you run yourself.",+ Long: "Show whether rexctl upgrades Fluxer, or the voice servers on a voice machine, by itself, when it runs next and how the last run went. Each run is rexctl upgrade --yes and works like an upgrade you run yourself.",Args: noArgs,RunE: func(c *cobra.Command, _ []string) error {return runAutoUpgradeShow(c.Context(), app)@@ -190,6 +190,9 @@ func runAutoUpgradeOn(ctx context.Context, app *App, o autoUpgradeOptions) errorif err != nil {return err}+ if inst.State.IsVoice() && (scope != backup.ScopeFull || fullBackup != "") {+ return ui.Usage("A voice machine has nothing to back up. Leave out --backup and --full-backup.")+ }if err := managed(inst); err != nil {return err}@@ -219,7 +222,7 @@ func runAutoUpgradeOn(ctx context.Context, app *App, o autoUpgradeOptions) errorif err != nil {return ui.Usage("Pass --at as a time of day like 04:00.")}- app.UI.OK("rexctl upgrades Fluxer %s.", autoUpgradeWhen(a.Scheduler, startDay, start))+ app.UI.OK("rexctl upgrades %s %s.", autoUpgradeTarget(inst), autoUpgradeWhen(a.Scheduler, startDay, start))if fullBackup != "" {backupAt, _ := a.BackupStart()app.UI.Info("%s", fullBackupWhen(backupAt))@@ -323,10 +326,10 @@ func showAutoUpgrade(app *App, inst *instance.Instance, a host.AutoUpgrade, s horeturn app.UI.JSON(out)}if !s.On {- app.UI.Info("Automatic upgrades are off. Run rexctl auto-upgrade on to upgrade Fluxer every day or once a week.")+ app.UI.Info("Automatic upgrades are off. Run rexctl auto-upgrade on to upgrade %s every day or once a week.", autoUpgradeTarget(inst))return nil}- app.UI.OK("rexctl upgrades Fluxer %s.", autoUpgradeWhen(a.Scheduler, s.Day, s.At))+ app.UI.OK("rexctl upgrades %s %s.", autoUpgradeTarget(inst), autoUpgradeWhen(a.Scheduler, s.Day, s.At))if s.FullBackup != "" {app.UI.Info("%s", fullBackupWhen(s.FullBackupAt))}@@ -348,6 +351,13 @@ func showAutoUpgrade(app *App, inst *instance.Instance, a host.AutoUpgrade, s horeturn nil}+func autoUpgradeTarget(inst *instance.Instance) string {+ if inst.State.IsVoice() {+ return "the voice servers on this machine"+ }+ return "Fluxer"+}+func autoUpgradeWhen(s host.Scheduler, day, at string) string {every := "every day"if long := host.DayLong(day); long != "" {diff --git a/fluxer_rexctl/cmd/autoupgrade_test.go b/fluxer_rexctl/cmd/autoupgrade_test.goindex cbca7e47b..59c52119c 100644--- a/fluxer_rexctl/cmd/autoupgrade_test.go+++ b/fluxer_rexctl/cmd/autoupgrade_test.go@@ -136,6 +136,19 @@ func TestUnknownDayIsRefused(t *testing.T) {}}+func TestVoiceMachineRefusesBackupOptions(t *testing.T) {+ inst := voiceMachine(t)+ app, _ := testVoiceApp()+ app.Flags.Dir = inst.Dir+ for _, o := range []autoUpgradeOptions{{At: "04:00", FullBackup: "monthly"}, {At: "04:00", Backup: backup.ScopeWithoutUploads}} {+ err := runAutoUpgradeOn(context.Background(), app, o)+ var ee *ui.ExitError+ if !errors.As(err, &ee) || ee.Code != ui.ExitUsage || !strings.Contains(ee.Msg, "voice machine") {+ t.Errorf("%+v returned %v, want a usage error about the voice machine", o, err)+ }+ }+}+func TestDayAcceptsFullNames(t *testing.T) {for in, want := range map[string]string{"sun": "Sun", "Sunday": "Sun", "MONDAY": "Mon", "thu": "Thu", "saturday": "Sat", "": ""} {got, err := parseDay(in)diff --git a/fluxer_rexctl/cmd/bundle.go b/fluxer_rexctl/cmd/bundle.goindex 74876b55f..30e9fd584 100644--- a/fluxer_rexctl/cmd/bundle.go+++ b/fluxer_rexctl/cmd/bundle.go@@ -38,6 +38,12 @@ func newBundleCmd(app *App) *cobra.Command {if err := os.WriteFile(filepath.Join(out, ".env.example"), []byte(instance.BundleEnv()), 0o644); err != nil {return ui.Wrap(err, fmt.Sprintf("Couldn't write the files to %s.", out), "Check that you can write to that folder.")}+ if err := os.WriteFile(filepath.Join(out, "voice", "voice.env.example"), []byte(instance.VoiceBundleEnv()), 0o644); err != nil {+ return ui.Wrap(err, fmt.Sprintf("Couldn't write the files to %s.", out), "Check that you can write to that folder.")+ }+ if err := os.WriteFile(filepath.Join(out, "voice", "edge.env.example"), []byte(instance.VoiceEdgeBundleEnv()), 0o644); err != nil {+ return ui.Wrap(err, fmt.Sprintf("Couldn't write the files to %s.", out), "Check that you can write to that folder.")+ }return nil},}diff --git a/fluxer_rexctl/cmd/bundledvoice_test.go b/fluxer_rexctl/cmd/bundledvoice_test.gonew file mode 100644index 000000000..1c4cc976d--- /dev/null+++ b/fluxer_rexctl/cmd/bundledvoice_test.go@@ -0,0 +1,73 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "errors"+ "slices"+ "strings"+ "testing"++ "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/ui"+)++func TestInstallWithoutBundledVoiceOpensNoVoicePorts(t *testing.T) {+ in := &installer{f: &installFlags{NoVoice: true}}+ for _, p := range in.firewallPorts() {+ if p.Number == 7881 || p.Number == 7882 {+ t.Fatalf("firewall ports %v include a voice port", in.firewallPorts())+ }+ }+ if got := portList(in.firewallPorts()); got != "80 and 443" {+ t.Fatalf("firewall ports %s", got)+ }+ in = &installer{f: &installFlags{}}+ if got := in.firewallPorts(); !slices.Contains(got, host.Port{Number: 7881, Proto: "tcp"}) || !slices.Contains(got, host.Port{Number: 7882, Proto: "udp"}) {+ t.Fatalf("firewall ports %v leave out the bundled LiveKit", got)+ }+}++func TestInstallRefusesVoicePortsWithoutBundledVoice(t *testing.T) {+ err := validateInstallFlags(&installFlags{Project: defaultProject, NoVoice: true, VoiceTCPPort: 7883})+ var ee *ui.ExitError+ if !errors.As(err, &ee) || ee.Code != ui.ExitUsage || !strings.Contains(ee.Msg, "--no-bundled-voice") {+ t.Fatalf("got %v", err)+ }+ if err := validateInstallFlags(&installFlags{Project: defaultProject, NoVoice: true}); err != nil {+ t.Fatalf("--no-bundled-voice on its own: %v", err)+ }+}++func TestBundledServerIDStaysTakenForVoiceAdd(t *testing.T) {+ inst := &instance.Instance{Dir: t.TempDir(), State: &instance.State{Settings: map[string]string{}}}+ if got := takenVoiceIDs(nil, inst); !slices.Contains(got, "default-server-1") {+ t.Fatalf("taken IDs %v leave out the bundled server", got)+ }+ inst.State.Settings["FLUXER_LIVEKIT_DEFAULT_REGION"] = `{"id":"fra","name":"Frankfurt","emoji":"x","latitude":50,"longitude":8}`+ if got := takenVoiceIDs(nil, inst); !slices.Contains(got, "fra-server-1") {+ t.Fatalf("taken IDs %v leave out the bundled server of the fra region", got)+ }+ inst.State.Settings[settings.VoiceBundled] = "false"+ if got := takenVoiceIDs(nil, inst); slices.Contains(got, "fra-server-1") {+ t.Fatalf("taken IDs %v keep the bundled server after it is turned off", got)+ }+}++func TestLogsWithoutBundledVoiceRefusesTheVoicePart(t *testing.T) {+ inst := &instance.Instance{Dir: t.TempDir(), State: &instance.State{Settings: map[string]string{settings.VoiceBundled: "false"}}}+ _, err := runningPart(inst, []string{"voice"}, []string{"livekit"})+ var ee *ui.ExitError+ if !errors.As(err, &ee) || ee.Code != ui.ExitRefused || !strings.Contains(ee.Msg, "bundled LiveKit") {+ t.Fatalf("got %v", err)+ }+ if got, err := runningPart(inst, []string{"api"}, []string{"api"}); err != nil || !slices.Equal(got, []string{"api"}) {+ t.Fatalf("api part = %v, %v", got, err)+ }+ inst.State.Settings[settings.VoiceBundled] = "true"+ if got, err := runningPart(inst, []string{"voice"}, []string{"livekit"}); err != nil || !slices.Equal(got, []string{"livekit"}) {+ t.Fatalf("voice part with the bundled LiveKit = %v, %v", got, err)+ }+}diff --git a/fluxer_rexctl/cmd/config.go b/fluxer_rexctl/cmd/config.goindex 5eea9111c..3a78a7b02 100644--- a/fluxer_rexctl/cmd/config.go+++ b/fluxer_rexctl/cmd/config.go@@ -771,6 +771,14 @@ func applySettings(ctx context.Context, app *App, inst *instance.Instance, e *enr = <-done}}+ if r.change != nil && len(r.change.VoiceDown) > 0 {+ ids := ui.JoinAnd(r.change.VoiceDown)+ if r.change.Applied || r.err != nil {+ app.UI.Warn(fmt.Sprintf("Voice servers %s didn't start again.", ids), "Run rexctl voice status to see why.")+ } else {+ app.UI.Info("Stopped voice servers %s. They start again with Fluxer.", ids)+ }+ }if errors.Is(r.err, deploy.ErrDeclined) {return nil, &ui.ExitError{Code: ui.ExitRefused, Msg: "rexctl left your settings as they were.", Err: r.err}}@@ -779,6 +787,13 @@ func applySettings(ctx context.Context, app *App, inst *instance.Instance, e *en}if r.change.Applied && !r.change.Unchanged {closePorts()+ moveVoiceEndpoints(ctx, app, inst)+ }+ if _, ok := changes[settings.VoicePublicIP]; ok && !r.change.Unchanged {+ moveVoicePublicIP(ctx, app, inst, e, r.change.Applied)+ }+ if _, ok := changes[settings.VoiceBundled]; ok && r.change.Applied && !r.change.Unchanged {+ restoreBundledVoiceServer(ctx, app, inst)}return r.change, nil}@@ -828,7 +843,7 @@ func confirmPreview(app *App, p deploy.Preview) (bool, error) {func openPorts(ctx context.Context, app *App, inst *instance.Instance, changes map[string]*string) (func(), error) {nothing := func() {}opened, closed, err := deploy.PortChanges(inst, changes)- if err != nil || len(opened) == 0 {+ if err != nil || (len(opened) == 0 && len(closed) == 0) {return nothing, err}fw, err := host.DetectFirewall(ctx)@@ -851,6 +866,9 @@ func openPorts(ctx context.Context, app *App, inst *instance.Instance, changes m}}return func() {+ if len(closed) == 0 {+ return+ }ok, err := app.UI.Confirm(fmt.Sprintf("Close port %s in the firewall? Fluxer doesn't use it any more.", deploy.PortList(closed)), true)if err != nil || !ok {returndiff --git a/fluxer_rexctl/cmd/doctor.go b/fluxer_rexctl/cmd/doctor.goindex 0c89147d6..5a7597d16 100644--- a/fluxer_rexctl/cmd/doctor.go+++ b/fluxer_rexctl/cmd/doctor.go@@ -49,6 +49,9 @@ func runDoctor(ctx context.Context, app *App, fix bool) error {case err == nil:env.Instance = instapp.UI.Info("Using %s (%s).", inst.Dir, l.Why())+ if inst.State.IsVoice() {+ phase = checks.Voice+ }case errors.Is(err, instance.ErrNotFound):phase = checks.Preinstallcase errors.Is(err, instance.ErrLegacy):@@ -68,7 +71,7 @@ func runDoctor(ctx context.Context, app *App, fix bool) error {}e, err := app.Engine(ctx, env.Instance)env.SetEngine(e, err)- if err == nil {+ if err == nil && phase != checks.Voice {env.Admin = adminapi.New(adminapi.NewExec(e.Named(env.Instance.Project(), env.Instance.Dir)), app.Version)}}diff --git a/fluxer_rexctl/cmd/install.go b/fluxer_rexctl/cmd/install.goindex 33b293ec4..90f56caae 100644--- a/fluxer_rexctl/cmd/install.go+++ b/fluxer_rexctl/cmd/install.go@@ -57,6 +57,7 @@ type installFlags struct {Project stringVoiceTCPPort intVoiceUDPPort int+ NoVoice boolSetup setupFlagsStorage storageFlags}@@ -106,12 +107,53 @@ func newInstallCmd(app *App) *cobra.Command {fl.StringVar(&f.Project, "project", defaultProject, "name for this instance's containers and volumes, for running more than one instance on a host")fl.IntVar(&f.VoiceTCPPort, "voice-tcp-port", 0, "TCP port for voice and video, for running more than one instance on a host")fl.IntVar(&f.VoiceUDPPort, "voice-udp-port", 0, "UDP port for voice and video, for running more than one instance on a host")+ fl.BoolVar(&f.NoVoice, "no-bundled-voice", false, "run without the bundled LiveKit and use only voice servers you add later")confirmFlag(c, &f.Confirm)f.Setup.bind(c)f.Storage.bind(c)return c}+func installTarget(app *App) (string, error) {+ l, err := app.locate()+ if err != nil {+ return "", err+ }+ dir := l.Dir+ switch {+ case l.Stale != "" && l.Chosen:+ dir = l.Stale+ app.located = &instance.Located{Dir: dir, From: instance.FromRecord, Scope: l.Scope}+ app.UI.Info("Installing Fluxer in %s, the folder you picked with rexctl use.", dir)+ case l.Stale != "":+ app.warnStale(l)+ }+ if dir, err = filepath.Abs(dir); err != nil {+ return "", err+ }+ inst, err := instance.Find(dir)+ if err != nil || !inst.State.IsVoice() {+ return dir, nil+ }+ refusal := ui.Refused(fmt.Sprintf("%s holds voice servers that joined %s, not a Fluxer instance.", dir, voiceInstances(inst)), "Pass --dir with another folder to install Fluxer there.")+ if l.From == instance.FromFlag || l.From == instance.FromWorkdir {+ return "", refusal+ }+ fallback, err := instance.DefaultDir()+ if err != nil {+ return "", err+ }+ if fallback, err = filepath.Abs(fallback); err != nil {+ return "", err+ }+ if other, err := instance.Find(fallback); err == nil && other.State.IsVoice() {+ return "", refusal+ }+ app.located = &instance.Located{Dir: fallback, From: instance.FromFallback}+ app.UI.Info("Installing Fluxer in %s. %s holds this machine's voice servers.", fallback, dir)+ return fallback, nil+}+func runInstall(ctx context.Context, app *App, c *cobra.Command, f *installFlags) error {if err := validateInstallFlags(f); err != nil {return err@@ -125,20 +167,7 @@ func runInstall(ctx context.Context, app *App, c *cobra.Command, f *installFlagsif names := f.Setup.passedNames(c); f.SetupWhere == "browser" && len(names) > 0 {return &ui.ExitError{Code: ui.ExitUsage, Msg: fmt.Sprintf("--setup browser doesn't take %s.", ui.JoinAnd(names)), Fix: "Pass --setup terminal instead, or leave out the setup options and enter them in the browser."}}- l, err := app.locate()- if err != nil {- return err- }- dir := l.Dir- switch {- case l.Stale != "" && l.Chosen:- dir = l.Stale- app.located = &instance.Located{Dir: dir, From: instance.FromRecord, Scope: l.Scope}- app.UI.Info("Installing Fluxer in %s, the folder you picked with rexctl use.", dir)- case l.Stale != "":- app.warnStale(l)- }- dir, err = filepath.Abs(dir)+ dir, err := installTarget(app)if err != nil {return err}@@ -306,6 +335,9 @@ func validateInstallFlags(f *installFlags) error {return ui.Usage("Pass a voice port from 1 to 65535.")}}+ if f.NoVoice && (f.VoiceTCPPort > 0 || f.VoiceUDPPort > 0) {+ return ui.Usage("Pass --voice-tcp-port and --voice-udp-port only with the bundled LiveKit, not with --no-bundled-voice.")+ }if !projectPattern.MatchString(f.Project) {return ui.Usage("Pass --project with lowercase letters, numbers, dashes or underscores.")}@@ -753,16 +785,25 @@ func (in *installer) settings() map[string]string {if in.f.VoiceUDPPort > 0 {out[stack.VoiceUDPPortKey] = strconv.Itoa(in.f.VoiceUDPPort)}+ if in.f.NoVoice {+ out[stack.VoiceBundledKey] = "false"+ }return out}func (in *installer) resolveVoicePorts(ctx context.Context) {+ if in.f.NoVoice {+ return+ }tcpSet := in.f.VoiceTCPPort > 0udpSet := in.f.VoiceUDPPort > 0if tcpSet && udpSet {return}pub := in.publishedPorts(ctx)+ for _, v := range instance.RecordedVoicePorts() {+ pub[host.Port{Number: v.Number, Proto: v.Proto}] = true+ }free := func(number int, proto string) bool {if number < 1 || number > 65535 {return false@@ -1097,6 +1138,9 @@ func (in *installer) write(ctx context.Context, inst *instance.Instance, deleteOif f.VoiceIP != "" {st.Settings[settings.VoicePublicIP] = f.VoiceIP}+ if f.NoVoice {+ st.Settings[settings.VoiceBundled] = "false"+ }if f.MemoryProfile != "" {st.Settings[settings.MemoryProfile] = f.MemoryProfile}diff --git a/fluxer_rexctl/cmd/lifecycle.go b/fluxer_rexctl/cmd/lifecycle.goindex 984aa1837..09cdf5a25 100644--- a/fluxer_rexctl/cmd/lifecycle.go+++ b/fluxer_rexctl/cmd/lifecycle.go@@ -81,6 +81,17 @@ func servicesForName(name string) ([]string, error) {}}+func runningPart(inst *instance.Instance, args, services []string) ([]string, error) {+ if len(services) == 0 || deploy.VoiceBundled(inst) {+ return services, nil+ }+ out := slices.DeleteFunc(slices.Clone(services), func(s string) bool { return s == "livekit" })+ if len(out) == 0 {+ return nil, ui.Refused(fmt.Sprintf("This instance runs without the bundled LiveKit, and %s has no logs here.", args[0]), "Run rexctl voice status to check your voice servers.")+ }+ return out, nil+}+func engineFailure(err error) error {return engineFailureFor("", err)}@@ -462,6 +473,9 @@ func newLogsCmd(app *App) *cobra.Command {if err != nil {return err}+ if services, err = runningPart(inst, args, services); err != nil {+ return err+ }opts := engine.LogsOptions{Follow: follow, Since: since, Stdout: app.UI.Stdout()}if since == "" {opts.Tail = 200@@ -526,6 +540,8 @@ func runStart(ctx context.Context, app *App) error {if err := waitForFluxer(ctx, inst, p, s, "Starting"); err != nil {return err}+ startLocalVoiceServers(ctx, app, inst, e)+ moveVoiceEndpoints(ctx, app, inst)s.Done("Started Fluxer at %s.", inst.Origin())return nil})@@ -559,10 +575,35 @@ func runBootStart(ctx context.Context, app *App) error {if err != nil || inst == nil {return err}- if inst.State.Stopped || inst.State.Ejected != nil || alreadyUp(ctx, app, inst) {+ if inst.State.Stopped || inst.State.Ejected != nil {return nil}- waitForRoute(ctx, bootRouteWait)+ if !bootStackUp(ctx, app, inst) {+ waitForRoute(ctx, bootRouteWait)+ if err := bootStack(ctx, app, inst); err != nil {+ return err+ }+ if err := reload(inst); err != nil {+ return err+ }+ if inst.State.Stopped || inst.State.Ejected != nil {+ return nil+ }+ }+ e, err := app.Engine(ctx, inst)+ if err != nil {+ return err+ }+ startVoiceAtBoot(ctx, app, inst, e)+ return nil+}++var (+ bootStackUp = alreadyUp+ startVoiceAtBoot = startLocalVoiceServers+)++func bootStack(ctx context.Context, app *App, inst *instance.Instance) error {return bootLocked(ctx, app, inst, func() error {if err := reload(inst); err != nil {return err@@ -691,6 +732,13 @@ func runStop(ctx context.Context, app *App) error {return err}err = app.UI.Step(ctx, "Stopping Fluxer", func(ctx context.Context, s *ui.Step) error {+ switch n := len(inst.State.Voice); {+ case n == 1:+ s.Info("Stopping the voice server %s too. Calls on it end.", inst.State.Voice[0].ID)+ case n > 1:+ s.Info("Stopping %d voice servers too. Calls on them end.", n)+ }+ stopLocalVoiceServers(ctx, app, inst, p.Engine)if err := p.Stop(ctx); err != nil {return ui.Wrap(engineFailure(err), "Couldn't stop Fluxer.", "Run rexctl status to see what's still running.")}@@ -755,6 +803,8 @@ func newRestartCmd(app *App) *cobra.Command {if err := waitForFluxer(ctx, inst, p, s, "Restarting"); err != nil {return err}+ startLocalVoiceServers(ctx, app, inst, e)+ moveVoiceEndpoints(ctx, app, inst)if inst.State.Stopped {inst.State.Stopped = falseif err := inst.Save(); err != nil {diff --git a/fluxer_rexctl/cmd/migrate_test.go b/fluxer_rexctl/cmd/migrate_test.goindex d71da2bb5..d61c8a32e 100644--- a/fluxer_rexctl/cmd/migrate_test.go+++ b/fluxer_rexctl/cmd/migrate_test.go@@ -7,6 +7,7 @@ import ("context""encoding/json""errors"+ "fmt""net/http""net/http/httptest""os"@@ -38,7 +39,7 @@ func oldInstance(t *testing.T, yes bool) (*App, *instance.Instance, *bytes.Buffeif err := inst.Save(); err != nil {t.Fatal(err)}- data := bytes.Replace(readFile(t, inst.StatePath()), []byte(`"schema": 3`), []byte(`"schema": 2`), 1)+ data := bytes.Replace(readFile(t, inst.StatePath()), fmt.Appendf(nil, `"schema": %d`, instance.StateSchema), []byte(`"schema": 2`), 1)if err := os.WriteFile(inst.StatePath(), data, 0o600); err != nil {t.Fatal(err)}@@ -236,3 +237,30 @@ func TestStatusAndDoctorSayAnEditedRootCaddyfileIsUnused(t *testing.T) {t.Fatalf("output:\n%s", out)}}++func TestStateWithVoiceServersRefusesARexctlThatWouldDropThem(t *testing.T) {+ inst, err := instance.Create(t.TempDir(), "fluxer")+ if err != nil {+ t.Fatal(err)+ }+ inst.State.Voice = []instance.VoiceServer{{ID: "fra-server-1"}}+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ saved, err := instance.Find(inst.Dir)+ if err != nil {+ t.Fatal(err)+ }+ if saved.State.Schema != instance.StateSchema || instance.StateSchema <= instance.LayoutSchema {+ t.Fatalf("saved schema %d, layout schema %d", saved.State.Schema, instance.LayoutSchema)+ }+ older := &release.Release{Version: "2026.1029.100000", StateSchema: instance.LayoutSchema}+ if err := release.CheckTarget(nil, older, saved.State.Schema); !errors.Is(err, release.ErrOlderSchema) {+ t.Fatalf("CheckTarget = %v, want ErrOlderSchema", err)+ }+ data := bytes.Replace(readFile(t, saved.StatePath()), fmt.Appendf(nil, `"schema": %d`, instance.StateSchema), fmt.Appendf(nil, `"schema": %d`, instance.LayoutSchema), 1)+ s, err := instance.ParseState(data, inst.Dir)+ if err != nil || s.Schema != instance.StateSchema || len(s.Voice) != 1 {+ t.Fatalf("state from the layout schema = %+v, %v", s, err)+ }+}diff --git a/fluxer_rexctl/cmd/restore.go b/fluxer_rexctl/cmd/restore.goindex 78718b166..f032bfc8a 100644--- a/fluxer_rexctl/cmd/restore.go+++ b/fluxer_rexctl/cmd/restore.go@@ -6,6 +6,7 @@ import ("context""errors""fmt"+ "os""runtime""slices""strings"@@ -20,6 +21,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/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice")const restoreReadyTimeout = 10 * time.Minute@@ -184,6 +186,10 @@ func stopOnOldEngine(ctx context.Context, app *App, inst *instance.Instance, oldfunc restoreInto(ctx context.Context, app *App, e *engine.Engine, path, dir string, m *backup.Manifest, existing *instance.Instance, old instance.EngineRef) error {var inst *instance.Instance+ var before []instance.VoiceServer+ if existing != nil && existing.State != nil {+ before = slices.Clone(existing.State.Voice)+ }err := app.UI.Step(ctx, "Checking the backup", func(ctx context.Context, s *ui.Step) error {var err errorinst, err = backup.Restore(ctx, path, dir, e, backup.RestoreOptions{@@ -191,6 +197,13 @@ func restoreInto(ctx context.Context, app *App, e *engine.Engine, path, dir striif t := restoreStageTitle(stage); t != "" {s.Title(t)}+ if stage == backup.StageClearing {+ for i := range before {+ if err := removeVoiceServerProject(ctx, e, &before[i]); err != nil {+ app.UI.Detail("stopping voice server %s: %v", before[i].ID, err)+ }+ }+ }},OnEvent: func(ev engine.Event) {if ev.Total > 0 {@@ -229,12 +242,67 @@ func restoreInto(ctx context.Context, app *App, e *engine.Engine, path, dir stri}return ui.Wrap(err, "Fluxer didn't start after the restore.", "Run rexctl doctor to find the problem.")}+ restoreVoiceServers(ctx, app, e, p, inst, before)if origin := inst.Origin(); origin != "" {app.UI.Highlight(origin)}return nil}+func droppedVoiceServers(before, after []instance.VoiceServer) []instance.VoiceServer {+ var out []instance.VoiceServer+ for _, b := range before {+ if !slices.ContainsFunc(after, func(a instance.VoiceServer) bool { return a.ID == b.ID && a.Project == b.Project }) {+ out = append(out, b)+ }+ }+ return out+}++func restoreVoiceServers(ctx context.Context, app *App, e *engine.Engine, p *engine.Project, inst *instance.Instance, before []instance.VoiceServer) {+ for _, gone := range droppedVoiceServers(before, inst.State.Voice) {+ closeVoiceFirewall(ctx, app, gone.Opened)+ _ = os.RemoveAll(voice.ServerDir(inst.Dir, gone.ID))+ app.UI.Info("Removed the voice server %s, which isn't in the backup.", gone.ID)+ }+ if len(inst.State.Voice) == 0 {+ return+ }+ err := app.UI.Step(ctx, "Starting the voice servers", func(ctx context.Context, s *ui.Step) error {+ started := 0+ for i := range inst.State.Voice {+ srv := &inst.State.Voice[i]+ err := writeVoiceServer(inst, srv)+ if err == nil {+ err = startVoiceServer(ctx, app, e, inst, srv, s)+ }+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Voice server %s didn't start (%v).", srv.ID, err), "Run rexctl voice status "+srv.ID+" to see why.")+ continue+ }+ started+++ if len(srv.Opened) > 0 {+ for _, o := range openVoiceFirewall(ctx, app, voicePortStrings(srv), false) {+ if !slices.Contains(srv.Opened, o) {+ srv.Opened = append(srv.Opened, o)+ }+ }+ }+ }+ if err := reloadCaddy(ctx, p, "edge", "/etc/caddy/Caddyfile"); err != nil {+ app.UI.Detail("reloading the edge: %v", err)+ }+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ s.Done("Started %s.", plural(started, "voice server", "voice servers"))+ return nil+ })+ if err != nil {+ app.UI.Warn(fmt.Sprintf("The voice servers didn't start (%v).", err), "Run rexctl voice status to see why.")+ }+}+func restoreBootUnit(ctx context.Context, app *App, inst *instance.Instance, e *engine.Engine, p *engine.Project, existing *instance.Instance, old instance.EngineRef) {if !host.BootUnitSupported() {return@@ -307,13 +375,19 @@ func restoreEngine(ctx context.Context, app *App, existing *instance.Instance, d}func installDockerForRestore(ctx context.Context, app *App, dir string) (*engine.Engine, error) {- manual := ui.Refused("Docker isn't installed or isn't running.", engine.Docker.InstallHint()+", then run rexctl restore again.")+ return installDockerHere(ctx, app, dir, "run rexctl restore again", func() (bool, error) {+ return app.UI.Confirm("Install Docker?", true)+ })+}++func installDockerHere(ctx context.Context, app *App, dir, again string, confirm func() (bool, error)) (*engine.Engine, error) {+ manual := ui.Refused("Docker isn't installed or isn't running.", engine.Docker.InstallHint()+", then "+again+".")info, err := host.Detect(ctx, dir)if err != nil || !host.CanInstallDocker(info) {return nil, manual}app.UI.Warn("Docker isn't installed.", "")- ok, err := app.UI.Confirm("Install Docker?", true)+ ok, err := confirm()if err != nil {return nil, err}@@ -328,14 +402,14 @@ func installDockerForRestore(ctx context.Context, app *App, dir string) (*enginereturn nil})if errors.Is(err, host.ErrRelogin) {- return nil, ui.Refused("Docker is installed, and your account can use it after you sign in again.", "Sign out and back in, then run rexctl restore again.")+ return nil, ui.Refused("Docker is installed, and your account can use it after you sign in again.", "Sign out and back in, then "+again+".")}if err != nil {- return nil, ui.Wrap(err, "Couldn't install Docker.", "Install Docker, then run rexctl restore again.")+ return nil, ui.Wrap(err, "Couldn't install Docker.", "Install Docker, then "+again+".")}e, err := engine.Connect(ctx, engine.ConnectOptions{Prefer: engine.Docker})if err != nil || e.Kind != engine.Docker {- return nil, ui.Wrap(err, "Docker was installed but isn't answering.", "Start Docker, then run rexctl restore again.")+ return nil, ui.Wrap(err, "Docker was installed but isn't answering.", "Start Docker, then "+again+".")}return e, nil}diff --git a/fluxer_rexctl/cmd/root.go b/fluxer_rexctl/cmd/root.goindex e28887439..a2bc90f75 100644--- a/fluxer_rexctl/cmd/root.go+++ b/fluxer_rexctl/cmd/root.go@@ -122,6 +122,7 @@ func newRoot(app *App) *cobra.Command {newUsersCmd(app),newAdminsCmd(app),newInvitesCmd(app),+ newVoiceCmd(app),newRegistrationCmd(app),newEmailCmd(app),newBrandingCmd(app),@@ -161,7 +162,7 @@ const (var commandGroups = map[string][]string{"run": {"install", "setup", "status", "doctor", "logs", "start", "stop", "restart", "upgrade", "auto-upgrade", "rollback", "backup", "restore", "proxy", "takeover", "eject", "use", "uninstall"},- "manage": {"users", "admins", "invites", "registration", "email", "branding", "search", "config"},+ "manage": {"users", "admins", "invites", "voice", "registration", "email", "branding", "search", "config"},"other": {"version", "docs"},}diff --git a/fluxer_rexctl/cmd/status.go b/fluxer_rexctl/cmd/status.goindex 7623e8b71..851a6099e 100644--- a/fluxer_rexctl/cmd/status.go+++ b/fluxer_rexctl/cmd/status.go@@ -59,6 +59,9 @@ func runStatus(ctx context.Context, app *App) error {if err != nil {return err}+ if inst.State.IsVoice() {+ return listVoiceMachine(ctx, app, inst)+ }out := statusJSON{URL: inst.Origin(),Domain: inst.Domain(),diff --git a/fluxer_rexctl/cmd/uninstall.go b/fluxer_rexctl/cmd/uninstall.goindex 0f7a6b06d..d85061a1e 100644--- a/fluxer_rexctl/cmd/uninstall.go+++ b/fluxer_rexctl/cmd/uninstall.go@@ -39,6 +39,9 @@ func runUninstall(ctx context.Context, app *App, keepData bool, confirm string)if err != nil {return err}+ if inst.State.IsVoice() {+ return runVoiceMachineUninstall(ctx, app, inst)+ }p, err := app.Named(ctx, inst)if err != nil {return err@@ -64,6 +67,7 @@ func runUninstall(ctx context.Context, app *App, keepData bool, confirm string)}}}+ removeLocalVoiceServersForUninstall(ctx, app, inst, p.Engine)if err := p.Down(context.WithoutCancel(ctx), !keepData); err != nil {return engineFailure(err)}diff --git a/fluxer_rexctl/cmd/upgrade.go b/fluxer_rexctl/cmd/upgrade.goindex cbd7dc56b..13692f86d 100644--- a/fluxer_rexctl/cmd/upgrade.go+++ b/fluxer_rexctl/cmd/upgrade.go@@ -126,6 +126,12 @@ func runUpgrade(ctx context.Context, app *App, f *upgradeFlags) error {if err != nil || inst == nil {return err}+ if inst.State.IsVoice() {+ if f.Prefetch || f.To != "" {+ return ui.Usage("A voice machine follows the release its instance runs. Run rexctl upgrade without --prefetch or --to.")+ }+ return runVoiceMachineUpgrade(ctx, app, inst, f.DryRun)+ }if err := managed(inst); err != nil {return err}@@ -173,7 +179,7 @@ func runUpgrade(ctx context.Context, app *App, f *upgradeFlags) error {app.UI.Info("Fluxer %s is available.", to)}needSelf := release.Compare(rel.Version, app.Version) != 0- app.keepLayout = needSelf && rel.StateSchema > 0 && rel.StateSchema < instance.StateSchema+ app.keepLayout = needSelf && rel.StateSchema > 0 && rel.StateSchema < instance.LayoutSchemaif f.DryRun {for _, k := range keptCopies(inst) {warnKeptCopy(app.UI, k.Name, k.Copy, true)@@ -1464,6 +1470,7 @@ func finishUpgrade(ctx context.Context, app *App, inst *instance.Instance, e *en_, toShort := versionPair(pend.From, pend.To)app.outcome = fmt.Sprintf("Upgraded to Fluxer %s.", toShort)app.UI.OK("Your instance is on Fluxer %s.", toShort)+ upgradeLocalVoiceServers(ctx, app, inst, e)return nil}diff --git a/fluxer_rexctl/cmd/voice.go b/fluxer_rexctl/cmd/voice.gonew file mode 100644index 000000000..f92929fcf--- /dev/null+++ b/fluxer_rexctl/cmd/voice.go@@ -0,0 +1,818 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "cmp"+ "context"+ "errors"+ "fmt"+ "net/http"+ "os"+ "path/filepath"+ "runtime"+ "slices"+ "strings"+ "time"++ "github.com/spf13/cobra"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++const defaultDrainTimeout = 15 * time.Minute++func newVoiceCmd(app *App) *cobra.Command {+ c := &cobra.Command{+ Use: "voice",+ Short: "Run extra voice servers here or on other machines",+ Long: "Run standalone LiveKit voice servers next to Fluxer with rexctl voice add, " ++ "or on other machines with rexctl voice token and rexctl voice join.\n\n" ++ "To run without the bundled voice server, run rexctl config set voice.bundled false, " ++ "or install with rexctl install --no-bundled-voice. Calls then go only to the servers you add. " ++ "rexctl config set voice.bundled true brings the bundled server back.",+ Args: noArgs,+ }+ c.AddCommand(+ newVoiceAddCmd(app),+ newVoiceJoinCmd(app),+ newVoiceTokenCmd(app),+ newVoiceListCmd(app),+ newVoiceStatusCmd(app),+ newVoiceTestCmd(app),+ newVoiceHostnameCmd(app),+ newVoiceRemoveCmd(app),+ )+ return c+}++func newVoiceTokenCmd(app *App) *cobra.Command {+ var region, newRegion, regionName, id, hostname, internal, name, expires string+ var soft int+ var lat, long float64+ c := &cobra.Command{+ Use: "token",+ Short: "Create a join token for a voice server on another machine",+ Long: "Create a join token and print the command that installs a voice server with it. The token works once.",+ Args: noArgs,+ }+ c.Flags().StringVar(®ion, "region", "", "existing region for the server")+ c.Flags().StringVar(&newRegion, "new-region", "", "create a region with this ID when the server joins")+ c.Flags().StringVar(®ionName, "region-name", "", "name of the new region")+ c.Flags().Float64Var(&lat, "lat", 0, "latitude of the new region")+ c.Flags().Float64Var(&long, "long", 0, "longitude of the new region")+ c.Flags().StringVar(&id, "id", "", "server ID")+ c.Flags().StringVar(&hostname, "hostname", "", "hostname the server must use")+ c.Flags().StringVar(&internal, "internal-endpoint", "", "URL the API uses to connect to the server, such as http://10.0.0.5:7880")+ c.Flags().StringVar(&name, "name", "", "a note for the server")+ c.Flags().IntVar(&soft, "soft-limit", 0, "connections above which placement prefers another server")+ c.Flags().StringVar(&expires, "expires", "1h", "how long the token works, at most 24h")+ c.RunE = func(c *cobra.Command, _ []string) error {+ ttl, err := parseExpiry(expires)+ if err != nil || ttl <= 0 || ttl < time.Minute || ttl > 24*time.Hour {+ return ui.Usage("Pass --expires from 1m to 24h.")+ }+ if newRegion != "" && (regionName == "" || region != "") {+ return ui.Usage("Pass --new-region with --region-name, and without --region.")+ }+ coords := c.Flags().Changed("lat")+ if coords != c.Flags().Changed("long") {+ return ui.Usage("Pass --lat and --long together.")+ }+ if coords && newRegion == "" {+ return ui.Usage("Pass --lat and --long only with --new-region.")+ }+ if lat < -90 || lat > 90 || long < -180 || long > 180 {+ return ui.Usage("Pass --lat from -90 to 90 and --long from -180 to 180.")+ }+ if err := checkServerID(id); err != nil {+ return err+ }+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, inst *instance.Instance) error {+ seconds := int(ttl / time.Second)+ req := adminapi.CreateVoiceJoinTokenRequest{ExpiresInSeconds: &seconds}+ set := func(v string) *string {+ if v == "" {+ return nil+ }+ return &v+ }+ req.RegionId, req.ServerId, req.Hostname, req.Notes = set(region), set(id), set(hostname), set(name)+ req.InternalEndpoint = set(internal)+ if newRegion != "" {+ emoji := "🌍"+ req.RegionId, req.RegionName, req.RegionEmoji = &newRegion, ®ionName, &emoji+ if coords {+ la, lo := float32(lat), float32(long)+ req.RegionLatitude, req.RegionLongitude = &la, &lo+ }+ }+ if soft > 0 {+ req.SoftConnectionLimit = &soft+ }+ created, err := api.CreateVoiceJoinToken(ctx, req)+ if err != nil {+ return err+ }+ app.UI.Info("Run this on the voice machine:")+ app.UI.Info("")+ app.UI.Highlight(" " + created.Command)+ app.UI.Info("")+ app.UI.Info("The token works once and expires %s.", expiryWhen(created.ExpiresAt))+ app.UI.Info("A voice server can record the calls placed on it. Give the token only to a machine you trust.")+ return nil+ })+ }++ list := &cobra.Command{Use: "list", Short: "List join tokens", Args: noArgs}+ app.jsonFlag(list)+ list.RunE = func(c *cobra.Command, _ []string) error {+ return adminRead(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {+ tokens, err := api.VoiceJoinTokens(ctx)+ if err != nil {+ return err+ }+ if app.Flags.JSON {+ return app.UI.JSON(tokens)+ }+ if len(tokens) == 0 {+ app.UI.Info("There are no join tokens from the last 7 days. Run rexctl voice token to make one.")+ return nil+ }+ rows := make([][]string, 0, len(tokens))+ for _, t := range tokens {+ region := "automatic"+ if t.RegionId != nil {+ region = *t.RegionId+ }+ server := ""+ if t.UsedServerId != nil {+ server = *t.UsedServerId+ } else if t.ServerId != nil {+ server = *t.ServerId+ }+ rows = append(rows, []string{t.TokenId, string(t.Status), region, server, expiryText(&t.ExpiresAt)})+ }+ app.UI.Table([]string{"ID", "Status", "Region", "Server", "Expires"}, rows)+ return nil+ })+ }++ revoke := &cobra.Command{Use: "revoke <token id>", Short: "Stop a join token from working", Args: exactArgs("the token ID")}+ revoke.RunE = func(c *cobra.Command, args []string) error {+ return adminWrite(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {+ if err := api.RevokeVoiceJoinToken(ctx, args[0]); err != nil {+ return err+ }+ app.UI.OK("The join token %s doesn't work anymore.", args[0])+ return nil+ })+ }+ c.AddCommand(list, revoke)+ return c+}++func newVoiceListCmd(app *App) *cobra.Command {+ c := &cobra.Command{Use: "list", Short: "List voice servers", Args: noArgs}+ app.jsonFlag(c)+ c.RunE = func(c *cobra.Command, _ []string) error {+ inst, err := app.Instance()+ if err != nil {+ return err+ }+ if inst.State.IsVoice() {+ return listVoiceMachine(c.Context(), app, inst)+ }+ return adminRead(c, app, func(ctx context.Context, api *adminapi.Client, inst *instance.Instance) error {+ regions, err := api.VoiceRegions(ctx)+ if err != nil {+ return err+ }+ if app.Flags.JSON {+ return app.UI.JSON(regions)+ }+ rows, servers := voiceListRows(regions, inst)+ if servers == 0 {+ app.UI.Info("Your instance has no voice servers.")+ }+ if len(rows) > 0 {+ app.UI.Table([]string{"ID", "Region", "Health", "Endpoint", "Runs on"}, rows)+ }+ return nil+ })+ }+ return c+}++func voiceListRows(regions []adminapi.VoiceRegion, inst *instance.Instance) ([][]string, int) {+ var rows [][]string+ servers := 0+ for _, r := range regions {+ if len(r.Servers) == 0 {+ rows = append(rows, []string{"-", r.Id, "no servers", "", ""})+ }+ for _, s := range r.Servers {+ servers+++ here := ""+ if inst.State.VoiceServer(s.ServerId) != nil {+ here = "this machine"+ }+ state := string(s.Health.Status)+ if !s.IsActive {+ state = "off"+ } else if s.Draining {+ state += ", draining"+ }+ if s.DuplicateApiKey {+ state += ", shared key"+ }+ rows = append(rows, []string{s.ServerId, r.Id, state, s.Endpoint, here})+ }+ }+ return rows, servers+}++func listVoiceMachine(ctx context.Context, app *App, inst *instance.Instance) error {+ client := &voice.Client{HTTP: &http.Client{Timeout: 10 * time.Second}}+ type row struct {+ ID string `json:"id"`+ Instance string `json:"instance"`+ Region string `json:"region"`+ Endpoint string `json:"endpoint"`+ Health string `json:"health"`+ }+ var out []row+ for _, s := range inst.State.Voice {+ health := "unknown"+ if node, err := client.GetNode(ctx, s.NodeURL, s.Key, s.Secret); err == nil {+ health = node.Health.Status+ if node.Draining {+ health += ", draining"+ }+ if !node.IsActive {+ health = "off"+ }+ } else if code := voice.CodeOf(err); code != "" {+ health = strings.ToLower(code)+ }+ out = append(out, row{s.ID, s.Instance, s.Region, s.Endpoint, health})+ }+ if app.Flags.JSON {+ return app.UI.JSON(out)+ }+ if len(out) == 0 {+ app.UI.Info("This machine runs no voice servers. Run rexctl voice join to add one.")+ return nil+ }+ rows := make([][]string, len(out))+ for i, r := range out {+ rows[i] = []string{r.ID, r.Instance, r.Region, r.Health, r.Endpoint}+ }+ app.UI.Table([]string{"ID", "Instance", "Region", "Health", "Endpoint"}, rows)+ return nil+}++func localVoiceServers(inst *instance.Instance, args []string) (*instance.Instance, []*instance.VoiceServer, error) {+ if len(args) == 0 {+ out := make([]*instance.VoiceServer, 0, len(inst.State.Voice))+ for i := range inst.State.Voice {+ out = append(out, &inst.State.Voice[i])+ }+ return inst, out, nil+ }+ owner, srv := findVoiceServer(inst, args[0])+ if srv == nil {+ return inst, nil, ui.Refused(fmt.Sprintf("This machine doesn't run a voice server called %s.", args[0]), "Run rexctl voice list to see the servers.")+ }+ return owner, []*instance.VoiceServer{srv}, nil+}++func findVoiceServer(inst *instance.Instance, id string) (*instance.Instance, *instance.VoiceServer) {+ if srv := inst.State.VoiceServer(id); srv != nil {+ return inst, srv+ }+ for _, dir := range instance.ReadRecord(instance.OwnScope()).Instances {+ if dir == inst.Dir {+ continue+ }+ other, err := instance.Find(dir)+ if err != nil || !other.State.IsVoice() {+ continue+ }+ if srv := other.State.VoiceServer(id); srv != nil {+ return other, srv+ }+ }+ return inst, nil+}++func newVoiceStatusCmd(app *App) *cobra.Command {+ return &cobra.Command{+ Use: "status [id]",+ Short: "Show the voice servers on this machine",+ Args: maxArgs(1),+ RunE: func(c *cobra.Command, args []string) error {+ ctx := c.Context()+ inst, err := app.Instance()+ if err != nil {+ return err+ }+ inst, servers, err := localVoiceServers(inst, args)+ if err != nil {+ return err+ }+ if len(servers) == 0 {+ app.UI.Info("This machine runs no voice servers.")+ return nil+ }+ e, err := app.Engine(ctx, inst)+ if err != nil {+ return err+ }+ for _, srv := range servers {+ showVoiceStatus(ctx, app, e, inst, srv)+ }+ return nil+ },+ }+}++func showVoiceStatus(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer) {+ u := app.UI+ container := "not running"+ if cs, err := voiceProject(e, inst, srv).PS(ctx); err == nil && len(cs) > 0 {+ container = cs[0].State+ if cs[0].Health != "" {+ container += ", " + cs[0].Health+ }+ }+ base, dial := voiceProbeTarget(inst, srv)+ reach := "answers"+ if err := voice.ProbeThroughEdge(ctx, base, dial, true); err != nil {+ reach = err.Error()+ }+ cert := "-"+ if srv.Edge == voice.EdgeOwn {+ if exp, err := voice.CertificateExpiry(ctx, srv.Endpoint, dial); err != nil {+ cert = "not valid (" + err.Error() + ")"+ } else {+ cert = "valid until " + exp.Local().Format("2 January 15:04")+ }+ }+ load := "unknown"+ if l, err := voice.ListRooms(ctx, base, dial, srv.Key, srv.Secret, true); err == nil {+ load = fmt.Sprintf("%s, %s", plural(l.Rooms, "room", "rooms"), plural(l.Participants, "participant", "participants"))+ }+ u.Info("Voice server %s", srv.ID)+ rows := [][]string{+ {"Region", srv.Region},+ {"Endpoint", srv.Endpoint + ", " + reach},+ {"Container", container},+ {"Ports", fmt.Sprintf("%d/tcp, %d/udp", srv.TCPPort, srv.UDPPort)},+ {"Certificate", cert},+ {"Calls", load},+ {"LiveKit", srv.Image},+ {"Webhook", srv.Webhook + ", " + webhookState(ctx, e, inst, srv)},+ }+ if srv.Instance != "" {+ rows = append(rows, []string{"Instance", srv.Instance})+ }+ if srv.Joined && srv.Instance != "" {+ rows = append(rows, []string{"Browsers", browsersText(ctx, srv)})+ }+ for _, r := range rows {+ u.Info(" %-15s %s", r[0], r[1])+ }+ u.Info("")+}++func browsersText(ctx context.Context, srv *instance.VoiceServer) string {+ client := &voice.Client{HTTP: &http.Client{Timeout: 10 * time.Second}}+ switch voiceOriginState(ctx, client, srv.Instance, endpointOrigin(srv.Endpoint)) {+ case originAllowed:+ return "can connect from " + srv.Instance+ case originUnlisted:+ return srv.Instance + " runs a release without the list of voice servers"+ case originPending:+ return "can't connect from " + srv.Instance + " yet"+ }+ return "unknown, rexctl can't read " + srv.Instance+}++func webhookState(ctx context.Context, e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer) string {+ failed, sent := voiceWebhookLog(ctx, e, inst, srv)+ switch {+ case failed != "":+ return "failing (" + failed + ")"+ case sent:+ return "delivered"+ }+ return "pending until a call runs"+}++func newVoiceTestCmd(app *App) *cobra.Command {+ return &cobra.Command{+ Use: "test [id]",+ Short: "Test that calls can connect to a voice server over UDP and TCP",+ Long: "Join a throwaway room on a voice server and report which transports connect. " ++ "On the Fluxer machine it can test any server of the instance, including servers on other machines.",+ Args: maxArgs(1),+ RunE: func(c *cobra.Command, args []string) error {+ ctx := c.Context()+ inst, err := app.Instance()+ if err != nil {+ return err+ }+ owner, servers, err := localVoiceServers(inst, args)+ if err != nil && len(args) == 1 && !inst.State.IsVoice() {+ return testRemoteVoiceServer(c, app, args[0])+ }+ if err != nil {+ return err+ }+ if len(servers) == 0 {+ return ui.Refused("This machine runs no voice servers.", "Pass the ID of a server on another machine.")+ }+ ok := true+ for _, srv := range servers {+ app.UI.Info("Testing %s at %s", srv.ID, srv.Endpoint)+ results := runTransportTests(ctx, srv.Endpoint, func() (string, error) {+ return voice.TestToken(srv.Key, srv.Secret, voice.TestRoom(), time.Now()), nil+ }, !owner.State.IsVoice())+ ok = reportTransports(app, results, true) && ok+ }+ if !inst.State.IsVoice() {+ app.UI.Info("Servers on other machines can be tested from here with rexctl voice test <id>.")+ }+ if !ok {+ app.SetExit(ui.ExitFailed)+ }+ return nil+ },+ }+}++func testRemoteVoiceServer(c *cobra.Command, app *App, id string) error {+ return adminRead(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {+ regions, err := api.VoiceRegions(ctx)+ if err != nil {+ return err+ }+ region := ""+ for _, r := range regions {+ for _, s := range r.Servers {+ if s.ServerId == id {+ region = r.Id+ }+ }+ }+ if region == "" {+ return ui.Refused(fmt.Sprintf("Your instance has no voice server called %s.", id), "Run rexctl voice list to see the servers.")+ }+ var endpoint string+ var results []transportResult+ for _, t := range []voice.Transport{voice.TransportUDP, voice.TransportTCP} {+ tok, err := api.VoiceTestToken(ctx, region, id)+ if err == nil {+ endpoint = tok.Endpoint+ err = voice.TestTransport(ctx, tok.Endpoint, tok.Token, t, false, 20*time.Second)+ }+ results = append(results, transportResult{Transport: t, Err: err})+ }+ app.UI.Info("Tested %s at %s from this machine.", id, endpoint)+ if !reportTransports(app, results, false) {+ if slices.ContainsFunc(results, func(r transportResult) bool { return errors.Is(r.Err, voice.ErrTokenRejected) }) {+ app.UI.Info("%s rejected a token signed with the API secret your instance has for it. Put the server's own secret back in the admin panel. It's FLUXER_VOICE_API_SECRET in voice/%s/.env in the rexctl folder on the machine that runs it.", id, id)+ } else {+ app.UI.Info("Open the server's TCP and UDP ports in the firewall of its provider and on the machine itself.")+ }+ app.SetExit(ui.ExitFailed)+ }+ return nil+ })+}++func newVoiceHostnameCmd(app *App) *cobra.Command {+ return &cobra.Command{+ Use: "hostname <id> <host>",+ Short: "Move a voice server to a new hostname",+ Long: "Move a voice server on this machine to a hostname once its DNS record points here. rexctl gets a certificate and updates the server's endpoint on the instance.",+ Args: exactArgs("the server ID", "the hostname"),+ RunE: func(c *cobra.Command, args []string) error {+ ctx := c.Context()+ inst, err := app.Instance()+ if err != nil {+ return err+ }+ if err := managed(inst); err != nil {+ return err+ }+ inst, srv := findVoiceServer(inst, args[0])+ if srv != nil && srv.Edge == voice.EdgeInstance {+ return ui.Refused(fmt.Sprintf("%s uses the domain of the Fluxer instance on this machine.", srv.ID), "rexctl voice hostname applies only to a server with its own edge.")+ }+ if srv == nil || srv.Edge != voice.EdgeOwn {+ return ui.Refused(fmt.Sprintf("%s isn't a voice server with its own edge on this machine.", args[0]), "Run rexctl voice list to see the servers.")+ }+ name := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(args[1]), "."))+ check := func(h string) voice.HostnameCheck {+ answer, err := voice.PublicLookup(&http.Client{Timeout: 5 * time.Second})(ctx, h)+ if err != nil {+ return voice.HostnameMissing+ }+ return voice.CheckHostname(answer, srv.NodeIP, voice.InterfaceAddresses())+ }+ if !waitForHostname(ctx, app, name, srv.NodeIP, check, false, true) {+ return ui.Refused(fmt.Sprintf("%s doesn't point at this machine yet.", name), "Fix the DNS record, then run the same command again.")+ }+ e, err := app.Engine(ctx, inst)+ if err != nil {+ return err+ }+ return app.Locked(ctx, inst, func() error {+ old := srv.Endpoint+ srv.Endpoint, srv.Hostname = voice.Endpoint(name, srv.ID), name+ if err := routeVoiceServer(ctx, app, e, inst, srv); err != nil {+ return err+ }+ if err := waitForEndpoint(ctx, voice.HTTPBase(srv.Endpoint), "127.0.0.1:443", false, 3*time.Minute); err != nil {+ srv.Endpoint = old+ _ = routeVoiceServer(ctx, app, e, inst, srv)+ return ui.Wrap(err, fmt.Sprintf("%s didn't get a certificate.", name), "Check that ports 80 and 443 are open, then try again.")+ }+ client := &voice.Client{HTTP: &http.Client{Timeout: 30 * time.Second}}+ if _, err := client.UpdateNode(ctx, srv.NodeURL, srv.Key, srv.Secret, voice.NodeUpdate{Endpoint: &srv.Endpoint}); err != nil {+ srv.Endpoint = old+ _ = routeVoiceServer(ctx, app, e, inst, srv)+ return joinError(err, srv.Instance, srv.ID)+ }+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ app.UI.OK("%s now uses %s.", srv.ID, srv.Endpoint)+ waitForVoiceOrigin(ctx, app, client, srv.Instance, srv.Endpoint)+ return nil+ })+ },+ }+}++func newVoiceRemoveCmd(app *App) *cobra.Command {+ var all, now bool+ var drain time.Duration+ c := &cobra.Command{+ Use: "remove [id]",+ Short: "Drain, unregister and delete a voice server",+ Args: maxArgs(1),+ RunE: func(c *cobra.Command, args []string) error {+ if all == (len(args) == 1) {+ return ui.Usage("Pass the ID of the server, or --all.")+ }+ inst, err := app.Instance()+ if err != nil {+ return err+ }+ if all && !inst.State.IsVoice() {+ return ui.Refused("--all works on a voice machine.", "Pass the ID of the server to remove.")+ }+ owner, servers, err := localVoiceServers(inst, args)+ if err != nil && len(args) == 1 && !inst.State.IsVoice() && instanceHasVoiceServer(c, app, args[0]) {+ return ui.Refused(fmt.Sprintf("%s runs on another machine.", args[0]), fmt.Sprintf("Run rexctl voice remove %s on the machine that runs it, or delete it in the admin panel.", args[0]))+ }+ if err != nil {+ return err+ }+ inst = owner+ if !now && drain <= 0 {+ drain = defaultDrainTimeout+ }+ if now {+ drain = 0+ }+ return removeVoiceServers(c.Context(), app, inst, servers, drain, all)+ },+ }+ c.Flags().BoolVar(&all, "all", false, "remove every voice server and the edge on this machine")+ c.Flags().BoolVar(&now, "now", false, "don't wait for calls to end")+ c.Flags().DurationVar(&drain, "drain-timeout", defaultDrainTimeout, "how long to wait for calls to end")+ return c+}++func instanceHasVoiceServer(c *cobra.Command, app *App, id string) bool {+ found := false+ _ = adminRead(c, app, func(ctx context.Context, api *adminapi.Client, _ *instance.Instance) error {+ regions, err := api.VoiceRegions(ctx)+ if err != nil {+ return err+ }+ for _, r := range regions {+ found = found || slices.ContainsFunc(r.Servers, func(s adminapi.VoiceServerAdminResponse) bool { return s.ServerId == id })+ }+ return nil+ })+ return found+}++func removeVoiceServers(ctx context.Context, app *App, inst *instance.Instance, servers []*instance.VoiceServer, drain time.Duration, all bool) error {+ if err := managed(inst); err != nil {+ return err+ }+ ids := make([]string, len(servers))+ for i, s := range servers {+ ids[i] = s.ID+ }+ if err := confirmVoice(app, "Remove "+ui.JoinAnd(ids)+"?"); err != nil {+ return err+ }+ e, err := app.Engine(ctx, inst)+ if err != nil {+ return err+ }+ var api *adminapi.Client+ if !inst.State.IsVoice() {+ p, err := app.Named(ctx, inst)+ if err != nil {+ return err+ }+ api = app.adminClient(inst, p)+ }+ return app.Locked(ctx, inst, func() error {+ for _, id := range ids {+ srv := inst.State.VoiceServer(id)+ if srv == nil {+ continue+ }+ if err := removeOneVoiceServer(ctx, app, e, api, inst, srv, drain); err != nil {+ return err+ }+ }+ if inst.State.IsVoice() && len(inst.State.Voice) == 0 {+ return removeVoiceMachine(ctx, app, e, inst)+ }+ return nil+ })+}++func setVoiceDraining(ctx context.Context, api *adminapi.Client, inst *instance.Instance, srv *instance.VoiceServer, draining bool) error {+ if inst.State.IsVoice() {+ client := &voice.Client{HTTP: &http.Client{Timeout: 30 * time.Second}}+ _, err := client.UpdateNode(ctx, srv.NodeURL, srv.Key, srv.Secret, voice.NodeUpdate{Draining: &draining})+ return err+ }+ _, err := api.UpdateVoiceServer(ctx, srv.Region, srv.ID, adminapi.UpdateVoiceServerRequestBody{Draining: &draining})+ return err+}++func waitForDrain(ctx context.Context, app *App, inst *instance.Instance, srv *instance.VoiceServer, limit time.Duration) {+ if limit <= 0 {+ return+ }+ base, dial := voiceProbeTarget(inst, srv)+ _ = app.UI.Step(ctx, "Waiting for calls on "+srv.ID+" to end", func(ctx context.Context, s *ui.Step) error {+ deadline := time.Now().Add(limit)+ for {+ load, err := voice.ListRooms(ctx, base, dial, srv.Key, srv.Secret, true)+ if err == nil && load.Participants == 0 {+ s.Done("No calls left on %s.", srv.ID)+ return nil+ }+ if time.Now().After(deadline) {+ if err != nil {+ s.Done("rexctl couldn't read the calls on %s (%v), and the wait is over.", srv.ID, err)+ } else {+ s.Done("The wait is over with %s still on %s.", plural(load.Participants, "participant", "participants"), srv.ID)+ }+ return nil+ }+ if err != nil {+ s.Title(fmt.Sprintf("Waiting for calls on %s to end. rexctl can't read them right now", srv.ID))+ } else {+ s.Title(fmt.Sprintf("Waiting for %s on %s to leave", plural(load.Participants, "participant", "participants"), srv.ID))+ }+ select {+ case <-ctx.Done():+ return ctx.Err()+ case <-time.After(5 * time.Second):+ }+ }+ })+}++func unregisterVoiceServer(ctx context.Context, app *App, api *adminapi.Client, inst *instance.Instance, srv *instance.VoiceServer, drain time.Duration) error {+ if inst.State.IsVoice() && srv.NodeURL == "" {+ app.UI.Info("%s never joined %s. rexctl removes it here.", srv.ID, cmp.Or(srv.Instance, "the instance"))+ return nil+ }+ gone := false+ if err := setVoiceDraining(ctx, api, inst, srv, true); err != nil {+ switch {+ case voice.CodeOf(err) == voice.CodeNodeUnknown, adminapi.IsStatus(err, http.StatusNotFound):+ gone = true+ app.UI.Info("%s is already gone from the instance. rexctl removes it here.", srv.ID)+ case voice.CodeOf(err) == voice.CodeTokenExpired:+ return ui.Refused(voice.ClockProblem(err, srv.Instance, time.Now())+".", voice.ClockFix)+ case voice.CodeOf(err) == voice.CodeSignatureInvalid:+ return ui.Refused(fmt.Sprintf("The instance has another secret for %s.", srv.ID), fmt.Sprintf("Put FLUXER_VOICE_API_SECRET from %s back in the admin panel, or delete the server there and run this again.", filepath.Join(voice.ServerDir(inst.Dir, srv.ID), voice.EnvFile)))+ default:+ app.UI.Warn(fmt.Sprintf("Couldn't mark %s as draining (%v).", srv.ID, err), "")+ }+ }+ if !gone {+ waitForDrain(ctx, app, inst, srv, drain)+ var err error+ if inst.State.IsVoice() {+ client := &voice.Client{HTTP: &http.Client{Timeout: 30 * time.Second}}+ err = client.DeleteNode(ctx, srv.NodeURL, srv.Key, srv.Secret)+ if voice.CodeOf(err) == voice.CodeNodeUnknown {+ err = nil+ }+ } else {+ err = api.DeleteVoiceServer(ctx, srv.Region, srv.ID)+ if adminapi.IsStatus(err, http.StatusNotFound) {+ err = nil+ }+ }+ if err != nil {+ return ui.Wrap(err, fmt.Sprintf("Couldn't remove %s from the instance.", srv.ID), "Run the same command again.")+ }+ }+ return nil+}++func removeOneVoiceServer(ctx context.Context, app *App, e *engine.Engine, api *adminapi.Client, inst *instance.Instance, srv *instance.VoiceServer, drain time.Duration) error {+ if err := unregisterVoiceServer(ctx, app, api, inst, srv, drain); err != nil {+ return err+ }+ err := app.UI.Step(ctx, "Removing voice server "+srv.ID, func(ctx context.Context, s *ui.Step) error {+ if err := removeVoiceServerProject(ctx, e, srv); err != nil {+ return err+ }+ _ = os.RemoveAll(voice.ServerDir(inst.Dir, srv.ID))+ removed := *srv+ inst.State.RemoveVoiceServer(srv.ID)+ if !inst.State.IsVoice() {+ _ = os.Remove(filepath.Join(inst.Path(instance.CaddyDropInDir), voice.DropInName(removed.ID)))+ if err := reloadCaddy(ctx, e.Named(inst.Project(), inst.Dir), "edge", "/etc/caddy/Caddyfile"); err != nil {+ app.UI.Detail("reloading the edge: %v", err)+ }+ } else {+ unrouteVoiceServer(ctx, app, e, inst, &removed)+ }+ closeVoiceFirewall(ctx, app, removed.Opened)+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ s.Done("Removed voice server %s.", removed.ID)+ return nil+ })+ return err+}++func removeVoiceMachine(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance) error {+ done := "Removed every voice server in " + inst.Dir + "."+ if edge := inst.State.VoiceEdge; edge != nil && edge.Instance == "" {+ done = "Removed every voice server and the edge from this machine."+ if err := engineFailure(e.Named(voice.EdgeProject, inst.Path(voice.EdgeDir)).Down(context.WithoutCancel(ctx), true)); err != nil {+ return ui.Wrap(err, "Couldn't stop the voice edge.", "Run the same command again.")+ }+ closeVoiceFirewall(ctx, app, edge.Opened)+ }+ if _, err := removeAutoUpgradeFor(ctx, inst, e); err != nil {+ app.UI.Detail("removing automatic upgrades: %v", err)+ }+ app.forget(inst.Dir)+ removeVoiceSysctl(app)+ if err := os.RemoveAll(inst.Dir); err != nil {+ return ui.Wrap(err, "Couldn't delete "+inst.Dir+".", "Delete it yourself.")+ }+ app.UI.OK("%s", done)+ return nil+}++var removeAutoUpgradeFor = removeAutoUpgrade++func removeVoiceSysctl(app *App) {+ if runtime.GOOS != "linux" || euid() != 0 {+ return+ }+ for _, dir := range instance.ReadRecord(instance.OwnScope()).Instances {+ if other, err := instance.Find(dir); err == nil && other.State.IsVoice() && len(other.State.Voice) > 0 {+ return+ }+ }+ if err := os.Remove(voiceSysctlFile); err != nil && !errors.Is(err, os.ErrNotExist) {+ app.UI.Detail("removing %s: %v", voiceSysctlFile, err)+ }+}++func voiceCommandAllowed(path string) bool {+ parts := strings.Fields(path)+ if len(parts) < 2 {+ return true+ }+ return slices.Contains([]string{"voice", "uninstall", "upgrade", "auto-upgrade", "doctor", "status", "version", "docs", "use", "help", "completion"}, parts[1])+}diff --git a/fluxer_rexctl/cmd/voice_add.go b/fluxer_rexctl/cmd/voice_add.gonew file mode 100644index 000000000..e9c2013c1--- /dev/null+++ b/fluxer_rexctl/cmd/voice_add.go@@ -0,0 +1,489 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "context"+ "fmt"+ "net/netip"+ "os"+ "path/filepath"+ "slices"+ "strings"+ "time"++ "github.com/spf13/cobra"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"+ "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/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++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+}++func addVoiceOverrideFlags(c *cobra.Command, f *voiceFlags) {+ fl := c.Flags()+ fl.StringVar(&f.ID, "id", "", "server ID, such as fra-server-2")+ fl.StringVar(&f.Region, "region", "", "existing voice region for the server")+ fl.Float64Var(&f.Lat, "lat", 0, "latitude of the server")+ fl.Float64Var(&f.Long, "long", 0, "longitude of the server")+ fl.StringVar(&f.IP, "ip", "", "public IPv4 address LiveKit gives to clients, also the address the ports bind to when it's on this machine")+ fl.IntVar(&f.TCPPort, "tcp-port", 0, "TCP port for media")+ fl.IntVar(&f.UDPPort, "udp-port", 0, "UDP port for media")+ fl.IntVar(&f.SoftLimit, "soft-limit", 0, "connections above which placement prefers another server")+ fl.StringVar(&f.Memory, "memory", "", "memory limit for the server, such as 2g")+ fl.StringVar(&f.Image, "image", "", "LiveKit image to run")+ fl.BoolVar(&f.NoFirewall, "no-firewall", false, "leave the firewall on this machine alone")+ fl.BoolVar(&f.DryRun, "dry-run", false, "show what would happen and change nothing")+}++func validateVoiceFlags(c *cobra.Command, f *voiceFlags) error {+ if err := checkServerID(f.ID); err != nil {+ return err+ }+ if f.NewRegion != "" && !voice.ValidID(f.NewRegion) {+ return ui.Usage(fmt.Sprintf("%s isn't a region ID. Use lowercase letters, digits and dashes.", f.NewRegion))+ }+ if f.IP != "" {+ ip, err := netip.ParseAddr(f.IP)+ if err != nil || !ip.Is4() {+ return ui.Usage(fmt.Sprintf("Pass --ip as an IPv4 address. %s isn't one.", f.IP))+ }+ }+ for _, p := range []int{f.TCPPort, f.UDPPort, f.HTTPPort} {+ if p < 0 || p > 65535 {+ return ui.Usage("Pass a port from 1 to 65535.")+ }+ }+ if c.Flags().Changed("lat") != c.Flags().Changed("long") {+ return ui.Usage("Pass --lat and --long together.")+ }+ f.HasCoords = c.Flags().Changed("lat")+ if f.Lat < -90 || f.Lat > 90 || f.Long < -180 || f.Long > 180 {+ return ui.Usage("Pass --lat from -90 to 90 and --long from -180 to 180.")+ }+ if f.Memory != "" {+ if _, ok := voice.ParseBytes(f.Memory); !ok {+ return ui.Usage(fmt.Sprintf("Pass --memory as a size such as 2g or 1536m. %s isn't one.", f.Memory))+ }+ }+ return nil+}++func checkServerID(id string) error {+ switch {+ case id == "":+ return nil+ case id == voice.EdgeID:+ return ui.Usage("The voice edge uses the ID edge. Pick another server ID.")+ case !voice.ValidID(id):+ return ui.Usage(fmt.Sprintf("%s isn't a server ID. Use lowercase letters, digits and dashes.", id))+ }+ return nil+}++func newVoiceAddCmd(app *App) *cobra.Command {+ f := &voiceFlags{}+ c := &cobra.Command{+ Use: "add",+ Short: "Add voice servers on this Fluxer machine",+ Long: "Add standalone LiveKit voice servers next to your Fluxer instance on this machine. " ++ "Each one gets its own ID, ports, keys and path on your domain, and rexctl registers it with your instance.",+ Args: noArgs,+ RunE: func(c *cobra.Command, _ []string) error {+ if err := validateVoiceFlags(c, f); err != nil {+ return err+ }+ if f.Count < 1 || f.Count > 16 {+ return ui.Usage("Pass --count from 1 to 16.")+ }+ if f.Count > 1 && (f.ID != "" || f.TCPPort > 0 || f.UDPPort > 0) {+ return ui.Usage("Pass --id, --tcp-port and --udp-port only when you add one server.")+ }+ if f.NewRegion != "" && f.RegionName == "" {+ return ui.Usage("Pass --region-name with --new-region.")+ }+ if f.NewRegion != "" && !f.HasCoords {+ return ui.Usage("Pass --lat and --long with --new-region. Placement sends people to the nearest region.")+ }+ return runVoiceAdd(c.Context(), app, c, f)+ },+ }+ addVoiceOverrideFlags(c, f)+ c.Flags().IntVar(&f.Count, "count", 1, "how many servers to add")+ c.Flags().StringVar(&f.NewRegion, "new-region", "", "create a voice region with this ID for the servers")+ c.Flags().StringVar(&f.RegionName, "region-name", "", "name of the new region")+ c.Flags().BoolVar(&f.Force, "force", false, "add servers even when Fluxer runs through a tunnel")+ return c+}++type plannedVoice struct {+ srv instance.VoiceServer+ publicIP string+ softLimit int+}++func runVoiceAdd(ctx context.Context, app *App, c *cobra.Command, f *voiceFlags) error {+ inst, err := app.Instance()+ if err != nil {+ return err+ }+ if inst.State.IsVoice() {+ return ui.Refused("This folder holds voice servers that joined another instance.", "Run rexctl voice join with a new token to add one more here.")+ }+ if err := managed(inst); err != nil {+ return err+ }+ named, err := app.Named(ctx, inst)+ if err != nil {+ return err+ }+ api := app.adminClient(inst, named)+ switch inst.State.Settings[settings.EdgeMode] {+ case settings.EdgeTunnel:+ if !f.Force {+ return ui.Refused("Fluxer runs through a tunnel here. Signalling works through a tunnel and call audio doesn't.", "Pass --force to add the server anyway, then open its ports to the internet.")+ }+ case settings.EdgeProxy:+ app.UI.Info("Your proxy must pass WebSocket upgrades on the new paths, as it does for /livekit/.")+ }+ return app.Locked(ctx, inst, func() error {+ e, err := app.Engine(ctx, inst)+ if err != nil {+ return err+ }+ regions, err := api.VoiceRegions(ctx)+ if err != nil {+ return adminapi.Explain(err)+ }+ region, err := pickRegion(regions, f)+ if err != nil {+ return err+ }+ plans, err := planVoiceAdd(ctx, app, e, inst, regions, region, f)+ if err != nil {+ return err+ }+ showVoiceAddSummary(app, inst, region, plans, f)+ if f.DryRun {+ return nil+ }+ if err := confirmVoice(app, "Add "+plural(len(plans), "voice server", "voice servers")+"?"); err != nil {+ return err+ }+ if f.NewRegion != "" {+ lat, long := float32(f.Lat), float32(f.Long)+ err := api.CreateVoiceRegion(ctx, adminapi.CreateVoiceRegionRequest{Id: f.NewRegion, Name: f.RegionName, Emoji: "🌍", Latitude: lat, Longitude: long})+ if err != nil {+ return adminapi.Explain(err)+ }+ }+ for i := range plans {+ if err := addOneVoiceServer(ctx, app, api, e, named, inst, &plans[i], f); err != nil {+ return err+ }+ }+ app.UI.Info("Run rexctl voice list to see every server, and rexctl voice remove <id> to take one away.")+ return nil+ })+}++func pickRegion(regions []adminapi.VoiceRegion, f *voiceFlags) (string, error) {+ if f.NewRegion != "" {+ if slices.ContainsFunc(regions, func(r adminapi.VoiceRegion) bool { return r.Id == f.NewRegion }) {+ return "", ui.Refused(fmt.Sprintf("The voice region %s already exists.", f.NewRegion), "Pass --region "+f.NewRegion+" to use it.")+ }+ return f.NewRegion, nil+ }+ if f.Region != "" {+ if !slices.ContainsFunc(regions, func(r adminapi.VoiceRegion) bool { return r.Id == f.Region }) {+ return "", ui.Refused(fmt.Sprintf("There's no voice region called %s.", f.Region), "Run rexctl voice list to see the regions, or pass --new-region.")+ }+ return f.Region, nil+ }+ for _, r := range regions {+ if r.IsDefault {+ return r.Id, nil+ }+ }+ if len(regions) > 0 {+ return regions[0].Id, nil+ }+ return "", ui.Refused("Your instance has no voice regions.", "Pass --new-region, --region-name, --lat and --long to create one.")+}++func takenVoiceIDs(regions []adminapi.VoiceRegion, inst *instance.Instance) []string {+ var out []string+ for _, r := range regions {+ for _, s := range r.Servers {+ out = append(out, s.ServerId)+ }+ }+ for _, s := range inst.State.Voice {+ out = append(out, s.ID)+ }+ if id := deploy.BundledVoiceServerID(inst); id != "" {+ out = append(out, id)+ }+ return out+}++func planVoiceAdd(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, regions []adminapi.VoiceRegion, region string, f *voiceFlags) ([]plannedVoice, error) {+ ids := append(takenVoiceIDs(regions, inst), voiceIDsInUse(ctx, e, inst, voice.ProjectName(inst.Project(), ""))...)+ ports := takenVoicePorts(ctx, e, inst)+ nodeIP := f.IP+ if nodeIP == "" {+ nodeIP = strings.TrimSpace(inst.State.Settings[settings.VoicePublicIP])+ }+ publicIP := nodeIP+ if publicIP == "" {+ pctx, cancel := context.WithTimeout(ctx, 10*time.Second)+ publicIP, _ = host.PublicIPv4(pctx)+ cancel()+ }+ memory := f.Memory+ if memory == "" {+ memory = strings.TrimSpace(inst.Setting("FLUXER_LIVEKIT_MEMORY_LIMIT"))+ if _, ok := voice.ParseBytes(memory); !ok {+ memory = voice.DefaultMemory+ }+ }+ image := f.Image+ if image == "" {+ image = instanceLiveKitImage(inst)+ }+ _, cpus := host.Hardware()+ softLimit := f.SoftLimit+ if softLimit == 0 {+ softLimit = voice.SoftLimit(cpus)+ }+ base := voice.WSOrigin(inst.Origin())+ if base == "" {+ return nil, ui.Refused("rexctl can't work out your instance's address.", "Run rexctl config set domain <domain>.")+ }+ var plans []plannedVoice+ for range f.Count {+ id := f.ID+ if id == "" {+ id = voice.NextID(region, ids)+ } else if slices.Contains(ids, id) {+ return nil, ui.Refused(fmt.Sprintf("A voice server or Compose project with the ID %s already exists.", id), "Pass --id with another ID.")+ }+ ids = append(ids, id)+ tcp, udp := f.TCPPort, f.UDPPort+ if tcp == 0 || udp == 0 {+ pt, pu := voice.PickPorts(ports, freeOnHost)+ if tcp == 0 {+ tcp = pt+ }+ if udp == 0 {+ udp = pu+ }+ }+ if tcp == 0 || udp == 0 {+ return nil, ui.Refused("No free pair of ports is left for voice on this machine.", "Pass --tcp-port and --udp-port.")+ }+ ports[host.Port{Number: tcp, Proto: "tcp"}] = true+ ports[host.Port{Number: udp, Proto: "udp"}] = true+ secret, err := voice.NewSecret()+ if err != nil {+ return nil, err+ }+ bindIP := bindAddress(f.IP, voice.InterfaceAddresses())+ plans = append(plans, plannedVoice{+ srv: instance.VoiceServer{+ ID: id,+ Region: region,+ Endpoint: base + voice.Path(id),+ Key: voice.Key(id),+ Secret: secret,+ TCPPort: tcp,+ UDPPort: udp,+ NodeIP: nodeIP,+ IPPinned: f.IP != "",+ BindIP: bindIP,+ Image: image,+ Pinned: f.Image != "",+ Memory: memory,+ SoftLimit: softLimit,+ Project: voice.ProjectName(inst.Project(), id),+ Network: inst.Project() + "_fluxer",+ Webhook: "http://api:8080/webhooks/livekit",+ Edge: voice.EdgeInstance,+ },+ publicIP: publicIP,+ softLimit: softLimit,+ })+ }+ return plans, nil+}++func showVoiceAddSummary(app *App, inst *instance.Instance, region string, plans []plannedVoice, f *voiceFlags) {+ u := app.UI+ hostMem, _ := host.Hardware()+ var total uint64+ for _, p := range plans {+ n, _ := voice.ParseBytes(p.srv.Memory)+ total += n+ }+ for _, p := range plans {+ s := p.srv+ u.Info("Voice server %s on %s", s.ID, inst.Domain())+ ip := p.publicIP+ if s.NodeIP == "" && ip != "" {+ ip += ", found with STUN"+ } else if ip == "" {+ ip = "LiveKit finds it with STUN"+ }+ rows := [][]string{+ {"Region", region},+ {"Endpoint", s.Endpoint},+ {"Public IPv4", ip},+ {"Ports", fmt.Sprintf("%d/tcp, %d/udp", s.TCPPort, s.UDPPort)},+ {"LiveKit", s.Image},+ {"Memory limit", fmt.Sprintf("%s of %s", s.Memory, voice.HumanBytes(hostMem))},+ {"Soft limit", plural(p.softLimit, "connection", "connections")},+ {"Webhook", s.Webhook},+ }+ if f.HasCoords {+ rows = append(rows, []string{"Location", fmt.Sprintf("%.2f, %.2f", f.Lat, f.Long)})+ }+ for _, r := range rows {+ u.Info(" %-15s %s", r[0], r[1])+ }+ u.Info("")+ }+ if hostMem > 0 && total > hostMem/2 {+ u.Warn(fmt.Sprintf("These servers can use %s together, more than half of this machine's %s.", voice.HumanBytes(total), voice.HumanBytes(hostMem)), "Pass --memory with a smaller limit.")+ }+ u.Info("Adding a path to the edge reloads it, and people connected to Fluxer reconnect once.")+ u.Info("Open the same ports in your provider's firewall. rexctl can't see that one.")+ if f.NoFirewall {+ var ports []string+ for _, p := range plans {+ ports = append(ports, portStrings(voicePortStrings(&p.srv))...)+ }+ u.Info("rexctl leaves the firewall on this machine alone. Open %s in it too.", ui.JoinAnd(ports))+ }+ u.Info("Change any of these with flags. Run rexctl voice add --help to see them.")+}++func confirmVoice(app *App, question string) error {+ if app.UI.Yes() || !app.UI.Interactive() {+ return nil+ }+ ok, err := app.UI.Confirm(question, true)+ if err != nil {+ return err+ }+ if !ok {+ return ui.Refused("Stopped without changing anything.", "")+ }+ return nil+}++func addOneVoiceServer(ctx context.Context, app *App, api *adminapi.Client, e *engine.Engine, p *engine.Project, inst *instance.Instance, plan *plannedVoice, f *voiceFlags) error {+ srv := plan.srv+ srv.CreatedAt = time.Now().UTC().Truncate(time.Second)+ if err := writeVoiceServer(inst, &srv); err != nil {+ return ui.Wrap(err, "Couldn't write the voice server's files.", "Check that you can write to "+inst.Dir+".")+ }+ inst.State.Voice = append(inst.State.Voice, srv)+ saved := inst.State.VoiceServer(srv.ID)+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ undo := fmt.Sprintf("Run rexctl voice remove %s to undo what rexctl did so far.", srv.ID)+ err := app.UI.Step(ctx, "Starting voice server "+srv.ID, func(ctx context.Context, s *ui.Step) error {+ if err := startVoiceServer(ctx, app, e, inst, saved, s); err != nil {+ return err+ }+ s.Done("Started voice server %s.", srv.ID)+ return nil+ })+ if err != nil {+ return err+ }+ draining := true+ active := true+ internal := voice.InternalURL(srv.ID)+ notes := "Added with rexctl voice add on " + time.Now().UTC().Format("2006-01-02")+ limit := plan.softLimit+ body := adminapi.CreateVoiceServerRequestBody{+ ServerId: srv.ID,+ Endpoint: srv.Endpoint,+ ApiKey: srv.Key,+ ApiSecret: srv.Secret,+ IsActive: &active,+ Draining: &draining,+ InternalEndpoint: &internal,+ Notes: ¬es,+ SoftConnectionLimit: &limit,+ }+ if f.HasCoords {+ lat, long := float32(f.Lat), float32(f.Long)+ body.Latitude, body.Longitude = &lat, &long+ }+ _, err = api.CreateVoiceServer(ctx, srv.Region, body)+ if err != nil {+ return ui.Wrap(adminapi.Explain(err), fmt.Sprintf("Couldn't register %s with your instance.", srv.ID), undo)+ }+ dropIn := filepath.Join(inst.Path(instance.CaddyDropInDir), voice.DropInName(srv.ID))+ if err := os.WriteFile(dropIn, voice.DropIn(srv.ID), 0o644); err != nil {+ return ui.Wrap(err, "Couldn't write the edge route.", undo)+ }+ if err := reloadCaddy(ctx, p, "edge", "/etc/caddy/Caddyfile"); err != nil {+ return ui.Wrap(engineFailure(err), "Couldn't reload the edge with the new route.", undo)+ }+ base, dial := voiceProbeTarget(inst, saved)+ if err := waitForEndpoint(ctx, base, dial, true, time.Minute); err != nil {+ return ui.Wrap(err, fmt.Sprintf("%s doesn't answer through the edge.", srv.Endpoint), undo)+ }+ saved.Opened = openVoiceFirewall(ctx, app, voicePortStrings(saved), f.NoFirewall)+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ open := false+ if _, err := api.UpdateVoiceServer(ctx, srv.Region, srv.ID, adminapi.UpdateVoiceServerRequestBody{Draining: &open}); err != nil {+ return ui.Wrap(adminapi.Explain(err), fmt.Sprintf("%s runs, and it's still draining.", srv.ID), fmt.Sprintf("Clear Draining for %s in the admin panel.", srv.ID))+ }+ app.UI.OK("Added voice server %s at %s.", srv.ID, srv.Endpoint)+ results := runTransportTests(ctx, srv.Endpoint, func() (string, error) {+ return voice.TestToken(srv.Key, srv.Secret, voice.TestRoom(), time.Now()), nil+ }, true)+ reportTransports(app, results, true)+ app.UI.Info("%s", providerPortsText(saved))+ return nil+}++func providerPortsText(srv *instance.VoiceServer) string {+ return fmt.Sprintf("Open %d/tcp and %d/udp in your provider's firewall too, such as a Hetzner Cloud Firewall or an AWS security group.", srv.TCPPort, srv.UDPPort)+}diff --git a/fluxer_rexctl/cmd/voice_installsh_test.go b/fluxer_rexctl/cmd/voice_installsh_test.gonew file mode 100644index 000000000..5b2dd5e0e--- /dev/null+++ b/fluxer_rexctl/cmd/voice_installsh_test.go@@ -0,0 +1,117 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "archive/tar"+ "bytes"+ "compress/gzip"+ "crypto/sha256"+ "encoding/hex"+ "encoding/pem"+ "fmt"+ "net/http"+ "net/http/httptest"+ "os"+ "os/exec"+ "path/filepath"+ "runtime"+ "strings"+ "testing"+)++func fakeRexctlArchive(t *testing.T, argsFile string) []byte {+ t.Helper()+ script := fmt.Sprintf("#!/bin/sh\nfor a in \"$@\"; do printf '%%s\\n' \"$a\"; done > %q\n", argsFile)+ var buf bytes.Buffer+ gz := gzip.NewWriter(&buf)+ tw := tar.NewWriter(gz)+ if err := tw.WriteHeader(&tar.Header{Name: "rexctl", Mode: 0o755, Size: int64(len(script))}); err != nil {+ t.Fatal(err)+ }+ if _, err := tw.Write([]byte(script)); err != nil {+ t.Fatal(err)+ }+ if err := tw.Close(); err != nil {+ t.Fatal(err)+ }+ if err := gz.Close(); err != nil {+ t.Fatal(err)+ }+ return buf.Bytes()+}++func TestInstallScriptMapsVoiceShorthand(t *testing.T) {+ if runtime.GOOS == "windows" {+ t.Skip("install.sh runs on Linux and macOS")+ }+ for _, tool := range []string{"sh", "curl", "tar"} {+ if _, err := exec.LookPath(tool); err != nil {+ t.Skipf("%s is missing", tool)+ }+ }+ script, err := filepath.Abs("../../fluxer_docs/src/installer/install.sh")+ if err != nil {+ t.Fatal(err)+ }+ cases := []struct {+ args []string+ want []string+ }{+ {[]string{"voice", "chat.example.com", "fvj_1_x"}, []string{"voice", "join", "chat.example.com", "fvj_1_x"}},+ {[]string{"voice", "chat.example.com", "fvj_1_x", "--yes"}, []string{"voice", "join", "chat.example.com", "fvj_1_x", "--yes"}},+ {[]string{"voice", "chat.example.com"}, []string{"voice", "join", "chat.example.com"}},+ {[]string{"voice", "chat.example.com", "--yes"}, []string{"voice", "join", "chat.example.com", "--yes"}},+ {[]string{"voice", "join", "chat.example.com", "fvj_1_x"}, []string{"voice", "join", "chat.example.com", "fvj_1_x"}},+ {[]string{"voice", "--help"}, []string{"voice", "--help"}},+ {[]string{"voice", "list"}, []string{"voice", "list"}},+ {[]string{"voice", "remove", "fra-server-1"}, []string{"voice", "remove", "fra-server-1"}},+ {[]string{"voice", "token", "list"}, []string{"voice", "token", "list"}},+ {[]string{"voice", "token", "revoke", "123"}, []string{"voice", "token", "revoke", "123"}},+ {[]string{"voice", "hostname", "fra-server-1", "voice.example.com"}, []string{"voice", "hostname", "fra-server-1", "voice.example.com"}},+ {[]string{"voice", "status", "fra-server-1", "--yes"}, []string{"voice", "status", "fra-server-1", "--yes"}},+ }+ for _, tc := range cases {+ t.Run(strings.Join(tc.args, " "), func(t *testing.T) {+ dir := t.TempDir()+ argsFile := filepath.Join(dir, "args")+ archive := fakeRexctlArchive(t, argsFile)+ sum := sha256.Sum256(archive)+ var srv *httptest.Server+ srv = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if strings.HasSuffix(r.URL.Path, ".tar.gz") {+ _, _ = w.Write(archive)+ return+ }+ fmt.Fprintf(w, "version v9.9.9\nurl %s/rexctl.tar.gz\nsha256 %s\n", srv.URL, hex.EncodeToString(sum[:]))+ }))+ defer srv.Close()+ ca := filepath.Join(dir, "ca.pem")+ certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: srv.Certificate().Raw})+ if err := os.WriteFile(ca, certPEM, 0o644); err != nil {+ t.Fatal(err)+ }+ cmd := exec.Command("sh", append([]string{script}, tc.args...)...)+ cmd.Env = []string{+ "PATH=" + os.Getenv("PATH"),+ "HOME=" + dir,+ "REXCTL_RELEASE_URL=" + srv.URL + "/release.json",+ "CURL_CA_BUNDLE=" + ca,+ "SSL_CERT_FILE=" + ca,+ "FLUXER_VOICE_JOIN_TOKEN=fvj_1_x",+ }+ cmd.Stdin = strings.NewReader("")+ out, err := cmd.CombinedOutput()+ if err != nil {+ t.Fatalf("install.sh: %v\n%s", err, out)+ }+ got, err := os.ReadFile(argsFile)+ if err != nil {+ t.Fatalf("rexctl never ran: %v\n%s", err, out)+ }+ if strings.TrimSpace(string(got)) != strings.Join(tc.want, "\n") {+ t.Fatalf("rexctl got %q, want %q", strings.Fields(string(got)), tc.want)+ }+ })+ }+}diff --git a/fluxer_rexctl/cmd/voice_join.go b/fluxer_rexctl/cmd/voice_join.gonew file mode 100644index 000000000..eb6ffe145--- /dev/null+++ b/fluxer_rexctl/cmd/voice_join.go@@ -0,0 +1,1111 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "bufio"+ "context"+ "errors"+ "fmt"+ "io"+ "net"+ "net/http"+ "net/url"+ "os"+ "path/filepath"+ "runtime"+ "slices"+ "strings"+ "time"++ "github.com/spf13/cobra"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"+ "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/release"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++const (+ voiceTokenEnv = "FLUXER_VOICE_JOIN_TOKEN"+ voiceSysctlFile = "/etc/sysctl.d/61-fluxer-voice.conf"+ voiceSysctl = "net.core.rmem_max = 16777216\nnet.core.wmem_max = 16777216\n"+ voiceJoinAttempts = 5+)++var (+ dnsWait = 10 * time.Minute+ hostnameLookup = func() voice.Resolver { return voice.PublicLookup(&http.Client{Timeout: 5 * time.Second}) }+)++func newVoiceJoinCmd(app *App) *cobra.Command {+ f := &voiceFlags{}+ c := &cobra.Command{+ Use: "join <domain> [token]",+ Short: "Install a voice server on this machine and connect it to an instance",+ Long: "Install a standalone LiveKit voice server on this machine and register it with the Fluxer instance at <domain>. " ++ "Get the token with rexctl voice token on the Fluxer machine, or with Add a voice server in the admin panel. " ++ "The token works once. rexctl also reads it from " + voiceTokenEnv + " or, with --token-stdin, from standard input.",+ Args: func(c *cobra.Command, args []string) error {+ if len(args) == 0 {+ return ui.Usage("Pass the domain of your Fluxer instance.")+ }+ if len(args) > 2 {+ return ui.Usage(c.CommandPath() + " takes a domain and a token.")+ }+ return nil+ },+ RunE: func(c *cobra.Command, args []string) error {+ if err := validateVoiceFlags(c, f); err != nil {+ return err+ }+ if f.NoEdge && f.Endpoint == "" {+ return ui.Usage("Pass --endpoint with --no-edge.")+ }+ if f.Endpoint != "" && !strings.HasPrefix(f.Endpoint, "wss://") {+ return ui.Usage("Pass --endpoint as a wss:// URL.")+ }+ token, err := joinToken(c, args, f)+ if err != nil {+ return err+ }+ return runVoiceJoin(c.Context(), app, normaliseDomain(args[0]), token, f)+ },+ }+ addVoiceOverrideFlags(c, f)+ 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.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.BoolVar(&f.TokenStdin, "token-stdin", false, "read the join token from standard input")+ return c+}++func joinToken(c *cobra.Command, args []string, f *voiceFlags) (string, error) {+ token := ""+ switch {+ case len(args) == 2:+ token = args[1]+ case f.TokenStdin:+ line, err := bufio.NewReader(c.InOrStdin()).ReadString('\n')+ if err != nil && !errors.Is(err, io.EOF) {+ return "", err+ }+ token = line+ default:+ token = os.Getenv(voiceTokenEnv)+ }+ token = strings.TrimSpace(token)+ if token == "" {+ return "", ui.Usage("Pass the join token after the domain, set " + voiceTokenEnv + ", or pass --token-stdin.")+ }+ if !voice.ValidToken(token) {+ return "", ui.Usage("That isn't a voice join token. They start with fvj_.")+ }+ return token, nil+}++func voiceMachineDir(app *App) (string, error) {+ if app.Flags.Dir != "" {+ return filepath.Abs(app.Flags.Dir)+ }+ for _, dir := range instance.ReadRecord(instance.OwnScope()).Instances {+ if inst, err := instance.Find(dir); err == nil && inst.State.IsVoice() {+ return dir, nil+ }+ }+ if euid() == 0 {+ return "/opt/fluxer-voice", nil+ }+ home, err := os.UserHomeDir()+ if err != nil {+ return "", err+ }+ return filepath.Join(home, "fluxer-voice"), nil+}++func openVoiceMachine(dir string) (*instance.Instance, bool, error) {+ inst, err := instance.Find(dir)+ if err == nil {+ if !inst.State.IsVoice() {+ return nil, false, ui.Refused(fmt.Sprintf("%s holds a Fluxer instance.", dir), "Run rexctl voice add there, or pass --dir with another folder.")+ }+ return inst, false, nil+ }+ if !errors.Is(err, instance.ErrNotFound) {+ return nil, false, ui.Wrap(err, fmt.Sprintf("rexctl can't read %s.", dir), "Pass --dir with another folder.")+ }+ now := time.Now().UTC().Truncate(time.Second)+ inst = &instance.Instance{Dir: dir, State: &instance.State{+ Schema: instance.StateSchema, Kind: instance.KindVoice, Project: voice.MachineProject,+ Settings: map[string]string{}, Secrets: map[string]string{}, InstalledAt: now, UpdatedAt: now,+ }}+ return inst, true, nil+}++type joinPlan struct {+ domain string+ disc *voice.Discovery+ preview *voice.Preview+ reach voice.Reachability+ srv instance.VoiceServer+ address string+ ipCert bool+ edge string+ edgeInst *instance.Instance+ newEdge bool+ firewall host.Firewall+ hostMem uint64+ cpus int+ image string+ imageWhy string+ coords bool+ lat float64+ long float64+}++func runVoiceJoin(ctx context.Context, app *App, domain, token string, f *voiceFlags) error {+ if runtime.GOOS != "linux" && !f.DryRun {+ return ui.Refused("Voice servers run on Linux.", "Run this on a Linux machine.")+ }+ hc := &http.Client{Timeout: 20 * time.Second}+ client := &voice.Client{HTTP: hc}+ disc, err := client.Discover(ctx, domain)+ if err != nil {+ return ui.Wrap(err, fmt.Sprintf("rexctl can't read the Fluxer instance at %s.", domain), "Check the domain, and that it has a valid certificate.")+ }+ if !disc.VoiceEnabled {+ return ui.Refused(fmt.Sprintf("Voice is turned off on %s.", domain), "Turn on voice there first.")+ }+ dir, err := voiceMachineDir(app)+ if err != nil {+ return err+ }+ inst, fresh, err := openVoiceMachine(dir)+ if err != nil {+ return err+ }+ e, err := app.Engine(ctx, nil)+ if err != nil && joinInstallsDocker(err, runtime.GOOS, f.DryRun) {+ e, err = installDockerHere(ctx, app, dir, "run the same command again", func() (bool, error) {+ if app.UI.Yes() || !app.UI.Interactive() {+ return true, nil+ }+ return app.UI.Confirm("Install Docker?", true)+ })+ if err == nil {+ app.engine = e+ }+ }+ if err != nil {+ return err+ }+ plan := &joinPlan{domain: domain, disc: disc, coords: f.HasCoords, lat: f.Lat, long: f.Long}+ if err := planJoin(ctx, app, client, e, inst, token, plan, f); err != nil {+ return err+ }+ if err := podmanJoinRefusal(e, plan); err != nil {+ return err+ }+ showJoinSummary(app, plan, f)+ if f.DryRun {+ return nil+ }+ if err := confirmVoice(app, "Install this voice server?"); err != nil {+ return err+ }+ if fresh {+ if err := os.MkdirAll(inst.Path(instance.StateDirName), 0o700); err != nil {+ return ui.Wrap(err, "Couldn't create "+dir+".", "Run rexctl as root, or pass --dir with a folder you can write to.")+ }+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ app.remember(dir)+ }+ l, err := inst.Lock(ctx)+ if err != nil {+ return ui.Refused("Another rexctl command is changing this machine's voice servers.", "Wait for it to finish, then try again.")+ }+ defer l.Release()+ return finishJoin(ctx, app, client, e, inst, token, plan, f)+}++func podmanJoinRefusal(e *engine.Engine, plan *joinPlan) error {+ if e.Kind != engine.Podman || !olderPodman(e.Version) || plan.edgeInst != nil {+ return nil+ }+ return ui.Refused(fmt.Sprintf("Podman %s doesn't start voice servers again when this machine restarts.", e.Version), fmt.Sprintf("Upgrade Podman to %s or later, or use Docker.", podmanRestartsContainers))+}++func joinInstallsDocker(err error, goos string, dryRun bool) bool {+ return goos == "linux" && !dryRun && !errors.Is(err, context.Canceled) && engine.ClassOf(err) == engine.ClassUnreachable+}++func planJoin(ctx context.Context, app *App, client *voice.Client, e *engine.Engine, inst *instance.Instance, token string, plan *joinPlan, f *voiceFlags) error {+ pctx, cancel := context.WithTimeout(ctx, 15*time.Second)+ publicIP, _ := host.PublicIPv4(pctx)+ cancel()+ if f.IP != "" {+ publicIP = f.IP+ }+ if publicIP == "" {+ return ui.Refused("This machine has no public IPv4 address that rexctl can find.", "Voice servers need IPv4. Pass --ip if the address is known.")+ }+ reach, err := assessJoin(publicIP, f.IP, voice.InterfaceAddresses(), voice.DefaultRouteIPv4())+ if err != nil {+ return err+ }+ plan.reach = reach+ plan.hostMem, plan.cpus = host.Hardware()+ plan.preview, err = client.Preview(ctx, plan.disc.APIPublic, token, voice.PreviewRequest{+ PublicIPv4: publicIP, RegionID: f.Region, CPUCount: plan.cpus, MemoryBytes: plan.hostMem, RexctlVersion: app.Version,+ })+ if err != nil {+ return joinError(err, plan.domain, "")+ }+ plan.image, plan.imageWhy = joinImage(app, plan.preview, f)+ plan.firewall, _ = host.DetectFirewall(ctx)+ if err := planEdge(ctx, app, e, inst, plan, f); err != nil {+ return err+ }+ id, err := chooseJoinID(plan.preview, f.ID, inst, voiceIDsInUse(ctx, e, inst, voice.JoinedProject("")))+ if err != nil {+ return err+ }+ if plan.edge == voice.EdgeOwn {+ if err := chooseAddress(ctx, app, plan, id, f); err != nil {+ return err+ }+ }+ tcp, udp := f.TCPPort, f.UDPPort+ if existing := inst.State.VoiceServer(id); existing != nil {+ tcp, udp = existing.TCPPort, existing.UDPPort+ }+ if tcp == 0 || udp == 0 {+ pt, pu := voice.PickPorts(takenVoicePorts(ctx, e, inst), freeOnHost)+ tcp, udp = orInt(tcp, pt), orInt(udp, pu)+ }+ if tcp == 0 || udp == 0 {+ return ui.Refused("No free pair of ports is left for voice on this machine.", "Pass --tcp-port and --udp-port.")+ }+ secret, err := voice.NewSecret()+ if err != nil {+ return err+ }+ if existing := inst.State.VoiceServer(id); existing != nil {+ secret = existing.Secret+ }+ memory := f.Memory+ if memory == "" {+ memory = voice.MachineMemory(plan.hostMem)+ }+ soft := f.SoftLimit+ if soft == 0 {+ if plan.preview.SoftLimit != nil {+ soft = *plan.preview.SoftLimit+ } else {+ soft = voice.SoftLimit(plan.cpus)+ }+ }+ network := voice.MachineNetwork+ if plan.edgeInst != nil {+ network = plan.edgeInst.Project() + "_fluxer"+ }+ httpPort := 0+ if f.NoEdge {+ httpPort = f.HTTPPort+ if existing := inst.State.VoiceServer(id); httpPort == 0 && existing != nil {+ httpPort = existing.HTTPPort+ }+ if httpPort == 0 {+ httpPort = pickHTTPPort(takenVoicePorts(ctx, e, inst), tcp, freeOnHost)+ }+ }+ plan.srv = instance.VoiceServer{+ ID: id,+ Region: plan.preview.Region.ID,+ Key: voice.Key(id),+ Secret: secret,+ TCPPort: tcp,+ UDPPort: udp,+ HTTPPort: httpPort,+ NodeIP: publicIP,+ BindIP: bindAddress(f.IP, voice.InterfaceAddresses()),+ Image: plan.image,+ Pinned: f.Image != "",+ Memory: memory,+ SoftLimit: soft,+ Project: voice.JoinedProject(id),+ Network: network,+ Webhook: plan.preview.WebhookURL,+ Instance: plan.domain,+ Edge: plan.edge,+ Joined: true,+ }+ if f.NoEdge {+ plan.srv.Endpoint = strings.TrimRight(f.Endpoint, "/")+ } else {+ plan.srv.Endpoint = voice.Endpoint(plan.address, id)+ }+ if u, err := url.Parse(plan.srv.Endpoint); err == nil {+ plan.srv.Hostname = u.Hostname()+ }+ return nil+}++func chooseJoinID(preview *voice.Preview, flagID string, inst *instance.Instance, taken []string) (string, error) {+ id := preview.ServerID+ switch {+ case flagID != "" && preview.ServerIDPinned && flagID != id:+ return "", ui.Refused(fmt.Sprintf("The token is for the server ID %s.", id), "Leave out --id.")+ case flagID != "":+ id = flagID+ }+ if existing := inst.State.VoiceServer(id); existing != nil {+ if existing.NodeURL != "" {+ return "", ui.Refused(fmt.Sprintf("This machine already runs %s.", id), "Pass --id with another ID.")+ }+ return id, nil+ }+ if !slices.Contains(taken, id) {+ return id, nil+ }+ switch {+ case preview.ServerIDPinned:+ return "", ui.Refused(fmt.Sprintf("The token is for the server ID %s, and this machine already has a voice server or Compose project with that ID.", id), "Make a new token with another server ID on the Fluxer machine.")+ case flagID != "":+ return "", ui.Refused(fmt.Sprintf("This machine already has a voice server or Compose project with the ID %s.", id), "Pass --id with another ID.")+ }+ return voice.NextID(preview.Region.ID, taken), nil+}++func assessJoin(publicIP, flagIP string, local []string, routeIP string) (voice.Reachability, error) {+ reach := voice.Assess(publicIP, local, routeIP)+ if reach.CGNAT && flagIP == "" {+ return reach, ui.Refused("This machine is behind CGNAT, a shared address that can't take incoming calls.", "Pass --ip with the machine's public address if it has one of its own.")+ }+ if flagIP != "" {+ reach.CGNAT = false+ }+ return reach, nil+}++func pickHTTPPort(taken map[host.Port]bool, tcp int, free func(int, string) bool) int {+ for n := 7880; n < 7980; n++ {+ if n != tcp && !taken[host.Port{Number: n, Proto: "tcp"}] && free(n, "tcp") {+ return n+ }+ }+ return 0+}++func bindAddress(ip string, local []string) string {+ if ip != "" && slices.Contains(local, ip) {+ return ip+ }+ return ""+}++func joinFirewallText(fw host.Firewall, ports int, noFirewall bool) string {+ switch {+ case fw == host.FirewallNone:+ return "none found on this machine. Open the ports in your provider's firewall."+ case noFirewall:+ return fmt.Sprintf("%s, left alone. Open the ports in it and in your provider's firewall.", fw)+ }+ return fmt.Sprintf("%s, will open %d ports. Open the same %d in your provider's firewall.", fw, ports, ports)+}++func orInt(v, fallback int) int {+ if v != 0 {+ return v+ }+ return fallback+}++func joinImage(app *App, preview *voice.Preview, f *voiceFlags) (string, string) {+ if f.Image != "" {+ return f.Image, "from --image"+ }+ own := ownLiveKitImage()+ switch {+ case preview.Release == "" || !release.Valid(preview.Release):+ return own, "the pin of this rexctl, because the instance runs a development build"+ case preview.Release == app.Version:+ return own, "matches " + preview.Release+ default:+ return own, fmt.Sprintf("the pin of rexctl %s. The instance runs %s, and rexctl upgrade brings this machine to it", app.Version, preview.Release)+ }+}++func planEdge(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, plan *joinPlan, f *voiceFlags) error {+ if f.NoEdge {+ plan.edge = voice.EdgeNone+ return nil+ }+ owners, _ := e.PublishedHostPorts(ctx)+ holder := ""+ for _, o := range owners {+ if (o.Number == 443 || o.Number == 80) && o.Proto == "tcp" {+ holder = o.Project+ }+ }+ switch {+ case holder == voice.EdgeProject:+ plan.edge = voice.EdgeOwn+ case holder != "":+ for _, dir := range instance.ReadRecord(instance.OwnScope()).Instances {+ other, err := instance.Find(dir)+ if err == nil && !other.State.IsVoice() && other.Project() == holder {+ if f.Hostname != "" {+ return ui.Refused(fmt.Sprintf("Ports 80 and 443 belong to the Fluxer instance in %s. The server uses that instance's domain.", dir), "Leave out --hostname.")+ }+ plan.edge, plan.edgeInst = voice.EdgeInstance, other+ plan.address = other.Domain()+ return nil+ }+ }+ return ui.Refused(fmt.Sprintf("Ports 80 and 443 belong to the Compose project %s.", holder), "Pass --no-edge and --endpoint to use your own proxy.")+ default:+ for _, p := range []int{80, 443} {+ if used, owner, err := host.PortInUse("tcp", p); err == nil && used {+ if owner == "" {+ owner = "another program"+ }+ return ui.Refused(fmt.Sprintf("Port %d is in use by %s.", p, owner), "Pass --no-edge and --endpoint to use your own proxy.")+ }+ }+ plan.edge, plan.newEdge = voice.EdgeOwn, true+ }+ return nil+}++func chooseAddress(ctx context.Context, app *App, plan *joinPlan, id string, f *voiceFlags) error {+ pinned := ""+ if plan.preview.Hostname != nil {+ pinned = strings.ToLower(*plan.preview.Hostname)+ }+ name := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(f.Hostname), "."))+ if pinned != "" && name != "" && name != pinned {+ return ui.Refused(fmt.Sprintf("The token is for the hostname %s.", pinned), "Leave out --hostname.")+ }+ if name == "" {+ name = pinned+ }+ resolve := hostnameLookup()+ check := func(host string) voice.HostnameCheck {+ answer, err := resolve(ctx, host)+ if err != nil {+ return voice.HostnameMissing+ }+ return voice.CheckHostname(answer, plan.reach.PublicIPv4, voice.InterfaceAddresses())+ }+ if name == "" && plan.preview.SuggestedHostname != "" && check(plan.preview.SuggestedHostname) == voice.HostnameOK {+ name = plan.preview.SuggestedHostname+ }+ if name == "" {+ plan.address, plan.ipCert = plan.reach.PublicIPv4, true+ return nil+ }+ if pinned != "" {+ if waitForHostname(ctx, app, name, plan.reach.PublicIPv4, check, f.DryRun, true) || f.DryRun {+ plan.address = name+ return nil+ }+ return ui.Refused(fmt.Sprintf("The token needs the hostname %s, and it doesn't point at this machine yet.", name), "Point it at "+plan.reach.PublicIPv4+" with DNS only, then run the same command again.")+ }+ if waitForHostname(ctx, app, name, plan.reach.PublicIPv4, check, f.DryRun, false) {+ plan.address = name+ return nil+ }+ app.UI.Info("Using %s for now. Once DNS points at this machine, run rexctl voice hostname %s %s.", plan.reach.PublicIPv4, id, name)+ plan.address, plan.ipCert = plan.reach.PublicIPv4, true+ return nil+}++func waitForHostname(ctx context.Context, app *App, name, ipv4 string, check func(string) voice.HostnameCheck, dryRun, always bool) bool {+ result := check(name)+ if result == voice.HostnameOK {+ return true+ }+ switch result {+ case voice.HostnameProxied:+ app.UI.Warn(fmt.Sprintf("%s goes through the Cloudflare proxy.", name), "Set the record to DNS only. Calls can't go through the proxy.")+ case voice.HostnameIPv6Elsewhere:+ app.UI.Warn(fmt.Sprintf("%s has an AAAA record that points somewhere else.", name), "Remove that record. Let's Encrypt checks IPv6 first.")+ default:+ app.UI.Info("%s doesn't point at this machine yet. Add this record at your DNS provider:", name)+ app.UI.Info("")+ app.UI.Info(" %s. A %s", name, ipv4)+ app.UI.Info("")+ app.UI.Info("On Cloudflare, set the record to DNS only.")+ }+ if dryRun || !always && (app.UI.Yes() || !app.UI.Interactive()) {+ return false+ }+ if always {+ app.UI.Info("Waiting up to 10 minutes.")+ } else {+ app.UI.Info("Waiting up to 10 minutes. rexctl uses the IP address after that.")+ }+ deadline := time.Now().Add(dnsWait)+ for time.Now().Before(deadline) {+ select {+ case <-ctx.Done():+ return false+ case <-time.After(15 * time.Second):+ }+ if check(name) == voice.HostnameOK {+ app.UI.OK("%s points at this machine.", name)+ return true+ }+ }+ return false+}++func joinRegionText(p *voice.Preview, requested string) string {+ region := p.Region.ID+ if p.Region.Name != "" {+ region += " (" + p.Region.Name + ")"+ }+ if p.Region.New {+ return region + ", a new region from the token"+ }+ source := p.Region.Source+ if source == "" && requested != "" && requested == p.Region.ID {+ source = "requested"+ }+ if p.Region.DistanceKm == nil {+ switch source {+ case "token":+ return region + ", from the token"+ case "requested":+ return region + ", from --region"+ }+ return region+ }+ km := *p.Region.DistanceKm+ switch source {+ case "nearest":+ return region + fmt.Sprintf(", nearest existing region, %.0f km", km)+ case "token":+ return region + fmt.Sprintf(", from the token, %.0f km away", km)+ case "requested":+ return region + fmt.Sprintf(", from --region, %.0f km away", km)+ }+ return region + fmt.Sprintf(", %.0f km away", km)+}++func showJoinSummary(app *App, plan *joinPlan, f *voiceFlags) {+ u := app.UI+ s := plan.srv+ p := plan.preview+ region := joinRegionText(p, f.Region)+ cert := "Let's Encrypt for " + plan.address+ switch {+ case s.Edge == voice.EdgeNone:+ cert = "from your own proxy"+ case s.Edge == voice.EdgeInstance:+ cert = "the certificate of " + plan.address+ case plan.ipCert:+ cert = "Let's Encrypt IP address certificate for " + plan.address + ", renewed about every 6 days"+ }+ ports := []string{fmt.Sprintf("%d/tcp", s.TCPPort), fmt.Sprintf("%d/udp", s.UDPPort)}+ if plan.newEdge {+ ports = append(ports, "80/tcp", "443/tcp")+ }+ fw := joinFirewallText(plan.firewall, len(ports), f.NoFirewall)+ ip := s.NodeIP+ if plan.reach.BehindNAT {+ ip += ", behind NAT. Forward each port to the same port number on this machine."+ }+ rows := [][]string{+ {"Server ID", s.ID},+ {"Region", region},+ {"Endpoint", s.Endpoint},+ {"Certificate", cert},+ {"Public IPv4", ip},+ {"Ports", strings.Join(ports, ", ")},+ {"Firewall", fw},+ {"LiveKit", s.Image + ", " + plan.imageWhy},+ {"Memory limit", fmt.Sprintf("%s of %s", s.Memory, voice.HumanBytes(plan.hostMem))},+ {"Soft limit", plural(s.SoftLimit, "connection", "connections")},+ {"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)})+ }+ u.Info("Voice server for %s", plan.domain)+ u.Info("")+ for _, r := range rows {+ u.Info(" %-15s %s", r[0], r[1])+ }+ u.Info("")+ u.Info("Change any of these with flags. Run rexctl voice join --help to see them.")+}++func finishJoin(ctx context.Context, app *App, client *voice.Client, e *engine.Engine, inst *instance.Instance, token string, plan *joinPlan, f *voiceFlags) error {+ srv := plan.srv+ if existing := inst.State.VoiceServer(srv.ID); existing != nil {+ srv.Opened = slices.Clone(existing.Opened)+ srv.CreatedAt = existing.CreatedAt+ inst.State.RemoveVoiceServer(srv.ID)+ }+ if srv.CreatedAt.IsZero() {+ srv.CreatedAt = time.Now().UTC().Truncate(time.Second)+ }+ inst.State.Voice = append(inst.State.Voice, srv)+ if plan.edgeInst != nil {+ inst.State.VoiceEdge = &instance.VoiceEdge{Instance: plan.edgeInst.Dir}+ } else if inst.State.VoiceEdge == nil && srv.Edge == voice.EdgeOwn {+ inst.State.VoiceEdge = &instance.VoiceEdge{Email: f.Email}+ }+ saved := inst.State.VoiceServer(srv.ID)+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ writeVoiceSysctl(ctx, app)+ if err := writeVoiceServer(inst, saved); err != nil {+ return ui.Wrap(err, "Couldn't write the voice server's files.", "Check that you can write to "+inst.Dir+".")+ }+ dirFlag := voiceDirFlag(inst.Dir)+ undo := fmt.Sprintf("Run rexctl voice join %s <token>%s again to retry, or rexctl voice remove %s%s to undo.", plan.domain, dirFlag, srv.ID, dirFlag)+ err := app.UI.Step(ctx, "Starting voice server "+srv.ID, func(ctx context.Context, s *ui.Step) error {+ if saved.Edge == voice.EdgeOwn {+ if err := ensureVoiceEdge(ctx, app, e, inst, s); err != nil {+ return err+ }+ }+ if err := startVoiceServer(ctx, app, e, inst, saved, s); err != nil {+ return err+ }+ if err := routeVoiceServer(ctx, app, e, inst, saved); err != nil {+ return err+ }+ s.Done("Started voice server %s.", srv.ID)+ return nil+ })+ if err != nil {+ return ui.Wrap(err, "The voice server didn't start.", undo)+ }+ ports := voicePortStrings(saved)+ if saved.Edge == voice.EdgeOwn {+ ports = append(ports, host.Port{Number: 80, Proto: "tcp"}, host.Port{Number: 443, Proto: "tcp"})+ }+ opened := openVoiceFirewall(ctx, app, ports, f.NoFirewall)+ for _, o := range opened {+ if (o == "80/tcp" || o == "443/tcp") && inst.State.VoiceEdge != nil {+ inst.State.VoiceEdge.Opened = append(inst.State.VoiceEdge.Opened, o)+ } else {+ saved.Opened = append(saved.Opened, o)+ }+ }+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ edgeAddr := "127.0.0.1:443"+ if saved.Edge == voice.EdgeNone {+ edgeAddr = ""+ }+ err = app.UI.Step(ctx, "Waiting for "+srv.Endpoint, func(ctx context.Context, s *ui.Step) error {+ var err error+ if edgeAddr == "" {+ err = waitForPublicEndpoint(ctx, srv.Endpoint, 10*time.Minute)+ } else {+ err = waitForEndpoint(ctx, voice.HTTPBase(srv.Endpoint), edgeAddr, false, 3*time.Minute)+ }+ if err != nil {+ return err+ }+ s.Done("%s answers with a valid certificate.", srv.Endpoint)+ return nil+ })+ if err != nil {+ check := "Check that ports 80 and 443 are open to the internet"+ if saved.Edge == voice.EdgeNone {+ path := "/"+ if 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)+ }+ return ui.Wrap(err, fmt.Sprintf("%s doesn't answer yet.", srv.Endpoint), check+", then "+strings.ToLower(undo[:1])+undo[1:])+ }+ joined, err := joinWithRetries(ctx, app, client, e, inst, token, plan, saved)+ if err != nil {+ return err+ }+ saved.NodeURL = joined.NodeURL+ saved.Region = joined.RegionID+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ app.UI.OK("Voice server %s joined %s in region %s.", saved.ID, plan.domain, joined.RegionID)+ waitForVoiceOrigin(ctx, app, client, plan.domain, saved.Endpoint)+ results := runTransportTests(ctx, saved.Endpoint, func() (string, error) {+ return voice.TestToken(saved.Key, saved.Secret, voice.TestRoom(), time.Now()), nil+ }, false)+ reportTransports(app, results, true)+ app.UI.Info("Run rexctl voice test %s on the Fluxer machine to test from outside this network.", saved.ID)+ if status, _, err := client.TestWebhook(ctx, saved.Webhook, saved.Key, saved.Secret, time.Now()); err != nil || status < 200 || status > 299 {+ app.UI.Warn(fmt.Sprintf("%s doesn't accept a test webhook from %s.", plan.domain, saved.ID), "Run rexctl doctor to see why.")+ } else {+ app.UI.OK("%s accepts webhooks from %s.", plan.domain, saved.ID)+ }+ app.UI.Info("Run rexctl voice status%s to check it later, and rexctl voice remove %s%s to take it away.", dirFlag, saved.ID, dirFlag)+ app.UI.Info("This machine doesn't upgrade with the Fluxer machine. Run rexctl upgrade%s here after you upgrade Fluxer, or rexctl auto-upgrade on%s to upgrade it every day.", dirFlag, dirFlag)+ return nil+}++func joinWithRetries(ctx context.Context, app *App, client *voice.Client, e *engine.Engine, inst *instance.Instance, token string, plan *joinPlan, saved *instance.VoiceServer) (*voice.Joined, error) {+ for attempt := 1; ; attempt++ {+ req := voice.JoinRequest{+ ServerID: saved.ID,+ RegionID: plan.preview.Region.ID,+ Endpoint: saved.Endpoint,+ APIKey: saved.Key,+ APISecret: saved.Secret,+ SoftLimit: saved.SoftLimit,+ PublicIPv4: saved.NodeIP,+ }+ switch {+ case plan.coords:+ req.Latitude, req.Longitude = &plan.lat, &plan.long+ case plan.preview.Latitude != nil && plan.preview.Longitude != nil:+ req.Latitude, req.Longitude = plan.preview.Latitude, plan.preview.Longitude+ }+ joined, err := client.Join(ctx, plan.disc.APIPublic, token, req)+ code := voice.CodeOf(err)+ if err == nil {+ return joined, nil+ }+ if plan.preview.ServerIDPinned {+ return nil, pinnedJoinError(err, plan.domain, saved.ID)+ }+ if (code != voice.CodeIDInUse && code != voice.CodeKeyInUse) || attempt >= voiceJoinAttempts {+ return nil, joinError(err, plan.domain, saved.ID)+ }+ next := voice.NextID(plan.preview.Region.ID, append(voiceIDsInUse(ctx, e, inst, voice.JoinedProject("")), saved.ID))+ app.UI.Info("%s is taken on %s. Trying %s.", saved.ID, plan.domain, next)+ if err := renameJoinedServer(ctx, app, e, inst, saved, next, plan); err != nil {+ return nil, err+ }+ }+}++func renameJoinedServer(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, saved *instance.VoiceServer, next string, plan *joinPlan) error {+ old := *saved+ if err := removeVoiceServerProject(ctx, e, &old); err != nil {+ return err+ }+ unrouteVoiceServer(ctx, app, e, inst, &old)+ _ = os.RemoveAll(voice.ServerDir(inst.Dir, old.ID))+ saved.ID = next+ saved.Key = voice.Key(next)+ saved.Project = voice.JoinedProject(next)+ if saved.Edge != voice.EdgeNone {+ saved.Endpoint = voice.Endpoint(plan.address, next)+ }+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ if err := writeVoiceServer(inst, saved); err != nil {+ return err+ }+ return app.UI.Step(ctx, "Starting voice server "+next, func(ctx context.Context, s *ui.Step) error {+ if err := startVoiceServer(ctx, app, e, inst, saved, s); err != nil {+ return err+ }+ if err := routeVoiceServer(ctx, app, e, inst, saved); err != nil {+ return err+ }+ if saved.Edge != voice.EdgeNone {+ if err := waitForEndpoint(ctx, voice.HTTPBase(saved.Endpoint), "127.0.0.1:443", false, 2*time.Minute); err != nil {+ return err+ }+ }+ s.Done("Started voice server %s.", next)+ return nil+ })+}++func pinnedJoinError(err error, domain, id string) error {+ if code := voice.CodeOf(err); code == voice.CodeIDInUse || code == voice.CodeKeyInUse {+ return ui.Refused(fmt.Sprintf("%s already has a server called %s.", domain, id), "Delete it in the admin panel, or run rexctl voice token --id with another server ID for a new token.")+ }+ return joinError(err, domain, id)+}++func joinError(err error, domain, id string) error {+ var api *voice.APIError+ if !errors.As(err, &api) {+ return ui.Wrap(err, fmt.Sprintf("rexctl couldn't connect to %s.", domain), "Check the domain and this machine's internet connection, then try again.")+ }+ detail := ""+ if api.Detail != "" {+ detail = " (" + api.Detail + ")"+ }+ switch api.Code {+ case voice.CodeTokenInvalid:+ return ui.Refused("The join token doesn't work. It may be used, expired or revoked.", "Run rexctl voice token on the Fluxer machine for a new one.")+ case voice.CodeTokenBusy:+ return ui.Refused("Another machine is joining with this token right now.", "Wait two minutes, then try again.")+ case voice.CodeEndpointUnreachable:+ return ui.Failed(fmt.Sprintf("%s couldn't connect to the server's endpoint%s.", domain, detail), "Check that ports 80 and 443 are open in your provider's firewall, then run the same command again.")+ case voice.CodeEndpointInvalid:+ if host, ok := strings.CutPrefix(api.Detail, "The endpoint must use the hostname "); ok {+ return ui.Refused(fmt.Sprintf("%s accepts only the hostname %s for this server.", domain, host), fmt.Sprintf("Point %s at this machine, then use it.", host))+ }+ return ui.Refused(fmt.Sprintf("%s doesn't accept the server's endpoint%s.", domain, detail), "Use a hostname that resolves to a public address, or the machine's public IPv4 address.")+ case voice.CodeKeyMismatch:+ return ui.Failed("The voice server rejected its own key.", fmt.Sprintf("Run rexctl voice remove %s, then join again.", id))+ case voice.CodeIDInUse, voice.CodeKeyInUse:+ return ui.Refused(fmt.Sprintf("%s already has a server with this ID or key.", domain), "Pass --id with another ID.")+ case voice.CodeUnknownRegion:+ return ui.Refused("That region doesn't exist on the instance.", "Leave out --region, or create the region with a new token.")+ }+ return ui.Failed(fmt.Sprintf("%s refused the request (%s).", domain, api.Error()), "Run the same command again, or ask the instance's admin.")+}++func writeVoiceSysctl(ctx context.Context, app *App) {+ if runtime.GOOS != "linux" {+ return+ }+ if euid() != 0 {+ if voice.ReadSysctl("net.core.rmem_max") != "16777216" || voice.ReadSysctl("net.core.wmem_max") != "16777216" {+ showVoiceSysctlCommands(app)+ }+ return+ }+ if data, err := os.ReadFile(voiceSysctlFile); err != nil || string(data) != voiceSysctl {+ if err := os.WriteFile(voiceSysctlFile, []byte(voiceSysctl), 0o644); err != nil {+ app.UI.Detail("writing %s: %v", voiceSysctlFile, err)+ return+ }+ }+ if voice.ReadSysctl("net.core.rmem_max") == "16777216" && voice.ReadSysctl("net.core.wmem_max") == "16777216" {+ return+ }+ if err := runQuietCommand(ctx, "sysctl", "-p", voiceSysctlFile); err != nil {+ app.UI.Detail("loading %s: %v", voiceSysctlFile, err)+ }+}++func showVoiceSysctlCommands(app *App) {+ app.UI.Info("LiveKit needs larger UDP buffers for many calls, and only root can raise them. Run these commands:")+ app.UI.Info(" printf '%s' | sudo tee %s", strings.ReplaceAll(voiceSysctl, "\n", `\n`), voiceSysctlFile)+ app.UI.Info(" sudo sysctl -p %s", voiceSysctlFile)+}++func ensureVoiceEdge(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, s *ui.Step) error {+ dir := inst.Path(voice.EdgeDir)+ if err := os.MkdirAll(filepath.Join(dir, voice.SitesDir), 0o755); err != nil {+ return err+ }+ set := voiceSet(nil)+ for _, name := range []string{voice.EdgeComposeFile, voice.EdgeCaddyfile} {+ data, err := voice.ShippedFile(set, name)+ if err != nil {+ return err+ }+ if err := os.WriteFile(filepath.Join(dir, name), data, 0o644); err != nil {+ return err+ }+ }+ if err := writeVoiceSites(inst); err != nil {+ return err+ }+ p, err := e.Load(ctx, voiceEdgeSpec(inst))+ if err != nil {+ return engineFailure(err)+ }+ if err := p.Pull(ctx, app.progress(s, "Starting")); err != nil {+ return engineFailure(err)+ }+ if err := p.Up(context.WithoutCancel(ctx), engine.UpOptions{Wait: true, WaitTimeout: deploy.VoiceStartTimeout}); err != nil {+ return ui.Wrap(engineFailure(err), "The voice edge didn't start.", "Run rexctl voice status to see why.")+ }+ return nil+}++func voiceEdgeSpec(inst *instance.Instance) engine.Spec {+ env := map[string]string{"FLUXER_VOICE_NETWORK": voice.MachineNetwork}+ if inst.State.VoiceEdge != nil && inst.State.VoiceEdge.Email != "" {+ env["FLUXER_VOICE_EDGE_EMAIL"] = inst.State.VoiceEdge.Email+ }+ for _, s := range inst.State.Voice {+ if u, err := url.Parse(s.Endpoint); err == nil && s.Edge == voice.EdgeOwn && net.ParseIP(u.Hostname()) != nil {+ env["FLUXER_VOICE_EDGE_IP"] = u.Hostname()+ }+ }+ return engine.Spec{Name: voice.EdgeProject, Dir: inst.Path(voice.EdgeDir), Files: []string{voice.EdgeComposeFile}, Env: env}+}++func writeVoiceSites(inst *instance.Instance) error {+ dir := inst.Path(voice.EdgeDir, voice.SitesDir)+ byAddress := map[string][]string{}+ for _, s := range inst.State.Voice {+ if s.Edge != voice.EdgeOwn {+ continue+ }+ u, err := url.Parse(s.Endpoint)+ if err != nil {+ continue+ }+ byAddress[u.Hostname()] = append(byAddress[u.Hostname()], s.ID)+ }+ entries, _ := os.ReadDir(dir)+ for _, ent := range entries {+ keep := false+ for address := range byAddress {+ if ent.Name() == voice.SiteFileName(address) {+ keep = true+ }+ }+ if !keep {+ _ = os.Remove(filepath.Join(dir, ent.Name()))+ }+ }+ for address, ids := range byAddress {+ ip := strings.Trim(address, "0123456789.") == ""+ if err := os.WriteFile(filepath.Join(dir, voice.SiteFileName(address)), voice.SiteFile(address, ids, ip), 0o644); err != nil {+ return err+ }+ }+ return nil+}++func routeVoiceServer(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer) error {+ switch srv.Edge {+ case voice.EdgeOwn:+ if err := writeVoiceSites(inst); err != nil {+ return err+ }+ return reloadCaddy(ctx, e.Named(voice.EdgeProject, inst.Path(voice.EdgeDir)), "edge", "/etc/caddy/Caddyfile")+ case voice.EdgeInstance:+ target := inst+ if inst.State.IsVoice() && inst.State.VoiceEdge != nil && inst.State.VoiceEdge.Instance != "" {+ other, err := instance.Find(inst.State.VoiceEdge.Instance)+ if err != nil {+ return err+ }+ target = other+ }+ path := filepath.Join(target.Path(instance.CaddyDropInDir), voice.DropInName(srv.ID))+ if err := os.WriteFile(path, voice.DropIn(srv.ID), 0o644); err != nil {+ return err+ }+ 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)+ return nil+}++func unrouteVoiceServer(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer) {+ switch srv.Edge {+ case voice.EdgeOwn:+ if err := writeVoiceSites(inst); err == nil {+ _ = reloadCaddy(ctx, e.Named(voice.EdgeProject, inst.Path(voice.EdgeDir)), "edge", "/etc/caddy/Caddyfile")+ }+ case voice.EdgeInstance:+ target := inst+ if inst.State.IsVoice() && inst.State.VoiceEdge != nil && inst.State.VoiceEdge.Instance != "" {+ other, err := instance.Find(inst.State.VoiceEdge.Instance)+ if err != nil {+ app.UI.Detail("%v", err)+ return+ }+ target = other+ }+ _ = os.Remove(filepath.Join(target.Path(instance.CaddyDropInDir), voice.DropInName(srv.ID)))+ if err := reloadCaddy(ctx, e.Named(target.Project(), target.Dir), "edge", "/etc/caddy/Caddyfile"); err != nil {+ app.UI.Detail("reloading the edge: %v", err)+ }+ }+}++func waitForPublicEndpoint(ctx context.Context, endpoint string, limit time.Duration) error {+ u, err := url.Parse(endpoint)+ if err != nil {+ return err+ }+ return waitForEndpoint(ctx, voice.HTTPBase(endpoint), u.Host+portSuffix(u), false, limit)+}++func portSuffix(u *url.URL) string {+ if u.Port() != "" {+ return ""+ }+ return ":443"+}++type originState int++const (+ originUnknown originState = iota+ originUnlisted+ originPending+ originAllowed+)++func voiceOriginState(ctx context.Context, client *voice.Client, domain, origin string) originState {+ disc, err := client.Discover(ctx, domain)+ switch {+ case err != nil:+ return originUnknown+ case !disc.ListsOrigins:+ return originUnlisted+ case !slices.Contains(disc.VoiceOrigins, origin):+ return originPending+ }+ if ok, err := client.AllowsOrigin(ctx, domain, origin); err == nil && ok {+ return originAllowed+ }+ return originPending+}++func endpointOrigin(endpoint string) string {+ u, err := url.Parse(endpoint)+ if err != nil || u.Host == "" {+ return ""+ }+ return u.Scheme + "://" + u.Host+}++func waitForVoiceOrigin(ctx context.Context, app *App, client *voice.Client, domain, endpoint string) {+ origin := endpointOrigin(endpoint)+ if origin == "" {+ return+ }+ state := originUnknown+ deadline := time.Now().Add(3 * time.Minute)+ for time.Now().Before(deadline) {+ if state = voiceOriginState(ctx, client, domain, origin); state == originAllowed {+ app.UI.OK("Browsers on %s can connect to %s.", domain, origin)+ return+ }+ select {+ case <-ctx.Done():+ return+ case <-time.After(5 * time.Second):+ }+ }+ app.UI.Warn(fmt.Sprintf("Browsers on %s can't connect to %s yet.", domain, origin), originFix(state, domain, app.Version))+}++func originFix(state originState, domain, version string) string {+ if state == originUnlisted {+ target := "the latest release"+ if release.Valid(version) {+ target = version+ }+ return fmt.Sprintf("Upgrade Fluxer on %s to %s, then reload open pages.", domain, target)+ }+ return "Check again in a minute with rexctl voice status. Pages opened before then need a reload."+}diff --git a/fluxer_rexctl/cmd/voice_join_test.go b/fluxer_rexctl/cmd/voice_join_test.gonew file mode 100644index 000000000..e28c1893c--- /dev/null+++ b/fluxer_rexctl/cmd/voice_join_test.go@@ -0,0 +1,152 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "context"+ "errors"+ "strings"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"+)++func TestJoinInstallsDockerOnBareLinux(t *testing.T) {+ unreachable := engineFailure(&engine.Error{Op: "connect", Class: engine.ClassUnreachable})+ denied := engineFailure(&engine.Error{Op: "connect", Class: engine.ClassDenied})+ cases := []struct {+ name string+ err error+ goos string+ dryRun bool+ want bool+ }{+ {"no engine on linux", unreachable, "linux", false, true},+ {"dry run", unreachable, "linux", true, false},+ {"macOS", unreachable, "darwin", false, false},+ {"permission denied", denied, "linux", false, false},+ {"cancelled", context.Canceled, "linux", false, false},+ }+ for _, c := range cases {+ if got := joinInstallsDocker(c.err, c.goos, c.dryRun); got != c.want {+ t.Errorf("%s: got %v, want %v", c.name, got, c.want)+ }+ }+}++func TestVoiceServersFollowThePinnedLiveKitImage(t *testing.T) {+ inst := &instance.Instance{Dir: t.TempDir(), State: &instance.State{+ Settings: map[string]string{},+ Images: instance.Images{Release: "2026.1030.2", Refs: map[string]string{"livekit": "livekit/livekit-server:v1.13.11"}},+ }}+ if got := instanceLiveKitImage(inst); got != "livekit/livekit-server:v1.13.11" {+ t.Errorf("pinned: got %s", got)+ }+ inst.State.Settings["images.livekit"] = "registry.example.com/livekit:v1"+ if got := instanceLiveKitImage(inst); got != "registry.example.com/livekit:v1" {+ t.Errorf("operator setting: got %s", got)+ }+}++func TestUpgradeLeavesOffAndDrainingServersAsTheyWere(t *testing.T) {+ cases := []struct {+ active, draining, want bool+ }{+ {true, false, true},+ {false, false, false},+ {true, true, false},+ {false, true, false},+ }+ for _, c := range cases {+ if got := upgradeDrains(c.active, c.draining); got != c.want {+ t.Errorf("active %v, draining %v: got %v", c.active, c.draining, got)+ }+ }+}++func TestJoinTakesIPForAShared100Dot64Route(t *testing.T) {+ local := []string{"100.64.12.7"}+ _, err := assessJoin("203.0.113.9", "", local, "100.64.12.7")+ var ee *ui.ExitError+ if !errors.As(err, &ee) || !strings.Contains(ee.Fix, "--ip") {+ t.Fatalf("without --ip: %v", err)+ }+ reach, err := assessJoin("203.0.113.9", "203.0.113.9", local, "100.64.12.7")+ if err != nil {+ t.Fatalf("with --ip: %v", err)+ }+ if reach.CGNAT || !reach.BehindNAT || reach.PublicIPv4 != "203.0.113.9" {+ t.Fatalf("reach = %+v", reach)+ }+ if bindAddress("203.0.113.9", local) != "" {+ t.Fatal("bound to an address this machine doesn't have")+ }+}++func TestNewRegionsNeedCoordinates(t *testing.T) {+ app, _ := testVoiceApp()+ for _, args := range [][]string{+ {"--new-region", "fra", "--region-name", "Frankfurt"},+ {"--new-region", "fra", "--region-name", "Frankfurt", "--lat", "50.1"},+ {"--new-region", "fra", "--region-name", "Frankfurt", "--lat", "95", "--long", "8.7"},+ } {+ c := newVoiceAddCmd(app)+ c.SetArgs(args)+ c.SilenceUsage, c.SilenceErrors = true, true+ err := c.Execute()+ var ee *ui.ExitError+ if !errors.As(err, &ee) || ee.Code != ui.ExitUsage || !strings.Contains(ee.Msg, "--lat") {+ t.Errorf("%v: %v", args, err)+ }+ }+ c := newVoiceTokenCmd(app)+ c.SetArgs([]string{"--region", "fra", "--lat", "50.1", "--long", "8.7"})+ c.SilenceUsage, c.SilenceErrors = true, true+ if err := c.Execute(); err == nil || !strings.Contains(err.Error(), "--new-region") {+ t.Errorf("token coordinates without a new region: %v", err)+ }+}++func TestVoiceListShowsRegionsWithoutServers(t *testing.T) {+ inst := &instance.Instance{State: &instance.State{Settings: map[string]string{}}}+ regions := []adminapi.VoiceRegion{+ {VoiceRegionAdminResponse: adminapi.VoiceRegionAdminResponse{Id: "default"}, Servers: []adminapi.VoiceServerAdminResponse{{ServerId: "default-server-1", IsActive: true}}},+ {VoiceRegionAdminResponse: adminapi.VoiceRegionAdminResponse{Id: "fra"}},+ }+ rows, servers := voiceListRows(regions, inst)+ if servers != 1 || len(rows) != 2 || rows[1][0] != "-" || rows[1][1] != "fra" || rows[1][2] != "no servers" {+ t.Fatalf("rows = %v, servers = %d", rows, servers)+ }+}++func TestJoinRefusesOlderPodmanWithoutAFluxerInstance(t *testing.T) {+ old := &engine.Engine{Kind: engine.Podman, Version: "5.4.2"}+ if err := podmanJoinRefusal(old, &joinPlan{}); err == nil {+ t.Fatal("Podman 5.4.2 on a voice machine was allowed")+ }+ if err := podmanJoinRefusal(old, &joinPlan{edgeInst: &instance.Instance{}}); err != nil {+ t.Fatalf("a server next to Fluxer was refused: %v", err)+ }+ if err := podmanJoinRefusal(&engine.Engine{Kind: engine.Podman, Version: podmanRestartsContainers}, &joinPlan{}); err != nil {+ t.Fatalf("Podman %s was refused: %v", podmanRestartsContainers, err)+ }+ if err := podmanJoinRefusal(&engine.Engine{Kind: engine.Docker, Version: "27.0.0"}, &joinPlan{}); err != nil {+ t.Fatalf("Docker was refused: %v", err)+ }+}++func TestSysctlCommandsMatchTheComposePage(t *testing.T) {+ app, out := testVoiceApp()+ showVoiceSysctlCommands(app)+ for _, want := range []string{+ `printf 'net.core.rmem_max = 16777216\nnet.core.wmem_max = 16777216\n' | sudo tee /etc/sysctl.d/61-fluxer-voice.conf`,+ "sudo sysctl -p /etc/sysctl.d/61-fluxer-voice.conf",+ } {+ if !strings.Contains(out.String(), want) {+ t.Fatalf("missing %q in %s", want, out.String())+ }+ }+}diff --git a/fluxer_rexctl/cmd/voice_lifecycle.go b/fluxer_rexctl/cmd/voice_lifecycle.gonew file mode 100644index 000000000..52d570294--- /dev/null+++ b/fluxer_rexctl/cmd/voice_lifecycle.go@@ -0,0 +1,377 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "bytes"+ "context"+ "errors"+ "fmt"+ "net/http"+ "os"+ "path/filepath"+ "runtime"+ "sync"+ "time"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/release"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++var startUpgradedVoiceServer = startVoiceServer++func upgradeVoiceServer(ctx context.Context, app *App, e *engine.Engine, api *adminapi.Client, inst *instance.Instance, srv *instance.VoiceServer, image string, drain time.Duration) error {+ active, draining, err := voiceServerFlags(ctx, api, inst, srv)+ if err != nil {+ app.UI.Detail("reading the state of %s: %v", srv.ID, err)+ }+ if err == nil && !draining {+ srv.Drained = false+ }+ drains := err == nil && draining && srv.Drained+ if err == nil && upgradeDrains(active, draining) {+ drains = startUpgradeDrain(ctx, app, api, inst, srv)+ }+ if drains {+ defer endUpgradeDrain(ctx, app, api, inst, srv)+ }+ title, done := "Upgrading voice server "+srv.ID, fmt.Sprintf("Upgraded voice server %s to %s.", srv.ID, image)+ if srv.Image == image {+ title, done = "Restarting voice server "+srv.ID, fmt.Sprintf("Restarted voice server %s.", srv.ID)+ }+ next := *srv+ next.Image = image+ if err := writeVoiceServer(inst, &next); err != nil {+ return err+ }+ grace := drain+ if grace <= 0 {+ grace = defaultDrainTimeout+ }+ err = app.UI.Step(ctx, title, func(ctx context.Context, s *ui.Step) error {+ s.Info("LiveKit waits until the calls on %s end, for up to %s.", srv.ID, grace)+ if err := startUpgradedVoiceServer(ctx, app, e, inst, &next, s); err != nil {+ return err+ }+ s.Done("%s", done)+ return nil+ })+ if err != nil {+ return err+ }+ srv.Image = image+ if err := saveVoiceState(inst); err != nil {+ return err+ }+ if srv.Edge != voice.EdgeNone {+ base, dial := voiceProbeTarget(inst, srv)+ _ = waitForEndpoint(ctx, base, dial, true, 2*time.Minute)+ }+ return nil+}++func startUpgradeDrain(ctx context.Context, app *App, api *adminapi.Client, inst *instance.Instance, srv *instance.VoiceServer) bool {+ srv.Drained = true+ if err := saveVoiceState(inst); err != nil {+ srv.Drained = false+ app.UI.Warn(fmt.Sprintf("Couldn't mark %s as draining (%v).", srv.ID, err), "")+ return false+ }+ if err := setVoiceDraining(ctx, api, inst, srv, true); err != nil {+ srv.Drained = false+ _ = saveVoiceState(inst)+ app.UI.Warn(fmt.Sprintf("Couldn't mark %s as draining (%v).", srv.ID, err), "")+ return false+ }+ return true+}++func endUpgradeDrain(ctx context.Context, app *App, api *adminapi.Client, inst *instance.Instance, srv *instance.VoiceServer) {+ if err := setVoiceDraining(context.WithoutCancel(ctx), api, inst, srv, false); err != nil {+ app.UI.Warn(fmt.Sprintf("%s is still draining (%v).", srv.ID, err), "Run rexctl upgrade again, or clear Draining for it in the admin panel.")+ return+ }+ srv.Drained = false+ if err := saveVoiceState(inst); err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't record that %s takes calls again (%v).", srv.ID, err), "")+ }+}++func voiceServerFlags(ctx context.Context, api *adminapi.Client, inst *instance.Instance, srv *instance.VoiceServer) (bool, bool, error) {+ if inst.State.IsVoice() {+ client := &voice.Client{HTTP: &http.Client{Timeout: 30 * time.Second}}+ node, err := client.GetNode(ctx, srv.NodeURL, srv.Key, srv.Secret)+ if err != nil {+ return false, false, err+ }+ return node.IsActive, node.Draining, nil+ }+ if api == nil {+ return false, false, errors.New("no admin API")+ }+ regions, err := api.VoiceRegions(ctx)+ if err != nil {+ return false, false, err+ }+ for _, r := range regions {+ for _, s := range r.Servers {+ if s.ServerId == srv.ID {+ return s.IsActive, s.Draining, nil+ }+ }+ }+ return false, false, fmt.Errorf("the instance has no voice server called %s", srv.ID)+}++func upgradeDrains(active, draining bool) bool {+ return active && !draining+}++func voiceTargetImage(srv *instance.VoiceServer, image string) string {+ if srv.Pinned {+ return srv.Image+ }+ return image+}++func voiceServerStale(inst *instance.Instance, srv *instance.VoiceServer, image string) bool {+ if srv.Image != image {+ return true+ }+ dir := voice.ServerDir(inst.Dir, srv.ID)+ set := voiceSet(inst)+ for _, name := range []string{voice.ComposeFile, voice.HTTPComposeFile} {+ want, err := voice.ShippedFile(set, name)+ if err != nil {+ return false+ }+ have, err := os.ReadFile(filepath.Join(dir, name))+ if err != nil || !bytes.Equal(have, want) {+ return true+ }+ }+ have, err := os.ReadFile(filepath.Join(dir, voice.EnvFile))+ return err != nil || !bytes.Equal(have, deploy.VoiceEnv(inst, srv).File())+}++func upgradeLocalVoiceServers(ctx context.Context, app *App, inst *instance.Instance, e *engine.Engine) {+ if len(inst.State.Voice) > 0 {+ image := instanceLiveKitImage(inst)+ api := app.adminClient(inst, e.Named(inst.Project(), inst.Dir))+ for i := range inst.State.Voice {+ srv := &inst.State.Voice[i]+ target := voiceTargetImage(srv, image)+ if !voiceServerStale(inst, srv, target) {+ continue+ }+ if err := upgradeVoiceServer(ctx, app, e, api, inst, srv, target, defaultDrainTimeout); err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't upgrade voice server %s (%v).", srv.ID, err), "Run rexctl voice status "+srv.ID+" to see why.")+ }+ }+ }+ for _, other := range voiceMachinesUsing(inst) {+ err := app.Locked(ctx, other, func() error {+ for _, srv := range staleVoiceServers(other, ownLiveKitImage()) {+ if err := upgradeVoiceServer(ctx, app, e, nil, other, srv, voiceTargetImage(srv, ownLiveKitImage()), defaultDrainTimeout); err != nil {+ return err+ }+ }+ return nil+ })+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't upgrade the voice servers in %s (%v).", other.Dir, err), "Run rexctl upgrade --dir "+other.Dir+".")+ }+ }+}++func staleVoiceServers(inst *instance.Instance, image string) []*instance.VoiceServer {+ var out []*instance.VoiceServer+ for i := range inst.State.Voice {+ srv := &inst.State.Voice[i]+ if voiceServerStale(inst, srv, voiceTargetImage(srv, image)) {+ out = append(out, srv)+ }+ }+ return out+}++func voiceMachinesUsing(inst *instance.Instance) []*instance.Instance {+ var out []*instance.Instance+ for _, dir := range instance.ReadRecord(instance.OwnScope()).Instances {+ other, err := instance.Find(dir)+ if err == nil && other.State.IsVoice() && other.State.VoiceEdge != nil && other.State.VoiceEdge.Instance == inst.Dir {+ out = append(out, other)+ }+ }+ return out+}++func instanceRelease(ctx context.Context, domain string) (string, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+domain+"/.well-known/fluxer", nil)+ if err != nil {+ return "", err+ }+ resp, err := (&http.Client{Timeout: 20 * time.Second}).Do(req)+ if err != nil {+ return "", err+ }+ defer resp.Body.Close()+ if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotModified {+ return "", fmt.Errorf("voice: %s answered %d", domain, resp.StatusCode)+ }+ return resp.Header.Get("X-Fluxer-Version"), nil+}++func runVoiceMachineUpgrade(ctx context.Context, app *App, inst *instance.Instance, dryRun bool) error {+ if len(inst.State.Voice) == 0 {+ app.UI.OK("This machine runs no voice servers.")+ return nil+ }+ domain := inst.State.Voice[0].Instance+ target, err := instanceRelease(ctx, domain)+ if err != nil {+ return ui.Wrap(err, fmt.Sprintf("rexctl can't connect to %s. Nothing changed.", domain), "Run rexctl upgrade again once the instance answers.")+ }+ if release.Valid(target) && release.Valid(app.Version) && release.Compare(target, app.Version) > 0 {+ rel, err := resolveRelease(ctx, target)+ if err != nil {+ return err+ }+ if dryRun {+ app.UI.Info("rexctl would update itself to %s, the release %s runs, then upgrade the voice servers.", target, domain)+ return nil+ }+ asset, err := rel.AssetFor(runtime.GOOS, runtime.GOARCH)+ if err != nil {+ return ui.Failed(fmt.Sprintf("There's no rexctl %s for this machine.", target), "")+ }+ if err := app.UI.Step(ctx, "Updating rexctl", func(ctx context.Context, s *ui.Step) error {+ if err := selfUpdate(ctx, asset); err != nil {+ return err+ }+ s.Done("Updated rexctl to %s.", target)+ return nil+ }); err != nil {+ return ui.Wrap(err, "Couldn't update rexctl.", "Run rexctl upgrade again.")+ }+ if err := reexec(os.Args[1:]); err != nil {+ return ui.Wrap(err, "Updated rexctl but couldn't start it again.", "Run rexctl upgrade again.")+ }+ return nil+ }+ image := ownLiveKitImage()+ if release.Valid(target) && release.Valid(app.Version) && release.Compare(target, app.Version) < 0 {+ app.UI.Info("%s runs %s, older than this rexctl. Voice servers stay on their LiveKit release until the instance catches up.", domain, target)+ return nil+ }+ pending := staleVoiceServers(inst, image)+ if dryRun {+ for _, s := range pending {+ app.UI.Info("rexctl would drain %s and start it again with %s.", s.ID, voiceTargetImage(s, image))+ }+ if len(pending) == 0 {+ app.UI.OK("Every voice server on this machine is up to date.")+ }+ return nil+ }+ e, err := app.Engine(ctx, inst)+ if err != nil {+ return err+ }+ return app.Locked(ctx, inst, func() error {+ if inst.State.VoiceEdge != nil && inst.State.VoiceEdge.Instance == "" {+ if err := app.UI.Step(ctx, "Updating the voice edge", func(ctx context.Context, s *ui.Step) error {+ if err := ensureVoiceEdge(ctx, app, e, inst, s); err != nil {+ return err+ }+ if err := reloadCaddy(ctx, e.Named(voice.EdgeProject, inst.Path(voice.EdgeDir)), "edge", "/etc/caddy/Caddyfile"); err != nil {+ return engineFailure(err)+ }+ s.Done("Updated the voice edge.")+ return nil+ }); err != nil {+ return err+ }+ }+ for _, srv := range pending {+ if err := upgradeVoiceServer(ctx, app, e, nil, inst, srv, voiceTargetImage(srv, image), defaultDrainTimeout); err != nil {+ return err+ }+ }+ if len(pending) == 0 {+ app.UI.OK("Every voice server on this machine is up to date.")+ }+ return nil+ })+}++func runVoiceMachineUninstall(ctx context.Context, app *App, inst *instance.Instance) error {+ _, servers, _ := localVoiceServers(inst, nil)+ if len(servers) == 0 {+ e, err := app.Engine(ctx, inst)+ if err != nil {+ return err+ }+ return app.Locked(ctx, inst, func() error { return removeVoiceMachine(ctx, app, e, inst) })+ }+ return removeVoiceServers(ctx, app, inst, servers, defaultDrainTimeout, true)+}++func removeLocalVoiceServersForUninstall(ctx context.Context, app *App, inst *instance.Instance, e *engine.Engine) {+ eachVoiceServer(app, inst, "removing", func(srv *instance.VoiceServer) error {+ return removeVoiceServerProject(ctx, e, srv)+ })+ for _, srv := range inst.State.Voice {+ closeVoiceFirewall(ctx, app, srv.Opened)+ }+ for _, other := range voiceMachinesUsing(inst) {+ for _, s := range other.State.Voice {+ app.UI.Warn(fmt.Sprintf("The voice server %s on this machine used this instance's edge and stops working.", s.ID), "Run rexctl voice remove "+s.ID+voiceDirFlag(other.Dir)+".")+ }+ }+}++func eachVoiceServer(app *App, inst *instance.Instance, verb string, fn func(srv *instance.VoiceServer) error) {+ errs := make([]error, len(inst.State.Voice))+ var wg sync.WaitGroup+ for i := range inst.State.Voice {+ wg.Go(func() { errs[i] = fn(&inst.State.Voice[i]) })+ }+ wg.Wait()+ for i, err := range errs {+ if err != nil {+ app.UI.Detail("%s voice server %s: %v", verb, inst.State.Voice[i].ID, err)+ }+ }+}++func voiceDirFlag(dir string) string {+ if l, err := instance.Resolve(""); err == nil && l.Dir == dir {+ return ""+ }+ return " --dir " + dir+}++func startLocalVoiceServers(ctx context.Context, app *App, inst *instance.Instance, e *engine.Engine) {+ for _, v := range deploy.VoiceOnNetwork(inst) {+ srv := &v.Server+ p, err := e.Load(ctx, deploy.VoiceSpec(v.Owner, srv))+ if err == nil {+ err = p.Up(context.WithoutCancel(ctx), engine.UpOptions{Wait: true, WaitTimeout: deploy.VoiceStartTimeout})+ }+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Voice server %s didn't start (%v).", srv.ID, engineFailure(err)), "Run rexctl voice status "+srv.ID+" to see why.")+ }+ }+}++func stopLocalVoiceServers(ctx context.Context, app *App, inst *instance.Instance, e *engine.Engine) {+ eachVoiceServer(app, inst, "stopping", func(srv *instance.VoiceServer) error {+ return voiceProject(e, inst, srv).StopWithin(context.WithoutCancel(ctx), deploy.VoiceStopTimeout)+ })+}diff --git a/fluxer_rexctl/cmd/voice_lifecycle_test.go b/fluxer_rexctl/cmd/voice_lifecycle_test.gonew file mode 100644index 000000000..0e396dd71--- /dev/null+++ b/fluxer_rexctl/cmd/voice_lifecycle_test.go@@ -0,0 +1,456 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "bytes"+ "context"+ "encoding/json"+ "errors"+ "net"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "strings"+ "sync"+ "testing"+ "time"++ "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/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++func testVoiceApp() (*App, *bytes.Buffer) {+ out := &bytes.Buffer{}+ return &App{UI: ui.New(ui.Options{Stdin: strings.NewReader(""), Stdout: out, Stderr: out})}, out+}++func voiceMachine(t *testing.T, servers ...instance.VoiceServer) *instance.Instance {+ t.Helper()+ dir := resolved(t, t.TempDir())+ inst := &instance.Instance{Dir: dir, State: &instance.State{+ Schema: instance.StateSchema, Kind: instance.KindVoice, Project: voice.MachineProject,+ Settings: map[string]string{}, Secrets: map[string]string{}, Voice: servers,+ }}+ if err := os.MkdirAll(inst.Path(instance.StateDirName), 0o700); err != nil {+ t.Fatal(err)+ }+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ if err := instance.Remember(instance.OwnScope(), dir, false); err != nil {+ t.Fatal(err)+ }+ return inst+}++func TestRemovingAVoiceMachineTurnsOffAutoUpgrade(t *testing.T) {+ clearRecords(t)+ inst := voiceMachine(t)+ var removed []string+ removeAutoUpgradeFor = func(_ context.Context, inst *instance.Instance, _ *engine.Engine) (bool, error) {+ removed = append(removed, inst.Dir)+ return true, nil+ }+ t.Cleanup(func() { removeAutoUpgradeFor = removeAutoUpgrade })+ app, _ := testVoiceApp()+ if err := removeVoiceMachine(context.Background(), app, nil, inst); err != nil {+ t.Fatal(err)+ }+ if len(removed) != 1 || removed[0] != inst.Dir {+ t.Fatalf("auto-upgrade removed for %v, want %s", removed, inst.Dir)+ }+ if _, err := os.Stat(inst.Dir); !os.IsNotExist(err) {+ t.Fatalf("%s is still there", inst.Dir)+ }+}++func TestAJoinedServerNextToFluxerIsFoundWithoutDir(t *testing.T) {+ clearRecords(t)+ fluxer, err := instance.Create(resolved(t, t.TempDir()), "fluxer")+ if err != nil {+ t.Fatal(err)+ }+ if err := fluxer.Save(); err != nil {+ t.Fatal(err)+ }+ if err := instance.Remember(instance.OwnScope(), fluxer.Dir, true); err != nil {+ t.Fatal(err)+ }+ machine := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", Edge: voice.EdgeInstance})+ machine.State.VoiceEdge = &instance.VoiceEdge{Instance: fluxer.Dir}+ if err := machine.Save(); err != nil {+ t.Fatal(err)+ }+ owner, srv := findVoiceServer(fluxer, "fra-server-1")+ if srv == nil || owner.Dir != machine.Dir {+ t.Fatalf("findVoiceServer found %v in %s", srv, owner.Dir)+ }+ if _, _, err := localVoiceServers(fluxer, []string{"fra-server-9"}); err == nil {+ t.Fatal("an unknown ID was found")+ }+ if got := voiceDirFlag(machine.Dir); got != " --dir "+machine.Dir {+ t.Fatalf("voiceDirFlag = %q", got)+ }+ if got := voiceDirFlag(fluxer.Dir); got != "" {+ t.Fatalf("voiceDirFlag for the default = %q", got)+ }+ using := voiceMachinesUsing(fluxer)+ if len(using) != 1 || using[0].Dir != machine.Dir {+ t.Fatalf("voiceMachinesUsing = %v", using)+ }+}++func TestVoiceServerStaleFollowsTheShippedFiles(t *testing.T) {+ srv := instance.VoiceServer{ID: "fra-server-1", Key: "fluxer-fra-server-1", Secret: strings.Repeat("a", 64), TCPPort: 7883, UDPPort: 7884, Image: "livekit/livekit-server:v1", Memory: "1g", Webhook: "https://chat.example.com/api/webhooks/livekit", Network: voice.MachineNetwork}+ inst := voiceMachine(t, srv)+ saved := &inst.State.Voice[0]+ if err := writeVoiceServer(inst, saved); err != nil {+ t.Fatal(err)+ }+ if voiceServerStale(inst, saved, saved.Image) {+ t.Fatal("a freshly written server is stale")+ }+ if !voiceServerStale(inst, saved, "livekit/livekit-server:v2") {+ t.Fatal("a new image isn't stale")+ }+ compose := filepath.Join(voice.ServerDir(inst.Dir, saved.ID), voice.ComposeFile)+ if err := os.WriteFile(compose, []byte("services: {}\n"), 0o644); err != nil {+ t.Fatal(err)+ }+ if !voiceServerStale(inst, saved, saved.Image) {+ t.Fatal("a changed compose file isn't stale")+ }+ saved.Pinned = true+ if got := voiceTargetImage(saved, "livekit/livekit-server:v2"); got != saved.Image {+ t.Fatalf("a pinned server moves to %s", got)+ }+}++func TestRestoreDropsServersMissingFromTheBackup(t *testing.T) {+ before := []instance.VoiceServer{{ID: "a", Project: "fluxer-voice-a"}, {ID: "b", Project: "fluxer-voice-b"}}+ after := []instance.VoiceServer{{ID: "a", Project: "fluxer-voice-a"}}+ got := droppedVoiceServers(before, after)+ if len(got) != 1 || got[0].ID != "b" {+ t.Fatalf("dropped %v", got)+ }+}++func TestBindAddressOnlyWhenTheMachineHasIt(t *testing.T) {+ local := []string{"10.0.0.5", "198.51.100.8"}+ if got := bindAddress("198.51.100.8", local); got != "198.51.100.8" {+ t.Errorf("a local address binds to %q", got)+ }+ if got := bindAddress("203.0.113.7", local); got != "" {+ t.Errorf("an address behind NAT binds to %q", got)+ }+}++func TestJoinFirewallText(t *testing.T) {+ if got := joinFirewallText(host.FirewallNone, 4, true); !strings.HasPrefix(got, "none found") {+ t.Errorf("no firewall: %s", got)+ }+ if got := joinFirewallText(host.Firewall("ufw"), 4, true); !strings.Contains(got, "left alone") {+ t.Errorf("--no-firewall: %s", got)+ }+ if got := joinFirewallText(host.Firewall("ufw"), 4, false); !strings.Contains(got, "will open 4 ports") {+ t.Errorf("ufw: %s", got)+ }+}++func TestTakenVoicePortsIncludeTheHTTPPort(t *testing.T) {+ clearRecords(t)+ inst := voiceMachine(t, instance.VoiceServer{ID: "b", TCPPort: 7883, UDPPort: 7884, HTTPPort: 7882, Edge: voice.EdgeNone})+ taken := takenVoicePorts(context.Background(), nil, inst)+ if !taken[host.Port{Number: 7882, Proto: "tcp"}] {+ t.Fatalf("the HTTP port of a stopped server is free: %v", taken)+ }+ if !instancePortsContain(instance.RecordedVoicePorts(), 7882, "tcp") {+ t.Fatal("the recorded voice ports miss the HTTP port")+ }+}++func instancePortsContain(ports []instance.VoicePort, number int, proto string) bool {+ for _, p := range ports {+ if p.Number == number && p.Proto == proto {+ return true+ }+ }+ return false+}++func TestDrainWaitDoesNotTreatAnUnreadableServerAsEmpty(t *testing.T) {+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ w.WriteHeader(http.StatusBadGateway)+ }))+ defer srv.Close()+ port := srv.Listener.Addr().(*net.TCPAddr).Port+ inst := &instance.Instance{Dir: t.TempDir(), State: &instance.State{Kind: instance.KindVoice}}+ server := &instance.VoiceServer{ID: "fra-server-1", Edge: voice.EdgeNone, HTTPPort: port, Endpoint: "wss://voice.example.com/livekit-fra-server-1", Key: "k", Secret: "s"}+ app, out := testVoiceApp()+ start := time.Now()+ waitForDrain(context.Background(), app, inst, server, time.Second)+ if time.Since(start) < time.Second {+ t.Fatal("the wait ended before its limit")+ }+ if strings.Contains(out.String(), "No calls left") || !strings.Contains(out.String(), "couldn't read the calls") {+ t.Fatalf("output: %s", out.String())+ }+}++func TestPickHTTPPortSkipsTheServersOwnTCPPort(t *testing.T) {+ taken := map[host.Port]bool{{Number: 7880, Proto: "tcp"}: true}+ free := func(int, string) bool { return true }+ if got := pickHTTPPort(taken, 7881, free); got != 7882 {+ t.Fatalf("pickHTTPPort = %d, want 7882", got)+ }+}++func TestInstallAfterAJoinUsesTheStandardFolder(t *testing.T) {+ clearRecords(t)+ machine := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", Instance: "chat.example.com"})+ app, out := testVoiceApp()+ dir, err := installTarget(app)+ if err != nil {+ t.Fatal(err)+ }+ want, err := instance.DefaultDir()+ if err != nil {+ t.Fatal(err)+ }+ if dir != want {+ t.Fatalf("install goes to %s, want %s", dir, want)+ }+ if !strings.Contains(out.String(), machine.Dir) {+ t.Fatalf("output: %s", out.String())+ }++ app, _ = testVoiceApp()+ app.Flags.Dir = machine.Dir+ _, err = installTarget(app)+ var exit *ui.ExitError+ if !errors.As(err, &exit) || exit.Code != ui.ExitRefused || !strings.Contains(exit.Msg, "chat.example.com") {+ t.Fatalf("install with --dir of a voice machine: %v", err)+ }+}++func TestBootStartStartsVoiceServersWhenFluxerIsAlreadyUp(t *testing.T) {+ clearRecords(t)+ inst, err := instance.Create(resolved(t, t.TempDir()), "fluxer")+ if err != nil {+ t.Fatal(err)+ }+ inst.State.Voice = []instance.VoiceServer{{ID: "fra-server-1", Network: "fluxer_fluxer"}}+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ app, _ := testVoiceApp()+ app.Flags.Dir = inst.Dir+ app.engine = &engine.Engine{Kind: engine.Podman}+ var started []string+ prevUp, prevStart := bootStackUp, startVoiceAtBoot+ t.Cleanup(func() { bootStackUp, startVoiceAtBoot = prevUp, prevStart })+ bootStackUp = func(context.Context, *App, *instance.Instance) bool { return true }+ startVoiceAtBoot = func(_ context.Context, _ *App, got *instance.Instance, _ *engine.Engine) {+ started = append(started, got.Dir)+ }+ if err := runBootStart(context.Background(), app); err != nil {+ t.Fatal(err)+ }+ if len(started) != 1 || started[0] != inst.Dir {+ t.Fatalf("voice servers started for %v", started)+ }++ started = nil+ inst.State.Stopped = true+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ if err := runBootStart(context.Background(), app); err != nil {+ t.Fatal(err)+ }+ if len(started) != 0 {+ t.Fatalf("a stopped instance started voice servers for %v", started)+ }+}++func TestVoiceCommandsRefuseAnEjectedInstance(t *testing.T) {+ clearRecords(t)+ inst, err := instance.Create(resolved(t, t.TempDir()), "fluxer")+ if err != nil {+ t.Fatal(err)+ }+ inst.State.Ejected = &instance.Ejected{At: time.Now()}+ inst.State.Voice = []instance.VoiceServer{{ID: "fra-server-1", Edge: voice.EdgeInstance}}+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ before, err := os.ReadFile(inst.StatePath())+ if err != nil {+ t.Fatal(err)+ }+ sites, _ := os.ReadDir(inst.Path("caddy.d"))+ refused := func(name string, err error) {+ t.Helper()+ var exit *ui.ExitError+ if !errors.As(err, &exit) || exit.Code != ui.ExitRefused || !strings.Contains(exit.Fix, "rexctl takeover") {+ t.Fatalf("%s: %v", name, err)+ }+ }+ app, _ := testVoiceApp()+ app.Flags.Dir = inst.Dir+ refused("voice add", runVoiceAdd(context.Background(), app, nil, &voiceFlags{}))+ refused("voice remove", removeVoiceServers(context.Background(), app, inst, []*instance.VoiceServer{&inst.State.Voice[0]}, 0, false))+ after, err := os.ReadFile(inst.StatePath())+ if err != nil {+ t.Fatal(err)+ }+ if !bytes.Equal(before, after) {+ t.Fatal("the state changed")+ }+ if now, _ := os.ReadDir(inst.Path("caddy.d")); len(now) != len(sites) {+ t.Fatalf("caddy.d went from %d to %d files", len(sites), len(now))+ }+}++func TestRemovingAJoinedServerThatNeverRegisteredSkipsTheInstance(t *testing.T) {+ clearRecords(t)+ inst := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", Instance: "chat.example.com", Edge: voice.EdgeOwn, Joined: true})+ app, out := testVoiceApp()+ if err := unregisterVoiceServer(context.Background(), app, nil, inst, &inst.State.Voice[0], time.Minute); err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(out.String(), "fra-server-1 never joined chat.example.com") {+ t.Fatalf("output: %s", out.String())+ }+}++func TestVoiceMachineUpgradeRefusesPrefetchAndTo(t *testing.T) {+ clearRecords(t)+ inst := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", Edge: voice.EdgeInstance})+ for _, f := range []*upgradeFlags{{Prefetch: true}, {To: "2026.1029.100000"}} {+ app, out := testVoiceApp()+ app.Flags.Dir = inst.Dir+ err := runUpgrade(context.Background(), app, f)+ var ee *ui.ExitError+ if !errors.As(err, &ee) || ee.Code != ui.ExitUsage || !strings.Contains(ee.Msg, "voice machine") {+ t.Fatalf("flags %+v gave %v", f, err)+ }+ if strings.Contains(out.String(), "drain") {+ t.Fatalf("flags %+v drained: %q", f, out.String())+ }+ }+}++type drainNode struct {+ mu sync.Mutex+ draining bool+ patches []bool+}++func (n *drainNode) serve(t *testing.T) string {+ t.Helper()+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ n.mu.Lock()+ defer n.mu.Unlock()+ if r.Method == http.MethodPatch {+ var update voice.NodeUpdate+ if err := json.NewDecoder(r.Body).Decode(&update); err == nil && update.Draining != nil {+ n.draining = *update.Draining+ n.patches = append(n.patches, *update.Draining)+ }+ }+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]any{"is_active": true, "draining": n.draining})+ }))+ t.Cleanup(srv.Close)+ return srv.URL+}++func stubUpgradedVoiceStart(t *testing.T, start func() error) {+ t.Helper()+ prev := startUpgradedVoiceServer+ t.Cleanup(func() { startUpgradedVoiceServer = prev })+ startUpgradedVoiceServer = func(context.Context, *App, *engine.Engine, *instance.Instance, *instance.VoiceServer, *ui.Step) error {+ return start()+ }+}++func TestFailedVoiceUpgradeClearsItsDrainAndStaysStale(t *testing.T) {+ clearRecords(t)+ node := &drainNode{}+ inst := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", NodeURL: node.serve(t), Key: "k", Secret: strings.Repeat("s", 64), Image: "livekit:old", Edge: voice.EdgeNone})+ stubUpgradedVoiceStart(t, func() error { return errors.New("pull failed") })+ app, _ := testVoiceApp()+ srv := &inst.State.Voice[0]+ if err := upgradeVoiceServer(context.Background(), app, nil, nil, inst, srv, "livekit:new", time.Second); err == nil {+ t.Fatal("the failed start gave no error")+ }+ if node.draining || len(node.patches) != 2 || !node.patches[0] || node.patches[1] {+ t.Fatalf("draining %v after patches %v", node.draining, node.patches)+ }+ saved, err := instance.Find(inst.Dir)+ if err != nil {+ t.Fatal(err)+ }+ if got := saved.State.Voice[0]; got.Image != "livekit:old" || got.Drained {+ t.Fatalf("saved server %+v", got)+ }+ if !voiceServerStale(saved, &saved.State.Voice[0], "livekit:new") {+ t.Fatal("the failed server reads as current")+ }+}++func TestVoiceUpgradeClearsADrainLeftByAnEarlierRun(t *testing.T) {+ clearRecords(t)+ node := &drainNode{draining: true}+ inst := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", NodeURL: node.serve(t), Key: "k", Secret: strings.Repeat("s", 64), Image: "livekit:old", Edge: voice.EdgeNone, Drained: true})+ stubUpgradedVoiceStart(t, func() error { return nil })+ app, _ := testVoiceApp()+ srv := &inst.State.Voice[0]+ if err := upgradeVoiceServer(context.Background(), app, nil, nil, inst, srv, "livekit:new", time.Second); err != nil {+ t.Fatal(err)+ }+ if node.draining || len(node.patches) != 1 || node.patches[0] {+ t.Fatalf("draining %v after patches %v", node.draining, node.patches)+ }+ if srv.Image != "livekit:new" || srv.Drained {+ t.Fatalf("server %+v", srv)+ }+}++func TestVoiceUpgradeLeavesAnAdminDrainAlone(t *testing.T) {+ clearRecords(t)+ node := &drainNode{draining: true}+ inst := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", NodeURL: node.serve(t), Key: "k", Secret: strings.Repeat("s", 64), Image: "livekit:old", Edge: voice.EdgeNone})+ stubUpgradedVoiceStart(t, func() error { return nil })+ app, _ := testVoiceApp()+ if err := upgradeVoiceServer(context.Background(), app, nil, nil, inst, &inst.State.Voice[0], "livekit:new", time.Second); err != nil {+ t.Fatal(err)+ }+ if !node.draining || len(node.patches) > 0 {+ t.Fatalf("draining %v after patches %v", node.draining, node.patches)+ }+}++func TestCancelledVoiceUpgradeStillClearsItsDrain(t *testing.T) {+ clearRecords(t)+ node := &drainNode{}+ inst := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1", NodeURL: node.serve(t), Key: "k", Secret: strings.Repeat("s", 64), Image: "livekit:old", Edge: voice.EdgeNone})+ ctx, cancel := context.WithCancel(context.Background())+ stubUpgradedVoiceStart(t, func() error {+ cancel()+ return context.Canceled+ })+ app, _ := testVoiceApp()+ if err := upgradeVoiceServer(ctx, app, nil, nil, inst, &inst.State.Voice[0], "livekit:new", time.Second); err == nil {+ t.Fatal("the cancelled upgrade gave no error")+ }+ if node.draining {+ t.Fatalf("still draining after patches %v", node.patches)+ }+}diff --git a/fluxer_rexctl/cmd/voice_ops.go b/fluxer_rexctl/cmd/voice_ops.gonew file mode 100644index 000000000..25e713236--- /dev/null+++ b/fluxer_rexctl/cmd/voice_ops.go@@ -0,0 +1,465 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "bytes"+ "context"+ "fmt"+ "os"+ "os/exec"+ "path/filepath"+ "slices"+ "strconv"+ "strings"+ "time"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/envfile"+ "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"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++func voiceSet(inst *instance.Instance) *stack.Set {+ if inst != nil && inst.State != nil && !inst.State.IsVoice() {+ if set, err := inst.Set(); err == nil {+ if _, err := voice.ShippedFile(set, voice.ComposeFile); err == nil {+ return set+ }+ }+ }+ return stack.Embedded()+}++func writeVoiceServer(inst *instance.Instance, srv *instance.VoiceServer) error {+ dir := voice.ServerDir(inst.Dir, srv.ID)+ if err := os.MkdirAll(dir, 0o700); err != nil {+ return err+ }+ if err := envfile.RestrictToOwner(dir); err != nil {+ return err+ }+ set := voiceSet(inst)+ for _, name := range []string{voice.ComposeFile, voice.HTTPComposeFile} {+ data, err := voice.ShippedFile(set, name)+ if err != nil {+ return err+ }+ if err := os.WriteFile(filepath.Join(dir, name), data, 0o644); err != nil {+ return err+ }+ }+ return envfile.WriteFileAtomic(filepath.Join(dir, voice.EnvFile), deploy.VoiceEnv(inst, srv).File(), true)+}++func startVoiceServer(ctx context.Context, app *App, e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer, s *ui.Step) error {+ if srv.Network == voice.MachineNetwork {+ if err := e.EnsureNetwork(ctx, voice.MachineNetwork, voice.EdgeProject, "voice"); err != nil {+ return engineFailure(err)+ }+ }+ p, err := e.Load(ctx, deploy.VoiceSpec(inst, srv))+ if err != nil {+ return engineFailure(err)+ }+ if err := p.Pull(ctx, app.progress(s, "Starting")); err != nil {+ return engineFailure(err)+ }+ if err := p.Up(context.WithoutCancel(ctx), engine.UpOptions{RemoveOrphans: true, Wait: true, WaitTimeout: deploy.VoiceStartTimeout}); err != nil {+ return ui.Wrap(engineFailure(err), fmt.Sprintf("The voice server %s didn't start.", srv.ID), fmt.Sprintf("Run rexctl voice status %s to see why.", srv.ID))+ }+ return nil+}++func removeVoiceServerProject(ctx context.Context, e *engine.Engine, srv *instance.VoiceServer) error {+ return engineFailure(e.Named(srv.Project, "").DownWithin(context.WithoutCancel(ctx), true, deploy.VoiceStopTimeout))+}++func voiceProject(e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer) *engine.Project {+ return e.Named(srv.Project, voice.ServerDir(inst.Dir, srv.ID))+}++func ownLiveKitImage() string {+ if images, err := stack.EmbeddedImages(); err == nil {+ if ref, err := images.Image("livekit"); err == nil && ref != "" {+ return ref+ }+ }+ return voice.DefaultImage+}++func instanceLiveKitImage(inst *instance.Instance) string {+ name := settings.ImageVars["livekit"]+ for _, ref := range []string{inst.State.Settings[settings.ImagesPrefix+"livekit"], inst.State.Settings[name], inst.State.Images.Refs["livekit"], inst.Setting(name)} {+ if ref = strings.TrimSpace(ref); ref != "" && !strings.Contains(ref, "$") {+ return ref+ }+ }+ return ownLiveKitImage()+}++func voicePortStrings(srv *instance.VoiceServer) []host.Port {+ return []host.Port{{Number: srv.TCPPort, Proto: "tcp"}, {Number: srv.UDPPort, Proto: "udp"}}+}++func portStrings(ports []host.Port) []string {+ out := make([]string, len(ports))+ for i, p := range ports {+ out[i] = p.String()+ }+ return out+}++func parsePorts(specs []string) []host.Port {+ var out []host.Port+ for _, s := range specs {+ num, proto, ok := strings.Cut(s, "/")+ n, err := strconv.Atoi(num)+ if ok && err == nil {+ out = append(out, host.Port{Number: n, Proto: proto})+ }+ }+ return out+}++func takenVoicePorts(ctx context.Context, e *engine.Engine, inst *instance.Instance) map[host.Port]bool {+ taken := map[host.Port]bool{}+ if e != nil {+ if owners, err := e.PublishedHostPorts(ctx); err == nil {+ for _, o := range owners {+ taken[host.Port{Number: o.Number, Proto: o.Proto}] = true+ }+ }+ }+ dirs := instance.ReadRecord(instance.OwnScope()).Instances+ if inst != nil && !slices.Contains(dirs, inst.Dir) {+ dirs = append(dirs, inst.Dir)+ }+ for _, dir := range dirs {+ other := inst+ if inst == nil || dir != inst.Dir {+ found, err := instance.Find(dir)+ if err != nil {+ continue+ }+ other = found+ }+ for _, srv := range other.State.Voice {+ for _, p := range srv.Ports() {+ taken[host.Port{Number: p.Number, Proto: p.Proto}] = true+ }+ }+ if deploy.VoiceBundled(other) {+ tcp, udp := 7881, 7882+ if n, err := strconv.Atoi(other.State.Settings[settings.VoiceTCPPort]); err == nil {+ tcp = n+ }+ if n, err := strconv.Atoi(other.State.Settings[settings.VoiceUDPPort]); err == nil {+ udp = n+ }+ taken[host.Port{Number: tcp, Proto: "tcp"}] = true+ taken[host.Port{Number: udp, Proto: "udp"}] = true+ }+ }+ return taken+}++func voiceIDsInUse(ctx context.Context, e *engine.Engine, inst *instance.Instance, projectPrefix string) []string {+ ids := []string{voice.EdgeID}+ dirs := instance.ReadRecord(instance.OwnScope()).Instances+ if inst != nil && !slices.Contains(dirs, inst.Dir) {+ dirs = append(dirs, inst.Dir)+ }+ for _, dir := range dirs {+ other := inst+ if inst == nil || dir != inst.Dir {+ found, err := instance.Find(dir)+ if err != nil {+ continue+ }+ other = found+ }+ for _, srv := range other.State.Voice {+ ids = append(ids, srv.ID)+ }+ if other.State.IsVoice() {+ continue+ }+ entries, _ := os.ReadDir(other.Path(instance.CaddyDropInDir))+ for _, ent := range entries {+ name, ok := strings.CutPrefix(ent.Name(), voice.DropInPrefix)+ if id, caddy := strings.CutSuffix(name, ".caddy"); ok && caddy {+ ids = append(ids, id)+ }+ }+ }+ if e != nil {+ if projects, err := e.Projects(ctx); err == nil {+ for _, p := range projects {+ if id, ok := strings.CutPrefix(p.Name, projectPrefix); ok && id != "" {+ ids = append(ids, id)+ }+ }+ }+ }+ return ids+}++func freeOnHost(number int, proto string) bool {+ used, _, err := host.PortInUse(proto, number)+ return err == nil && !used+}++func openVoiceFirewall(ctx context.Context, app *App, ports []host.Port, noFirewall bool) []string {+ if noFirewall {+ return nil+ }+ fw, err := host.DetectFirewall(ctx)+ if err != nil || fw == host.FirewallNone {+ return nil+ }+ closed, err := host.ClosedPorts(ctx, fw, ports)+ if err != nil || len(closed) == 0 {+ return nil+ }+ if err := host.OpenPorts(ctx, fw, closed); err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't open %s in %s.", strings.Join(portStrings(closed), ", "), fw), "Open them yourself before people connect.")+ return nil+ }+ app.UI.OK("Opened %s in %s.", strings.Join(portStrings(closed), ", "), fw)+ return portStrings(closed)+}++func closeVoiceFirewall(ctx context.Context, app *App, opened []string) {+ if len(opened) == 0 {+ return+ }+ fw, err := host.DetectFirewall(ctx)+ if err != nil || fw == host.FirewallNone {+ return+ }+ if err := host.ClosePorts(ctx, fw, parsePorts(opened)); err != nil {+ app.UI.Detail("closing %s: %v", strings.Join(opened, ", "), err)+ }+}++func reloadCaddy(ctx context.Context, p *engine.Project, service, caddyfile string) error {+ return p.Exec(ctx, service, engine.ExecOptions{}, "caddy", "reload", "--config", caddyfile, "--adapter", "caddyfile")+}++func moveVoiceEndpoints(ctx context.Context, app *App, inst *instance.Instance) {+ if len(deploy.StaleVoiceEndpoints(inst)) == 0 {+ return+ }+ origin := voice.WSOrigin(inst.Origin())+ p, err := app.Named(ctx, inst)+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't move the voice servers to %s (%v).", origin, err), "Run rexctl doctor --fix to try again.")+ return+ }+ moved, err := deploy.MoveVoiceEndpoints(ctx, app.adminClient(inst, p), inst)+ if len(moved) > 0 {+ app.UI.OK("Moved %s to %s.", ui.JoinAnd(moved), origin)+ }+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't move every voice server to %s (%v).", origin, adminapi.Explain(err)), "Run rexctl doctor --fix to try again.")+ }+}++func followVoicePublicIP(inst *instance.Instance) []*instance.VoiceServer {+ ip := strings.TrimSpace(inst.State.Settings[settings.VoicePublicIP])+ var out []*instance.VoiceServer+ for i := range inst.State.Voice {+ srv := &inst.State.Voice[i]+ if srv.Joined || srv.IPPinned || srv.NodeIP == ip {+ continue+ }+ srv.NodeIP = ip+ out = append(out, srv)+ }+ return out+}++func moveVoicePublicIP(ctx context.Context, app *App, inst *instance.Instance, e *engine.Engine, restart bool) {+ moved := followVoicePublicIP(inst)+ if len(moved) == 0 {+ return+ }+ if err := saveVoiceState(inst); err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't give the voice servers the new public address (%v).", err), "Run rexctl config set voice.public_ip again.")+ return+ }+ var api *adminapi.Client+ if restart {+ api = app.adminClient(inst, e.Named(inst.Project(), inst.Dir))+ }+ for _, srv := range moved {+ var err error+ if restart {+ err = upgradeVoiceServer(ctx, app, e, api, inst, srv, srv.Image, defaultDrainTimeout)+ } else {+ err = writeVoiceServer(inst, srv)+ }+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Voice server %s still gives clients its old address (%v).", srv.ID, err), "Run rexctl voice status "+srv.ID+" to see why.")+ }+ }+}++func voiceProbeTarget(inst *instance.Instance, srv *instance.VoiceServer) (string, string) {+ switch {+ case srv.Edge == voice.EdgeNone:+ return voice.LocalBase(srv.HTTPPort), ""+ case 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"))+}++func waitForEndpoint(ctx context.Context, base, edgeAddr string, insecure bool, limit time.Duration) error {+ deadline := time.Now().Add(limit)+ var last error+ for {+ if last = voice.ProbeThroughEdge(ctx, base, edgeAddr, insecure); last == nil {+ return nil+ }+ if time.Now().After(deadline) {+ return last+ }+ select {+ case <-ctx.Done():+ return ctx.Err()+ case <-time.After(2 * time.Second):+ }+ }+}++type transportResult struct {+ Transport voice.Transport+ Err error+}++func runTransportTests(ctx context.Context, endpoint string, token func() (string, error), insecure bool) []transportResult {+ var out []transportResult+ for _, t := range []voice.Transport{voice.TransportUDP, voice.TransportTCP} {+ tok, err := token()+ if err == nil {+ err = voice.TestTransport(ctx, endpoint, tok, t, insecure, 20*time.Second)+ }+ out = append(out, transportResult{Transport: t, Err: err})+ }+ return out+}++func reportTransports(app *App, results []transportResult, local bool) bool {+ ok := true+ for _, r := range results {+ name := strings.ToUpper(string(r.Transport))+ if r.Err == nil {+ app.UI.OK("%s connects.", name)+ continue+ }+ ok = false+ app.UI.Warn(fmt.Sprintf("%s doesn't connect (%v).", name, r.Err), "")+ }+ if local && !ok {+ app.UI.Info("Run rexctl doctor to check the ports, the firewall on this machine and the address the server gives clients.")+ }+ if local {+ app.UI.Info("This test ran on the machine that runs the server. It can't see a firewall at your provider or your router.")+ }+ return ok+}++func runQuietCommand(ctx context.Context, name string, args ...string) error {+ return exec.CommandContext(ctx, name, args...).Run()+}++func restoreBundledVoiceServer(ctx context.Context, app *App, inst *instance.Instance) {+ id := deploy.BundledVoiceServerID(inst)+ if id == "" {+ return+ }+ region := deploy.DefaultVoiceRegion(inst)+ fix := fmt.Sprintf("Add %s to the region %s in the admin panel, with the key and secret in LIVEKIT_API_KEY and LIVEKIT_API_SECRET.", id, region)+ p, err := app.Named(ctx, inst)+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't add the bundled voice server %s (%v).", id, err), fix)+ return+ }+ api := app.adminClient(inst, p)+ regions, err := api.VoiceRegions(ctx)+ if err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't add the bundled voice server %s (%v).", id, adminapi.Explain(err)), fix)+ return+ }+ for _, r := range regions {+ for _, s := range r.Servers {+ if s.ServerId == id {+ return+ }+ }+ }+ key, secret := inst.Setting("LIVEKIT_API_KEY"), inst.Setting("LIVEKIT_API_SECRET")+ if key == "" || secret == "" {+ app.UI.Warn(fmt.Sprintf("Couldn't add the bundled voice server %s. LIVEKIT_API_KEY or LIVEKIT_API_SECRET is empty.", id), fix)+ return+ }+ body := adminapi.CreateVoiceServerRequestBody{ServerId: id, Endpoint: voice.WSOrigin(inst.Origin()) + "/livekit", ApiKey: key, ApiSecret: secret}+ if _, err := api.CreateVoiceServer(ctx, region, body); err != nil {+ app.UI.Warn(fmt.Sprintf("Couldn't add the bundled voice server %s (%v).", id, adminapi.Explain(err)), fix)+ return+ }+ app.UI.OK("Added the bundled voice server %s to the region %s.", id, region)+}++func saveVoiceState(inst *instance.Instance) error {+ if err := inst.Save(); err != nil {+ return ui.Wrap(err, "Couldn't save the voice server state.", "Check that you can write to "+inst.Dir+", then try again.")+ }+ return nil+}++func voiceLogLines(ctx context.Context, e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer, tail int) []string {+ var buf bytes.Buffer+ if err := voiceProject(e, inst, srv).Logs(ctx, engine.LogsOptions{Tail: tail, Stdout: &buf}, "livekit"); err != nil {+ return nil+ }+ return voice.LogLines(&buf, func(string) bool { return true })+}++func voiceWebhookLog(ctx context.Context, e *engine.Engine, inst *instance.Instance, srv *instance.VoiceServer) (string, bool) {+ failed, sent := "", false+ for _, line := range voiceLogLines(ctx, e, inst, srv, 2000) {+ lower := strings.ToLower(line)+ switch {+ case strings.Contains(lower, "failed to send webhook") || strings.Contains(lower, "could not send webhook"):+ failed = strings.TrimSpace(line)+ if len(failed) > 160 {+ failed = failed[:160]+ }+ case strings.Contains(lower, "sent webhook") || strings.Contains(lower, "webhook sent"):+ failed, sent = "", true+ }+ }+ return failed, sent+}++func voiceInstances(inst *instance.Instance) string {+ var out []string+ for _, s := range inst.State.Voice {+ if s.Instance != "" && !slices.Contains(out, s.Instance) {+ out = append(out, s.Instance)+ }+ }+ if len(out) == 0 {+ return "another instance"+ }+ return ui.JoinAnd(out)+}diff --git a/fluxer_rexctl/cmd/voice_plan_test.go b/fluxer_rexctl/cmd/voice_plan_test.gonew file mode 100644index 000000000..5554ce200--- /dev/null+++ b/fluxer_rexctl/cmd/voice_plan_test.go@@ -0,0 +1,326 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package cmd++import (+ "context"+ "encoding/json"+ "errors"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "slices"+ "strings"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++func fluxerWithVoice(t *testing.T, servers ...instance.VoiceServer) *instance.Instance {+ t.Helper()+ inst, err := instance.Create(resolved(t, t.TempDir()), "fluxer")+ if err != nil {+ t.Fatal(err)+ }+ inst.State.Settings["domain"] = "chat.example.com"+ inst.State.Voice = servers+ if err := os.MkdirAll(inst.Path(instance.CaddyDropInDir), 0o755); err != nil {+ t.Fatal(err)+ }+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ if err := instance.Remember(instance.OwnScope(), inst.Dir, true); err != nil {+ t.Fatal(err)+ }+ return inst+}++func refusal(t *testing.T, err error) *ui.ExitError {+ t.Helper()+ var ee *ui.ExitError+ if !errors.As(err, &ee) {+ t.Fatalf("not a refusal: %v", err)+ }+ return ee+}++func TestVoiceIDsAreUniqueOnTheWholeMachine(t *testing.T) {+ clearRecords(t)+ fluxer := fluxerWithVoice(t, instance.VoiceServer{ID: "default-server-2", Edge: voice.EdgeInstance})+ if err := os.WriteFile(filepath.Join(fluxer.Path(instance.CaddyDropInDir), voice.DropInName("hand-made")), voice.DropIn("hand-made"), 0o644); err != nil {+ t.Fatal(err)+ }+ machine := voiceMachine(t, instance.VoiceServer{ID: "default-server-3", Edge: voice.EdgeInstance, Joined: true})+ machine.State.VoiceEdge = &instance.VoiceEdge{Instance: fluxer.Dir}+ if err := machine.Save(); err != nil {+ t.Fatal(err)+ }+ for _, inst := range []*instance.Instance{fluxer, machine} {+ taken := voiceIDsInUse(context.Background(), nil, inst, voice.JoinedProject(""))+ for _, id := range []string{"edge", "default-server-2", "default-server-3", "hand-made"} {+ if !slices.Contains(taken, id) {+ t.Errorf("from %s: %s is free in %v", inst.Dir, id, taken)+ }+ }+ }+}++func TestJoinSkipsAnIDThatAServerNextDoorUses(t *testing.T) {+ clearRecords(t)+ machine := voiceMachine(t)+ preview := &voice.Preview{ServerID: "default-server-2"}+ preview.Region.ID = "default"+ taken := []string{"edge", "default-server-1", "default-server-2"}+ id, err := chooseJoinID(preview, "", machine, taken)+ if err != nil || id != "default-server-3" {+ t.Fatalf("suggested ID: %s, %v", id, err)+ }+ if _, err := chooseJoinID(preview, "default-server-1", machine, taken); !strings.Contains(refusal(t, err).Fix, "--id") {+ t.Fatalf("taken --id: %v", err)+ }+ preview.ServerIDPinned = true+ if _, err := chooseJoinID(preview, "", machine, taken); !strings.Contains(refusal(t, err).Fix, "new token") {+ t.Fatalf("taken pinned ID: %v", err)+ }+ if _, err := chooseJoinID(preview, "other", machine, nil); !strings.Contains(refusal(t, err).Fix, "Leave out --id") {+ t.Fatalf("--id against a pinned ID: %v", err)+ }+ preview = &voice.Preview{ServerID: "edge"}+ preview.Region.ID = "edge"+ if id, err := chooseJoinID(preview, "", machine, []string{"edge"}); err != nil || id != "edge-server-1" {+ t.Fatalf("edge: %s, %v", id, err)+ }+}++func TestJoinRetryKeepsItsOwnID(t *testing.T) {+ clearRecords(t)+ machine := voiceMachine(t, instance.VoiceServer{ID: "fra-server-1"})+ preview := &voice.Preview{ServerID: "fra-server-1"}+ if id, err := chooseJoinID(preview, "", machine, []string{"fra-server-1"}); err != nil || id != "fra-server-1" {+ t.Fatalf("retry: %s, %v", id, err)+ }+ machine.State.Voice[0].NodeURL = "https://chat.example.com/api/voice/node"+ if _, err := chooseJoinID(preview, "", machine, nil); err == nil {+ t.Fatal("a joined server was joined again")+ }+}++func TestVoiceAddSkipsIDsOfJoinedServersNextDoor(t *testing.T) {+ clearRecords(t)+ fluxer := fluxerWithVoice(t)+ machine := voiceMachine(t, instance.VoiceServer{ID: "default-server-2", Edge: voice.EdgeInstance, Joined: true})+ machine.State.VoiceEdge = &instance.VoiceEdge{Instance: fluxer.Dir}+ if err := machine.Save(); err != nil {+ t.Fatal(err)+ }+ regions := []adminapi.VoiceRegion{{VoiceRegionAdminResponse: adminapi.VoiceRegionAdminResponse{Id: "default"}, Servers: []adminapi.VoiceServerAdminResponse{{ServerId: "default-server-1"}}}}+ app, _ := testVoiceApp()+ f := &voiceFlags{Count: 1, IP: "203.0.113.9", TCPPort: 7883, UDPPort: 7884}+ plans, err := planVoiceAdd(context.Background(), app, nil, fluxer, regions, "default", f)+ if err != nil || len(plans) != 1 || plans[0].srv.ID != "default-server-3" {+ t.Fatalf("plans %v, %v", plans, err)+ }+ f.ID = "default-server-2"+ if _, err := planVoiceAdd(context.Background(), app, nil, fluxer, regions, "default", f); !strings.Contains(refusal(t, err).Fix, "--id") {+ t.Fatalf("taken --id: %v", err)+ }+}++func TestEdgeIsNotAServerID(t *testing.T) {+ if err := checkServerID("edge"); err == nil {+ t.Fatal("edge is a server ID")+ }+ if err := checkServerID("fra-server-1"); err != nil {+ t.Fatal(err)+ }+ app, _ := testVoiceApp()+ c := newVoiceTokenCmd(app)+ c.SetArgs([]string{"--id", "edge"})+ c.SilenceUsage, c.SilenceErrors = true, true+ if err := c.Execute(); refusal(t, err).Code != ui.ExitUsage {+ t.Fatalf("token --id edge: %v", err)+ }+}++func nodeServer(t *testing.T, status int, node map[string]any) string {+ t.Helper()+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(status)+ _ = json.NewEncoder(w).Encode(node)+ }))+ t.Cleanup(srv.Close)+ return srv.URL+}++func TestUpgradeReadsDrainingOnAVoiceMachine(t *testing.T) {+ url := nodeServer(t, http.StatusOK, map[string]any{"is_active": true, "draining": true})+ inst := &instance.Instance{Dir: t.TempDir(), State: &instance.State{Kind: instance.KindVoice}}+ srv := &instance.VoiceServer{ID: "fra-server-1", NodeURL: url, Key: "k", Secret: strings.Repeat("s", 64)}+ active, draining, err := voiceServerFlags(context.Background(), nil, inst, srv)+ if err != nil || !active || !draining {+ t.Fatalf("active %v, draining %v, %v", active, draining, err)+ }+ if upgradeDrains(active, draining) {+ t.Fatal("the upgrade clears a draining flag an admin set")+ }+ srv.NodeURL = nodeServer(t, http.StatusBadGateway, map[string]any{})+ if _, _, err := voiceServerFlags(context.Background(), nil, inst, srv); err == nil {+ t.Fatal("an unreadable node gave a state")+ }+ fluxer := &instance.Instance{Dir: t.TempDir(), State: &instance.State{}}+ if _, _, err := voiceServerFlags(context.Background(), nil, fluxer, srv); err == nil {+ t.Fatal("no admin API gave a state")+ }+}++func TestVoiceAddServersFollowThePublicIP(t *testing.T) {+ inst := &instance.Instance{Dir: t.TempDir(), State: &instance.State{Settings: map[string]string{settings.VoicePublicIP: "198.51.100.20"}, Voice: []instance.VoiceServer{+ {ID: "a", NodeIP: "198.51.100.10"},+ {ID: "b", NodeIP: "198.51.100.11", IPPinned: true},+ {ID: "c", NodeIP: "198.51.100.10", Joined: true},+ {ID: "d", NodeIP: "198.51.100.20"},+ }}}+ moved := followVoicePublicIP(inst)+ if len(moved) != 1 || moved[0].ID != "a" || inst.State.Voice[0].NodeIP != "198.51.100.20" {+ t.Fatalf("moved %v", moved)+ }+ if inst.State.Voice[1].NodeIP != "198.51.100.11" || inst.State.Voice[2].NodeIP != "198.51.100.10" {+ t.Fatalf("a pinned or joined server moved: %+v", inst.State.Voice)+ }+ delete(inst.State.Settings, settings.VoicePublicIP)+ moved = followVoicePublicIP(inst)+ if len(moved) != 2 || inst.State.Voice[0].NodeIP != "" || inst.State.Voice[3].NodeIP != "" {+ t.Fatalf("clearing the setting moved %v", moved)+ }+}++func TestPinnedHostnameIsRequired(t *testing.T) {+ old, oldWait := hostnameLookup, dnsWait+ t.Cleanup(func() { hostnameLookup, dnsWait = old, oldWait })+ dnsWait = 0+ hostnameLookup = func() voice.Resolver {+ return func(context.Context, string) (voice.DNSAnswer, error) {+ return voice.DNSAnswer{A: []string{"192.0.2.1"}}, nil+ }+ }+ pinned := "voice.example.com"+ plan := &joinPlan{preview: &voice.Preview{Hostname: &pinned}, reach: voice.Reachability{PublicIPv4: "203.0.113.7"}}+ app, out := testVoiceApp()+ if err := chooseAddress(context.Background(), app, plan, "fra-server-1", &voiceFlags{Hostname: "other.example.com"}); !strings.Contains(refusal(t, err).Msg, pinned) {+ t.Fatalf("another --hostname: %v", err)+ }+ err := chooseAddress(context.Background(), app, plan, "fra-server-1", &voiceFlags{})+ if ee := refusal(t, err); !strings.Contains(ee.Msg, pinned) || plan.address != "" {+ t.Fatalf("pinned name not on this machine: %v, address %q", err, plan.address)+ }+ if strings.Contains(out.String(), "rexctl voice hostname") {+ t.Fatalf("offered the IP address: %s", out.String())+ }+ plan.preview.Hostname = nil+ if err := chooseAddress(context.Background(), app, plan, "fra-server-1", &voiceFlags{Hostname: "other.example.com"}); err != nil || plan.address != "203.0.113.7" || !plan.ipCert {+ t.Fatalf("unpinned name: %v, address %q", err, plan.address)+ }+}++func TestEndpointRefusalShowsThePinnedHostname(t *testing.T) {+ err := joinError(&voice.APIError{Code: voice.CodeEndpointInvalid, Detail: "The endpoint must use the hostname voice.example.com"}, "chat.example.com", "fra-server-1")+ if ee := refusal(t, err); !strings.Contains(ee.Fix, "voice.example.com") || strings.Contains(ee.Fix, "IPv4") {+ t.Fatalf("refusal: %+v", ee)+ }+}++func TestOriginWarningSaysWhatToDo(t *testing.T) {+ if got := originFix(originUnlisted, "chat.example.com", "2026.1030.400"); got != "Upgrade Fluxer on chat.example.com to 2026.1030.400, then reload open pages." {+ t.Errorf("old release: %s", got)+ }+ if got := originFix(originPending, "chat.example.com", "2026.1030.400"); !strings.Contains(got, "rexctl voice status") {+ t.Errorf("pending: %s", got)+ }+ for _, c := range []struct {+ body string+ listed bool+ }{+ {`{"endpoints":{"api_public":"https://chat.example.com/api"}}`, false},+ {`{"endpoints":{"api_public":"https://chat.example.com/api"},"voice_origins":[]}`, true},+ } {+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ _, _ = w.Write([]byte(c.body))+ }))+ disc, err := (&voice.Client{}).Discover(context.Background(), srv.URL)+ srv.Close()+ if err != nil || disc.ListsOrigins != c.listed {+ t.Errorf("%s: %+v, %v", c.body, disc, err)+ }+ }+}++func TestVoiceAddListsTheProviderPorts(t *testing.T) {+ got := providerPortsText(&instance.VoiceServer{TCPPort: 7883, UDPPort: 7884})+ if !strings.Contains(got, "7883/tcp and 7884/udp") {+ t.Fatal(got)+ }+}++func TestATakenPinnedIDDoesNotAskForID(t *testing.T) {+ taken := &voice.APIError{Code: voice.CodeIDInUse}+ ee := refusal(t, pinnedJoinError(taken, "chat.example.com", "fra-server-1"))+ if !strings.Contains(ee.Msg, "fra-server-1") || strings.Contains(ee.Fix, "Pass --id") || !strings.Contains(ee.Fix, "rexctl voice token --id") {+ t.Fatalf("pinned: %+v", ee)+ }+ if ee := refusal(t, joinError(taken, "chat.example.com", "fra-server-1")); ee.Fix != "Pass --id with another ID." {+ t.Fatalf("unpinned: %+v", ee)+ }+ var exit *ui.ExitError+ if err := joinError(&voice.APIError{Code: "SOMETHING_ELSE", Message: "No"}, "chat.example.com", "fra-server-1"); !errors.As(err, &exit) || !strings.HasPrefix(exit.Msg, "chat.example.com refused the request (") {+ t.Fatalf("other refusal: %v", err)+ }+}++func TestJoinSummarySaysWhyItPickedTheRegion(t *testing.T) {+ km := 6200.0+ preview := func(source string) *voice.Preview {+ p := &voice.Preview{}+ p.Region.ID, p.Region.Name, p.Region.DistanceKm, p.Region.Source = "fra", "Frankfurt", &km, source+ return p+ }+ cases := map[string]string{+ "nearest": "fra (Frankfurt), nearest existing region, 6200 km",+ "token": "fra (Frankfurt), from the token, 6200 km away",+ "requested": "fra (Frankfurt), from --region, 6200 km away",+ "": "fra (Frankfurt), 6200 km away",+ }+ for source, want := range cases {+ if got := joinRegionText(preview(source), ""); got != want {+ t.Errorf("%q: %s, want %s", source, got, want)+ }+ }+ if got := joinRegionText(preview(""), "fra"); got != "fra (Frankfurt), from --region, 6200 km away" {+ t.Errorf("an older API with --region: %s", got)+ }+}++func TestAFailedLocalTestSaysWhatToCheck(t *testing.T) {+ const hint = "Run rexctl doctor to check the ports"+ failed := []transportResult{{Transport: "udp", Err: errors.New("timeout")}}+ app, out := testVoiceApp()+ if reportTransports(app, failed, true) || strings.Count(out.String(), hint) != 1 {+ t.Fatalf("local failure: %s", out.String())+ }+ app, out = testVoiceApp()+ reportTransports(app, []transportResult{{Transport: "udp"}}, true)+ if strings.Contains(out.String(), hint) {+ t.Fatalf("a passing test asks for doctor: %s", out.String())+ }+ app, out = testVoiceApp()+ reportTransports(app, failed, false)+ if strings.Contains(out.String(), hint) {+ t.Fatalf("a remote test asks for doctor: %s", out.String())+ }+}diff --git a/fluxer_rexctl/go.mod b/fluxer_rexctl/go.modindex c2564da04..a72623d7f 100644--- a/fluxer_rexctl/go.mod+++ b/fluxer_rexctl/go.mod@@ -20,10 +20,12 @@ require (github.com/moby/moby/api v1.56.1github.com/moby/moby/client v0.6.1github.com/oapi-codegen/runtime v1.6.0+ github.com/pion/webrtc/v4 v4.2.26github.com/sirupsen/logrus v1.10.2github.com/spf13/cobra v1.10.2github.com/spf13/pflag v1.0.10go.yaml.in/yaml/v4 v4.0.0-rc.6+ golang.org/x/net v0.60.0golang.org/x/sys v0.49.0golang.org/x/term v0.47.0)@@ -107,6 +109,21 @@ require (github.com/muesli/roff v0.1.0 // indirectgithub.com/opencontainers/go-digest v1.0.0 // indirectgithub.com/opencontainers/image-spec v1.1.1 // indirect+ github.com/pion/datachannel v1.6.3 // indirect+ github.com/pion/dtls/v3 v3.1.15 // indirect+ github.com/pion/ice/v4 v4.4.8 // indirect+ github.com/pion/interceptor v0.1.51 // indirect+ github.com/pion/logging v0.2.4 // indirect+ github.com/pion/mdns/v2 v2.2.2 // indirect+ github.com/pion/randutil v0.1.0 // indirect+ github.com/pion/rtcp v1.2.20 // indirect+ github.com/pion/rtp v1.10.6 // indirect+ github.com/pion/sctp v1.12.0 // indirect+ github.com/pion/sdp/v3 v3.0.20 // indirect+ github.com/pion/srtp/v3 v3.1.3 // indirect+ github.com/pion/stun/v4 v4.0.1 // indirect+ github.com/pion/transport/v5 v5.1.4 // indirect+ github.com/pion/turn/v5 v5.1.3 // indirectgithub.com/pkg/errors v0.9.1 // indirectgithub.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirectgithub.com/rivo/uniseg v0.4.7 // indirect@@ -119,6 +136,7 @@ require (github.com/tonistiigi/go-csvvalue v0.0.0-20240814133006-030d3b2625d0 // indirectgithub.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea // indirectgithub.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab // indirect+ github.com/wlynxg/anet v0.0.5 // indirectgithub.com/xhit/go-str2duration/v2 v2.1.0 // indirectgithub.com/xo/terminfo v1.0.0 // indirectgo.opentelemetry.io/auto/sdk v1.2.1 // indirect@@ -138,7 +156,6 @@ require (go.opentelemetry.io/proto/otlp v1.11.0 // indirectgo.yaml.in/yaml/v3 v3.0.5 // indirectgolang.org/x/crypto v0.57.0 // indirect- golang.org/x/net v0.60.0 // indirectgolang.org/x/sync v0.23.0 // indirectgolang.org/x/text v0.42.0 // indirectgolang.org/x/time v0.16.0 // indirectdiff --git a/fluxer_rexctl/go.sum b/fluxer_rexctl/go.sumindex 0c2107de9..99fc24304 100644--- a/fluxer_rexctl/go.sum+++ b/fluxer_rexctl/go.sum@@ -256,6 +256,38 @@ github.com/package-url/packageurl-go v0.1.1 h1:KTRE0bK3sKbFKAk3yy63DpeskU7Cvs/x/github.com/package-url/packageurl-go v0.1.1/go.mod h1:uQd4a7Rh3ZsVg5j0lNyAfyxIeGde9yrlhjF78GzeW0c=github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY=github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=+github.com/pion/datachannel v1.6.3 h1:HZAYLpunVI0xi057ZiANl558d0uSpomcWw4UEPz3qXs=+github.com/pion/datachannel v1.6.3/go.mod h1:oRmkXVVXxkUbqa8MatgdNHW29kvtQY3HUdjJQsfv2GE=+github.com/pion/dtls/v3 v3.1.15 h1:okWk5LNwU21DfhvG8z4wm45knHVSPEdkihwp6BxfQcc=+github.com/pion/dtls/v3 v3.1.15/go.mod h1:iKFQNYrjsN2TiA2YKKMqB9MOZaFpjFULBI/A4sW0eyc=+github.com/pion/ice/v4 v4.4.8 h1:NB22Ze5WrgMjcf9L+O+1B29bl6anq+sbyvddImDLdhc=+github.com/pion/ice/v4 v4.4.8/go.mod h1:zCRRg4Mkpdbx9B+75fd4z2APvIXeIraeUOB7IMPt8mU=+github.com/pion/interceptor v0.1.51 h1:PU09y6RghjWXR4AYJ+AVNyDX7EmgIZuDFMGDl6smKQA=+github.com/pion/interceptor v0.1.51/go.mod h1:nVgAA8pNNhaglm6NSe9lMuFhmi8kwWbf7Whw1ukt9VU=+github.com/pion/logging v0.2.4 h1:tTew+7cmQ+Mc1pTBLKH2puKsOvhm32dROumOZ655zB8=+github.com/pion/logging v0.2.4/go.mod h1:DffhXTKYdNZU+KtJ5pyQDjvOAh/GsNSyv1lbkFbe3so=+github.com/pion/mdns/v2 v2.2.2 h1:qq9Kj3PnjF0mfSwd8Icu0CXy5aoYdQKnNUIsGPeUpgk=+github.com/pion/mdns/v2 v2.2.2/go.mod h1:ZX5f0AAH1D6TOjdjvcBORZZHaZuG0t9+br78lHEwiJ4=+github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA=+github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8=+github.com/pion/rtcp v1.2.20 h1:ZxQpb1Z3st9W5Rts4PjGoC3ynDhUtQ9B/itKJho8h2I=+github.com/pion/rtcp v1.2.20/go.mod h1:vX7Es6skWYm4oVvOSC5+4WHLYUTZHcbGNmk5wjzMGH4=+github.com/pion/rtp v1.10.6 h1:1qs356bmniSc89gTF1KFdI0kej7qTdIUSnHOXu3P2EY=+github.com/pion/rtp v1.10.6/go.mod h1:qvV617FVkp5DBUfoHGxP8uOnTI4UaNDJK0z7Sildylk=+github.com/pion/sctp v1.12.0 h1:AzUdVkVBNyGY1W4crWO1/aOWzMaW3zsVHGrNuAfFQiI=+github.com/pion/sctp v1.12.0/go.mod h1:6k1zrpkrxSwbisitn/AlzDQaa15DR49MFsrMochG/yc=+github.com/pion/sdp/v3 v3.0.20 h1:TS6DViqcmp+49f0+mjw9anbr9xY3vJtsZewxAvlMCRQ=+github.com/pion/sdp/v3 v3.0.20/go.mod h1:slIMXDK5OKj0nhISwjfeN18AzTBCt2LYZq9uPw0cU5Q=+github.com/pion/srtp/v3 v3.1.3 h1:O3sfbfWfq/OyEaXRbX02Z0SNQRijS9nDdc/DEb8jxlU=+github.com/pion/srtp/v3 v3.1.3/go.mod h1:v3uQtrCz9LH3aahau0jtzRDsWGwKkh4a0BiBwzzxhtQ=+github.com/pion/stun/v4 v4.0.1 h1:S2ggK4hsUJJKoZfpL90uIqIRTj6xJ0NOIEkXgcpQif0=+github.com/pion/stun/v4 v4.0.1/go.mod h1:byktbPA8U7HjJ2H8w3moP7nzq0Q+kYNulvMLPaubQL4=+github.com/pion/transport/v5 v5.1.4 h1:j0E5QIPyv28YnWxa8yE9AyDmrxVnwhZ109c7Dpt48K4=+github.com/pion/transport/v5 v5.1.4/go.mod h1:Qxw6fCEjFWQkRDZOhS4Vf+neJBcihauvA3uyEa1J1F0=+github.com/pion/turn/v5 v5.1.3 h1:dxBq+M6o5DCxIjtiBQNvtHkKshS+xlJbXwgc1MKzBH8=+github.com/pion/turn/v5 v5.1.3/go.mod h1:ocKg/4W5d4N9hvpADSwoc5HdU53/4i966FIU1qJL0tY=+github.com/pion/webrtc/v4 v4.2.26 h1:RX3FUQVHhr488sgPFJWthbbtxpuYNm3XAgYDEj4RDyQ=+github.com/pion/webrtc/v4 v4.2.26/go.mod h1:vH5Fn0zOZ8LuLhIPEtsLkoYkwnvubV4TX+ZtjV5Jqgk=github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=@@ -317,6 +349,8 @@ github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab h1:H6aJ0yKQ0gF49Qgithub.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab/go.mod h1:ulncasL3N9uLrVann0m+CDlJKWsIAP34MPcOJF6VRvc=github.com/vbatts/tar-split v0.12.3 h1:Cd46rkGXI3Td4yrVNwU8ripbxFaQbmesqhjBUUYAJSw=github.com/vbatts/tar-split v0.12.3/go.mod h1:sQOc6OlqGCr7HkGx/IDBeKiTIvqhmj8KffNhEXG4Nq0=+github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU=+github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=github.com/xo/terminfo v1.0.0 h1:2ZpYzqWzyyytjk3TP6aJVDhkMAkc99/1xKQdA3TDTBY=diff --git a/fluxer_rexctl/internal/adminapi/client.go b/fluxer_rexctl/internal/adminapi/client.goindex 35d78a301..84af4f420 100644--- a/fluxer_rexctl/internal/adminapi/client.go+++ b/fluxer_rexctl/internal/adminapi/client.go@@ -9,6 +9,7 @@ import ("encoding/json""errors""fmt"+ "net/http""strings""sync")@@ -53,6 +54,9 @@ func (c *Client) Loopback(ctx context.Context, method, path string, headers map[}func (c *Client) do(ctx context.Context, req Request, in, out any) error {+ if in == nil && req.Method == http.MethodDelete && req.Target == TargetOperator {+ in = json.RawMessage("{}")+ }switch body := in.(type) {case nil:case RawBody:diff --git a/fluxer_rexctl/internal/adminapi/gen.go b/fluxer_rexctl/internal/adminapi/gen.goindex bf7e01009..30589b132 100644--- a/fluxer_rexctl/internal/adminapi/gen.go+++ b/fluxer_rexctl/internal/adminapi/gen.go@@ -209,6 +209,9 @@ const (UserViewDob AdminAclType = "user:view:dob"UserViewEmail AdminAclType = "user:view:email"UserViewIp AdminAclType = "user:view:ip"+ VoiceJoinTokenCreate AdminAclType = "voice:join_token:create"+ VoiceJoinTokenList AdminAclType = "voice:join_token:list"+ VoiceJoinTokenRevoke AdminAclType = "voice:join_token:revoke"VoiceRegionCreate AdminAclType = "voice:region:create"VoiceRegionDelete AdminAclType = "voice:region:delete"VoiceRegionList AdminAclType = "voice:region:list"@@ -438,6 +441,12 @@ func (e AdminAclType) Valid() bool {return truecase UserViewIp:return true+ case VoiceJoinTokenCreate:+ return true+ case VoiceJoinTokenList:+ return true+ case VoiceJoinTokenRevoke:+ return truecase VoiceRegionCreate:return truecase VoiceRegionDelete:@@ -1416,6 +1425,51 @@ func (e VerificationBasisSchema) Valid() bool {}}+// Defines values for VoiceJoinTokenStatus.+const (+ Expired VoiceJoinTokenStatus = "expired"+ Pending VoiceJoinTokenStatus = "pending"+ Revoked VoiceJoinTokenStatus = "revoked"+ Used VoiceJoinTokenStatus = "used"+)++// Valid indicates whether the value is a known member of the VoiceJoinTokenStatus enum.+func (e VoiceJoinTokenStatus) Valid() bool {+ switch e {+ case Expired:+ return true+ case Pending:+ return true+ case Revoked:+ return true+ case Used:+ return true+ default:+ return false+ }+}++// Defines values for VoiceServerHealthResponseStatus.+const (+ Down VoiceServerHealthResponseStatus = "down"+ Unknown VoiceServerHealthResponseStatus = "unknown"+ Up VoiceServerHealthResponseStatus = "up"+)++// Valid indicates whether the value is a known member of the VoiceServerHealthResponseStatus enum.+func (e VoiceServerHealthResponseStatus) Valid() bool {+ switch e {+ case Down:+ return true+ case Unknown:+ return true+ case Up:+ return true+ default:+ return false+ }+}+// AccountIdentityLockReasonSchema Why the sign-in method can no longer changetype AccountIdentityLockReasonSchema string@@ -1745,6 +1799,159 @@ type CreateRegistrationUrlResponse struct {Url string `json:"url"`}+// CreateVoiceJoinTokenRequest defines model for CreateVoiceJoinTokenRequest.+type CreateVoiceJoinTokenRequest struct {+ AllowedGuildIds *[]SnowflakeType `json:"allowed_guild_ids,omitempty"`+ AllowedUserIds *[]SnowflakeType `json:"allowed_user_ids,omitempty"`++ // ExpiresInSeconds Seconds until the token stops working, from 60 to 86400+ ExpiresInSeconds *int `json:"expires_in_seconds,omitempty"`++ // Hostname Hostname the server must use for its endpoint+ Hostname *string `json:"hostname,omitempty"`+ InternalEndpoint *VoiceInternalEndpointType `json:"internal_endpoint,omitempty"`+ Notes *string `json:"notes,omitempty"`++ // RegionEmoji Emoji for the new region in region_name+ RegionEmoji *string `json:"region_emoji,omitempty"`++ // RegionId Region for the new server, or null to let the API pick the nearest existing region+ RegionId *string `json:"region_id,omitempty"`++ // RegionLatitude Latitude of the new region in region_name. Placement sends people to the nearest region+ RegionLatitude *float32 `json:"region_latitude,omitempty"`++ // RegionLongitude Longitude of the new region in region_name+ RegionLongitude *float32 `json:"region_longitude,omitempty"`++ // RegionName Name for a new region with the id in region_id, created when the server joins+ RegionName *string `json:"region_name,omitempty"`+ RequiredGuildFeatures *[]string `json:"required_guild_features,omitempty"`++ // ServerId Id for the new server+ ServerId *string `json:"server_id,omitempty"`+ SoftConnectionLimit *int `json:"soft_connection_limit,omitempty"`+ VipOnly *bool `json:"vip_only,omitempty"`+}++// CreateVoiceJoinTokenResponse defines model for CreateVoiceJoinTokenResponse.+type CreateVoiceJoinTokenResponse struct {+ // Command Command to run on the voice machine+ Command string `json:"command"`++ // ExpiresAt ISO 8601 timestamp when the token stops working+ ExpiresAt string `json:"expires_at"`++ // Token The token. It appears in this response only+ Token string `json:"token"`++ // TokenId ID of the token+ TokenId SnowflakeStringType `json:"token_id"`+}++// CreateVoiceRegionRequest defines model for CreateVoiceRegionRequest.+type CreateVoiceRegionRequest struct {+ // AllowedGuildIds Guild IDs explicitly allowed to use this region+ AllowedGuildIds *[]SnowflakeType `json:"allowed_guild_ids,omitempty"`++ // AllowedUserIds User IDs explicitly allowed to use this region+ AllowedUserIds *[]SnowflakeType `json:"allowed_user_ids,omitempty"`++ // Emoji Emoji representing the region+ Emoji string `json:"emoji"`++ // Id Unique identifier for the voice region+ Id string `json:"id"`++ // IsDefault Whether this is the default region+ IsDefault *bool `json:"is_default,omitempty"`++ // Latitude Geographic latitude coordinate+ Latitude float32 `json:"latitude"`++ // Longitude Geographic longitude coordinate+ Longitude float32 `json:"longitude"`++ // Name Display name of the voice region+ Name string `json:"name"`++ // RequiredGuildFeatures Guild features required to use this region+ RequiredGuildFeatures *[]string `json:"required_guild_features,omitempty"`++ // VipOnly Whether this region is restricted to VIP users+ VipOnly *bool `json:"vip_only,omitempty"`+}++// CreateVoiceRegionResponse defines model for CreateVoiceRegionResponse.+type CreateVoiceRegionResponse struct {+ // Region Created voice region+ Region VoiceRegionAdminResponse `json:"region"`+}++// CreateVoiceServerRequestBody defines model for CreateVoiceServerRequestBody.+type CreateVoiceServerRequestBody struct {+ // AllowedGuildIds Guild IDs explicitly allowed to use this server+ AllowedGuildIds *[]SnowflakeType `json:"allowed_guild_ids,omitempty"`++ // AllowedUserIds User IDs explicitly allowed to use this server+ AllowedUserIds *[]SnowflakeType `json:"allowed_user_ids,omitempty"`++ // ApiKey API key for authenticating with the voice server+ ApiKey string `json:"api_key"`++ // ApiSecret API secret for authenticating with the voice server+ ApiSecret string `json:"api_secret"`++ // Draining Whether placement sends new calls elsewhere while calls on this server finish+ Draining *bool `json:"draining,omitempty"`++ // Endpoint Client signal WebSocket endpoint URL for the voice server+ Endpoint string `json:"endpoint"`+ InternalEndpoint *VoiceInternalEndpointType `json:"internal_endpoint,omitempty"`++ // IsActive Whether the server is currently active+ IsActive *bool `json:"is_active,omitempty"`++ // Latitude Optional geographic latitude override for this server+ Latitude *float32 `json:"latitude,omitempty"`++ // Longitude Optional geographic longitude override for this server+ Longitude *float32 `json:"longitude,omitempty"`++ // Notes Free-form admin notes about this server+ Notes *string `json:"notes,omitempty"`++ // RequiredGuildFeatures Guild features required to use this server+ RequiredGuildFeatures *[]string `json:"required_guild_features,omitempty"`++ // ServerId Unique identifier for the voice server+ ServerId string `json:"server_id"`++ // SoftConnectionLimit Connection count above which placement prefers another server, or null for no limit+ SoftConnectionLimit *int `json:"soft_connection_limit,omitempty"`++ // VipOnly Whether this server is restricted to VIP users+ VipOnly *bool `json:"vip_only,omitempty"`+}++// CreateVoiceServerResponse defines model for CreateVoiceServerResponse.+type CreateVoiceServerResponse struct {+ // Server Created voice server+ Server VoiceServerAdminResponse `json:"server"`+}++// CreateVoiceServerTestTokenResponse defines model for CreateVoiceServerTestTokenResponse.+type CreateVoiceServerTestTokenResponse struct {+ // Endpoint Endpoint of the server+ Endpoint string `json:"endpoint"`++ // Room Name of the throwaway test room+ Room string `json:"room"`++ // Token LiveKit token for a hidden audio only participant, valid for 60 seconds+ Token string `json:"token"`+}+// CustomBadgeIdListResponse IDs of the custom badges assigned to ittype CustomBadgeIdListResponse = []SnowflakeStringType@@ -1766,6 +1973,12 @@ type DeleteAllUserMessagesResponse struct {// DeleteAllUserMessagesResponseSuccess defines model for DeleteAllUserMessagesResponse.Success.type DeleteAllUserMessagesResponseSuccess bool+// DeleteVoiceResponse defines model for DeleteVoiceResponse.+type DeleteVoiceResponse struct {+ // Success Whether the deletion was successful+ Success bool `json:"success"`+}+// DeletionReasonCode Reason the account was scheduled for deletiontype DeletionReasonCode int32@@ -2568,6 +2781,57 @@ type ListUserSessionsResponse struct {} `json:"sessions"`}+// ListVoiceJoinTokensResponse defines model for ListVoiceJoinTokensResponse.+type ListVoiceJoinTokensResponse struct {+ // Tokens Join tokens, newest first+ Tokens []VoiceJoinTokenAdminResponse `json:"tokens"`+}++// ListVoiceRegionsResponse defines model for ListVoiceRegionsResponse.+type ListVoiceRegionsResponse struct {+ // Regions List of voice regions+ Regions []struct {+ // AllowedGuildIds Guild IDs explicitly allowed to use this region+ AllowedGuildIds []SnowflakeStringType `json:"allowed_guild_ids"`++ // AllowedUserIds User IDs explicitly allowed to use this region+ AllowedUserIds []SnowflakeStringType `json:"allowed_user_ids"`++ // CreatedAt ISO 8601 timestamp when the region was created+ CreatedAt *string `json:"created_at"`++ // Emoji Emoji representing the region+ Emoji string `json:"emoji"`++ // Id Unique identifier for the voice region+ Id string `json:"id"`++ // IsDefault Whether this is the default region+ IsDefault bool `json:"is_default"`++ // Latitude Geographic latitude coordinate+ Latitude float32 `json:"latitude"`++ // Longitude Geographic longitude coordinate+ Longitude float32 `json:"longitude"`++ // Name Display name of the voice region+ Name string `json:"name"`++ // RequiredGuildFeatures Guild features required to use this region+ RequiredGuildFeatures []string `json:"required_guild_features"`++ // Servers Voice servers in this region+ Servers *[]VoiceServerAdminResponse `json:"servers,omitempty"`++ // UpdatedAt ISO 8601 timestamp when the region was last updated+ UpdatedAt *string `json:"updated_at"`++ // VipOnly Whether this region is restricted to VIP users+ VipOnly bool `json:"vip_only"`+ } `json:"regions"`+}+// LookupUserResponse defines model for LookupUserResponse.type LookupUserResponse struct {Users []UserAdminResponseSchema `json:"users"`@@ -2727,6 +2991,55 @@ type ThrottledError struct {AdditionalProperties map[string]interface{} `json:"-"`}+// UpdateVoiceServerRequestBody defines model for UpdateVoiceServerRequestBody.+type UpdateVoiceServerRequestBody struct {+ // AllowedGuildIds Guild IDs explicitly allowed to use this server+ AllowedGuildIds *[]SnowflakeType `json:"allowed_guild_ids,omitempty"`++ // AllowedUserIds User IDs explicitly allowed to use this server+ AllowedUserIds *[]SnowflakeType `json:"allowed_user_ids,omitempty"`++ // ApiKey API key for authenticating with the voice server+ ApiKey *string `json:"api_key,omitempty"`++ // ApiSecret API secret for authenticating with the voice server+ ApiSecret *string `json:"api_secret,omitempty"`++ // Draining Whether placement sends new calls elsewhere while calls on this server finish+ Draining *bool `json:"draining,omitempty"`++ // Endpoint Client signal WebSocket endpoint URL for the voice server+ Endpoint *string `json:"endpoint,omitempty"`+ InternalEndpoint *VoiceInternalEndpointType `json:"internal_endpoint,omitempty"`++ // IsActive Whether the server is currently active+ IsActive *bool `json:"is_active,omitempty"`++ // Latitude Optional geographic latitude override for this server+ Latitude *float32 `json:"latitude,omitempty"`++ // Longitude Optional geographic longitude override for this server+ Longitude *float32 `json:"longitude,omitempty"`++ // Notes Free-form admin notes about this server, or null to clear them+ Notes *string `json:"notes,omitempty"`++ // RequiredGuildFeatures Guild features required to use this server+ RequiredGuildFeatures *[]string `json:"required_guild_features,omitempty"`++ // SoftConnectionLimit Connection count above which placement prefers another server, or null for no limit+ SoftConnectionLimit *int `json:"soft_connection_limit,omitempty"`++ // VipOnly Whether this server is restricted to VIP users+ VipOnly *bool `json:"vip_only,omitempty"`+}++// UpdateVoiceServerResponse defines model for UpdateVoiceServerResponse.+type UpdateVoiceServerResponse struct {+ // Server Updated voice server+ Server VoiceServerAdminResponse `json:"server"`+}+// UserAdminResponseSchema defines model for UserAdminResponseSchema.type UserAdminResponseSchema struct {AccentColor *Int32Type `json:"accent_color"`@@ -2816,6 +3129,51 @@ type UsernameType = string// VerificationBasisSchema What an account needs to finish setting uptype VerificationBasisSchema string+// VoiceInternalEndpointType URL the API uses for the control API and health checks of this server, such as http://voice-fra:7880+type VoiceInternalEndpointType = string++// VoiceJoinTokenAdminResponse defines model for VoiceJoinTokenAdminResponse.+type VoiceJoinTokenAdminResponse struct {+ // CreatedAt ISO 8601 timestamp when the token was created+ CreatedAt string `json:"created_at"`++ // ExpiresAt ISO 8601 timestamp when the token stops working+ ExpiresAt string `json:"expires_at"`++ // Hostname Hostname preset, or null+ Hostname *string `json:"hostname"`++ // Notes Notes for the new server, or null+ Notes *string `json:"notes"`++ // RegionId Region preset, or null+ RegionId *string `json:"region_id"`++ // RegionName Name of the new region it creates, or null+ RegionName *string `json:"region_name"`++ // RevokedAt ISO 8601 timestamp when it was revoked+ RevokedAt *string `json:"revoked_at"`++ // ServerId Server id preset, or null+ ServerId *string `json:"server_id"`++ // Status pending, used, expired or revoked+ Status VoiceJoinTokenStatus `json:"status"`++ // TokenId ID of the token+ TokenId SnowflakeStringType `json:"token_id"`++ // UsedAt ISO 8601 timestamp when a server joined with it+ UsedAt *string `json:"used_at"`++ // UsedServerId ID of the server that joined with it+ UsedServerId *string `json:"used_server_id"`+}++// VoiceJoinTokenStatus defines model for VoiceJoinTokenStatus.+type VoiceJoinTokenStatus string+// VoiceP2pConfigResponse defines model for VoiceP2pConfigResponse.type VoiceP2pConfigResponse struct {ConfigVersion int `json:"config_version"`@@ -2843,6 +3201,117 @@ type VoiceP2pConfigUpdateRequest struct {RolloutSalt *string `json:"rollout_salt,omitempty"`}+// VoiceRegionAdminResponse defines model for VoiceRegionAdminResponse.+type VoiceRegionAdminResponse struct {+ // AllowedGuildIds Guild IDs explicitly allowed to use this region+ AllowedGuildIds []SnowflakeStringType `json:"allowed_guild_ids"`++ // AllowedUserIds User IDs explicitly allowed to use this region+ AllowedUserIds []SnowflakeStringType `json:"allowed_user_ids"`++ // CreatedAt ISO 8601 timestamp when the region was created+ CreatedAt *string `json:"created_at"`++ // Emoji Emoji representing the region+ Emoji string `json:"emoji"`++ // Id Unique identifier for the voice region+ Id string `json:"id"`++ // IsDefault Whether this is the default region+ IsDefault bool `json:"is_default"`++ // Latitude Geographic latitude coordinate+ Latitude float32 `json:"latitude"`++ // Longitude Geographic longitude coordinate+ Longitude float32 `json:"longitude"`++ // Name Display name of the voice region+ Name string `json:"name"`++ // RequiredGuildFeatures Guild features required to use this region+ RequiredGuildFeatures []string `json:"required_guild_features"`++ // UpdatedAt ISO 8601 timestamp when the region was last updated+ UpdatedAt *string `json:"updated_at"`++ // VipOnly Whether this region is restricted to VIP users+ VipOnly bool `json:"vip_only"`+}++// VoiceServerAdminResponse defines model for VoiceServerAdminResponse.+type VoiceServerAdminResponse struct {+ // AllowedGuildIds Guild IDs explicitly allowed to use this server+ AllowedGuildIds []SnowflakeStringType `json:"allowed_guild_ids"`++ // AllowedUserIds User IDs explicitly allowed to use this server+ AllowedUserIds []SnowflakeStringType `json:"allowed_user_ids"`++ // CreatedAt ISO 8601 timestamp when the server was created+ CreatedAt *string `json:"created_at"`++ // Draining Whether placement sends new calls elsewhere while calls on this server finish+ Draining bool `json:"draining"`++ // DuplicateApiKey Whether another server uses the same API key+ DuplicateApiKey bool `json:"duplicate_api_key"`++ // Endpoint Client signal WebSocket endpoint URL for the voice server+ Endpoint string `json:"endpoint"`++ // Health Health of the server as this API node last saw it+ Health VoiceServerHealthResponse `json:"health"`++ // InternalEndpoint URL the API uses for the control API and health checks, or null to use the endpoint+ InternalEndpoint *string `json:"internal_endpoint"`++ // IsActive Whether the server is currently active+ IsActive bool `json:"is_active"`++ // Latitude Optional geographic latitude override for this server+ Latitude *float32 `json:"latitude"`++ // Longitude Optional geographic longitude override for this server+ Longitude *float32 `json:"longitude"`++ // Notes Free-form admin notes about this server, or null when there are none+ Notes *string `json:"notes"`++ // RegionId ID of the region this server belongs to+ RegionId string `json:"region_id"`++ // RequiredGuildFeatures Guild features required to use this server+ RequiredGuildFeatures []string `json:"required_guild_features"`++ // ServerId Unique identifier for the voice server+ ServerId string `json:"server_id"`++ // SoftConnectionLimit Connection count above which placement prefers another server, or null when the server has no limit+ SoftConnectionLimit *int `json:"soft_connection_limit"`++ // UpdatedAt ISO 8601 timestamp when the server was last updated+ UpdatedAt *string `json:"updated_at"`++ // VipOnly Whether this server is restricted to VIP users+ VipOnly bool `json:"vip_only"`+}++// VoiceServerHealthResponse defines model for VoiceServerHealthResponse.+type VoiceServerHealthResponse struct {+ // CheckedAt ISO 8601 timestamp of the last check, or null before the first+ CheckedAt *string `json:"checked_at"`++ // Error Status code or short transport error of the last failed check+ Error *string `json:"error"`++ // Status up, down, or unknown before the first check+ Status VoiceServerHealthResponseStatus `json:"status"`+}++// VoiceServerHealthResponseStatus up, down, or unknown before the first check+type VoiceServerHealthResponseStatus string+// adminApiKeyContextKey is the context key for adminApiKey security schemetype adminApiKeyContextKey string@@ -2896,6 +3365,12 @@ type DeleteAdminUserMessagesParams struct {DryRun *string `form:"dry_run,omitempty" json:"dry_run,omitempty"`}+// ListAdminVoiceRegionsParams defines parameters for ListAdminVoiceRegions.+type ListAdminVoiceRegionsParams struct {+ // IncludeServers Whether to include voice servers in the response+ IncludeServers *string `form:"include_servers,omitempty" json:"include_servers,omitempty"`+}+// UpdateAdminInstanceConfigJSONRequestBody defines body for UpdateAdminInstanceConfig for application/json ContentType.type UpdateAdminInstanceConfigJSONRequestBody = InstanceConfigUpdateRequest@@ -2926,6 +3401,18 @@ type ScheduleAdminUserDeletionJSONRequestBody = AdminUserDeletionScheduleRequest// DeleteAdminUserMessagesJSONRequestBody defines body for DeleteAdminUserMessages for application/json ContentType.type DeleteAdminUserMessagesJSONRequestBody = AdminUserMessageDeleteRequest+// CreateAdminVoiceJoinTokenJSONRequestBody defines body for CreateAdminVoiceJoinToken for application/json ContentType.+type CreateAdminVoiceJoinTokenJSONRequestBody = CreateVoiceJoinTokenRequest++// CreateAdminVoiceRegionJSONRequestBody defines body for CreateAdminVoiceRegion for application/json ContentType.+type CreateAdminVoiceRegionJSONRequestBody = CreateVoiceRegionRequest++// CreateAdminVoiceServerJSONRequestBody defines body for CreateAdminVoiceServer for application/json ContentType.+type CreateAdminVoiceServerJSONRequestBody = CreateVoiceServerRequestBody++// UpdateAdminVoiceServerJSONRequestBody defines body for UpdateAdminVoiceServer for application/json ContentType.+type UpdateAdminVoiceServerJSONRequestBody = UpdateVoiceServerRequestBody+// Getter for additional properties for Error. Returns the specified// element and whether it was foundfunc (a Error) Get(fieldName string) (value interface{}, found bool) {diff --git a/fluxer_rexctl/internal/adminapi/oapi-codegen.yaml b/fluxer_rexctl/internal/adminapi/oapi-codegen.yamlindex d8b222527..e2e37d844 100644--- a/fluxer_rexctl/internal/adminapi/oapi-codegen.yaml+++ b/fluxer_rexctl/internal/adminapi/oapi-codegen.yaml@@ -25,3 +25,12 @@ output-options:- update_admin_instance_config- create_admin_instance_branding_asset- create_admin_instance_smtp_test+ - list_admin_voice_regions+ - create_admin_voice_region+ - create_admin_voice_server+ - update_admin_voice_server+ - delete_admin_voice_server+ - create_admin_voice_server_test_token+ - create_admin_voice_join_token+ - list_admin_voice_join_tokens+ - revoke_admin_voice_join_tokendiff --git a/fluxer_rexctl/internal/adminapi/voice.go b/fluxer_rexctl/internal/adminapi/voice.gonew file mode 100644index 000000000..a5b26e743--- /dev/null+++ b/fluxer_rexctl/internal/adminapi/voice.go@@ -0,0 +1,80 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package adminapi++import (+ "context"+ "net/http"+ "net/url"+)++type VoiceRegion struct {+ VoiceRegionAdminResponse+ Servers []VoiceServerAdminResponse `json:"servers"`+}++func (c *Client) VoiceRegions(ctx context.Context) ([]VoiceRegion, error) {+ var resp struct {+ Regions []VoiceRegion `json:"regions"`+ }+ if err := c.Call(ctx, http.MethodGet, "/admin/voice/regions", map[string]string{"include_servers": "true"}, nil, &resp); err != nil {+ return nil, err+ }+ return resp.Regions, nil+}++func (c *Client) CreateVoiceRegion(ctx context.Context, req CreateVoiceRegionRequest) error {+ return c.Call(ctx, http.MethodPost, "/admin/voice/regions", nil, req, nil)+}++func voiceServerPath(region, server string) string {+ return "/admin/voice/regions/" + url.PathEscape(region) + "/servers/" + url.PathEscape(server)+}++func (c *Client) CreateVoiceServer(ctx context.Context, region string, req CreateVoiceServerRequestBody) (*VoiceServerAdminResponse, error) {+ var resp CreateVoiceServerResponse+ if err := c.Call(ctx, http.MethodPost, "/admin/voice/regions/"+url.PathEscape(region)+"/servers", nil, req, &resp); err != nil {+ return nil, err+ }+ return &resp.Server, nil+}++func (c *Client) UpdateVoiceServer(ctx context.Context, region, server string, req UpdateVoiceServerRequestBody) (*VoiceServerAdminResponse, error) {+ var resp UpdateVoiceServerResponse+ if err := c.Call(ctx, http.MethodPatch, voiceServerPath(region, server), nil, req, &resp); err != nil {+ return nil, err+ }+ return &resp.Server, nil+}++func (c *Client) DeleteVoiceServer(ctx context.Context, region, server string) error {+ return c.Call(ctx, http.MethodDelete, voiceServerPath(region, server), nil, nil, nil)+}++func (c *Client) VoiceTestToken(ctx context.Context, region, server string) (*CreateVoiceServerTestTokenResponse, error) {+ var resp CreateVoiceServerTestTokenResponse+ if err := c.Call(ctx, http.MethodPost, voiceServerPath(region, server)+"/test-token", nil, nil, &resp); err != nil {+ return nil, err+ }+ return &resp, nil+}++func (c *Client) CreateVoiceJoinToken(ctx context.Context, req CreateVoiceJoinTokenRequest) (*CreateVoiceJoinTokenResponse, error) {+ var resp CreateVoiceJoinTokenResponse+ if err := c.Call(ctx, http.MethodPost, "/admin/voice/join-tokens", nil, req, &resp); err != nil {+ return nil, err+ }+ return &resp, nil+}++func (c *Client) VoiceJoinTokens(ctx context.Context) ([]VoiceJoinTokenAdminResponse, error) {+ var resp ListVoiceJoinTokensResponse+ if err := c.Call(ctx, http.MethodGet, "/admin/voice/join-tokens", nil, nil, &resp); err != nil {+ return nil, err+ }+ return resp.Tokens, nil+}++func (c *Client) RevokeVoiceJoinToken(ctx context.Context, id string) error {+ return c.Call(ctx, http.MethodDelete, "/admin/voice/join-tokens/"+url.PathEscape(id), nil, nil, nil)+}diff --git a/fluxer_rexctl/internal/checks/bundledvoice_test.go b/fluxer_rexctl/internal/checks/bundledvoice_test.gonew file mode 100644index 000000000..bf4d5b4e9--- /dev/null+++ b/fluxer_rexctl/internal/checks/bundledvoice_test.go@@ -0,0 +1,39 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package checks++import (+ "context"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+)++func TestInstallWithoutBundledVoiceWantsNoVoicePorts(t *testing.T) {+ e := &Env{prepared: true, Settings: map[string]string{stack.VoiceBundledKey: "false"}}+ for _, p := range e.wantedPorts() {+ if p.Number == 7881 || p.Number == 7882 {+ t.Fatalf("wanted ports %v include a voice port", e.wantedPorts())+ }+ }+ e = &Env{prepared: true, Settings: map[string]string{}}+ voice := 0+ for _, p := range e.wantedPorts() {+ if p.Number == 7881 || p.Number == 7882 {+ voice+++ }+ }+ if voice != 2 {+ t.Fatalf("wanted ports %v, want both voice ports", e.wantedPorts())+ }+}++func TestLiveKitIPSkipsWithoutTheBundledLiveKit(t *testing.T) {+ e := &Env{prepared: true, psDone: true, Instance: &instance.Instance{State: &instance.State{}},+ ps: []engine.Container{{Service: "api", State: "running"}, {Service: "edge", State: "running"}}}+ if r := runLiveKitIP(context.Background(), e); r.Status != Skip {+ t.Fatalf("result = %+v, want a skip", r)+ }+}diff --git a/fluxer_rexctl/internal/checks/checks.go b/fluxer_rexctl/internal/checks/checks.goindex a6af31e6e..663b38f36 100644--- a/fluxer_rexctl/internal/checks/checks.go+++ b/fluxer_rexctl/internal/checks/checks.go@@ -27,6 +27,7 @@ type Phase intconst (Preinstall Phase = 1 << iotaInstalled+ Voice)type Result struct {@@ -109,6 +110,15 @@ var order = []string{"postgres-password","postgres-scram","livekit-ip",+ "voice-servers",+ "voice-endpoints",+ "voice-ports",+ "voice-node-ip",+ "voice-tls",+ "voice-registration",+ "voice-webhook",+ "voice-keys",+ "voice-health","client-ip","sso-provider","backup-age",@@ -173,7 +183,7 @@ func RunOne(ctx context.Context, env *Env, c Check) Result {env.prepare(ctx)r := c.Run(ctx, env)r.ID = c.ID- if env.phase&Installed == 0 {+ if env.phase&(Installed|Voice) == 0 {r.Fix = strings.Replace(r.Fix, "then run rexctl doctor.", "then run rexctl install.", 1)}r.Fixable = r.Fixable && c.Fix != nil && (r.Status == Warn || r.Status == Fail)diff --git a/fluxer_rexctl/internal/checks/disk.go b/fluxer_rexctl/internal/checks/disk.goindex d63618ddf..fe04b7d64 100644--- a/fluxer_rexctl/internal/checks/disk.go+++ b/fluxer_rexctl/internal/checks/disk.go@@ -22,7 +22,7 @@ const ()func init() {- Register(Check{ID: "disk", Phases: Preinstall | Installed, Run: runDisk})+ Register(Check{ID: "disk", Phases: Preinstall | Installed | Voice, Run: runDisk})}func runDisk(ctx context.Context, e *Env) Result {diff --git a/fluxer_rexctl/internal/checks/engine.go b/fluxer_rexctl/internal/checks/engine.goindex b6029b213..f9cc96033 100644--- a/fluxer_rexctl/internal/checks/engine.go+++ b/fluxer_rexctl/internal/checks/engine.go@@ -15,7 +15,7 @@ import ()func init() {- Register(Check{ID: "engine", Phases: Preinstall | Installed, Run: runEngine, Fix: fixEngine, SafeFix: true})+ Register(Check{ID: "engine", Phases: Preinstall | Installed | Voice, Run: runEngine, Fix: fixEngine, SafeFix: true})}func runEngine(ctx context.Context, e *Env) Result {diff --git a/fluxer_rexctl/internal/checks/env.go b/fluxer_rexctl/internal/checks/env.goindex 50809a4e6..fb1c41f51 100644--- a/fluxer_rexctl/internal/checks/env.go+++ b/fluxer_rexctl/internal/checks/env.go@@ -63,7 +63,7 @@ func (e *Env) prepare(ctx context.Context) {}e.Engine, e.engineErr = engine.Connect(ctx, opts)}- if e.Project == nil && e.Engine != nil && e.Instance != nil && e.Instance.Project() != "" {+ if e.Project == nil && e.Engine != nil && e.Instance != nil && e.Instance.Project() != "" && !e.Instance.State.IsVoice() {p, err := deploy.Load(ctx, e.Instance, e.Engine)if err != nil {e.loadErr = err@@ -291,6 +291,7 @@ func (e *Env) ports() []stack.Port {return stack.Ports(map[string]string{stack.VoiceTCPPortKey: e.value(stack.VoiceTCPPortKey),stack.VoiceUDPPortKey: e.value(stack.VoiceUDPPortKey),+ stack.VoiceBundledKey: e.value(stack.VoiceBundledKey),})}diff --git a/fluxer_rexctl/internal/checks/voice.go b/fluxer_rexctl/internal/checks/voice.gonew file mode 100644index 000000000..f9e54147f--- /dev/null+++ b/fluxer_rexctl/internal/checks/voice.go@@ -0,0 +1,401 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package checks++import (+ "bytes"+ "context"+ "fmt"+ "net/http"+ "net/url"+ "path/filepath"+ "slices"+ "strings"+ "time"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/deploy"+ "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/ui"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++func init() {+ Register(Check{ID: "voice-servers", Phases: Installed | Voice, Run: runVoiceServers})+ Register(Check{ID: "voice-endpoints", Phases: Installed, Run: runVoiceEndpoints, Fix: fixVoiceEndpoints})+ Register(Check{ID: "voice-ports", Phases: Installed | Voice, Run: runVoicePorts})+ Register(Check{ID: "voice-node-ip", Phases: Installed | Voice, Run: runVoiceNodeIP})+ Register(Check{ID: "voice-tls", Phases: Voice, Run: runVoiceTLS})+ Register(Check{ID: "voice-registration", Phases: Voice, Run: runVoiceRegistration})+ Register(Check{ID: "voice-webhook", Phases: Installed | Voice, Run: runVoiceWebhook})+ Register(Check{ID: "voice-keys", Phases: Installed, Run: runVoiceKeys})+ Register(Check{ID: "voice-health", Phases: Installed, Run: runVoiceHealth})+}++var voiceHTTP = &http.Client{Timeout: 15 * time.Second}++func (e *Env) voiceServers() []instance.VoiceServer {+ if e.Instance == nil || e.Instance.State == nil {+ return nil+ }+ return e.Instance.State.Voice+}++func (e *Env) voiceProbeTarget(s instance.VoiceServer) (string, string) {+ switch {+ case s.Edge == voice.EdgeNone:+ return voice.LocalBase(s.HTTPPort), ""+ 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"))+}++func (e *Env) voiceProject(s instance.VoiceServer) *engine.Project {+ return e.Engine.Named(s.Project, voice.ServerDir(e.Instance.Dir, s.ID))+}++func (e *Env) voiceLogs(ctx context.Context, s instance.VoiceServer, tail int) string {+ var buf bytes.Buffer+ if err := e.voiceProject(s).Logs(ctx, engine.LogsOptions{Tail: tail, Stdout: &buf}, "livekit"); err != nil {+ return ""+ }+ return buf.String()+}++func runVoiceServers(ctx context.Context, e *Env) Result {+ servers := e.voiceServers()+ if len(servers) == 0 || e.Engine == nil {+ return skip("")+ }+ var bad []string+ for _, s := range servers {+ cs, err := e.voiceProject(s).PS(ctx)+ healthy := err == nil && len(cs) > 0 && cs[0].State == "running" && (cs[0].Health == "" || cs[0].Health == "healthy")+ if !healthy {+ bad = append(bad, s.ID+" isn't running")+ continue+ }+ base, dial := e.voiceProbeTarget(s)+ if err := voice.ProbeThroughEdge(ctx, base, dial, true); err != nil {+ bad = append(bad, s.ID+" doesn't answer through the edge")+ }+ }+ if len(bad) > 0 {+ r := fail(strings.Join(bad, ", ")+".", "Run rexctl voice status to see why, and rexctl voice remove <id> for a server you no longer need.")+ r.Details = bad+ return r+ }+ if len(servers) == 1 {+ return pass("The voice server runs and answers.")+ }+ return pass(fmt.Sprintf("All %d voice servers run and answer.", len(servers)))+}++func runVoiceEndpoints(_ context.Context, e *Env) Result {+ if e.Instance == nil || e.Instance.State == nil || e.Instance.State.IsVoice() {+ return skip("")+ }+ if !slices.ContainsFunc(e.Instance.State.Voice, func(s instance.VoiceServer) bool { return s.Edge == voice.EdgeInstance }) {+ return skip("")+ }+ stale := deploy.StaleVoiceEndpoints(e.Instance)+ if len(stale) == 0 {+ return pass("Every voice server uses your instance's address.")+ }+ origin := voice.WSOrigin(e.Instance.Origin())+ var details []string+ for _, id := range stale {+ if s := e.Instance.State.VoiceServer(id); s != nil {+ details = append(details, fmt.Sprintf("%s uses %s", id, s.Endpoint))+ }+ }+ r := fail(fmt.Sprintf("%s still %s the old address of your instance.", ui.JoinAnd(stale), pick(len(stale), "uses", "use")), fmt.Sprintf("Run rexctl doctor --fix to move %s to %s.", pick(len(stale), "it", "them"), origin))+ r.Details = details+ r.Fixable = true+ return r+}++func fixVoiceEndpoints(ctx context.Context, e *Env) error {+ if e.Admin == nil {+ return ErrNoFix+ }+ _, err := deploy.MoveVoiceEndpoints(ctx, e.Admin, e.Instance)+ return err+}++func runVoicePorts(ctx context.Context, e *Env) Result {+ servers := e.voiceServers()+ if len(servers) == 0 || e.Engine == nil {+ return skip("")+ }+ owners, err := e.Engine.PublishedHostPorts(ctx)+ if err != nil {+ return skip("")+ }+ var problems []string+ var ports []host.Port+ for _, s := range servers {+ for _, p := range []host.Port{{Number: s.TCPPort, Proto: "tcp"}, {Number: s.UDPPort, Proto: "udp"}} {+ ports = append(ports, p)+ mine, other := false, ""+ for _, o := range owners {+ if o.Number == p.Number && o.Proto == p.Proto {+ if o.Project == s.Project {+ mine = true+ } else {+ other = o.Project+ }+ }+ }+ switch {+ case other != "":+ problems = append(problems, fmt.Sprintf("%s of %s is also published by %s", p, s.ID, other))+ case !mine:+ problems = append(problems, fmt.Sprintf("%s of %s isn't published", p, s.ID))+ }+ }+ }+ if fw, err := host.DetectFirewall(ctx); err == nil && fw != host.FirewallNone {+ if closed, err := host.ClosedPorts(ctx, fw, ports); err == nil {+ for _, p := range closed {+ problems = append(problems, fmt.Sprintf("%s is closed in %s", p, fw))+ }+ }+ }+ if len(problems) > 0 {+ r := fail(strings.Join(problems, ", ")+".", "Open the ports in the firewall on this machine and at your provider, and run rexctl voice status.")+ r.Details = problems+ return r+ }+ return pass("Every voice port is published. Check that your provider's firewall lets them in too.")+}++func runVoiceNodeIP(ctx context.Context, e *Env) Result {+ servers := e.voiceServers()+ if len(servers) == 0 || e.Engine == nil {+ return skip("")+ }+ public, perr := e.publicAddress(ctx)+ var problems []string+ for _, s := range servers {+ if s.NodeIP != "" {+ if perr == nil && s.NodeIP != public && s.BindIP == "" {+ problems = append(problems, fmt.Sprintf("%s gives clients %s and this machine's public address is %s", s.ID, s.NodeIP, public))+ }+ continue+ }+ if strings.Contains(strings.ToLower(e.voiceLogs(ctx, s, 300)), "could not resolve external ip") {+ problems = append(problems, s.ID+" can't find its public address")+ }+ }+ if len(problems) > 0 {+ fix := "Run rexctl config set voice.public_ip with the right address. A server added with --ip keeps its address. Remove it and add it again with the right --ip."+ if e.Instance.State.IsVoice() {+ fix = "Run rexctl voice remove <id>, then rexctl voice join with a new token and --ip set to the right address."+ }+ return warn(strings.Join(problems, ", ")+".", fix)+ }+ return pass("Voice servers give clients the right address.")+}++func runVoiceTLS(ctx context.Context, e *Env) Result {+ var problems []string+ checked := 0+ for _, s := range e.voiceServers() {+ if s.Edge != voice.EdgeOwn {+ continue+ }+ checked+++ exp, err := voice.CertificateExpiry(ctx, s.Endpoint, "127.0.0.1:443")+ switch {+ case err != nil:+ problems = append(problems, fmt.Sprintf("%s has no valid certificate (%v)", s.Endpoint, err))+ case time.Until(exp) < 48*time.Hour:+ problems = append(problems, fmt.Sprintf("the certificate of %s expires %s", s.Endpoint, exp.Local().Format("2 January 15:04")))+ }+ }+ if checked == 0 {+ return skip("")+ }+ if len(problems) > 0 {+ return fail(strings.Join(problems, ", ")+".", "Check that ports 80 and 443 are open to the internet. docker logs fluxer-voice-edge-edge-1 shows the ACME error.")+ }+ return pass("Voice certificates are valid for more than 2 days.")+}++func runVoiceRegistration(ctx context.Context, e *Env) Result {+ servers := e.voiceServers()+ if len(servers) == 0 {+ return skip("")+ }+ client := &voice.Client{HTTP: voiceHTTP}+ var problems []string+ fix := "Run rexctl voice status, and rexctl voice remove <id> for a server the instance no longer knows."+ for _, s := range servers {+ node, err := client.GetNode(ctx, s.NodeURL, s.Key, s.Secret)+ switch voice.CodeOf(err) {+ case "":+ case voice.CodeNodeUnknown:+ problems = append(problems, fmt.Sprintf("%s was deleted on %s", s.ID, s.Instance))+ fix = "Run rexctl voice remove " + s.ID + ", then join again with a new token from rexctl voice token."+ continue+ case voice.CodeTokenExpired:+ return fail(voice.ClockProblem(err, s.Instance, time.Now())+".", voice.ClockFix)+ case voice.CodeSignatureInvalid:+ problems = append(problems, fmt.Sprintf("%s has another secret on %s", s.ID, s.Instance))+ fix = "Put the server's secret back in the admin panel. It's FLUXER_VOICE_API_SECRET in " + filepath.Join(voice.ServerDir(e.Instance.Dir, s.ID), voice.EnvFile) + ". Or run rexctl voice remove " + s.ID + " and join again with a new token."+ continue+ }+ if err != nil {+ problems = append(problems, fmt.Sprintf("%s can't connect to %s (%v)", s.ID, s.Instance, err))+ continue+ }+ if node.Endpoint != s.Endpoint || node.RegionID != s.Region {+ problems = append(problems, fmt.Sprintf("%s is registered with %s in %s, and this machine serves %s in %s", s.ID, node.Endpoint, node.RegionID, s.Endpoint, s.Region))+ }+ }+ if len(problems) > 0 {+ return fail(strings.Join(problems, ", ")+".", fix)+ }+ return pass("Every voice server is registered with its instance.")+}++func runVoiceWebhook(ctx context.Context, e *Env) Result {+ servers := e.voiceServers()+ if len(servers) == 0 || e.Engine == nil {+ return skip("")+ }+ client := &voice.Client{HTTP: voiceHTTP}+ var problems []string+ rejected := false+ for _, s := range servers {+ logs := strings.ToLower(e.voiceLogs(ctx, s, 2000))+ if i := strings.LastIndex(logs, "failed to send webhook"); i >= 0 && i > strings.LastIndex(logs, "sent webhook") {+ problems = append(problems, fmt.Sprintf("%s can't send webhooks to %s", s.ID, s.Webhook))+ continue+ }+ problem, fault := webhookProblem(ctx, client, s, e.webhookTarget(s))+ switch fault {+ case webhookClock:+ return fail(problem+".", voice.ClockFix)+ case webhookSecret:+ rejected = true+ }+ if problem != "" {+ problems = append(problems, problem)+ }+ }+ if len(problems) > 0 {+ fix := "Check that the webhook URL answers, and that no firewall or bot protection in front of the instance blocks it."+ if rejected {+ fix = "Put the server's own secret back in the admin panel. It's FLUXER_VOICE_API_SECRET in voice/<id>/.env in " + e.Instance.Dir + "."+ }+ return fail(strings.Join(problems, ", ")+".", fix)+ }+ return pass("Your instance accepts webhooks from every voice server.")+}++func (e *Env) webhookTarget(s instance.VoiceServer) string {+ u, err := url.Parse(s.Webhook)+ if err != nil || u.Hostname() != "api" || e.Instance == nil {+ return s.Webhook+ }+ return strings.TrimRight(e.Instance.Origin(), "/") + "/api" + u.Path+}++type webhookFault int++const (+ webhookOK webhookFault = iota+ webhookDown+ webhookSecret+ webhookClock+)++func webhookProblem(ctx context.Context, client *voice.Client, s instance.VoiceServer, target string) (string, webhookFault) {+ now := time.Now()+ status, date, err := client.TestWebhook(ctx, target, s.Key, s.Secret, now)+ host := target+ if u, perr := url.Parse(target); perr == nil {+ host = u.Host+ }+ rejected := status == http.StatusBadRequest || status == http.StatusUnauthorized+ switch {+ case err != nil:+ return fmt.Sprintf("%s can't connect to %s (%v)", s.ID, target, err), webhookDown+ case rejected && voice.ClockOff(date, host, now) != "":+ return voice.ClockOff(date, host, now), webhookClock+ case rejected:+ return fmt.Sprintf("%s rejects webhooks signed with the secret of %s", target, s.ID), webhookSecret+ case status < 200 || status > 299:+ return fmt.Sprintf("%s answers webhooks from %s with HTTP %d", target, s.ID, status), webhookDown+ }+ return "", webhookOK+}++func runVoiceKeys(ctx context.Context, e *Env) Result {+ if e.Admin == nil || e.Instance == nil || e.Instance.State.IsVoice() {+ return skip("")+ }+ regions, err := e.Admin.VoiceRegions(ctx)+ if err != nil {+ return skip("")+ }+ var shared []string+ for _, r := range regions {+ for _, s := range r.Servers {+ if s.DuplicateApiKey {+ shared = append(shared, s.ServerId)+ }+ }+ }+ if len(shared) > 0 {+ slices.Sort(shared)+ return warn(fmt.Sprintf("Voice servers %s share an API key.", ui.JoinAnd(shared)),+ "Give each server its own key. Remove the newer server and add it again with rexctl voice add or rexctl voice join.")+ }+ return pass("Every voice server has its own API key.")+}++func runVoiceHealth(ctx context.Context, e *Env) Result {+ if e.Admin == nil || e.Instance == nil || e.Instance.State.IsVoice() {+ return skip("")+ }+ regions, err := e.Admin.VoiceRegions(ctx)+ if err != nil {+ return skip("")+ }+ var down, stuck []string+ for _, r := range regions {+ for _, s := range r.Servers {+ if !s.IsActive {+ continue+ }+ if s.Health.Status == "down" {+ down = append(down, s.ServerId+" at "+s.Endpoint)+ }+ if s.Draining && s.UpdatedAt != nil {+ if t, err := time.Parse(time.RFC3339, *s.UpdatedAt); err == nil && time.Since(t) > time.Hour {+ stuck = append(stuck, s.ServerId)+ }+ }+ }+ }+ switch {+ case len(down) > 0:+ return fail(fmt.Sprintf("%s %s down.", ui.JoinAnd(down), pick(len(down), "is", "are")), "Run rexctl voice status on the machine that runs each one.")+ case len(stuck) > 0:+ return warn(fmt.Sprintf("%s %s been draining for more than an hour.", ui.JoinAnd(stuck), pick(len(stuck), "has", "have")), "Clear Draining in the admin panel, or remove the servers.")+ }+ return pass("Every active voice server is up.")+}++func pick(n int, one, many string) string {+ if n == 1 {+ return one+ }+ return many+}diff --git a/fluxer_rexctl/internal/checks/voice_test.go b/fluxer_rexctl/internal/checks/voice_test.gonew file mode 100644index 000000000..0ea3ec135--- /dev/null+++ b/fluxer_rexctl/internal/checks/voice_test.go@@ -0,0 +1,105 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package checks++import (+ "context"+ "crypto/hmac"+ "crypto/sha256"+ "encoding/base64"+ "encoding/json"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"+ "time"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++func webhookReceiver(t *testing.T, secret string) *httptest.Server {+ t.Helper()+ return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ body, _ := io.ReadAll(r.Body)+ parts := strings.Split(r.Header.Get("Authorization"), ".")+ if len(parts) != 3 {+ w.WriteHeader(http.StatusBadRequest)+ return+ }+ mac := hmac.New(sha256.New, []byte(secret))+ mac.Write([]byte(parts[0] + "." + parts[1]))+ if base64.RawURLEncoding.EncodeToString(mac.Sum(nil)) != parts[2] {+ w.WriteHeader(http.StatusBadRequest)+ return+ }+ payload, _ := base64.RawURLEncoding.DecodeString(parts[1])+ var claims map[string]any+ _ = json.Unmarshal(payload, &claims)+ sum := sha256.Sum256(body)+ var event struct {+ Room struct{ Name string } `json:"room"`+ }+ _ = json.Unmarshal(body, &event)+ if claims["sha256"] != base64.StdEncoding.EncodeToString(sum[:]) || claims["iss"] != "fluxer-fra-server-1" || !strings.HasPrefix(event.Room.Name, voice.TestRoomPrefix) {+ w.WriteHeader(http.StatusBadRequest)+ return+ }+ w.WriteHeader(http.StatusOK)+ }))+}++func TestWebhookCheckSendsASignedTestEvent(t *testing.T) {+ srv := webhookReceiver(t, "right")+ defer srv.Close()+ client := &voice.Client{HTTP: srv.Client()}+ s := instance.VoiceServer{ID: "fra-server-1", Key: "fluxer-fra-server-1", Secret: "right"}+ if problem, _ := webhookProblem(context.Background(), client, s, srv.URL); problem != "" {+ t.Errorf("right secret: %s", problem)+ }+ s.Secret = "wrong"+ problem, fault := webhookProblem(context.Background(), client, s, srv.URL)+ if fault != webhookSecret || !strings.Contains(problem, "signed with the secret of fra-server-1") {+ t.Errorf("wrong secret: %q, %v", problem, fault)+ }+}++func TestWebhookTargetForServersOnTheFluxerMachine(t *testing.T) {+ inst := &instance.Instance{State: &instance.State{Settings: map[string]string{}}}+ inst.State.Settings["FLUXER_PUBLIC_ORIGIN"] = "https://chat.example.com"+ e := &Env{Instance: inst}+ if got := e.webhookTarget(instance.VoiceServer{Webhook: "http://api:8080/webhooks/livekit"}); got != "https://chat.example.com/api/webhooks/livekit" {+ t.Errorf("same machine: %s", got)+ }+ if got := e.webhookTarget(instance.VoiceServer{Webhook: "https://chat.example.com/api/webhooks/livekit"}); got != "https://chat.example.com/api/webhooks/livekit" {+ t.Errorf("joined: %s", got)+ }+}++func TestVoiceEndpointsCheckSeesAnOldDomain(t *testing.T) {+ inst := &instance.Instance{State: &instance.State{Settings: map[string]string{"FLUXER_PUBLIC_ORIGIN": "https://new.example.com"}}}+ inst.State.Voice = []instance.VoiceServer{{ID: "default-server-2", Endpoint: "wss://old.example.com/livekit-default-server-2", Edge: voice.EdgeInstance}}+ r := runVoiceEndpoints(context.Background(), &Env{Instance: inst})+ if r.Status != Fail || !r.Fixable || !strings.Contains(r.Message, "default-server-2") || !strings.Contains(r.Fix, "wss://new.example.com") {+ t.Fatalf("old domain: %+v", r)+ }+ inst.State.Voice[0].Endpoint = "wss://new.example.com/livekit-default-server-2"+ if r := runVoiceEndpoints(context.Background(), &Env{Instance: inst}); r.Status != Pass {+ t.Fatalf("new domain: %+v", r)+ }+}++func TestWebhookCheckReportsAClockThatIsOff(t *testing.T) {+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Date", time.Now().Add(-3*time.Minute).UTC().Format(http.TimeFormat))+ w.WriteHeader(http.StatusUnauthorized)+ }))+ defer srv.Close()+ client := &voice.Client{HTTP: srv.Client()}+ s := instance.VoiceServer{ID: "fra-server-1", Key: "fluxer-fra-server-1", Secret: "right"}+ problem, fault := webhookProblem(context.Background(), client, s, srv.URL)+ if fault != webhookClock || !strings.Contains(problem, "about 3 minutes ahead") {+ t.Fatalf("clock ahead: %q, %v", problem, fault)+ }+}diff --git a/fluxer_rexctl/internal/deploy/api.go b/fluxer_rexctl/internal/deploy/api.goindex 928be969c..cd8abea74 100644--- a/fluxer_rexctl/internal/deploy/api.go+++ b/fluxer_rexctl/internal/deploy/api.go@@ -28,4 +28,5 @@ type Change struct {Applied boolReverted boolUnchanged bool+ VoiceDown []string}diff --git a/fluxer_rexctl/internal/deploy/ports.go b/fluxer_rexctl/internal/deploy/ports.goindex 5e505bf5f..f27256eac 100644--- a/fluxer_rexctl/internal/deploy/ports.go+++ b/fluxer_rexctl/internal/deploy/ports.go@@ -14,6 +14,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/settings"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack""github.com/fluxerapp/fluxer/fluxer_rexctl/internal/ui")@@ -21,13 +22,18 @@ type portVar struct {name stringdef intprotos []string+ voice bool}var portVars = []portVar{- {"FLUXER_HTTP_PORT", 80, []string{"tcp"}},- {"FLUXER_HTTPS_PORT", 443, []string{"tcp", "udp"}},- {"FLUXER_LIVEKIT_TCP_PORT", 7881, []string{"tcp"}},- {"FLUXER_LIVEKIT_UDP_PORT", 7882, []string{"udp"}},+ {"FLUXER_HTTP_PORT", 80, []string{"tcp"}, false},+ {"FLUXER_HTTPS_PORT", 443, []string{"tcp", "udp"}, false},+ {"FLUXER_LIVEKIT_TCP_PORT", 7881, []string{"tcp"}, true},+ {"FLUXER_LIVEKIT_UDP_PORT", 7882, []string{"udp"}, true},+}++func published(env map[string]string, v portVar) bool {+ return !v.voice || stack.VoiceBundled(env[stack.VoiceBundledKey])}func portKeyChanged(changed []string) bool {@@ -50,12 +56,17 @@ func portOf(env map[string]string, v portVar) int {func portDiff(oldEnv, newEnv map[string]string) (added, removed []host.Port) {for _, v := range portVars {was, will := portOf(oldEnv, v), portOf(newEnv, v)- if was == will {+ wasOn, willOn := published(oldEnv, v), published(newEnv, v)+ if was == will && wasOn == willOn {continue}for _, proto := range v.protos {- added = append(added, host.Port{Number: will, Proto: proto})- removed = append(removed, host.Port{Number: was, Proto: proto})+ if willOn {+ added = append(added, host.Port{Number: will, Proto: proto})+ }+ if wasOn {+ removed = append(removed, host.Port{Number: was, Proto: proto})+ }}}return added, removed@@ -101,6 +112,12 @@ func checkPortsFree(ctx context.Context, e *engine.Engine, self string, ports []}}}+ for _, v := range instance.RecordedVoicePorts() {+ key := host.Port{Number: v.Number, Proto: v.Proto}+ if _, ok := published[key]; !ok {+ published[key] = "the voice server " + v.Server+ }+ }seen := map[host.Port]bool{}for _, p := range ports {if seen[p] {@@ -146,6 +163,9 @@ func effectivePorts(env map[string]string, mode string) []portRole {portRole{Port: host.Port{Number: https, Proto: "udp"}, subject: "The web address", noun: "the web address", setting: settings.EdgeHTTPSPort},)}+ if !published(env, portVars[2]) {+ return out+ }tcp := portOf(env, portVars[2])udp := portOf(env, portVars[3])out = append(out,diff --git a/fluxer_rexctl/internal/deploy/ports_test.go b/fluxer_rexctl/internal/deploy/ports_test.goindex d7ae6dd63..904733f7c 100644--- a/fluxer_rexctl/internal/deploy/ports_test.go+++ b/fluxer_rexctl/internal/deploy/ports_test.go@@ -100,3 +100,32 @@ func samePorts(a, b []host.Port) bool {}return true}++func TestPortDiffTurningBundledVoiceOffAndOn(t *testing.T) {+ on := map[string]string{"FLUXER_LIVEKIT_TCP_PORT": "7883"}+ off := map[string]string{"FLUXER_LIVEKIT_TCP_PORT": "7883", "FLUXER_LIVEKIT_BUNDLED": "false"}+ voicePorts := []host.Port{{Number: 7883, Proto: "tcp"}, {Number: 7882, Proto: "udp"}}+ added, removed := portDiff(on, off)+ if len(added) != 0 || !samePorts(removed, voicePorts) {+ t.Errorf("turning bundled voice off: added %v removed %v", added, removed)+ }+ added, removed = portDiff(off, on)+ if !samePorts(added, voicePorts) || len(removed) != 0 {+ t.Errorf("turning bundled voice on: added %v removed %v", added, removed)+ }+ if added, removed := portDiff(off, map[string]string{"FLUXER_LIVEKIT_TCP_PORT": "7885", "FLUXER_LIVEKIT_BUNDLED": "false"}); len(added)+len(removed) != 0 {+ t.Errorf("moving an unused voice port: added %v removed %v", added, removed)+ }+}++func TestEffectivePortsWithoutBundledVoice(t *testing.T) {+ env := map[string]string{"FLUXER_LIVEKIT_TCP_PORT": "443", "FLUXER_LIVEKIT_BUNDLED": "false"}+ if err := checkPortsDistinct(effectivePorts(env, settings.EdgeBundled)); err != nil {+ t.Errorf("an unused voice port can't clash, got %v", err)+ }+ for _, p := range effectivePorts(env, settings.EdgeBundled) {+ if p.subject == "Voice" {+ t.Errorf("effective ports list %v without the bundled LiveKit", p)+ }+ }+}diff --git a/fluxer_rexctl/internal/deploy/resolve.go b/fluxer_rexctl/internal/deploy/resolve.goindex 49fe3104f..831eb07f6 100644--- a/fluxer_rexctl/internal/deploy/resolve.go+++ b/fluxer_rexctl/internal/deploy/resolve.go@@ -43,8 +43,12 @@ func Resolve(ctx context.Context, inst *instance.Instance, e *engine.Engine) errif err := save(inst); err != nil {return err}+ var detached []AttachedVoiceif ipv6 {- if err := e.Named(inst.Project(), inst.Dir).Down(context.WithoutCancel(ctx), false); err != nil {+ if detached, err = detachVoice(ctx, e, inst); err != nil {+ return err+ }+ if err := downInstance(ctx, e, inst); err != nil {return err}}@@ -62,5 +66,14 @@ func Resolve(ctx context.Context, inst *instance.Instance, e *engine.Engine) err}}inst.State.Pending = nil- return save(inst)+ if err := save(inst); err != nil {+ return err+ }+ if inst.State.Stopped {+ return nil+ }+ if failed := attachVoice(ctx, e, detached); len(failed) > 0 {+ return fmt.Errorf("deploy: voice servers %s didn't start again", strings.Join(failed, ", "))+ }+ return nil}diff --git a/fluxer_rexctl/internal/deploy/set.go b/fluxer_rexctl/internal/deploy/set.goindex 848fc3afa..3c5933c46 100644--- a/fluxer_rexctl/internal/deploy/set.go+++ b/fluxer_rexctl/internal/deploy/set.go@@ -35,7 +35,7 @@ func Promised(set *stack.Set, custom []byte, k settings.Key) []string {switch k.Name {case settings.EngineHost:return nil- case settings.EdgeMode, settings.IPv6, settings.ImagesRegistry, settings.StorageEndpoint:+ case settings.EdgeMode, settings.IPv6, settings.ImagesRegistry, settings.StorageEndpoint, settings.VoiceBundled:return slices.Clone(stack.Services)case settings.MemoryProfile:return settings.Services(set, custom, append(stack.ProfileKeys(set), stack.SizingKeys()...))@@ -215,8 +215,16 @@ func Set(ctx context.Context, inst *instance.Instance, e *engine.Engine, changes}if !live {+ var stopped []stringif ipv6 {- if err := e.Named(inst.Project(), inst.Dir).Down(context.WithoutCancel(ctx), false); err != nil {+ detached, err := detachVoice(ctx, e, inst)+ if err != nil {+ return nil, err+ }+ for _, v := range detached {+ stopped = append(stopped, v.Server.ID)+ }+ if err := downInstance(ctx, e, inst); err != nil {return nil, err}}@@ -228,7 +236,7 @@ func Set(ctx context.Context, inst *instance.Instance, e *engine.Engine, changesif err := save(inst); err != nil {return nil, err}- return &Change{Keys: changed}, nil+ return &Change{Keys: changed, VoiceDown: stopped}, nil}if err := newP.Pull(ctx, o.OnEvent); err != nil {@@ -249,6 +257,10 @@ func Set(ctx context.Context, inst *instance.Instance, e *engine.Engine, changesif err == nil {err = save(inst)}+ var detached []AttachedVoice+ if err == nil && ipv6 {+ detached, err = detachVoice(ctx, e, inst)+ }if err == nil {err = bringUp(ctx, oldP, newP, ipv6, o.OnEvent)}@@ -257,14 +269,14 @@ func Set(ctx context.Context, inst *instance.Instance, e *engine.Engine, changeso.Failed(newP, err)}rerr := revert(ctx, inst, rs, oldP, ipv6, o.OnEvent)- return &Change{Keys: changed, Reverted: rerr == nil}, errors.Join(err, rerr)+ return &Change{Keys: changed, Reverted: rerr == nil, VoiceDown: attachVoice(ctx, e, detached)}, errors.Join(err, rerr)}st.Settings = nextst.Pending = nilif err := save(inst); err != nil {return nil, err}- return &Change{Keys: changed, Restarted: preview.Restarts, Removed: preview.Removes, Applied: true}, nil+ return &Change{Keys: changed, Restarted: preview.Restarts, Removed: preview.Removes, Applied: true, VoiceDown: attachVoice(ctx, e, detached)}, nil}func checkPorts(ctx context.Context, e *engine.Engine, project string, oldEnv, newEnv map[string]string, changed []string, mode string) error {diff --git a/fluxer_rexctl/internal/deploy/voice.go b/fluxer_rexctl/internal/deploy/voice.gonew file mode 100644index 000000000..a1ad762db--- /dev/null+++ b/fluxer_rexctl/internal/deploy/voice.go@@ -0,0 +1,186 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package deploy++import (+ "context"+ "encoding/json"+ "errors"+ "fmt"+ "slices"+ "strings"+ "time"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "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/voice"+)++func VoiceBundled(inst *instance.Instance) bool {+ return inst.State != nil && !inst.State.IsVoice() && stack.VoiceBundled(inst.State.Settings[settings.VoiceBundled])+}++func DefaultVoiceRegion(inst *instance.Instance) string {+ var region struct {+ ID string `json:"id"`+ }+ raw := strings.TrimSpace(inst.Setting("FLUXER_LIVEKIT_DEFAULT_REGION"))+ if raw != "" && json.Unmarshal([]byte(raw), ®ion) == nil && strings.TrimSpace(region.ID) != "" {+ return strings.TrimSpace(region.ID)+ }+ return "default"+}++func BundledVoiceServerID(inst *instance.Instance) string {+ if !VoiceBundled(inst) {+ return ""+ }+ return DefaultVoiceRegion(inst) + "-server-1"+}++const (+ VoiceStartTimeout = 3 * time.Minute+ VoiceStopTimeout = 30 * time.Second+)++func VoiceEnv(inst *instance.Instance, srv *instance.VoiceServer) voice.Env {+ env := voice.Env{+ ID: srv.ID,+ Key: srv.Key,+ Secret: srv.Secret,+ WebhookURL: srv.Webhook,+ Network: srv.Network,+ TCPPort: srv.TCPPort,+ UDPPort: srv.UDPPort,+ HTTPPort: srv.HTTPPort,+ NodeIP: srv.NodeIP,+ BindIP: srv.BindIP,+ Image: srv.Image,+ Memory: srv.Memory,+ }+ if !inst.State.IsVoice() {+ env.StunPrimary = inst.Setting("FLUXER_LIVEKIT_STUN_PRIMARY")+ env.StunSecondary = inst.Setting("FLUXER_LIVEKIT_STUN_SECONDARY")+ env.LogLevel = inst.Setting("FLUXER_LIVEKIT_LOG_LEVEL")+ }+ return env+}++func VoiceSpec(inst *instance.Instance, srv *instance.VoiceServer) engine.Spec {+ files := []string{voice.ComposeFile}+ if srv.HTTPPort > 0 {+ files = append(files, voice.HTTPComposeFile)+ }+ return engine.Spec{+ Name: srv.Project,+ Dir: voice.ServerDir(inst.Dir, srv.ID),+ Files: files,+ Env: VoiceEnv(inst, srv).Values(),+ }+}++func StaleVoiceEndpoints(inst *instance.Instance) []string {+ base := voice.WSOrigin(inst.Origin())+ var out []string+ for _, srv := range inst.State.Voice {+ if base != "" && srv.Edge == voice.EdgeInstance && srv.Endpoint != base+voice.Path(srv.ID) {+ out = append(out, srv.ID)+ }+ }+ return out+}++func MoveVoiceEndpoints(ctx context.Context, api *adminapi.Client, inst *instance.Instance) ([]string, error) {+ stale := StaleVoiceEndpoints(inst)+ base := voice.WSOrigin(inst.Origin())+ var moved []string+ var errs []error+ for i := range inst.State.Voice {+ srv := &inst.State.Voice[i]+ if !slices.Contains(stale, srv.ID) {+ continue+ }+ endpoint := base + voice.Path(srv.ID)+ if _, err := api.UpdateVoiceServer(ctx, srv.Region, srv.ID, adminapi.UpdateVoiceServerRequestBody{Endpoint: &endpoint}); err != nil {+ errs = append(errs, fmt.Errorf("%s: %w", srv.ID, err))+ continue+ }+ srv.Endpoint = endpoint+ moved = append(moved, srv.ID)+ }+ if len(moved) > 0 {+ if err := save(inst); err != nil {+ errs = append(errs, err)+ }+ }+ return moved, errors.Join(errs...)+}++type AttachedVoice struct {+ Owner *instance.Instance+ Server instance.VoiceServer+}++var (+ downInstance = func(ctx context.Context, e *engine.Engine, inst *instance.Instance) error {+ return e.Named(inst.Project(), inst.Dir).Down(context.WithoutCancel(ctx), false)+ }+ stopVoice = func(ctx context.Context, e *engine.Engine, v AttachedVoice) error {+ return e.Named(v.Server.Project, "").DownWithin(ctx, false, VoiceStopTimeout)+ }+ startVoice = func(ctx context.Context, e *engine.Engine, v AttachedVoice) error {+ p, err := e.Load(ctx, VoiceSpec(v.Owner, &v.Server))+ if err != nil {+ return err+ }+ return p.Up(ctx, engine.UpOptions{RemoveOrphans: true, Wait: true, WaitTimeout: VoiceStartTimeout})+ }+)++func VoiceOnNetwork(inst *instance.Instance) []AttachedVoice {+ network := inst.Project() + "_fluxer"+ var out []AttachedVoice+ add := func(owner *instance.Instance) {+ for _, srv := range owner.State.Voice {+ if srv.Network == network {+ out = append(out, AttachedVoice{Owner: owner, Server: srv})+ }+ }+ }+ add(inst)+ for _, dir := range instance.ReadRecord(instance.OwnScope()).Instances {+ if dir == inst.Dir {+ continue+ }+ if other, err := instance.Find(dir); err == nil && other.State.IsVoice() {+ add(other)+ }+ }+ return out+}++func detachVoice(ctx context.Context, e *engine.Engine, inst *instance.Instance) ([]AttachedVoice, error) {+ servers := VoiceOnNetwork(inst)+ bg := context.WithoutCancel(ctx)+ for i, v := range servers {+ if err := stopVoice(bg, e, v); err != nil {+ attachVoice(ctx, e, servers[:i])+ return nil, fmt.Errorf("deploy: stopping voice server %s: %w", v.Server.ID, err)+ }+ }+ return servers, nil+}++func attachVoice(ctx context.Context, e *engine.Engine, servers []AttachedVoice) []string {+ bg := context.WithoutCancel(ctx)+ var failed []string+ for _, v := range servers {+ if err := startVoice(bg, e, v); err != nil {+ failed = append(failed, v.Server.ID)+ }+ }+ return failed+}diff --git a/fluxer_rexctl/internal/deploy/voice_test.go b/fluxer_rexctl/internal/deploy/voice_test.gonew file mode 100644index 000000000..fa4bb18d5--- /dev/null+++ b/fluxer_rexctl/internal/deploy/voice_test.go@@ -0,0 +1,222 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package deploy++import (+ "context"+ "encoding/json"+ "errors"+ "net/http"+ "os"+ "path/filepath"+ "slices"+ "strings"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/adminapi"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/voice"+)++type voiceEvents struct {+ got []string+ fail map[string]bool+}++func recordVoice(t *testing.T) *voiceEvents {+ t.Helper()+ ev := &voiceEvents{fail: map[string]bool{}}+ oldDown, oldStop, oldStart := downInstance, stopVoice, startVoice+ t.Cleanup(func() { downInstance, stopVoice, startVoice = oldDown, oldStop, oldStart })+ downInstance = func(_ context.Context, _ *engine.Engine, inst *instance.Instance) error {+ ev.got = append(ev.got, "down "+inst.Project())+ return nil+ }+ stopVoice = func(_ context.Context, _ *engine.Engine, v AttachedVoice) error {+ ev.got = append(ev.got, "stop "+v.Server.ID)+ if ev.fail["stop "+v.Server.ID] {+ return errors.New("stuck")+ }+ return nil+ }+ startVoice = func(_ context.Context, _ *engine.Engine, v AttachedVoice) error {+ ev.got = append(ev.got, "start "+v.Server.ID)+ if ev.fail["start "+v.Server.ID] {+ return errors.New("no network")+ }+ return nil+ }+ return ev+}++func voiceHome(t *testing.T) string {+ t.Helper()+ home := t.TempDir()+ t.Setenv("HOME", home)+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))+ return home+}++func fluxerWithVoice(t *testing.T, home string, servers ...instance.VoiceServer) *instance.Instance {+ t.Helper()+ inst, err := instance.Create(filepath.Join(home, "fluxer"), "fluxer")+ if err != nil {+ t.Fatal(err)+ }+ inst.State.Voice = servers+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ if err := instance.Remember(instance.OwnScope(), inst.Dir, true); err != nil {+ t.Fatal(err)+ }+ return inst+}++func joinedMachine(t *testing.T, home string, servers ...instance.VoiceServer) {+ t.Helper()+ inst := &instance.Instance{Dir: filepath.Join(home, "voice"), State: &instance.State{+ Schema: instance.StateSchema, Kind: instance.KindVoice, Project: voice.MachineProject,+ Settings: map[string]string{}, Secrets: map[string]string{}, Voice: servers,+ }}+ if err := os.MkdirAll(inst.Path(instance.StateDirName), 0o700); err != nil {+ t.Fatal(err)+ }+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ if err := instance.Remember(instance.OwnScope(), inst.Dir, false); err != nil {+ t.Fatal(err)+ }+}++func TestIPv6ChangeTakesVoiceServersOffTheNetworkFirst(t *testing.T) {+ home := voiceHome(t)+ inst := fluxerWithVoice(t, home, instance.VoiceServer{ID: "fra-server-1", Project: "fluxer-voice-fra-server-1", Network: "fluxer_fluxer"})+ joinedMachine(t, home,+ instance.VoiceServer{ID: "fra-server-2", Project: "fluxer-voice-fra-server-2", Network: "fluxer_fluxer"},+ instance.VoiceServer{ID: "sgp-server-1", Project: "fluxer-voice-sgp-server-1", Network: voice.MachineNetwork},+ )+ inst.State.Stopped = true+ inst.State.Pending = &instance.Pending{Settings: map[string]string{settings.IPv6: "true"}, Keys: []string{settings.IPv6}}+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ ev := recordVoice(t)+ if err := Resolve(context.Background(), inst, nil); err != nil {+ t.Fatal(err)+ }+ want := []string{"stop fra-server-1", "stop fra-server-2", "down fluxer"}+ if !slices.Equal(ev.got, want) {+ t.Fatalf("events = %v, want %v", ev.got, want)+ }+ if inst.State.Pending != nil {+ t.Fatal("the pending change is still there")+ }+}++func TestVoiceServersStartAgainAfterTheNetworkChange(t *testing.T) {+ home := voiceHome(t)+ inst := fluxerWithVoice(t, home,+ instance.VoiceServer{ID: "fra-server-1", Project: "fluxer-voice-fra-server-1", Network: "fluxer_fluxer"},+ instance.VoiceServer{ID: "fra-server-2", Project: "fluxer-voice-fra-server-2", Network: "fluxer_fluxer"},+ )+ ev := recordVoice(t)+ ev.fail["start fra-server-2"] = true+ detached, err := detachVoice(context.Background(), nil, inst)+ if err != nil {+ t.Fatal(err)+ }+ failed := attachVoice(context.Background(), nil, detached)+ want := []string{"stop fra-server-1", "stop fra-server-2", "start fra-server-1", "start fra-server-2"}+ if !slices.Equal(ev.got, want) {+ t.Fatalf("events = %v, want %v", ev.got, want)+ }+ if !slices.Equal(failed, []string{"fra-server-2"}) {+ t.Fatalf("failed = %v", failed)+ }+}++func TestAServerThatWontStopBringsTheOthersBack(t *testing.T) {+ home := voiceHome(t)+ inst := fluxerWithVoice(t, home,+ instance.VoiceServer{ID: "fra-server-1", Project: "fluxer-voice-fra-server-1", Network: "fluxer_fluxer"},+ instance.VoiceServer{ID: "fra-server-2", Project: "fluxer-voice-fra-server-2", Network: "fluxer_fluxer"},+ )+ ev := recordVoice(t)+ ev.fail["stop fra-server-2"] = true+ if _, err := detachVoice(context.Background(), nil, inst); err == nil {+ t.Fatal("detach succeeded with a stuck server")+ }+ want := []string{"stop fra-server-1", "stop fra-server-2", "start fra-server-1"}+ if !slices.Equal(ev.got, want) {+ t.Fatalf("events = %v, want %v", ev.got, want)+ }+}++type endpointAdmin struct {+ sent map[string]string+ fail string+}++func (f *endpointAdmin) Do(_ context.Context, req adminapi.Request) (*adminapi.Response, error) {+ if req.Method != http.MethodPatch {+ return &adminapi.Response{Status: 404, Body: json.RawMessage(`{}`)}, nil+ }+ if strings.HasSuffix(req.Path, "/"+f.fail) {+ return &adminapi.Response{Status: 500, Body: json.RawMessage(`{"code":"GENERAL_ERROR","message":"boom"}`)}, nil+ }+ var body struct {+ Endpoint string `json:"endpoint"`+ }+ if err := json.Unmarshal(req.Body, &body); err != nil {+ return nil, err+ }+ f.sent[req.Path] = body.Endpoint+ return &adminapi.Response{Status: 200, Body: json.RawMessage(`{"server":{}}`)}, nil+}++func TestANewDomainMovesVoiceAddEndpoints(t *testing.T) {+ dir := t.TempDir()+ inst := &instance.Instance{Dir: dir, State: &instance.State{+ Schema: instance.StateSchema, Project: "fluxer", Secrets: map[string]string{},+ Settings: map[string]string{"FLUXER_PUBLIC_ORIGIN": "https://new.example.com"},+ Voice: []instance.VoiceServer{+ {ID: "default-server-2", Region: "default", Endpoint: "wss://old.example.com/livekit-default-server-2", Edge: voice.EdgeInstance},+ {ID: "default-server-3", Region: "default", Endpoint: "wss://old.example.com/livekit-default-server-3", Edge: voice.EdgeInstance},+ {ID: "fra-server-1", Region: "fra", Endpoint: "wss://voice.example.net/livekit-fra-server-1", Edge: voice.EdgeOwn},+ },+ }}+ if err := os.MkdirAll(inst.Path(instance.StateDirName), 0o700); err != nil {+ t.Fatal(err)+ }+ if got := StaleVoiceEndpoints(inst); !slices.Equal(got, []string{"default-server-2", "default-server-3"}) {+ t.Fatalf("stale = %v", got)+ }+ fake := &endpointAdmin{sent: map[string]string{}, fail: "default-server-3"}+ moved, err := MoveVoiceEndpoints(context.Background(), adminapi.New(fake, ""), inst)+ if err == nil || !strings.Contains(err.Error(), "default-server-3") {+ t.Fatalf("err = %v", err)+ }+ if !slices.Equal(moved, []string{"default-server-2"}) {+ t.Fatalf("moved = %v", moved)+ }+ if got := fake.sent["/admin/voice/regions/default/servers/default-server-2"]; got != "wss://new.example.com/livekit-default-server-2" {+ t.Fatalf("sent %v", fake.sent)+ }+ saved, err := instance.Find(dir)+ if err != nil {+ t.Fatal(err)+ }+ if got := saved.State.VoiceServer("default-server-2").Endpoint; got != "wss://new.example.com/livekit-default-server-2" {+ t.Fatalf("saved endpoint = %s", got)+ }+ if got := saved.State.VoiceServer("default-server-3").Endpoint; got != "wss://old.example.com/livekit-default-server-3" {+ t.Fatalf("failed server changed to %s", got)+ }+ if got := StaleVoiceEndpoints(inst); !slices.Equal(got, []string{"default-server-3"}) {+ t.Fatalf("stale after the move = %v", got)+ }+}diff --git a/fluxer_rexctl/internal/deploy/voiceports_test.go b/fluxer_rexctl/internal/deploy/voiceports_test.gonew file mode 100644index 000000000..8296f443d--- /dev/null+++ b/fluxer_rexctl/internal/deploy/voiceports_test.go@@ -0,0 +1,41 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package deploy++import (+ "context"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/instance"+)++func TestCheckPortsFreeSeesStoppedVoiceServers(t *testing.T) {+ home := t.TempDir()+ t.Setenv("HOME", home)+ t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))+ inst := &instance.Instance{Dir: filepath.Join(home, "voice"), State: &instance.State{+ Schema: instance.StateSchema, Kind: instance.KindVoice, Project: "fluxer-voice",+ Settings: map[string]string{}, Secrets: map[string]string{},+ Voice: []instance.VoiceServer{{ID: "fra-server-1", TCPPort: 47883, UDPPort: 47884}},+ }}+ if err := os.MkdirAll(inst.Path(instance.StateDirName), 0o700); err != nil {+ t.Fatal(err)+ }+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ if err := instance.Remember(instance.OwnScope(), inst.Dir, false); err != nil {+ t.Fatal(err)+ }+ err := checkPortsFree(context.Background(), nil, "fluxer", []host.Port{{Number: 47883, Proto: "tcp"}})+ if err == nil || !strings.Contains(err.Error(), "fra-server-1") {+ t.Fatalf("checkPortsFree = %v", err)+ }+ if err := checkPortsFree(context.Background(), nil, "fluxer", []host.Port{{Number: 47885, Proto: "tcp"}}); err != nil {+ t.Fatalf("a free port: %v", err)+ }+}diff --git a/fluxer_rexctl/internal/engine/apply.go b/fluxer_rexctl/internal/engine/apply.goindex b619d1a78..183a8a23b 100644--- a/fluxer_rexctl/internal/engine/apply.go+++ b/fluxer_rexctl/internal/engine/apply.go@@ -256,6 +256,14 @@ func (p *Project) present(ctx context.Context, op string, services []string) err}func (p *Project) Stop(ctx context.Context, services ...string) error {+ return p.stop(ctx, nil, services)+}++func (p *Project) StopWithin(ctx context.Context, timeout time.Duration) error {+ return p.stop(ctx, &timeout, nil)+}++func (p *Project) stop(ctx context.Context, timeout *time.Duration, services []string) error {c, err := p.Engine.conn()if err != nil {return err@@ -268,7 +276,7 @@ func (p *Project) Stop(ctx context.Context, services ...string) error {if marker := p.stopMarker(); marker != "" && os.WriteFile(marker, []byte(strings.Join(services, "\n")), 0o600) == nil {defer os.Remove(marker)}- if err := c.svc.Stop(ctx, p.Name, api.StopOptions{Project: p.modelOrNil(), Services: services}); err != nil {+ if err := c.svc.Stop(ctx, p.Name, api.StopOptions{Project: p.modelOrNil(), Services: services, Timeout: timeout}); err != nil {return p.wrap("stop", err)}return p.stopPodmanCompose(ctx, services)@@ -346,6 +354,14 @@ func (p *Project) Remove(ctx context.Context, services ...string) error {}func (p *Project) Down(ctx context.Context, volumes bool) error {+ return p.down(ctx, volumes, nil)+}++func (p *Project) DownWithin(ctx context.Context, volumes bool, timeout time.Duration) error {+ return p.down(ctx, volumes, &timeout)+}++func (p *Project) down(ctx context.Context, volumes bool, timeout *time.Duration) error {c, err := p.Engine.conn()if err != nil {return err@@ -357,7 +373,7 @@ func (p *Project) Down(ctx context.Context, volumes bool) error {if err := p.removeHelpers(ctx, a); err != nil {return err}- return p.wrap("down", c.svc.Down(ctx, p.Name, api.DownOptions{Project: p.modelOrNil(), RemoveOrphans: true, Volumes: volumes}))+ return p.wrap("down", c.svc.Down(ctx, p.Name, api.DownOptions{Project: p.modelOrNil(), RemoveOrphans: true, Volumes: volumes, Timeout: timeout}))}func (p *Project) DownKeeping(ctx context.Context, keep ...string) error {@@ -434,3 +450,23 @@ func (p *Project) wrap(op string, err error) error {}return &Error{Op: op, Class: ClassOf(err), Err: err}}++func (e *Engine) EnsureNetwork(ctx context.Context, name, project, key string) error {+ c, err := e.api()+ if err != nil {+ return err+ }+ if _, err := c.NetworkInspect(ctx, name, client.NetworkInspectOptions{}); err == nil {+ return nil+ } else if !cerrdefs.IsNotFound(err) {+ return err+ }+ _, err = c.NetworkCreate(ctx, name, client.NetworkCreateOptions{+ Driver: "bridge",+ Labels: map[string]string{api.ProjectLabel: project, api.NetworkLabel: key},+ })+ if cerrdefs.IsConflict(err) {+ return nil+ }+ return err+}diff --git a/fluxer_rexctl/internal/host/sys.go b/fluxer_rexctl/internal/host/sys.goindex caaad6ee7..5128ba73e 100644--- a/fluxer_rexctl/internal/host/sys.go+++ b/fluxer_rexctl/internal/host/sys.go@@ -45,6 +45,21 @@ func PublicIP(ctx context.Context) (string, error) {return "", fmt.Errorf("%w: %v", ErrNoPublicIP, last)}+func PublicIPv4(ctx context.Context) (string, error) {+ var last error = ErrNoPublicIP+ for _, server := range stunServers {+ ip, err := stunQuery(ctx, "udp4", server)+ if err == nil {+ return ip.String(), nil+ }+ last = err+ if ctx.Err() != nil {+ return "", ctx.Err()+ }+ }+ return "", fmt.Errorf("%w: %v", ErrNoPublicIP, last)+}+func stunQuery(ctx context.Context, network, server string) (net.IP, error) {ctx, cancel := context.WithTimeout(ctx, 3*time.Second)defer cancel()diff --git a/fluxer_rexctl/internal/instance/bundledvoice_test.go b/fluxer_rexctl/internal/instance/bundledvoice_test.gonew file mode 100644index 000000000..4044fc047--- /dev/null+++ b/fluxer_rexctl/internal/instance/bundledvoice_test.go@@ -0,0 +1,69 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package instance++import (+ "context"+ "slices"+ "testing"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/engine"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/secrets"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+)++func loadWithVoice(t *testing.T, bundled string) *engine.Project {+ t.Helper()+ inst, err := Create(t.TempDir(), "fluxer")+ if err != nil {+ t.Fatal(err)+ }+ set := stack.Embedded()+ inst.State.Release = set.Release+ inst.State.Images = Images{Release: set.Release, Refs: fluxerRefs("a")}+ inst.State.Settings[settings.Domain] = "chat.example.com"+ if bundled != "" {+ inst.State.Settings[settings.VoiceBundled] = bundled+ }+ if _, err := secrets.Mint(inst.State.Secrets, false); err != nil {+ t.Fatal(err)+ }+ if _, err := inst.Render(set, stack.OverlaysFor(inst.State.Settings), inst.State.Images.Refs); err != nil {+ t.Fatal(err)+ }+ env, err := inst.Environment()+ if err != nil {+ t.Fatal(err)+ }+ var none engine.Engine+ p, err := none.Load(context.Background(), engine.Spec{Name: "fluxer", Dir: inst.Dir, Files: inst.ComposeFiles(), Env: env})+ if err != nil {+ t.Fatalf("voice.bundled %q: %v", bundled, err)+ }+ return p+}++func TestWithoutBundledVoiceLiveKitDoesNotRun(t *testing.T) {+ for _, tc := range []struct {+ bundled string+ runs bool+ env string+ }{+ {"", true, "true"},+ {"true", true, "true"},+ {"false", false, "false"},+ } {+ p := loadWithVoice(t, tc.bundled)+ if got := slices.Contains(p.Services(), "livekit"); got != tc.runs {+ t.Errorf("voice.bundled %q: livekit runs = %v, want %v", tc.bundled, got, tc.runs)+ }+ if !slices.Contains(p.Services(), "api") || !slices.Contains(p.Services(), "edge") {+ t.Errorf("voice.bundled %q: services %v", tc.bundled, p.Services())+ }+ got := p.Model.Services["api"].Environment["FLUXER_LIVEKIT_BUNDLED"]+ if got == nil || *got != tc.env {+ t.Errorf("voice.bundled %q: api FLUXER_LIVEKIT_BUNDLED = %v, want %s", tc.bundled, got, tc.env)+ }+ }+}diff --git a/fluxer_rexctl/internal/instance/bundleenv.go b/fluxer_rexctl/internal/instance/bundleenv.goindex 66b996f36..6d3dc49b7 100644--- a/fluxer_rexctl/internal/instance/bundleenv.go+++ b/fluxer_rexctl/internal/instance/bundleenv.go@@ -4,6 +4,7 @@ package instanceimport ("fmt"+ "regexp""slices""strings""sync"@@ -27,6 +28,7 @@ var envOverlays = []struct {{stack.OverlayTunnel, "for a tunnel"},{stack.OverlayExternalObjectStore, "for your own object store"},{stack.OverlayIPv6, "for IPv6 on Fluxer's network"},+ {stack.OverlayExternalVoice, "for voice servers of your own without the bundled LiveKit"},}var envExamples = map[string]string{@@ -312,3 +314,87 @@ func literalDefault(defs []string) string {}return defs[0]}++var voiceSharedVars = []string{"FLUXER_LIVEKIT_IMAGE", "FLUXER_LIVEKIT_LOG_LEVEL", "FLUXER_LIVEKIT_STUN_PRIMARY", "FLUXER_LIVEKIT_STUN_SECONDARY"}++var voiceRequiredVars = []string{"FLUXER_VOICE_ID", "FLUXER_VOICE_API_KEY", "FLUXER_VOICE_API_SECRET", "FLUXER_VOICE_WEBHOOK_URL"}++var composeDefaultPattern = regexp.MustCompile(`\$\{([A-Z][A-Z0-9_]*):-([^}$]*)\}`)++func voiceDefaults() map[string]string {+ out := map[string]string{}+ set := stack.Embedded()+ for _, name := range []string{"voice/voice.compose.yml", "voice/voice-http.compose.yml", "voice/voice-edge.compose.yml"} {+ data, err := stack.ReadFile(set, name)+ if err != nil {+ continue+ }+ for _, m := range composeDefaultPattern.FindAllStringSubmatch(string(data), -1) {+ if _, ok := out[m[1]]; !ok {+ out[m[1]] = m[2]+ }+ }+ }+ return out+}++func VoiceBundleEnv() string {+ return voiceEnvFile("Settings for one standalone voice server",+ "Copy this file to <id>.env, fill in the required values and start the server with `docker compose -p fluxer-voice-<id> --env-file <id>.env -f voice.compose.yml up -d`. "++ "Each server needs its own settings file, project name, key and ports. Settings that start with # are optional.",+ voiceRequiredVars, envVoiceServerSection, voiceSharedVars)+}++func VoiceEdgeBundleEnv() string {+ return voiceEnvFile("Settings for the voice edge",+ "Copy this file to edge.env and start the edge with `docker compose -p fluxer-voice-edge --env-file edge.env -f voice-edge.compose.yml up -d`. "++ "Every setting is optional.",+ nil, envVoiceEdgeSection, nil)+}++func voiceEnvFile(title, intro string, required []string, section string, shared []string) string {+ docs := envDocs()+ defaults := voiceDefaults()+ var b strings.Builder+ b.WriteString(envRule + "\n")+ b.WriteString("# " + title + "\n#\n")+ envComment(&b, "# ", intro)+ b.WriteString(envRule + "\n")+ entry := func(name string, active bool) {+ d := docs.get(name)+ if d.name == "" {+ return+ }+ d.setting, d.active, d.defaults = "", active, nil+ if v, ok := defaults[name]; ok && v != "" {+ d.defaults = []envDefault{{value: v}}+ }+ envEntry(&b, d)+ }+ if len(required) > 0 {+ envFrame(&b, "REQUIRED", "", nil)+ for i, name := range required {+ if i > 0 {+ b.WriteString("\n")+ }+ entry(name, true)+ }+ }+ envFrame(&b, "OPTIONAL", "", nil)+ first := true+ for _, d := range docs.inAll(section) {+ if slices.Contains(required, d.name) {+ continue+ }+ if !first {+ b.WriteString("\n")+ }+ first = false+ entry(d.name, false)+ }+ for _, name := range shared {+ b.WriteString("\n")+ entry(name, false)+ }+ return b.String()+}diff --git a/fluxer_rexctl/internal/instance/envdocs_test.go b/fluxer_rexctl/internal/instance/envdocs_test.goindex 8e02f6ed1..1fecedf0a 100644--- a/fluxer_rexctl/internal/instance/envdocs_test.go+++ b/fluxer_rexctl/internal/instance/envdocs_test.go@@ -90,6 +90,9 @@ func TestDocsEnvNames(t *testing.T) {}for _, m := range docsDottedName.FindAllStringSubmatch(data, -1) {first, _, _ := strings.Cut(m[1], ".")+ if slices.ContainsFunc([]string{".yml", ".yaml", ".env", ".example"}, func(ext string) bool { return strings.HasSuffix(m[1], ext) }) {+ continue+ }if prefixes[first] && strings.HasPrefix(rel, docsContent+"/operator/") && !slices.Contains(helmPages, rel) {keys[m[1]] = true}diff --git a/fluxer_rexctl/internal/instance/envinventory_test.go b/fluxer_rexctl/internal/instance/envinventory_test.goindex 100a672dc..6beb56d00 100644--- a/fluxer_rexctl/internal/instance/envinventory_test.go+++ b/fluxer_rexctl/internal/instance/envinventory_test.go@@ -1147,6 +1147,14 @@ func rangeValue(v string) string {func TestEnvInventory(t *testing.T) {var err errorset := stack.Embedded()+ voiceFiles, err := fs.Sub(set.Files, "voice")+ if err != nil {+ t.Fatal(err)+ }+ voiceVars, err := stack.FilesVars(voiceFiles)+ if err != nil {+ t.Fatal(err)+ }reads, all := codeEnvNames(t)code := runtimeReads(reads)fluxer, other, feeds := composeEnv(t, set)@@ -1170,6 +1178,10 @@ func TestEnvInventory(t *testing.T) {if slices.Contains(set.Vars, v.name) || !passed(v.name) {t.Errorf("%s is listed as set by the compose stack, but compose doesn't set it for any service, or it's a .env setting", v.name)}+ case v.section == envVoiceServerSection || v.section == envVoiceEdgeSection:+ if !slices.Contains(voiceVars, v.name) {+ t.Errorf("%s is listed as a voice server setting, but no voice compose file uses it", v.name)+ }case v.section == envRexctlSection:if passed(v.name) {t.Errorf("%s is listed in the rexctl section, but compose passes it", v.name)@@ -1192,6 +1204,11 @@ func TestEnvInventory(t *testing.T) {t.Errorf("%s is used by a compose file but has no entry in envVars", v)}}+ for _, v := range voiceVars {+ if _, ok := registry[v]; !ok {+ t.Errorf("%s is used by a voice compose file but has no entry in envVars", v)+ }+ }for _, k := range slices.Sorted(maps.Keys(fluxer)) {if _, ok := registry[k]; !ok {t.Errorf("compose sets %s for %v but it has no entry in envVars", k, fluxer[k])diff --git a/fluxer_rexctl/internal/instance/envref.go b/fluxer_rexctl/internal/instance/envref.goindex 78f3bf39e..2aa4b5a0d 100644--- a/fluxer_rexctl/internal/instance/envref.go+++ b/fluxer_rexctl/internal/instance/envref.go@@ -18,15 +18,17 @@ import ()const (- envRequiredSection = "required"- envAdminSection = "admin"- envSecretsSection = "secrets"- envComposeSection = "compose"- envFilesSection = "files"- envRexctlSection = "rexctl"- envStackSection = "stack"- envInternalSection = "internal"- envNetworkSection = "network"+ envRequiredSection = "required"+ envAdminSection = "admin"+ envSecretsSection = "secrets"+ envComposeSection = "compose"+ envFilesSection = "files"+ envRexctlSection = "rexctl"+ envStackSection = "stack"+ envInternalSection = "internal"+ envNetworkSection = "network"+ envVoiceServerSection = "voice-server"+ envVoiceEdgeSection = "voice-edge")type envSection struct {@@ -46,7 +48,10 @@ var envSections = []envSection{stack.ComposeFile, stack.ComposeFile, stack.OverlayFiles[stack.OverlayProxy]), ""},{"address", "Public address", "Fluxer works out every address from FLUXER_DOMAIN. Set these only when a part of Fluxer runs on another address.", ""},{"edge", "Edge and HTTPS", "By default Fluxer listens on ports 80 and 443 and gets its own HTTPS certificate for your domain.", ""},- {"voice", "Voice and video", "Calls connect directly to this machine on FLUXER_LIVEKIT_TCP_PORT and FLUXER_LIVEKIT_UDP_PORT, even when Fluxer is behind your own proxy.", ""},+ {"voice", "Voice and video", "Calls connect directly to this machine on FLUXER_LIVEKIT_TCP_PORT and FLUXER_LIVEKIT_UDP_PORT, even when Fluxer is behind your own proxy. " ++ "To run without the bundled LiveKit, add " + stack.OverlayFiles[stack.OverlayExternalVoice] + " to COMPOSE_FILE and set FLUXER_LIVEKIT_BUNDLED to `false`.", ""},+ {envVoiceServerSection, "Standalone voice servers", "These go in the settings file of a voice server from the voice folder of the compose bundle, such as `fra-server-1.env`, not in Fluxer's .env. `rexctl voice add` and `rexctl voice join` write them for you.", ""},+ {envVoiceEdgeSection, "Voice edge", "These go in `edge.env` next to voice-edge.compose.yml in the voice folder of the compose bundle. `rexctl voice join` writes them for you.", ""},{"storage", "Object storage", "By default Fluxer keeps files in the bundled object store. To use your own S3-compatible store, set its address and buckets, add " +stack.OverlayFiles[stack.OverlayExternalObjectStore] + " to COMPOSE_FILE and put its keys in FLUXER_S3_ACCESS_KEY and FLUXER_S3_SECRET_KEY.", ""},{"database", "Database", "Leave these unset to use the bundled Postgres.", ""},@@ -143,6 +148,16 @@ func (l envDocList) in(section string) []envDoc {return out}+func (l envDocList) inAll(section string) []envDoc {+ var out []envDoc+ for _, d := range l {+ if d.section == section {+ out = append(out, d)+ }+ }+ return out+}+func (l envDocList) get(name string) envDoc {for _, d := range l {if d.name == name {@@ -226,7 +241,7 @@ func envDocs() envDocList {return out}-var envUnbundledSections = []string{envRexctlSection, envStackSection, envInternalSection}+var envUnbundledSections = []string{envRexctlSection, envStackSection, envInternalSection, envVoiceServerSection, envVoiceEdgeSection}var envRotatableVars = []string{"FLUXER_S3_SECRET_KEY", "LIVEKIT_API_SECRET"}diff --git a/fluxer_rexctl/internal/instance/envvars.go b/fluxer_rexctl/internal/instance/envvars.goindex ffcc40bdd..2a708ead5 100644--- a/fluxer_rexctl/internal/instance/envvars.go+++ b/fluxer_rexctl/internal/instance/envvars.go@@ -90,10 +90,13 @@ var envVars = []envVar{{"FLUXER_EDGE_GLOBAL_OPTIONS", "edge", "Extra Caddy global options on one line, added to the global block of the edge Caddyfile, such as `email [email protected]` for the certificate account. An empty value adds nothing."},{"FLUXER_EDGE_TLS", "edge", "The arguments of a Caddy tls directive for the edge, such as `internal` for a certificate from Caddy's own local CA, or a certificate and key path under /certs. When it's empty, the edge adds no tls line and Caddy gets certificates on its own. docker-compose.proxy.yml and tunnel.compose.yml ignore it."},{"FLUXER_LIVEKIT_ENABLED", "voice", "Whether voice and video calls are turned on. Use `true` or `false`."},- {"FLUXER_LIVEKIT_INTERNAL_URL", "voice", "The URL the API uses for the LiveKit control API of the bundled LiveKit, the first server in the default voice region. Servers added in the admin panel use their own address."},+ {"FLUXER_LIVEKIT_INTERNAL_URL", "voice", "The URL the API uses for the LiveKit control API of the bundled LiveKit, the first server in the default voice region. Servers added in the admin panel use their own address. Fluxer ignores it when FLUXER_LIVEKIT_BUNDLED is `false`."},+ {"FLUXER_LIVEKIT_BUNDLED", "voice", "Use `true` or `false`. The bundled LiveKit is the first server in the default voice region. With `false`, Fluxer creates the default region with no server in it, removes the server it made for the bundled LiveKit and keeps calls on the servers you add with `rexctl voice add`, `rexctl voice join` or the admin panel. With Docker Compose, also add external-voice.compose.yml to COMPOSE_FILE."},{"FLUXER_LIVEKIT_DEFAULT_REGION", "voice", "The voice region Fluxer creates when no voice regions exist yet, as a JSON object with `id`, `name`, `emoji`, `latitude` and `longitude`."},{"FLUXER_LIVEKIT_URL", "voice", "The URL browsers and apps use for voice signalling. When it's empty, Fluxer uses the public origin, or FLUXER_PUBLIC_SCHEME, FLUXER_DOMAIN and FLUXER_PUBLIC_PORT, followed by /livekit. Set it only when LiveKit runs on another host."},{"FLUXER_VOICE_P2P_STUN_URLS", "voice", "STUN URLs for peer-to-peer calls, separated by commas, such as `stun:stun.example.com:3478`. When it's empty, peer-to-peer calls work only between devices with a direct route to each other, such as on one local network."},+ {"FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS", "voice", "Whether the API accepts a voice server joining with an endpoint that resolves to a private address. Use `true` or `false`. Set it to `true` only for tests and private networks."},+ {"FLUXER_VOICE_HEALTH_INTERVAL_MS", "voice", "How often each API node checks every active voice server, in milliseconds. A server that fails two checks in a row gets no new calls until a check passes. Set it to `0` to turn the checks off. When it's empty, the API checks every 15 seconds."},{"FLUXER_LIVEKIT_TCP_PORT", "voice", ""},{"FLUXER_LIVEKIT_UDP_PORT", "voice", ""},{"FLUXER_LIVEKIT_NODE_IP", "voice", "Set FLUXER_LIVEKIT_USE_EXTERNAL_IP to `false` with it."},@@ -101,6 +104,22 @@ var envVars = []envVar{{"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_VOICE_ID", "voice-server", "The ID of the voice server, such as `fra-server-2`. Use lowercase letters, digits and dashes. The container answers as `voice-` followed by this ID on the voice network, and the edge routes `/livekit-` followed by this ID to it."},+ {"FLUXER_VOICE_API_KEY", "voice-server", "The LiveKit API key of this server. Every voice server of an instance needs its own key, such as `fluxer-` followed by the server ID."},+ {"FLUXER_VOICE_API_SECRET", "voice-server", "The LiveKit API secret that goes with FLUXER_VOICE_API_KEY, at least 32 characters, such as the output of `openssl rand -hex 32`."},+ {"FLUXER_VOICE_WEBHOOK_URL", "voice-server", "The URL LiveKit sends its webhooks to. On the Fluxer machine it's `http://api:8080/webhooks/livekit`. On another machine it's the public API address followed by /webhooks/livekit."},+ {"FLUXER_VOICE_TCP_PORT", "voice-server", "The TCP port for media on this host. LiveKit listens on the same port inside the container, and each server on a host needs its own."},+ {"FLUXER_VOICE_UDP_PORT", "voice-server", "The UDP port for media on this host. LiveKit listens on the same port inside the container, and each server on a host needs its own."},+ {"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_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."},+ {"FLUXER_VOICE_STOP_GRACE", "voice-server", "How long Docker waits for the calls on the server to end when it stops or replaces the container, before it stops LiveKit anyway."},+ {"FLUXER_VOICE_EDGE_IP", "voice-edge", "The public IPv4 address of the voice machine, when an endpoint uses that address and not a hostname. The edge then answers clients that send no server name with the certificate for that address, which browsers need."},+ {"FLUXER_VOICE_EDGE_EMAIL", "voice-edge", "The email address the voice edge gives Let's Encrypt for notices about its certificates. When it's empty, the edge gets certificates without one."},{"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", ""},@@ -449,6 +468,7 @@ var envVars = []envVar{{"FLUXER_REGISTRY", "images", "The registry of Fluxer's own images in the docker-compose.yml of the Fluxer repository. When it's empty, that file uses `ghcr.io/` followed by FLUXER_REGISTRY_OWNER. The compose bundle pins every Fluxer image to the release."},{"FLUXER_REGISTRY_OWNER", "images", "The owner of Fluxer's own images on `ghcr.io`, in the docker-compose.yml of the Fluxer repository. The compose bundle pins every Fluxer image to the release."},{"FLUXER_IMAGE_TAG", "images", "The tag of Fluxer's own images in the docker-compose.yml of the Fluxer repository. The compose bundle pins every Fluxer image to the release. `rexctl upgrade --check` reads it to find the release a Docker Compose install runs, and uses `v1` when it's empty."},+ {"FLUXER_VOICE_JOIN_TOKEN", "rexctl", "The join token for `rexctl voice join` when the command line doesn't have one. Other accounts on the machine can't see it in the process list."},{"REXCTL_RELEASE_URL", "rexctl", "The URL of the release index that rexctl and its install scripts download releases from. When it's empty, they use https://fluxer.dev/rexctl/release.json. Another server must answer the same queries as that address, including `version` and `format=sh`."},{"REXCTL_IMAGE_SET", "rexctl", "The path of an image set JSON file with a release and its image list, which rexctl uses for its built-in images. Only tests use it."},{"VISUAL", "rexctl", "The editor `rexctl config edit` opens. When it's empty, rexctl uses `EDITOR`."},diff --git a/fluxer_rexctl/internal/instance/layout.go b/fluxer_rexctl/internal/instance/layout.goindex 6ef3c83f5..f5177db75 100644--- a/fluxer_rexctl/internal/instance/layout.go+++ b/fluxer_rexctl/internal/instance/layout.go@@ -19,6 +19,8 @@ import (const generatedSchema = 3+const LayoutSchema = generatedSchema+var (euid = os.Geteuidchown = os.Lchowndiff --git a/fluxer_rexctl/internal/instance/layout_test.go b/fluxer_rexctl/internal/instance/layout_test.goindex 573aaaf55..4f8bb6ac7 100644--- a/fluxer_rexctl/internal/instance/layout_test.go+++ b/fluxer_rexctl/internal/instance/layout_test.go@@ -7,6 +7,7 @@ import ("context""encoding/json""errors"+ "fmt""maps""os""path/filepath"@@ -561,12 +562,12 @@ func TestOlderRexctlRefusesMovedState(t *testing.T) {dir, old := oldLayout(t)migrated(t, dir, old)data := readTestFile(t, filepath.Join(dir, StateDirName, StateFileName))- older := strings.Replace(string(data), `"schema": 3`, `"schema": 4`, 1)- if _, err := ParseState([]byte(older), dir); !errors.Is(err, ErrNewerState) {+ newer := strings.Replace(string(data), fmt.Sprintf(`"schema": %d`, StateSchema), fmt.Sprintf(`"schema": %d`, StateSchema+1), 1)+ if _, err := ParseState([]byte(newer), dir); !errors.Is(err, ErrNewerState) {t.Fatalf("got %v, want ErrNewerState", err)}- if !bytes.Contains(data, []byte(`"schema": 3`)) {- t.Fatal("the moved state isn't schema 3, so rexctl with schema 2 would read it")+ if !bytes.Contains(data, fmt.Appendf(nil, `"schema": %d`, StateSchema)) || StateSchema <= LayoutSchema {+ t.Fatalf("the moved state has another schema than %d, and an older rexctl would read it", StateSchema)}}diff --git a/fluxer_rexctl/internal/instance/record.go b/fluxer_rexctl/internal/instance/record.goindex fc77f6791..2d6e3c0f1 100644--- a/fluxer_rexctl/internal/instance/record.go+++ b/fluxer_rexctl/internal/instance/record.go@@ -212,22 +212,33 @@ func (r Record) remember(dir string, makeDefault bool) Record {r = Record{Default: clean(fb), Instances: []string{clean(fb)}}}}- var others []string+ var others, fluxer []stringfor _, d := range r.Instances {if !same(d, dir) && !gone(d) {others = append(others, d)+ if !voiceMachine(d) {+ fluxer = append(fluxer, d)+ }}}out := r.With(dir)switch {case makeDefault:out.Default, out.Used = dir, true- case out.Default == "" && len(others) == 1:- out.Default, out.Used = others[0], false+ case out.Default != "":+ case len(fluxer) == 1:+ out.Default, out.Used = fluxer[0], false+ case len(fluxer) == 0 && len(others) > 0 && !voiceMachine(dir):+ out.Default, out.Used = dir, false}return out}+func voiceMachine(dir string) bool {+ s, err := loadState(filepath.Join(dir, StateDirName, StateFileName), nil)+ return err == nil && s.IsVoice()+}+func (r Record) Encode() []byte {if r.Instances == nil {r.Instances = []string{}diff --git a/fluxer_rexctl/internal/instance/record_test.go b/fluxer_rexctl/internal/instance/record_test.goindex d3a87c133..3dd459a79 100644--- a/fluxer_rexctl/internal/instance/record_test.go+++ b/fluxer_rexctl/internal/instance/record_test.go@@ -197,6 +197,38 @@ func TestRememberKeepsTheFirstAsDefault(t *testing.T) {}}+func voiceMachineAt(t *testing.T, dir string) string {+ t.Helper()+ inst, err := Create(dir, "fluxer-voice")+ if err != nil {+ t.Fatal(err)+ }+ inst.State.Kind = KindVoice+ if err := inst.Save(); err != nil {+ t.Fatal(err)+ }+ return clean(inst.Dir)+}++func TestRememberDoesNotMakeAVoiceMachineTheDefault(t *testing.T) {+ isolateRecords(t)+ machine, fluxer := voiceMachineAt(t, t.TempDir()), managedAt(t, t.TempDir())+ remember(t, ScopeUser, machine, false)+ if r := ReadRecord(ScopeUser); r.Default != "" {+ t.Fatalf("a lone voice machine became the default: %+v", r)+ }+ remember(t, ScopeUser, fluxer, false)+ r := ReadRecord(ScopeUser)+ if r.Default != fluxer || !slices.Equal(r.Instances, []string{machine, fluxer}) {+ t.Fatalf("record = %+v", r)+ }+ second := voiceMachineAt(t, t.TempDir())+ remember(t, ScopeUser, second, false)+ if l, err := Resolve(""); err != nil || l.Dir != fluxer {+ t.Errorf("Resolve = %+v, %v", l, err)+ }+}+func TestRememberTakesTheDefaultFromItsOwnScope(t *testing.T) {isolateRecords(t)machine := managedAt(t, t.TempDir())diff --git a/fluxer_rexctl/internal/instance/shipped.json b/fluxer_rexctl/internal/instance/shipped.jsonindex c52d19697..e71424173 100644--- a/fluxer_rexctl/internal/instance/shipped.json+++ b/fluxer_rexctl/internal/instance/shipped.json@@ -21,6 +21,7 @@"38eac49e5649a4520d03973c8bc2b1517a8a54a609305588d273124fb87ff943","39d9609d5fcb2c4df68267ea879600367c0a4dc261b056e005b1504b30f207d7","3c7c86d97879b3783cd02dc54bba1457506f3d13317f02d0f24dc520ea30b51e",+ "3d7a4b8c17e007b6d819e32cd428dff248b65b96e211e41c5032d725202b6c25","455e8fa8321e7feeb2eecec376c58e6d9b55fec7b411d575fd47e72e3a6591b7","48387bf3cad43d59021a54e194a57536ab29b902a1391ad960c5155003023cdd","4839813570835bd490a77c223393918a8886de6d41c91191fae650a3a2d9b62f",@@ -180,6 +181,7 @@"74eeabaed9809af982878f2891637b4e146b5da78e31dfaae87aa0ae66d530a4","75f33db78a433707af6b20749933215e38aa2506c2d7ee51d367051d7b855220","794babaca92fff4fc70620fd056e39e6c266d77ae8960d71a46bc9a69b54949b",+ "79a44a6e607b9ffa0b0860b0015661db8aefda9b248aa6c211a92c8a30241e05","7a37892360b92bd824767020df68c6e12a50488daed5c1079682cfd949eca94f","7ac84290b16576a44d8adbe0ae1bd8fd70e224087774d9e5a7ffceec929458fc","7cf2553a07462c9a433b4030e0994698589c7dfede40e0d0d38cc180912b8e0c",@@ -250,6 +252,7 @@"fdca5099f7d1012ff62b3353d6d79b1044471bd08186ae3ab45ef52042a53857"],"external-object-store.compose.yml": ["f8fb8bbca04500af5c266d8931dc7419f4899590eb92ed54e515f16f172fb8d8"],+ "external-voice.compose.yml": ["6f4018dcb5736cb813d84605e279cb33e592b7c25ec99a1a519e03a2c36d0872"],"ipv6.compose.yml": ["ee4bcdfd2a907372b5a0f3ab878ace957b72359c929456524cd095599fa0472f"],"tunnel.compose.yml": ["1c4225bf4663a705505b8db18aad4835614ee1e05bfda694026f0f0c30d361a4",@@ -258,5 +261,9 @@"60cf22578e6060036544a52ced0b45e2123a93a197827bd00094a93b17d61cec","7eed21df29b958db5af366aba9ea60bf9d234c73a50f319f373dd53f09b8d78f","d36c5a277a6c57c50e3a659e2ff60306862a11b3d3e44c54567b49d2d64779c0"- ]+ ],+ "voice/Caddyfile.voice": ["193a85faef0174276028f154858bfd98aaaebc2e2ba2d09c1154ff24156d3060"],+ "voice/voice-edge.compose.yml": ["8c42013b22dc829072af16e1db89a902358cf2c07e1a44bdd6297421ffba397c"],+ "voice/voice-http.compose.yml": ["f7a207a03625b080768b57f89a44b5828384d0efb528f92f30aa18034e4cf40d"],+ "voice/voice.compose.yml": ["b0ea7cf174eea1b5b882f21f7ab08f6526ecac6e96cb98ab990b3a0e8e6f26d9"]}diff --git a/fluxer_rexctl/internal/instance/shipped/main.go b/fluxer_rexctl/internal/instance/shipped/main.goindex 939d4b98f..1584f3aff 100644--- a/fluxer_rexctl/internal/instance/shipped/main.go+++ b/fluxer_rexctl/internal/instance/shipped/main.go@@ -45,6 +45,9 @@ func run() error {for _, name := range stack.OverlayFiles {names = append(names, name)}+ for _, name := range []string{"voice.compose.yml", "voice-http.compose.yml", "voice-edge.compose.yml", "Caddyfile.voice"} {+ names = append(names, "voice/"+name)+ }for _, name := range names {data, err := stack.ReadFile(set, name)if err != nil {diff --git a/fluxer_rexctl/internal/instance/state.go b/fluxer_rexctl/internal/instance/state.goindex 7fc07fa33..7bb5c49b2 100644--- a/fluxer_rexctl/internal/instance/state.go+++ b/fluxer_rexctl/internal/instance/state.go@@ -18,12 +18,13 @@ import ("github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings")-const StateSchema = generatedSchema+const StateSchema = generatedSchema + 1var ErrNewerState = errors.New("instance: state was written by a newer rexctl")type State struct {Schema int `json:"schema"`+ Kind string `json:"kind,omitempty"`Project string `json:"project"`Engine EngineRef `json:"engine"`Release string `json:"release"`@@ -47,6 +48,8 @@ type State struct {BackupDir string `json:"backup_dir,omitempty"`KeepBackups int `json:"keep_backups,omitempty"`NotifyURL string `json:"notify_url,omitempty"`+ Voice []VoiceServer `json:"voice,omitempty"`+ VoiceEdge *VoiceEdge `json:"voice_edge,omitempty"`InstalledAt time.Time `json:"installed_at"`UpdatedAt time.Time `json:"updated_at"`@@ -54,6 +57,86 @@ type State struct {rootLayout bool}+const KindVoice = "voice"++type VoiceServer struct {+ ID string `json:"id"`+ Region string `json:"region"`+ Endpoint string `json:"endpoint"`+ Hostname string `json:"hostname,omitempty"`+ Key string `json:"key"`+ Secret string `json:"secret"`+ TCPPort int `json:"tcp_port"`+ UDPPort int `json:"udp_port"`+ HTTPPort int `json:"http_port,omitempty"`+ NodeIP string `json:"node_ip,omitempty"`+ IPPinned bool `json:"pinned_node_ip,omitempty"`+ BindIP string `json:"bind_ip,omitempty"`+ Image string `json:"image"`+ Pinned bool `json:"pinned_image,omitempty"`+ Memory string `json:"memory"`+ SoftLimit int `json:"soft_limit,omitempty"`+ Opened []string `json:"opened,omitempty"`+ Project string `json:"project"`+ Network string `json:"network"`+ Webhook string `json:"webhook"`+ Instance string `json:"instance,omitempty"`+ NodeURL string `json:"node_url,omitempty"`+ Edge string `json:"edge,omitempty"`+ Joined bool `json:"joined,omitempty"`+ Drained bool `json:"drained,omitempty"`+ CreatedAt time.Time `json:"created_at"`+}++type VoiceEdge struct {+ Email string `json:"email,omitempty"`+ Opened []string `json:"opened,omitempty"`+ Instance string `json:"instance,omitempty"`+}++func (s *State) IsVoice() bool { return s != nil && s.Kind == KindVoice }++type VoicePort struct {+ Number int+ Proto string+ Server string+}++func (v VoiceServer) Ports() []VoicePort {+ out := []VoicePort{{v.TCPPort, "tcp", v.ID}, {v.UDPPort, "udp", v.ID}}+ if v.HTTPPort > 0 {+ out = append(out, VoicePort{v.HTTPPort, "tcp", v.ID})+ }+ return out+}++func RecordedVoicePorts() []VoicePort {+ var out []VoicePort+ for _, dir := range ReadRecord(OwnScope()).Instances {+ inst, err := Find(dir)+ if err != nil || inst.State == nil {+ continue+ }+ for _, v := range inst.State.Voice {+ out = append(out, v.Ports()...)+ }+ }+ return out+}++func (s *State) VoiceServer(id string) *VoiceServer {+ for i := range s.Voice {+ if s.Voice[i].ID == id {+ return &s.Voice[i]+ }+ }+ return nil+}++func (s *State) RemoveVoiceServer(id string) {+ s.Voice = slices.DeleteFunc(s.Voice, func(v VoiceServer) bool { return v.ID == id })+}+type EngineRef struct {Kind string `json:"kind"`Host string `json:"host"`@@ -151,6 +234,7 @@ var stateFields = map[string]bool{"upgrade": true, "pending": true, "images": true, "settings": true, "secrets": true, "admin_import": true,"setup_done": true, "installing": true, "stopped": true, "rendered": true, "mounted": true, "left_in_root": true, "moved_sites": true, "ejected": true,"installed_at": true, "updated_at": true, "backup_dir": true, "keep_backups": true, "notify_url": true,+ "kind": true, "voice": true, "voice_edge": true,}func parseState(data []byte, dir string, repairing []string) (*State, error) {diff --git a/fluxer_rexctl/internal/instance/values.go b/fluxer_rexctl/internal/instance/values.goindex 0bc1014be..4d1ea4ec1 100644--- a/fluxer_rexctl/internal/instance/values.go+++ b/fluxer_rexctl/internal/instance/values.go@@ -136,6 +136,8 @@ func importValues(in valueInput) (*valueResult, error) {r.settings[settings.EdgeMode] = string(o)case stack.OverlayIPv6:r.settings[settings.IPv6] = "true"+ case stack.OverlayExternalVoice:+ r.settings[settings.VoiceBundled] = "false"}}if err := dropRedundant(in.set, r); err != nil {diff --git a/fluxer_rexctl/internal/settings/keys.go b/fluxer_rexctl/internal/settings/keys.goindex 1c51bf1f5..264e71e62 100644--- a/fluxer_rexctl/internal/settings/keys.go+++ b/fluxer_rexctl/internal/settings/keys.go@@ -46,6 +46,7 @@ const (VoiceTCPPort = "voice.tcp_port"VoiceUDPPort = "voice.udp_port"VoicePublicIP = "voice.public_ip"+ VoiceBundled = "voice.bundled"StorageEndpoint = "storage.endpoint"StoragePublic = "storage.public_endpoint"StorageRegion = "storage.region"@@ -115,6 +116,9 @@ func buildNamed() []Key {Help: "The UDP port for voice and video"},{Name: VoicePublicIP, Type: "ip", Vars: []string{"FLUXER_LIVEKIT_NODE_IP", "FLUXER_LIVEKIT_USE_EXTERNAL_IP"}, Validate: validIP,Help: "The public IP address voice and video use, when Fluxer can't find it by itself"},+ {Name: VoiceBundled, Type: "bool", Vars: []string{"FLUXER_LIVEKIT_BUNDLED"}, Validate: validBool,+ Confirm: "The bundled LiveKit starts or stops, and calls on it end.",+ Help: "Whether Fluxer runs its own LiveKit voice server on this machine"},{Name: StorageEndpoint, Type: "url", Vars: []string{"FLUXER_S3_ENDPOINT"}, Validate: validURL, Confirm: storageConfirm,Help: "The address of the S3-compatible object store"},{Name: StoragePublic, Type: "url", Vars: []string{"FLUXER_S3_PUBLIC_ENDPOINT"}, Validate: validURL, Confirm: storageConfirm,diff --git a/fluxer_rexctl/internal/stack/bundle.go b/fluxer_rexctl/internal/stack/bundle.goindex b8b74a139..a117b4dda 100644--- a/fluxer_rexctl/internal/stack/bundle.go+++ b/fluxer_rexctl/internal/stack/bundle.go@@ -3,8 +3,10 @@package stackimport (+ "io/fs""os""path/filepath"+ "strings")const BundleProject = "fluxer"@@ -32,5 +34,23 @@ func Bundle(dir string, images map[string]string) error {return err}}- return nil+ return fs.WalkDir(set.Files, "voice", func(name string, d fs.DirEntry, err error) error {+ if err != nil {+ return err+ }+ target := filepath.Join(dir, filepath.FromSlash(name))+ if d.IsDir() {+ return os.MkdirAll(target, 0o755)+ }+ data, err := ReadFile(set, name)+ if err != nil {+ return err+ }+ if strings.HasSuffix(name, ".compose.yml") && set.Release != LegacyRelease {+ if data, err = pinImages(data, set.Release, images); err != nil {+ return err+ }+ }+ return os.WriteFile(target, data, 0o644)+ })}diff --git a/fluxer_rexctl/internal/stack/bundle_test.go b/fluxer_rexctl/internal/stack/bundle_test.gonew file mode 100644index 000000000..f477509f4--- /dev/null+++ b/fluxer_rexctl/internal/stack/bundle_test.go@@ -0,0 +1,41 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package stack++import (+ "os"+ "path/filepath"+ "strings"+ "testing"+)++func TestBundlePinsTheVoiceImages(t *testing.T) {+ compose, err := ReadFile(Embedded(), ComposeFile)+ if err != nil {+ t.Fatal(err)+ }+ found, err := imageLines(compose)+ if err != nil {+ t.Fatal(err)+ }+ images := map[string]string{}+ for _, img := range found {+ images[img.service] = "registry.example.com/" + img.service + ":pinned"+ }+ dir := t.TempDir()+ if err := Bundle(dir, images); err != nil {+ t.Fatal(err)+ }+ for file, want := range map[string]string{+ "voice.compose.yml": "${FLUXER_LIVEKIT_IMAGE:-registry.example.com/livekit:pinned}",+ "voice-edge.compose.yml": "${FLUXER_CADDY_IMAGE:-registry.example.com/edge:pinned}",+ } {+ data, err := os.ReadFile(filepath.Join(dir, "voice", file))+ if err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(string(data), want) {+ t.Errorf("%s doesn't pin %s", file, want)+ }+ }+}diff --git a/fluxer_rexctl/internal/stack/files/.env.example b/fluxer_rexctl/internal/stack/files/.env.exampleindex 43b0113e5..dece6c948 100644--- a/fluxer_rexctl/internal/stack/files/.env.example+++ b/fluxer_rexctl/internal/stack/files/.env.example@@ -179,6 +179,7 @@ FLUXER_VAPID_PRIVATE_KEY=# tunnel.compose.yml for a tunnel# external-object-store.compose.yml for your own object store# ipv6.compose.yml for IPv6 on Fluxer's network+# external-voice.compose.yml for voice servers of your own without the bundled LiveKit# ============================================================================# The compose files Docker Compose uses, with docker-compose.yml first.@@ -407,7 +408,9 @@ FLUXER_VAPID_PRIVATE_KEY=# VOICE AND VIDEO## Calls connect directly to this machine on FLUXER_LIVEKIT_TCP_PORT and-# FLUXER_LIVEKIT_UDP_PORT, even when Fluxer is behind your own proxy.+# FLUXER_LIVEKIT_UDP_PORT, even when Fluxer is behind your own proxy. To run+# without the bundled LiveKit, add external-voice.compose.yml to COMPOSE_FILE+# and set FLUXER_LIVEKIT_BUNDLED to false.# ============================================================================# Whether voice and video calls are turned on. Use true or false.@@ -417,11 +420,22 @@ FLUXER_VAPID_PRIVATE_KEY=# The URL the API uses for the LiveKit control API of the bundled LiveKit, the# first server in the default voice region. Servers added in the admin panel-# use their own address.+# use their own address. Fluxer ignores it when FLUXER_LIVEKIT_BUNDLED is+# false.# default: http://livekit:7880# rexctl: rexctl config set FLUXER_LIVEKIT_INTERNAL_URL# FLUXER_LIVEKIT_INTERNAL_URL=+# Whether Fluxer runs its own LiveKit voice server on this machine. Use true+# or false. The bundled LiveKit is the first server in the default voice+# region. With false, Fluxer creates the default region with no server in it,+# removes the server it made for the bundled LiveKit and keeps calls on the+# servers you add with rexctl voice add, rexctl voice join or the admin panel.+# With Docker Compose, also add external-voice.compose.yml to COMPOSE_FILE.+# default: true+# rexctl: rexctl config set voice.bundled+# FLUXER_LIVEKIT_BUNDLED=+# The voice region Fluxer creates when no voice regions exist yet, as a JSON# object with id, name, emoji, latitude and longitude.# default: {"id":"default","name":"Default","emoji":"🌍","latitude":0,"longitude":0}@@ -442,6 +456,19 @@ FLUXER_VAPID_PRIVATE_KEY=# rexctl: rexctl config set FLUXER_VOICE_P2P_STUN_URLS# FLUXER_VOICE_P2P_STUN_URLS=+# Whether the API accepts a voice server joining with an endpoint that+# resolves to a private address. Use true or false. Set it to true only for+# tests and private networks.+# rexctl: rexctl config set FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS+# FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS=++# How often each API node checks every active voice server, in milliseconds. A+# server that fails two checks in a row gets no new calls until a check+# passes. Set it to 0 to turn the checks off. When it's empty, the API checks+# every 15 seconds.+# rexctl: rexctl config set FLUXER_VOICE_HEALTH_INTERVAL_MS+# FLUXER_VOICE_HEALTH_INTERVAL_MS=+# The TCP port for voice and video.# default: 7881# rexctl: rexctl config set voice.tcp_portdiff --git a/fluxer_rexctl/internal/stack/files/docker-compose.yml b/fluxer_rexctl/internal/stack/files/docker-compose.ymlindex e3f8a34f4..722e1a8ae 100644--- a/fluxer_rexctl/internal/stack/files/docker-compose.yml+++ b/fluxer_rexctl/internal/stack/files/docker-compose.yml@@ -113,9 +113,12 @@ x-fluxer-env: &fluxer-envFLUXER_LIVEKIT_API_KEY: ${LIVEKIT_API_KEY:-}FLUXER_LIVEKIT_API_SECRET: ${LIVEKIT_API_SECRET:-}FLUXER_LIVEKIT_INTERNAL_URL: ${FLUXER_LIVEKIT_INTERNAL_URL:-http://livekit:7880}+ FLUXER_LIVEKIT_BUNDLED: "${FLUXER_LIVEKIT_BUNDLED:-true}"FLUXER_LIVEKIT_DEFAULT_REGION: '${FLUXER_LIVEKIT_DEFAULT_REGION:-{"id":"default","name":"Default","emoji":"🌍","latitude":0,"longitude":0}}'FLUXER_LIVEKIT_URL: ${FLUXER_LIVEKIT_URL:-${FLUXER_PUBLIC_ORIGIN:-${FLUXER_PUBLIC_SCHEME:-https}://${FLUXER_DOMAIN:-}:${FLUXER_PUBLIC_PORT:-443}}/livekit}FLUXER_VOICE_P2P_STUN_URLS: ${FLUXER_VOICE_P2P_STUN_URLS:-}+ FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS: ${FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS:-}+ FLUXER_VOICE_HEALTH_INTERVAL_MS: ${FLUXER_VOICE_HEALTH_INTERVAL_MS:-}FLUXER_KLIPY_API_KEY: ${FLUXER_KLIPY_API_KEY:-}FLUXER_YOUTUBE_API_KEY: ${FLUXER_YOUTUBE_API_KEY:-}diff --git a/fluxer_rexctl/internal/stack/files/external-voice.compose.yml b/fluxer_rexctl/internal/stack/files/external-voice.compose.ymlnew file mode 100644index 000000000..252baa1be--- /dev/null+++ b/fluxer_rexctl/internal/stack/files/external-voice.compose.yml@@ -0,0 +1,3 @@+services:+ livekit:+ profiles: [bundled-voice]diff --git a/fluxer_rexctl/internal/stack/files/voice/Caddyfile.voice b/fluxer_rexctl/internal/stack/files/voice/Caddyfile.voicenew file mode 100644index 000000000..3d33063dc--- /dev/null+++ b/fluxer_rexctl/internal/stack/files/voice/Caddyfile.voice@@ -0,0 +1,22 @@+{+ {$FLUXER_VOICE_EDGE_GLOBAL_OPTIONS}+ {$FLUXER_VOICE_EDGE_SNI_OPTION}+}++(livekit) {+ reverse_proxy {args[0]} {+ header_up X-Forwarded-For {client_ip}+ header_up X-Real-IP {client_ip}+ header_up -Forwarded+ stream_timeout 24h+ stream_close_delay 5m+ }+}++:80 {+ handle /_health {+ respond "OK" 200+ }+}++import /etc/caddy/sites.d/*.caddydiff --git a/fluxer_rexctl/internal/stack/files/voice/sites.d/voice.caddy.example b/fluxer_rexctl/internal/stack/files/voice/sites.d/voice.caddy.examplenew file mode 100644index 000000000..baf010097--- /dev/null+++ b/fluxer_rexctl/internal/stack/files/voice/sites.d/voice.caddy.example@@ -0,0 +1,13 @@+voice-fra.chat.example.com {+ handle /_health {+ respond "OK" 200+ }++ handle_path /livekit-fra-server-1/* {+ import livekit voice-fra-server-1:7880+ }++ handle {+ respond 404+ }+}diff --git a/fluxer_rexctl/internal/stack/files/voice/voice-edge.compose.yml b/fluxer_rexctl/internal/stack/files/voice/voice-edge.compose.ymlnew file mode 100644index 000000000..1ae108dc8--- /dev/null+++ b/fluxer_rexctl/internal/stack/files/voice/voice-edge.compose.yml@@ -0,0 +1,38 @@+services:+ edge:+ image: ${FLUXER_CADDY_IMAGE:-caddy:2.11-alpine}+ restart: unless-stopped+ logging:+ driver: json-file+ options:+ max-size: ${FLUXER_LOG_MAX_SIZE:-10m}+ max-file: "${FLUXER_LOG_MAX_FILE:-3}"+ ports:+ - "80:80"+ - "443:443"+ - "443:443/udp"+ environment:+ FLUXER_VOICE_EDGE_GLOBAL_OPTIONS: ${FLUXER_VOICE_EDGE_EMAIL:+email ${FLUXER_VOICE_EDGE_EMAIL:-}}+ FLUXER_VOICE_EDGE_SNI_OPTION: ${FLUXER_VOICE_EDGE_IP:+default_sni ${FLUXER_VOICE_EDGE_IP:-}}+ volumes:+ - ./Caddyfile.voice:/etc/caddy/Caddyfile:ro+ - ./sites.d:/etc/caddy/sites.d:ro+ - edge-data:/data+ - edge-config:/config+ networks:+ - voice+ healthcheck:+ test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:2019/config/"]+ interval: 10s+ timeout: 5s+ retries: 10+ start_period: 30s+ start_interval: 1s++networks:+ voice:+ name: ${FLUXER_VOICE_NETWORK:-fluxer-voice}++volumes:+ edge-data:+ edge-config:diff --git a/fluxer_rexctl/internal/stack/files/voice/voice-http.compose.yml b/fluxer_rexctl/internal/stack/files/voice/voice-http.compose.ymlnew file mode 100644index 000000000..fc849516c--- /dev/null+++ b/fluxer_rexctl/internal/stack/files/voice/voice-http.compose.yml@@ -0,0 +1,4 @@+services:+ livekit:+ ports:+ - "127.0.0.1:${FLUXER_VOICE_HTTP_PORT:-7880}:7880/tcp"diff --git a/fluxer_rexctl/internal/stack/files/voice/voice.compose.yml b/fluxer_rexctl/internal/stack/files/voice/voice.compose.ymlnew file mode 100644index 000000000..549d3bd99--- /dev/null+++ b/fluxer_rexctl/internal/stack/files/voice/voice.compose.yml@@ -0,0 +1,53 @@+services:+ livekit:+ image: ${FLUXER_LIVEKIT_IMAGE:-livekit/livekit-server:v1.12.0}+ restart: unless-stopped+ stop_grace_period: ${FLUXER_VOICE_STOP_GRACE:-15m}+ deploy:+ resources:+ limits:+ memory: ${FLUXER_VOICE_MEMORY_LIMIT:-2g}+ logging:+ driver: json-file+ options:+ max-size: ${FLUXER_LOG_MAX_SIZE:-10m}+ max-file: "${FLUXER_LOG_MAX_FILE:-3}"+ environment:+ GOMEMLIMIT: ${FLUXER_VOICE_GOMEMLIMIT:-1700MiB}+ LIVEKIT_KEYS: "${FLUXER_VOICE_API_KEY:?Set FLUXER_VOICE_API_KEY}: ${FLUXER_VOICE_API_SECRET:?Set FLUXER_VOICE_API_SECRET}"+ LIVEKIT_CONFIG: |+ port: 7880+ log_level: ${FLUXER_LIVEKIT_LOG_LEVEL:-info}+ rtc:+ tcp_port: ${FLUXER_VOICE_TCP_PORT:-7881}+ udp_port: ${FLUXER_VOICE_UDP_PORT:-7882}+ use_external_ip: ${FLUXER_VOICE_USE_EXTERNAL_IP:-true}+ node_ip: "${FLUXER_VOICE_NODE_IP:-}"+ stun_servers:+ - ${FLUXER_LIVEKIT_STUN_PRIMARY:-stun.l.google.com:19302}+ - ${FLUXER_LIVEKIT_STUN_SECONDARY:-stun1.l.google.com:19302}+ turn:+ enabled: false+ webhook:+ api_key: ${FLUXER_VOICE_API_KEY:?Set FLUXER_VOICE_API_KEY}+ urls:+ - ${FLUXER_VOICE_WEBHOOK_URL:?Set FLUXER_VOICE_WEBHOOK_URL}+ ports:+ - "${FLUXER_VOICE_BIND_IP:+${FLUXER_VOICE_BIND_IP}:}${FLUXER_VOICE_TCP_PORT:-7881}:${FLUXER_VOICE_TCP_PORT:-7881}/tcp"+ - "${FLUXER_VOICE_BIND_IP:+${FLUXER_VOICE_BIND_IP}:}${FLUXER_VOICE_UDP_PORT:-7882}:${FLUXER_VOICE_UDP_PORT:-7882}/udp"+ networks:+ voice:+ aliases:+ - voice-${FLUXER_VOICE_ID:?Set FLUXER_VOICE_ID}+ healthcheck:+ test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:7880/"]+ interval: 10s+ timeout: 5s+ retries: 10+ start_period: 30s+ start_interval: 1s++networks:+ voice:+ external: true+ name: ${FLUXER_VOICE_NETWORK:-fluxer-voice}diff --git a/fluxer_rexctl/internal/stack/services.go b/fluxer_rexctl/internal/stack/services.goindex 2599b1505..f89e30df8 100644--- a/fluxer_rexctl/internal/stack/services.go+++ b/fluxer_rexctl/internal/stack/services.go@@ -52,10 +52,12 @@ type Port struct {const (VoiceTCPPortKey = "FLUXER_LIVEKIT_TCP_PORT"VoiceUDPPortKey = "FLUXER_LIVEKIT_UDP_PORT"+ VoiceBundledKey = "FLUXER_LIVEKIT_BUNDLED")func Ports(env map[string]string) []Port {out := make([]Port, 0, len(PublishedPorts))+ bundled := VoiceBundled(env[VoiceBundledKey])for _, p := range PublishedPorts {key := ""switch {@@ -64,6 +66,9 @@ func Ports(env map[string]string) []Port {case p.Number == 7882 && p.Proto == "udp":key = VoiceUDPPortKey}+ if key != "" && !bundled {+ continue+ }if n, err := strconv.Atoi(strings.TrimSpace(env[key])); key != "" && err == nil && n > 0 && n <= 65535 {p.Number = n}diff --git a/fluxer_rexctl/internal/stack/services_test.go b/fluxer_rexctl/internal/stack/services_test.goindex d7ee8c6d3..8dbfff0eb 100644--- a/fluxer_rexctl/internal/stack/services_test.go+++ b/fluxer_rexctl/internal/stack/services_test.go@@ -97,3 +97,42 @@ func TestNatsClientsWaitForHealthyNats(t *testing.T) {t.Errorf("gateway healthcheck %q should ask /_health/ready, which fails while the gateway's NATS subscriptions are missing", check)}}++func TestPortsWithoutBundledVoice(t *testing.T) {+ got := Ports(map[string]string{VoiceBundledKey: "false", VoiceTCPPortKey: "7883"})+ want := []Port{+ {Number: 80, Proto: "tcp"},+ {Number: 443, Proto: "tcp"},+ {Number: 443, Proto: "udp"},+ }+ if !slices.Equal(got, want) {+ t.Errorf("Ports without bundled voice = %v, want %v", got, want)+ }+ if got := Ports(map[string]string{VoiceBundledKey: "true"}); !slices.Equal(got, PublishedPorts) {+ t.Errorf("Ports with bundled voice = %v", got)+ }+}++func TestOverlaysForBundledVoice(t *testing.T) {+ for value, want := range map[string]bool{"": false, "true": false, "1": false, "false": true, "0": true, "nonsense": false} {+ got := slices.Contains(OverlaysFor(map[string]string{"voice.bundled": value}), OverlayExternalVoice)+ if got != want {+ t.Errorf("voice.bundled %q: external voice overlay = %v, want %v", value, got, want)+ }+ }+ data, err := ReadFile(Embedded(), OverlayFiles[OverlayExternalVoice])+ if err != nil {+ t.Fatal(err)+ }+ var overlay struct {+ Services map[string]struct {+ Profiles []string `yaml:"profiles"`+ } `yaml:"services"`+ }+ if err := yaml.Unmarshal(data, &overlay); err != nil {+ t.Fatal(err)+ }+ if got := overlay.Services["livekit"].Profiles; !slices.Equal(got, []string{"bundled-voice"}) {+ t.Errorf("livekit profiles = %v", got)+ }+}diff --git a/fluxer_rexctl/internal/stack/stack.go b/fluxer_rexctl/internal/stack/stack.goindex 044f47b2b..0cec80143 100644--- a/fluxer_rexctl/internal/stack/stack.go+++ b/fluxer_rexctl/internal/stack/stack.go@@ -35,6 +35,7 @@ const (OverlayTunnel Overlay = "tunnel"OverlayExternalObjectStore Overlay = "external-object-store"OverlayIPv6 Overlay = "ipv6"+ OverlayExternalVoice Overlay = "external-voice")var OverlayFiles = map[Overlay]string{@@ -42,9 +43,10 @@ var OverlayFiles = map[Overlay]string{OverlayTunnel: "tunnel.compose.yml",OverlayExternalObjectStore: "external-object-store.compose.yml",OverlayIPv6: "ipv6.compose.yml",+ OverlayExternalVoice: "external-voice.compose.yml",}-var overlayOrder = []Overlay{OverlayProxy, OverlayTunnel, OverlayExternalObjectStore, OverlayIPv6}+var overlayOrder = []Overlay{OverlayProxy, OverlayTunnel, OverlayExternalObjectStore, OverlayIPv6, OverlayExternalVoice}var MountedFiles = map[string][]string{Caddyfile: {"edge"},@@ -108,9 +110,17 @@ func OverlaysFor(settings map[string]string) []Overlay {if on, err := strconv.ParseBool(strings.TrimSpace(settings["network.ipv6"])); err == nil && on {out = append(out, OverlayIPv6)}+ if !VoiceBundled(settings["voice.bundled"]) {+ out = append(out, OverlayExternalVoice)+ }return out}+func VoiceBundled(value string) bool {+ on, err := strconv.ParseBool(strings.TrimSpace(value))+ return err != nil || on+}+func FileNames() []string {names := []string{ComposeFile, Caddyfile}for _, o := range overlayOrder {diff --git a/fluxer_rexctl/internal/voice/net.go b/fluxer_rexctl/internal/voice/net.gonew file mode 100644index 000000000..91251040d--- /dev/null+++ b/fluxer_rexctl/internal/voice/net.go@@ -0,0 +1,397 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package voice++import (+ "bufio"+ "context"+ "crypto/tls"+ "crypto/x509"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "net"+ "net/http"+ "net/netip"+ "net/url"+ "os"+ "slices"+ "strings"+ "time"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/settings"+)++var cgnatRange = netip.MustParsePrefix("100.64.0.0/10")++var cloudflareRanges = []string{+ "173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", "141.101.64.0/18",+ "108.162.192.0/18", "190.93.240.0/20", "188.114.96.0/20", "197.234.240.0/22", "198.41.128.0/17",+ "162.158.0.0/15", "104.16.0.0/13", "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22",+}++func IsCloudflare(addr string) bool {+ ip, err := netip.ParseAddr(addr)+ if err != nil {+ return false+ }+ for _, r := range cloudflareRanges {+ if netip.MustParsePrefix(r).Contains(ip) {+ return true+ }+ }+ return false+}++func IsCGNAT(addr string) bool {+ ip, err := netip.ParseAddr(addr)+ return err == nil && cgnatRange.Contains(ip.Unmap())+}++func InterfaceAddresses() []string {+ var out []string+ addrs, err := net.InterfaceAddrs()+ if err != nil {+ return nil+ }+ for _, a := range addrs {+ if p, err := netip.ParsePrefix(a.String()); err == nil {+ out = append(out, p.Addr().Unmap().String())+ }+ }+ return out+}++func DefaultRouteIPv4() string {+ conn, err := net.Dial("udp4", "198.51.100.1:9")+ if err != nil {+ return ""+ }+ defer conn.Close()+ if a, ok := conn.LocalAddr().(*net.UDPAddr); ok {+ return a.IP.String()+ }+ return ""+}++type Reachability struct {+ PublicIPv4 string+ BehindNAT bool+ CGNAT bool+}++func Assess(publicIPv4 string, local []string, routeIP string) Reachability {+ r := Reachability{PublicIPv4: publicIPv4}+ r.BehindNAT = publicIPv4 != "" && !slices.Contains(local, publicIPv4)+ r.CGNAT = IsCGNAT(routeIP)+ return r+}++type DNSAnswer struct {+ A []string+ AAAA []string+}++type Resolver func(ctx context.Context, name string) (DNSAnswer, error)++var dohServers = []string{"https://1.1.1.1/dns-query", "https://8.8.8.8/resolve"}++func PublicLookup(hc *http.Client) Resolver {+ return func(ctx context.Context, name string) (DNSAnswer, error) {+ var last error+ for _, server := range dohServers {+ a, errA := doh(ctx, hc, server, name, "A")+ aaaa, errAAAA := doh(ctx, hc, server, name, "AAAA")+ if errA == nil && errAAAA == nil {+ return DNSAnswer{A: a, AAAA: aaaa}, nil+ }+ last = errors.Join(errA, errAAAA)+ }+ addrs, err := net.DefaultResolver.LookupHost(ctx, name)+ if err != nil {+ return DNSAnswer{}, errors.Join(last, err)+ }+ var out DNSAnswer+ for _, a := range addrs {+ if ip, err := netip.ParseAddr(a); err == nil && ip.Is4() {+ out.A = append(out.A, a)+ } else if err == nil {+ out.AAAA = append(out.AAAA, a)+ }+ }+ return out, nil+ }+}++func doh(ctx context.Context, hc *http.Client, server, name, kind string) ([]string, error) {+ u, _ := url.Parse(server)+ q := u.Query()+ q.Set("name", name)+ q.Set("type", kind)+ u.RawQuery = q.Encode()+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)+ if err != nil {+ return nil, err+ }+ req.Header.Set("Accept", "application/dns-json")+ resp, err := hc.Do(req)+ if err != nil {+ return nil, err+ }+ defer resp.Body.Close()+ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("voice: %s answered %d", server, resp.StatusCode)+ }+ var body struct {+ Status int `json:"Status"`+ Answer []struct {+ Type int `json:"type"`+ Data string `json:"data"`+ } `json:"Answer"`+ }+ if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<16)).Decode(&body); err != nil {+ return nil, err+ }+ want := map[string]int{"A": 1, "AAAA": 28}[kind]+ var out []string+ for _, a := range body.Answer {+ if a.Type == want {+ out = append(out, a.Data)+ }+ }+ return out, nil+}++type HostnameCheck int++const (+ HostnameMissing HostnameCheck = iota+ HostnameOK+ HostnameElsewhere+ HostnameIPv6Elsewhere+ HostnameProxied+)++func CheckHostname(answer DNSAnswer, publicIPv4 string, ownIPv6 []string) HostnameCheck {+ if len(answer.A) == 0 {+ return HostnameMissing+ }+ if !slices.ContainsFunc(answer.A, func(a string) bool { return !IsCloudflare(a) }) {+ return HostnameProxied+ }+ for _, a := range answer.A {+ if a != publicIPv4 {+ return HostnameElsewhere+ }+ }+ for _, a := range answer.AAAA {+ if !slices.Contains(ownIPv6, a) {+ return HostnameIPv6Elsewhere+ }+ }+ return HostnameOK+}++func HTTPBase(endpoint string) string {+ u, err := url.Parse(endpoint)+ if err != nil {+ return endpoint+ }+ switch u.Scheme {+ case "wss":+ u.Scheme = "https"+ case "ws":+ u.Scheme = "http"+ }+ return strings.TrimRight(u.String(), "/")+}++func LocalBase(httpPort int) string {+ return fmt.Sprintf("http://127.0.0.1:%d", httpPort)+}++func InstanceEdge(endpoint, mode, httpsPort, bind string) (string, string) {+ base := HTTPBase(endpoint)+ switch mode {+ case settings.EdgeProxy, settings.EdgeTunnel:+ addr := "127.0.0.1:8080"+ if mode == settings.EdgeTunnel {+ addr = "127.0.0.1:80"+ }+ if v := usable(bind); v != "" {+ addr = loopback(v)+ }+ if u, err := url.Parse(base); err == nil {+ u.Scheme = "http"+ base = u.String()+ }+ return base, addr+ }+ port := "443"+ if v := usable(httpsPort); v != "" {+ port = v+ }+ return base, "127.0.0.1:" + port+}++func usable(v string) string {+ v = strings.TrimSpace(v)+ if strings.Contains(v, "$") {+ return ""+ }+ return v+}++func loopback(v string) string {+ h, port, err := net.SplitHostPort(v)+ if err != nil {+ h, port = "", strings.TrimPrefix(v, ":")+ }+ if h == "" || h == "0.0.0.0" || h == "::" {+ h = "127.0.0.1"+ }+ return net.JoinHostPort(h, port)+}++func probeClient(base, dialAddr string, insecure bool) (*http.Client, *http.Transport, error) {+ u, err := url.Parse(base)+ if err != nil {+ return nil, nil, err+ }+ dialer := &net.Dialer{Timeout: 5 * time.Second}+ tr := &http.Transport{+ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {+ if dialAddr != "" {+ addr = dialAddr+ }+ return dialer.DialContext(ctx, network, addr)+ },+ TLSClientConfig: &tls.Config{ServerName: u.Hostname(), InsecureSkipVerify: insecure},+ }+ hc := &http.Client{Transport: tr, Timeout: 10 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}+ return hc, tr, nil+}++func ProbeThroughEdge(ctx context.Context, base, dialAddr string, insecure bool) error {+ hc, tr, err := probeClient(base, dialAddr, insecure)+ if err != nil {+ return err+ }+ defer tr.CloseIdleConnections()+ target := strings.TrimRight(base, "/") + "/"+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil)+ if err != nil {+ return err+ }+ resp, err := hc.Do(req)+ if err != nil {+ return err+ }+ defer resp.Body.Close()+ body, _ := io.ReadAll(io.LimitReader(resp.Body, 64))+ if resp.StatusCode != http.StatusOK || strings.TrimSpace(string(body)) != "OK" {+ return fmt.Errorf("voice: %s answered %d", target, resp.StatusCode)+ }+ return nil+}++func CertificateExpiry(ctx context.Context, endpoint, edgeAddr string) (time.Time, error) {+ u, err := url.Parse(endpoint)+ if err != nil {+ return time.Time{}, err+ }+ d := &tls.Dialer{NetDialer: &net.Dialer{Timeout: 5 * time.Second}, Config: &tls.Config{ServerName: u.Hostname(), InsecureSkipVerify: true}}+ conn, err := d.DialContext(ctx, "tcp", edgeAddr)+ if err != nil {+ return time.Time{}, err+ }+ defer conn.Close()+ state := conn.(*tls.Conn).ConnectionState()+ if len(state.PeerCertificates) == 0 {+ return time.Time{}, errors.New("voice: no certificate")+ }+ leaf := state.PeerCertificates[0]+ pool := x509.NewCertPool()+ for _, c := range state.PeerCertificates[1:] {+ pool.AddCert(c)+ }+ if _, err := leaf.Verify(x509.VerifyOptions{DNSName: u.Hostname(), Intermediates: pool}); err != nil {+ return leaf.NotAfter, err+ }+ return leaf.NotAfter, nil+}++func ReadSysctl(name string) string {+ data, err := os.ReadFile("/proc/sys/" + strings.ReplaceAll(name, ".", "/"))+ if err != nil {+ return ""+ }+ return strings.TrimSpace(string(data))+}++func LogLines(r io.Reader, match func(string) bool) []string {+ var out []string+ sc := bufio.NewScanner(r)+ sc.Buffer(make([]byte, 0, 64*1024), 1<<20)+ for sc.Scan() {+ if line := sc.Text(); match(line) {+ out = append(out, line)+ }+ }+ return out+}++type RoomLoad struct {+ Rooms int+ Participants int+}++func ListRooms(ctx context.Context, base, dialAddr, key, secret string, insecure bool) (RoomLoad, error) {+ hc, tr, err := probeClient(base, dialAddr, insecure)+ if err != nil {+ return RoomLoad{}, err+ }+ defer tr.CloseIdleConnections()+ now := time.Now()+ token := SignHS256(secret, map[string]any{+ "iss": key,+ "nbf": now.Add(-10 * time.Second).Unix(),+ "exp": now.Add(time.Minute).Unix(),+ "video": map[string]any{"roomList": true},+ })+ target := strings.TrimRight(base, "/") + "/twirp/livekit.RoomService/ListRooms"+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, target, strings.NewReader("{}"))+ if err != nil {+ return RoomLoad{}, err+ }+ req.Header.Set("Content-Type", "application/json")+ req.Header.Set("Authorization", "Bearer "+token)+ resp, err := hc.Do(req)+ if err != nil {+ return RoomLoad{}, err+ }+ defer resp.Body.Close()+ if resp.StatusCode != http.StatusOK {+ return RoomLoad{}, fmt.Errorf("voice: the room service answered %d", resp.StatusCode)+ }+ var body struct {+ Rooms []struct {+ Name string `json:"name"`+ NumParticipants int `json:"num_participants"`+ CamelCase int `json:"numParticipants"`+ } `json:"rooms"`+ }+ if err := json.NewDecoder(io.LimitReader(resp.Body, 4<<20)).Decode(&body); err != nil {+ return RoomLoad{}, err+ }+ var load RoomLoad+ for _, r := range body.Rooms {+ if strings.HasPrefix(r.Name, TestRoomPrefix) {+ continue+ }+ load.Rooms+++ load.Participants += max(r.NumParticipants, r.CamelCase)+ }+ return load, nil+}diff --git a/fluxer_rexctl/internal/voice/node.go b/fluxer_rexctl/internal/voice/node.gonew file mode 100644index 000000000..80ae8cd26--- /dev/null+++ b/fluxer_rexctl/internal/voice/node.go@@ -0,0 +1,440 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package voice++import (+ "bytes"+ "context"+ "crypto/hmac"+ "crypto/sha256"+ "encoding/base64"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "net/http"+ "net/url"+ "strconv"+ "strings"+ "time"+)++const (+ TokenPrefix = "fvj_"+ NodeAudience = "fluxer-voice-node"+ NodePath = "/voice/node"+ nodeTokenLife = 2 * time.Minute+)++const (+ CodeTokenInvalid = "VOICE_JOIN_TOKEN_INVALID"+ CodeTokenBusy = "VOICE_JOIN_TOKEN_BUSY"+ CodeEndpointInvalid = "VOICE_JOIN_ENDPOINT_INVALID"+ CodeEndpointUnreachable = "VOICE_JOIN_ENDPOINT_UNREACHABLE"+ CodeKeyMismatch = "VOICE_JOIN_KEY_MISMATCH"+ CodeIDInUse = "VOICE_SERVER_ID_IN_USE"+ CodeKeyInUse = "VOICE_SERVER_API_KEY_IN_USE"+ CodeNodeUnknown = "VOICE_NODE_UNKNOWN"+ CodeSignatureInvalid = "VOICE_NODE_SIGNATURE_INVALID"+ CodeTokenExpired = "VOICE_NODE_TOKEN_EXPIRED"+ CodeUnknownRegion = "UNKNOWN_VOICE_REGION"+)++func ValidToken(token string) bool {+ rest, ok := strings.CutPrefix(token, TokenPrefix)+ if !ok {+ return false+ }+ id, secret, ok := strings.Cut(rest, "_")+ return ok && id != "" && strings.Trim(id, "0123456789") == "" && len(secret) == 43+}++type APIError struct {+ Status int+ Code string+ Message string+ Detail string+ Date time.Time+}++func (e *APIError) Error() string {+ msg := e.Message+ if e.Detail != "" {+ msg += " (" + e.Detail + ")"+ }+ if e.Code != "" {+ return fmt.Sprintf("%s: %s", e.Code, msg)+ }+ return fmt.Sprintf("HTTP %d: %s", e.Status, msg)+}++func CodeOf(err error) string {+ var e *APIError+ if errors.As(err, &e) {+ return e.Code+ }+ return ""+}++const ClockFix = "Set the clock on this machine. sudo timedatectl set-ntp true turns on time sync on most Linux machines."++const clockLeeway = time.Minute++func ClockProblem(err error, instance string, now time.Time) string {+ var e *APIError+ if !errors.As(err, &e) || e.Code != CodeTokenExpired {+ return ""+ }+ if text := ClockOff(e.Date, instance, now); text != "" {+ return text+ }+ return fmt.Sprintf("%s rejects the time on requests from this machine", instance)+}++func ClockOff(date time.Time, instance string, now time.Time) string {+ off := now.Sub(date)+ switch {+ case date.IsZero() || off.Abs() <= clockLeeway:+ return ""+ case off < 0:+ return fmt.Sprintf("The clock on this machine is about %s behind %s", about(-off), instance)+ }+ return fmt.Sprintf("The clock on this machine is about %s ahead of %s", about(off), instance)+}++func about(d time.Duration) string {+ unit, name := time.Second, "second"+ switch {+ case d >= 90*time.Minute:+ unit, name = time.Hour, "hour"+ case d >= 90*time.Second:+ unit, name = time.Minute, "minute"+ }+ n := int(d.Round(unit) / unit)+ if n == 1 {+ return "1 " + name+ }+ return fmt.Sprintf("%d %ss", n, name)+}++type Discovery struct {+ APIPublic string+ APICodeVersion int+ VoiceEnabled bool+ VoiceOrigins []string+ ListsOrigins bool+}++type Client struct {+ HTTP *http.Client+}++func (c *Client) http() *http.Client {+ if c.HTTP != nil {+ return c.HTTP+ }+ return &http.Client{Timeout: 30 * time.Second}+}++func (c *Client) Discover(ctx context.Context, domain string) (*Discovery, error) {+ target := domain+ if !strings.Contains(target, "://") {+ target = "https://" + target+ }+ u, err := url.Parse(target)+ if err != nil || u.Host == "" {+ return nil, fmt.Errorf("voice: %s isn't a domain", domain)+ }+ u.Path = "/.well-known/fluxer"+ var doc struct {+ APICodeVersion int `json:"api_code_version"`+ Endpoints struct {+ APIPublic string `json:"api_public"`+ } `json:"endpoints"`+ Features struct {+ VoiceEnabled bool `json:"voice_enabled"`+ } `json:"features"`+ VoiceOrigins *[]string `json:"voice_origins"`+ }+ if err := c.do(ctx, http.MethodGet, u.String(), nil, nil, &doc); err != nil {+ return nil, err+ }+ if doc.Endpoints.APIPublic == "" {+ return nil, fmt.Errorf("voice: %s has no public API endpoint in its discovery document", domain)+ }+ return &Discovery{+ APIPublic: strings.TrimRight(doc.Endpoints.APIPublic, "/"),+ APICodeVersion: doc.APICodeVersion,+ VoiceEnabled: doc.Features.VoiceEnabled,+ VoiceOrigins: deref(doc.VoiceOrigins),+ ListsOrigins: doc.VoiceOrigins != nil,+ }, nil+}++func deref(list *[]string) []string {+ if list == nil {+ return nil+ }+ return *list+}++type PreviewRequest struct {+ PublicIPv4 string `json:"public_ipv4,omitempty"`+ RegionID string `json:"region_id,omitempty"`+ CPUCount int `json:"cpu_count,omitempty"`+ MemoryBytes uint64 `json:"memory_bytes,omitempty"`+ RexctlVersion string `json:"rexctl_version,omitempty"`+}++type Preview struct {+ ServerID string `json:"server_id"`+ ServerIDPinned bool `json:"server_id_pinned"`+ Region struct {+ ID string `json:"id"`+ Name string `json:"name"`+ DistanceKm *float64 `json:"distance_km"`+ New bool `json:"new"`+ Source string `json:"source"`+ } `json:"region"`+ Latitude *float64 `json:"latitude"`+ Longitude *float64 `json:"longitude"`+ Hostname *string `json:"hostname"`+ SuggestedHostname string `json:"suggested_hostname"`+ SoftLimit *int `json:"soft_connection_limit"`+ WebhookURL string `json:"webhook_url"`+ APICodeVersion int `json:"api_code_version"`+ Release string `json:"release"`+ ExpiresAt string `json:"expires_at"`+}++type JoinRequest struct {+ ServerID string `json:"server_id,omitempty"`+ RegionID string `json:"region_id,omitempty"`+ Endpoint string `json:"endpoint"`+ APIKey string `json:"api_key"`+ APISecret string `json:"api_secret"`+ Latitude *float64 `json:"latitude,omitempty"`+ Longitude *float64 `json:"longitude,omitempty"`+ SoftLimit int `json:"soft_connection_limit,omitempty"`+ PublicIPv4 string `json:"public_ipv4,omitempty"`+}++type Joined struct {+ ServerID string `json:"server_id"`+ RegionID string `json:"region_id"`+ Endpoint string `json:"endpoint"`+ WebhookURL string `json:"webhook_url"`+ NodeURL string `json:"node_url"`+}++type Node struct {+ RegionID string `json:"region_id"`+ ServerID string `json:"server_id"`+ Endpoint string `json:"endpoint"`+ Latitude *float64 `json:"latitude"`+ Longitude *float64 `json:"longitude"`+ IsActive bool `json:"is_active"`+ Draining bool `json:"draining"`+ SoftLimit *int `json:"soft_connection_limit"`+ Health struct {+ Status string `json:"status"`+ CheckedAt *string `json:"checked_at"`+ Error *string `json:"error"`+ } `json:"health"`+}++func (c *Client) Preview(ctx context.Context, api, token string, req PreviewRequest) (*Preview, error) {+ var out Preview+ err := c.do(ctx, http.MethodPost, api+"/voice/join/preview", map[string]string{"Authorization": "VoiceJoin " + token}, req, &out)+ return &out, err+}++func (c *Client) Join(ctx context.Context, api, token string, req JoinRequest) (*Joined, error) {+ var out Joined+ err := c.do(ctx, http.MethodPost, api+"/voice/join", map[string]string{"Authorization": "VoiceJoin " + token}, req, &out)+ return &out, err+}++type NodeUpdate struct {+ Endpoint *string `json:"endpoint,omitempty"`+ Draining *bool `json:"draining,omitempty"`+ Latitude *float64 `json:"latitude,omitempty"`+ Longitude *float64 `json:"longitude,omitempty"`+}++func (c *Client) GetNode(ctx context.Context, nodeURL, key, secret string) (*Node, error) {+ var out Node+ err := c.node(ctx, http.MethodGet, nodeURL, key, secret, nil, &out)+ return &out, err+}++func (c *Client) UpdateNode(ctx context.Context, nodeURL, key, secret string, update NodeUpdate) (*Node, error) {+ var out Node+ err := c.node(ctx, http.MethodPatch, nodeURL, key, secret, update, &out)+ return &out, err+}++func (c *Client) DeleteNode(ctx context.Context, nodeURL, key, secret string) error {+ return c.node(ctx, http.MethodDelete, nodeURL, key, secret, nil, nil)+}++func (c *Client) node(ctx context.Context, method, nodeURL, key, secret string, in, out any) error {+ var body []byte+ if in != nil {+ var err error+ if body, err = json.Marshal(in); err != nil {+ return err+ }+ }+ token := NodeToken(key, secret, method, body, time.Now())+ return c.doRaw(ctx, method, nodeURL, map[string]string{"Authorization": "Bearer " + token}, body, out)+}++func NodeToken(key, secret, method string, body []byte, now time.Time) string {+ digest := ""+ if len(body) > 0 {+ sum := sha256.Sum256(body)+ digest = base64.StdEncoding.EncodeToString(sum[:])+ }+ return SignHS256(secret, map[string]any{+ "iss": key,+ "aud": NodeAudience,+ "method": method,+ "path": NodePath,+ "sha256": digest,+ "iat": now.Unix(),+ "exp": now.Add(nodeTokenLife).Unix(),+ })+}++func SignHS256(secret string, claims map[string]any) string {+ enc := base64.RawURLEncoding+ header, _ := json.Marshal(map[string]string{"alg": "HS256", "typ": "JWT"})+ payload, _ := json.Marshal(claims)+ input := enc.EncodeToString(header) + "." + enc.EncodeToString(payload)+ mac := hmac.New(sha256.New, []byte(secret))+ mac.Write([]byte(input))+ return input + "." + enc.EncodeToString(mac.Sum(nil))+}++func (c *Client) do(ctx context.Context, method, target string, headers map[string]string, in, out any) error {+ var body []byte+ if in != nil {+ var err error+ if body, err = json.Marshal(in); err != nil {+ return err+ }+ }+ return c.doRaw(ctx, method, target, headers, body, out)+}++func (c *Client) doRaw(ctx context.Context, method, target string, headers map[string]string, body []byte, out any) error {+ var reader io.Reader+ if body != nil {+ reader = bytes.NewReader(body)+ }+ req, err := http.NewRequestWithContext(ctx, method, target, reader)+ if err != nil {+ return err+ }+ if body != nil {+ req.Header.Set("Content-Type", "application/json")+ }+ req.Header.Set("Accept", "application/json")+ for k, v := range headers {+ req.Header.Set(k, v)+ }+ resp, err := c.http().Do(req)+ if err != nil {+ return err+ }+ defer resp.Body.Close()+ data, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))+ if err != nil {+ return err+ }+ if resp.StatusCode >= 400 {+ e := &APIError{Status: resp.StatusCode}+ if d, err := http.ParseTime(resp.Header.Get("Date")); err == nil {+ e.Date = d+ }+ var parsed struct {+ Code string `json:"code"`+ Message string `json:"message"`+ Detail string `json:"detail"`+ }+ if json.Unmarshal(data, &parsed) == nil {+ e.Code, e.Message, e.Detail = parsed.Code, parsed.Message, parsed.Detail+ }+ if e.Message == "" {+ e.Message = strings.TrimSpace(string(data))+ }+ return e+ }+ if out == nil || len(bytes.TrimSpace(data)) == 0 {+ return nil+ }+ return json.Unmarshal(data, out)+}++func (c *Client) TestWebhook(ctx context.Context, target, key, secret string, now time.Time) (int, time.Time, error) {+ body, err := json.Marshal(map[string]any{+ "event": "room_started",+ "id": "EV_" + strings.TrimPrefix(TestRoom(), TestRoomPrefix),+ "createdAt": strconv.FormatInt(now.Unix(), 10),+ "room": map[string]any{"name": TestRoom()},+ })+ if err != nil {+ return 0, time.Time{}, err+ }+ sum := sha256.Sum256(body)+ token := SignHS256(secret, map[string]any{+ "iss": key,+ "nbf": now.Add(-10 * time.Second).Unix(),+ "exp": now.Add(time.Minute).Unix(),+ "sha256": base64.StdEncoding.EncodeToString(sum[:]),+ })+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, target, bytes.NewReader(body))+ if err != nil {+ return 0, time.Time{}, err+ }+ req.Header.Set("Content-Type", "application/webhook+json")+ req.Header.Set("Authorization", token)+ resp, err := c.HTTP.Do(req)+ if err != nil {+ return 0, time.Time{}, err+ }+ resp.Body.Close()+ date, _ := http.ParseTime(resp.Header.Get("Date"))+ return resp.StatusCode, date, nil+}++func (c *Client) AllowsOrigin(ctx context.Context, domain, origin string) (bool, error) {+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+domain+"/", nil)+ if err != nil {+ return false, err+ }+ resp, err := c.HTTP.Do(req)+ if err != nil {+ return false, err+ }+ resp.Body.Close()+ return ConnectSrcAllows(resp.Header.Values("Content-Security-Policy"), origin), nil+}++func ConnectSrcAllows(policies []string, origin string) bool {+ for _, policy := range policies {+ for _, directive := range strings.Split(policy, ";") {+ fields := strings.Fields(directive)+ if len(fields) == 0 || fields[0] != "connect-src" {+ continue+ }+ for _, source := range fields[1:] {+ if strings.TrimRight(source, "/") == origin {+ return true+ }+ }+ }+ }+ return false+}diff --git a/fluxer_rexctl/internal/voice/transport.go b/fluxer_rexctl/internal/voice/transport.gonew file mode 100644index 000000000..793beb402--- /dev/null+++ b/fluxer_rexctl/internal/voice/transport.go@@ -0,0 +1,296 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package voice++import (+ "context"+ "crypto/rand"+ "crypto/tls"+ "encoding/hex"+ "encoding/json"+ "errors"+ "fmt"+ "net/http"+ "net/url"+ "strings"+ "sync"+ "time"++ "github.com/pion/webrtc/v4"+ "golang.org/x/net/websocket"+)++type Transport string++const (+ TransportUDP Transport = "udp"+ TransportTCP Transport = "tcp"+)++var (+ ErrTransportTimeout = errors.New("voice: no media path within the time limit")+ ErrTokenRejected = errors.New("voice: the server rejected the access token")+)++const TestRoomPrefix = "rexctl-test-"++func TestRoom() string {+ b := make([]byte, 6)+ _, _ = rand.Read(b)+ return TestRoomPrefix + hex.EncodeToString(b)+}++func TestToken(key, secret, room string, now time.Time) string {+ b := make([]byte, 4)+ _, _ = rand.Read(b)+ return SignHS256(secret, map[string]any{+ "iss": key,+ "sub": "rexctl-test-" + hex.EncodeToString(b),+ "nbf": now.Add(-10 * time.Second).Unix(),+ "exp": now.Add(time.Minute).Unix(),+ "video": map[string]any{+ "room": room,+ "roomJoin": true,+ "hidden": true,+ "canPublish": true,+ "canSubscribe": true,+ "canPublishData": true,+ "canPublishSources": []string{"microphone"},+ },+ })+}++type signalMessage struct {+ Offer *webrtc.SessionDescription `json:"offer,omitempty"`+ Answer *webrtc.SessionDescription `json:"answer,omitempty"`+ Trickle *struct {+ CandidateInit string `json:"candidateInit"`+ Target any `json:"target"`+ } `json:"trickle,omitempty"`+ Leave *struct{} `json:"leave,omitempty"`+}++type sdpJSON struct {+ Type string `json:"type"`+ SDP string `json:"sdp"`+}++func rtcURL(endpoint, token string) (string, string, error) {+ u, err := url.Parse(endpoint)+ if err != nil {+ return "", "", err+ }+ u.Path = strings.TrimRight(u.Path, "/") + "/rtc"+ q := url.Values{}+ q.Set("access_token", token)+ q.Set("auto_subscribe", "0")+ q.Set("protocol", "9")+ q.Set("sdk", "go")+ q.Set("version", "1.0.0")+ u.RawQuery = q.Encode()+ origin := "https://" + u.Host+ if u.Scheme == "ws" {+ origin = "http://" + u.Host+ }+ return u.String(), origin, nil+}++func TestTransport(ctx context.Context, endpoint, token string, transport Transport, insecure bool, timeout time.Duration) error {+ ctx, cancel := context.WithTimeout(ctx, timeout)+ defer cancel()+ target, origin, err := rtcURL(endpoint, token)+ if err != nil {+ return err+ }+ cfg, err := websocket.NewConfig(target, origin)+ if err != nil {+ return err+ }+ cfg.TlsConfig = &tls.Config{InsecureSkipVerify: insecure}+ ws, err := cfg.DialContext(ctx)+ if err != nil {+ return dialFailed(ctx, target, insecure, err)+ }+ defer ws.Close()++ se := webrtc.SettingEngine{}+ if transport == TransportTCP {+ se.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeTCP4})+ } else {+ se.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})+ }+ se.SetICEMulticastDNSMode(0)+ api := webrtc.NewAPI(webrtc.WithSettingEngine(se))+ pc, err := api.NewPeerConnection(webrtc.Configuration{})+ if err != nil {+ return err+ }+ defer pc.Close()++ var sendMu sync.Mutex+ send := func(v any) error {+ data, err := json.Marshal(v)+ if err != nil {+ return err+ }+ sendMu.Lock()+ defer sendMu.Unlock()+ return websocket.Message.Send(ws, string(data))+ }++ connected := make(chan struct{})+ failed := make(chan struct{})+ var once sync.Once+ pc.OnICEConnectionStateChange(func(s webrtc.ICEConnectionState) {+ switch s {+ case webrtc.ICEConnectionStateConnected, webrtc.ICEConnectionStateCompleted:+ once.Do(func() { close(connected) })+ case webrtc.ICEConnectionStateFailed:+ once.Do(func() { close(failed) })+ }+ })+ pc.OnICECandidate(func(c *webrtc.ICECandidate) {+ if c == nil {+ return+ }+ init, err := json.Marshal(c.ToJSON())+ if err != nil {+ return+ }+ _ = send(map[string]any{"trickle": map[string]any{"candidateInit": string(init), "target": "PUBLISHER"}})+ })+ if _, err := pc.CreateDataChannel("_reliable", nil); err != nil {+ return err+ }++ joined := make(chan struct{})+ answers := make(chan webrtc.SessionDescription, 1)+ candidates := make(chan webrtc.ICECandidateInit, 32)+ readErr := make(chan error, 1)+ go func() {+ first := true+ for {+ var raw []byte+ if err := websocket.Message.Receive(ws, &raw); err != nil {+ readErr <- err+ return+ }+ if first {+ first = false+ close(joined)+ continue+ }+ var msg signalMessage+ if json.Unmarshal(raw, &msg) != nil {+ continue+ }+ switch {+ case msg.Answer != nil:+ answers <- *msg.Answer+ case msg.Trickle != nil && publisherTarget(msg.Trickle.Target):+ var init webrtc.ICECandidateInit+ if json.Unmarshal([]byte(msg.Trickle.CandidateInit), &init) == nil {+ candidates <- init+ }+ case msg.Leave != nil:+ readErr <- errors.New("voice: the server ended the session")+ return+ }+ }+ }()++ select {+ case <-joined:+ case err := <-readErr:+ return fmt.Errorf("voice: signalling closed before joining: %w", err)+ case <-ctx.Done():+ return fmt.Errorf("voice: no join response: %w", ctx.Err())+ }+ offer, err := pc.CreateOffer(nil)+ if err != nil {+ return err+ }+ if err := pc.SetLocalDescription(offer); err != nil {+ return err+ }+ if err := send(map[string]any{"offer": sdpJSON{Type: "offer", SDP: offer.SDP}}); err != nil {+ return err+ }+ defer func() { _ = send(map[string]any{"leave": map[string]any{}}) }()++ var pending []webrtc.ICECandidateInit+ haveAnswer := false+ for {+ select {+ case answer := <-answers:+ if err := pc.SetRemoteDescription(answer); err != nil {+ return err+ }+ haveAnswer = true+ for _, c := range pending {+ _ = pc.AddICECandidate(c)+ }+ pending = nil+ case c := <-candidates:+ if !haveAnswer {+ pending = append(pending, c)+ continue+ }+ _ = pc.AddICECandidate(c)+ case <-connected:+ return nil+ case <-failed:+ return noMediaPath(transport)+ case err := <-readErr:+ return signallingClosed(transport, haveAnswer, err)+ case <-ctx.Done():+ return ErrTransportTimeout+ }+ }+}++func dialFailed(ctx context.Context, target string, insecure bool, err error) error {+ var de *websocket.DialError+ if errors.As(err, &de) {+ err = de.Err+ }+ u, perr := url.Parse(target)+ if perr == nil {+ u.Scheme = strings.Replace(u.Scheme, "ws", "http", 1)+ u.Path += "/validate"+ client := &http.Client{Timeout: 10 * time.Second, Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}}}+ req, rerr := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)+ if rerr == nil {+ if resp, derr := client.Do(req); derr == nil {+ resp.Body.Close()+ if resp.StatusCode == http.StatusUnauthorized {+ return ErrTokenRejected+ }+ }+ }+ }+ return fmt.Errorf("voice: signalling connection failed: %w", err)+}++func noMediaPath(transport Transport) error {+ return fmt.Errorf("voice: no %s media path to the server", strings.ToUpper(string(transport)))+}++func signallingClosed(transport Transport, answered bool, err error) error {+ if answered {+ return noMediaPath(transport)+ }+ return fmt.Errorf("voice: signalling closed: %w", err)+}++func publisherTarget(v any) bool {+ switch t := v.(type) {+ case nil:+ return true+ case string:+ return t == "PUBLISHER"+ case float64:+ return t == 0+ }+ return false+}diff --git a/fluxer_rexctl/internal/voice/voice.go b/fluxer_rexctl/internal/voice/voice.gonew file mode 100644index 000000000..3b319d53b--- /dev/null+++ b/fluxer_rexctl/internal/voice/voice.go@@ -0,0 +1,266 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package voice++import (+ "crypto/rand"+ "encoding/hex"+ "fmt"+ "net/url"+ "path/filepath"+ "regexp"+ "slices"+ "strconv"+ "strings"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+)++const (+ ServersDir = "voice"+ EdgeDir = "edge"+ SitesDir = "sites.d"+ ComposeFile = "voice.compose.yml"+ HTTPComposeFile = "voice-http.compose.yml"+ EdgeComposeFile = "voice-edge.compose.yml"+ EdgeCaddyfile = "Caddyfile.voice"+ EnvFile = ".env"+ MachineNetwork = "fluxer-voice"+ MachineProject = "fluxer-voice"+ EdgeProject = "fluxer-voice-edge"+ EdgeID = "edge"+ KeyPrefix = "fluxer-"+ PathPrefix = "/livekit-"+ DropInPrefix = "voice-"+ ContainerPort = 7880+ FirstTCPPort = 7881+ FirstUDPPort = 7882+ DefaultImage = "livekit/livekit-server:v1.12.0"+ DefaultMemory = "2g"+ DefaultGrace = "15m"+ ConnsPerCPU = 75+)++const (+ EdgeOwn = "own"+ EdgeInstance = "instance"+ EdgeNone = "none"+)++var idPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,62}$`)++func ValidID(id string) bool { return idPattern.MatchString(id) }++func JoinedProject(id string) string { return MachineProject + "-" + id }++func ProjectName(instanceProject, id string) string {+ return instanceProject + "-voice-" + id+}++func ServerDir(dir, id string) string { return filepath.Join(dir, ServersDir, id) }++func Alias(id string) string { return "voice-" + id }++func Key(id string) string { return KeyPrefix + id }++func Path(id string) string { return PathPrefix + id }++func InternalURL(id string) string {+ return fmt.Sprintf("http://%s:%d", Alias(id), ContainerPort)+}++func NewSecret() (string, error) {+ b := make([]byte, 32)+ if _, err := rand.Read(b); err != nil {+ return "", err+ }+ return hex.EncodeToString(b), nil+}++func NextID(region string, taken []string) string {+ for n := 1; ; n++ {+ id := fmt.Sprintf("%s-server-%d", region, n)+ if !slices.Contains(taken, id) {+ return id+ }+ }+}++type Env struct {+ ID string+ Key string+ Secret string+ WebhookURL string+ Network string+ TCPPort int+ UDPPort int+ HTTPPort int+ NodeIP string+ BindIP string+ Image string+ Memory string+ StopGrace string+ StunPrimary string+ StunSecondary string+ LogLevel string+}++func (e Env) Values() map[string]string {+ useExternal := "true"+ if e.NodeIP != "" {+ useExternal = "false"+ }+ out := map[string]string{+ "FLUXER_VOICE_ID": e.ID,+ "FLUXER_VOICE_API_KEY": e.Key,+ "FLUXER_VOICE_API_SECRET": e.Secret,+ "FLUXER_VOICE_WEBHOOK_URL": e.WebhookURL,+ "FLUXER_VOICE_NETWORK": e.Network,+ "FLUXER_VOICE_TCP_PORT": strconv.Itoa(e.TCPPort),+ "FLUXER_VOICE_UDP_PORT": strconv.Itoa(e.UDPPort),+ "FLUXER_VOICE_NODE_IP": e.NodeIP,+ "FLUXER_VOICE_USE_EXTERNAL_IP": useExternal,+ "FLUXER_VOICE_BIND_IP": e.BindIP,+ "FLUXER_LIVEKIT_IMAGE": orDefault(e.Image, DefaultImage),+ "FLUXER_VOICE_MEMORY_LIMIT": orDefault(e.Memory, DefaultMemory),+ "FLUXER_VOICE_GOMEMLIMIT": GoMemLimit(orDefault(e.Memory, DefaultMemory)),+ "FLUXER_VOICE_STOP_GRACE": orDefault(e.StopGrace, DefaultGrace),+ }+ if e.HTTPPort > 0 {+ out["FLUXER_VOICE_HTTP_PORT"] = strconv.Itoa(e.HTTPPort)+ }+ for k, v := range map[string]string{+ "FLUXER_LIVEKIT_STUN_PRIMARY": e.StunPrimary,+ "FLUXER_LIVEKIT_STUN_SECONDARY": e.StunSecondary,+ "FLUXER_LIVEKIT_LOG_LEVEL": e.LogLevel,+ } {+ if v != "" {+ out[k] = v+ }+ }+ return out+}++func (e Env) File() []byte {+ values := e.Values()+ keys := make([]string, 0, len(values))+ for k := range values {+ keys = append(keys, k)+ }+ slices.Sort(keys)+ var b strings.Builder+ for _, k := range keys {+ fmt.Fprintf(&b, "%s=%s\n", k, values[k])+ }+ return []byte(b.String())+}++func orDefault(v, fallback string) string {+ if strings.TrimSpace(v) == "" {+ return fallback+ }+ return v+}++func ParseBytes(v string) (uint64, bool) {+ v = strings.ToLower(strings.TrimSpace(v))+ units := []struct {+ suffix string+ mult uint64+ }{{"gib", 1 << 30}, {"mib", 1 << 20}, {"kib", 1 << 10}, {"gb", 1 << 30}, {"mb", 1 << 20}, {"kb", 1 << 10}, {"g", 1 << 30}, {"m", 1 << 20}, {"k", 1 << 10}, {"b", 1}}+ for _, u := range units {+ if n, ok := strings.CutSuffix(v, u.suffix); ok {+ f, err := strconv.ParseFloat(strings.TrimSpace(n), 64)+ if err != nil || f <= 0 {+ return 0, false+ }+ return uint64(f * float64(u.mult)), true+ }+ }+ n, err := strconv.ParseUint(v, 10, 64)+ return n, err == nil && n > 0+}++func GoMemLimit(memory string) string {+ n, ok := ParseBytes(memory)+ if !ok {+ return "1700MiB"+ }+ return strconv.FormatUint(n*85/100>>20, 10) + "MiB"+}++func MachineMemory(hostBytes uint64) string {+ half := hostBytes / 2+ half = max(half, 512<<20)+ half = min(half, 8<<30)+ return strconv.FormatUint(half>>20, 10) + "m"+}++func SoftLimit(cpus int) int {+ return max(1, cpus) * ConnsPerCPU+}++func HumanBytes(n uint64) string {+ if n >= 1<<30 {+ return strings.TrimSuffix(strconv.FormatFloat(float64(n)/(1<<30), 'f', 1, 64), ".0") + " GB"+ }+ return strconv.FormatUint(n>>20, 10) + " MB"+}++func PickPorts(taken map[host.Port]bool, free func(number int, proto string) bool) (int, int) {+ usable := func(n int, proto string) bool {+ return n >= 1 && n <= 65535 && !taken[host.Port{Number: n, Proto: proto}] && (free == nil || free(n, proto))+ }+ for tcp, udp := FirstTCPPort, FirstUDPPort; udp <= 65535; tcp, udp = tcp+2, udp+2 {+ if usable(tcp, "tcp") && usable(udp, "udp") {+ return tcp, udp+ }+ }+ return 0, 0+}++func DropIn(id string) []byte {+ return fmt.Appendf(nil, "handle_path %s/* {\n\timport upstream %s:%d\n}\n", Path(id), Alias(id), ContainerPort)+}++func DropInName(id string) string { return DropInPrefix + id + ".caddy" }++func SiteFileName(address string) string {+ return strings.NewReplacer(":", "_", "/", "_").Replace(address) + ".caddy"+}++func SiteFile(address string, ids []string, ipCertificate bool) []byte {+ var b strings.Builder+ fmt.Fprintf(&b, "%s {\n", address)+ if ipCertificate {+ b.WriteString("\ttls {\n\t\tissuer acme {\n\t\t\tprofile shortlived\n\t\t}\n\t}\n\n")+ }+ b.WriteString("\thandle /_health {\n\t\trespond \"OK\" 200\n\t}\n")+ sorted := slices.Clone(ids)+ slices.Sort(sorted)+ for _, id := range sorted {+ fmt.Fprintf(&b, "\n\thandle_path %s/* {\n\t\timport livekit %s:%d\n\t}\n", Path(id), Alias(id), ContainerPort)+ }+ b.WriteString("\n\thandle {\n\t\trespond 404\n\t}\n}\n")+ return []byte(b.String())+}++func WSOrigin(origin string) string {+ u, err := url.Parse(origin)+ if err != nil || u.Host == "" {+ return ""+ }+ if u.Scheme == "http" {+ return "ws://" + u.Host+ }+ return "wss://" + u.Host+}++func Endpoint(address, id string) string {+ return "wss://" + address + Path(id)+}++func ShippedFile(set *stack.Set, name string) ([]byte, error) {+ return stack.ReadFile(set, ServersDir+"/"+name)+}diff --git a/fluxer_rexctl/internal/voice/voice_test.go b/fluxer_rexctl/internal/voice/voice_test.gonew file mode 100644index 000000000..67753e529--- /dev/null+++ b/fluxer_rexctl/internal/voice/voice_test.go@@ -0,0 +1,440 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++package voice++import (+ "context"+ "crypto/hmac"+ "crypto/sha256"+ "encoding/base64"+ "encoding/json"+ "errors"+ "io"+ "net"+ "net/http"+ "net/http/httptest"+ "slices"+ "strings"+ "testing"+ "time"++ "github.com/compose-spec/compose-go/v2/template"+ "go.yaml.in/yaml/v4"++ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/host"+ "github.com/fluxerapp/fluxer/fluxer_rexctl/internal/stack"+)++func TestPickPortsSkipsEveryTakenPort(t *testing.T) {+ taken := map[host.Port]bool{+ {Number: 7881, Proto: "tcp"}: true,+ {Number: 7884, Proto: "udp"}: true,+ }+ busy := func(n int, proto string) bool { return !(n == 7885 && proto == "tcp") }+ tcp, udp := PickPorts(taken, busy)+ if tcp != 7887 || udp != 7888 {+ t.Fatalf("got %d/%d, want 7887/7888", tcp, udp)+ }+}++func TestNextIDIsUniqueAcrossRegions(t *testing.T) {+ if got := NextID("fra", []string{"fra-server-1", "default-server-2", "fra-server-3"}); got != "fra-server-2" {+ t.Fatalf("got %s", got)+ }+}++func TestEnvSetsExternalIPOnlyWithoutNodeIP(t *testing.T) {+ e := Env{ID: "fra-server-1", Key: "fluxer-fra-server-1", Secret: "s", WebhookURL: "http://api:8080/webhooks/livekit", Network: "fluxer_fluxer", TCPPort: 7883, UDPPort: 7884, Memory: "2g"}+ v := e.Values()+ if v["FLUXER_VOICE_USE_EXTERNAL_IP"] != "true" || v["FLUXER_VOICE_GOMEMLIMIT"] != "1740MiB" || v["FLUXER_LIVEKIT_IMAGE"] != DefaultImage {+ t.Fatalf("unexpected values %v", v)+ }+ e.NodeIP = "198.51.100.8"+ if e.Values()["FLUXER_VOICE_USE_EXTERNAL_IP"] != "false" {+ t.Fatal("node IP must turn off external IP discovery")+ }+ if _, ok := e.Values()["FLUXER_VOICE_HTTP_PORT"]; ok {+ t.Fatal("no HTTP port without --no-edge")+ }+ if !strings.Contains(string(e.File()), "FLUXER_VOICE_API_SECRET=s\n") {+ t.Fatal("env file misses the secret")+ }+}++func TestShippedComposeRendersWithTheEnv(t *testing.T) {+ data, err := ShippedFile(stack.Embedded(), ComposeFile)+ if err != nil {+ t.Fatal(err)+ }+ e := Env{ID: "fra-server-1", Key: "k", Secret: "s", WebhookURL: "http://api:8080/webhooks/livekit", Network: "fluxer_fluxer", TCPPort: 7883, UDPPort: 7884, NodeIP: "198.51.100.8", BindIP: "198.51.100.8"}+ values := e.Values()+ out, err := template.Substitute(string(data), func(k string) (string, bool) {+ v, ok := values[k]+ return v, ok+ })+ if err != nil {+ t.Fatal(err)+ }+ var doc struct {+ Services map[string]struct {+ Ports []string `yaml:"ports"`+ Networks map[string]struct {+ Aliases []string `yaml:"aliases"`+ } `yaml:"networks"`+ Environment map[string]string `yaml:"environment"`+ } `yaml:"services"`+ Networks map[string]struct {+ Name string `yaml:"name"`+ } `yaml:"networks"`+ }+ if err := yaml.Unmarshal([]byte(out), &doc); err != nil {+ t.Fatal(err)+ }+ svc := doc.Services["livekit"]+ if !slices.Equal(svc.Ports, []string{"198.51.100.8:7883:7883/tcp", "198.51.100.8:7884:7884/udp"}) {+ t.Fatalf("ports %v", svc.Ports)+ }+ if !slices.Equal(svc.Networks["voice"].Aliases, []string{"voice-fra-server-1"}) || doc.Networks["voice"].Name != "fluxer_fluxer" {+ t.Fatalf("network %+v %+v", svc.Networks, doc.Networks)+ }+ for _, want := range []string{"node_ip: \"198.51.100.8\"", "use_external_ip: false", "tcp_port: 7883", "- http://api:8080/webhooks/livekit"} {+ if !strings.Contains(svc.Environment["LIVEKIT_CONFIG"], want) {+ t.Fatalf("LIVEKIT_CONFIG misses %q:\n%s", want, svc.Environment["LIVEKIT_CONFIG"])+ }+ }+ if svc.Environment["LIVEKIT_KEYS"] != "k: s" {+ t.Fatalf("keys %q", svc.Environment["LIVEKIT_KEYS"])+ }+}++func TestEdgeFiles(t *testing.T) {+ if got := string(DropIn("fra-server-2")); got != "handle_path /livekit-fra-server-2/* {\n\timport upstream voice-fra-server-2:7880\n}\n" {+ t.Fatalf("drop-in %q", got)+ }+ site := string(SiteFile("203.0.113.7", []string{"b", "a"}, true))+ for _, want := range []string{"203.0.113.7 {", "profile shortlived", "handle_path /livekit-a/*", "import livekit voice-b:7880", "respond 404"} {+ if !strings.Contains(site, want) {+ t.Fatalf("site misses %q:\n%s", want, site)+ }+ }+ if strings.Index(site, "/livekit-a/") > strings.Index(site, "/livekit-b/") {+ t.Fatal("routes are not sorted")+ }+ if strings.Contains(string(SiteFile("voice.example.com", []string{"a"}, false)), "issuer") {+ t.Fatal("a hostname site must use Caddy's default issuers")+ }+ if Endpoint("203.0.113.7", "a") != "wss://203.0.113.7/livekit-a" {+ t.Fatal("endpoint")+ }+}++func TestSizing(t *testing.T) {+ if MachineMemory(1<<30) != "512m" || MachineMemory(4<<30) != "2048m" || MachineMemory(64<<30) != "8192m" {+ t.Fatal("machine memory must be half the host, between 512 MB and 8 GB")+ }+ if SoftLimit(4) != 300 || SoftLimit(0) != 75 {+ t.Fatal("soft limit")+ }+ if n, ok := ParseBytes("1536m"); !ok || n != 1536<<20 {+ t.Fatal("parse")+ }+}++func TestCheckHostname(t *testing.T) {+ cases := []struct {+ answer DNSAnswer+ want HostnameCheck+ }{+ {DNSAnswer{}, HostnameMissing},+ {DNSAnswer{A: []string{"203.0.113.7"}}, HostnameOK},+ {DNSAnswer{A: []string{"203.0.113.9"}}, HostnameElsewhere},+ {DNSAnswer{A: []string{"104.16.1.1", "172.67.1.1"}}, HostnameProxied},+ {DNSAnswer{A: []string{"203.0.113.7"}, AAAA: []string{"2001:db8::9"}}, HostnameIPv6Elsewhere},+ {DNSAnswer{A: []string{"203.0.113.7"}, AAAA: []string{"2001:db8::7"}}, HostnameOK},+ }+ for _, c := range cases {+ if got := CheckHostname(c.answer, "203.0.113.7", []string{"2001:db8::7"}); got != c.want {+ t.Errorf("%+v: got %d, want %d", c.answer, got, c.want)+ }+ }+}++func TestReachability(t *testing.T) {+ if r := Assess("203.0.113.7", []string{"10.0.0.4"}, "10.0.0.4"); !r.BehindNAT || r.CGNAT {+ t.Fatalf("%+v", r)+ }+ if r := Assess("203.0.113.7", []string{"100.64.3.2"}, "100.64.3.2"); !r.CGNAT {+ t.Fatalf("%+v", r)+ }+ if r := Assess("203.0.113.7", []string{"203.0.113.7", "100.100.1.1"}, "203.0.113.7"); r.BehindNAT || r.CGNAT {+ t.Fatalf("tailscale on another interface is fine: %+v", r)+ }+}++func TestValidToken(t *testing.T) {+ good := "fvj_123_" + strings.Repeat("a", 43)+ if !ValidToken(good) || ValidToken("fvj_x_"+strings.Repeat("a", 43)) || ValidToken("flx_123") {+ t.Fatal("token format")+ }+}++func verifyNodeToken(t *testing.T, header, secret, method string, body []byte) map[string]any {+ t.Helper()+ token := strings.TrimPrefix(header, "Bearer ")+ parts := strings.Split(token, ".")+ mac := hmac.New(sha256.New, []byte(secret))+ mac.Write([]byte(parts[0] + "." + parts[1]))+ if base64.RawURLEncoding.EncodeToString(mac.Sum(nil)) != parts[2] {+ t.Fatal("bad signature")+ }+ payload, _ := base64.RawURLEncoding.DecodeString(parts[1])+ var claims map[string]any+ _ = json.Unmarshal(payload, &claims)+ digest := ""+ if len(body) > 0 {+ sum := sha256.Sum256(body)+ digest = base64.StdEncoding.EncodeToString(sum[:])+ }+ if claims["aud"] != NodeAudience || claims["method"] != method || claims["path"] != NodePath || claims["sha256"] != digest {+ t.Fatalf("claims %v", claims)+ }+ return claims+}++func TestNodeClientSignsEveryRequest(t *testing.T) {+ var seen []string+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ body, _ := io.ReadAll(r.Body)+ claims := verifyNodeToken(t, r.Header.Get("Authorization"), "secret", r.Method, body)+ if claims["iss"] != "fluxer-a" {+ t.Fatalf("iss %v", claims["iss"])+ }+ seen = append(seen, r.Method+" "+string(body))+ if r.Method == http.MethodDelete {+ w.WriteHeader(http.StatusNoContent)+ return+ }+ _, _ = w.Write([]byte(`{"server_id":"a","region_id":"fra","endpoint":"wss://x/livekit-a","is_active":true,"draining":true,"health":{"status":"up"}}`))+ }))+ defer srv.Close()+ c := &Client{HTTP: srv.Client()}+ ctx := context.Background()+ if n, err := c.GetNode(ctx, srv.URL, "fluxer-a", "secret"); err != nil || n.Health.Status != "up" {+ t.Fatal(err)+ }+ draining := true+ if _, err := c.UpdateNode(ctx, srv.URL, "fluxer-a", "secret", NodeUpdate{Draining: &draining}); err != nil {+ t.Fatal(err)+ }+ if err := c.DeleteNode(ctx, srv.URL, "fluxer-a", "secret"); err != nil {+ t.Fatal(err)+ }+ if !slices.Equal(seen, []string{"GET ", `PATCH {"draining":true}`, "DELETE "}) {+ t.Fatalf("requests %v", seen)+ }+}++func TestJoinClientErrors(t *testing.T) {+ for _, code := range []string{CodeTokenInvalid, CodeTokenBusy, CodeEndpointUnreachable, CodeKeyMismatch, CodeIDInUse, CodeKeyInUse} {+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.Header.Get("Authorization") != "VoiceJoin fvj_1_t" {+ t.Fatalf("auth %q", r.Header.Get("Authorization"))+ }+ w.WriteHeader(http.StatusConflict)+ _, _ = w.Write([]byte(`{"code":"` + code + `","message":"no","detail":"HTTP 502"}`))+ }))+ _, err := (&Client{HTTP: srv.Client()}).Join(context.Background(), srv.URL, "fvj_1_t", JoinRequest{})+ srv.Close()+ if CodeOf(err) != code || !strings.Contains(err.Error(), "HTTP 502") {+ t.Fatalf("%s: %v", code, err)+ }+ }+}++func TestDiscover(t *testing.T) {+ srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if r.URL.Path != "/.well-known/fluxer" {+ t.Fatalf("path %s", r.URL.Path)+ }+ _, _ = w.Write([]byte(`{"api_code_version":7,"endpoints":{"api_public":"https://chat.example.com/api/"},"features":{"voice_enabled":true},"voice_origins":["wss://v.example.com"]}`))+ }))+ defer srv.Close()+ d, err := (&Client{HTTP: srv.Client()}).Discover(context.Background(), srv.URL)+ if err != nil || d.APIPublic != "https://chat.example.com/api" || !d.VoiceEnabled || d.VoiceOrigins[0] != "wss://v.example.com" {+ t.Fatalf("%+v %v", d, err)+ }+}++func TestTestTokenIsHiddenAndShort(t *testing.T) {+ tok := TestToken("k", "s", "rexctl-test-abc", time.Unix(1000, 0))+ payload, _ := base64.RawURLEncoding.DecodeString(strings.Split(tok, ".")[1])+ var claims struct {+ Exp int64 `json:"exp"`+ Video map[string]any `json:"video"`+ }+ _ = json.Unmarshal(payload, &claims)+ if claims.Exp != 1060 || claims.Video["hidden"] != true || claims.Video["room"] != "rexctl-test-abc" {+ t.Fatalf("%+v", claims)+ }+}++func TestClosedSignallingAfterTheAnswerMeansNoMediaPath(t *testing.T) {+ eof := io.EOF+ if got := signallingClosed(TransportUDP, true, eof).Error(); got != "voice: no UDP media path to the server" {+ t.Errorf("after the answer: %q", got)+ }+ if got := signallingClosed(TransportTCP, false, eof).Error(); !strings.Contains(got, "signalling closed") {+ t.Errorf("before the answer: %q", got)+ }+}++func TestRejectedTokenIsReportedAndNotPrinted(t *testing.T) {+ srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "invalid token", http.StatusUnauthorized)+ }))+ defer srv.Close()+ endpoint := "wss://" + strings.TrimPrefix(srv.URL, "https://") + "/livekit-x"+ err := TestTransport(context.Background(), endpoint, "secret-token-value", TransportUDP, true, 5*time.Second)+ if !errors.Is(err, ErrTokenRejected) {+ t.Fatalf("got %v", err)+ }+ if strings.Contains(err.Error(), "secret-token-value") {+ t.Errorf("the error shows the token: %v", err)+ }+}++func TestFailedDialHidesTheToken(t *testing.T) {+ srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Error(w, "gone", http.StatusBadGateway)+ }))+ defer srv.Close()+ endpoint := "wss://" + strings.TrimPrefix(srv.URL, "https://") + "/livekit-x"+ err := TestTransport(context.Background(), endpoint, "secret-token-value", TransportTCP, true, 5*time.Second)+ if err == nil || errors.Is(err, ErrTokenRejected) || strings.Contains(err.Error(), "secret-token-value") {+ t.Errorf("got %v", err)+ }+}++func TestConnectSrcAllows(t *testing.T) {+ csp := []string{"default-src 'self'; connect-src 'self' wss://voice.example.com https://voice.example.com; img-src *"}+ if !ConnectSrcAllows(csp, "wss://voice.example.com") {+ t.Error("listed origin refused")+ }+ if ConnectSrcAllows(csp, "wss://other.example.com") || ConnectSrcAllows([]string{"img-src wss://voice.example.com"}, "wss://voice.example.com") {+ t.Error("unlisted origin allowed")+ }+}++func TestLocalProbesSpeakPlainHTTPWithoutThePublicPath(t *testing.T) {+ var paths []string+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ paths = append(paths, r.URL.Path)+ switch r.URL.Path {+ case "/":+ _, _ = io.WriteString(w, "OK")+ case "/twirp/livekit.RoomService/ListRooms":+ _, _ = io.WriteString(w, `{"rooms":[{"name":"guild_1_channel_2","num_participants":3},{"name":"`+TestRoomPrefix+`x","num_participants":1}]}`)+ default:+ http.NotFound(w, r)+ }+ }))+ defer srv.Close()+ port := srv.Listener.Addr().(*net.TCPAddr).Port+ base := LocalBase(port)+ if err := ProbeThroughEdge(context.Background(), base, "", true); err != nil {+ t.Fatalf("probe: %v", err)+ }+ load, err := ListRooms(context.Background(), base, "", "key", "secret", true)+ if err != nil {+ t.Fatalf("list rooms: %v", err)+ }+ if load != (RoomLoad{Rooms: 1, Participants: 3}) {+ t.Fatalf("load = %+v", load)+ }+ if !slices.Equal(paths, []string{"/", "/twirp/livekit.RoomService/ListRooms"}) {+ t.Fatalf("paths = %v", paths)+ }+}++func TestHTTPBaseKeepsThePathOfTheEndpoint(t *testing.T) {+ for in, want := range map[string]string{+ "wss://chat.example.com/livekit-fra-server-1/": "https://chat.example.com/livekit-fra-server-1",+ "ws://127.0.0.1:8080/livekit-x": "http://127.0.0.1:8080/livekit-x",+ } {+ if got := HTTPBase(in); got != want {+ t.Fatalf("HTTPBase(%q) = %q, want %q", in, got, want)+ }+ }+}++func TestInstanceEdgeFollowsTheEdgeMode(t *testing.T) {+ const endpoint = "wss://chat.example.com/livekit-fra-server-1"+ cases := []struct {+ mode, port, bind, base, addr string+ }{+ {"", "", "", "https://chat.example.com/livekit-fra-server-1", "127.0.0.1:443"},+ {"", "8443", "", "https://chat.example.com/livekit-fra-server-1", "127.0.0.1:8443"},+ {"", "${FLUXER_HTTPS_PORT}", "", "https://chat.example.com/livekit-fra-server-1", "127.0.0.1:443"},+ {"proxy", "443", "", "http://chat.example.com/livekit-fra-server-1", "127.0.0.1:8080"},+ {"proxy", "443", "0.0.0.0:9000", "http://chat.example.com/livekit-fra-server-1", "127.0.0.1:9000"},+ {"proxy", "443", "[::]:9000", "http://chat.example.com/livekit-fra-server-1", "127.0.0.1:9000"},+ {"proxy", "443", "10.0.0.5:8080", "http://chat.example.com/livekit-fra-server-1", "10.0.0.5:8080"},+ {"tunnel", "443", "", "http://chat.example.com/livekit-fra-server-1", "127.0.0.1:80"},+ {"tunnel", "443", "127.0.0.1:8081", "http://chat.example.com/livekit-fra-server-1", "127.0.0.1:8081"},+ }+ for _, c := range cases {+ base, addr := InstanceEdge(endpoint, c.mode, c.port, c.bind)+ if base != c.base || addr != c.addr {+ t.Errorf("InstanceEdge(%q, %q, %q) = %s, %s, want %s, %s", c.mode, c.port, c.bind, base, addr, c.base, c.addr)+ }+ }+}++func TestProxyModeProbeSpeaksPlainHTTPWithTheDomain(t *testing.T) {+ var hosts, paths []string+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ hosts, paths = append(hosts, r.Host), append(paths, r.URL.Path)+ _, _ = io.WriteString(w, "OK")+ }))+ defer srv.Close()+ base, addr := InstanceEdge("wss://chat.example.com/livekit-fra-server-1", "proxy", "443", srv.Listener.Addr().String())+ if err := ProbeThroughEdge(context.Background(), base, addr, false); err != nil {+ t.Fatalf("probe: %v", err)+ }+ if !slices.Equal(hosts, []string{"chat.example.com"}) || !slices.Equal(paths, []string{"/livekit-fra-server-1/"}) {+ t.Fatalf("hosts = %v, paths = %v", hosts, paths)+ }+}++func TestAClockProblemIsNotAWrongSecret(t *testing.T) {+ now := time.Date(2026, 10, 30, 12, 0, 0, 0, time.UTC)+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Date", now.Add(-3*time.Minute).Format(http.TimeFormat))+ w.WriteHeader(http.StatusUnauthorized)+ _, _ = io.WriteString(w, `{"code":"VOICE_NODE_TOKEN_EXPIRED","message":"expired"}`)+ }))+ defer srv.Close()+ client := &Client{HTTP: srv.Client()}+ _, err := client.GetNode(context.Background(), srv.URL, "fluxer-fra-server-1", "secret")+ if CodeOf(err) != CodeTokenExpired {+ t.Fatalf("code = %q", CodeOf(err))+ }+ if got := ClockProblem(err, "chat.example.com", now); got != "The clock on this machine is about 3 minutes ahead of chat.example.com" {+ t.Fatalf("ahead: %q", got)+ }+ if got := ClockProblem(err, "chat.example.com", now.Add(-6*time.Minute)); got != "The clock on this machine is about 3 minutes behind chat.example.com" {+ t.Fatalf("behind: %q", got)+ }+ if got := ClockProblem(&APIError{Code: CodeSignatureInvalid}, "chat.example.com", now); got != "" {+ t.Fatalf("wrong secret: %q", got)+ }+}++func TestJoinedServersUseTheMachineProjectPrefix(t *testing.T) {+ if got := JoinedProject("fra-server-1"); got != "fluxer-voice-fra-server-1" {+ t.Fatalf("JoinedProject = %s", got)+ }+ if JoinedProject(EdgeID) != EdgeProject {+ t.Fatal("the edge ID no longer matches the edge project")+ }+}diff --git a/packages/config/src/MasterConfig.ts b/packages/config/src/MasterConfig.tsindex dc93cfa75..63f0f9084 100644--- a/packages/config/src/MasterConfig.ts+++ b/packages/config/src/MasterConfig.ts@@ -210,6 +210,7 @@ export interface MasterConfig {api_secret: string;url: string;internal_url: string;+ bundled?: boolean;default_region?: {id: string;name: string;@@ -218,6 +219,8 @@ export interface MasterConfig {longitude: number;};p2p_stun_urls?: Array<string>;+ join_allow_private_endpoints?: boolean;+ health_interval_ms?: number;};search: {engine: 'elasticsearch' | 'meilisearch' | 'none';diff --git a/packages/config/src/config_loader/EnvironmentOverrides.ts b/packages/config/src/config_loader/EnvironmentOverrides.tsindex ae0a1e05e..df9975e74 100644--- a/packages/config/src/config_loader/EnvironmentOverrides.ts+++ b/packages/config/src/config_loader/EnvironmentOverrides.ts@@ -185,8 +185,14 @@ const NAMED_FLUXER_ENV_OVERRIDES: Record<string, NamedEnvOverride> = {FLUXER_LIVEKIT_API_SECRET: {path: ['integrations', 'voice', 'api_secret']},FLUXER_LIVEKIT_URL: {path: ['integrations', 'voice', 'url']},FLUXER_LIVEKIT_INTERNAL_URL: {path: ['integrations', 'voice', 'internal_url']},+ FLUXER_LIVEKIT_BUNDLED: {path: ['integrations', 'voice', 'bundled'], parse: parseBoolean},FLUXER_LIVEKIT_DEFAULT_REGION: {path: ['integrations', 'voice', 'default_region'], parse: parseJsonObject},FLUXER_VOICE_P2P_STUN_URLS: {path: ['integrations', 'voice', 'p2p_stun_urls'], parse: parseCsv},+ FLUXER_VOICE_JOIN_ALLOW_PRIVATE_ENDPOINTS: {+ path: ['integrations', 'voice', 'join_allow_private_endpoints'],+ parse: parseBoolean,+ },+ FLUXER_VOICE_HEALTH_INTERVAL_MS: {path: ['integrations', 'voice', 'health_interval_ms'], parse: parseInteger},FLUXER_SEARCH_ENGINE: {path: ['integrations', 'search', 'engine']},FLUXER_SEARCH_URL: {path: ['integrations', 'search', 'url']},FLUXER_SEARCH_API_KEY: {path: ['integrations', 'search', 'api_key']},diff --git a/packages/constants/src/AdminACLs.ts b/packages/constants/src/AdminACLs.tsindex e490336db..1161765fd 100644--- a/packages/constants/src/AdminACLs.ts+++ b/packages/constants/src/AdminACLs.ts@@ -109,6 +109,9 @@ export const AdminACLs = {USER_UPDATE_PROFILE: 'user:update:profile',USER_UPDATE_TRAITS: 'user:update:traits',USER_UPDATE_USERNAME: 'user:update:username',+ VOICE_JOIN_TOKEN_CREATE: 'voice:join_token:create',+ VOICE_JOIN_TOKEN_LIST: 'voice:join_token:list',+ VOICE_JOIN_TOKEN_REVOKE: 'voice:join_token:revoke',VOICE_REGION_CREATE: 'voice:region:create',VOICE_REGION_DELETE: 'voice:region:delete',VOICE_REGION_LIST: 'voice:region:list',diff --git a/packages/constants/src/ApiErrorCodes.ts b/packages/constants/src/ApiErrorCodes.tsindex 6553c7d1b..a91b59836 100644--- a/packages/constants/src/ApiErrorCodes.ts+++ b/packages/constants/src/ApiErrorCodes.ts@@ -344,6 +344,16 @@ export const APIErrorCodes = {USER_OWNS_GUILDS: 'USER_OWNS_GUILDS',VALIDATION_ERROR: 'VALIDATION_ERROR',VOICE_CHANNEL_FULL: 'VOICE_CHANNEL_FULL',+ VOICE_JOIN_ENDPOINT_INVALID: 'VOICE_JOIN_ENDPOINT_INVALID',+ VOICE_JOIN_ENDPOINT_UNREACHABLE: 'VOICE_JOIN_ENDPOINT_UNREACHABLE',+ VOICE_JOIN_KEY_MISMATCH: 'VOICE_JOIN_KEY_MISMATCH',+ VOICE_JOIN_TOKEN_BUSY: 'VOICE_JOIN_TOKEN_BUSY',+ VOICE_JOIN_TOKEN_INVALID: 'VOICE_JOIN_TOKEN_INVALID',+ VOICE_NODE_SIGNATURE_INVALID: 'VOICE_NODE_SIGNATURE_INVALID',+ VOICE_NODE_TOKEN_EXPIRED: 'VOICE_NODE_TOKEN_EXPIRED',+ VOICE_NODE_UNKNOWN: 'VOICE_NODE_UNKNOWN',+ VOICE_SERVER_API_KEY_IN_USE: 'VOICE_SERVER_API_KEY_IN_USE',+ VOICE_SERVER_ID_IN_USE: 'VOICE_SERVER_ID_IN_USE',WEBAUTHN_CREDENTIAL_LIMIT_REACHED: 'WEBAUTHN_CREDENTIAL_LIMIT_REACHED',ACCOUNT_VERIFICATION_REQUIRED: 'ACCOUNT_VERIFICATION_REQUIRED',DATE_OF_BIRTH_REQUIRED: 'DATE_OF_BIRTH_REQUIRED',diff --git a/packages/errors/src/domains/voice/VoiceJoinEndpointInvalidError.ts b/packages/errors/src/domains/voice/VoiceJoinEndpointInvalidError.tsnew file mode 100644index 000000000..7a6be75ab--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceJoinEndpointInvalidError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {BadRequestError} from '@fluxer/errors/src/domains/core/BadRequestError';++export class VoiceJoinEndpointInvalidError extends BadRequestError {+ constructor(detail: string) {+ super({code: APIErrorCodes.VOICE_JOIN_ENDPOINT_INVALID, data: {detail}});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceJoinEndpointUnreachableError.ts b/packages/errors/src/domains/voice/VoiceJoinEndpointUnreachableError.tsnew file mode 100644index 000000000..41aaca683--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceJoinEndpointUnreachableError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {BadRequestError} from '@fluxer/errors/src/domains/core/BadRequestError';++export class VoiceJoinEndpointUnreachableError extends BadRequestError {+ constructor(detail: string) {+ super({code: APIErrorCodes.VOICE_JOIN_ENDPOINT_UNREACHABLE, data: {detail}});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceJoinKeyMismatchError.ts b/packages/errors/src/domains/voice/VoiceJoinKeyMismatchError.tsnew file mode 100644index 000000000..16416a8bb--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceJoinKeyMismatchError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {BadRequestError} from '@fluxer/errors/src/domains/core/BadRequestError';++export class VoiceJoinKeyMismatchError extends BadRequestError {+ constructor() {+ super({code: APIErrorCodes.VOICE_JOIN_KEY_MISMATCH});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceJoinTokenBusyError.ts b/packages/errors/src/domains/voice/VoiceJoinTokenBusyError.tsnew file mode 100644index 000000000..67bf587be--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceJoinTokenBusyError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {ConflictError} from '@fluxer/errors/src/domains/core/ConflictError';++export class VoiceJoinTokenBusyError extends ConflictError {+ constructor() {+ super({code: APIErrorCodes.VOICE_JOIN_TOKEN_BUSY});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceJoinTokenInvalidError.ts b/packages/errors/src/domains/voice/VoiceJoinTokenInvalidError.tsnew file mode 100644index 000000000..c66940738--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceJoinTokenInvalidError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {UnauthorizedError} from '@fluxer/errors/src/domains/core/UnauthorizedError';++export class VoiceJoinTokenInvalidError extends UnauthorizedError {+ constructor() {+ super({code: APIErrorCodes.VOICE_JOIN_TOKEN_INVALID});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceNodeSignatureInvalidError.ts b/packages/errors/src/domains/voice/VoiceNodeSignatureInvalidError.tsnew file mode 100644index 000000000..913dd8c7f--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceNodeSignatureInvalidError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {UnauthorizedError} from '@fluxer/errors/src/domains/core/UnauthorizedError';++export class VoiceNodeSignatureInvalidError extends UnauthorizedError {+ constructor() {+ super({code: APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceNodeTokenExpiredError.ts b/packages/errors/src/domains/voice/VoiceNodeTokenExpiredError.tsnew file mode 100644index 000000000..58e6030e6--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceNodeTokenExpiredError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {UnauthorizedError} from '@fluxer/errors/src/domains/core/UnauthorizedError';++export class VoiceNodeTokenExpiredError extends UnauthorizedError {+ constructor() {+ super({code: APIErrorCodes.VOICE_NODE_TOKEN_EXPIRED});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceNodeUnknownError.ts b/packages/errors/src/domains/voice/VoiceNodeUnknownError.tsnew file mode 100644index 000000000..f34ec2b3b--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceNodeUnknownError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {UnauthorizedError} from '@fluxer/errors/src/domains/core/UnauthorizedError';++export class VoiceNodeUnknownError extends UnauthorizedError {+ constructor() {+ super({code: APIErrorCodes.VOICE_NODE_UNKNOWN});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceServerApiKeyInUseError.ts b/packages/errors/src/domains/voice/VoiceServerApiKeyInUseError.tsnew file mode 100644index 000000000..04cedd61c--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceServerApiKeyInUseError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {ConflictError} from '@fluxer/errors/src/domains/core/ConflictError';++export class VoiceServerApiKeyInUseError extends ConflictError {+ constructor() {+ super({code: APIErrorCodes.VOICE_SERVER_API_KEY_IN_USE});+ }+}diff --git a/packages/errors/src/domains/voice/VoiceServerIdInUseError.ts b/packages/errors/src/domains/voice/VoiceServerIdInUseError.tsnew file mode 100644index 000000000..b5b6213f9--- /dev/null+++ b/packages/errors/src/domains/voice/VoiceServerIdInUseError.ts@@ -0,0 +1,10 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {APIErrorCodes} from '@fluxer/constants/src/ApiErrorCodes';+import {ConflictError} from '@fluxer/errors/src/domains/core/ConflictError';++export class VoiceServerIdInUseError extends ConflictError {+ constructor() {+ super({code: APIErrorCodes.VOICE_SERVER_ID_IN_USE});+ }+}diff --git a/packages/errors/src/i18n/ErrorCodeMappings.ts b/packages/errors/src/i18n/ErrorCodeMappings.tsindex ae5dd7b5f..d74a52158 100644--- a/packages/errors/src/i18n/ErrorCodeMappings.ts+++ b/packages/errors/src/i18n/ErrorCodeMappings.ts@@ -410,6 +410,16 @@ export const ErrorCodeToI18nKey = {[APIErrorCodes.USER_NOT_IN_VOICE]: 'permissions.user_not_in_voice',[APIErrorCodes.USER_OWNS_GUILDS]: 'permissions.user_owns_guilds',[APIErrorCodes.VOICE_CHANNEL_FULL]: 'channels_and_guilds.voice_channel_full',+ [APIErrorCodes.VOICE_JOIN_ENDPOINT_INVALID]: 'calls_and_voice.voice_join_endpoint_invalid',+ [APIErrorCodes.VOICE_JOIN_ENDPOINT_UNREACHABLE]: 'calls_and_voice.voice_join_endpoint_unreachable',+ [APIErrorCodes.VOICE_JOIN_KEY_MISMATCH]: 'calls_and_voice.voice_join_key_mismatch',+ [APIErrorCodes.VOICE_JOIN_TOKEN_BUSY]: 'calls_and_voice.voice_join_token_busy',+ [APIErrorCodes.VOICE_JOIN_TOKEN_INVALID]: 'calls_and_voice.voice_join_token_invalid',+ [APIErrorCodes.VOICE_NODE_SIGNATURE_INVALID]: 'calls_and_voice.voice_node_signature_invalid',+ [APIErrorCodes.VOICE_NODE_TOKEN_EXPIRED]: 'calls_and_voice.voice_node_token_expired',+ [APIErrorCodes.VOICE_NODE_UNKNOWN]: 'calls_and_voice.voice_node_unknown',+ [APIErrorCodes.VOICE_SERVER_API_KEY_IN_USE]: 'calls_and_voice.voice_server_api_key_in_use',+ [APIErrorCodes.VOICE_SERVER_ID_IN_USE]: 'calls_and_voice.voice_server_id_in_use',[APIErrorCodes.WEBAUTHN_CREDENTIAL_LIMIT_REACHED]: 'mfa_and_passkeys.webauthn_credential_limit_reached',[ValidationErrorCodes.MESSAGE_IDS_CANNOT_BE_EMPTY]: 'messages.message_ids_cannot_be_empty',[ValidationErrorCodes.MESSAGES_ARRAY_REQUIRED_AND_NOT_EMPTY]: 'messages.messages_array_required',diff --git a/packages/errors/src/i18n/ErrorI18nMessages.ts b/packages/errors/src/i18n/ErrorI18nMessages.tsindex a7f72b432..29dc146da 100644--- a/packages/errors/src/i18n/ErrorI18nMessages.ts+++ b/packages/errors/src/i18n/ErrorI18nMessages.ts@@ -265,6 +265,17 @@ export const ERROR_I18N_MESSAGES = {"Voice message duration can't exceed {maxDuration, plural, one {# second} other {# seconds}}.",'calls_and_voice.voice_message_duration_required': 'Voice message attachments must specify a duration.','calls_and_voice.voice_message_waveform_required': 'Voice message attachments must include waveform data.',+ 'calls_and_voice.voice_join_endpoint_invalid': "The voice server endpoint isn't allowed.",+ 'calls_and_voice.voice_join_endpoint_unreachable': "Fluxer couldn't connect to the voice server endpoint.",+ 'calls_and_voice.voice_join_key_mismatch': 'The voice server rejected its API key and secret.',+ 'calls_and_voice.voice_join_token_busy': 'Another voice server is joining with this token right now.',+ 'calls_and_voice.voice_join_token_invalid': 'The voice join token is not valid.',+ 'calls_and_voice.voice_node_signature_invalid': "The voice server's signature doesn't match its secret.",+ 'calls_and_voice.voice_node_token_expired':+ "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ 'calls_and_voice.voice_node_unknown': 'No voice server has this API key.',+ 'calls_and_voice.voice_server_api_key_in_use': 'Another voice server already uses this API key.',+ 'calls_and_voice.voice_server_id_in_use': 'Another voice server already uses this ID.','calls_and_voice.voice_messages_require_single_attachment': 'Voice messages must contain exactly one attachment.','captcha.invalid': 'Verification failed. Try again.','captcha.required': 'Verification required. Try again.',diff --git a/packages/errors/src/i18n/weblate/locales/ar.json b/packages/errors/src/i18n/weblate/locales/ar.jsonindex 1b107f453..2b6e6ad7c 100644--- a/packages/errors/src/i18n/weblate/locales/ar.json+++ b/packages/errors/src/i18n/weblate/locales/ar.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "عدم تطابق نطاق مفتاح البث.","calls_and_voice.unknown_voice_region": "منطقة صوتية غير معروفة.","calls_and_voice.unknown_voice_server": "خادم صوتي غير معروف.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "لا يمكن أن تتجاوز مدة الرسالة الصوتية {maxDuration, plural, one {ثانية واحدة} two {ثانيتين} few {# ثوانٍ} many {# ثانية} other {# ثانية}}.","calls_and_voice.voice_message_duration_required": "يجب أن تحدد مرفقات الرسائل الصوتية مدة.","calls_and_voice.voice_message_waveform_required": "يجب أن تحتوي مرفقات الرسائل الصوتية على بيانات الموجة الصوتية.","calls_and_voice.voice_messages_require_single_attachment": "يجب أن تحتوي الرسائل الصوتية على مرفق واحد فقط.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "فشل التحقق. حاول مرة أخرى.","captcha.required": "مطلوب التحقق. حاول مرة أخرى.","channels_and_guilds.afk_channel_must_be_in_guild": "يجب أن تكون قناة AFK ضمن هذا المجتمع.",diff --git a/packages/errors/src/i18n/weblate/locales/bg.json b/packages/errors/src/i18n/weblate/locales/bg.jsonindex 148a3d7ae..456e76210 100644--- a/packages/errors/src/i18n/weblate/locales/bg.json+++ b/packages/errors/src/i18n/weblate/locales/bg.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Несъответствие на обхвата на ключа за стрийм.","calls_and_voice.unknown_voice_region": "Неизвестен гласов регион.","calls_and_voice.unknown_voice_server": "Неизвестен гласов сървър.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Продължителността на гласовото съобщение не може да надвишава {maxDuration} {maxDuration, plural, one {секунда} other {секунди}}.","calls_and_voice.voice_message_duration_required": "Прикачените файлове към гласови съобщения трябва да имат продължителност.","calls_and_voice.voice_message_waveform_required": "Прикачените файлове към гласови съобщения трябва да съдържат данни за вълновата форма.","calls_and_voice.voice_messages_require_single_attachment": "Гласовите съобщения трябва да съдържат точно един прикачен файл.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Проверката не успя. Опитай отново.","captcha.required": "Изисква се проверка. Опитай отново.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK каналът трябва да е в рамките на тази общност.",diff --git a/packages/errors/src/i18n/weblate/locales/cs.json b/packages/errors/src/i18n/weblate/locales/cs.jsonindex 4f630c59e..f9fc0a5b1 100644--- a/packages/errors/src/i18n/weblate/locales/cs.json+++ b/packages/errors/src/i18n/weblate/locales/cs.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Neshoda rozsahu a streamovacího klíče.","calls_and_voice.unknown_voice_region": "Neznámý hlasový region.","calls_and_voice.unknown_voice_server": "Neznámý hlasový server.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Délka hlasové zprávy nesmí přesáhnout {maxDuration, plural, one {# sekundu} few {# sekundy} many {# sekundy} other {# sekund}}.","calls_and_voice.voice_message_duration_required": "Přílohy hlasových zpráv musí uvádět délku.","calls_and_voice.voice_message_waveform_required": "Přílohy hlasových zpráv musí obsahovat data průběhu zvukové vlny.","calls_and_voice.voice_messages_require_single_attachment": "Hlasové zprávy musí obsahovat právě jednu přílohu.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Ověření se nezdařilo. Zkuste to znovu.","captcha.required": "Ověření je nutné. Zkuste to znovu.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK kanál musí být v této komunitě.",diff --git a/packages/errors/src/i18n/weblate/locales/da.json b/packages/errors/src/i18n/weblate/locales/da.jsonindex 6e569a0d3..5c0029367 100644--- a/packages/errors/src/i18n/weblate/locales/da.json+++ b/packages/errors/src/i18n/weblate/locales/da.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Streamnøglens gyldighedsområde stemmer ikke overens.","calls_and_voice.unknown_voice_region": "Ukendt taleregion.","calls_and_voice.unknown_voice_server": "Ukendt taleserver.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "En talebesked må højst vare {maxDuration, plural, one {# sekund} other {# sekunder}}.","calls_and_voice.voice_message_duration_required": "Vedhæftede filer til talebeskeder skal angive en varighed.","calls_and_voice.voice_message_waveform_required": "Vedhæftede filer til talebeskeder skal indeholde bølgeformdata.","calls_and_voice.voice_messages_require_single_attachment": "Talebeskeder skal indeholde præcis én vedhæftet fil.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Bekræftelse mislykkedes. Prøv igen.","captcha.required": "Bekræftelse påkrævet. Prøv igen.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK-kanalen skal være i dette fællesskab.",diff --git a/packages/errors/src/i18n/weblate/locales/de.json b/packages/errors/src/i18n/weblate/locales/de.jsonindex 98c6d485b..2417f9564 100644--- a/packages/errors/src/i18n/weblate/locales/de.json+++ b/packages/errors/src/i18n/weblate/locales/de.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Der Stream-Key stimmt nicht mit dem Scope überein.","calls_and_voice.unknown_voice_region": "Die Sprachregion ist unbekannt.","calls_and_voice.unknown_voice_server": "Der Sprachserver ist unbekannt.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Die Dauer der Sprachnachricht darf maximal {maxDuration, plural, one {# Sekunde} other {# Sekunden}} betragen.","calls_and_voice.voice_message_duration_required": "Für Anhänge von Sprachnachrichten muss eine Dauer angegeben werden.","calls_and_voice.voice_message_waveform_required": "Anhänge von Sprachnachrichten müssen Wellenformdaten enthalten.","calls_and_voice.voice_messages_require_single_attachment": "Sprachnachrichten müssen genau einen Anhang enthalten.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Überprüfung fehlgeschlagen. Versuche es erneut.","captcha.required": "Überprüfung erforderlich. Versuche es erneut.","channels_and_guilds.afk_channel_must_be_in_guild": "Der AFK-Kanal muss sich in dieser Community befinden.",diff --git a/packages/errors/src/i18n/weblate/locales/el.json b/packages/errors/src/i18n/weblate/locales/el.jsonindex 9348c76bc..d3f86f1b2 100644--- a/packages/errors/src/i18n/weblate/locales/el.json+++ b/packages/errors/src/i18n/weblate/locales/el.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Το κλειδί ροής δεν αντιστοιχεί στο πεδίο εφαρμογής.","calls_and_voice.unknown_voice_region": "Άγνωστη περιοχή φωνής.","calls_and_voice.unknown_voice_server": "Άγνωστος φωνητικός διακομιστής.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Η διάρκεια του φωνητικού μηνύματος δεν μπορεί να υπερβαίνει {maxDuration, plural, one {το # δευτερόλεπτο} other {τα # δευτερόλεπτα}}.","calls_and_voice.voice_message_duration_required": "Τα συνημμένα φωνητικών μηνυμάτων πρέπει να έχουν διάρκεια.","calls_and_voice.voice_message_waveform_required": "Τα συνημμένα φωνητικών μηνυμάτων πρέπει να περιέχουν δεδομένα κυματομορφής.","calls_and_voice.voice_messages_require_single_attachment": "Τα φωνητικά μηνύματα πρέπει να περιέχουν ακριβώς ένα συνημμένο.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Η επαλήθευση απέτυχε. Δοκίμασε ξανά.","captcha.required": "Απαιτείται επαλήθευση. Δοκίμασε ξανά.","channels_and_guilds.afk_channel_must_be_in_guild": "Το κανάλι AFK πρέπει να βρίσκεται σε αυτήν την κοινότητα.",diff --git a/packages/errors/src/i18n/weblate/locales/en-GB.json b/packages/errors/src/i18n/weblate/locales/en-GB.jsonindex e09a60c03..d9f886e4f 100644--- a/packages/errors/src/i18n/weblate/locales/en-GB.json+++ b/packages/errors/src/i18n/weblate/locales/en-GB.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Stream key scope mismatch.","calls_and_voice.unknown_voice_region": "Unknown voice region.","calls_and_voice.unknown_voice_server": "Unknown voice server.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Voice message duration can't exceed {maxDuration, plural, one {# second} other {# seconds}}.","calls_and_voice.voice_message_duration_required": "Voice message attachments must specify a duration.","calls_and_voice.voice_message_waveform_required": "Voice message attachments must include waveform data.","calls_and_voice.voice_messages_require_single_attachment": "Voice messages must contain exactly one attachment.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verification failed. Try again.","captcha.required": "Verification required. Try again.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK channel must be in this community.",diff --git a/packages/errors/src/i18n/weblate/locales/es-419.json b/packages/errors/src/i18n/weblate/locales/es-419.jsonindex 656371ea5..891629444 100644--- a/packages/errors/src/i18n/weblate/locales/es-419.json+++ b/packages/errors/src/i18n/weblate/locales/es-419.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "El ámbito de la clave de transmisión no coincide.","calls_and_voice.unknown_voice_region": "Región de voz desconocida.","calls_and_voice.unknown_voice_server": "Servidor de voz desconocido.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "La duración del mensaje de voz no puede exceder {maxDuration} {maxDuration, plural, one {segundo} other {segundos}}.","calls_and_voice.voice_message_duration_required": "Los archivos adjuntos de mensajes de voz deben especificar una duración.","calls_and_voice.voice_message_waveform_required": "Los archivos adjuntos de mensajes de voz deben incluir datos de onda.","calls_and_voice.voice_messages_require_single_attachment": "Los mensajes de voz deben contener exactamente un archivo adjunto.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verificación fallida. Inténtalo de nuevo.","captcha.required": "Verificación requerida. Inténtalo de nuevo.","channels_and_guilds.afk_channel_must_be_in_guild": "El canal AFK debe estar en esta comunidad.",diff --git a/packages/errors/src/i18n/weblate/locales/es-ES.json b/packages/errors/src/i18n/weblate/locales/es-ES.jsonindex 48ca97c1a..d9cffe960 100644--- a/packages/errors/src/i18n/weblate/locales/es-ES.json+++ b/packages/errors/src/i18n/weblate/locales/es-ES.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "El ámbito de la clave de transmisión no coincide.","calls_and_voice.unknown_voice_region": "Región de voz desconocida.","calls_and_voice.unknown_voice_server": "Servidor de voz desconocido.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "La duración del mensaje de voz no puede superar {maxDuration} {maxDuration, plural, one {segundo} other {segundos}}.","calls_and_voice.voice_message_duration_required": "Los archivos adjuntos de mensajes de voz deben especificar una duración.","calls_and_voice.voice_message_waveform_required": "Los archivos adjuntos de mensajes de voz deben incluir datos de forma de onda.","calls_and_voice.voice_messages_require_single_attachment": "Los mensajes de voz deben contener exactamente un archivo adjunto.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verificación fallida. Inténtalo de nuevo.","captcha.required": "Verificación necesaria. Inténtalo de nuevo.","channels_and_guilds.afk_channel_must_be_in_guild": "El canal AFK debe pertenecer a esta comunidad.",diff --git a/packages/errors/src/i18n/weblate/locales/fi.json b/packages/errors/src/i18n/weblate/locales/fi.jsonindex f7598d3a0..4a4767c22 100644--- a/packages/errors/src/i18n/weblate/locales/fi.json+++ b/packages/errors/src/i18n/weblate/locales/fi.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Striimiavaimen käyttöoikeus ei täsmää.","calls_and_voice.unknown_voice_region": "Tuntematon äänialue.","calls_and_voice.unknown_voice_server": "Tuntematon äänipalvelin.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Ääniviestin kesto ei voi ylittää {maxDuration, plural, one {# sekuntia} other {# sekuntia}}.","calls_and_voice.voice_message_duration_required": "Ääniviestin liitteiden on sisällettävä kesto.","calls_and_voice.voice_message_waveform_required": "Ääniviestin liitteiden on sisällettävä aaltomuotodataa.","calls_and_voice.voice_messages_require_single_attachment": "Ääniviestien on sisällettävä täsmälleen yksi liite.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Vahvistus epäonnistui. Yritä uudelleen.","captcha.required": "Vahvistus vaaditaan. Yritä uudelleen.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK-kanavan on oltava tässä yhteisössä.",diff --git a/packages/errors/src/i18n/weblate/locales/fr.json b/packages/errors/src/i18n/weblate/locales/fr.jsonindex 3860dd80a..8b81b6e22 100644--- a/packages/errors/src/i18n/weblate/locales/fr.json+++ b/packages/errors/src/i18n/weblate/locales/fr.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Incompatibilité de portée pour la clé de diffusion.","calls_and_voice.unknown_voice_region": "Région vocale inconnue.","calls_and_voice.unknown_voice_server": "Serveur vocal inconnu.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "La durée du message vocal ne peut pas dépasser {maxDuration, plural, =1 {# seconde} other {# secondes}}.","calls_and_voice.voice_message_duration_required": "Les pièces jointes des messages vocaux doivent spécifier une durée.","calls_and_voice.voice_message_waveform_required": "Les pièces jointes des messages vocaux doivent inclure des données de forme d'onde.","calls_and_voice.voice_messages_require_single_attachment": "Les messages vocaux doivent contenir exactement une pièce jointe.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Échec de la vérification. Réessayez.","captcha.required": "Vérification requise. Réessayez.","channels_and_guilds.afk_channel_must_be_in_guild": "Le salon AFK doit être dans cette communauté.",diff --git a/packages/errors/src/i18n/weblate/locales/he.json b/packages/errors/src/i18n/weblate/locales/he.jsonindex 1caa77ccb..9d991e4ba 100644--- a/packages/errors/src/i18n/weblate/locales/he.json+++ b/packages/errors/src/i18n/weblate/locales/he.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "אי-התאמה בהיקף של מפתח הזרם.","calls_and_voice.unknown_voice_region": "אזור קול לא ידוע.","calls_and_voice.unknown_voice_server": "שרת קול לא ידוע.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "משך הודעה קולית לא יכול לחרוג מ-{maxDuration} {maxDuration, plural, one {שנייה} other {שניות}}.","calls_and_voice.voice_message_duration_required": "קבצים מצורפים של הודעות קוליות חייבים לציין משך זמן.","calls_and_voice.voice_message_waveform_required": "קבצים מצורפים של הודעות קוליות חייבים לכלול נתוני צורת גל.","calls_and_voice.voice_messages_require_single_attachment": "הודעות קוליות חייבות לכלול קובץ מצורף אחד בלבד.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "האימות נכשל. ניתן לנסות שוב.","captcha.required": "נדרש אימות. ניתן לנסות שוב.","channels_and_guilds.afk_channel_must_be_in_guild": "ערוץ AFK חייב להיות בקהילה זו.",diff --git a/packages/errors/src/i18n/weblate/locales/hi.json b/packages/errors/src/i18n/weblate/locales/hi.jsonindex 8546142c8..0e349fbe8 100644--- a/packages/errors/src/i18n/weblate/locales/hi.json+++ b/packages/errors/src/i18n/weblate/locales/hi.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "स्ट्रीम कुंजी का स्कोप मेल नहीं खा रहा।","calls_and_voice.unknown_voice_region": "अज्ञात वॉइस रीजन।","calls_and_voice.unknown_voice_server": "अज्ञात वॉइस सर्वर।",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "वॉइस मैसेज की ड्यूरेशन {maxDuration} {maxDuration, plural, one {सेकंड} other {सेकंड}} से ज़्यादा नहीं हो सकती।","calls_and_voice.voice_message_duration_required": "वॉइस मैसेज अटैचमेंट में ड्यूरेशन बतानी होगी।","calls_and_voice.voice_message_waveform_required": "वॉइस मैसेज अटैचमेंट में वेवफ़ॉर्म डेटा होना चाहिए।","calls_and_voice.voice_messages_require_single_attachment": "वॉइस मैसेज में केवल एक अटैचमेंट होना चाहिए।",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "सत्यापन विफल रहा। फिर से कोशिश करें।","captcha.required": "सत्यापन आवश्यक है। फिर से कोशिश करें।","channels_and_guilds.afk_channel_must_be_in_guild": "AFK चैनल इस कम्युनिटी में होना चाहिए।",diff --git a/packages/errors/src/i18n/weblate/locales/hr.json b/packages/errors/src/i18n/weblate/locales/hr.jsonindex fb6a4166f..075b97af9 100644--- a/packages/errors/src/i18n/weblate/locales/hr.json+++ b/packages/errors/src/i18n/weblate/locales/hr.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Neusklađenost ključa streama i opsega.","calls_and_voice.unknown_voice_region": "Nepoznata glasovna regija.","calls_and_voice.unknown_voice_server": "Nepoznat glasovni poslužitelj.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Trajanje glasovne poruke ne može premašiti {maxDuration, plural, one {# sekundu} few {# sekunde} other {# sekundi}}.","calls_and_voice.voice_message_duration_required": "Privici glasovne poruke moraju imati navedeno trajanje.","calls_and_voice.voice_message_waveform_required": "Privici glasovne poruke moraju sadržavati podatke o valnom obliku.","calls_and_voice.voice_messages_require_single_attachment": "Glasovne poruke moraju sadržavati točno jedan privitak.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Provjera nije uspjela. Pokušaj ponovno.","captcha.required": "Potrebna je provjera. Pokušaj ponovno.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK kanal mora biti u ovoj zajednici.",diff --git a/packages/errors/src/i18n/weblate/locales/hu.json b/packages/errors/src/i18n/weblate/locales/hu.jsonindex ef0210ec0..80ee90b11 100644--- a/packages/errors/src/i18n/weblate/locales/hu.json+++ b/packages/errors/src/i18n/weblate/locales/hu.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Nem egyezik a streamkulcs és a hatókör.","calls_and_voice.unknown_voice_region": "Ismeretlen hangrégió.","calls_and_voice.unknown_voice_server": "Ismeretlen hangkiszolgáló.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "A hangüzenet legnagyobb megengedett hossza: {maxDuration, plural, one {# másodperc} other {# másodperc}}.","calls_and_voice.voice_message_duration_required": "A hangüzenet mellékleteihez meg kell adni az időtartamot.","calls_and_voice.voice_message_waveform_required": "A hangüzenet mellékleteinek hullámformaadatokat kell tartalmazniuk.","calls_and_voice.voice_messages_require_single_attachment": "A hangüzeneteknek pontosan egy mellékletet kell tartalmazniuk.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Ellenőrzés sikertelen. Próbáld újra.","captcha.required": "Ellenőrzés szükséges. Próbáld újra.","channels_and_guilds.afk_channel_must_be_in_guild": "Az AFK-csatornának ebben a közösségben kell lennie.",diff --git a/packages/errors/src/i18n/weblate/locales/id.json b/packages/errors/src/i18n/weblate/locales/id.jsonindex a1adac74f..988e50f93 100644--- a/packages/errors/src/i18n/weblate/locales/id.json+++ b/packages/errors/src/i18n/weblate/locales/id.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Cakupan kunci stream tidak cocok.","calls_and_voice.unknown_voice_region": "Wilayah suara tidak dikenal.","calls_and_voice.unknown_voice_server": "Server suara tidak dikenal.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Durasi pesan suara tidak boleh melebihi {maxDuration} {maxDuration, plural, other {detik}}.","calls_and_voice.voice_message_duration_required": "Lampiran pesan suara harus menentukan durasi.","calls_and_voice.voice_message_waveform_required": "Lampiran pesan suara harus menyertakan data bentuk gelombang.","calls_and_voice.voice_messages_require_single_attachment": "Pesan suara harus berisi tepat satu lampiran.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verifikasi gagal. Coba lagi.","captcha.required": "Verifikasi diperlukan. Coba lagi.","channels_and_guilds.afk_channel_must_be_in_guild": "Saluran AFK harus ada di komunitas ini.",diff --git a/packages/errors/src/i18n/weblate/locales/it.json b/packages/errors/src/i18n/weblate/locales/it.jsonindex 22fb3bab1..25b047432 100644--- a/packages/errors/src/i18n/weblate/locales/it.json+++ b/packages/errors/src/i18n/weblate/locales/it.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "L’ambito della chiave di streaming non corrisponde.","calls_and_voice.unknown_voice_region": "Regione vocale sconosciuta.","calls_and_voice.unknown_voice_server": "Server vocale sconosciuto.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "La durata del messaggio vocale non può superare {maxDuration, plural, one {# secondo} other {# secondi}}.","calls_and_voice.voice_message_duration_required": "Gli allegati dei messaggi vocali devono specificare una durata.","calls_and_voice.voice_message_waveform_required": "Gli allegati dei messaggi vocali devono includere i dati della forma d'onda.","calls_and_voice.voice_messages_require_single_attachment": "I messaggi vocali devono contenere esattamente un allegato.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verifica non riuscita. Riprova.","captcha.required": "Verifica richiesta. Riprova.","channels_and_guilds.afk_channel_must_be_in_guild": "Il canale AFK deve appartenere a questa Community.",diff --git a/packages/errors/src/i18n/weblate/locales/ja.json b/packages/errors/src/i18n/weblate/locales/ja.jsonindex b9e47132c..d24084c27 100644--- a/packages/errors/src/i18n/weblate/locales/ja.json+++ b/packages/errors/src/i18n/weblate/locales/ja.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "ストリームキーのスコープが一致していません。","calls_and_voice.unknown_voice_region": "不明なボイスリージョンです。","calls_and_voice.unknown_voice_server": "不明なボイスサーバーです。",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "ボイスメッセージの長さは{maxDuration, plural, other {#秒}}を超えることはできません。","calls_and_voice.voice_message_duration_required": "ボイスメッセージの添付ファイルには長さを指定する必要があります。","calls_and_voice.voice_message_waveform_required": "ボイスメッセージの添付ファイルには波形データを含める必要があります。","calls_and_voice.voice_messages_require_single_attachment": "ボイスメッセージには添付ファイルが1つだけ必要です。",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "確認に失敗しました。もう一度お試しください。","captcha.required": "確認が必要です。もう一度お試しください。","channels_and_guilds.afk_channel_must_be_in_guild": "AFKチャンネルはこのコミュニティ内に設定する必要があります。",diff --git a/packages/errors/src/i18n/weblate/locales/ko.json b/packages/errors/src/i18n/weblate/locales/ko.jsonindex 0cfa49b51..b747b5bf6 100644--- a/packages/errors/src/i18n/weblate/locales/ko.json+++ b/packages/errors/src/i18n/weblate/locales/ko.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "스트림 키 범위가 일치하지 않아요.","calls_and_voice.unknown_voice_region": "알 수 없는 음성 지역이에요.","calls_and_voice.unknown_voice_server": "알 수 없는 음성 서버예요.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "음성 메시지 길이는 {maxDuration, plural, other {#초}}를 초과할 수 없어요.","calls_and_voice.voice_message_duration_required": "음성 메시지 첨부 파일은 길이를 지정해야 해요.","calls_and_voice.voice_message_waveform_required": "음성 메시지 첨부 파일에는 파형 데이터가 포함되어야 해요.","calls_and_voice.voice_messages_require_single_attachment": "음성 메시지에는 첨부 파일이 하나만 포함되어야 해요.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "확인하지 못했어요. 다시 시도해 주세요.","captcha.required": "확인이 필요해요. 다시 시도해 주세요.","channels_and_guilds.afk_channel_must_be_in_guild": "자리 비움 채널은 이 커뮤니티에 있어야 해요.",diff --git a/packages/errors/src/i18n/weblate/locales/lt.json b/packages/errors/src/i18n/weblate/locales/lt.jsonindex 50954d686..391b70ed5 100644--- a/packages/errors/src/i18n/weblate/locales/lt.json+++ b/packages/errors/src/i18n/weblate/locales/lt.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Transliacijos rakto apimties neatitikimas.","calls_and_voice.unknown_voice_region": "Nežinomas balso regionas.","calls_and_voice.unknown_voice_server": "Nežinomas balso serveris.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Balso žinutės trukmė negali viršyti {maxDuration, plural, one {# sekundės} few {# sekundžių} many {# sekundės} other {# sekundžių}}.","calls_and_voice.voice_message_duration_required": "Balso žinutės priedai turi nurodyti trukmę.","calls_and_voice.voice_message_waveform_required": "Balso žinutės prieduose turi būti bangos formos duomenys.","calls_and_voice.voice_messages_require_single_attachment": "Balso žinutėse turi būti lygiai vienas priedas.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Nepavyko patvirtinti. Bandyk dar kartą.","captcha.required": "Reikalingas patvirtinimas. Bandyk dar kartą.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK kanalas turi būti šioje bendruomenėje.",diff --git a/packages/errors/src/i18n/weblate/locales/nl.json b/packages/errors/src/i18n/weblate/locales/nl.jsonindex 9947f9d2a..8fc7338d8 100644--- a/packages/errors/src/i18n/weblate/locales/nl.json+++ b/packages/errors/src/i18n/weblate/locales/nl.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Het bereik van de streamsleutel komt niet overeen.","calls_and_voice.unknown_voice_region": "Onbekende spraakregio.","calls_and_voice.unknown_voice_server": "Onbekende spraakserver.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Een spraakbericht mag maximaal {maxDuration, plural, one {# seconde} other {# seconden}} duren.","calls_and_voice.voice_message_duration_required": "Bij spraakberichtbijlagen moet de duur worden opgegeven.","calls_and_voice.voice_message_waveform_required": "Spraakberichtbijlagen moeten golfvormgegevens bevatten.","calls_and_voice.voice_messages_require_single_attachment": "Spraakberichten moeten precies één bijlage bevatten.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verificatie mislukt. Probeer het opnieuw.","captcha.required": "Verificatie vereist. Probeer het opnieuw.","channels_and_guilds.afk_channel_must_be_in_guild": "Het AFK-kanaal moet deel uitmaken van deze community.",diff --git a/packages/errors/src/i18n/weblate/locales/no.json b/packages/errors/src/i18n/weblate/locales/no.jsonindex 27aa09500..d96da02f1 100644--- a/packages/errors/src/i18n/weblate/locales/no.json+++ b/packages/errors/src/i18n/weblate/locales/no.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Strømmenøkkel og omfang samsvarer ikke.","calls_and_voice.unknown_voice_region": "Ukjent taleregion.","calls_and_voice.unknown_voice_server": "Ukjent taleserver.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Varigheten på talemeldingen kan ikke overstige {maxDuration, plural, one {# sekund} other {# sekunder}}.","calls_and_voice.voice_message_duration_required": "Vedlegg til talemeldinger må angi en varighet.","calls_and_voice.voice_message_waveform_required": "Vedlegg til talemeldinger må inneholde bølgeformdata.","calls_and_voice.voice_messages_require_single_attachment": "Talemeldinger må inneholde nøyaktig ett vedlegg.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verifisering mislyktes. Prøv igjen.","captcha.required": "Verifisering kreves. Prøv igjen.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK-kanalen må være i dette fellesskapet.",diff --git a/packages/errors/src/i18n/weblate/locales/pl.json b/packages/errors/src/i18n/weblate/locales/pl.jsonindex 43604cbb8..183f3c4cd 100644--- a/packages/errors/src/i18n/weblate/locales/pl.json+++ b/packages/errors/src/i18n/weblate/locales/pl.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Niezgodny zakres klucza transmisji.","calls_and_voice.unknown_voice_region": "Nieznany region głosowy.","calls_and_voice.unknown_voice_server": "Nieznany serwer głosowy.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Czas trwania wiadomości głosowej nie może przekroczyć {maxDuration, plural, one {# sekundy} few {# sekund} many {# sekund} other {# sekundy}}.","calls_and_voice.voice_message_duration_required": "Załączniki wiadomości głosowych muszą określać czas trwania.","calls_and_voice.voice_message_waveform_required": "Załączniki wiadomości głosowych muszą zawierać dane przebiegu fali dźwiękowej.","calls_and_voice.voice_messages_require_single_attachment": "Wiadomości głosowe muszą zawierać dokładnie jeden załącznik.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Weryfikacja nie powiodła się. Spróbuj ponownie.","captcha.required": "Weryfikacja wymagana. Spróbuj ponownie.","channels_and_guilds.afk_channel_must_be_in_guild": "Kanał AFK musi znajdować się w tej społeczności.",diff --git a/packages/errors/src/i18n/weblate/locales/pt-BR.json b/packages/errors/src/i18n/weblate/locales/pt-BR.jsonindex bd57c78c4..cabe14bd7 100644--- a/packages/errors/src/i18n/weblate/locales/pt-BR.json+++ b/packages/errors/src/i18n/weblate/locales/pt-BR.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Incompatibilidade da chave de transmissão com o escopo.","calls_and_voice.unknown_voice_region": "Região de voz desconhecida.","calls_and_voice.unknown_voice_server": "Servidor de voz desconhecido.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "A duração da mensagem de voz não pode exceder {maxDuration, plural, =1 {# segundo} other {# segundos}}.","calls_and_voice.voice_message_duration_required": "Os anexos de mensagem de voz devem especificar uma duração.","calls_and_voice.voice_message_waveform_required": "Os anexos de mensagem de voz devem incluir dados da forma de onda.","calls_and_voice.voice_messages_require_single_attachment": "As mensagens de voz devem conter exatamente um anexo.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Falha na verificação. Tente novamente.","captcha.required": "Verificação necessária. Tente novamente.","channels_and_guilds.afk_channel_must_be_in_guild": "O canal AFK deve estar nesta comunidade.",diff --git a/packages/errors/src/i18n/weblate/locales/ro.json b/packages/errors/src/i18n/weblate/locales/ro.jsonindex 4290a6710..73f9d9a72 100644--- a/packages/errors/src/i18n/weblate/locales/ro.json+++ b/packages/errors/src/i18n/weblate/locales/ro.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Domeniul cheii de flux nu corespunde.","calls_and_voice.unknown_voice_region": "Regiune vocală necunoscută.","calls_and_voice.unknown_voice_server": "Server vocal necunoscut.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Durata mesajului vocal nu poate depăși {maxDuration, plural, one {# secundă} few {# secunde} other {# de secunde}}.","calls_and_voice.voice_message_duration_required": "Atașamentele mesajelor vocale trebuie să specifice o durată.","calls_and_voice.voice_message_waveform_required": "Atașamentele mesajelor vocale trebuie să includă datele formei de undă.","calls_and_voice.voice_messages_require_single_attachment": "Mesajele vocale trebuie să conțină exact un atașament.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verificare eșuată. Încearcă din nou.","captcha.required": "Verificare necesară. Încearcă din nou.","channels_and_guilds.afk_channel_must_be_in_guild": "Canalul AFK trebuie să fie în această comunitate.",diff --git a/packages/errors/src/i18n/weblate/locales/ru.json b/packages/errors/src/i18n/weblate/locales/ru.jsonindex 982470e5d..dcd19cff3 100644--- a/packages/errors/src/i18n/weblate/locales/ru.json+++ b/packages/errors/src/i18n/weblate/locales/ru.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Область действия ключа трансляции не соответствует каналу.","calls_and_voice.unknown_voice_region": "Неизвестный голосовой регион.","calls_and_voice.unknown_voice_server": "Неизвестный голосовой сервер.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Длительность голосового сообщения не может превышать {maxDuration} {maxDuration, plural, one {секунду} few {секунды} many {секунд} other {секунды}}.","calls_and_voice.voice_message_duration_required": "У вложений голосовых сообщений должна быть указана длительность.","calls_and_voice.voice_message_waveform_required": "Вложения голосовых сообщений должны содержать данные формы волны.","calls_and_voice.voice_messages_require_single_attachment": "Голосовые сообщения должны содержать ровно одно вложение.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Проверка не удалась. Повторите попытку.","captcha.required": "Требуется проверка. Повторите попытку.","channels_and_guilds.afk_channel_must_be_in_guild": "Канал AFK должен быть в этом сообществе.",diff --git a/packages/errors/src/i18n/weblate/locales/sv-SE.json b/packages/errors/src/i18n/weblate/locales/sv-SE.jsonindex 6507d080a..0f0c52646 100644--- a/packages/errors/src/i18n/weblate/locales/sv-SE.json+++ b/packages/errors/src/i18n/weblate/locales/sv-SE.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Strömningsnyckelns omfång matchar inte.","calls_and_voice.unknown_voice_region": "Okänd röstregion.","calls_and_voice.unknown_voice_server": "Okänd röstserver.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Röstmeddelandets längd får inte överstiga {maxDuration, plural, one {# sekund} other {# sekunder}}.","calls_and_voice.voice_message_duration_required": "Bilagor till röstmeddelanden måste ha en angiven speltid.","calls_and_voice.voice_message_waveform_required": "Bilagor till röstmeddelanden måste innehålla vågformsdata.","calls_and_voice.voice_messages_require_single_attachment": "Röstmeddelanden måste innehålla exakt en bilaga.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verifieringen misslyckades. Försök igen.","captcha.required": "Verifiering krävs. Försök igen.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK-kanalen måste tillhöra denna community.",diff --git a/packages/errors/src/i18n/weblate/locales/th.json b/packages/errors/src/i18n/weblate/locales/th.jsonindex 9baafb5bd..c887bf43b 100644--- a/packages/errors/src/i18n/weblate/locales/th.json+++ b/packages/errors/src/i18n/weblate/locales/th.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "ขอบเขตคีย์สตรีมไม่ตรงกัน","calls_and_voice.unknown_voice_region": "ไม่พบภูมิภาคเสียง","calls_and_voice.unknown_voice_server": "ไม่พบเซิร์ฟเวอร์เสียง",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "ระยะเวลาข้อความเสียงต้องไม่เกิน {maxDuration, plural, other {# วินาที}}","calls_and_voice.voice_message_duration_required": "ไฟล์แนบข้อความเสียงต้องกำหนดระยะเวลา","calls_and_voice.voice_message_waveform_required": "ไฟล์แนบข้อความเสียงต้องแนบข้อมูลรูปคลื่น","calls_and_voice.voice_messages_require_single_attachment": "ข้อความเสียงต้องมีไฟล์แนบเพียงไฟล์เดียวเท่านั้น",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "การยืนยันล้มเหลว ลองอีกครั้ง","captcha.required": "ต้องมีการยืนยัน ลองอีกครั้ง","channels_and_guilds.afk_channel_must_be_in_guild": "ช่อง AFK ต้องอยู่ในคอมมูนิตี้นี้",diff --git a/packages/errors/src/i18n/weblate/locales/tr.json b/packages/errors/src/i18n/weblate/locales/tr.jsonindex b274cb330..5315057bb 100644--- a/packages/errors/src/i18n/weblate/locales/tr.json+++ b/packages/errors/src/i18n/weblate/locales/tr.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Yayın anahtarı ile kapsam uyuşmuyor.","calls_and_voice.unknown_voice_region": "Bilinmeyen ses bölgesi.","calls_and_voice.unknown_voice_server": "Bilinmeyen ses sunucusu.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Sesli mesaj süresi {maxDuration, plural, one {# saniyeyi} other {# saniyeyi}} geçemez.","calls_and_voice.voice_message_duration_required": "Sesli mesaj ekleri bir süre belirtmeli.","calls_and_voice.voice_message_waveform_required": "Sesli mesaj ekleri dalga biçimi verilerini içermeli.","calls_and_voice.voice_messages_require_single_attachment": "Sesli mesajlar tam olarak bir ek içermeli.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Doğrulama başarısız oldu. Tekrar dene.","captcha.required": "Doğrulama gerekli. Tekrar dene.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK kanalı bu toplulukta olmalı.",diff --git a/packages/errors/src/i18n/weblate/locales/uk.json b/packages/errors/src/i18n/weblate/locales/uk.jsonindex e3998fe3d..ac6377252 100644--- a/packages/errors/src/i18n/weblate/locales/uk.json+++ b/packages/errors/src/i18n/weblate/locales/uk.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Невідповідність області ключа потоку.","calls_and_voice.unknown_voice_region": "Невідомий голосовий регіон.","calls_and_voice.unknown_voice_server": "Невідомий голосовий сервер.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Тривалість голосового повідомлення не може перевищувати {maxDuration} {maxDuration, plural, one {секунду} few {секунди} many {секунд} other {секунди}}.","calls_and_voice.voice_message_duration_required": "У вкладеннях голосових повідомлень має бути вказана тривалість.","calls_and_voice.voice_message_waveform_required": "Вкладення голосових повідомлень мають містити дані форми хвилі.","calls_and_voice.voice_messages_require_single_attachment": "Голосові повідомлення мають містити рівно одне вкладення.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Перевірку не пройдено. Спробуй ще раз.","captcha.required": "Потрібна перевірка. Спробуй ще раз.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK-канал має належати цій спільноті.",diff --git a/packages/errors/src/i18n/weblate/locales/vi.json b/packages/errors/src/i18n/weblate/locales/vi.jsonindex fda2365e4..9901c04b4 100644--- a/packages/errors/src/i18n/weblate/locales/vi.json+++ b/packages/errors/src/i18n/weblate/locales/vi.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Phạm vi khóa stream không khớp.","calls_and_voice.unknown_voice_region": "Vùng thoại không xác định.","calls_and_voice.unknown_voice_server": "Máy chủ thoại không xác định.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Thời lượng tin nhắn thoại không được vượt quá {maxDuration} {maxDuration, plural, other {giây}}.","calls_and_voice.voice_message_duration_required": "Tệp đính kèm tin nhắn thoại phải có thời lượng.","calls_and_voice.voice_message_waveform_required": "Tệp đính kèm tin nhắn thoại phải bao gồm dữ liệu dạng sóng.","calls_and_voice.voice_messages_require_single_attachment": "Tin nhắn thoại phải chứa chính xác một tệp đính kèm.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Xác minh thất bại. Hãy thử lại.","captcha.required": "Cần xác minh. Hãy thử lại.","channels_and_guilds.afk_channel_must_be_in_guild": "Kênh AFK phải thuộc cộng đồng này.",diff --git a/packages/errors/src/i18n/weblate/locales/zh-CN.json b/packages/errors/src/i18n/weblate/locales/zh-CN.jsonindex cd0615ed4..cd1102be8 100644--- a/packages/errors/src/i18n/weblate/locales/zh-CN.json+++ b/packages/errors/src/i18n/weblate/locales/zh-CN.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "推流密钥范围不匹配。","calls_and_voice.unknown_voice_region": "未知语音区域。","calls_and_voice.unknown_voice_server": "未知语音服务器。",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "语音消息时长不能超过 {maxDuration, plural, other {# 秒}}。","calls_and_voice.voice_message_duration_required": "语音消息附件必须指定时长。","calls_and_voice.voice_message_waveform_required": "语音消息附件必须包含波形数据。","calls_and_voice.voice_messages_require_single_attachment": "语音消息必须仅包含一个附件。",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "验证失败。请重试。","captcha.required": "需要验证。请重试。","channels_and_guilds.afk_channel_must_be_in_guild": "AFK 频道必须属于此社区。",diff --git a/packages/errors/src/i18n/weblate/locales/zh-TW.json b/packages/errors/src/i18n/weblate/locales/zh-TW.jsonindex 955851c05..fbddbadf2 100644--- a/packages/errors/src/i18n/weblate/locales/zh-TW.json+++ b/packages/errors/src/i18n/weblate/locales/zh-TW.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "串流金鑰範圍不符。","calls_and_voice.unknown_voice_region": "不明語音地區。","calls_and_voice.unknown_voice_server": "不明語音伺服器。",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "語音訊息長度無法超過 {maxDuration, plural, other {# 秒}}。","calls_and_voice.voice_message_duration_required": "語音訊息附件必須設定持續時間。","calls_and_voice.voice_message_waveform_required": "語音訊息附件必須包含波形資料。","calls_and_voice.voice_messages_require_single_attachment": "語音訊息必須只包含一個附件。",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "驗證失敗。請再試一次。","captcha.required": "需要驗證。請再試一次。","channels_and_guilds.afk_channel_must_be_in_guild": "AFK 頻道必須屬於此社群。",diff --git a/packages/errors/src/i18n/weblate/messages.json b/packages/errors/src/i18n/weblate/messages.jsonindex cb44f1e24..89c4a64ce 100644--- a/packages/errors/src/i18n/weblate/messages.json+++ b/packages/errors/src/i18n/weblate/messages.json@@ -205,10 +205,20 @@"calls_and_voice.stream_key_scope_mismatch": "Stream key scope mismatch.","calls_and_voice.unknown_voice_region": "Unknown voice region.","calls_and_voice.unknown_voice_server": "Unknown voice server.",+ "calls_and_voice.voice_join_endpoint_invalid": "The voice server endpoint isn't allowed.",+ "calls_and_voice.voice_join_endpoint_unreachable": "Fluxer couldn't connect to the voice server endpoint.",+ "calls_and_voice.voice_join_key_mismatch": "The voice server rejected its API key and secret.",+ "calls_and_voice.voice_join_token_busy": "Another voice server is joining with this token right now.",+ "calls_and_voice.voice_join_token_invalid": "The voice join token is not valid.","calls_and_voice.voice_message_duration_exceeds_limit": "Voice message duration can't exceed {maxDuration, plural, one {# second} other {# seconds}}.","calls_and_voice.voice_message_duration_required": "Voice message attachments must specify a duration.","calls_and_voice.voice_message_waveform_required": "Voice message attachments must include waveform data.","calls_and_voice.voice_messages_require_single_attachment": "Voice messages must contain exactly one attachment.",+ "calls_and_voice.voice_node_signature_invalid": "The voice server's signature doesn't match its secret.",+ "calls_and_voice.voice_node_token_expired": "The voice server's request is expired or not valid yet. Check the clock on that machine.",+ "calls_and_voice.voice_node_unknown": "No voice server has this API key.",+ "calls_and_voice.voice_server_api_key_in_use": "Another voice server already uses this API key.",+ "calls_and_voice.voice_server_id_in_use": "Another voice server already uses this ID.","captcha.invalid": "Verification failed. Try again.","captcha.required": "Verification required. Try again.","channels_and_guilds.afk_channel_must_be_in_guild": "AFK channel must be in this community.",diff --git a/packages/schema/src/domains/admin/AdminVoiceSchemas.ts b/packages/schema/src/domains/admin/AdminVoiceSchemas.tsindex 05230ed05..2a2663fe8 100644--- a/packages/schema/src/domains/admin/AdminVoiceSchemas.ts+++ b/packages/schema/src/domains/admin/AdminVoiceSchemas.ts@@ -23,6 +23,11 @@ function areServerCoordinatesPaired(const SoftConnectionLimitType = z.number().int().min(1).max(2147483647).nullable();+export const VoiceInternalEndpointType = z+ .url({protocol: /^https?$/})+ .max(512)+ .describe('URL the API uses for the control API and health checks of this server, such as http://voice-fra:7880');+const VoiceServerNotesType = z.string().trim()@@ -30,6 +35,14 @@ const VoiceServerNotesType = z.nullable().transform((value) => value || null);+export const VoiceServerHealthResponse = z.object({+ status: z.enum(['up', 'down', 'unknown']).describe('up, down, or unknown before the first check'),+ checked_at: z.string().nullable().describe('ISO 8601 timestamp of the last check, or null before the first'),+ error: z.string().nullable().describe('Status code or short transport error of the last failed check'),+});++export type VoiceServerHealthResponse = z.infer<typeof VoiceServerHealthResponse>;+export const VoiceRegionAdminResponse = z.object({id: z.string().describe('Unique identifier for the voice region'),name: z.string().describe('Display name of the voice region'),@@ -62,6 +75,13 @@ export const VoiceServerAdminResponse = z.object({required_guild_features: z.array(z.string()).max(100).describe('Guild features required to use this server'),allowed_guild_ids: z.array(SnowflakeStringType).max(1000).describe('Guild IDs explicitly allowed to use this server'),allowed_user_ids: z.array(SnowflakeStringType).max(1000).describe('User IDs explicitly allowed to use this server'),+ internal_endpoint: z+ .string()+ .nullable()+ .describe('URL the API uses for the control API and health checks, or null to use the endpoint'),+ draining: z.boolean().describe('Whether placement sends new calls elsewhere while calls on this server finish'),+ health: VoiceServerHealthResponse.describe('Health of the server as this API node last saw it'),+ duplicate_api_key: z.boolean().describe('Whether another server uses the same API key'),created_at: z.string().nullable().describe('ISO 8601 timestamp when the server was created'),updated_at: z.string().nullable().describe('ISO 8601 timestamp when the server was last updated'),});@@ -165,6 +185,12 @@ export const CreateVoiceServerRequestBody = z.optional().default([]).describe('User IDs explicitly allowed to use this server'),+ internal_endpoint: VoiceInternalEndpointType.nullable().optional().default(null),+ draining: z+ .boolean()+ .optional()+ .default(false)+ .describe('Whether placement sends new calls elsewhere while calls on this server finish'),}).refine((data) => areServerCoordinatesPaired(data.latitude, data.longitude), {error: 'Latitude and longitude must both be provided or both be omitted',@@ -205,6 +231,11 @@ export const UpdateVoiceServerRequestBody = z.max(1000).optional().describe('User IDs explicitly allowed to use this server'),+ internal_endpoint: VoiceInternalEndpointType.nullable().optional(),+ draining: z+ .boolean()+ .optional()+ .describe('Whether placement sends new calls elsewhere while calls on this server finish'),}).refine((data) => areServerCoordinatesPaired(data.latitude, data.longitude), {error: 'Latitude and longitude must both be provided or both be omitted',@@ -337,3 +368,115 @@ export const GetVoiceRegionQuery = z.object({});export type GetVoiceRegionQuery = z.infer<typeof GetVoiceRegionQuery>;++const VoiceIdType = z+ .string()+ .min(1)+ .max(64)+ .regex(/^[a-z0-9][a-z0-9-]*$/, {error: 'Use lowercase letters, digits and dashes'});++export const VoiceJoinTokenStatus = z.enum(['pending', 'used', 'expired', 'revoked']);++export const CreateVoiceJoinTokenRequest = z.object({+ region_id: VoiceIdType.nullable()+ .optional()+ .default(null)+ .describe('Region for the new server, or null to let the API pick the nearest existing region'),+ region_name: createStringType(1, 100)+ .nullable()+ .optional()+ .default(null)+ .describe('Name for a new region with the id in region_id, created when the server joins'),+ region_emoji: createStringType(1, 64)+ .nullable()+ .optional()+ .default(null)+ .describe('Emoji for the new region in region_name'),+ region_latitude: z+ .number()+ .min(-90)+ .max(90)+ .nullable()+ .optional()+ .default(null)+ .describe('Latitude of the new region in region_name. Placement sends people to the nearest region'),+ region_longitude: z+ .number()+ .min(-180)+ .max(180)+ .nullable()+ .optional()+ .default(null)+ .describe('Longitude of the new region in region_name'),+ server_id: VoiceIdType.nullable().optional().default(null).describe('Id for the new server'),+ hostname: z+ .string()+ .trim()+ .max(253)+ .regex(/^[A-Za-z0-9.-]+$/)+ .nullable()+ .optional()+ .default(null)+ .describe('Hostname the server must use for its endpoint'),+ internal_endpoint: VoiceInternalEndpointType.nullable().optional().default(null),+ soft_connection_limit: SoftConnectionLimitType.optional().default(null),+ notes: VoiceServerNotesType.optional().default(null),+ vip_only: z.boolean().optional().default(false),+ required_guild_features: z.array(createStringType(1, 64)).max(100).optional().default([]),+ allowed_guild_ids: z.array(SnowflakeType).max(1000).optional().default([]),+ allowed_user_ids: z.array(SnowflakeType).max(1000).optional().default([]),+ expires_in_seconds: z+ .number()+ .int()+ .min(60)+ .max(86400)+ .optional()+ .default(3600)+ .describe('Seconds until the token stops working, from 60 to 86400'),+});++export type CreateVoiceJoinTokenRequest = z.infer<typeof CreateVoiceJoinTokenRequest>;++export const CreateVoiceJoinTokenResponse = z.object({+ token_id: SnowflakeStringType.describe('ID of the token'),+ token: z.string().describe('The token. It appears in this response only'),+ expires_at: z.string().describe('ISO 8601 timestamp when the token stops working'),+ command: z.string().describe('Command to run on the voice machine'),+});++export type CreateVoiceJoinTokenResponse = z.infer<typeof CreateVoiceJoinTokenResponse>;++export const VoiceJoinTokenAdminResponse = z.object({+ token_id: SnowflakeStringType.describe('ID of the token'),+ status: VoiceJoinTokenStatus.describe('pending, used, expired or revoked'),+ created_at: z.string().describe('ISO 8601 timestamp when the token was created'),+ expires_at: z.string().describe('ISO 8601 timestamp when the token stops working'),+ used_at: z.string().nullable().describe('ISO 8601 timestamp when a server joined with it'),+ used_server_id: z.string().nullable().describe('ID of the server that joined with it'),+ revoked_at: z.string().nullable().describe('ISO 8601 timestamp when it was revoked'),+ region_id: z.string().nullable().describe('Region preset, or null'),+ region_name: z.string().nullable().describe('Name of the new region it creates, or null'),+ server_id: z.string().nullable().describe('Server id preset, or null'),+ hostname: z.string().nullable().describe('Hostname preset, or null'),+ notes: z.string().nullable().describe('Notes for the new server, or null'),+});++export type VoiceJoinTokenAdminResponse = z.infer<typeof VoiceJoinTokenAdminResponse>;++export const ListVoiceJoinTokensResponse = z.object({+ tokens: z.array(VoiceJoinTokenAdminResponse).describe('Join tokens, newest first'),+});++export type ListVoiceJoinTokensResponse = z.infer<typeof ListVoiceJoinTokensResponse>;++export const VoiceJoinTokenIdParam = z.object({+ token_id: SnowflakeType.describe('ID of the join token'),+});++export const CreateVoiceServerTestTokenResponse = z.object({+ token: z.string().describe('LiveKit token for a hidden audio only participant, valid for 60 seconds'),+ endpoint: z.string().describe('Endpoint of the server'),+ room: z.string().describe('Name of the throwaway test room'),+});++export type CreateVoiceServerTestTokenResponse = z.infer<typeof CreateVoiceServerTestTokenResponse>;diff --git a/packages/schema/src/domains/instance/InstanceSchemas.ts b/packages/schema/src/domains/instance/InstanceSchemas.tsindex 18baa31c1..a2f136157 100644--- a/packages/schema/src/domains/instance/InstanceSchemas.ts+++ b/packages/schema/src/domains/instance/InstanceSchemas.ts@@ -425,6 +425,11 @@ export const WellKnownFluxerResponse = z.object({.max(MAX_CUSTOM_BADGES_PER_TYPE).optional().describe('Custom badges that communities on this instance can show next to their name, in display order'),+ voice_origins: z+ .array(z.string())+ .max(1000)+ .optional()+ .describe('Origins of every voice server endpoint, such as wss://voice.example.com, for content security policies'),});export type WellKnownFluxerResponse = z.infer<typeof WellKnownFluxerResponse>;diff --git a/packages/schema/src/domains/voice/VoiceNodeSchemas.ts b/packages/schema/src/domains/voice/VoiceNodeSchemas.tsnew file mode 100644index 000000000..e57c208ef--- /dev/null+++ b/packages/schema/src/domains/voice/VoiceNodeSchemas.ts@@ -0,0 +1,109 @@+// SPDX-License-Identifier: AGPL-3.0-or-later++import {VoiceServerHealthResponse} from '@fluxer/schema/src/domains/admin/AdminVoiceSchemas';+import {createStringType} from '@fluxer/schema/src/primitives/SchemaPrimitives';+import {z} from 'zod';++const VoiceIdType = z+ .string()+ .min(1)+ .max(64)+ .regex(/^[a-z0-9][a-z0-9-]*$/, {error: 'Use lowercase letters, digits and dashes'});++const LatitudeType = z.number().min(-90).max(90);+const LongitudeType = z.number().min(-180).max(180);+const SoftLimitType = z.number().int().min(1).max(2147483647);++export const VoiceJoinPreviewRequest = z.object({+ public_ipv4: z.ipv4().nullable().optional().describe('Public IPv4 address of the voice machine'),+ region_id: VoiceIdType.optional().describe('Existing region the operator asked for'),+ cpu_count: z.number().int().min(1).max(4096).optional().describe('CPU count of the voice machine'),+ memory_bytes: z.number().int().min(0).optional().describe('Memory of the voice machine in bytes'),+ rexctl_version: z.string().max(64).optional().describe('Version of rexctl on the voice machine'),+});++export type VoiceJoinPreviewRequest = z.infer<typeof VoiceJoinPreviewRequest>;++export const VoiceJoinPreviewResponse = z.object({+ server_id: z.string().describe('Suggested server id, or the preset of the token'),+ server_id_pinned: z.boolean().describe('Whether the token sets the server id, which the join then always uses'),+ region: z.object({+ id: z.string().describe('Region id'),+ name: z.string().describe('Region name'),+ distance_km: z.number().nullable().describe('Distance from the voice machine, or null without GeoIP data'),+ new: z.boolean().describe('Whether the region is created when the server joins'),+ source: z+ .enum(['token', 'requested', 'nearest', 'default'])+ .describe('Why Fluxer picked the region, from the token, region_id, the nearest region or the default region'),+ }),+ latitude: z.number().nullable().describe('Server latitude from GeoIP, rounded to 0.1 degrees'),+ longitude: z.number().nullable().describe('Server longitude from GeoIP, rounded to 0.1 degrees'),+ hostname: z.string().nullable().describe('Hostname preset of the token, or null'),+ suggested_hostname: z.string().describe('A hostname under the instance domain the operator may use'),+ soft_connection_limit: z.number().nullable().describe('Soft limit preset of the token, or null'),+ webhook_url: z.string().describe('URL the server sends LiveKit webhooks to'),+ api_code_version: z.number().int().describe('Version of the API server code'),+ release: z.string().describe('Fluxer release of the API, or an empty string for a development build'),+ expires_at: z.string().describe('ISO 8601 timestamp when the token stops working'),+});++export type VoiceJoinPreviewResponse = z.infer<typeof VoiceJoinPreviewResponse>;++export const VoiceJoinRequest = z+ .object({+ server_id: VoiceIdType.optional().describe('Server id, ignored when the token has a preset'),+ region_id: VoiceIdType.optional().describe('Existing region, ignored when the token has a preset'),+ endpoint: z.url({protocol: /^wss$/}).max(512).describe('Public wss endpoint of the server'),+ api_key: createStringType(1, 256).describe('LiveKit API key of the server'),+ api_secret: createStringType(32, 256).describe('LiveKit API secret of the server'),+ latitude: LatitudeType.optional(),+ longitude: LongitudeType.optional(),+ soft_connection_limit: SoftLimitType.optional(),+ public_ipv4: z.ipv4().optional().describe('Public IPv4 address of the voice machine'),+ })+ .refine((data) => (data.latitude === undefined) === (data.longitude === undefined), {+ error: 'Latitude and longitude must both be provided or both be omitted',+ path: ['latitude'],+ });++export type VoiceJoinRequest = z.infer<typeof VoiceJoinRequest>;++export const VoiceJoinResponse = z.object({+ server_id: z.string().describe('ID of the new server'),+ region_id: z.string().describe('Region of the new server'),+ endpoint: z.string().describe('Endpoint of the new server'),+ webhook_url: z.string().describe('URL the server sends LiveKit webhooks to'),+ node_url: z.string().describe('URL of the node routes for this server'),+});++export type VoiceJoinResponse = z.infer<typeof VoiceJoinResponse>;++export const VoiceNodeResponse = z.object({+ region_id: z.string().describe('Region of the server'),+ server_id: z.string().describe('ID of the server'),+ endpoint: z.string().describe('Endpoint of the server'),+ latitude: z.number().nullable().describe('Server latitude'),+ longitude: z.number().nullable().describe('Server longitude'),+ is_active: z.boolean().describe('Whether an admin keeps the server turned on'),+ draining: z.boolean().describe('Whether placement sends new calls elsewhere'),+ soft_connection_limit: z.number().nullable().describe('Soft connection limit, or null'),+ health: VoiceServerHealthResponse.describe('Health of the server as this API node last saw it'),+});++export type VoiceNodeResponse = z.infer<typeof VoiceNodeResponse>;++export const UpdateVoiceNodeRequest = z+ .object({+ endpoint: z.url({protocol: /^wss$/}).max(512).optional().describe('New public wss endpoint'),+ draining: z.boolean().optional().describe('Whether placement sends new calls elsewhere'),+ latitude: LatitudeType.nullable().optional(),+ longitude: LongitudeType.nullable().optional(),+ soft_connection_limit: SoftLimitType.nullable().optional(),+ })+ .strict()+ .refine((data) => (data.latitude === undefined) === (data.longitude === undefined), {+ error: 'Latitude and longitude must both be provided or both be omitted',+ path: ['latitude'],+ });++export type UpdateVoiceNodeRequest = z.infer<typeof UpdateVoiceNodeRequest>;diff --git a/tools/dev/cassandra_target_schema.json b/tools/dev/cassandra_target_schema.jsonindex 2ee246ad4..dc176606e 100644--- a/tools/dev/cassandra_target_schema.json+++ b/tools/dev/cassandra_target_schema.json@@ -10422,11 +10422,134 @@{"name": "notes","type": "text"+ },+ {+ "name": "internal_endpoint",+ "type": "text"+ },+ {+ "name": "draining",+ "type": "boolean"+ },+ {+ "name": "pinned_hostname",+ "type": "text"+ },+ {+ "name": "pinned_soft_limit",+ "type": "boolean"}],"primary_key": "((region_id), server_id)","options": ""},+ {+ "name": "voice_join_tokens",+ "columns": [+ {+ "name": "token_id",+ "type": "bigint"+ },+ {+ "name": "secret_hash",+ "type": "text"+ },+ {+ "name": "created_by",+ "type": "bigint"+ },+ {+ "name": "created_at",+ "type": "timestamp"+ },+ {+ "name": "expires_at",+ "type": "timestamp"+ },+ {+ "name": "claim_id",+ "type": "text"+ },+ {+ "name": "claimed_at",+ "type": "timestamp"+ },+ {+ "name": "used_at",+ "type": "timestamp"+ },+ {+ "name": "used_by_ip",+ "type": "text"+ },+ {+ "name": "used_server_id",+ "type": "text"+ },+ {+ "name": "revoked_at",+ "type": "timestamp"+ },+ {+ "name": "region_id",+ "type": "text"+ },+ {+ "name": "region_name",+ "type": "text"+ },+ {+ "name": "region_emoji",+ "type": "text"+ },+ {+ "name": "region_latitude",+ "type": "double"+ },+ {+ "name": "region_longitude",+ "type": "double"+ },+ {+ "name": "server_id",+ "type": "text"+ },+ {+ "name": "hostname",+ "type": "text"+ },+ {+ "name": "internal_endpoint",+ "type": "text"+ },+ {+ "name": "soft_connection_limit",+ "type": "int"+ },+ {+ "name": "notes",+ "type": "text"+ },+ {+ "name": "vip_only",+ "type": "boolean"+ },+ {+ "name": "required_guild_features",+ "type": "set<text>"+ },+ {+ "name": "allowed_guild_ids",+ "type": "set<bigint>"+ },+ {+ "name": "allowed_user_ids",+ "type": "set<bigint>"+ }+ ],+ "primary_key": "((token_id))",+ "options": ""+ },{"name": "voice_p2p_connection_reports","columns": [