跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

mac_guest_install_darwin_arm64.go

183 行 · 7.5 KB历史下载
1//go:build darwin && arm64 && cgo && !no_vz
2
3package main
4
5import (
6 "bytes"
7 "context"
8 "errors"
9 "fmt"
10 "io"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "regexp"
15 "strings"
16 "time"
17
18 "al.essio.dev/pkg/shellescape"
19 "github.com/sylvinus/agent-vm/internal/guestos"
20 "github.com/sylvinus/agent-vm/internal/hostproxy"
21 "github.com/sylvinus/agent-vm/internal/vm"
22)
23
24const appleImageURL = "https://updates.cdn-apple.com/2026SpringFCS/fullrestores/140-24263/B95838F0-6815-4F0B-A039-156526C081AD/UniversalMac_26.5.2_25F84_Restore.ipsw"
25const appleImageDigest = "065abd295a1a456a46c1155217eab92ee95816520ec9aeed83f249f074f68a04"
26const appleImageBytes int64 = 19769902281
27
28func installMacWorkbench(ctx context.Context, profile, state string, l *vm.Lima) error {
29 i, err := guestos.LoadInstallation(profile)
30 if err != nil || i == nil || i.System != guestos.MacOS {
31 return errors.New("缺少这台虚拟 Mac 的首次安装记录")
32 }
33 pw, err := io.ReadAll(io.LimitReader(os.Stdin, 257))
34 if err != nil || !guestos.ValidMacOSInstallPassword(string(pw)) {
35 return errors.New("客机初始密码需使用 8 至 256 个英文键盘可输入的字母、数字、空格或符号")
36 }
37 defer clear(pw)
38 ordinaryUser := os.Getenv("CAGE_MACOS_ACCOUNT")
39 if !guestos.ValidMacOSAccountName(ordinaryUser) {
40 return errors.New("请从 Cage 注册共用账密后继续安装 macOS 客机")
41 }
42 if !macGuestLoginAvailable() {
43 return errors.New("此 Cage 构建缺少虚拟 Mac 登录组件")
44 }
45 exe, err := os.Executable()
46 if err != nil {
47 return err
48 }
49 kit := filepath.Join(filepath.Dir(exe), "guest-os")
50 components, err := guestos.RequiredNativeComponents(kit, guestos.MacOS)
51 if err != nil {
52 return err
53 }
54 componentStage, err := os.MkdirTemp("", "cage-install-components-")
55 if err != nil {
56 return err
57 }
58 defer os.RemoveAll(componentStage)
59 if err = guestos.WriteNativeComponentManifest(filepath.Join(componentStage, "native-components.json"), components); err != nil {
60 return err
61 }
62 cache := filepath.Join(state, "guest-media", i.System)
63 if err := os.MkdirAll(cache, 0700); err != nil {
64 return err
65 }
66 local := func(name string, args ...string) error {
67 cmd := exec.CommandContext(ctx, name, args...)
68 cmd.Env = append(append(os.Environ(), hostproxy.DownloadEnvironment()...), "CAGE_HOST_DOWNLOADER="+exe)
69 cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
70 return cmd.Run()
71 }
72 media := i.Media
73 if media == "" {
74 media = filepath.Join(cache, "MacOS.ipsw")
75 fmt.Fprintln(os.Stdout, "正在从 Apple 下载并校验 macOS 26.5.2(19.77 GB)…")
76 err = prepareMacRestoreMedia(ctx, media, appleImageURL, appleImageDigest, appleImageBytes, os.Stdout)
77 if err != nil {
78 return fmt.Errorf("Apple 恢复镜像下载失败:%w", err)
79 }
80 }
81 fmt.Fprintln(os.Stdout, "正在准备客机应用和开发工具…")
82 fmt.Fprintln(os.Stdout, "CAGE_GUEST_STAGE macos media")
83 if err := local("/bin/bash", filepath.Join(kit, "prepare-macos-media.sh"), cache); err != nil {
84 return fmt.Errorf("准备客机应用失败:%w", err)
85 }
86 fmt.Fprintln(os.Stdout, "正在恢复虚拟 Mac…")
87 fmt.Fprintln(os.Stdout, "CAGE_GUEST_STAGE macos restore")
88 if err := runMacGuest(ctx, profile, state, []string{"create", media}); err != nil {
89 return err
90 }
91 if err := l.CheckMacOSGuest(ctx, "mac"); err != nil {
92 return err
93 }
94 if err := guestos.PinMacOSInstallHostKey(profile); err != nil {
95 return err
96 }
97 provision, err := guestos.MacOSProvisionUser(profile)
98 if err != nil {
99 return err
100 }
101 if provision != "cageprovision" {
102 return errors.New("此开发客机的置备身份不匹配")
103 }
104 adminPassword := " < /Users/" + provision + "/password"
105 args, err := guestos.MacOSSSHArgs(profile, provision)
106 if err != nil {
107 return err
108 }
109 guestCommand := func(opctx context.Context, script string) *exec.Cmd {
110 argv := append(append([]string{}, args...), "-T", "cage-macos", "--", "exec /bin/sh -ec "+shellescape.Quote(script))
111 cmd := exec.CommandContext(opctx, "/usr/bin/ssh", argv...)
112 cmd.Stderr = os.Stderr
113 return cmd
114 }
115 out, err := guestCommand(ctx, `umask 077; test "$(uname -s)" = Darwin; test "$(id -un)" = `+provision+`; mktemp -d /private/var/tmp/cage-install.XXXXXXXX`).Output()
116 remote := strings.TrimSpace(string(out))
117 if err != nil || !regexp.MustCompile(`^/private/var/tmp/cage-install\.[A-Za-z0-9]{8}$`).MatchString(remote) {
118 return errors.New("无法在已验证的虚拟 Mac 中准备安装目录")
119 }
120 defer func() {
121 clean, cancel := context.WithTimeout(context.Background(), 10*time.Second)
122 defer cancel()
123 _ = guestCommand(clean, `sudo -S -p '' /bin/rm -rf `+remote+adminPassword).Run()
124 }()
125 fmt.Fprintln(os.Stdout, "正在将开发组件送入已验证的离线虚拟 Mac…")
126 fmt.Fprintln(os.Stdout, "CAGE_GUEST_STAGE macos tools")
127 // Only the fixed subset of media is transferred. The IPSW stays on the host.
128 tar := exec.CommandContext(ctx, "/usr/bin/tar", "--no-mac-metadata", "-cf", "-", "-C", kit, ".", "-C", cache, "node.tgz", "editor.tgz", "playwright.tgz", "chrome.dmg", "claude.dmg", "cli", "macfuse.pkg", "-C", componentStage, "native-components.json")
129 tar.Env = append(os.Environ(), "COPYFILE_DISABLE=1")
130 tar.Stderr = os.Stderr
131 pipe, err := tar.StdoutPipe()
132 if err != nil {
133 return err
134 }
135 receiver := guestCommand(ctx, `umask 077; cd `+remote+`; /usr/bin/tar -xf -; mv macfuse.pkg project/macfuse.pkg`)
136 receiver.Stdin = pipe
137 if err = tar.Start(); err != nil {
138 return err
139 }
140 recvErr := receiver.Run()
141 _ = pipe.Close()
142 if err := errors.Join(recvErr, tar.Wait()); err != nil {
143 return fmt.Errorf("客机组件传输失败:%w", err)
144 }
145 // The staging directory belongs exclusively to the provisioning account.
146 // Before invoking the installer, root takes ownership and restricts access.
147 auth := guestCommand(ctx, `umask 077; cat > `+remote+`/login-input; sudo -S -p '' /usr/sbin/chown -R root:wheel `+remote+adminPassword)
148 auth.Stdin = io.MultiReader(bytes.NewReader(pw), strings.NewReader("\n"))
149 if err := auth.Run(); err != nil {
150 return errors.New("无法准备虚拟 Mac 账户")
151 }
152 cmd := guestCommand(ctx, `sudo -S -p '' /bin/sh -ec `+shellescape.Quote(`cd `+remote+`; exec 4<login-input; rm login-input; /usr/bin/perl ./native-component-manifest.pl source ./native-components.json .; /bin/bash ./provision-macos-workbench.sh `+remote+` `+i.ID.String()+` `+provision+` `+ordinaryUser+` <&4; /usr/bin/perl ./native-component-manifest.pl publish ./native-components.json .`)+adminPassword)
153 cmd.Stdout = os.Stdout
154 if err := cmd.Run(); err != nil {
155 return fmt.Errorf("安装虚拟 Mac 应用失败,可在工作台继续安装:%w", err)
156 }
157 ordinary, err := guestos.MacOSSSHArgs(profile, ordinaryUser)
158 if err != nil {
159 return err
160 }
161 verify := exec.CommandContext(ctx, "/usr/bin/ssh", append(ordinary, "-T", "cage-macos", "--",
162 `test "$(id -un)" = `+ordinaryUser+` && test "$(uname -s)" = Darwin && ! id -Gn | /usr/bin/grep -qw admin`)...)
163 if err := verify.Run(); err != nil {
164 return errors.New("普通客机账户无法通过已验证的 SSH 通道连接,已保留安装进度")
165 }
166 key, err := guestos.MacOSInstallHostKey(profile)
167 if err != nil {
168 return err
169 }
170 // Remove the root-owned media before stopping. No user data is in this dir.
171 if err := guestCommand(ctx, `sudo -S -p '' /bin/rm -rf `+remote+adminPassword).Run(); err != nil {
172 return err
173 }
174 fmt.Fprintln(os.Stdout, "CAGE_GUEST_STAGE macos services")
175 if err := l.Stop(ctx, "mac"); err != nil {
176 return err
177 }
178 if err := guestos.BindNewMacOSProfile(profile, ordinaryUser, key, i.ID); err != nil {
179 return err
180 }
181 fmt.Fprintln(os.Stdout, "macOS 客机应用已安装,自动登录已准备。Cage 将继续完成首次授权。")
182 return nil
183}