// Package deps ensures media tools (ffmpeg, N_m3u8DL-RE, mp4decrypt) exist // under bin/ next to the install, downloading static builds when missing. package deps import ( "fmt" "io" "log" "net/http" "os" "os/exec" "path/filepath" "runtime" "strings" "time" "drmdecryption/repo" ) // Status is the resolved path for each tool after Ensure. type Status struct { BinDir string `json:"bin_dir"` FFmpeg string `json:"ffmpeg"` NRE string `json:"nre"` MP4Decrypt string `json:"mp4decrypt"` Notes []string `json:"notes,omitempty"` } // Public is Status with filesystem paths reduced to presence flags, for the // unauthenticated part of /api/health. Absolute install paths are useful to an // operator and useful to an attacker, so anonymous callers get neither. func (s Status) Public() map[string]any { have := func(p string) bool { return strings.TrimSpace(p) != "" } return map[string]any{ "ffmpeg": have(s.FFmpeg), "nre": have(s.NRE), "mp4decrypt": have(s.MP4Decrypt), } } // Options controls Ensure. type Options struct { // BinDir overrides where binaries are installed. Empty → /bin // or the directory containing the running executable. BinDir string // SkipDownload only resolves existing paths (flags/env/bin/PATH). SkipDownload bool Log *log.Logger } func (o Options) logf(format string, args ...any) { if o.Log != nil { o.Log.Printf(format, args...) } else { log.Printf(format, args...) } } // Ensure resolves ffmpeg, N_m3u8DL-RE and mp4decrypt, downloading into BinDir // when missing (unless SkipDownload). Returns paths suitable for the supervisor. func Ensure(o Options, preferNRE, preferFFmpeg, preferDecrypt string) (Status, error) { st := Status{} binDir := o.BinDir if binDir == "" { binDir = defaultBinDir() } if err := os.MkdirAll(binDir, 0o755); err != nil { return st, err } st.BinDir = binDir var notes []string ff, err := resolveOrFetch(o, "ffmpeg", preferFFmpeg, binDir, ffmpegCandidates(binDir), fetchFFmpeg) if err != nil { notes = append(notes, "ffmpeg: "+err.Error()) o.logf("[!] deps: ffmpeg: %v", err) } else { st.FFmpeg = ff o.logf("[+] deps: ffmpeg → %s", ff) } nre, err := resolveOrFetch(o, "N_m3u8DL-RE", preferNRE, binDir, nreCandidates(binDir), fetchNRE) if err != nil { notes = append(notes, "N_m3u8DL-RE: "+err.Error()) o.logf("[!] deps: N_m3u8DL-RE: %v", err) } else { st.NRE = nre o.logf("[+] deps: N_m3u8DL-RE → %s", nre) } dec, err := resolveOrFetch(o, "mp4decrypt", preferDecrypt, binDir, decryptCandidates(binDir), fetchMP4Decrypt) if err != nil { notes = append(notes, "mp4decrypt: "+err.Error()) o.logf("[!] deps: mp4decrypt: %v", err) } else { st.MP4Decrypt = dec o.logf("[+] deps: mp4decrypt → %s", dec) } st.Notes = notes // Serve can start even if a tool is missing; stream start will fail clearly. return st, nil } func defaultBinDir() string { root := repo.Root() if root != "" { return filepath.Join(root, "bin") } exe, err := os.Executable() if err != nil { return "bin" } if resolved, err := filepath.EvalSymlinks(exe); err == nil { exe = resolved } dir := filepath.Dir(exe) if strings.EqualFold(filepath.Base(dir), "bin") { return dir } return filepath.Join(dir, "bin") } type fetchFunc func(binDir string, logf func(string, ...any)) (string, error) func resolveOrFetch(o Options, label, prefer, binDir string, candidates []string, fetch fetchFunc) (string, error) { if p := firstExisting(prefer); p != "" { return p, nil } for _, c := range candidates { if p := firstExisting(c); p != "" { return p, nil } } if o.SkipDownload { return "", fmt.Errorf("%s not found (auto-download disabled)", label) } o.logf("[*] deps: %s missing → downloading into %s …", label, binDir) return fetch(binDir, o.logf) } func firstExisting(paths ...string) string { for _, p := range paths { p = strings.TrimSpace(p) if p == "" { continue } if filepath.IsAbs(p) || strings.ContainsAny(p, `/\`) { if st, err := os.Stat(p); err == nil && !st.IsDir() { return p } continue } if abs, err := exec.LookPath(p); err == nil { return abs } } return "" } func ffmpegCandidates(binDir string) []string { name := "ffmpeg" if runtime.GOOS == "windows" { name = "ffmpeg.exe" } return []string{ os.Getenv("FFMPEG_PATH"), os.Getenv("FFMPEG"), filepath.Join(binDir, name), name, } } func nreCandidates(binDir string) []string { names := []string{"N_m3u8DL-RE"} if runtime.GOOS == "windows" { names = []string{"N_m3u8DL-RE.exe", "N_m3u8DL-RE"} } out := []string{os.Getenv("NRE_PATH"), os.Getenv("N_M3U8DL_RE")} for _, n := range names { out = append(out, filepath.Join(binDir, n), n) } return out } func decryptCandidates(binDir string) []string { name := "mp4decrypt" if runtime.GOOS == "windows" { name = "mp4decrypt.exe" } return []string{ os.Getenv("MP4DECRYPT_PATH"), os.Getenv("MP4DECRYPT"), filepath.Join(binDir, name), name, } } func httpGet(url string) (*http.Response, error) { client := &http.Client{Timeout: 10 * time.Minute} req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, err } req.Header.Set("User-Agent", "null-drm-streamd/1.0") return client.Do(req) } func downloadTo(url, dest string, logf func(string, ...any)) error { logf("[*] deps: GET %s", url) resp, err := httpGet(url) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("download %s: HTTP %s", url, resp.Status) } tmp := dest + ".partial" f, err := os.Create(tmp) if err != nil { return err } n, copyErr := io.Copy(f, resp.Body) closeErr := f.Close() if copyErr != nil { _ = os.Remove(tmp) return copyErr } if closeErr != nil { _ = os.Remove(tmp) return closeErr } if err := os.Rename(tmp, dest); err != nil { _ = os.Remove(tmp) return err } logf("[+] deps: saved %s (%d bytes)", dest, n) return nil } func chmodX(path string) { if runtime.GOOS == "windows" { return } _ = os.Chmod(path, 0o755) }