Skip to content

Commit

Permalink
http: return proxy URL in place of chromium's URL
Browse files Browse the repository at this point in the history
  • Loading branch information
nadiamoe committed Jul 23, 2024
1 parent 823d619 commit 5eeb604
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
43 changes: 9 additions & 34 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package http
import (
"encoding/json"
"log/slog"
"net"
"net/http"
"net/url"
"path"

"github.com/grafana/crocochrome"
"github.com/koding/websocketproxy"
Expand Down Expand Up @@ -54,17 +54,15 @@ func (s *Server) Create(rw http.ResponseWriter, r *http.Request) {
return
}

// Chromium is guaranteed to run on the same host as this server, and we make it listen in 0.0.0.0. However,
// chromium always returns `localhost` as the URL host. Here we replace `localhost` in that URL with the host being
// used to reach this service.
newURL, err := replaceHost(session.ChromiumVersion.WebSocketDebuggerURL, r.Host)
if err != nil {
s.logger.Error("replacing chromium url with host header", "err", err)
rw.WriteHeader(http.StatusInternalServerError)
return
// Replace chromium's listen url with our proxy's for this session.
// Copy request's url to use the same scheme, host and port the client used to connect to us, as it is guaranteed
// that's correct.
proxyUrl := url.URL{
Scheme: "ws", // TODO: For some reason we cannot copy this from the request URL.
Host: r.Host,
Path: path.Join("/", "proxy", session.ID),
}

session.ChromiumVersion.WebSocketDebuggerURL = newURL
session.ChromiumVersion.WebSocketDebuggerURL = proxyUrl.String()

rw.Header().Add("content-type", "application/json")
_ = json.NewEncoder(rw).Encode(session)
Expand Down Expand Up @@ -119,26 +117,3 @@ func (s *Server) Proxy(rw http.ResponseWriter, r *http.Request) {
}
wsp.ServeHTTP(rw, r)
}

// replaceHost returns a new url with its hostname replaced with host. The port is kept as it is.
func replaceHost(urlStr, host string) (string, error) {
parsedURL, err := url.Parse(urlStr)
if err != nil {
return "", err
}

_, port, err := net.SplitHostPort(parsedURL.Host)
if err != nil {
return "", err
}

// Get rid of the port if a port is present in host.
host, _, err = net.SplitHostPort(host)
if err != nil {
return "", err
}

parsedURL.Host = net.JoinHostPort(host, port)

return parsedURL.String(), nil
}
5 changes: 3 additions & 2 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"

"github.com/grafana/crocochrome"
Expand Down Expand Up @@ -63,8 +64,8 @@ func TestHTTP(t *testing.T) {
t.Fatalf("expected returned url to have 127.0.0.1 as host, got %q", response.ChromiumVersion.WebSocketDebuggerURL)
}

if parsedURL.Port() != "9222" {
t.Fatalf("expected returned url to have 9222 as port, got %q", response.ChromiumVersion.WebSocketDebuggerURL)
if !strings.HasPrefix(parsedURL.Path, "/proxy/") {
t.Fatalf("expected returned url to be replaced to /proxy, got %q", parsedURL.String())
}
})
}

0 comments on commit 5eeb604

Please sign in to comment.