| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "path/filepath" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | type macBrowserRequest struct { |
| 14 | Timezone string `json:"timezone,omitempty"` |
| 15 | Network bool `json:"network,omitempty"` |
| 16 | Action string `json:"action"` |
| 17 | URL string `json:"url,omitempty"` |
| 18 | Workspace string `json:"workspace,omitempty"` |
| 19 | Folder string `json:"folder,omitempty"` |
| 20 | X int `json:"x"` |
| 21 | Y int `json:"y"` |
| 22 | DX int `json:"dx"` |
| 23 | DY int `json:"dy"` |
| 24 | Double bool `json:"double,omitempty"` |
| 25 | Key string `json:"key,omitempty"` |
| 26 | Text string `json:"text,omitempty"` |
| 27 | Meta bool `json:"meta,omitempty"` |
| 28 | Ctrl bool `json:"ctrl,omitempty"` |
| 29 | Shift bool `json:"shift,omitempty"` |
| 30 | Alt bool `json:"alt,omitempty"` |
| 31 | } |
| 32 | |
| 33 | |
| 34 | func (s *Server) macBrowserExchange(ctx context.Context, request macBrowserRequest) ([]byte, error) { |
| 35 | client := s.nativeBrowserClient() |
| 36 | if request.Action == "frame" { |
| 37 | data, err := client.request(ctx, "GET", "/frame", nil) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | var frame struct { |
| 42 | JPEG string |
| 43 | URL string |
| 44 | Title string |
| 45 | Width, Height int |
| 46 | } |
| 47 | if err = json.Unmarshal(data, &frame); err != nil || frame.JPEG == "" { |
| 48 | return nil, errors.New("请在单个客机浏览器标签中查看画面") |
| 49 | } |
| 50 | return json.Marshal(map[string]any{"image": frame.JPEG, "url": frame.URL, "title": frame.Title, "width": frame.Width, "height": frame.Height}) |
| 51 | } |
| 52 | action := request.Action |
| 53 | if action == "text" { |
| 54 | action = "insert-text" |
| 55 | } |
| 56 | if action == "editor" { |
| 57 | _, err := client.request(ctx, "POST", "/editor", map[string]string{"workspace": filepath.Base(request.Folder)}) |
| 58 | if err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | return []byte(`{"ok":true}`), nil |
| 62 | } |
| 63 | return client.request(ctx, "POST", "/"+action, request) |
| 64 | } |
| 65 | |
| 66 | func (s *Server) registerMacOSBrowserRoutes(mux routeRegistrar) { |
| 67 | mux.HandleFunc("POST /api/macos-browser/action", func(w http.ResponseWriter, r *http.Request) { |
| 68 | if s.macosGuest == nil { |
| 69 | http.NotFound(w, r) |
| 70 | return |
| 71 | } |
| 72 | s.mu.Lock() |
| 73 | ready := s.ready && !s.closing |
| 74 | s.mu.Unlock() |
| 75 | if !ready { |
| 76 | http.Error(w, "请先启动 macOS 客机", http.StatusConflict) |
| 77 | return |
| 78 | } |
| 79 | var request macBrowserRequest |
| 80 | if !readJSON(w, r, &request) { |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | request.Folder = "" |
| 85 | switch request.Action { |
| 86 | case "editor": |
| 87 | if request.Workspace == "" { |
| 88 | http.Error(w, "请先打开项目", 400) |
| 89 | return |
| 90 | } |
| 91 | if !s.op.TryLock() { |
| 92 | http.Error(w, "正在处理其他项目操作", 409) |
| 93 | return |
| 94 | } |
| 95 | project, err := s.connectProject(request.Workspace, false) |
| 96 | s.op.Unlock() |
| 97 | if err != nil || isClientMountPath(project) { |
| 98 | http.Error(w, "请先打开并授权本机项目", 409) |
| 99 | return |
| 100 | } |
| 101 | request.Folder = macOSProjectPath(s.macosGuest.User, project) |
| 102 | case "navigate": |
| 103 | u, err := url.Parse(request.URL) |
| 104 | if err != nil || u == nil || u.Hostname() == "" || u.User != nil || (u.Scheme != "http" && u.Scheme != "https") || len(request.URL) > 4096 { |
| 105 | http.Error(w, "请输入 HTTP 或 HTTPS 地址", 400) |
| 106 | return |
| 107 | } |
| 108 | case "text", "key", "click", "wheel", "back", "forward", "frame", "reload": |
| 109 | default: |
| 110 | http.Error(w, "未知的客机浏览器操作", 400) |
| 111 | return |
| 112 | } |
| 113 | request.Workspace = "" |
| 114 | if len(request.Text) > 8192 || len(request.Key) > 32 || request.X < 0 || request.Y < 0 || request.X > 1140 || request.Y > 700 || request.DX > 2000 || request.DX < -2000 || request.DY > 2000 || request.DY < -2000 { |
| 115 | http.Error(w, "输入超出范围", 400) |
| 116 | return |
| 117 | } |
| 118 | ctx, cancel := context.WithTimeout(r.Context(), 28*time.Second) |
| 119 | defer cancel() |
| 120 | data, err := s.macBrowserExchange(ctx, request) |
| 121 | if err != nil { |
| 122 | http.Error(w, "无法连接客机里的浏览器,请检查客机运行状态", 503) |
| 123 | return |
| 124 | } |
| 125 | w.Header().Set("Content-Type", "application/json") |
| 126 | w.Header().Set("Cache-Control", "no-store") |
| 127 | _, _ = w.Write(data) |
| 128 | }) |
| 129 | } |