Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

9.9 KB
Raw Blame
package admin

import (
	"encoding/json"
	"net/http"

	log "github.com/sirupsen/logrus"

	"github.com/owncast/owncast/models"
	"github.com/owncast/owncast/services/transcoder"
	"github.com/owncast/owncast/utils"
	"github.com/owncast/owncast/webserver/router/middleware"
)

// GetServerConfig gets the config details of the server.
func (a *Admin) GetServerConfig(w http.ResponseWriter, r *http.Request) {
	configRepository := a.configRepository
	ffmpeg := utils.ValidatedFfmpegPath(configRepository.GetFfMpegPath())
	usernameBlocklist := configRepository.GetForbiddenUsernameList()
	usernameSuggestions := configRepository.GetSuggestedUsernamesList()

	streamOutputVariants := configRepository.GetStreamOutputVariants()
	videoQualityVariants := make([]models.StreamOutputVariant, 0, len(streamOutputVariants))
	for _, variant := range streamOutputVariants {
		videoQualityVariants = append(videoQualityVariants, models.StreamOutputVariant{
			Name:               variant.GetName(),
			IsAudioPassthrough: variant.GetIsAudioPassthrough(),
			IsVideoPassthrough: variant.IsVideoPassthrough,
			Framerate:          variant.GetFramerate(),
			VideoBitrate:       variant.VideoBitrate,
			AudioBitrate:       variant.AudioBitrate,
			CPUUsageLevel:      variant.CPUUsageLevel,
			ScaledWidth:        variant.ScaledWidth,
			ScaledHeight:       variant.ScaledHeight,
		})
	}
	response := serverConfigAdminResponse{
		InstanceDetails: webConfigResponse{
			Name:                configRepository.GetServerName(),
			Summary:             configRepository.GetServerSummary(),
			Tags:                configRepository.GetServerMetadataTags(),
			ExtraPageContent:    configRepository.GetExtraPageBodyContent(),
			StreamTitle:         configRepository.GetStreamTitle(),
			WelcomeMessage:      configRepository.GetServerWelcomeMessage(),
			OfflineMessage:      configRepository.GetCustomOfflineMessage(),
			Logo:                configRepository.GetLogoPath(),
			SocialHandles:       configRepository.GetSocialHandles(),
			NSFW:                configRepository.GetNSFW(),
			Autoplay:            configRepository.GetAutoplay(),
			CustomStyles:        configRepository.GetCustomStyles(),
			CustomJavascript:    configRepository.GetCustomJavascript(),
			AppearanceVariables: configRepository.GetCustomColorVariableValues(),
		},
		FFmpegPath:                ffmpeg,
		AdminPassword:             configRepository.GetAdminPassword(),
		StreamKeys:                configRepository.GetStreamKeys(),
		StreamKeyOverridden:       a.cfg.TemporaryStreamKey != "",
		WebServerPort:             a.cfg.WebServerPort,
		WebServerIP:               a.cfg.WebServerIP,
		RTMPServerPort:            configRepository.GetRTMPPortNumber(),
		RTMPServerAddress:         configRepository.GetRTMPBindAddress(),
		ChatDisabled:              configRepository.GetChatDisabled(),
		ChatJoinMessagesEnabled:   configRepository.GetChatJoinPartMessagesEnabled(),
		SocketHostOverride:        configRepository.GetWebsocketOverrideHost(),
		VideoServingEndpoint:      configRepository.GetVideoServingEndpoint(),
		ChatEstablishedUserMode:   configRepository.GetChatEstbalishedUsersOnlyMode(),
		ChatSpamProtectionEnabled: configRepository.GetChatSpamProtectionEnabled(),
		ChatSlurFilterEnabled:     configRepository.GetChatSlurFilterEnabled(),
		ChatRequireAuthentication: configRepository.GetChatRequireAuthentication(),
		HideViewerCount:           configRepository.GetHideViewerCount(),
		DisableSearchIndexing:     configRepository.GetDisableSearchIndexing(),
		VideoSettings: videoSettings{
			VideoQualityVariants: videoQualityVariants,
			LatencyLevel:         configRepository.GetStreamLatencyLevel().Level,
		},
		YP: yp{
			Enabled:     configRepository.GetDirectoryEnabled(),
			InstanceURL: configRepository.GetServerURL(),
		},
		S3:                 configRepository.GetS3Config(),
		ExternalActions:    configRepository.GetExternalActions(),
		StyleContributors:  a.styleContributorsOrEmpty(),
		SupportedCodecs:    transcoder.GetCodecs(ffmpeg),
		VideoCodec:         configRepository.GetVideoCodec(),
		ForbiddenUsernames: usernameBlocklist,
		SuggestedUsernames: usernameSuggestions,
		Federation: federationConfigResponse{
			Enabled:          configRepository.GetFederationEnabled(),
			IsPrivate:        configRepository.GetFederationIsPrivate(),
			Username:         configRepository.GetFederationUsername(),
			GoLiveMessage:    configRepository.GetFederationGoLiveMessage(),
			ShowEngagement:   configRepository.GetFederationShowEngagement(),
			EnableQuotes:     configRepository.GetFederationEnableQuotes(),
			HideFollowersTab: configRepository.GetFederationHideFollowersTab(),
			BlockedDomains:   configRepository.GetBlockedFederatedDomains(),
		},
		Notifications: notificationsConfigResponse{
			Discord: configRepository.GetDiscordConfig(),
			Browser: configRepository.GetBrowserPushConfig(),
		},
	}

	w.Header().Set("Content-Type", "application/json")
	middleware.DisableCache(w)

	if err := json.NewEncoder(w).Encode(response); err != nil {
		log.Errorln(err)
	}
}

// styleContributorsOrEmpty returns the plugin page-styling report, or
// an empty (non-nil) slice when the getter is unset (no plugin host) or
// returns nothing. The empty-slice contract keeps the JSON wire shape
// stable: `styleContributors: []` rather than null, so the admin UI
// doesn't need a defensive nil-check before iterating.
func (a *Admin) styleContributorsOrEmpty() []models.PluginStyleInfo {
	if a.pluginStyleContributors == nil {
		return []models.PluginStyleInfo{}
	}
	if c := a.pluginStyleContributors(); len(c) > 0 {
		return c
	}
	return []models.PluginStyleInfo{}
}

type serverConfigAdminResponse struct {
	InstanceDetails           webConfigResponse           `json:"instanceDetails"`
	Notifications             notificationsConfigResponse `json:"notifications"`
	YP                        yp                          `json:"yp"`
	FFmpegPath                string                      `json:"ffmpegPath"`
	AdminPassword             string                      `json:"adminPassword"`
	SocketHostOverride        string                      `json:"socketHostOverride,omitempty"`
	WebServerIP               string                      `json:"webServerIP"`
	VideoCodec                string                      `json:"videoCodec"`
	VideoServingEndpoint      string                      `json:"videoServingEndpoint"`
	S3                        models.S3                   `json:"s3"`
	Federation                federationConfigResponse    `json:"federation"`
	SupportedCodecs           []string                    `json:"supportedCodecs"`
	ExternalActions           []models.ExternalAction     `json:"externalActions"`
	StyleContributors         []models.PluginStyleInfo    `json:"styleContributors"`
	ForbiddenUsernames        []string                    `json:"forbiddenUsernames"`
	SuggestedUsernames        []string                    `json:"suggestedUsernames"`
	StreamKeys                []models.StreamKey          `json:"streamKeys"`
	VideoSettings             videoSettings               `json:"videoSettings"`
	RTMPServerPort            int                         `json:"rtmpServerPort"`
	RTMPServerAddress         string                      `json:"rtmpServerAddress"`
	WebServerPort             int                         `json:"webServerPort"`
	ChatDisabled              bool                        `json:"chatDisabled"`
	ChatJoinMessagesEnabled   bool                        `json:"chatJoinMessagesEnabled"`
	ChatEstablishedUserMode   bool                        `json:"chatEstablishedUserMode"`
	ChatSpamProtectionEnabled bool                        `json:"chatSpamProtectionEnabled"`
	ChatSlurFilterEnabled     bool                        `json:"chatSlurFilterEnabled"`
	ChatRequireAuthentication bool                        `json:"chatRequireAuthentication"`
	DisableSearchIndexing     bool                        `json:"disableSearchIndexing"`
	StreamKeyOverridden       bool                        `json:"streamKeyOverridden"`
	HideViewerCount           bool                        `json:"hideViewerCount"`
}

type videoSettings struct {
	VideoQualityVariants []models.StreamOutputVariant `json:"videoQualityVariants"`
	LatencyLevel         int                          `json:"latencyLevel"`
}

type webConfigResponse struct {
	AppearanceVariables map[string]string     `json:"appearanceVariables"`
	Version             string                `json:"version"`
	WelcomeMessage      string                `json:"welcomeMessage"`
	OfflineMessage      string                `json:"offlineMessage"`
	Logo                string                `json:"logo"`
	Name                string                `json:"name"`
	ExtraPageContent    string                `json:"extraPageContent"`
	StreamTitle         string                `json:"streamTitle"` // What's going on with the current stream
	CustomStyles        string                `json:"customStyles"`
	CustomJavascript    string                `json:"customJavascript"`
	Summary             string                `json:"summary"`
	Tags                []string              `json:"tags"`
	SocialHandles       []models.SocialHandle `json:"socialHandles"`
	NSFW                bool                  `json:"nsfw"`
	Autoplay            models.AutoplayMode   `json:"autoplay"`
}

type yp struct {
	InstanceURL  string `json:"instanceUrl"` // The public URL the directory should link to
	YPServiceURL string `json:"-"`           // The base URL to the YP API to register with (optional)
	Enabled      bool   `json:"enabled"`
}

type federationConfigResponse struct {
	Username         string   `json:"username"`
	GoLiveMessage    string   `json:"goLiveMessage"`
	BlockedDomains   []string `json:"blockedDomains"`
	Enabled          bool     `json:"enabled"`
	IsPrivate        bool     `json:"isPrivate"`
	ShowEngagement   bool     `json:"showEngagement"`
	EnableQuotes     bool     `json:"enableQuotes"`
	HideFollowersTab bool     `json:"hideFollowersTab"`
}

type notificationsConfigResponse struct {
	Browser models.BrowserNotificationConfiguration `json:"browser"`
	Discord models.DiscordConfiguration             `json:"discord"`
}