| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "image/jpeg" |
| 9 | "net/http" |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | |
| 14 | func (s *Server) macOSPhoneDesktop(ctx context.Context, token, op string, input phoneSurfaceInput) ([]byte, int, error) { |
| 15 | if token == "" { |
| 16 | return nil, http.StatusGone, nil |
| 17 | } |
| 18 | out, err := s.macOSWindowCall(ctx, macOSWindowRequest{Action: "phone-" + op, Phone: &input, PhoneToken: token}) |
| 19 | if err != nil { |
| 20 | return nil, http.StatusBadGateway, err |
| 21 | } |
| 22 | return macOSPhoneDesktopReply(out, op, input.ID) |
| 23 | } |
| 24 | |
| 25 | func macOSPhoneDesktopReply(out []byte, op, id string) ([]byte, int, error) { |
| 26 | limit := 4096 |
| 27 | if op == "frame" { |
| 28 | limit = 3 << 20 |
| 29 | } |
| 30 | var reply struct { |
| 31 | Error string `json:"error"` |
| 32 | ID string `json:"id"` |
| 33 | Document string `json:"document"` |
| 34 | Image string `json:"image"` |
| 35 | Width int `json:"width"` |
| 36 | Height int `json:"height"` |
| 37 | ContentWidth float64 `json:"content_width"` |
| 38 | ContentHeight float64 `json:"content_height"` |
| 39 | } |
| 40 | if len(out) > limit || json.Unmarshal(out, &reply) != nil { |
| 41 | return nil, http.StatusBadGateway, nil |
| 42 | } |
| 43 | if reply.Error != "" { |
| 44 | status := http.StatusBadGateway |
| 45 | switch reply.Error { |
| 46 | case "phone_session_ended", "gui_session_changed", "requires_gui_login": |
| 47 | status = http.StatusGone |
| 48 | case "window_changed", "phone_document_changed", "phone_focus_changed", "not_running", "no_visible_window": |
| 49 | status = http.StatusConflict |
| 50 | case "screen_recording_permission", "accessibility_permission": |
| 51 | status = http.StatusForbidden |
| 52 | case "invalid_input": |
| 53 | status = http.StatusBadRequest |
| 54 | case "not_installed": |
| 55 | status = http.StatusNotImplemented |
| 56 | } |
| 57 | return nil, status, nil |
| 58 | } |
| 59 | if reply.ID != id { |
| 60 | return nil, http.StatusBadGateway, nil |
| 61 | } |
| 62 | if op == "frame" { |
| 63 | if reply.Document == "" || len(reply.Document) > 128 || reply.Width < 240 || reply.Width > 1440 || reply.Height < 160 || reply.Height > 1600 { |
| 64 | return nil, http.StatusBadGateway, nil |
| 65 | } |
| 66 | if reply.ContentWidth <= 0 || reply.ContentWidth > 2560 || reply.ContentHeight <= 0 || reply.ContentHeight > 1600 { |
| 67 | return nil, http.StatusBadGateway, nil |
| 68 | } |
| 69 | image, err := base64.StdEncoding.DecodeString(reply.Image) |
| 70 | if err != nil { |
| 71 | return nil, http.StatusBadGateway, nil |
| 72 | } |
| 73 | config, err := jpeg.DecodeConfig(bytes.NewReader(image)) |
| 74 | if err != nil || config.Width < 1 || config.Height < 1 || config.Width > 5120 || config.Height > 3200 { |
| 75 | return nil, http.StatusBadGateway, nil |
| 76 | } |
| 77 | } |
| 78 | return out, http.StatusOK, nil |
| 79 | } |