提交记录修改internal/workbench/windows_browser.go+53 −2看文件
修改internal/workbench/windows_browser_client.go+17 −1看文件
修改internal/workbench/windows_browser_client_test.go+27 −0看文件
新增internal/workbench/windows_browser_focus_test.go+220 −0看文件
修改internal/workbench/windows_display.go+59 −2看文件
新增internal/workbench/windows_display_transition_test.go+115 −0看文件
| @@ -263,23 +263,31 @@ func (s *Server) nativeBrowserVNC(w http.ResponseWriter, r *http.Request) { | ||
| 263 | 263 | } |
| 264 | 264 | defer func() { release() }() |
| 265 | 265 | var current windowsWindowFrame |
| 266 | 新增: inputSuspended := false | |
| 266 | 267 | request := func(input map[string]any, output any) error { |
| 267 | 268 | var data []byte |
| 268 | 269 | var err error |
| 269 | 270 | switch input["action"] { |
| 270 | 271 | case "frame": |
| 271 | 删除: data, err = client.request(ctx, "GET", "/frame", input) | |
| 272 | 新增: if inputSuspended { | |
| 273 | 新增: delete(input, "after") // Require a fresh proof before accepting input. | |
| 274 | 新增: } | |
| 275 | 新增: data, err = requestBrowserFrame(ctx, client.request, input) | |
| 272 | 276 | if err == nil { |
| 273 | 277 | var next windowsWindowFrame |
| 274 | 278 | if err = json.Unmarshal(data, &next); err == nil { |
| 275 | 279 | if !next.Unchanged { |
| 276 | 280 | current = next |
| 281 | 新增: inputSuspended = false | |
| 277 | 282 | } |
| 278 | 283 | *output.(*windowsWindowFrame) = next |
| 279 | 284 | } |
| 280 | 285 | } |
| 281 | 286 | return err |
| 282 | 287 | case "pointer", "key": |
| 288 | 新增: if inputSuspended { | |
| 289 | 新增: return nil // Drop queued events bound to the rejected old frame. | |
| 290 | 新增: } | |
| 283 | 291 | input["kind"] = input["action"] |
| 284 | 292 | delete(input, "action") |
| 285 | 293 | s.mu.Lock() |
| @@ -290,8 +298,14 @@ func (s *Server) nativeBrowserVNC(w http.ResponseWriter, r *http.Request) { | ||
| 290 | 298 | input["keysym"] = windowsGuestKeysym(key, keyboard) |
| 291 | 299 | } |
| 292 | 300 | } |
| 293 | 删除: data, err = client.request(ctx, "POST", "/input", input) | |
| 301 | 新增: data, err = recoverBrowserInputFocus(ctx, client.request, input, func() { | |
| 302 | 新增: inputSuspended = true | |
| 303 | 新增: client.releaseViewerInput() | |
| 304 | 新增: }) | |
| 294 | 305 | case "text": |
| 306 | 新增: if inputSuspended { | |
| 307 | 新增: return nil | |
| 308 | 新增: } | |
| 295 | 309 | data, err = client.request(ctx, "POST", "/insert-text", map[string]any{"id": current.ID, "document": current.Document, "text": input["text"]}) |
| 296 | 310 | default: |
| 297 | 311 | return errors.New("浏览器画面操作无效") |
| @@ -332,3 +346,40 @@ func (s *Server) nativeBrowserVNC(w http.ResponseWriter, r *http.Request) { | ||
| 332 | 346 | s.streamVNC(w, r, local, local, func() { cancel(); local.Close(); remote.Close(); release() }) |
| 333 | 347 | <-done |
| 334 | 348 | } |
| 349 | 新增: | |
| 350 | 新增:// Focus rejection is not a broken display. Drop the rejected event rather than | |
| 351 | 新增:// replaying it: a key or click may already have been partially delivered. A | |
| 352 | 新增:// host blur also releases keys, so recovery must never steal focus in response. | |
| 353 | 新增:// The guest's foreground-window and input-target validation stay unchanged. | |
| 354 | 新增:func recoverBrowserInputFocus(ctx context.Context, request func(context.Context, string, string, any) ([]byte, error), input map[string]any, release func()) ([]byte, error) { | |
| 355 | 新增: data, err := request(ctx, "POST", "/input", input) | |
| 356 | 新增: var failure windowsBrowserFailure | |
| 357 | 新增: if !errors.As(err, &failure) || (failure.code != "browser_focus_required" && failure.code != "browser_target_changed" && failure.code != "target_changed") { | |
| 358 | 新增: return data, err | |
| 359 | 新增: } | |
| 360 | 新增: // End this viewer's input lease so a rejected key-up cannot leave a held | |
| 361 | 新增: // key behind. The persistent guest browser survives; the next frame opens | |
| 362 | 新增: // a fresh authenticated relay without interrupting the host display socket. | |
| 363 | 新增: release() | |
| 364 | 新增: return nil, nil | |
| 365 | 新增:} | |
| 366 | 新增: | |
| 367 | 新增:// A window can move between the guest's before/after capture checks. Retry | |
| 368 | 新增:// only that read, never an input event. Every attempt still runs the guest's | |
| 369 | 新增:// full identity/session validation; persistent transitions remain an error. | |
| 370 | 新增:func requestBrowserFrame(ctx context.Context, request func(context.Context, string, string, any) ([]byte, error), input map[string]any) ([]byte, error) { | |
| 371 | 新增: for attempt := 0; ; attempt++ { | |
| 372 | 新增: data, err := request(ctx, "GET", "/frame", input) | |
| 373 | 新增: var failure windowsBrowserFailure | |
| 374 | 新增: if attempt >= 5 || !errors.As(err, &failure) || !transientWindowFrame(failure.code) { | |
| 375 | 新增: return data, err | |
| 376 | 新增: } | |
| 377 | 新增: timer := time.NewTimer(80 * time.Millisecond) | |
| 378 | 新增: select { | |
| 379 | 新增: case <-ctx.Done(): | |
| 380 | 新增: timer.Stop() | |
| 381 | 新增: return nil, ctx.Err() | |
| 382 | 新增: case <-timer.C: | |
| 383 | 新增: } | |
| 384 | 新增: } | |
| 385 | 新增:} | |
| @@ -29,6 +29,12 @@ type windowsBrowserClient struct { | ||
| 29 | 29 | output *bufio.Scanner |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 新增:// Keep the guest's structured failure code: the display can distinguish a | |
| 33 | 新增:// rejected input from a broken or untrusted frame transport. | |
| 34 | 新增:type windowsBrowserFailure struct{ code, message string } | |
| 35 | 新增: | |
| 36 | 新增:func (e windowsBrowserFailure) Error() string { return e.message } | |
| 37 | 新增: | |
| 32 | 38 | func newWindowsBrowserClient(parent context.Context, profile string) *windowsBrowserClient { |
| 33 | 39 | ctx, cancel := context.WithCancel(parent) |
| 34 | 40 | return &windowsBrowserClient{ctx: ctx, cancel: cancel, start: func(ctx context.Context) (*exec.Cmd, error) { |
| @@ -165,6 +171,12 @@ func (c *windowsBrowserClient) close() { | ||
| 165 | 171 | c.disconnectLocked() |
| 166 | 172 | } |
| 167 | 173 | |
| 174 | 新增:func (c *windowsBrowserClient) releaseViewerInput() { | |
| 175 | 新增: c.mu.Lock() | |
| 176 | 新增: defer c.mu.Unlock() | |
| 177 | 新增: c.disconnectLocked() | |
| 178 | 新增:} | |
| 179 | 新增: | |
| 168 | 180 | func (c *windowsBrowserClient) request(ctx context.Context, method, path string, input any) ([]byte, error) { |
| 169 | 181 | if !validWindowsBrowserRoute(method, path) { |
| 170 | 182 | return nil, browserInputError{errors.New("客机浏览器操作无效")} |
| @@ -281,6 +293,10 @@ func (c *windowsBrowserClient) request(ctx context.Context, method, path string, | ||
| 281 | 293 | "target_changed": "接收操作的网页已经切换,请回到原网页重试", |
| 282 | 294 | "browser_target_changed": "客机浏览器窗口已变化,请在新画面中重试", |
| 283 | 295 | "browser_focus_required": "客机浏览器未能获得前台焦点,请先激活浏览器或关闭阻挡操作的弹窗", |
| 296 | 新增: "desktop_target_changed": "弹窗正在切换,正在重新读取画面", | |
| 297 | 新增: "desktop_capture_busy": "弹窗画面正在准备", | |
| 298 | 新增: "desktop_empty_frame": "弹窗尚未返回画面", | |
| 299 | 新增: "frame_unavailable": "浏览器画面流尚未就绪", | |
| 284 | 300 | "transfer_changed": "文件传递已经关闭,请重新选择文件", |
| 285 | 301 | "frame_limit": "浏览器画面过大,请缩小窗口后重试", |
| 286 | 302 | } |
| @@ -292,7 +308,7 @@ func (c *windowsBrowserClient) request(ctx context.Context, method, path string, | ||
| 292 | 308 | diagnostics.Log("warn", "native-browser", method+" "+path+" failed ("+code+")") |
| 293 | 309 | } |
| 294 | 310 | if reason := known[code]; reason != "" { |
| 295 | 删除: return nil, errors.New(reason) | |
| 311 | 新增: return nil, windowsBrowserFailure{code: code, message: reason} | |
| 296 | 312 | } |
| 297 | 313 | return nil, errors.New("客机浏览器操作未完成,请恢复连接后核对结果") |
| 298 | 314 | } |
| @@ -47,6 +47,10 @@ func TestWindowsBrowserRelayHelper(t *testing.T) { | ||
| 47 | 47 | var result any = map[string]any{"ok": true} |
| 48 | 48 | switch request.Path { |
| 49 | 49 | case "/frame": |
| 50 | 新增: if code := os.Getenv("CAGE_BROWSER_FRAME_FAILURE"); code != "" { | |
| 51 | 新增: result = map[string]any{"error": code, "code": code} | |
| 52 | 新增: break | |
| 53 | 新增: } | |
| 50 | 54 | time.Sleep(time.Hour) |
| 51 | 55 | case "/tabs": |
| 52 | 56 | result = map[string]any{"active": "tab", "tabs": []any{}} |
| @@ -211,6 +215,29 @@ func TestWindowsBrowserCanceledFrameCannotBecomeNextViewerReply(t *testing.T) { | ||
| 211 | 215 | } |
| 212 | 216 | } |
| 213 | 217 | |
| 218 | 新增:func TestWindowsBrowserPreservesPopupFrameFailure(t *testing.T) { | |
| 219 | 新增: for _, code := range []string{"desktop_target_changed", "desktop_capture_busy", "desktop_empty_frame", "frame_unavailable"} { | |
| 220 | 新增: t.Run(code, func(t *testing.T) { | |
| 221 | 新增: client := fixtureWindowsBrowserClient(t) | |
| 222 | 新增: start := client.start | |
| 223 | 新增: client.start = func(ctx context.Context) (*exec.Cmd, error) { | |
| 224 | 新增: cmd, err := start(ctx) | |
| 225 | 新增: if err == nil { | |
| 226 | 新增: cmd.Env = append(cmd.Env, "CAGE_BROWSER_FRAME_FAILURE="+code) | |
| 227 | 新增: } | |
| 228 | 新增: return cmd, err | |
| 229 | 新增: } | |
| 230 | 新增: ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) | |
| 231 | 新增: defer cancel() | |
| 232 | 新增: _, err := client.request(ctx, "GET", "/frame", nil) | |
| 233 | 新增: var failure windowsBrowserFailure | |
| 234 | 新增: if !errors.As(err, &failure) || failure.code != code { | |
| 235 | 新增: t.Fatalf("lost structured code %s: %v", code, err) | |
| 236 | 新增: } | |
| 237 | 新增: }) | |
| 238 | 新增: } | |
| 239 | 新增:} | |
| 240 | 新增: | |
| 214 | 241 | func TestNativeBrowserEventsPreserveCopyContextAndSequence(t *testing.T) { |
| 215 | 242 | client := fixtureWindowsBrowserClient(t) |
| 216 | 243 | acknowledgement := filepath.Join(t.TempDir(), "event-cursor") |
| @@ -0,0 +1,220 @@ | ||
| 1 | 新增:package workbench | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "context" | |
| 5 | 新增: "encoding/binary" | |
| 6 | 新增: "errors" | |
| 7 | 新增: "io" | |
| 8 | 新增: "net" | |
| 9 | 新增: "reflect" | |
| 10 | 新增: "testing" | |
| 11 | 新增: "time" | |
| 12 | 新增:) | |
| 13 | 新增: | |
| 14 | 新增:func TestBrowserFocusRecoveryKeepsRFBDisplayAlive(t *testing.T) { | |
| 15 | 新增: frame := windowTestFrame(t, 2, 1) | |
| 16 | 新增: frame.ID, frame.Document = "tab", "doc" | |
| 17 | 新增: server, client := net.Pipe() | |
| 18 | 新增: defer client.Close() | |
| 19 | 新增: client.SetDeadline(time.Now().Add(5 * time.Second)) | |
| 20 | 新增: | |
| 21 | 新增: go func() { | |
| 22 | 新增: defer server.Close() | |
| 23 | 新增: serveWindowsWindowRFB(server, frame, func(input map[string]any, output any) error { | |
| 24 | 新增: if input["action"] == "frame" { | |
| 25 | 新增: *output.(*windowsWindowFrame) = frame | |
| 26 | 新增: return nil | |
| 27 | 新增: } | |
| 28 | 新增: _, err := recoverBrowserInputFocus(context.Background(), func(_ context.Context, _, path string, _ any) ([]byte, error) { | |
| 29 | 新增: switch path { | |
| 30 | 新增: case "/input": | |
| 31 | 新增: return nil, windowsBrowserFailure{"browser_focus_required", "focus"} | |
| 32 | 新增: case "/tabs": | |
| 33 | 新增: return []byte(`{"active":"tab","tabs":[{"id":"tab","document":"doc"}]}`), nil | |
| 34 | 新增: default: | |
| 35 | 新增: return []byte(`{"ok":true}`), nil | |
| 36 | 新增: } | |
| 37 | 新增: }, input, func() {}) | |
| 38 | 新增: return err | |
| 39 | 新增: }) | |
| 40 | 新增: }() | |
| 41 | 新增: read := func(n int) []byte { | |
| 42 | 新增: t.Helper() | |
| 43 | 新增: b := make([]byte, n) | |
| 44 | 新增: if _, err := io.ReadFull(client, b); err != nil { | |
| 45 | 新增: t.Fatal(err) | |
| 46 | 新增: } | |
| 47 | 新增: return b | |
| 48 | 新增: } | |
| 49 | 新增: write := func(b []byte) { | |
| 50 | 新增: t.Helper() | |
| 51 | 新增: if _, err := client.Write(b); err != nil { | |
| 52 | 新增: t.Fatal(err) | |
| 53 | 新增: } | |
| 54 | 新增: } | |
| 55 | 新增: read(12) | |
| 56 | 新增: write([]byte("RFB 003.008\n")) | |
| 57 | 新增: read(2) | |
| 58 | 新增: write([]byte{1}) | |
| 59 | 新增: read(4) | |
| 60 | 新增: write([]byte{1}) | |
| 61 | 新增: init := read(24) | |
| 62 | 新增: read(int(binary.BigEndian.Uint32(init[20:]))) | |
| 63 | 新增: // Input is accepted only after the first frame, so establish it first. | |
| 64 | 新增: write([]byte{3, 0, 0, 0, 0, 0, 0, 2, 0, 1}) | |
| 65 | 新增: read(24) | |
| 66 | 新增: // A rejected mouse event followed by a rejected key event must not close | |
| 67 | 新增: // the display socket; the next frame must still be delivered. | |
| 68 | 新增: write([]byte{5, 1, 0, 1, 0, 0}) | |
| 69 | 新增: write([]byte{4, 1, 0, 0, 0, 0, 0, 97}) | |
| 70 | 新增: write([]byte{3, 0, 0, 0, 0, 0, 0, 2, 0, 1}) | |
| 71 | 新增: update := read(16) | |
| 72 | 新增: if binary.BigEndian.Uint16(update[2:]) != 1 { | |
| 73 | 新增: t.Fatal("display frame missing after focus rejection") | |
| 74 | 新增: } | |
| 75 | 新增: read(8) | |
| 76 | 新增:} | |
| 77 | 新增: | |
| 78 | 新增:func TestBrowserFrameTransitionRetry(t *testing.T) { | |
| 79 | 新增: for _, tc := range []struct { | |
| 80 | 新增: name, code string | |
| 81 | 新增: failures, calls int | |
| 82 | 新增: fatal bool | |
| 83 | 新增: }{ | |
| 84 | 新增: {"transient", "browser_target_changed", 1, 2, false}, | |
| 85 | 新增: {"document transition", "target_changed", 1, 2, false}, | |
| 86 | 新增: {"popup capture transition", "desktop_target_changed", 2, 3, false}, | |
| 87 | 新增: {"popup capture busy", "desktop_capture_busy", 1, 2, false}, | |
| 88 | 新增: {"persistent", "browser_target_changed", 8, 6, true}, | |
| 89 | 新增: {"permission", "desktop_capture_permission_required", 1, 1, true}, | |
| 90 | 新增: {"locked", "requires_gui_login", 1, 1, true}, | |
| 91 | 新增: {"identity", "browser_process_unavailable", 1, 1, true}, | |
| 92 | 新增: } { | |
| 93 | 新增: t.Run(tc.name, func(t *testing.T) { | |
| 94 | 新增: calls := 0 | |
| 95 | 新增: data, err := requestBrowserFrame(context.Background(), func(_ context.Context, method, path string, _ any) ([]byte, error) { | |
| 96 | 新增: if method != "GET" || path != "/frame" { | |
| 97 | 新增: t.Fatal("retried a mutation") | |
| 98 | 新增: } | |
| 99 | 新增: calls++ | |
| 100 | 新增: if calls <= tc.failures { | |
| 101 | 新增: return nil, windowsBrowserFailure{tc.code, "rejected"} | |
| 102 | 新增: } | |
| 103 | 新增: return []byte("fresh validated frame"), nil | |
| 104 | 新增: }, map[string]any{"action": "frame"}) | |
| 105 | 新增: if calls != tc.calls || (err != nil) != tc.fatal { | |
| 106 | 新增: t.Fatalf("calls=%d error=%v", calls, err) | |
| 107 | 新增: } | |
| 108 | 新增: if !tc.fatal && string(data) != "fresh validated frame" { | |
| 109 | 新增: t.Fatal("missing fresh frame") | |
| 110 | 新增: } | |
| 111 | 新增: }) | |
| 112 | 新增: } | |
| 113 | 新增:} | |
| 114 | 新增: | |
| 115 | 新增:func TestBrowserFrameTransitionCancellation(t *testing.T) { | |
| 116 | 新增: ctx, cancel := context.WithCancel(context.Background()) | |
| 117 | 新增: calls := 0 | |
| 118 | 新增: _, err := requestBrowserFrame(ctx, func(context.Context, string, string, any) ([]byte, error) { | |
| 119 | 新增: calls++ | |
| 120 | 新增: cancel() | |
| 121 | 新增: return nil, windowsBrowserFailure{"browser_target_changed", "changed"} | |
| 122 | 新增: }, nil) | |
| 123 | 新增: if !errors.Is(err, context.Canceled) || calls != 1 { | |
| 124 | 新增: t.Fatalf("calls=%d err=%v", calls, err) | |
| 125 | 新增: } | |
| 126 | 新增:} | |
| 127 | 新增: | |
| 128 | 新增:func TestBrowserFocusRecoveryDoesNotReplayInput(t *testing.T) { | |
| 129 | 新增: | |
| 130 | 新增: var calls []string | |
| 131 | 新增: attempts := 0 | |
| 132 | 新增: released := 0 | |
| 133 | 新增: request := func(_ context.Context, method, path string, input any) ([]byte, error) { | |
| 134 | 新增: calls = append(calls, method+" "+path) | |
| 135 | 新增: switch path { | |
| 136 | 新增: case "/input": | |
| 137 | 新增: attempts++ | |
| 138 | 新增: if attempts == 1 { | |
| 139 | 新增: return nil, windowsBrowserFailure{"browser_focus_required", "focus rejected"} | |
| 140 | 新增: } | |
| 141 | 新增: return []byte(`{"ok":true}`), nil | |
| 142 | 新增: case "/tabs": | |
| 143 | 新增: return []byte(`{"active":"tab","tabs":[{"id":"tab","document":"doc"}]}`), nil | |
| 144 | 新增: case "/activate": | |
| 145 | 新增: if input.(map[string]any)["id"] != "tab" { | |
| 146 | 新增: t.Fatal("activated wrong tab") | |
| 147 | 新增: } | |
| 148 | 新增: return []byte(`{"ok":true}`), nil | |
| 149 | 新增: } | |
| 150 | 新增: t.Fatalf("unexpected request %s", path) | |
| 151 | 新增: return nil, nil | |
| 152 | 新增: } | |
| 153 | 新增: input := map[string]any{"kind": "key", "target": "geometry", "down": true} | |
| 154 | 新增: if _, err := recoverBrowserInputFocus(context.Background(), request, input, func() { released++ }); err != nil { | |
| 155 | 新增: t.Fatal(err) | |
| 156 | 新增: } | |
| 157 | 新增: if attempts != 1 || released != 1 { | |
| 158 | 新增: t.Fatal("input was replayed or its lease was not released") | |
| 159 | 新增: } | |
| 160 | 新增: if !reflect.DeepEqual(calls, []string{"POST /input"}) { | |
| 161 | 新增: t.Fatal(calls) | |
| 162 | 新增: } | |
| 163 | 新增: if _, err := recoverBrowserInputFocus(context.Background(), request, input, func() { released++ }); err != nil { | |
| 164 | 新增: t.Fatal(err) | |
| 165 | 新增: } | |
| 166 | 新增: if attempts != 2 || released != 1 { | |
| 167 | 新增: t.Fatal("next user input was not accepted") | |
| 168 | 新增: } | |
| 169 | 新增:} | |
| 170 | 新增: | |
| 171 | 新增:func TestBrowserFocusRecoveryPreservesBoundaries(t *testing.T) { | |
| 172 | 新增: for _, tc := range []struct { | |
| 173 | 新增: name, code, tabs string | |
| 174 | 新增: fatal bool | |
| 175 | 新增: }{ | |
| 176 | 新增: {"other tab", "browser_focus_required", `{"active":"other","tabs":[{"id":"tab","document":"doc"}]}`, false}, | |
| 177 | 新增: {"new document", "browser_focus_required", `{"active":"tab","tabs":[{"id":"tab","document":"new"}]}`, false}, | |
| 178 | 新增: {"target changed", "browser_target_changed", "", false}, | |
| 179 | 新增: {"document changed", "target_changed", "", false}, | |
| 180 | 新增: {"locked session", "requires_gui_login", "", true}, | |
| 181 | 新增: {"transport failure", "", "", true}, | |
| 182 | 新增: } { | |
| 183 | 新增: t.Run(tc.name, func(t *testing.T) { | |
| 184 | 新增: | |
| 185 | 新增: failure := error(windowsBrowserFailure{tc.code, "rejected"}) | |
| 186 | 新增: if tc.code == "" { | |
| 187 | 新增: failure = errors.New("EOF") | |
| 188 | 新增: } | |
| 189 | 新增: request := func(_ context.Context, method, path string, _ any) ([]byte, error) { | |
| 190 | 新增: switch path { | |
| 191 | 新增: case "/input": | |
| 192 | 新增: return nil, failure | |
| 193 | 新增: case "/tabs": | |
| 194 | 新增: return []byte(tc.tabs), nil | |
| 195 | 新增: default: | |
| 196 | 新增: t.Fatalf("unexpected recovery %s %s", method, path) | |
| 197 | 新增: return nil, nil | |
| 198 | 新增: } | |
| 199 | 新增: } | |
| 200 | 新增: _, err := recoverBrowserInputFocus(context.Background(), request, map[string]any{}, func() {}) | |
| 201 | 新增: if (err != nil) != tc.fatal { | |
| 202 | 新增: t.Fatalf("fatal=%v err=%v", tc.fatal, err) | |
| 203 | 新增: } | |
| 204 | 新增: }) | |
| 205 | 新增: } | |
| 206 | 新增:} | |
| 207 | 新增: | |
| 208 | 新增:func TestBrowserFocusRecoveryNeverStealsFocus(t *testing.T) { | |
| 209 | 新增: | |
| 210 | 新增: request := func(_ context.Context, _, path string, _ any) ([]byte, error) { | |
| 211 | 新增: if path != "/input" { | |
| 212 | 新增: t.Fatal("focus recovery flooded") | |
| 213 | 新增: } | |
| 214 | 新增: return nil, windowsBrowserFailure{"browser_focus_required", "focus"} | |
| 215 | 新增: } | |
| 216 | 新增: _, err := recoverBrowserInputFocus(context.Background(), request, map[string]any{}, func() {}) | |
| 217 | 新增: if err != nil { | |
| 218 | 新增: t.Fatal(err) | |
| 219 | 新增: } | |
| 220 | 新增:} | |
| @@ -51,6 +51,62 @@ func (e windowsWindowFailure) Error() string { | ||
| 51 | 51 | return "Windows 应用操作未完成,请确认客机已解锁且 Claude 窗口仍在运行" |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 新增:// Retry only validated frame reads during short window/popup transitions. | |
| 55 | 新增:// Session, identity, permission and transport failures are never suppressed. | |
| 56 | 新增:func transientWindowFrame(code string) bool { | |
| 57 | 新增: switch code { | |
| 58 | 新增: case "browser_target_changed", "target_changed", "desktop_target_changed", "desktop_capture_busy", "desktop_empty_frame": | |
| 59 | 新增: return true | |
| 60 | 新增: } | |
| 61 | 新增: return false | |
| 62 | 新增:} | |
| 63 | 新增: | |
| 64 | 新增:func requestDesktopFrame(ctx context.Context, request func(any, any) error, input map[string]any, output any) error { | |
| 65 | 新增: for attempt := 0; ; attempt++ { | |
| 66 | 新增: err := request(input, output) | |
| 67 | 新增: var failure windowsWindowFailure | |
| 68 | 新增: if attempt >= 5 || !errors.As(err, &failure) || !transientWindowFrame(failure.code) { | |
| 69 | 新增: return err | |
| 70 | 新增: } | |
| 71 | 新增: timer := time.NewTimer(80 * time.Millisecond) | |
| 72 | 新增: select { | |
| 73 | 新增: case <-ctx.Done(): | |
| 74 | 新增: timer.Stop() | |
| 75 | 新增: return ctx.Err() | |
| 76 | 新增: case <-timer.C: | |
| 77 | 新增: } | |
| 78 | 新增: } | |
| 79 | 新增:} | |
| 80 | 新增: | |
| 81 | 新增:func desktopViewerRequest(ctx context.Context, request func(any, any) error) func(map[string]any, any) error { | |
| 82 | 新增: inputSuspended := false | |
| 83 | 新增: return func(input map[string]any, output any) error { | |
| 84 | 新增: if input["action"] == "frame" { | |
| 85 | 新增: if inputSuspended { | |
| 86 | 新增: delete(input, "after") | |
| 87 | 新增: } | |
| 88 | 新增: err := requestDesktopFrame(ctx, request, input, output) | |
| 89 | 新增: if err == nil && !output.(*windowsWindowFrame).Unchanged { | |
| 90 | 新增: inputSuspended = false | |
| 91 | 新增: } | |
| 92 | 新增: return err | |
| 93 | 新增: } | |
| 94 | 新增: isInput := input["action"] == "pointer" || input["action"] == "key" || input["action"] == "text" | |
| 95 | 新增: if isInput && inputSuspended { | |
| 96 | 新增: return nil | |
| 97 | 新增: } | |
| 98 | 新增: err := request(input, output) | |
| 99 | 新增: var failure windowsWindowFailure | |
| 100 | 新增: if isInput && errors.As(err, &failure) && failure.code == "desktop_target_changed" { | |
| 101 | 新增: // The guest resets this connection's held-input lease on rejection. | |
| 102 | 新增: // Drop the old event and queued input; never replay it on a popup. | |
| 103 | 新增: inputSuspended = true | |
| 104 | 新增: return nil | |
| 105 | 新增: } | |
| 106 | 新增: return err | |
| 107 | 新增: } | |
| 108 | 新增:} | |
| 109 | 新增: | |
| 54 | 110 | type nativeWindowConnection struct { |
| 55 | 111 | cmd *exec.Cmd |
| 56 | 112 | input io.WriteCloser |
| @@ -144,7 +200,7 @@ func (s *Server) windowsDesktopVNC(w http.ResponseWriter, r *http.Request) { | ||
| 144 | 200 | } |
| 145 | 201 | defer connection.Close() |
| 146 | 202 | var frame windowsWindowFrame |
| 147 | 删除: if err = connection.request(map[string]any{"action": "frame"}, &frame); err != nil { | |
| 203 | 新增: if err = requestDesktopFrame(ctx, connection.request, map[string]any{"action": "frame"}, &frame); err != nil { | |
| 148 | 204 | http.Error(w, err.Error(), 409) |
| 149 | 205 | return |
| 150 | 206 | } |
| @@ -154,6 +210,7 @@ func (s *Server) windowsDesktopVNC(w http.ResponseWriter, r *http.Request) { | ||
| 154 | 210 | go func() { |
| 155 | 211 | defer close(done) |
| 156 | 212 | defer remote.Close() |
| 213 | 新增: request := desktopViewerRequest(ctx, connection.request) | |
| 157 | 214 | _ = serveWindowsWindowRFB(remote, frame, func(input map[string]any, output any) error { |
| 158 | 215 | s.mu.Lock() |
| 159 | 216 | keyboard := s.preferences.Keyboard |
| @@ -163,7 +220,7 @@ func (s *Server) windowsDesktopVNC(w http.ResponseWriter, r *http.Request) { | ||
| 163 | 220 | input["keysym"] = windowsGuestKeysym(key, keyboard) |
| 164 | 221 | } |
| 165 | 222 | } |
| 166 | 删除: return connection.request(input, output) | |
| 223 | 新增: return request(input, output) | |
| 167 | 224 | }) |
| 168 | 225 | }() |
| 169 | 226 | s.streamVNC(w, r, local, local, func() { cancel(); local.Close(); remote.Close(); connection.Close() }) |
| @@ -0,0 +1,115 @@ | ||
| 1 | 新增:package workbench | |
| 2 | 新增: | |
| 3 | 新增:import ( | |
| 4 | 新增: "context" | |
| 5 | 新增: "errors" | |
| 6 | 新增: "testing" | |
| 7 | 新增:) | |
| 8 | 新增: | |
| 9 | 新增:func TestDesktopFrameTransitionRetry(t *testing.T) { | |
| 10 | 新增: for _, tc := range []struct { | |
| 11 | 新增: code string | |
| 12 | 新增: failures, calls int | |
| 13 | 新增: fatal bool | |
| 14 | 新增: }{ | |
| 15 | 新增: {"desktop_target_changed", 2, 3, false}, | |
| 16 | 新增: {"desktop_empty_frame", 1, 2, false}, | |
| 17 | 新增: {"desktop_capture_busy", 1, 2, false}, | |
| 18 | 新增: {"desktop_target_changed", 8, 6, true}, | |
| 19 | 新增: {"desktop_capture_permission_required", 1, 1, true}, | |
| 20 | 新增: {"desktop_registration_invalid", 1, 1, true}, | |
| 21 | 新增: {"requires_gui_login", 1, 1, true}, | |
| 22 | 新增: } { | |
| 23 | 新增: t.Run(tc.code, func(t *testing.T) { | |
| 24 | 新增: calls := 0 | |
| 25 | 新增: var frame windowsWindowFrame | |
| 26 | 新增: err := requestDesktopFrame(context.Background(), func(input, output any) error { | |
| 27 | 新增: if input.(map[string]any)["action"] != "frame" { | |
| 28 | 新增: t.Fatal("retried mutation") | |
| 29 | 新增: } | |
| 30 | 新增: calls++ | |
| 31 | 新增: if calls <= tc.failures { | |
| 32 | 新增: return windowsWindowFailure{tc.code} | |
| 33 | 新增: } | |
| 34 | 新增: *output.(*windowsWindowFrame) = windowsWindowFrame{Target: "fresh", Width: 2, Height: 1} | |
| 35 | 新增: return nil | |
| 36 | 新增: }, map[string]any{"action": "frame"}, &frame) | |
| 37 | 新增: if calls != tc.calls || (err != nil) != tc.fatal { | |
| 38 | 新增: t.Fatalf("calls=%d error=%v", calls, err) | |
| 39 | 新增: } | |
| 40 | 新增: if !tc.fatal && frame.Target != "fresh" { | |
| 41 | 新增: t.Fatal("missing fresh validated frame") | |
| 42 | 新增: } | |
| 43 | 新增: }) | |
| 44 | 新增: } | |
| 45 | 新增:} | |
| 46 | 新增: | |
| 47 | 新增:func TestDesktopFrameTransitionCancellation(t *testing.T) { | |
| 48 | 新增: ctx, cancel := context.WithCancel(context.Background()) | |
| 49 | 新增: calls := 0 | |
| 50 | 新增: err := requestDesktopFrame(ctx, func(any, any) error { calls++; cancel(); return windowsWindowFailure{"desktop_capture_busy"} }, nil, nil) | |
| 51 | 新增: if !errors.Is(err, context.Canceled) || calls != 1 { | |
| 52 | 新增: t.Fatalf("calls=%d error=%v", calls, err) | |
| 53 | 新增: } | |
| 54 | 新增:} | |
| 55 | 新增: | |
| 56 | 新增:func TestDesktopFrameTransportFailureNotRetried(t *testing.T) { | |
| 57 | 新增: failure := errors.New("disconnected") | |
| 58 | 新增: calls := 0 | |
| 59 | 新增: err := requestDesktopFrame(context.Background(), func(any, any) error { calls++; return failure }, nil, nil) | |
| 60 | 新增: if err != failure || calls != 1 { | |
| 61 | 新增: t.Fatal("transport failure hidden") | |
| 62 | 新增: } | |
| 63 | 新增:} | |
| 64 | 新增: | |
| 65 | 新增:func TestDesktopViewerDropsOldInputUntilFullFrame(t *testing.T) { | |
| 66 | 新增: calls := 0 | |
| 67 | 新增: unchanged := true | |
| 68 | 新增: request := desktopViewerRequest(context.Background(), func(input, output any) error { | |
| 69 | 新增: calls++ | |
| 70 | 新增: if input.(map[string]any)["action"] == "frame" { | |
| 71 | 新增: if _, exists := input.(map[string]any)["after"]; exists { | |
| 72 | 新增: t.Fatal("must request a full fresh frame") | |
| 73 | 新增: } | |
| 74 | 新增: *output.(*windowsWindowFrame) = windowsWindowFrame{Unchanged: unchanged, Target: "new"} | |
| 75 | 新增: return nil | |
| 76 | 新增: } | |
| 77 | 新增: return windowsWindowFailure{"desktop_target_changed"} | |
| 78 | 新增: }) | |
| 79 | 新增: if err := request(map[string]any{"action": "pointer"}, nil); err != nil { | |
| 80 | 新增: t.Fatal(err) | |
| 81 | 新增: } | |
| 82 | 新增: for _, action := range []string{"pointer", "key", "text"} { | |
| 83 | 新增: if err := request(map[string]any{"action": action}, nil); err != nil { | |
| 84 | 新增: t.Fatal(err) | |
| 85 | 新增: } | |
| 86 | 新增: } | |
| 87 | 新增: if calls != 1 { | |
| 88 | 新增: t.Fatal("stale input was replayed") | |
| 89 | 新增: } | |
| 90 | 新增: var frame windowsWindowFrame | |
| 91 | 新增: if err := request(map[string]any{"action": "frame", "after": "old"}, &frame); err != nil { | |
| 92 | 新增: t.Fatal(err) | |
| 93 | 新增: } | |
| 94 | 新增: request(map[string]any{"action": "text"}, nil) | |
| 95 | 新增: if calls != 2 { | |
| 96 | 新增: t.Fatal("unchanged frame resumed input") | |
| 97 | 新增: } | |
| 98 | 新增: unchanged = false | |
| 99 | 新增: if err := request(map[string]any{"action": "frame", "after": "old"}, &frame); err != nil { | |
| 100 | 新增: t.Fatal(err) | |
| 101 | 新增: } | |
| 102 | 新增: request(map[string]any{"action": "key"}, nil) | |
| 103 | 新增: if calls != 4 { | |
| 104 | 新增: t.Fatal("fresh frame did not resume new input") | |
| 105 | 新增: } | |
| 106 | 新增:} | |
| 107 | 新增: | |
| 108 | 新增:func TestDesktopViewerPreservesFatalErrors(t *testing.T) { | |
| 109 | 新增: for _, code := range []string{"requires_gui_login", "desktop_registration_invalid", "desktop_pointer_invalid"} { | |
| 110 | 新增: request := desktopViewerRequest(context.Background(), func(any, any) error { return windowsWindowFailure{code} }) | |
| 111 | 新增: if request(map[string]any{"action": "pointer"}, nil) == nil { | |
| 112 | 新增: t.Fatal("hidden security failure", code) | |
| 113 | 新增: } | |
| 114 | 新增: } | |
| 115 | 新增:} | |

