HandleWebSocketProxy handles incoming WebSocket connections from noVNC client and proxies them to the Proxmox VNC websocket endpoint.
(w http.ResponseWriter, r *http.Request)
| 124 | // HandleWebSocketProxy handles incoming WebSocket connections from noVNC client |
| 125 | // and proxies them to the Proxmox VNC websocket endpoint. |
| 126 | func (p *WebSocketProxy) HandleWebSocketProxy(w http.ResponseWriter, r *http.Request) { |
| 127 | targetName := getTargetName(p.config) |
| 128 | p.logger.Info("Handling WebSocket proxy request for %s from %s", targetName, r.RemoteAddr) |
| 129 | p.logger.Debug("Request headers: User-Agent=%s, Origin=%s", |
| 130 | r.Header.Get("User-Agent"), r.Header.Get("Origin")) |
| 131 | |
| 132 | // Upgrade the HTTP connection to WebSocket |
| 133 | p.logger.Debug("Upgrading HTTP connection to WebSocket for %s", targetName) |
| 134 | |
| 135 | clientConn, err := p.upgrader.Upgrade(w, r, nil) |
| 136 | if err != nil { |
| 137 | p.logger.Error("Failed to upgrade connection to WebSocket for %s: %v", targetName, err) |
| 138 | http.Error(w, fmt.Sprintf("Failed to upgrade connection: %v", err), http.StatusBadRequest) |
| 139 | |
| 140 | return |
| 141 | } |
| 142 | |
| 143 | defer func() { |
| 144 | p.logger.Debug("Closing client WebSocket connection for %s", targetName) |
| 145 | clientConn.Close() |
| 146 | }() |
| 147 | |
| 148 | p.logger.Info("WebSocket connection established with client for %s", targetName) |
| 149 | |
| 150 | // Notify session about client connection |
| 151 | if p.session != nil { |
| 152 | p.session.OnClientConnected() |
| 153 | p.logger.Debug("Notified session about client connection for %s", targetName) |
| 154 | } |
| 155 | |
| 156 | // Ensure we notify session about disconnection |
| 157 | defer func() { |
| 158 | if p.session != nil { |
| 159 | p.session.OnClientDisconnected() |
| 160 | p.logger.Debug("Notified session about client disconnection for %s", targetName) |
| 161 | } |
| 162 | }() |
| 163 | |
| 164 | // Create connection to Proxmox VNC websocket |
| 165 | p.logger.Debug("Establishing connection to Proxmox VNC websocket for %s", targetName) |
| 166 | |
| 167 | proxmoxConn, err := p.connectToProxmox() |
| 168 | if err != nil { |
| 169 | p.logger.Error("Failed to connect to Proxmox VNC websocket for %s: %v", targetName, err) |
| 170 | |
| 171 | if writeErr := clientConn.WriteMessage(websocket.CloseMessage, |
| 172 | websocket.FormatCloseMessage(websocket.CloseInternalServerErr, |
| 173 | fmt.Sprintf("Failed to connect to Proxmox: %v", err))); writeErr != nil { |
| 174 | p.logger.Debug("Failed to send close message to client: %v", writeErr) |
| 175 | } |
| 176 | |
| 177 | return |
| 178 | } |
| 179 | |
| 180 | defer func() { |
| 181 | p.logger.Debug("Closing Proxmox WebSocket connection for %s", targetName) |
| 182 | proxmoxConn.Close() |
| 183 | }() |
nothing calls this directly
no test coverage detected