| 1 | package guestos |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "io/fs" |
| 12 | "os" |
| 13 | "path" |
| 14 | "path/filepath" |
| 15 | "sort" |
| 16 | "strings" |
| 17 | |
| 18 | "github.com/sylvinus/agent-vm/internal/cage" |
| 19 | "github.com/sylvinus/agent-vm/internal/version" |
| 20 | ) |
| 21 | |
| 22 | const MacOSBrowserTools = "/usr/local/libexec/cage-workbench-browser-4.145.0-pw1.65.0" |
| 23 | const MacOSCLIRuntime = "/usr/local/libexec/cage-cli-runtime-2.1.311-py3.13.18-git2.56.0" |
| 24 | |
| 25 | type NativeComponentFile struct { |
| 26 | Source string `json:"source"` |
| 27 | Target string `json:"target,omitempty"` |
| 28 | SHA256 string `json:"sha256"` |
| 29 | InstalledSHA256 string `json:"installed_sha256"` |
| 30 | } |
| 31 | |
| 32 | type NativeComponentManifest struct { |
| 33 | cage.GuestManifest |
| 34 | System string `json:"system"` |
| 35 | Files []NativeComponentFile `json:"files"` |
| 36 | Tools map[string]string `json:"tools"` |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | func RequiredNativeComponents(kit, system string) (NativeComponentManifest, error) { |
| 42 | manifest := NativeComponentManifest{GuestManifest: cage.GuestManifest{Protocol: 1, AppVersion: version.Version}, System: system} |
| 43 | if system != MacOS && system != Windows11 { |
| 44 | return manifest, errors.New("原生客机系统无效") |
| 45 | } |
| 46 | essential := []string{"payload/Cage.GuestClipboard.exe", "payload/cage-guest-files.exe", "payload/cage-guest-boot.exe", "payload/Cage.HyperVManager.exe", "payload/windows-browser.mjs"} |
| 47 | if system == MacOS { |
| 48 | essential = []string{"Cage Guest Window.app/Contents/MacOS/CageGuestWindow", "Cage Guest Clipboard.app/Contents/MacOS/CageGuestClipboard", "project/cage-sshfs", "project/cage-macos-terminal", "native-browser/macos-native-browser.mjs", "native-browser/macos-browser-service.mjs", "register-macos-browser.sh", "macos-cli.py", "cli_channel.py"} |
| 49 | } |
| 50 | for _, relative := range essential { |
| 51 | info, err := os.Lstat(filepath.Join(kit, filepath.FromSlash(relative))) |
| 52 | if err != nil || !info.Mode().IsRegular() { |
| 53 | return manifest, fmt.Errorf("原生组件包缺少 %s", relative) |
| 54 | } |
| 55 | } |
| 56 | err := filepath.WalkDir(kit, func(filename string, entry fs.DirEntry, walkErr error) error { |
| 57 | if walkErr != nil { |
| 58 | return walkErr |
| 59 | } |
| 60 | if entry.IsDir() { |
| 61 | return nil |
| 62 | } |
| 63 | if entry.Type()&os.ModeSymlink != 0 { |
| 64 | return errors.New("原生组件目录不能包含文件链接") |
| 65 | } |
| 66 | info, err := entry.Info() |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | if !info.Mode().IsRegular() { |
| 71 | return errors.New("原生组件目录包含特殊文件") |
| 72 | } |
| 73 | relative, err := filepath.Rel(kit, filename) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | relative = filepath.ToSlash(relative) |
| 78 | if relative == "native-components.json" { |
| 79 | return nil |
| 80 | } |
| 81 | file, err := os.Open(filename) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | hash := sha256.New() |
| 86 | _, readErr := io.Copy(hash, file) |
| 87 | if err = errors.Join(readErr, file.Close()); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | digest := hex.EncodeToString(hash.Sum(nil)) |
| 91 | target := nativeComponentTarget(relative, system) |
| 92 | installed := digest |
| 93 | if system == MacOS && relative == "claude-account.py" { |
| 94 | data, err := os.ReadFile(filename) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | _, body, found := bytes.Cut(data, []byte("\n")) |
| 99 | if !found { |
| 100 | return errors.New("账号组件缺少程序正文") |
| 101 | } |
| 102 | rewritten := sha256.Sum256(append([]byte("#!"+MacOSCLIRuntime+"/bin/python3 -I\n"), body...)) |
| 103 | installed = hex.EncodeToString(rewritten[:]) |
| 104 | } |
| 105 | manifest.Files = append(manifest.Files, NativeComponentFile{Source: relative, Target: target, SHA256: digest, InstalledSHA256: installed}) |
| 106 | if system == Windows11 && relative == "payload/Cage.GuestClipboard.exe" { |
| 107 | manifest.Files = append(manifest.Files, NativeComponentFile{Source: relative, Target: "network/Cage.GuestNetwork.exe", SHA256: digest, InstalledSHA256: installed}) |
| 108 | } |
| 109 | return nil |
| 110 | }) |
| 111 | if err != nil { |
| 112 | return manifest, err |
| 113 | } |
| 114 | if len(manifest.Files) == 0 { |
| 115 | return manifest, errors.New("此应用缺少原生客机组件") |
| 116 | } |
| 117 | sort.Slice(manifest.Files, func(i, j int) bool { |
| 118 | if manifest.Files[i].Source != manifest.Files[j].Source { |
| 119 | return manifest.Files[i].Source < manifest.Files[j].Source |
| 120 | } |
| 121 | return manifest.Files[i].Target < manifest.Files[j].Target |
| 122 | }) |
| 123 | digest := sha256.New() |
| 124 | fmt.Fprintf(digest, "cage-native-components-1\x00%s\x00", system) |
| 125 | for _, file := range manifest.Files { |
| 126 | fmt.Fprintf(digest, "%s\x00%s\x00%s\x00%s\x00", file.Source, file.Target, file.SHA256, file.InstalledSHA256) |
| 127 | } |
| 128 | manifest.Revision = hex.EncodeToString(digest.Sum(nil)) |
| 129 | manifest.Tools = map[string]string{"chromium": "系统维护的 Chrome / Edge", "code_server": "261c84cb1eddf11235f9e2c921425e9663fbe556"} |
| 130 | if system == MacOS { |
| 131 | manifest.Tools = map[string]string{"chromium": "Google Chrome 稳定版", "code_server": "4.145.0", "claude": "2.1.311", "node": "24.22.2", "python": "3.13.18", "git": "2.56.0"} |
| 132 | } else { |
| 133 | var dependencies map[string]struct { |
| 134 | Version string `json:"version"` |
| 135 | } |
| 136 | if data, err := os.ReadFile(filepath.Join(kit, "payload", "cli-dependencies.json")); err != nil { |
| 137 | return manifest, err |
| 138 | } else if err = json.Unmarshal(data, &dependencies); err != nil { |
| 139 | return manifest, err |
| 140 | } |
| 141 | for _, name := range []string{"claude", "python", "git"} { |
| 142 | manifest.Tools[name] = dependencies[name].Version |
| 143 | } |
| 144 | manifest.Tools["node"] = "24.22.2" |
| 145 | } |
| 146 | return manifest, manifest.Validate() |
| 147 | } |
| 148 | |
| 149 | func nativeComponentTarget(source, system string) string { |
| 150 | if system == Windows11 { |
| 151 | if !strings.HasPrefix(source, "payload/") { |
| 152 | return "" |
| 153 | } |
| 154 | name := strings.TrimPrefix(source, "payload/") |
| 155 | if name == "Cage.HyperVManager.exe" || name == "windows-workbench-packages.json" { |
| 156 | return "" |
| 157 | } |
| 158 | return "bridge/" + name |
| 159 | } |
| 160 | switch { |
| 161 | case strings.HasPrefix(source, "Cage ") && strings.Contains(source, ".app/"): |
| 162 | return "/Applications/" + source |
| 163 | case strings.HasPrefix(source, "project/CageProjectAccess.app/"): |
| 164 | return "/Applications/" + strings.TrimPrefix(source, "project/") |
| 165 | case strings.HasPrefix(source, "native-browser/"): |
| 166 | return MacOSBrowserTools + "/controller/" + strings.TrimPrefix(source, "native-browser/") |
| 167 | } |
| 168 | targets := map[string]string{ |
| 169 | "project/cage-sshfs": "/usr/local/libexec/cage-sshfs", |
| 170 | "project/cage-macos-terminal": "/usr/local/libexec/cage-macos-terminal", |
| 171 | "cage-finish-initial-setup": "/usr/local/libexec/cage-finish-initial-setup", |
| 172 | "cage-macos-ipv6-check": "/usr/local/libexec/cage-macos-ipv6-check", |
| 173 | "macos-browser.mjs": "/usr/local/libexec/cage-macos-browser.mjs", |
| 174 | "cage-macos-editor": "/usr/local/libexec/cage-macos-editor", |
| 175 | "register-macos-claude-window.sh": "/usr/local/libexec/cage-register-claude-window", |
| 176 | "register-macos-clipboard.sh": "/usr/local/libexec/cage-register-clipboard", |
| 177 | "register-macos-browser.sh": "/usr/local/libexec/cage-register-browser", |
| 178 | "macos-cli.py": "/usr/local/libexec/cage-cli/macos-cli.py", |
| 179 | "cli_channel.py": "/usr/local/libexec/cage-cli/cli_channel.py", |
| 180 | "cli_channel.resume.md": "/usr/local/libexec/cage-cli/cli_channel.resume.md", |
| 181 | "claude_account_resume.md": "/usr/local/libexec/cage-cli/cli_channel.account-resume.md", |
| 182 | "claude-account.py": "/usr/local/libexec/cage-cli/bin/cage-claude-account", |
| 183 | } |
| 184 | return targets[source] |
| 185 | } |
| 186 | |
| 187 | func (m NativeComponentManifest) Validate() error { |
| 188 | if m.Protocol != 1 || (m.System != MacOS && m.System != Windows11) || len(m.Revision) != 64 || len(m.Files) == 0 || len(m.Files) > 10000 { |
| 189 | return errors.New("原生组件清单无效") |
| 190 | } |
| 191 | if _, err := hex.DecodeString(m.Revision); err != nil { |
| 192 | return errors.New("原生组件版本摘要无效") |
| 193 | } |
| 194 | seen := map[string]bool{} |
| 195 | for _, file := range m.Files { |
| 196 | key := file.Source + "\x00" + file.Target |
| 197 | if !fs.ValidPath(file.Source) || strings.ContainsAny(file.Source+file.Target, "\x00\r\n") || seen[key] || len(file.SHA256) != 64 || len(file.InstalledSHA256) != 64 { |
| 198 | return errors.New("原生组件文件记录无效") |
| 199 | } |
| 200 | if _, err := hex.DecodeString(file.SHA256 + file.InstalledSHA256); err != nil { |
| 201 | return errors.New("原生组件摘要无效") |
| 202 | } |
| 203 | if file.Target != "" && (path.Clean(file.Target) != file.Target || strings.Contains(file.Target, "\\")) { |
| 204 | return errors.New("原生组件安装位置无效") |
| 205 | } |
| 206 | if file.Target != "" && seen["\x00"+file.Target] { |
| 207 | return errors.New("原生组件安装位置重复") |
| 208 | } |
| 209 | if file.Target != "" && m.System == MacOS && !strings.HasPrefix(file.Target, "/Applications/Cage") && !strings.HasPrefix(file.Target, "/usr/local/libexec/cage-") { |
| 210 | return errors.New("macOS 组件安装位置无效") |
| 211 | } |
| 212 | if file.Target != "" && m.System == Windows11 && !strings.HasPrefix(file.Target, "bridge/") && !strings.HasPrefix(file.Target, "network/") { |
| 213 | return errors.New("Windows 组件安装位置无效") |
| 214 | } |
| 215 | if m.System == Windows11 && strings.ContainsAny(file.Source+file.Target, ":\\") { |
| 216 | return errors.New("Windows 组件文件名无效") |
| 217 | } |
| 218 | seen[key] = true |
| 219 | if file.Target != "" { |
| 220 | seen["\x00"+file.Target] = true |
| 221 | } |
| 222 | } |
| 223 | return nil |
| 224 | } |