跳到主要内容

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

浏览文件

installation.go

254 行 · 8.8 KB历史下载
1package guestos
2
3import (
4 "bytes"
5 "encoding/hex"
6 "encoding/json"
7 "errors"
8 "io"
9 "os"
10 "path/filepath"
11 "strings"
12
13 "github.com/google/uuid"
14 "github.com/sylvinus/agent-vm/internal/hostimage"
15)
16
17// Installation persists only the media choice and private instance identity.
18// macOS secrets live separately in the Cage account's encrypted vault.
19type Installation struct {
20 Version int `json:"version"`
21 System string `json:"system"`
22 Media string `json:"media,omitempty"`
23 SHA256 string `json:"sha256,omitempty"`
24 EditorTerms bool `json:"editor_terms,omitempty"`
25 ID uuid.UUID `json:"id"`
26 Source hostimage.Plan `json:"source,omitzero"`
27 // Empty preserves legacy host-clone records. LinuxImageSource selects a
28 // catalog image independently of the physical host OS.
29 SourceKind string `json:"source_kind,omitempty"`
30}
31
32const installRecord = "guest-install.json"
33const LinuxImageSource = "linux-image"
34const Ubuntu2204 = "ubuntu-22.04"
35
36// Older installers could publish the binding before removing the temporary
37// credential. Keep those profiles in setup until their original cleanup runs.
38func WindowsCloneCleanupPending(profile string) (bool, error) {
39 install, err := LoadInstallation(profile)
40 if err != nil || install == nil || install.Source.Host.System != Windows11 {
41 return false, err
42 }
43 path := filepath.Join(profile, "windows-setup-"+strings.ReplaceAll(install.ID.String(), "-", "")+".dpapi")
44 _, err = os.Lstat(path)
45 if errors.Is(err, os.ErrNotExist) {
46 return false, nil
47 }
48 return err == nil, err
49}
50
51func (i Installation) Validate() error {
52 if i.Version != 1 || i.ID == uuid.Nil || (i.System != MacOS && i.System != Windows11 && !hostimage.IsLinux(i.System)) {
53 return errors.New("客户机安装记录无效")
54 }
55 if i.SourceKind != "" && (i.SourceKind != LinuxImageSource || i.System != Ubuntu2204 || i.Source.Host.System != i.System) {
56 return errors.New("客户机镜像选择方式无效")
57 }
58 if i.Media != "" && (!filepath.IsAbs(i.Media) || strings.ContainsAny(i.Media, "\r\n\x00") || filepath.Clean(i.Media) != i.Media) {
59 return errors.New("请选择本机的安装镜像")
60 }
61 if i.Source.Host.System != "" {
62 if i.System != i.Source.Host.System || (i.System == Windows11 && !i.EditorTerms) {
63 return errors.New("本机系统安装选择无效,请重新检查")
64 }
65 if i.Media != "" && i.Media != i.Source.Path {
66 return errors.New("安装介质与已选择的本机系统来源不一致")
67 }
68 return i.Source.Validate()
69 }
70 if hostimage.IsLinux(i.System) {
71 return errors.New("缺少 Linux 本机系统安装来源")
72 }
73 if i.System == MacOS {
74 if i.SHA256 != "" || (i.Media != "" && !strings.EqualFold(filepath.Ext(i.Media), ".ipsw")) {
75 return errors.New("请选择 Apple 的 .ipsw 恢复镜像")
76 }
77 } else if hash, err := hex.DecodeString(i.SHA256); i.Media == "" || !i.EditorTerms || err != nil || len(hash) != 32 || !strings.EqualFold(filepath.Ext(i.Media), ".iso") {
78 return errors.New("请选择 Windows 11 x64 ISO,并填写这份镜像的完整 SHA-256")
79 }
80 return nil
81}
82
83func LoadInstallation(profile string) (*Installation, error) {
84 path := filepath.Join(profile, installRecord)
85 info, err := os.Lstat(path)
86 if errors.Is(err, os.ErrNotExist) {
87 return nil, nil
88 }
89 if err != nil {
90 return nil, err
91 }
92 if !info.Mode().IsRegular() || info.Size() > 32<<10 {
93 return nil, errors.New("客户机安装记录无法读取")
94 }
95 data, err := os.ReadFile(path)
96 if err != nil {
97 return nil, err
98 }
99 var i Installation
100 decoder := json.NewDecoder(bytes.NewReader(data))
101 decoder.DisallowUnknownFields()
102 if decoder.Decode(&i) != nil || decoder.Decode(new(any)) != io.EOF {
103 return nil, errors.New("客户机安装记录格式无效")
104 }
105 if err := i.Validate(); err != nil {
106 return nil, err
107 }
108 if i.Source.Host.System != "" {
109 var ready struct {
110 ID uuid.UUID `json:"id"`
111 Source hostimage.Plan `json:"source"`
112 }
113 mediaPath := filepath.Join(profile, "guest-install-media-"+i.ID.String()+".json")
114 info, err := os.Lstat(mediaPath)
115 if err == nil {
116 if !info.Mode().IsRegular() || info.Size() > 32<<10 {
117 return nil, errors.New("已校验的安装材料记录无效")
118 }
119 data, readErr := os.ReadFile(mediaPath)
120 if readErr != nil || json.Unmarshal(data, &ready) != nil || ready.ID != i.ID {
121 return nil, errors.New("无法读取已校验的安装材料")
122 }
123 if err := matchPreparedMedia(i.Source, ready.Source); err != nil {
124 return nil, err
125 }
126 i.Source, i.Media = ready.Source, ready.Source.Path
127 i.SHA256 = strings.TrimPrefix(ready.Source.Digest, "sha256:")
128 } else if !errors.Is(err, os.ErrNotExist) {
129 return nil, err
130 }
131 }
132 return &i, i.Validate()
133}
134
135func SaveInstallation(profile string, i Installation) error {
136 if err := i.Validate(); err != nil {
137 return err
138 }
139 if current, err := LoadInstallation(profile); err != nil {
140 return err
141 } else if current != nil {
142 if *current == i {
143 return nil
144 }
145 return errors.New("已有其他安装选择,请继续原安装")
146 }
147 data, err := json.Marshal(i)
148 if err != nil {
149 return err
150 }
151 return publishBinding(profile, installRecord, append(data, '\n'))
152}
153
154func matchPreparedMedia(chosen, ready hostimage.Plan) error {
155 if err := ready.Validate(); err != nil {
156 return err
157 }
158 if ready.Path == "" || ready.Digest == "" || ready.Host != chosen.Host || ready.Version != chosen.Version || ready.Build != chosen.Build ||
159 ready.Format != chosen.Format || ready.URL != chosen.URL || (chosen.Digest != "" && ready.Digest != chosen.Digest) ||
160 (chosen.Bytes != 0 && ready.Bytes != chosen.Bytes) || (chosen.Path != "" && ready.Path != chosen.Path) ||
161 (chosen.Modified != 0 && ready.Modified != chosen.Modified) {
162 return errors.New("已校验的系统材料与原安装选择不一致")
163 }
164 return nil
165}
166
167// RecordPreparedMedia publishes a create-only receipt. The original choice
168// remains immutable throughout downloading, installation and concurrent retries.
169func RecordPreparedMedia(profile string, id uuid.UUID, ready hostimage.Plan) error {
170 current, err := LoadInstallation(profile)
171 if err != nil {
172 return err
173 }
174 if current == nil || current.ID != id {
175 return errors.New("安装选择已变化")
176 }
177 if err := matchPreparedMedia(current.Source, ready); err != nil {
178 return err
179 }
180 data, err := json.Marshal(struct {
181 ID uuid.UUID `json:"id"`
182 Source hostimage.Plan `json:"source"`
183 }{id, ready})
184 if err != nil {
185 return err
186 }
187 return publishBinding(profile, "guest-install-media-"+id.String()+".json", data)
188}
189
190func FinishLinuxInstallation(profile string, id uuid.UUID) error {
191 installed, err := LoadInstallation(profile)
192 if err != nil {
193 return err
194 }
195 if installed == nil || installed.ID != id || !hostimage.IsLinux(installed.System) {
196 return errors.New("Linux 安装记录已变化")
197 }
198 // Keep recovery information until the persistent image choice can be read.
199 system, err := hostimage.LinuxSystem(filepath.Join(profile, "state"))
200 if err != nil {
201 return err
202 }
203 if system != installed.System {
204 return errors.New("Linux 安装尚未保存所选系统,保留安装记录以便继续")
205 }
206 return os.Remove(filepath.Join(profile, installRecord))
207}
208
209// UnstartedInstallation reports whether the choice can be changed while
210// retaining every downloaded file and disk. A partial VM counts as started.
211func UnstartedInstallation(profile string, i Installation) (bool, error) {
212 paths := []string{"macos-guest.json", "windows-guest.json", "windows-install-vm.txt",
213 filepath.Join("state", "windows-"+strings.ReplaceAll(i.ID.String(), "-", "")),
214 filepath.Join("state", "lima", "mac"), filepath.Join("state", "lima", "base-building"), filepath.Join("state", "lima", "base")}
215 for _, rel := range paths {
216 _, err := os.Lstat(filepath.Join(profile, rel))
217 if err == nil {
218 return false, nil
219 }
220 if !errors.Is(err, os.ErrNotExist) {
221 return false, err
222 }
223 }
224 return true, nil
225}
226
227func ClearUnstartedInstallation(profile string, id uuid.UUID) error {
228 pending, err := LoadInstallation(profile)
229 if err != nil {
230 return err
231 }
232 if pending == nil || pending.ID != id {
233 return errors.New("客户机安装选择已改变")
234 }
235 canClear, err := UnstartedInstallation(profile, *pending)
236 if err != nil {
237 return err
238 }
239 if !canClear {
240 return errors.New("已开始创建客户机,请继续原安装")
241 }
242 if hostimage.IsLinux(pending.System) {
243 if err := hostimage.ClearLinuxTemplate(filepath.Join(profile, "state"), pending.Source); err != nil {
244 return err
245 }
246 }
247 return os.Remove(filepath.Join(profile, installRecord))
248}
249
250// The Apple login surface accepts physical keys from the guest's initial US
251// layout. Check this before downloading media or creating the local account.
252func ValidMacOSInstallPassword(value string) bool {
253 return len(value) >= 8 && len(value) <= 256 && strings.IndexFunc(value, func(r rune) bool { return r < ' ' || r > '~' }) < 0
254}