跳到主要内容

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

浏览文件

windows_lifecycle_windows.go

64 行 · 1.8 KB历史下载
1//go:build windows
2
3package guestos
4
5import (
6 "context"
7 "encoding/json"
8 "errors"
9 "fmt"
10
11 "github.com/google/uuid"
12)
13
14// WindowsVMAction controls only the already paired VM. It never creates,
15// discovers, force-powers-off, removes, or changes a guest's network adapters.
16func WindowsVMAction(ctx context.Context, root, action string) error {
17 if action != "start" && action != "stop" {
18 return errors.New("Windows 客机操作无效")
19 }
20 binding, err := LoadWindowsProfile(root)
21 if err != nil {
22 return err
23 }
24 if binding == nil {
25 return errors.New("此配置尚未绑定 Windows 客机")
26 }
27 operation := action
28 if operation == "stop" {
29 operation = "shutdown"
30 }
31 var result WindowsVMResourceState
32 if err := windowsHostManagerCall(ctx, binding.VMID, operation, 0, 0, &result); err != nil {
33 return err
34 }
35 if action == "start" && result.State != "Running" || action == "stop" && result.State != "Off" {
36 return errors.New("Windows 客机未到达预期状态")
37 }
38 return nil
39}
40
41func windowsVMActionNative(ctx context.Context, id uuid.UUID, action string) error {
42 if id == uuid.Nil || action != "start" && action != "stop" {
43 return errors.New("Windows 客机操作无效")
44 }
45 script := fmt.Sprintf("$targetId = [guid]'%s'\n$action = '%s'\n", id.String(), action) + windowsVMLifecycleScript
46 cmd, err := systemPowerShellQuery(ctx, script)
47 if err != nil {
48 return err
49 }
50 data, err := cmd.Output()
51 if ctx.Err() != nil {
52 return ctx.Err()
53 }
54 if err != nil {
55 return errors.New("Windows 客机操作未完成,请检查绑定虚拟机的状态与管理权限")
56 }
57 var result struct {
58 State string `json:"state"`
59 }
60 if json.Unmarshal(data, &result) != nil || (action == "start" && result.State != "Running") || (action == "stop" && result.State != "Off") {
61 return errors.New("Windows 客机未到达预期状态")
62 }
63 return nil
64}