Null-DRM-Official/apps/agent/internal/config/config.go
404errordeveloper 066e018749
All checks were successful
release / build-and-upload (push) Successful in 6m18s
modules-release / build-and-upload (push) Successful in 5m18s
streamd: preview quality picker, dashboard polish, C4 wrap keys
- Preview player can select FHD/HD/SD (or ABR master) from publish links
- Embed dashboard screenshots in README; Linux streamd+agent setup guide
- C4 Widevine wrap license mode + require --wvd; prefer Widevine PSSH
- Channel 4 and My5 modules; streamd auth, variants, schedules, HLS gate
2026-10-09 20:57:43 +02:00

141 lines
3.9 KiB
Go

package config
import (
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v3"
"drmdecryption/repo"
)
type Config struct {
StreamdURL string `yaml:"streamd_url"`
// Token is this agent's streamd credential. It is now an API key created
// in the dashboard (Settings -> API keys, role operator) rather than the
// one shared secret; `api_key` is a clearer alias for the same field.
Token string `yaml:"token"`
APIKey string `yaml:"api_key"`
PollIntervalSec int `yaml:"poll_interval_sec"`
DataDir string `yaml:"data_dir"`
Workers int `yaml:"workers"`
MaxAttempts int `yaml:"max_attempts"`
CaptureWaitSec int `yaml:"capture_wait_sec"`
Devices []string `yaml:"devices"`
Python string `yaml:"python"`
WVD string `yaml:"wvd"` // path to .wvd device file (required for key fetch)
UserAgent string `yaml:"user_agent"`
AgentID string `yaml:"agent_id"`
// SyncModules pushes local modules/<name>/module.yaml to streamd when missing/stale.
SyncModules bool `yaml:"sync_modules"`
ModulesDir string `yaml:"modules_dir"` // empty = repo modules/
ModulesSyncIntervalSec int `yaml:"modules_sync_interval_sec"`
// OnDemandPriority lets a capture that streamd pushed — a viewer waking
// an on-demand stream, or a scheduled event about to start — jump ahead
// of routine refreshes in the queue.
OnDemandPriority *bool `yaml:"ondemand_priority"`
}
// PushedCapturesArePriority reports whether pushed captures jump the queue.
// Defaults to true: somebody is waiting on them.
func (c Config) PushedCapturesArePriority() bool {
return c.OnDemandPriority == nil || *c.OnDemandPriority
}
func (c Config) PollInterval() time.Duration {
if c.PollIntervalSec <= 0 {
return 30 * time.Second
}
return time.Duration(c.PollIntervalSec) * time.Second
}
func (c Config) CaptureWait() time.Duration {
if c.CaptureWaitSec <= 0 {
return 180 * time.Second
}
return time.Duration(c.CaptureWaitSec) * time.Second
}
func Load(path string) (Config, error) {
root := repo.Root()
if path == "" {
path = filepath.Join(root, "apps", "agent", "agent.yaml")
}
var cfg Config
raw, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
cfg = defaults(root)
return cfg, nil
}
return cfg, err
}
if err := yaml.Unmarshal(raw, &cfg); err != nil {
return cfg, err
}
cfg = applyDefaults(cfg, root)
return cfg, nil
}
func defaults(root string) Config {
return applyDefaults(Config{}, root)
}
func applyDefaults(cfg Config, root string) Config {
if cfg.StreamdURL == "" {
cfg.StreamdURL = "http://127.0.0.1:8083"
}
if cfg.Token == "" {
cfg.Token = cfg.APIKey
}
if cfg.Token == "" {
cfg.Token = os.Getenv("STREAMD_TOKEN")
}
if cfg.Token == "" {
cfg.Token = os.Getenv("STREAMD_API_KEY")
}
if cfg.PollIntervalSec <= 0 {
cfg.PollIntervalSec = 30
}
if cfg.DataDir == "" {
cfg.DataDir = filepath.Join(root, ".cache", "agent")
} else if !filepath.IsAbs(cfg.DataDir) {
cfg.DataDir = filepath.Join(root, cfg.DataDir)
}
if cfg.Workers <= 0 {
cfg.Workers = 1
}
if cfg.MaxAttempts <= 0 {
cfg.MaxAttempts = 5
}
if cfg.CaptureWaitSec <= 0 {
cfg.CaptureWaitSec = 180
}
if cfg.AgentID == "" {
// Stable across restarts so the same agent can reclaim after a crash.
// Override in apps/agent/agent.yaml when running multiple agents.
host, _ := os.Hostname()
if host == "" {
host = "agent"
}
cfg.AgentID = host
}
if cfg.WVD != "" && !filepath.IsAbs(cfg.WVD) {
cfg.WVD = filepath.Join(root, cfg.WVD)
}
if cfg.ModulesDir != "" && !filepath.IsAbs(cfg.ModulesDir) {
cfg.ModulesDir = filepath.Join(root, cfg.ModulesDir)
}
if cfg.ModulesSyncIntervalSec <= 0 {
cfg.ModulesSyncIntervalSec = 300
}
return cfg
}
func (c Config) ModulesSyncInterval() time.Duration {
if c.ModulesSyncIntervalSec <= 0 {
return 5 * time.Minute
}
return time.Duration(c.ModulesSyncIntervalSec) * time.Second
}