|
| 1 | +//go:build android |
| 2 | + |
| 3 | +package embedding |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | +) |
| 11 | + |
| 12 | +// GetProotPath 获取 proot 二进制文件路径 |
| 13 | +// proot 允许在 Android 上运行 Linux 二进制文件 |
| 14 | +func GetProotPath() (string, error) { |
| 15 | + libDir, err := findNativeLibraryDir() |
| 16 | + if err != nil { |
| 17 | + return "", fmt.Errorf("无法找到 lib 目录: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + // proot 文件名 |
| 21 | + var prootName string |
| 22 | + switch runtime.GOARCH { |
| 23 | + case "arm64": |
| 24 | + prootName = "libproot.so" |
| 25 | + case "arm": |
| 26 | + prootName = "libproot.so" |
| 27 | + case "amd64": |
| 28 | + prootName = "libproot_x86_64.so" |
| 29 | + case "386": |
| 30 | + prootName = "libproot_x86.so" |
| 31 | + default: |
| 32 | + prootName = "libproot.so" |
| 33 | + } |
| 34 | + |
| 35 | + // 可能的子目录 |
| 36 | + abiDirs := []string{"", "arm64-v8a", "arm64", "x86_64", "x86"} |
| 37 | + |
| 38 | + for _, abiDir := range abiDirs { |
| 39 | + baseDir := libDir |
| 40 | + if abiDir != "" { |
| 41 | + baseDir = filepath.Join(libDir, abiDir) |
| 42 | + } |
| 43 | + |
| 44 | + prootPath := filepath.Join(baseDir, prootName) |
| 45 | + if info, err := os.Stat(prootPath); err == nil && !info.IsDir() { |
| 46 | + // 确保有执行权限 |
| 47 | + os.Chmod(prootPath, 0755) |
| 48 | + return prootPath, nil |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return "", fmt.Errorf("找不到 proot 二进制文件") |
| 53 | +} |
| 54 | + |
| 55 | +// SetupProotEnvironment 设置 proot 运行环境 |
| 56 | +func SetupProotEnvironment() (map[string]string, error) { |
| 57 | + env := make(map[string]string) |
| 58 | + |
| 59 | + // 获取应用的私有数据目录 |
| 60 | + homeDir := os.Getenv("HOME") |
| 61 | + if homeDir == "" { |
| 62 | + // 尝试使用应用数据目录 |
| 63 | + homeDir = "/data/data/com.oneclickvirt.goecs" |
| 64 | + if _, err := os.Stat(homeDir); os.IsNotExist(err) { |
| 65 | + // 如果无法访问,使用临时目录 |
| 66 | + homeDir = "/data/local/tmp" |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + env["HOME"] = homeDir |
| 71 | + env["TMPDIR"] = filepath.Join(homeDir, "tmp") |
| 72 | + env["PATH"] = "/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin" |
| 73 | + |
| 74 | + // 创建必要的目录 |
| 75 | + os.MkdirAll(env["TMPDIR"], 0755) |
| 76 | + |
| 77 | + return env, nil |
| 78 | +} |
| 79 | + |
| 80 | +// BuildProotCommand 构建使用 proot 运行的命令 |
| 81 | +// prootPath: proot 二进制文件路径 |
| 82 | +// ecsPath: ECS 二进制文件路径 |
| 83 | +// args: ECS 命令参数 |
| 84 | +func BuildProotCommand(prootPath, ecsPath string, args []string) (string, []string) { |
| 85 | + // proot 参数 |
| 86 | + prootArgs := []string{ |
| 87 | + "-0", // 模拟 root 用户 |
| 88 | + "-r", "/", // 根目录(不改变) |
| 89 | + "-w", "/", // 工作目录 |
| 90 | + "--link2symlink", // 将硬链接转换为符号链接 |
| 91 | + } |
| 92 | + |
| 93 | + // 添加 ECS 命令和参数 |
| 94 | + prootArgs = append(prootArgs, ecsPath) |
| 95 | + prootArgs = append(prootArgs, args...) |
| 96 | + |
| 97 | + return prootPath, prootArgs |
| 98 | +} |
0 commit comments