-
Notifications
You must be signed in to change notification settings - Fork 36
Add /json/list and /json/new CDP proxy endpoints #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ import ( | |||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||
| "log/slog" | ||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||
| "net/url" | ||||||||||||||||||||||||||
|
|
@@ -172,6 +173,40 @@ func main() { | |||||||||||||||||||||||||
| "webSocketDebuggerUrl": proxyWSURL, | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| // Proxy CDP JSON endpoints to upstream Chromium DevTools | ||||||||||||||||||||||||||
| cdpProxyHandler := func(path string) http.HandlerFunc { | ||||||||||||||||||||||||||
| client := &http.Client{Timeout: 10 * time.Second} | ||||||||||||||||||||||||||
| return func(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||
| current := upstreamMgr.Current() | ||||||||||||||||||||||||||
| if current == "" { | ||||||||||||||||||||||||||
| http.Error(w, "upstream not ready", http.StatusServiceUnavailable) | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| upstreamURL, err := url.Parse(current) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| http.Error(w, "invalid upstream URL", http.StatusInternalServerError) | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| httpURL := &url.URL{ | ||||||||||||||||||||||||||
| Scheme: "http", | ||||||||||||||||||||||||||
| Host: upstreamURL.Host, | ||||||||||||||||||||||||||
| Path: path, | ||||||||||||||||||||||||||
| RawQuery: r.URL.RawQuery, | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| resp, err := client.Get(httpURL.String()) | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor:
Suggested change
|
||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| slogger.Error("failed to proxy CDP endpoint", "path", path, "err", err) | ||||||||||||||||||||||||||
| http.Error(w, "failed to reach upstream", http.StatusBadGateway) | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| defer resp.Body.Close() | ||||||||||||||||||||||||||
| w.Header().Set("Content-Type", "application/json") | ||||||||||||||||||||||||||
| w.WriteHeader(resp.StatusCode) | ||||||||||||||||||||||||||
| io.Copy(w, resp.Body) | ||||||||||||||||||||||||||
|
Comment on lines
+203
to
+205
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two small things here: (1) consider preserving the upstream
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Proxied CDP responses expose unusable internal WebSocket URLsMedium Severity The |
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| rDevtools.Get("/json/list", cdpProxyHandler("/json/list")) | ||||||||||||||||||||||||||
| rDevtools.Get("/json/new", cdpProxyHandler("/json/new")) | ||||||||||||||||||||||||||
| rDevtools.Get("/*", func(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||
| devtoolsproxy.WebSocketProxyHandler(upstreamMgr, slogger, config.LogCDPMessages, stz).ServeHTTP(w, r) | ||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be useful to log the parse error here (right now the client gets a 500 but you lose the underlying
url.Parsecontext in logs).