connectToProxmox establishes a WebSocket connection to the Proxmox VNC endpoint.
()
| 254 | |
| 255 | // connectToProxmox establishes a WebSocket connection to the Proxmox VNC endpoint. |
| 256 | func (p *WebSocketProxy) connectToProxmox() (*websocket.Conn, error) { |
| 257 | targetName := getTargetName(p.config) |
| 258 | p.logger.Debug("Building Proxmox VNC websocket URL for %s", targetName) |
| 259 | |
| 260 | // Build the Proxmox VNC websocket URL |
| 261 | // Format: wss://hostname:port/api2/json/nodes/{node}/qemu/{vmid}/vncwebsocket?port={port}&vncticket={ticket} |
| 262 | var vncPath string |
| 263 | if p.config.VMType == api.VMTypeQemu { |
| 264 | vncPath = fmt.Sprintf("/api2/json/nodes/%s/qemu/%d/vncwebsocket", |
| 265 | p.config.NodeName, p.config.VMID) |
| 266 | p.logger.Debug("Using QEMU VNC path for %s: %s", targetName, vncPath) |
| 267 | } else if p.config.VMType == api.VMTypeLXC { |
| 268 | vncPath = fmt.Sprintf("/api2/json/nodes/%s/lxc/%d/vncwebsocket", |
| 269 | p.config.NodeName, p.config.VMID) |
| 270 | p.logger.Debug("Using LXC VNC path for %s: %s", targetName, vncPath) |
| 271 | } else { |
| 272 | // For node VNC shells |
| 273 | vncPath = fmt.Sprintf("/api2/json/nodes/%s/vncwebsocket", p.config.NodeName) |
| 274 | p.logger.Debug("Using node VNC path for %s: %s", targetName, vncPath) |
| 275 | } |
| 276 | |
| 277 | // Add query parameters |
| 278 | vncURL := fmt.Sprintf("wss://%s%s?port=%s&vncticket=%s", |
| 279 | p.config.ProxmoxHost, vncPath, p.config.Port, url.QueryEscape(p.config.Ticket)) |
| 280 | |
| 281 | p.logger.Debug("Proxmox VNC websocket URL for %s: %s", targetName, vncURL) |
| 282 | |
| 283 | // Create WebSocket dialer with TLS config |
| 284 | dialer := websocket.Dialer{ |
| 285 | TLSClientConfig: &tls.Config{ |
| 286 | InsecureSkipVerify: true, // Skip TLS verification for self-signed certs |
| 287 | }, |
| 288 | HandshakeTimeout: 10 * time.Second, |
| 289 | } |
| 290 | |
| 291 | p.logger.Debug("Configuring WebSocket dialer for %s (handshake timeout: 10s)", targetName) |
| 292 | |
| 293 | // Set up headers for authentication |
| 294 | headers := make(http.Header) |
| 295 | |
| 296 | if p.config.AuthToken != "" { |
| 297 | // Check if it's an API token or cookie |
| 298 | if strings.HasPrefix(p.config.AuthToken, "PVEAPIToken") { |
| 299 | headers.Set("Authorization", p.config.AuthToken) |
| 300 | p.logger.Debug("Using API token authentication for %s", targetName) |
| 301 | } else if strings.HasPrefix(p.config.AuthToken, "PVEAuthCookie=") { |
| 302 | headers.Set("Cookie", p.config.AuthToken) |
| 303 | p.logger.Debug("Using cookie authentication for %s", targetName) |
| 304 | } else { |
| 305 | // Assume API token format |
| 306 | headers.Set("Authorization", p.config.AuthToken) |
| 307 | p.logger.Debug("Using assumed API token authentication for %s", targetName) |
| 308 | } |
| 309 | } else { |
| 310 | p.logger.Debug("No authentication token provided for %s", targetName) |
| 311 | } |
| 312 | |
| 313 | // Connect to Proxmox VNC websocket |
no test coverage detected