跳到主要内容

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

浏览文件

registry_windows.go

231 行 · 5.3 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package windows
5
6import (
7 "fmt"
8 "math/rand/v2"
9 "slices"
10 "strconv"
11 "strings"
12
13 "golang.org/x/sys/windows/registry"
14)
15
16const (
17 guestCommunicationsPrefix = `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices`
18 magicVSOCKSuffix = "-facb-11e6-bd58-64006a7986d3"
19 wslDistroInfoPrefix = `SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss`
20)
21
22// AddVSockRegistryKey makes a vsock server running on the host accessible in guests.
23func AddVSockRegistryKey(port int) error {
24 rootKey, err := getGuestCommunicationServicesKey(true)
25 if err != nil {
26 return err
27 }
28 defer rootKey.Close()
29
30 used, err := getUsedPorts(rootKey)
31 if err != nil {
32 return err
33 }
34
35 if slices.Contains(used, port) {
36 return fmt.Errorf("port %d in use", port)
37 }
38
39 vsockKeyPath := fmt.Sprintf(`%x%s`, port, magicVSOCKSuffix)
40 vSockKey, _, err := registry.CreateKey(
41 rootKey,
42 vsockKeyPath,
43 registry.ALL_ACCESS,
44 )
45 if err != nil {
46 return fmt.Errorf(
47 "failed to create new key (%s%s): %w",
48 guestCommunicationsPrefix,
49 vsockKeyPath,
50 err,
51 )
52 }
53 defer vSockKey.Close()
54
55 return nil
56}
57
58// RemoveVSockRegistryKey removes entries created by AddVSockRegistryKey.
59func RemoveVSockRegistryKey(port int) error {
60 rootKey, err := getGuestCommunicationServicesKey(true)
61 if err != nil {
62 return err
63 }
64 defer rootKey.Close()
65
66 vsockKeyPath := fmt.Sprintf(`%x%s`, port, magicVSOCKSuffix)
67 if err := registry.DeleteKey(rootKey, vsockKeyPath); err != nil {
68 return fmt.Errorf(
69 "failed to create new key (%s%s): %w",
70 guestCommunicationsPrefix,
71 vsockKeyPath,
72 err,
73 )
74 }
75
76 return nil
77}
78
79// IsVSockPortFree determines if a VSock port has been registered already.
80func IsVSockPortFree(port int) (bool, error) {
81 rootKey, err := getGuestCommunicationServicesKey(false)
82 if err != nil {
83 return false, err
84 }
85 defer rootKey.Close()
86
87 used, err := getUsedPorts(rootKey)
88 if err != nil {
89 return false, err
90 }
91
92 if slices.Contains(used, port) {
93 return false, nil
94 }
95
96 return true, nil
97}
98
99// GetDistroID returns a DistroId GUID corresponding to a Lima instance name.
100func GetDistroID(name string) (string, error) {
101 rootKey, err := registry.OpenKey(
102 registry.CURRENT_USER,
103 wslDistroInfoPrefix,
104 registry.READ,
105 )
106 if err != nil {
107 return "", fmt.Errorf(
108 "failed to open Lxss key (%s): %w",
109 wslDistroInfoPrefix,
110 err,
111 )
112 }
113 defer rootKey.Close()
114
115 keys, err := rootKey.ReadSubKeyNames(-1)
116 if err != nil {
117 return "", fmt.Errorf("failed to read subkey names for %s: %w", wslDistroInfoPrefix, err)
118 }
119
120 var out string
121 for _, k := range keys {
122 subKey, err := registry.OpenKey(
123 registry.CURRENT_USER,
124 fmt.Sprintf(`%s\%s`, wslDistroInfoPrefix, k),
125 registry.READ,
126 )
127 if err != nil {
128 return "", fmt.Errorf("failed to read subkey %#q for key %#q: %w", k, wslDistroInfoPrefix, err)
129 }
130 dn, _, err := subKey.GetStringValue("DistributionName")
131 if err != nil {
132 return "", fmt.Errorf("failed to read 'DistributionName' value for subkey %#q of %#q: %w", k, wslDistroInfoPrefix, err)
133 }
134 if dn == name {
135 out = k
136 break
137 }
138 }
139
140 if out == "" {
141 return "", fmt.Errorf("failed to find matching DistroID for %#q", name)
142 }
143
144 return out, nil
145}
146
147// GetRandomFreeVSockPort gets a list of all registered VSock ports and returns a non-registered port.
148func GetRandomFreeVSockPort(minPort, maxPort int) (int, error) {
149 rootKey, err := getGuestCommunicationServicesKey(false)
150 if err != nil {
151 return 0, err
152 }
153 defer rootKey.Close()
154
155 used, err := getUsedPorts(rootKey)
156 if err != nil {
157 return 0, err
158 }
159
160 type pair struct{ v, offset int }
161 tree := make([]pair, 1, len(used)+1)
162 tree[0] = pair{0, minPort}
163
164 slices.Sort(used)
165 for i, v := range used {
166 if tree[len(tree)-1].v+tree[len(tree)-1].offset == v {
167 tree[len(tree)-1].offset++
168 } else {
169 tree = append(tree, pair{v - minPort - i, minPort + i + 1})
170 }
171 }
172
173 v := rand.IntN(maxPort - minPort + 1 - len(used))
174
175 for len(tree) > 1 {
176 m := len(tree) / 2
177 if v < tree[m].v {
178 tree = tree[:m]
179 } else {
180 tree = tree[m:]
181 }
182 }
183
184 return tree[0].offset + v, nil
185}
186
187// getGuestCommunicationServicesKey returns the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices
188// registry key for use in other operations.
189//
190// allowWrite is configurable because setting it to true requires Administrator access.
191func getGuestCommunicationServicesKey(allowWrite bool) (registry.Key, error) {
192 var registryPermissions uint32 = registry.READ
193 if allowWrite {
194 registryPermissions = registry.WRITE | registry.READ
195 }
196 rootKey, err := registry.OpenKey(
197 registry.LOCAL_MACHINE,
198 guestCommunicationsPrefix,
199 registryPermissions,
200 )
201 if err != nil {
202 return 0, fmt.Errorf(
203 "failed to open GuestCommunicationServices key (%s): %w",
204 guestCommunicationsPrefix,
205 err,
206 )
207 }
208
209 return rootKey, nil
210}
211
212func getUsedPorts(key registry.Key) ([]int, error) {
213 keys, err := key.ReadSubKeyNames(-1)
214 if err != nil {
215 return nil, fmt.Errorf("failed to read subkey names for %s: %w", guestCommunicationsPrefix, err)
216 }
217
218 out := []int{}
219 for _, k := range keys {
220 split := strings.Split(k, magicVSOCKSuffix)
221 if len(split) == 2 {
222 i, err := strconv.Atoi(split[0])
223 if err != nil {
224 return nil, fmt.Errorf("failed convert %#q to int: %w", split[0], err)
225 }
226 out = append(out, i)
227 }
228 }
229
230 return out, nil
231}