From 4be18ea3a2e221e252a5e6e97b5d520bd28aa7f1 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Tue, 13 Jun 2023 21:57:31 -0700 Subject: [PATCH] Add TURN server support --- pkg/config/config.go | 15 ++++++++++++++- web/static/js/BareRTC.js | 15 ++++++++++++--- web/templates/chat.html | 1 + 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 37037de..882c7a4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -12,7 +12,7 @@ import ( // Version of the config format - when new fields are added, it will attempt // to write the settings.toml to disk so new defaults populate. -var currentVersion = 2 +var currentVersion = 3 // Config for your BareRTC app. type Config struct { @@ -37,9 +37,17 @@ type Config struct { MaxImageWidth int PreviewImageWidth int + TURN TurnConfig + PublicChannels []Channel } +type TurnConfig struct { + URLs []string + Username string + Credential string +} + // GetChannels returns a JavaScript safe array of the default PublicChannels. func (c Config) GetChannels() template.JS { data, _ := json.Marshal(c.PublicChannels) @@ -90,6 +98,11 @@ func DefaultConfig() Config { }, }, }, + TURN: TurnConfig{ + URLs: []string{ + "stun:stun.l.google.com:19302", + }, + }, } c.JWT.Strict = true return c diff --git a/web/static/js/BareRTC.js b/web/static/js/BareRTC.js index 8cc4722..5fd47ed 100644 --- a/web/static/js/BareRTC.js +++ b/web/static/js/BareRTC.js @@ -2,9 +2,18 @@ // WebRTC configuration. const configuration = { - iceServers: [{ - urls: 'stun:stun.l.google.com:19302' - }] + iceServers: TURN.URLs.map(val => { + let row = { + urls: val, + }; + + if (val.indexOf('turn:') === 0) { + row.username = TURN.Username; + row.credential = TURN.Credential; + } + + return row; + }) }; const FileUploadMaxSize = 1024 * 1024 * 8; // 8 MB diff --git a/web/templates/chat.html b/web/templates/chat.html index 69367fa..5736e3d 100644 --- a/web/templates/chat.html +++ b/web/templates/chat.html @@ -1041,6 +1041,7 @@ const PublicChannels = {{.Config.GetChannels}}; const WebsiteURL = "{{.Config.WebsiteURL}}"; const PermitNSFW = {{AsJS .Config.PermitNSFW}}; +const TURN = {{.Config.TURN}}; const UserJWTToken = {{.JWTTokenString}}; const UserJWTValid = {{if .JWTAuthOK}}true{{else}}false{{end}}; const UserJWTClaims = {{.JWTClaims.ToJSON}};