Null-DRM-Official/apps/agent/internal/config/config.go
404errordeveloper a091922efa
All checks were successful
release / build-and-upload (push) Successful in 7m2s
streamd v0.2.2: session uptimes, egress DNS, Support, agent/Widevine
Dashboard Live uptimes + Recent runs from stream_sessions; overview.recent_sessions.
WireGuard netns resolv.conf + missing-deps WARN. Support donations card.
Agent start/stop/update, job_backoff default off, wvkey ensure/venv, phonecap retries,
modhost YAML enrich, DRM SetCredentials requires keys.
2026-10-10 19:26:39 +02:00

182 lines
5.4 KiB
Go

package config
import (
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v3"
"streamd/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 pulls modules from streamd into the local cache when missing/stale.
SyncModules bool `yaml:"sync_modules"`
ModulesDir string `yaml:"modules_dir"` // empty = <data_dir>/modules
ModulesSyncIntervalSec int `yaml:"modules_sync_interval_sec"`
ModulesPrune bool `yaml:"modules_prune"` // remove local modules absent from panel
// 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"`
// JobBackoff enables exponential retry delays after a failed capture
// (1m, 2m, 4m, 8m…). Default off — a single agent should retry ASAP.
// Set job_backoff: true when several agents share the same streams.
JobBackoff *bool `yaml:"job_backoff"`
// Daemon / update (streamd agent start|stop|update)
LogFile string `yaml:"log_file"` // default agent.log next to config / install root
PIDFile string `yaml:"pid_file"` // default agent.pid
Channel string `yaml:"channel"` // stable|beta for agent update
Asset string `yaml:"asset"` // optional streamd-* asset override
KeepOld *int `yaml:"keep_old"` // archived binaries under old/ (default 10)
}
// 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
}
// JobBackoffEnabled is the exponential multi-agent retry delay.
// Defaults to false so a lone agent retries immediately after a failure.
func (c Config) JobBackoffEnabled() bool {
return c.JobBackoff != nil && *c.JobBackoff
}
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.Python != "" && !filepath.IsAbs(cfg.Python) {
cfg.Python = filepath.Join(root, cfg.Python)
}
if cfg.ModulesDir == "" {
cfg.ModulesDir = filepath.Join(cfg.DataDir, "modules")
} else if !filepath.IsAbs(cfg.ModulesDir) {
cfg.ModulesDir = filepath.Join(root, cfg.ModulesDir)
}
if cfg.ModulesSyncIntervalSec <= 0 {
cfg.ModulesSyncIntervalSec = 300
}
if cfg.LogFile == "" {
cfg.LogFile = "agent.log"
}
if !filepath.IsAbs(cfg.LogFile) {
cfg.LogFile = filepath.Join(root, cfg.LogFile)
}
if cfg.PIDFile == "" {
cfg.PIDFile = "agent.pid"
}
if !filepath.IsAbs(cfg.PIDFile) {
cfg.PIDFile = filepath.Join(root, cfg.PIDFile)
}
if cfg.Channel == "" {
cfg.Channel = "stable"
}
if cfg.KeepOld == nil {
k := 10
cfg.KeepOld = &k
}
return cfg
}
func (c Config) ModulesSyncInterval() time.Duration {
if c.ModulesSyncIntervalSec <= 0 {
return 5 * time.Minute
}
return time.Duration(c.ModulesSyncIntervalSec) * time.Second
}