跳到主要内容

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

浏览文件

maintenance.go

122 行 · 4.4 KB历史下载
1package maclogin
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "encoding/json"
7 "fmt"
8 "math"
9 "regexp"
10 "strings"
11 "time"
12
13 "github.com/google/uuid"
14)
15
16type maintenanceWindow struct {
17 Version int `json:"version"`
18 Session string `json:"session"`
19 User string `json:"user"`
20 PID int32 `json:"pid"`
21 Window uint32 `json:"window"`
22 Bundle string `json:"bundle"`
23 Rect [4]float64 `json:"rect"`
24 Display [4]float64 `json:"display"`
25 Error string `json:"error"`
26 BlockingPID int32 `json:"blocking_pid,omitempty"`
27 BlockingWindow uint32 `json:"blocking_window,omitempty"`
28 BlockingBundle string `json:"blocking_bundle,omitempty"`
29 PasswordFocused bool `json:"password_focused"`
30}
31
32var maintenanceBundleID = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9.-]{0,127}$`)
33
34func (w maintenanceWindow) probeMessage() string {
35 var message string
36 switch w.Error {
37 case "requires_gui_login":
38 return "请先登录并解锁虚拟 Mac 的客机账户"
39 case "maintenance_window_unavailable":
40 return "还没有可显示的系统授权窗口,请重新打开客机授权设置"
41 case "maintenance_window_obscured":
42 message = "授权窗口被其他客机窗口遮挡"
43 case "maintenance_foreground_changed":
44 message = "客机前台已切换到其他窗口"
45 default:
46 return "请保持系统设置或系统授权窗口位于客机前台,再重新打开维护画面"
47 }
48 detail := ""
49 if maintenanceBundleID.MatchString(w.BlockingBundle) {
50 detail = w.BlockingBundle
51 if detail == "com.apple.notificationcenterui" {
52 detail = "通知中心"
53 }
54 }
55 if w.BlockingPID > 0 {
56 detail += fmt.Sprintf(" PID %d", w.BlockingPID)
57 }
58 if w.BlockingWindow > 0 {
59 detail += fmt.Sprintf(" 窗口 %d", w.BlockingWindow)
60 }
61 if detail != "" {
62 message += "(" + strings.TrimSpace(detail) + ")"
63 }
64 return message + ",请重新打开客机授权设置"
65}
66
67func (w maintenanceWindow) valid(user string) bool {
68 if w.Version != 1 || w.Error != "" || w.User != user || w.PID <= 0 || w.Window == 0 ||
69 (w.Bundle != "com.apple.systempreferences" && w.Bundle != "com.apple.authorizationhost") {
70 return false
71 }
72 id, err := uuid.Parse(w.Session)
73 if err != nil || id == uuid.Nil {
74 return false
75 }
76 for _, value := range append(w.Rect[:], w.Display[:]...) {
77 if math.IsNaN(value) || math.IsInf(value, 0) || math.Abs(value) > 32768 {
78 return false
79 }
80 }
81 return w.Display[2] > 0 && w.Display[3] > 0 && math.Abs(w.Display[2]/w.Display[3]-1.6) < 0.001 && w.Rect[2] >= 80 && w.Rect[3] >= 40 &&
82 w.Rect[0] >= w.Display[0] && w.Rect[1] >= w.Display[1] &&
83 w.Rect[0]+w.Rect[2] <= w.Display[0]+w.Display[2] && w.Rect[1]+w.Rect[3] <= w.Display[1]+w.Display[3]
84}
85
86func (w maintenanceWindow) geometry() string {
87 w.PasswordFocused = false
88 data, _ := json.Marshal(w)
89 digest := sha256.Sum256(data)
90 return hex.EncodeToString(digest[:16])
91}
92
93// Round inward so the image and accepted coordinates remain inside the window.
94func (w maintenanceWindow) region() [4]int {
95 x := int(math.Ceil((w.Rect[0] - w.Display[0]) / w.Display[2] * 1024))
96 y := int(math.Ceil((w.Rect[1] - w.Display[1]) / w.Display[3] * 640))
97 right := int(math.Floor((w.Rect[0] + w.Rect[2] - w.Display[0]) / w.Display[2] * 1024))
98 bottom := int(math.Floor((w.Rect[1] + w.Rect[3] - w.Display[1]) / w.Display[3] * 640))
99 return [4]int{x, y, right - x, bottom - y}
100}
101
102func (w maintenanceWindow) contains(x, y int) bool {
103 r := w.region()
104 return x >= r[0] && y >= r[1] && x < r[0]+r[2] && y < r[1]+r[3]
105}
106
107func (selection *passwordSelection) selectMaintenance(window maintenanceWindow, target passwordTarget, user string, x, y int, now time.Time) {
108 *selection = passwordSelection{}
109 if target.valid(user) && target.SelectionRequired && window.geometry() == target.geometry() && target.contains(x, y) {
110 *selection = passwordSelection{Geometry: target.geometry(), X: x, Y: y, Clicked: now}
111 }
112}
113
114func (selection *passwordSelection) consumeMaintenance(window maintenanceWindow, target passwordTarget, user string, now time.Time) (passwordSelection, bool) {
115 selected := *selection
116 *selection = passwordSelection{}
117 return selected, window.permitsPassword(target, selected, user, now)
118}
119
120func (window maintenanceWindow) permitsPassword(target passwordTarget, selection passwordSelection, user string, now time.Time) bool {
121 return window.geometry() == target.geometry() && selection.permits(target, user, now)
122}