Null-DRM-Official/apps/pkg/bootstrap/bootstrap.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

171 lines
4.8 KiB
Go

// Package bootstrap materialises a GitHub-like install tree next to streamd
// so a single downloaded binary can create apps/, bin/, outputs/, etc. on first run.
package bootstrap
import (
"embed"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)
//go:embed all:embedded
var embedded embed.FS
const minProxyBytes = 100_000 // stub ELF is tiny; real proxy-android-arm64 is ~9MB
// InstallRoot is the directory that should contain apps/, bin/, outputs/.
// If streamd lives in …/bin/, the parent is the root; otherwise the exe directory.
func InstallRoot() (string, error) {
exe, err := os.Executable()
if err != nil {
return "", err
}
exe, err = filepath.EvalSymlinks(exe)
if err != nil {
exe, _ = os.Executable()
}
dir := filepath.Dir(exe)
if strings.EqualFold(filepath.Base(dir), "bin") {
return filepath.Dir(dir), nil
}
return dir, nil
}
// Ensure writes any missing layout files from the embedded tree into root.
// Existing files are left untouched (user edits to module.yaml are kept).
func Ensure(root string) (created []string, err error) {
if root == "" {
root, err = InstallRoot()
if err != nil {
return nil, err
}
}
err = fs.WalkDir(embedded, "embedded", func(path string, d fs.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
// embed.FS paths always use forward slashes — do not use filepath.Rel (Windows).
slashRel := strings.TrimPrefix(path, "embedded/")
if slashRel == path || slashRel == "embedded" || slashRel == "." {
return nil
}
// go:embed rejects files named go.mod. We ship root-go-mod.txt (and
// optionally apps/pkg/pkg.gomod) and materialise apps/pkg/go.mod.
outSlash := slashRel
switch slashRel {
case "root-go-mod.txt", "apps/pkg/pkg.gomod":
outSlash = "apps/pkg/go.mod"
}
dest := filepath.Join(root, filepath.FromSlash(outSlash))
if d.IsDir() {
return os.MkdirAll(dest, 0o755)
}
if st, err := os.Stat(dest); err == nil && !st.IsDir() {
return nil // keep existing
}
data, err := embedded.ReadFile(path)
if err != nil {
return err
}
// Do not write the tiny release stub as a real MITM binary.
if slashRel == "bin/proxy-android-arm64" && len(data) < minProxyBytes {
return nil
}
if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
return err
}
mode := os.FileMode(0o644)
if strings.HasSuffix(outSlash, "proxy-android-arm64") || strings.HasSuffix(outSlash, ".sh") {
mode = 0o755
}
if err := os.WriteFile(dest, data, mode); err != nil {
return err
}
created = append(created, outSlash)
return nil
})
if err != nil {
return created, err
}
// If streamd is not already under bin/, copy it there for 1:1 layout.
if err := ensureBinHost(root); err != nil {
return created, err
}
return created, nil
}
func ensureBinHost(root string) error {
exe, err := os.Executable()
if err != nil {
return nil
}
exe, _ = filepath.EvalSymlinks(exe)
return copyExeIfMissing(exe, filepath.Join(root, "bin", "streamd.exe"))
}
func copyExeIfMissing(src, dest string) error {
if sameFile(src, dest) {
return nil
}
if st, err := os.Stat(dest); err == nil && !st.IsDir() {
return nil
}
data, err := os.ReadFile(src)
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
return err
}
return os.WriteFile(dest, data, 0o755)
}
func sameFile(a, b string) bool {
absA, err1 := filepath.Abs(a)
absB, err2 := filepath.Abs(b)
if err1 != nil || err2 != nil {
return false
}
return strings.EqualFold(absA, absB)
}
// EnsureAndLog runs Ensure and prints what was created (if anything).
func EnsureAndLog() error {
return EnsureAndLogFor("")
}
// EnsureAndLogFor is EnsureAndLog with an optional subcommand hint so proxy-only
// runs do not warn about missing modules/.
func EnsureAndLogFor(cmd string) error {
root, err := InstallRoot()
if err != nil {
return err
}
created, err := Ensure(root)
if err != nil {
return err
}
if len(created) > 0 {
fmt.Printf("[*] Bootstrapped install layout under %s (%d new files)\n", root, len(created))
for _, c := range created {
fmt.Printf(" + %s\n", filepath.ToSlash(c))
}
}
proxy := filepath.Join(root, "bin", "proxy-android-arm64")
if st, err := os.Stat(proxy); err != nil || st.Size() < minProxyBytes {
fmt.Fprintf(os.Stderr, "[!] missing bin/proxy-android-arm64 — place it next to streamd or use a release build that embeds it\n")
}
// Providers live under modules/; only warn when the command needs them.
needsModules := cmd == "" || cmd == "modules" || cmd == "capture" || cmd == "catalog" || cmd == "agent" || cmd == "serve"
if needsModules {
modBBC := filepath.Join(root, "modules", "bbc", "module.yaml")
if _, err := os.Stat(modBBC); err != nil {
fmt.Fprintf(os.Stderr, "[!] no modules/*/ yet — add e.g. modules/bbc/ (Go source + module.yaml); see modules/README.md\n")
}
}
return nil
}