| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/hex" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "io" |
| 11 | "net/http" |
| 12 | "net/url" |
| 13 | "strings" |
| 14 | "sync" |
| 15 | "time" |
| 16 | |
| 17 | "github.com/sylvinus/agent-vm/internal/cage" |
| 18 | "github.com/sylvinus/agent-vm/internal/mobilelink" |
| 19 | ) |
| 20 | |
| 21 | type phoneSurface string |
| 22 | |
| 23 | const ( |
| 24 | phoneBrowser phoneSurface = "browser" |
| 25 | phoneDesktop phoneSurface = "desktop" |
| 26 | ) |
| 27 | |
| 28 | func (surface phoneSurface) label() string { |
| 29 | if surface == phoneDesktop { |
| 30 | return "Claude Desktop" |
| 31 | } |
| 32 | return "远程浏览器" |
| 33 | } |
| 34 | |
| 35 | type phoneSurfaceState struct { |
| 36 | op sync.Mutex |
| 37 | session *phoneSurfaceSession |
| 38 | nativeBrowser *windowsBrowserClient |
| 39 | nativeDesktop *windowsPhoneDesktopSession |
| 40 | } |
| 41 | |
| 42 | func (p *mobilePairing) surface(kind phoneSurface) *phoneSurfaceState { |
| 43 | if kind == phoneDesktop { |
| 44 | return &p.desktop |
| 45 | } |
| 46 | return &p.browser |
| 47 | } |
| 48 | |
| 49 | func (p *mobilePairing) pairedRequestLocked(r *http.Request) bool { |
| 50 | return p.roomCtx != nil && p.roomCtx.Err() == nil && p.roomProof != "" && equalSecret(p.roomProof, r.Header.Get(mobilelink.PhoneRoomHeader)) |
| 51 | } |
| 52 | |
| 53 | func (s *Server) mobileSurfaceRoutes(mux routeRegistrar) { |
| 54 | mux.HandleFunc("GET /api/mobile/browser/favorites", s.serveFavorites) |
| 55 | mux.HandleFunc("POST /api/mobile/browser/favorites", s.serveFavorites) |
| 56 | for _, surface := range []phoneSurface{phoneBrowser, phoneDesktop} { |
| 57 | route := "/api/mobile/" + string(surface) |
| 58 | handle := func(w http.ResponseWriter, r *http.Request) { s.phoneSurfaceRequest(surface, w, r) } |
| 59 | mux.HandleFunc("POST "+route+"/open", handle) |
| 60 | mux.HandleFunc("GET "+route+"/{id}/frame", handle) |
| 61 | mux.HandleFunc("POST "+route+"/{id}/action", handle) |
| 62 | mux.HandleFunc("POST "+route+"/{id}/close", handle) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | func (p *mobilePairing) peerLocked() string { |
| 68 | if p.record == nil { |
| 69 | return "" |
| 70 | } |
| 71 | digest := sha256.Sum256(p.record.Seed) |
| 72 | return hex.EncodeToString(digest[:]) |
| 73 | } |
| 74 | |
| 75 | type phoneSurfaceSession struct { |
| 76 | releaseDisplay func() |
| 77 | id, token string |
| 78 | ctx context.Context |
| 79 | cancel context.CancelFunc |
| 80 | stopEpoch func() bool |
| 81 | startJob string |
| 82 | openInput phoneSurfaceInput |
| 83 | readyUntil time.Time |
| 84 | } |
| 85 | |
| 86 | type phoneSurfaceInput struct { |
| 87 | After string `json:"after,omitempty"` |
| 88 | ReturnFrame bool `json:"return_frame,omitempty"` |
| 89 | ID string `json:"id,omitempty"` |
| 90 | Profile string `json:"profile,omitempty"` |
| 91 | Width int `json:"width,omitempty"` |
| 92 | Height int `json:"height,omitempty"` |
| 93 | PixelRatio float64 `json:"pixel_ratio,omitempty"` |
| 94 | URL string `json:"url,omitempty"` |
| 95 | Type string `json:"type,omitempty"` |
| 96 | X *float64 `json:"x,omitempty"` |
| 97 | Y *float64 `json:"y,omitempty"` |
| 98 | DX *float64 `json:"dx,omitempty"` |
| 99 | DY *float64 `json:"dy,omitempty"` |
| 100 | Document string `json:"document,omitempty"` |
| 101 | Focus string `json:"focus,omitempty"` |
| 102 | Text string `json:"text,omitempty"` |
| 103 | Key string `json:"key,omitempty"` |
| 104 | } |
| 105 | |
| 106 | func (in phoneSurfaceInput) valid(surface phoneSurface, op string) bool { |
| 107 | if in.PixelRatio != 0 && (surface != phoneDesktop || in.PixelRatio < 1 || in.PixelRatio > 8 || |
| 108 | (op != "open" && !(op == "action" && in.Type == "resize"))) { |
| 109 | return false |
| 110 | } |
| 111 | sizeOK := in.Width >= 240 && in.Width <= 1440 && in.Height >= 160 && in.Height <= 1600 |
| 112 | urlOK := func() bool { |
| 113 | addr, err := url.Parse(in.URL) |
| 114 | return err == nil && len(in.URL) <= 16384 && (addr.Scheme == "https" || addr.Scheme == "http") && addr.Hostname() != "" && addr.User == nil |
| 115 | } |
| 116 | switch op { |
| 117 | case "open": |
| 118 | return sizeOK && (in.URL == "" || surface == phoneBrowser && urlOK()) |
| 119 | case "frame", "close": |
| 120 | return true |
| 121 | case "action": |
| 122 | if len(in.Document) > 128 || len(in.Focus) > 128 { |
| 123 | return false |
| 124 | } |
| 125 | switch in.Type { |
| 126 | case "navigate": |
| 127 | return surface == phoneBrowser && urlOK() |
| 128 | case "resize": |
| 129 | return sizeOK |
| 130 | } |
| 131 | if in.Document == "" { |
| 132 | return false |
| 133 | } |
| 134 | switch in.Type { |
| 135 | case "tap", "scroll": |
| 136 | return in.X != nil && in.Y != nil |
| 137 | case "text": |
| 138 | return in.Focus != "" && in.Text != "" && len(in.Text) <= 8192 |
| 139 | case "key": |
| 140 | if surface == phoneDesktop && len(in.Key) == 1 && in.Key[0] >= 0x20 && in.Key[0] <= 0x7e { |
| 141 | return in.Focus != "" |
| 142 | } |
| 143 | switch in.Key { |
| 144 | case "Enter": |
| 145 | return in.Focus != "" |
| 146 | case "Backspace", "Tab", "Escape", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Delete": |
| 147 | return true |
| 148 | } |
| 149 | case "back", "forward", "reload": |
| 150 | return surface == phoneBrowser |
| 151 | case "desktop", "login": |
| 152 | return surface == phoneDesktop |
| 153 | } |
| 154 | } |
| 155 | return false |
| 156 | } |
| 157 | |
| 158 | func (s *Server) phoneSurfaceNetwork(ctx context.Context) bool { |
| 159 | s.mu.Lock() |
| 160 | blocked := !s.ready || s.closing || s.networkPaused || s.proxyNetworkLost |
| 161 | s.mu.Unlock() |
| 162 | if blocked { |
| 163 | return false |
| 164 | } |
| 165 | cfg, err := loadConfig(s.configPath) |
| 166 | if err != nil { |
| 167 | return false |
| 168 | } |
| 169 | client, err := s.gateClient() |
| 170 | if err != nil { |
| 171 | return false |
| 172 | } |
| 173 | gate, err := client.Status(ctx) |
| 174 | now := time.Now() |
| 175 | return err == nil && gate.Configuration == cfg.Fingerprint() && gate.Mode == cage.GateOpen && gate.PolicyVerified && |
| 176 | !gate.Fault && gate.Until.After(now) && gate.Report != nil && gate.Report.Authorizes(gate.RunID, gate.Configuration, now) == nil |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | func (s *Server) retirePhoneSurface(surface phoneSurface, b *phoneSurfaceSession) { |
| 182 | state := s.phones.surface(surface) |
| 183 | if b == nil || state.session != b { |
| 184 | return |
| 185 | } |
| 186 | state.session = nil |
| 187 | b.stopEpoch() |
| 188 | b.cancel() |
| 189 | input := phoneSurfaceInput{ID: b.id} |
| 190 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) |
| 191 | defer cancel() |
| 192 | _, _, _ = s.phoneSurfaceGuest(ctx, surface, b.token, "close", input) |
| 193 | if b.releaseDisplay != nil { |
| 194 | b.releaseDisplay() |
| 195 | b.releaseDisplay = nil |
| 196 | } |
| 197 | if surface == phoneBrowser && state.nativeBrowser != nil { |
| 198 | state.nativeBrowser.close() |
| 199 | state.nativeBrowser = nil |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | func (s *Server) phoneSurfaceGuest(ctx context.Context, surface phoneSurface, token, op string, input phoneSurfaceInput) ([]byte, int, error) { |
| 204 | if surface == phoneDesktop && s.windowsGuest != nil { |
| 205 | return s.windowsPhoneDesktop(ctx, token, op, input) |
| 206 | } |
| 207 | if surface == phoneDesktop && s.macosGuest != nil { |
| 208 | return s.macOSPhoneDesktop(ctx, token, op, input) |
| 209 | } |
| 210 | if surface == phoneBrowser && (s.windowsGuest != nil || s.macosGuest != nil) { |
| 211 | client := s.phones.browser.nativeBrowser |
| 212 | if client == nil { |
| 213 | return nil, 410, nil |
| 214 | } |
| 215 | out, err := client.request(ctx, "POST", "/phone-browser/"+op, input) |
| 216 | if err != nil { |
| 217 | var rejected windowsPhoneBrowserError |
| 218 | if errors.As(err, &rejected) { |
| 219 | return nil, rejected.status, nil |
| 220 | } |
| 221 | return nil, 502, err |
| 222 | } |
| 223 | limit := 4096 |
| 224 | if op == "frame" || input.ReturnFrame { |
| 225 | limit = 3 << 20 |
| 226 | } |
| 227 | if len(out) > limit || !json.Valid(out) { |
| 228 | return nil, 502, nil |
| 229 | } |
| 230 | return out, 200, nil |
| 231 | } |
| 232 | data, err := json.Marshal(input) |
| 233 | if err != nil { |
| 234 | return nil, 400, err |
| 235 | } |
| 236 | response, err := s.browserClient().open(ctx, token, "POST", "/phone-"+string(surface)+"/"+op, "application/json", bytes.NewReader(data), int64(len(data)), 15*time.Second) |
| 237 | if err != nil { |
| 238 | return nil, 502, err |
| 239 | } |
| 240 | defer response.Body.Close() |
| 241 | if response.StatusCode != 200 { |
| 242 | return nil, response.StatusCode, nil |
| 243 | } |
| 244 | limit := int64(4096) |
| 245 | if op == "frame" || input.ReturnFrame { |
| 246 | limit = 3 << 20 |
| 247 | } |
| 248 | out, err := io.ReadAll(io.LimitReader(response.Body, limit+1)) |
| 249 | if err != nil { |
| 250 | return nil, 502, err |
| 251 | } |
| 252 | if int64(len(out)) > limit || !json.Valid(out) { |
| 253 | return nil, 502, nil |
| 254 | } |
| 255 | return out, 200, nil |
| 256 | } |
| 257 | |
| 258 | func (s *Server) phoneSurfaceRequest(surface phoneSurface, w http.ResponseWriter, r *http.Request) { |
| 259 | p := &s.phones |
| 260 | p.mu.Lock() |
| 261 | state := p.surface(surface) |
| 262 | room, profile := p.roomCtx, p.peerLocked() |
| 263 | allowed := p.pairedRequestLocked(r) |
| 264 | p.mu.Unlock() |
| 265 | if !allowed { |
| 266 | http.Error(w, "请通过电脑 Cage 的二维码或配对码连接这台主机,再打开"+surface.label()+"。", 403) |
| 267 | return |
| 268 | } |
| 269 | epoch, _ := r.Context().Value(mobileEpochKey{}).(context.Context) |
| 270 | if epoch == nil || epoch.Err() != nil { |
| 271 | http.Error(w, "请先解锁 Cage", 423) |
| 272 | return |
| 273 | } |
| 274 | op := r.URL.Path[strings.LastIndexByte(r.URL.Path, '/')+1:] |
| 275 | var input phoneSurfaceInput |
| 276 | if r.Method == http.MethodPost && !readJSON(w, r, &input, 48<<10) { |
| 277 | return |
| 278 | } |
| 279 | if input.After != "" || input.Profile != "" || (op != "open" && input.ID != "") || |
| 280 | (input.ReturnFrame && (surface != phoneBrowser || op != "action")) { |
| 281 | http.Error(w, "图形会话请求无效", 400) |
| 282 | return |
| 283 | } |
| 284 | if r.URL.RawQuery != "" { |
| 285 | after, ok := mobilelink.FrameAfter(r.URL.RawQuery) |
| 286 | if !ok || surface != phoneBrowser || op != "frame" { |
| 287 | http.Error(w, "画面请求无效", 400) |
| 288 | return |
| 289 | } |
| 290 | input.After = after |
| 291 | } |
| 292 | if op != "open" { |
| 293 | input.ID = r.PathValue("id") |
| 294 | } |
| 295 | if !mobileID(input.ID) { |
| 296 | http.Error(w, "手机会话编号无效", 400) |
| 297 | return |
| 298 | } |
| 299 | if !input.valid(surface, op) { |
| 300 | http.Error(w, "操作参数或画面尺寸无效", 400) |
| 301 | return |
| 302 | } |
| 303 | defaultHome := op == "open" && surface == phoneBrowser && input.URL == "" |
| 304 | if op == "open" && surface == phoneBrowser { |
| 305 | input.Profile = profile |
| 306 | if defaultHome { |
| 307 | s.mu.Lock() |
| 308 | input.URL = browserHomeURL(s.preferences) |
| 309 | s.mu.Unlock() |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | state.op.Lock() |
| 315 | defer state.op.Unlock() |
| 316 | if r.Context().Err() != nil || room.Err() != nil || epoch.Err() != nil { |
| 317 | http.Error(w, surface.label()+"授权已结束", 403) |
| 318 | return |
| 319 | } |
| 320 | b := state.session |
| 321 | if b != nil && (b.ctx.Err() != nil || op == "open" && b.id != input.ID) { |
| 322 | s.retirePhoneSurface(surface, b) |
| 323 | b = nil |
| 324 | } |
| 325 | if op != "open" && (b == nil || b.id != input.ID) { |
| 326 | http.Error(w, surface.label()+"会话已结束,请重新打开", 410) |
| 327 | return |
| 328 | } |
| 329 | if op == "close" { |
| 330 | s.retirePhoneSurface(surface, b) |
| 331 | jsonReply(w, map[string]string{"id": input.ID}) |
| 332 | return |
| 333 | } |
| 334 | if !s.phoneSurfaceNetwork(r.Context()) { |
| 335 | s.retirePhoneSurface(surface, b) |
| 336 | http.Error(w, "请在电脑启动 Cage,并确保网络检查已放行。", 409) |
| 337 | return |
| 338 | } |
| 339 | if op == "open" && b != nil { |
| 340 | jsonReply(w, map[string]any{"id": input.ID, "action_frame": surface == phoneBrowser}) |
| 341 | return |
| 342 | } |
| 343 | if b == nil { |
| 344 | sessionCtx, cancel := context.WithCancel(room) |
| 345 | s.mu.Lock() |
| 346 | token := s.credentials.Token |
| 347 | s.mu.Unlock() |
| 348 | if surface == phoneDesktop && (s.macosGuest != nil || s.windowsGuest != nil) { |
| 349 | token = randomToken() |
| 350 | } |
| 351 | b = &phoneSurfaceSession{id: input.ID, token: token, ctx: sessionCtx, cancel: cancel, stopEpoch: context.AfterFunc(epoch, cancel)} |
| 352 | if surface == phoneBrowser && s.windowsGuest != nil { |
| 353 | state.nativeBrowser = newWindowsBrowserClient(s.lifetimeContext(), s.profile) |
| 354 | } |
| 355 | if surface == phoneBrowser && s.macosGuest != nil { |
| 356 | state.nativeBrowser = s.macOSPhoneBrowserClient(s.lifetimeContext()) |
| 357 | } |
| 358 | if surface == phoneDesktop { |
| 359 | b.readyUntil = time.Now().Add(90 * time.Second) |
| 360 | } |
| 361 | state.session = b |
| 362 | if surface == phoneDesktop { |
| 363 | release, err := s.acquireDesktopView() |
| 364 | if err != nil { |
| 365 | s.retirePhoneSurface(surface, b) |
| 366 | http.Error(w, "无法恢复 Claude Desktop 画面,请重新打开", http.StatusBadGateway) |
| 367 | return |
| 368 | } |
| 369 | b.releaseDisplay = release |
| 370 | } |
| 371 | context.AfterFunc(sessionCtx, func() { state.op.Lock(); defer state.op.Unlock(); s.retirePhoneSurface(surface, b) }) |
| 372 | } |
| 373 | ctx, cancel := context.WithCancel(r.Context()) |
| 374 | stop := context.AfterFunc(b.ctx, cancel) |
| 375 | defer func() { stop(); cancel() }() |
| 376 | if b.startJob != "" || b.openInput.ID != "" { |
| 377 | job := s.jobs.snapshot() |
| 378 | if b.startJob != "" && job.ID == b.startJob && !job.Running && job.Error != "" { |
| 379 | s.retirePhoneSurface(surface, b) |
| 380 | http.Error(w, "电脑上的 Claude Desktop 未能启动,请在电脑上查看操作详情", 502) |
| 381 | return |
| 382 | } |
| 383 | if b.startJob != "" && job.ID == b.startJob && job.Running && time.Now().Before(b.readyUntil) { |
| 384 | http.Error(w, "正在打开电脑上的 Claude Desktop", http.StatusTooEarly) |
| 385 | return |
| 386 | } |
| 387 | _, status, err := s.phoneSurfaceGuest(ctx, surface, b.token, "open", b.openInput) |
| 388 | if err != nil || status != 200 { |
| 389 | if err == nil && desktopPreparing(status) && time.Now().Before(b.readyUntil) { |
| 390 | http.Error(w, "正在打开 Claude Desktop", http.StatusTooEarly) |
| 391 | return |
| 392 | } |
| 393 | s.retirePhoneSurface(surface, b) |
| 394 | http.Error(w, "无法连接电脑上的 Claude Desktop,请重新打开", 502) |
| 395 | return |
| 396 | } |
| 397 | b.startJob, b.openInput = "", phoneSurfaceInput{} |
| 398 | } |
| 399 | out, status, err := s.phoneSurfaceGuest(ctx, surface, b.token, op, input) |
| 400 | homeUnavailable := defaultHome && err == nil && (status == http.StatusBadRequest || status == http.StatusForbidden) |
| 401 | if homeUnavailable && ctx.Err() == nil { |
| 402 | |
| 403 | |
| 404 | input.URL = "" |
| 405 | out, status, err = s.phoneSurfaceGuest(ctx, surface, b.token, op, input) |
| 406 | } |
| 407 | if b.ctx.Err() != nil || epoch.Err() != nil || room.Err() != nil { |
| 408 | s.retirePhoneSurface(surface, b) |
| 409 | http.Error(w, surface.label()+"授权已结束", 410) |
| 410 | return |
| 411 | } |
| 412 | if r.Context().Err() != nil { |
| 413 | if op == "open" { |
| 414 | s.retirePhoneSurface(surface, b) |
| 415 | } |
| 416 | return |
| 417 | } |
| 418 | if surface == phoneDesktop && op == "open" && err == nil && status == http.StatusPreconditionFailed { |
| 419 | job := s.jobs.snapshot() |
| 420 | var err error |
| 421 | if !job.Running || job.Kind != "claude-desktop-start" { |
| 422 | job, err = s.submitJob("claude-desktop-start", s.startClaudeDesktop) |
| 423 | } |
| 424 | if err != nil { |
| 425 | s.retirePhoneSurface(surface, b) |
| 426 | http.Error(w, err.Error(), http.StatusConflict) |
| 427 | return |
| 428 | } |
| 429 | b.startJob, b.openInput = job.ID, input |
| 430 | jsonReply(w, map[string]string{"id": input.ID}) |
| 431 | return |
| 432 | } |
| 433 | if surface == phoneDesktop && err == nil && time.Now().Before(b.readyUntil) && desktopPreparing(status) { |
| 434 | if op == "open" { |
| 435 | b.openInput = input |
| 436 | jsonReply(w, map[string]string{"id": input.ID}) |
| 437 | return |
| 438 | } |
| 439 | if op == "frame" { |
| 440 | http.Error(w, "正在等待 Claude Desktop 窗口", http.StatusTooEarly) |
| 441 | return |
| 442 | } |
| 443 | } |
| 444 | if (op == "frame" || input.ReturnFrame) && status == 200 && !s.phoneSurfaceNetwork(ctx) { |
| 445 | s.retirePhoneSurface(surface, b) |
| 446 | http.Error(w, "电脑上的 Cage 网络检查已暂停", 409) |
| 447 | return |
| 448 | } |
| 449 | if err != nil || status != 200 { |
| 450 | if op == "open" || status == 410 || status == 404 { |
| 451 | s.retirePhoneSurface(surface, b) |
| 452 | } |
| 453 | if surface == phoneBrowser && status == http.StatusTooEarly { |
| 454 | http.Error(w, "正在启动客机浏览器", status) |
| 455 | return |
| 456 | } |
| 457 | if surface == phoneBrowser && (op == "open" || op == "frame") && status == http.StatusServiceUnavailable { |
| 458 | s.retirePhoneSurface(surface, b) |
| 459 | http.Error(w, "客机浏览器暂时无法启动,请稍后重新打开;若仍失败,请在电脑 Cage 中检查并更新客机组件。", status) |
| 460 | return |
| 461 | } |
| 462 | switch status { |
| 463 | case 400: |
| 464 | s.mu.Lock() |
| 465 | needsUpdate := s.guestUpdateRequired |
| 466 | s.mu.Unlock() |
| 467 | if op == "open" && needsUpdate { |
| 468 | http.Error(w, "请在电脑上更新 Cage 环境后再打开"+surface.label(), 409) |
| 469 | } else { |
| 470 | http.Error(w, surface.label()+"操作无效,请检查输入后再试", 400) |
| 471 | } |
| 472 | case 409: |
| 473 | if surface == phoneDesktop && op == "frame" { |
| 474 | http.Error(w, "正在等待 Claude Desktop 窗口", http.StatusTooEarly) |
| 475 | return |
| 476 | } |
| 477 | http.Error(w, "画面或焦点已变化,请查看最新画面后再操作", 409) |
| 478 | case 410: |
| 479 | var native windowsWindowFailure |
| 480 | if surface == phoneDesktop && errors.As(err, &native) && native.code == "requires_gui_login" { |
| 481 | http.Error(w, "请在电脑的 Cage 中解锁已绑定的 Windows 客机账户,再从手机重新打开 Claude Desktop。", 410) |
| 482 | } else { |
| 483 | http.Error(w, surface.label()+"会话已结束,请重新打开", 410) |
| 484 | } |
| 485 | case 404: |
| 486 | http.Error(w, "当前客机未提供"+surface.label()+",请在电脑上更新环境", 409) |
| 487 | case 403: |
| 488 | if surface == phoneDesktop && s.macosGuest != nil { |
| 489 | http.Error(w, "请在电脑的 Cage 中完成虚拟 Mac 的屏幕录制和辅助功能授权,再重新打开。", 403) |
| 490 | } else { |
| 491 | http.Error(w, "客机拒绝此次访问", 403) |
| 492 | } |
| 493 | case 426: |
| 494 | http.Error(w, "请在电脑的 Cage 中更新 Windows 客机组件,再从手机打开 Claude Desktop。", http.StatusUpgradeRequired) |
| 495 | case 501: |
| 496 | if surface == phoneDesktop { |
| 497 | http.Error(w, "请先在电脑的 Cage 中安装 Claude Desktop,再从手机打开", 501) |
| 498 | } else { |
| 499 | http.Error(w, "请在电脑上更新 Cage 环境", 501) |
| 500 | } |
| 501 | default: |
| 502 | http.Error(w, surface.label()+"暂时不可用,请检查电脑上的环境", 502) |
| 503 | } |
| 504 | return |
| 505 | } |
| 506 | if ctx.Err() != nil || epoch.Err() != nil || room.Err() != nil { |
| 507 | http.Error(w, surface.label()+"授权已结束", 410) |
| 508 | return |
| 509 | } |
| 510 | if surface == phoneDesktop && op == "frame" { |
| 511 | b.readyUntil = time.Time{} |
| 512 | } |
| 513 | if homeUnavailable { |
| 514 | jsonReply(w, map[string]any{"id": input.ID, "action_frame": true, "home_notice": "电脑的新标签页暂时无法在手机上打开。可以输入公网网址或打开收藏夹。"}) |
| 515 | return |
| 516 | } |
| 517 | |
| 518 | |
| 519 | if surface == phoneBrowser && (op == "open" || op == "frame") { |
| 520 | out = bytes.TrimSpace(out) |
| 521 | if len(out) < 2 || out[0] != '{' { |
| 522 | http.Error(w, "客机返回的浏览器状态无效", 502) |
| 523 | return |
| 524 | } |
| 525 | out = out[:len(out)-1] |
| 526 | if len(bytes.TrimSpace(out[1:])) != 0 { |
| 527 | out = append(out, ',') |
| 528 | } |
| 529 | out = append(out, `"action_frame":true}`...) |
| 530 | } |
| 531 | defer interruptHTTPOnCancel(w, r.WithContext(ctx))() |
| 532 | if surface == phoneDesktop && op == "frame" { |
| 533 | var frame map[string]json.RawMessage |
| 534 | if json.Unmarshal(out, &frame) == nil && string(frame["pixel_ratio_control"]) == "true" { |
| 535 | |
| 536 | |
| 537 | frame["pixel_ratio_supported"] = json.RawMessage("true") |
| 538 | out, _ = json.Marshal(frame) |
| 539 | } |
| 540 | } |
| 541 | if (op == "frame" || input.ReturnFrame) && len(out) > 3<<20 { |
| 542 | http.Error(w, "客机画面超过传输大小限制,请重新读取", http.StatusBadGateway) |
| 543 | return |
| 544 | } |
| 545 | w.Header().Set("Content-Type", "application/json") |
| 546 | _, _ = w.Write(out) |
| 547 | } |
| 548 | |
| 549 | func desktopPreparing(status int) bool { |
| 550 | return status == http.StatusConflict || status == http.StatusPreconditionFailed || status == http.StatusServiceUnavailable |
| 551 | } |