提交记录修改internal/maclogin/maintenance.go+41 −0看文件
修改internal/maclogin/maintenance_darwin_arm64.go+1 −8看文件
修改internal/maclogin/maintenance_test.go+16 −0看文件
修改scripts/guest-os/macos-maintenance.swift+21 −7看文件
| @@ -4,7 +4,10 @@ import ( | ||
| 4 | 4 | "crypto/sha256" |
| 5 | 5 | "encoding/hex" |
| 6 | 6 | "encoding/json" |
| 7 | 新增: "fmt" | |
| 7 | 8 | "math" |
| 9 | 新增: "regexp" | |
| 10 | 新增: "strings" | |
| 8 | 11 | "time" |
| 9 | 12 | |
| 10 | 13 | "github.com/google/uuid" |
| @@ -20,9 +23,47 @@ type maintenanceWindow struct { | ||
| 20 | 23 | Rect [4]float64 `json:"rect"` |
| 21 | 24 | Display [4]float64 `json:"display"` |
| 22 | 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"` | |
| 23 | 29 | PasswordFocused bool `json:"password_focused"` |
| 24 | 30 | } |
| 25 | 31 | |
| 32 | 新增:var maintenanceBundleID = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9.-]{0,127}$`) | |
| 33 | 新增: | |
| 34 | 新增:func (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 | 新增: | |
| 26 | 67 | func (w maintenanceWindow) valid(user string) bool { |
| 27 | 68 | if w.Version != 1 || w.Error != "" || w.User != user || w.PID <= 0 || w.Window == 0 || |
| 28 | 69 | (w.Bundle != "com.apple.systempreferences" && w.Bundle != "com.apple.authorizationhost") { |
| @@ -17,7 +17,6 @@ import ( | ||
| 17 | 17 | "errors" |
| 18 | 18 | "io" |
| 19 | 19 | "os/exec" |
| 20 | 删除: "strings" | |
| 21 | 20 | "time" |
| 22 | 21 | |
| 23 | 22 | "al.essio.dev/pkg/shellescape" |
| @@ -86,13 +85,7 @@ func (s *loginSession) maintenanceProbe(open bool) (maintenanceWindow, error) { | ||
| 86 | 85 | return result, errors.New("客机维护组件需要更新,请在环境设置中更新组件") |
| 87 | 86 | } |
| 88 | 87 | if result.Error != "" { |
| 89 | 删除: message := "请保持系统设置或系统授权窗口位于客机前台,再重新打开维护画面" | |
| 90 | 删除: if result.Error == "requires_gui_login" { | |
| 91 | 删除: message = "请先登录并解锁虚拟 Mac 的客机账户" | |
| 92 | 删除: } else if strings.Contains(result.Error, "obscured") { | |
| 93 | 删除: message = "授权窗口被其他窗口遮挡,请关闭遮挡窗口后重新打开维护画面" | |
| 94 | 删除: } | |
| 95 | 删除: return result, errors.New(message) | |
| 88 | 新增: return result, errors.New(result.probeMessage()) | |
| 96 | 89 | } |
| 97 | 90 | if !open && !result.valid(user) { |
| 98 | 91 | return result, errors.New("客机授权窗口身份无法确认,请重新打开维护画面") |
| @@ -2,6 +2,7 @@ package maclogin | ||
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | 4 | "math" |
| 5 | 新增: "strings" | |
| 5 | 6 | "testing" |
| 6 | 7 | "time" |
| 7 | 8 | ) |
| @@ -126,3 +127,18 @@ func TestMaintenancePasswordRejectsChangedPanelAndSecureFocus(t *testing.T) { | ||
| 126 | 127 | t.Fatal("verified secure focus required an unnecessary manual selection") |
| 127 | 128 | } |
| 128 | 129 | } |
| 130 | 新增: | |
| 131 | 新增:func TestMaintenanceProbeDistinguishesBlockedWindow(t *testing.T) { | |
| 132 | 新增: w := maintenanceWindow{Error: "maintenance_window_obscured", BlockingPID: 42, BlockingWindow: 7, BlockingBundle: "com.apple.notificationcenterui"} | |
| 133 | 新增: if message := w.probeMessage(); !strings.Contains(message, "通知中心 PID 42 窗口 7") { | |
| 134 | 新增: t.Fatal(message) | |
| 135 | 新增: } | |
| 136 | 新增: w.BlockingBundle = "unexpected\nwindow title" | |
| 137 | 新增: if strings.Contains(w.probeMessage(), "unexpected") { | |
| 138 | 新增: t.Fatal("invalid bundle identifier reported") | |
| 139 | 新增: } | |
| 140 | 新增: w.Error = "maintenance_window_unavailable" | |
| 141 | 新增: if strings.Contains(w.probeMessage(), "遮挡") { | |
| 142 | 新增: t.Fatal("a missing window was attributed to an obstruction") | |
| 143 | 新增: } | |
| 144 | 新增:} | |
| @@ -85,14 +85,18 @@ enum MacMaintenance { | ||
| 85 | 85 | guard NSWorkspace.shared.open(url) else { throw Failure(8, "maintenance_open_failed") } |
| 86 | 86 | return ["opened": true] |
| 87 | 87 | } |
| 88 | 删除: guard operation == "probe", let front = NSWorkspace.shared.frontmostApplication, verified(front) else { | |
| 88 | 新增: guard operation == "probe", let front = NSWorkspace.shared.frontmostApplication else { | |
| 89 | 89 | throw Failure(7, "maintenance_foreground_changed") |
| 90 | 90 | } |
| 91 | 新增: if !verified(front) { | |
| 92 | 新增: return ["error": "maintenance_foreground_changed", "blocking_pid": front.processIdentifier, | |
| 93 | 新增: "blocking_bundle": front.bundleIdentifier ?? ""] | |
| 94 | 新增: } | |
| 91 | 95 | guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else { |
| 92 | 96 | throw Failure(7, "maintenance_window_unavailable") |
| 93 | 97 | } |
| 94 | 98 | let display = CGDisplayBounds(CGMainDisplayID()) |
| 95 | 删除: var covering: [CGRect] = [] | |
| 99 | 新增: var covering: [(bounds: CGRect, pid: Int32?, window: UInt32?)] = [] | |
| 96 | 100 | for window in windows { |
| 97 | 101 | guard let raw = window[kCGWindowBounds as String] as? [String: Any], |
| 98 | 102 | let bounds = CGRect(dictionaryRepresentation: raw as CFDictionary), bounds.width > 0, bounds.height > 0, |
| @@ -101,17 +105,27 @@ enum MacMaintenance { | ||
| 101 | 105 | if owner == front.processIdentifier && bounds.width >= 100 && bounds.height >= 60 { |
| 102 | 106 | let region = bounds.intersection(display) |
| 103 | 107 | guard !region.isNull, region.width >= 80, region.height >= 40, |
| 104 | 删除: !covering.contains(where: { $0.intersects(region) }), | |
| 105 | 删除: let id = window[kCGWindowNumber as String] as? UInt32, | |
| 106 | 删除: NSWorkspace.shared.frontmostApplication?.processIdentifier == front.processIdentifier, | |
| 107 | 删除: try consoleSession() == session else { throw Failure(7, "maintenance_window_obscured") } | |
| 108 | 新增: let id = window[kCGWindowNumber as String] as? UInt32 else { | |
| 109 | 新增: throw Failure(7, "maintenance_window_unavailable") | |
| 110 | 新增: } | |
| 111 | 新增: guard try consoleSession() == session else { throw Failure(4, "requires_gui_login") } | |
| 112 | 新增: guard NSWorkspace.shared.frontmostApplication?.processIdentifier == front.processIdentifier else { | |
| 113 | 新增: throw Failure(7, "maintenance_foreground_changed") | |
| 114 | 新增: } | |
| 115 | 新增: if let blocker = covering.first(where: { $0.bounds.intersects(region) }) { | |
| 116 | 新增: let bundle = blocker.pid.flatMap { NSRunningApplication(processIdentifier: $0)?.bundleIdentifier } | |
| 117 | 新增: return ["error": "maintenance_window_obscured", "blocking_pid": blocker.pid ?? 0, | |
| 118 | 新增: "blocking_window": blocker.window ?? 0, "blocking_bundle": bundle ?? ""] | |
| 119 | 新增: } | |
| 108 | 120 | return ["version": 1, "session": session, "user": NSUserName(), "pid": front.processIdentifier, |
| 109 | 121 | "window": id, "bundle": front.bundleIdentifier!, |
| 110 | 122 | "password_focused": passwordFocused(front, window: bounds), |
| 111 | 123 | "rect": [region.minX, region.minY, region.width, region.height], |
| 112 | 124 | "display": [display.minX, display.minY, display.width, display.height]] |
| 113 | 125 | } |
| 114 | 删除: if owner != front.processIdentifier { covering.append(bounds) } | |
| 126 | 新增: if owner != front.processIdentifier { | |
| 127 | 新增: covering.append((bounds, owner, window[kCGWindowNumber as String] as? UInt32)) | |
| 128 | 新增: } | |
| 115 | 129 | } |
| 116 | 130 | throw Failure(7, "maintenance_window_unavailable") |
| 117 | 131 | } |

