跳到主要内容

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

浏览文件

windows_safety.go

171 行 · 6.4 KB历史下载
1package workbench
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "io"
10 "path/filepath"
11 "strings"
12
13 "github.com/sylvinus/agent-vm/internal/cage"
14 "github.com/sylvinus/agent-vm/internal/diagnostics"
15 "github.com/sylvinus/agent-vm/internal/guestos"
16)
17
18func (s *Server) windowsGuestObservation(ctx context.Context, input map[string]any) (guestObservation, error) {
19 var observation guestObservation
20 cfg, err := loadConfig(s.configPath)
21 if err != nil {
22 return observation, err
23 }
24 input["timezone"] = cfg.Timezone
25 cmd, err := guestos.WindowsRelayCommand(ctx, s.profile, "safety")
26 if err != nil {
27 return observation, err
28 }
29 data, err := json.Marshal(input)
30 if err != nil {
31 return observation, err
32 }
33 cmd.Stdin = bytes.NewReader(append(data, '\n'))
34 cmd.Stderr = io.Discard
35 var output bytes.Buffer
36 cmd.Stdout = &boundedWindowsStatusWriter{buffer: &output, remaining: 64 << 10}
37 if err = cmd.Run(); err != nil {
38 return observation, errors.New("Windows 客机检测未返回完整结果")
39 }
40 var failure struct {
41 Error string `json:"error"`
42 Stage string `json:"stage"`
43 Code string `json:"error_code"`
44 NativeCode int `json:"native_code"`
45 }
46 if json.Unmarshal(output.Bytes(), &failure) == nil && failure.Error == "safety_probe_failed" {
47 stages := map[string]bool{"request": true, "environment": true, "network": true, "public-tcp": true, "udp": true, "dns": true, "project-grant": true, "shared-read": true, "shared-write": true, "private-scope": true}
48 codes := map[string]bool{"invalid_probe": true, "access_denied": true, "file_io": true, "socket": true, "probe_failed": true}
49 if stages[failure.Stage] && codes[failure.Code] && failure.NativeCode >= 0 && failure.NativeCode <= 65535 {
50 _ = diagnostics.New(s.profile).Record("warn", "windows-safety", fmt.Sprintf("stage=%s code=%s native_code=%d", failure.Stage, failure.Code, failure.NativeCode))
51 }
52 }
53 return decodeWindowsObservation(output.Bytes())
54}
55
56func decodeWindowsObservation(data []byte) (guestObservation, error) {
57 var observation guestObservation
58 var status struct {
59 Error string `json:"error"`
60 Stage string `json:"stage"`
61 }
62 if json.Unmarshal(data, &status) != nil {
63 return observation, errors.New("Windows 客机检查没有完成,外网保持关闭;请查看具体检查阶段后重试")
64 }
65 if status.Error != "" {
66 stage := map[string]string{"request": "请求校验", "environment": "系统读回", "network": "代理与 DNS", "public-tcp": "公网连接约束", "udp": "UDP 约束", "dns": "公共 DNS 约束", "project-grant": "项目授权", "shared-read": "共享文件读取", "shared-write": "共享文件写回", "private-scope": "私有目录约束"}[status.Stage]
67 if stage == "" {
68 stage = "客机"
69 }
70 return observation, errors.New("Windows " + stage + "检查没有完成,外网保持关闭")
71 }
72 decoder := json.NewDecoder(bytes.NewReader(data))
73 if decoder.Decode(&observation) != nil || decoder.Decode(new(any)) != io.EOF {
74 return observation, errors.New("Windows 客机检测返回无效结果")
75 }
76 if observation.TZ == "" || observation.SystemTimezone == "" || observation.ProcessLocale == "" || observation.SystemLocale == "" || !observation.LocaleValid {
77 return observation, errors.New("Windows 客机检查缺少系统读回结果,外网保持关闭")
78 }
79 return observation, nil
80}
81
82func (s *Server) windowsVMLayout() (bool, bool) {
83 ctx, cancel := s.controlContext()
84 defer cancel()
85 report, err := guestos.InspectHyperV(ctx, s.windowsGuest.VMID)
86 if err != nil || report.VM == nil || report.Lookup != "found" {
87 return false, false
88 }
89 vm := report.VM
90 network := vm.NetworkAdapterCount == 0 && vm.Generation == 2 && vm.SecureBoot && vm.VirtualTPM
91 projects, err := s.projectPaths()
92 if err != nil {
93 return false, network
94 }
95 s.mu.Lock()
96 defer s.mu.Unlock()
97 allowed := make(map[string]bool, len(projects))
98 for _, path := range projects {
99 allowed[path] = true
100 }
101 for path, share := range s.projectShares {
102 if share == nil || share.windowsProject == nil || !allowed[path] {
103 return false, network
104 }
105 select {
106 case <-share.done:
107 return false, network
108 default:
109 }
110 }
111 share := s.projectShares[s.environmentDir()]
112 return share != nil && share.windowsProject != nil && vm.DiskCount == 1 && vm.SingleLocalVHDX != nil && *vm.SingleLocalVHDX, network
113}
114
115func windowsGuestSettingsMatch(observation guestObservation, cfg cage.Config) bool {
116 locale := strings.ReplaceAll(strings.TrimSuffix(cfg.Locale, ".UTF-8"), "_", "-")
117 return observation.LocaleValid && effectiveWindowsZone(observation.SystemTimezone) == effectiveWindowsZone(cfg.Timezone) && effectiveWindowsZone(observation.TZ) == effectiveWindowsZone(cfg.Timezone) && strings.EqualFold(observation.ProcessLocale, locale) && strings.EqualFold(observation.SystemLocale, locale)
118}
119
120func effectiveWindowsZone(zone string) string {
121 switch zone {
122 case "Etc/UTC", "Etc/GMT", "UTC", "GMT":
123 return "UTC"
124 default:
125 return zone
126 }
127}
128
129func (s *Server) windowsBoundaryInput(root, private, shared, challenge string, input map[string]any) error {
130 project, err := s.windowsProjectForHost(root)
131 if err != nil || project == nil {
132 return errors.New("Windows 环境目录尚未连接")
133 }
134 input["project_id"] = project.ID
135 input["grant"] = project.Grant
136 input["shared_name"] = filepath.Base(shared)
137 input["private_name"] = filepath.Base(private)
138 input["challenge"] = challenge
139 delete(input, "private_file")
140 delete(input, "shared_file")
141 return nil
142}
143
144func (s *Server) applyWindowsGuestSettings(ctx context.Context, cfg cage.Config) error {
145 cmd, err := guestos.WindowsRelayCommand(ctx, s.profile, "network-settings")
146 if err != nil {
147 return err
148 }
149 data, err := json.Marshal(windowsGuestSettingsInput(cfg))
150 if err != nil {
151 return err
152 }
153 cmd.Stdin = bytes.NewReader(append(data, '\n'))
154 cmd.Stderr = io.Discard
155 var output bytes.Buffer
156 cmd.Stdout = &boundedWindowsStatusWriter{buffer: &output, remaining: 4096}
157 if err = cmd.Run(); err != nil {
158 return errors.New("Windows 客机系统设置未能完成确认,外网保持关闭;请重试应用并核对实际设置")
159 }
160 var result struct {
161 OK bool `json:"ok"`
162 }
163 if json.Unmarshal(output.Bytes(), &result) != nil || !result.OK {
164 return errors.New("Windows 客机系统设置未得到完成确认")
165 }
166 return nil
167}
168
169func windowsGuestSettingsInput(cfg cage.Config) map[string]string {
170 return map[string]string{"action": "apply", "timezone": cfg.Timezone, "locale": cfg.Locale, "browser_language": cfg.BrowserLanguage}
171}