This commit is contained in:
parent
84ae0f6994
commit
1d36961a7c
16 changed files with 734 additions and 48 deletions
159
apps/pkg/bootstrap/bootstrap.go
Normal file
159
apps/pkg/bootstrap/bootstrap.go
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
// Package bootstrap materialises a GitHub-like install tree next to drm.exe
|
||||
// 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 drm.exe 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 drm is not already under bin/, copy/link a drm.exe there for 1:1 layout.
|
||||
if err := ensureBinDrm(root); err != nil {
|
||||
return created, err
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func ensureBinDrm(root string) error {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
exe, _ = filepath.EvalSymlinks(exe)
|
||||
dest := filepath.Join(root, "bin", "drm.exe")
|
||||
if sameFile(exe, dest) {
|
||||
return nil
|
||||
}
|
||||
if st, err := os.Stat(dest); err == nil && !st.IsDir() {
|
||||
return nil
|
||||
}
|
||||
data, err := os.ReadFile(exe)
|
||||
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 {
|
||||
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 drm or use a release build that embeds it\n")
|
||||
}
|
||||
// Module configs are never shipped inside drm.exe — operator must add them.
|
||||
modBBC := filepath.Join(root, "apps", "modules", "bbc", "module.yaml")
|
||||
if _, err := os.Stat(modBBC); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[!] no apps/modules/*/module.yaml yet — create e.g. apps/modules/bbc/module.yaml (not bundled in the exe)\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue