跳到主要内容

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

浏览文件

native_tight.go

114 行 · 4.4 KB历史下载
1package workbench
2
3import (
4 "bytes"
5 "encoding/base64"
6 "encoding/binary"
7 "errors"
8 "image"
9)
10
11// Tight carries the guest's encoded image directly to noVNC. The host checks dimensions
12// and placement without decompressing pixels or compressing them a second time.
13func nativeTightRectangles(frame windowsWindowFrame, encoding int32) ([]byte, uint16, bool, error) {
14 if len(frame.Overlays) != 0 {
15 return nativeTightOverlayRectangles(frame, encoding)
16 }
17 if frame.Width < 1 || frame.Height < 1 || frame.Width > 8192 || frame.Height > 8192 ||
18 int64(frame.Width)*int64(frame.Height) > 40_000_000 || len(frame.Target) == 0 || len(frame.Target) > 256 {
19 return nil, 0, false, errors.New("应用画面尺寸无效")
20 }
21 panes := frame.Panes
22 if len(panes) != 0 && (len(panes) != 2 || frame.JPEG != "" || frame.PNG != "") {
23 return nil, 0, false, errors.New("应用画面分屏无效")
24 }
25 if len(panes) == 2 && (panes[0].X != 0 || panes[1].X != panes[0].Width ||
26 panes[0].Width+panes[1].Width != frame.Width || panes[0].Y != 0 || panes[1].Y != 0 ||
27 panes[0].Height != frame.Height || panes[1].Height != frame.Height) {
28 return nil, 0, false, nil
29 }
30 if len(panes) == 0 {
31 panes = []windowsWindowPane{{windowsWindowFrame: frame}}
32 }
33 if len(panes) > 2 {
34 return nil, 0, false, errors.New("应用画面分屏无效")
35 }
36 var output []byte
37 var previous image.Rectangle
38 for index, pane := range panes {
39 encoded, expected, control := pane.JPEG, "jpeg", byte(0x90)
40 if pane.PNG != "" {
41 if encoding != -260 {
42 return nil, 0, false, nil
43 }
44 encoded, expected, control = pane.PNG, "png", 0xa0
45 }
46 if encoded == "" || len(pane.Panes) != 0 || len(pane.Overlays) != 0 {
47 return nil, 0, false, nil
48 }
49 if len(encoded) > (8<<20)*4/3+4 {
50 return nil, 0, false, errors.New("应用画面过大")
51 }
52 data, err := base64.StdEncoding.DecodeString(encoded)
53 if err != nil {
54 return nil, 0, false, err
55 }
56 if len(data) > 0x3fffff {
57 return nil, 0, false, nil
58 }
59 config, format, err := image.DecodeConfig(bytes.NewReader(data))
60 bounds := image.Rect(pane.X, pane.Y, pane.X+pane.Width, pane.Y+pane.Height)
61 if err != nil || format != expected || config.Width != pane.Width || config.Height != pane.Height ||
62 !bounds.In(image.Rect(0, 0, frame.Width, frame.Height)) || index > 0 && !previous.Intersect(bounds).Empty() {
63 return nil, 0, false, errors.New("应用画面位置或编码无效")
64 }
65 previous = bounds
66 header := windowRFBRect(pane.Width, pane.Height, encoding)
67 binary.BigEndian.PutUint16(header[0:2], uint16(pane.X))
68 binary.BigEndian.PutUint16(header[2:4], uint16(pane.Y))
69 output = append(output, header...)
70 output = append(output, control)
71 size := len(data)
72 for i := 0; i < 2 && size >= 128; i++ {
73 output = append(output, byte(size&127)|128)
74 size >>= 7
75 }
76 output = append(output, byte(size))
77 output = append(output, data...)
78 }
79 return output, uint16(len(panes)), true, nil
80}
81
82// Redraw the complete base before its overlays, preserving alpha and stacking
83// order without decoding and recompressing the entire desktop on the host.
84func nativeTightOverlayRectangles(frame windowsWindowFrame, encoding int32) ([]byte, uint16, bool, error) {
85 if len(frame.Overlays) > 16 {
86 return nil, 0, false, errors.New("应用覆盖画面无效")
87 }
88 overlays := frame.Overlays
89 frame.Overlays = nil
90 output, count, supported, err := nativeTightRectangles(frame, encoding)
91 if err != nil || !supported {
92 return nil, 0, supported, err
93 }
94 for _, overlay := range overlays {
95 if len(overlay.Panes) != 0 || len(overlay.Overlays) != 0 || overlay.Width > frame.Width || overlay.Height > frame.Height || overlay.X < -frame.Width || overlay.Y < -frame.Height || overlay.X > frame.Width || overlay.Y > frame.Height {
96 return nil, 0, false, errors.New("应用覆盖位置无效")
97 }
98 overlay.Target = frame.Target
99 part, n, ok, err := nativeTightRectangles(overlay.windowsWindowFrame, encoding)
100 if err != nil || !ok {
101 return nil, 0, ok, err
102 }
103 bounds := image.Rect(overlay.X, overlay.Y, overlay.X+overlay.Width, overlay.Y+overlay.Height)
104 if !bounds.In(image.Rect(0, 0, frame.Width, frame.Height)) {
105 // Keep the existing compositor for partially clipped overlays.
106 return nil, 0, false, nil
107 }
108 binary.BigEndian.PutUint16(part[0:2], uint16(overlay.X))
109 binary.BigEndian.PutUint16(part[2:4], uint16(overlay.Y))
110 output = append(output, part...)
111 count += n
112 }
113 return output, count, true, nil
114}