From c4dc80294147a510c5c5c6ddfb9a791119ffc525 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Wed, 23 Nov 2022 16:39:55 -0800 Subject: [PATCH] Make stream keys objects with comment instead of string slice --- config/defaults.go | 6 ++++-- controllers/admin/config.go | 17 +++++++++-------- controllers/admin/serverConfig.go | 2 +- core/data/config.go | 20 +++++++++++++++----- core/data/datastoreMigrations.go | 5 ++++- core/rtmp/rtmp.go | 2 +- models/streamKey.go | 7 +++++++ test/automated/api/configmanagement.test.js | 6 +++++- 8 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 models/streamKey.go diff --git a/config/defaults.go b/config/defaults.go index 93e27fb33..9c3fbe384 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -21,7 +21,7 @@ type Defaults struct { WebServerIP string RTMPServerPort int AdminPassword string - StreamKeys []string + StreamKeys []models.StreamKey YPEnabled bool YPServer string @@ -44,7 +44,9 @@ func GetDefaults() Defaults { ServerWelcomeMessage: "", Logo: "logo.svg", AdminPassword: "abc123", - StreamKeys: []string{"abc123"}, + StreamKeys: []models.StreamKey{ + {Key: "abc123", Comment: "Default stream key"}, + }, Tags: []string{ "owncast", "streaming", diff --git a/controllers/admin/config.go b/controllers/admin/config.go index 13ad19e4c..1e42edd3f 100644 --- a/controllers/admin/config.go +++ b/controllers/admin/config.go @@ -796,17 +796,18 @@ func SetStreamKeys(w http.ResponseWriter, r *http.Request) { return } - configValues, success := getValuesFromRequest(w, r) - if !success { + type streamKeysRequest struct { + Value []models.StreamKey `json:"value"` + } + + decoder := json.NewDecoder(r.Body) + var streamKeys streamKeysRequest + if err := decoder.Decode(&streamKeys); err != nil { + controllers.WriteSimpleResponse(w, false, "unable to update stream keys with provided values") return } - streamKeyStrings := make([]string, 0) - for _, key := range configValues { - streamKeyStrings = append(streamKeyStrings, key.Value.(string)) - } - - if err := data.SetStreamKeys(streamKeyStrings); err != nil { + if err := data.SetStreamKeys(streamKeys.Value); err != nil { controllers.WriteSimpleResponse(w, false, err.Error()) return } diff --git a/controllers/admin/serverConfig.go b/controllers/admin/serverConfig.go index d2e8f12c6..5785190f9 100644 --- a/controllers/admin/serverConfig.go +++ b/controllers/admin/serverConfig.go @@ -100,7 +100,7 @@ type serverConfigAdminResponse struct { InstanceDetails webConfigResponse `json:"instanceDetails"` FFmpegPath string `json:"ffmpegPath"` AdminPassword string `json:"adminPassword"` - StreamKeys []string `json:"streamKeys"` + StreamKeys []models.StreamKey `json:"streamKeys"` WebServerPort int `json:"webServerPort"` WebServerIP string `json:"webServerIP"` RTMPServerPort int `json:"rtmpServerPort"` diff --git a/core/data/config.go b/core/data/config.go index 13f88dfb0..b77d1756c 100644 --- a/core/data/config.go +++ b/core/data/config.go @@ -946,12 +946,22 @@ func GetCustomColorVariableValues() map[string]string { } // GetStreamKeys will return valid stream keys. -func GetStreamKeys() []string { - keys, _ := _datastore.GetStringSlice(streamKeysKey) - return keys +func GetStreamKeys() []models.StreamKey { + configEntry, err := _datastore.Get(streamKeysKey) + if err != nil { + return []models.StreamKey{} + } + + var streamKeys []models.StreamKey + if err := configEntry.getObject(&streamKeys); err != nil { + return []models.StreamKey{} + } + + return streamKeys } // SetStreamKeys will set valid stream keys. -func SetStreamKeys(keys []string) error { - return _datastore.SetStringSlice(streamKeysKey, keys) +func SetStreamKeys(actions []models.StreamKey) error { + configEntry := ConfigEntry{Key: streamKeysKey, Value: actions} + return _datastore.Save(configEntry) } diff --git a/core/data/datastoreMigrations.go b/core/data/datastoreMigrations.go index 26086f8e8..b63df7a61 100644 --- a/core/data/datastoreMigrations.go +++ b/core/data/datastoreMigrations.go @@ -3,6 +3,7 @@ package data import ( "strings" + "github.com/owncast/owncast/models" log "github.com/sirupsen/logrus" ) @@ -56,5 +57,7 @@ func migrateToDatastoreValues1(datastore *Datastore) { func migrateToDatastoreValues2(datastore *Datastore) { oldAdminPassword, _ := datastore.GetString("stream_key") _ = SetAdminPassword(oldAdminPassword) - _ = SetStreamKeys([]string{oldAdminPassword}) + _ = SetStreamKeys([]models.StreamKey{ + {Key: oldAdminPassword, Comment: "Default stream key"}, + }) } diff --git a/core/rtmp/rtmp.go b/core/rtmp/rtmp.go index 5c0aef8d4..01d0fa38c 100644 --- a/core/rtmp/rtmp.go +++ b/core/rtmp/rtmp.go @@ -81,7 +81,7 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) { validStreamingKeys := data.GetStreamKeys() for _, key := range validStreamingKeys { - if secretMatch(key, c.URL.Path) { + if secretMatch(key.Key, c.URL.Path) { accessGranted = true break } diff --git a/models/streamKey.go b/models/streamKey.go new file mode 100644 index 000000000..0495f5b07 --- /dev/null +++ b/models/streamKey.go @@ -0,0 +1,7 @@ +package models + +// StreamKey represents a single stream key +type StreamKey struct { + Key string `json:"key"` + Comment string `json:"comment"` +} diff --git a/test/automated/api/configmanagement.test.js b/test/automated/api/configmanagement.test.js index fa56c2dea..43a3abfc1 100644 --- a/test/automated/api/configmanagement.test.js +++ b/test/automated/api/configmanagement.test.js @@ -7,7 +7,11 @@ const serverSummary = randomString(); const offlineMessage = randomString(); const pageContent = `

${randomString()}

`; const tags = [randomString(), randomString(), randomString()]; -const streamKeys = [randomString(), randomString(), randomString()]; +const streamKeys = [ + { key: randomString(), comment: 'test key 1' }, + { key: randomString(), comment: 'test key 1' }, + { key: randomString(), comment: 'test key 1' }, +]; const latencyLevel = Math.floor(Math.random() * 4); const appearanceValues = {