跳到主要内容

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

浏览文件

windows_profile.go

117 行 · 4.1 KB历史下载
1package guestos
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "io"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "regexp"
13 "strings"
14
15 "github.com/google/uuid"
16)
17
18// WindowsProfile binds one Hyper-V guest to its ordinary GUI user and the
19// current host user's protected pairing. It contains no authentication secret.
20type WindowsProfile struct {
21 Version int `json:"version"`
22 VMID uuid.UUID `json:"vm_id"`
23 GUIUserSID string `json:"gui_user_sid"`
24 GUIUser string `json:"gui_user,omitempty"`
25 InstanceDirectory string `json:"instance_directory"`
26 RelayExecutable string `json:"relay_executable"`
27 PairingFile string `json:"pairing_file"`
28 NetworkPairingFile string `json:"network_pairing_file,omitempty"`
29}
30
31var windowsGUISID = regexp.MustCompile(`^S-1-5-21-[0-9]+-[0-9]+-[0-9]+-[0-9]+$`)
32
33func WindowsProfilePath(root string) string { return filepath.Join(root, "windows-guest.json") }
34
35func (p WindowsProfile) validate() error {
36 if p.Version != 1 || p.VMID == uuid.Nil || !windowsGUISID.MatchString(p.GUIUserSID) ||
37 len(p.GUIUser) > 256 || strings.ContainsAny(p.GUIUser, "\x00\r\n") {
38 return errors.New("Windows 客机标识或图形用户无效")
39 }
40 for _, path := range []string{p.InstanceDirectory, p.RelayExecutable, p.PairingFile} {
41 if !filepath.IsAbs(path) || strings.ContainsAny(path, "\x00\r\n") {
42 return errors.New("Windows 客机配置需要绝对本机路径")
43 }
44 }
45 if p.NetworkPairingFile != "" && (!filepath.IsAbs(p.NetworkPairingFile) || strings.ContainsAny(p.NetworkPairingFile, "\x00\r\n")) {
46 return errors.New("Windows 客机网络配对需要绝对本机路径")
47 }
48 return nil
49}
50
51func LoadWindowsProfile(root string) (*WindowsProfile, error) {
52 path := WindowsProfilePath(root)
53 info, err := os.Lstat(path)
54 if errors.Is(err, os.ErrNotExist) {
55 return nil, nil
56 }
57 if err != nil {
58 return nil, err
59 }
60 if !info.Mode().IsRegular() || info.Size() == 0 || info.Size() > 16<<10 {
61 return nil, errors.New("Windows 客机配置文件无效")
62 }
63 data, err := os.ReadFile(path)
64 if err != nil {
65 return nil, err
66 }
67 var binding WindowsProfile
68 decoder := json.NewDecoder(bytes.NewReader(data))
69 decoder.DisallowUnknownFields()
70 if decoder.Decode(&binding) != nil || decoder.Decode(new(any)) != io.EOF {
71 return nil, errors.New("Windows 客机配置格式无效")
72 }
73 if err := binding.validate(); err != nil {
74 return nil, err
75 }
76 return &binding, nil
77}
78
79// BindWindowsProfile publishes an independently verified VM/SID binding.
80// Windows provisioning owns verification and DPAPI pairing; this never discovers
81// a user or VM by scanning the machine and never replaces an existing binding.
82func BindWindowsProfile(root string, binding WindowsProfile) error {
83 if err := binding.validate(); err != nil {
84 return err
85 }
86 data, err := json.Marshal(binding)
87 if err != nil {
88 return err
89 }
90 if err := publishBinding(root, "windows-guest.json", append(data, '\n')); err != nil {
91 return err
92 }
93 return protectWindowsProfile(WindowsProfilePath(root))
94}
95
96// WindowsRelayCommand carries only the protected profile path and operation.
97// File bytes and UTF-8 path arrays travel in stdin, never a shell or command line.
98func WindowsRelayCommand(ctx context.Context, root, operation string, args ...string) (*exec.Cmd, error) {
99 p, err := LoadWindowsProfile(root)
100 if err != nil {
101 return nil, err
102 }
103 if p == nil {
104 return nil, errors.New("此配置尚未绑定 Windows 客机")
105 }
106 switch operation {
107 case "versions":
108 case "stream", "status", "import", "export", "terminal", "browser", "desktop", "attachments", "audio", "resources", "diagnostics", "safety", "project-mount", "network-stream", "network-settings", "cli":
109 default:
110 return nil, errors.New("Windows 客机剪贴板操作无效")
111 }
112 if len(args) != 0 && !(operation == "export" && len(args) == 1 && args[0] == "--paths-stdin") {
113 return nil, errors.New("Windows 客机文件路径必须通过标准输入传递")
114 }
115 argv := append([]string{"--relay-profile", WindowsProfilePath(root), operation}, args...)
116 return exec.CommandContext(ctx, p.RelayExecutable, argv...), nil
117}