| 85 | } |
| 86 | |
| 87 | func (a *API) handleDesktopVNC(rw http.ResponseWriter, r *http.Request) { |
| 88 | ctx := r.Context() |
| 89 | logger := a.logger.With(agentchat.Fields(ctx)...) |
| 90 | |
| 91 | // Start the desktop session (idempotent). |
| 92 | _, err := a.desktop.Start(ctx) |
| 93 | if err != nil { |
| 94 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 95 | Message: "Failed to start desktop session.", |
| 96 | Detail: err.Error(), |
| 97 | }) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | // Get a VNC connection. |
| 102 | vncConn, err := a.desktop.VNCConn(ctx) |
| 103 | if err != nil { |
| 104 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 105 | Message: "Failed to connect to VNC server.", |
| 106 | Detail: err.Error(), |
| 107 | }) |
| 108 | return |
| 109 | } |
| 110 | defer vncConn.Close() |
| 111 | |
| 112 | // Accept WebSocket from coderd. |
| 113 | conn, err := websocket.Accept(rw, r, &websocket.AcceptOptions{ |
| 114 | CompressionMode: websocket.CompressionDisabled, |
| 115 | }) |
| 116 | if err != nil { |
| 117 | logger.Error(ctx, "failed to accept websocket", slog.Error(err)) |
| 118 | return |
| 119 | } |
| 120 | |
| 121 | // No read limit — RFB framebuffer updates can be large. |
| 122 | conn.SetReadLimit(-1) |
| 123 | |
| 124 | wsCtx, wsNetConn := codersdk.WebsocketNetConn(ctx, conn, websocket.MessageBinary) |
| 125 | defer wsNetConn.Close() |
| 126 | |
| 127 | // Bicopy raw bytes between WebSocket and VNC TCP. |
| 128 | agentssh.Bicopy(wsCtx, wsNetConn, vncConn) |
| 129 | } |
| 130 | |
| 131 | func (a *API) handleAction(rw http.ResponseWriter, r *http.Request) { |
| 132 | ctx := r.Context() |