Null-DRM-Official/apps/streamd/servecmd/config.go
404errordeveloper 8fc7e4c0b8 streamd: serve start/stop/update, streamd.yaml, egress, module panel fixes
- Add streamd serve start|stop|restart|status|update with auto-written streamd.yaml
- Panel modules: STREAMD_MODULES_DIR for describe, YAML channel fallback, textarea value fix
- Per-stream WireGuard/SOCKS egress profiles + Settings UI
- Drop drm-* from release builds; track streamd-bg.sh / streamd-update.sh wrappers
- Docs: streamd-server-setup lifecycle and update flow
2026-10-10 18:05:51 +02:00

336 lines
8.8 KiB
Go

package servecmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v3"
"streamd/bootstrap"
)
const (
defaultBind = "127.0.0.1:8083"
defaultLogLevel = "info"
defaultLogFile = "streamd.log"
defaultPIDFile = "streamd.pid"
defaultChannel = "stable"
defaultKeepOld = 10
configFileName = "streamd.yaml"
releaseBaseURL = "https://git.nulldrm.com/nulldrm/Null-DRM-Official/releases/download"
)
// FileConfig is the on-disk streamd.yaml next to the binary.
type FileConfig struct {
Bind string `yaml:"bind,omitempty"`
Data string `yaml:"data,omitempty"`
LogLevel string `yaml:"log_level,omitempty"`
LogFile string `yaml:"log_file,omitempty"`
PIDFile string `yaml:"pid_file,omitempty"`
Channel string `yaml:"channel,omitempty"` // stable | beta
Asset string `yaml:"asset,omitempty"`
KeepOld *int `yaml:"keep_old,omitempty"`
TrustedProxies string `yaml:"trusted_proxies,omitempty"`
AllowedOrigins string `yaml:"allowed_origins,omitempty"`
CookieSecure *bool `yaml:"cookie_secure,omitempty"`
SkipDeps *bool `yaml:"skip_deps,omitempty"`
BinDir string `yaml:"bin_dir,omitempty"`
NRE string `yaml:"nre,omitempty"`
FFmpeg string `yaml:"ffmpeg,omitempty"`
MP4Decrypt string `yaml:"mp4decrypt,omitempty"`
Token string `yaml:"token,omitempty"` // legacy; prefer dashboard keys
SessionTTL string `yaml:"session_ttl,omitempty"`
AuditRetain string `yaml:"audit_retain,omitempty"`
OnDemandWait string `yaml:"ondemand_wait,omitempty"`
CaptureWait string `yaml:"ondemand_capture_wait,omitempty"`
SchedulePreroll string `yaml:"schedule_preroll,omitempty"`
TSFanoutBuffer *int `yaml:"ts_fanout_buffer,omitempty"`
}
// ServeOptions is the fully resolved runtime config for serve run/start/update.
type ServeOptions struct {
InstallRoot string
ConfigPath string // path used or written; may be empty if --config pointed elsewhere missing
Bind string
Data string // absolute after resolve
LogLevel string
LogFile string // absolute
PIDFile string // absolute
Channel string
Asset string
KeepOld int
TrustedProxies string
AllowedOrigins string
CookieSecure bool
SkipDeps bool
BinDir string
NRE string
FFmpeg string
MP4Decrypt string
Token string
AdminUser string
AdminPass string
SessionTTL time.Duration
AuditRetain time.Duration
OnDemandWait time.Duration
CaptureWait time.Duration
SchedulePreroll time.Duration
TSFanoutBuffer int
// FlagSet / write tracking
WroteConfig bool
}
func defaultFileConfig() FileConfig {
keep := defaultKeepOld
cookie := false
skip := false
return FileConfig{
Bind: defaultBind,
Data: filepath.Join(".cache", "streamd"),
LogLevel: defaultLogLevel,
LogFile: defaultLogFile,
PIDFile: defaultPIDFile,
Channel: defaultChannel,
KeepOld: &keep,
CookieSecure: &cookie,
SkipDeps: &skip,
}
}
func installRoot() string {
root, err := bootstrap.InstallRoot()
if err != nil || root == "" {
wd, _ := os.Getwd()
return wd
}
return root
}
// findConfigPath returns the config path to use and whether the file exists.
func findConfigPath(explicit string) (path string, exists bool) {
if strings.TrimSpace(explicit) != "" {
abs, err := filepath.Abs(explicit)
if err != nil {
abs = explicit
}
_, err = os.Stat(abs)
return abs, err == nil
}
if env := strings.TrimSpace(os.Getenv("STREAMD_CONFIG")); env != "" {
abs, err := filepath.Abs(env)
if err != nil {
abs = env
}
_, err = os.Stat(abs)
return abs, err == nil
}
cwd := filepath.Join(".", configFileName)
if st, err := os.Stat(cwd); err == nil && !st.IsDir() {
abs, _ := filepath.Abs(cwd)
return abs, true
}
root := installRoot()
p := filepath.Join(root, configFileName)
if st, err := os.Stat(p); err == nil && !st.IsDir() {
return p, true
}
// Default write target: next to the binary.
return p, false
}
func loadFileConfig(path string) (FileConfig, error) {
b, err := os.ReadFile(path)
if err != nil {
return FileConfig{}, err
}
var fc FileConfig
if err := yaml.Unmarshal(b, &fc); err != nil {
return FileConfig{}, fmt.Errorf("%s: %w", path, err)
}
return fc, nil
}
func writeFileConfig(path string, fc FileConfig) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
b, err := yaml.Marshal(&fc)
if err != nil {
return err
}
header := []byte("# streamd.yaml — server settings (auto-written on first serve).\n" +
"# Edit bind/data/channel here; streamd serve start|update reuse this file.\n")
return os.WriteFile(path, append(header, b...), 0o644)
}
func absUnderRoot(root, p string) string {
p = strings.TrimSpace(p)
if p == "" {
return ""
}
if filepath.IsAbs(p) {
return p
}
return filepath.Join(root, p)
}
func parseDuration(s string, fallback time.Duration) time.Duration {
s = strings.TrimSpace(s)
if s == "" {
return fallback
}
d, err := time.ParseDuration(s)
if err != nil {
return fallback
}
return d
}
func mergeFileIntoOptions(opt *ServeOptions, fc FileConfig) {
if fc.Bind != "" {
opt.Bind = fc.Bind
}
if fc.Data != "" {
opt.Data = absUnderRoot(opt.InstallRoot, fc.Data)
}
if fc.LogLevel != "" {
opt.LogLevel = fc.LogLevel
}
if fc.LogFile != "" {
opt.LogFile = absUnderRoot(opt.InstallRoot, fc.LogFile)
}
if fc.PIDFile != "" {
opt.PIDFile = absUnderRoot(opt.InstallRoot, fc.PIDFile)
}
if fc.Channel != "" {
opt.Channel = fc.Channel
}
if fc.Asset != "" {
opt.Asset = fc.Asset
}
if fc.KeepOld != nil {
opt.KeepOld = *fc.KeepOld
}
if fc.TrustedProxies != "" {
opt.TrustedProxies = fc.TrustedProxies
}
if fc.AllowedOrigins != "" {
opt.AllowedOrigins = fc.AllowedOrigins
}
if fc.CookieSecure != nil {
opt.CookieSecure = *fc.CookieSecure
}
if fc.SkipDeps != nil {
opt.SkipDeps = *fc.SkipDeps
}
if fc.BinDir != "" {
opt.BinDir = absUnderRoot(opt.InstallRoot, fc.BinDir)
}
if fc.NRE != "" {
opt.NRE = fc.NRE
}
if fc.FFmpeg != "" {
opt.FFmpeg = fc.FFmpeg
}
if fc.MP4Decrypt != "" {
opt.MP4Decrypt = fc.MP4Decrypt
}
if fc.Token != "" {
opt.Token = fc.Token
}
opt.SessionTTL = parseDuration(fc.SessionTTL, opt.SessionTTL)
opt.AuditRetain = parseDuration(fc.AuditRetain, opt.AuditRetain)
opt.OnDemandWait = parseDuration(fc.OnDemandWait, opt.OnDemandWait)
opt.CaptureWait = parseDuration(fc.CaptureWait, opt.CaptureWait)
opt.SchedulePreroll = parseDuration(fc.SchedulePreroll, opt.SchedulePreroll)
if fc.TSFanoutBuffer != nil {
opt.TSFanoutBuffer = *fc.TSFanoutBuffer
}
}
func relOrAbs(root, p string) string {
if p == "" {
return ""
}
if rel, err := filepath.Rel(root, p); err == nil && !strings.HasPrefix(rel, "..") {
return filepath.ToSlash(rel)
}
return p
}
func optionsToFileConfig(opt ServeOptions) FileConfig {
keep := opt.KeepOld
cookie := opt.CookieSecure
skip := opt.SkipDeps
data := relOrAbs(opt.InstallRoot, opt.Data)
if data == "" {
data = filepath.ToSlash(filepath.Join(".cache", "streamd"))
}
logFile := relOrAbs(opt.InstallRoot, opt.LogFile)
if logFile == "" {
logFile = defaultLogFile
}
pidFile := relOrAbs(opt.InstallRoot, opt.PIDFile)
if pidFile == "" {
pidFile = defaultPIDFile
}
fc := FileConfig{
Bind: opt.Bind,
Data: data,
LogLevel: opt.LogLevel,
LogFile: logFile,
PIDFile: pidFile,
Channel: opt.Channel,
Asset: opt.Asset,
KeepOld: &keep,
TrustedProxies: opt.TrustedProxies,
AllowedOrigins: opt.AllowedOrigins,
CookieSecure: &cookie,
SkipDeps: &skip,
}
if opt.BinDir != "" {
fc.BinDir = relOrAbs(opt.InstallRoot, opt.BinDir)
}
return fc
}
// ensureConfigLoaded applies discovered yaml (if any) and optionally writes a
// default file when missing. explicitConfig is the --config value (may be empty).
func ensureConfigLoaded(opt *ServeOptions, explicitConfig string, writeIfMissing bool) error {
opt.InstallRoot = installRoot()
path, exists := findConfigPath(explicitConfig)
opt.ConfigPath = path
if exists {
fc, err := loadFileConfig(path)
if err != nil {
return err
}
mergeFileIntoOptions(opt, fc)
return nil
}
if !writeIfMissing {
return nil
}
// Don't write when user passed --config to a missing path outside install root
// unless it's the default install-root path.
if strings.TrimSpace(explicitConfig) != "" {
abs, _ := filepath.Abs(explicitConfig)
def := filepath.Join(opt.InstallRoot, configFileName)
if abs != def {
return fmt.Errorf("config file not found: %s", abs)
}
path = abs
opt.ConfigPath = path
}
fc := optionsToFileConfig(*opt)
if err := writeFileConfig(path, fc); err != nil {
return err
}
opt.WroteConfig = true
return nil
}