Make stream keys objects with comment instead of string slice

This commit is contained in:
Gabe Kangas 2022-11-23 16:39:55 -08:00
parent c9e3ccad45
commit c4dc802941
8 changed files with 46 additions and 19 deletions

View file

@ -21,7 +21,7 @@ type Defaults struct {
WebServerIP string WebServerIP string
RTMPServerPort int RTMPServerPort int
AdminPassword string AdminPassword string
StreamKeys []string StreamKeys []models.StreamKey
YPEnabled bool YPEnabled bool
YPServer string YPServer string
@ -44,7 +44,9 @@ func GetDefaults() Defaults {
ServerWelcomeMessage: "", ServerWelcomeMessage: "",
Logo: "logo.svg", Logo: "logo.svg",
AdminPassword: "abc123", AdminPassword: "abc123",
StreamKeys: []string{"abc123"}, StreamKeys: []models.StreamKey{
{Key: "abc123", Comment: "Default stream key"},
},
Tags: []string{ Tags: []string{
"owncast", "owncast",
"streaming", "streaming",

View file

@ -796,17 +796,18 @@ func SetStreamKeys(w http.ResponseWriter, r *http.Request) {
return return
} }
configValues, success := getValuesFromRequest(w, r) type streamKeysRequest struct {
if !success { 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 return
} }
streamKeyStrings := make([]string, 0) if err := data.SetStreamKeys(streamKeys.Value); err != nil {
for _, key := range configValues {
streamKeyStrings = append(streamKeyStrings, key.Value.(string))
}
if err := data.SetStreamKeys(streamKeyStrings); err != nil {
controllers.WriteSimpleResponse(w, false, err.Error()) controllers.WriteSimpleResponse(w, false, err.Error())
return return
} }

View file

@ -100,7 +100,7 @@ type serverConfigAdminResponse struct {
InstanceDetails webConfigResponse `json:"instanceDetails"` InstanceDetails webConfigResponse `json:"instanceDetails"`
FFmpegPath string `json:"ffmpegPath"` FFmpegPath string `json:"ffmpegPath"`
AdminPassword string `json:"adminPassword"` AdminPassword string `json:"adminPassword"`
StreamKeys []string `json:"streamKeys"` StreamKeys []models.StreamKey `json:"streamKeys"`
WebServerPort int `json:"webServerPort"` WebServerPort int `json:"webServerPort"`
WebServerIP string `json:"webServerIP"` WebServerIP string `json:"webServerIP"`
RTMPServerPort int `json:"rtmpServerPort"` RTMPServerPort int `json:"rtmpServerPort"`

View file

@ -946,12 +946,22 @@ func GetCustomColorVariableValues() map[string]string {
} }
// GetStreamKeys will return valid stream keys. // GetStreamKeys will return valid stream keys.
func GetStreamKeys() []string { func GetStreamKeys() []models.StreamKey {
keys, _ := _datastore.GetStringSlice(streamKeysKey) configEntry, err := _datastore.Get(streamKeysKey)
return keys 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. // SetStreamKeys will set valid stream keys.
func SetStreamKeys(keys []string) error { func SetStreamKeys(actions []models.StreamKey) error {
return _datastore.SetStringSlice(streamKeysKey, keys) configEntry := ConfigEntry{Key: streamKeysKey, Value: actions}
return _datastore.Save(configEntry)
} }

View file

@ -3,6 +3,7 @@ package data
import ( import (
"strings" "strings"
"github.com/owncast/owncast/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -56,5 +57,7 @@ func migrateToDatastoreValues1(datastore *Datastore) {
func migrateToDatastoreValues2(datastore *Datastore) { func migrateToDatastoreValues2(datastore *Datastore) {
oldAdminPassword, _ := datastore.GetString("stream_key") oldAdminPassword, _ := datastore.GetString("stream_key")
_ = SetAdminPassword(oldAdminPassword) _ = SetAdminPassword(oldAdminPassword)
_ = SetStreamKeys([]string{oldAdminPassword}) _ = SetStreamKeys([]models.StreamKey{
{Key: oldAdminPassword, Comment: "Default stream key"},
})
} }

View file

@ -81,7 +81,7 @@ func HandleConn(c *rtmp.Conn, nc net.Conn) {
validStreamingKeys := data.GetStreamKeys() validStreamingKeys := data.GetStreamKeys()
for _, key := range validStreamingKeys { for _, key := range validStreamingKeys {
if secretMatch(key, c.URL.Path) { if secretMatch(key.Key, c.URL.Path) {
accessGranted = true accessGranted = true
break break
} }

7
models/streamKey.go Normal file
View file

@ -0,0 +1,7 @@
package models
// StreamKey represents a single stream key
type StreamKey struct {
Key string `json:"key"`
Comment string `json:"comment"`
}

View file

@ -7,7 +7,11 @@ const serverSummary = randomString();
const offlineMessage = randomString(); const offlineMessage = randomString();
const pageContent = `<p>${randomString()}</p>`; const pageContent = `<p>${randomString()}</p>`;
const tags = [randomString(), randomString(), 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 latencyLevel = Math.floor(Math.random() * 4);
const appearanceValues = { const appearanceValues = {