Skip to content

can't connect to browser using go-rod/rod client #1208

@suntong

Description

@suntong

Not specifically gomcp issue, but trying to reach Lightpanda Cloud Go support team for go client connection demo code instead.

We tried both chromedp and go-rod, both failed after exhausted trials. Here's only the Go-Rod trial parts.

Support Request: Connecting Go-Rod Client to Lightpanda WSS Endpoint

We are attempting to connect a Go application using the github.com/go-rod/rod library to the Lightpanda Cloud endpoint for browser automation. We are consistently encountering a 400 Bad Request panic during the initial WebSocket handshake, suggesting the authentication token is not being accepted in the expected format.

Environment & Goal

  • Client Library: github.com/go-rod/rod (Version assumed to be v0.116.2 or recent)

  • Target Endpoint: wss://euwest.cloud.lightpanda.io/ws

  • Authentication: Environment variable LPD_TOKEN is set and used.

  • Goal: Successfully connect rod.New().MustConnect() to the endpoint to begin automation.

Error Encountered

The panic is:

panic: websocket bad handshake: 400 Bad request. <html><body><h1>400 Bad request</h1> Your browser sent an invalid request.

</body></html>

This strongly indicates the server is rejecting the connection based on an invalid URL or missing/malformed authentication header.

Full final test code:

package main

import (
	"log"
	"os"
	"time"

	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
)

func main() {
	// --- 1. Connection Setup ---
	lpdToken := os.Getenv("LPD_TOKEN")
	if lpdToken == "" {
		log.Fatal("LPD_TOKEN environment variable is not set. Please set it.")
	}

	// Use the WSS URL with the token as the query parameter.
	wsURL := "wss://euwest.cloud.lightpanda.io/ws?token=" + lpdToken

	log.Printf("Attempting connection via ControlURL: %s", wsURL)

	// 2. Resolve the URL string using a standard launcher utility.
	// This function is often used to ensure the URL is correctly structured for rod's network client.
	u := launcher.MustResolveURL(wsURL)

	// 3. Create a rod browser instance using the resolved URL string.
	browser := rod.New().
		ControlURL(u). // Provide the resolved URL string
		Timeout(20 * time.Second).
		MustConnect()

	defer browser.MustClose()

	// --- 4. Automation Task ---
	page := browser.MustPage("").MustWaitLoad()
	defer page.MustClose()
	
	page.MustNavigate("https://example.com")

	title := page.MustInfo().Title
	
	// --- 5. Output the result ---
	log.Printf("Successfully connected to Lightpanda Cloud using rod.")
	log.Printf("Page Title: %s", title)
}

Exhaustive Connection Attempts Made (All Failed)

We have attempted every standard and low-level method provided by the go-rod library, but all resulted in the 400 Bad Request or an "undefined" function error due to package export issues in our installed version.

Attempt # Method Used Code Pattern Result Hypothesis/Reason for Failure
1 (Standard) Token via Query Param rod.New().ControlURL("wss://.../ws?token=%s").MustConnect() 400 Bad Request Server rejects token in query parameter format.
2 (Low-Level) External WebSocket (gorilla/websocket) Attempted to use websocket.Dial to inject Authorization: Bearer header, then pass to rod.New().Client(conn) rod.Client missing required Call method. Interface mismatch: *websocket.Conn does not implement rod.CDPClient.
3 (Public API) Core launcher utilities Attempted to use launcher.MustResolveURL or launcher.MustConnectRemote Undefined function (MustResolveURL, MustConnectRemote) Functions are not publicly exported in the installed go-rod version.
4 (Final Public API) Core rod functions Attempted to use rod.WebSocket(url).Connect() and client.Dial(url) Undefined function (rod.WebSocket, client.Dial) Functions are not publicly exported in the installed go-rod version.

Request for Assistance

Since the required low-level API functions in go-rod are not publicly available in our version, and the high-level method is rejected by your server, we require the exact, working Go code snippet for establishing the go-rod connection, specifically handling the authentication for the Lightpanda WSS endpoint.

Please confirm:

  1. Should the token be passed as a URL Query Parameter (e.g., ?token=...)?

  2. Should the token be passed as an HTTP Header (e.g., Authorization: Bearer <token>)?

  3. If a header is required, what is the exact key and format (e.g., Token: <token> or Authorization: Bearer <token>)?

Thank you for your help in resolving this handshake failure.

Metadata

Metadata

Assignees

Labels

CDPChrome Debug Protocol

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions