package config import ( "os" "path/filepath" "time" "gopkg.in/yaml.v3" "drmdecryption/repo" ) type Config struct { StreamdURL string `yaml:"streamd_url"` Token string `yaml:"token"` 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"` } 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 = os.Getenv("STREAMD_TOKEN") } 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) } return cfg }