跳到主要内容

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

浏览文件

windows_host_windows.go

96 行 · 3.6 KB历史下载
1//go:build windows
2
3package guestos
4
5import (
6 "context"
7 "debug/pe"
8 "errors"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "strings"
13 "syscall"
14 "time"
15
16 "golang.org/x/sys/windows"
17)
18
19// probeWindowsHost checks the local platform without requiring VM-wide privileges.
20// It doesn't create a VM or test whether a guest image can be installed.
21func probeWindowsHost(ctx context.Context) (bool, string) {
22 version := windows.RtlGetVersion()
23 const workstation = 1 // VER_NT_WORKSTATION
24 if version.ProductType != workstation || version.MajorVersion < 10 || version.BuildNumber < 22000 {
25 return false, "Windows 原生客机需要 Windows 11 桌面版宿主。"
26 }
27 var processMachine, nativeMachine uint16
28 if err := windows.IsWow64Process2(windows.CurrentProcess(), &processMachine, &nativeMachine); err != nil {
29 return false, "无法读取 Windows 主机的原生处理器架构。"
30 }
31 if runtime.GOARCH != "amd64" || nativeMachine != pe.IMAGE_FILE_MACHINE_AMD64 {
32 return false, "Windows 原生客机需要 Intel 或 AMD x64 主机及 x64 版 Cage。"
33 }
34 ctx, cancel := context.WithTimeout(ctx, 12*time.Second)
35 defer cancel()
36 cmd, err := systemPowerShellQuery(ctx, hyperVHostProbe)
37 if err != nil {
38 return false, "无法定位系统自带的 Windows PowerShell。"
39 }
40 output, err := cmd.Output()
41 if errors.Is(ctx.Err(), context.DeadlineExceeded) {
42 return false, "查询 Hyper-V 超时,请检查本机虚拟机管理服务后重试。"
43 }
44 if ctx.Err() != nil {
45 return false, "已取消 Windows 主机预检。"
46 }
47 if err != nil {
48 var exit *exec.ExitError
49 if errors.As(err, &exit) && exit.ExitCode() == 2 {
50 if reason := strings.TrimSpace(string(output)); reason != "" {
51 return false, reason
52 }
53 }
54 return false, "无法通过系统 PowerShell 查询 Hyper-V,请检查 PowerShell 和 Windows 管理服务。"
55 }
56 if strings.TrimSpace(string(output)) != "ready" {
57 return false, "Hyper-V 预检返回了无法识别的结果。"
58 }
59 return true, "Hyper-V 平台已就绪;首次安装确认系统授权后,Cage 管理助手仅操作已绑定客机。"
60}
61
62func systemPowerShellQuery(ctx context.Context, script string) (*exec.Cmd, error) {
63 systemDir, err := windows.GetSystemDirectory()
64 if err != nil {
65 return nil, err
66 }
67 shell := filepath.Join(systemDir, "WindowsPowerShell", "v1.0", "powershell.exe")
68 cmd := exec.CommandContext(ctx, shell, "-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script)
69 cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
70 return cmd, nil
71}
72
73const hyperVHostProbe = `
74$ErrorActionPreference = 'Stop'
75[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)
76function Unavailable([string]$reason) { Write-Output $reason; exit 2 }
77
78$edition = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -Name EditionID).EditionID
79if ($edition -like 'Core*') {
80 Unavailable 'Windows 家庭版未提供完整的 Hyper-V 角色。此原型支持专业版、企业版和教育版。'
81}
82$service = Get-Service -Name vmms -ErrorAction SilentlyContinue
83if (!$service) {
84 Unavailable '未检测到 Hyper-V 虚拟机管理服务(vmms),请检查 Windows 功能中的 Hyper-V 平台。'
85}
86if ($service.Status -ne 'Running') {
87 Unavailable 'Hyper-V 虚拟机管理服务(vmms)当前未运行。'
88}
89if ((Get-CimInstance Win32_ComputerSystem).HypervisorPresent -ne $true) {
90 Unavailable 'Windows 未检测到正在运行的虚拟机监控程序,请检查固件虚拟化设置和 Hyper-V 启动状态。'
91}
92if (!(Get-Command 'Hyper-V\Get-VMHost' -ErrorAction SilentlyContinue)) {
93 Unavailable '缺少 Hyper-V PowerShell 管理模块,请检查 Windows 功能中的 Hyper-V 管理工具。'
94}
95Write-Output 'ready'
96`