Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
Expand All @@ -30,6 +31,8 @@ type Allocator interface {
// Cancelling the allocator context will already perform this operation,
// so normally there's no need to call Wait directly.
Wait()

getDialHeader() http.Header
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure we need this private method in the Interface? This is usually for external use, internally we should have access to the real implementation ad not through the interface

}

// setupExecAllocator is similar to NewExecAllocator, but it allows NewContext
Expand Down Expand Up @@ -327,6 +330,10 @@ func (a *ExecAllocator) Wait() {
a.wg.Wait()
}

func (a *ExecAllocator) getDialHeader() http.Header {
return nil
}

// ExecPath returns an ExecAllocatorOption which uses the given path to execute
// browser processes. The given path can be an absolute path to a binary, or
// just the name of the program to find via exec.LookPath.
Expand Down Expand Up @@ -552,6 +559,8 @@ type RemoteAllocator struct {
modifyURLFunc func(ctx context.Context, wsURL string) (string, error)

wg sync.WaitGroup

dialHeader http.Header
}

// Allocate satisfies the Allocator interface.
Expand Down Expand Up @@ -583,6 +592,7 @@ func (a *RemoteAllocator) Allocate(ctx context.Context, opts ...BrowserOption) (
a.wg.Done()
}()

opts = append(opts, WithDialHeaderBrowser(FromContext(ctx).Allocator.getDialHeader()))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see why we need it in the interface

browser, err := NewBrowser(wctx, wsURL, opts...)
if err != nil {
return nil, err
Expand All @@ -605,8 +615,23 @@ func (a *RemoteAllocator) Wait() {
a.wg.Wait()
}

func (a *RemoteAllocator) getDialHeader() http.Header {
return a.dialHeader
}

// NoModifyURL is a RemoteAllocatorOption that prevents the remote allocator
// from modifying the websocket debugger URL passed to it.
func NoModifyURL(a *RemoteAllocator) {
a.modifyURLFunc = nil
}

func WithDialHeader(h http.Header) RemoteAllocatorOption {
return func(a *RemoteAllocator) {
if a.dialHeader == nil {
a.dialHeader = make(http.Header)
}
for k, v := range h {
a.dialHeader[k] = v
}
}
}
18 changes: 17 additions & 1 deletion browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"os"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -41,6 +42,7 @@ type Browser struct {
closingGracefully chan struct{}

dialTimeout time.Duration
dialHeader http.Header

// pages keeps track of the attached targets, indexed by each's session
// ID. The only reason this is a field is so that the tests can check the
Expand Down Expand Up @@ -109,7 +111,7 @@ func NewBrowser(ctx context.Context, urlstr string, opts ...BrowserOption) (*Bro
}

var err error
b.conn, err = DialContext(dialCtx, urlstr, WithConnDebugf(b.dbgf))
b.conn, err = DialContext(dialCtx, urlstr, b.dialHeader, WithConnDebugf(b.dbgf))
if err != nil {
return nil, fmt.Errorf("could not dial %q: %w", urlstr, err)
}
Expand Down Expand Up @@ -358,3 +360,17 @@ func WithConsolef(f func(string, ...interface{})) BrowserOption {
func WithDialTimeout(d time.Duration) BrowserOption {
return func(b *Browser) { b.dialTimeout = d }
}

func WithDialHeaderBrowser(header http.Header) BrowserOption {
if header == nil {
return func(b *Browser) {}
}
return func(b *Browser) {
if b.dialHeader == nil {
b.dialHeader = make(http.Header)
}
for k, v := range header {
b.dialHeader[k] = v
}
}
}
9 changes: 7 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"io"
"net"
"net/http"

"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
Expand Down Expand Up @@ -41,9 +42,13 @@ type Conn struct {
}

// DialContext dials the specified websocket URL using gobwas/ws.
func DialContext(ctx context.Context, urlstr string, opts ...DialOption) (*Conn, error) {
func DialContext(ctx context.Context, urlstr string, header http.Header, opts ...DialOption) (*Conn, error) {
// connect
conn, br, _, err := ws.Dial(ctx, urlstr)
// h := FromContext(ctx).Allocator.getDialHeader()
dialer := ws.Dialer{
Header: ws.HandshakeHeaderHTTP(header),
}
conn, br, _, err := dialer.Dial(ctx, urlstr)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func modifyURL(ctx context.Context, urlstr string) (string, error) {

// to get "webSocketDebuggerUrl" in the response
req, err := http.NewRequestWithContext(lctx, "GET", u.String(), nil)
req.Header = FromContext(ctx).Allocator.getDialHeader()

if err != nil {
return "", err
}
Expand Down