Skip to content

feat: allow DERP headers to be set #6572

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

Merged
merged 7 commits into from
Mar 21, 2023
Merged
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
16 changes: 11 additions & 5 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,14 @@ func createUnauthenticatedClient(cmd *cobra.Command, serverURL *url.URL) (*coder
}
transport := &headerTransport{
transport: http.DefaultTransport,
headers: map[string]string{},
header: http.Header{},
}
for _, header := range headers {
parts := strings.SplitN(header, "=", 2)
if len(parts) < 2 {
return nil, xerrors.Errorf("split header %q had less than two parts", header)
}
transport.headers[parts[0]] = parts[1]
transport.header.Add(parts[0], parts[1])
}
client.HTTPClient.Transport = transport
return client, nil
Expand Down Expand Up @@ -655,12 +655,18 @@ func checkWarnings(cmd *cobra.Command, client *codersdk.Client) error {

type headerTransport struct {
transport http.RoundTripper
headers map[string]string
header http.Header
}

func (h *headerTransport) Header() http.Header {
return h.header.Clone()
}

func (h *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
for k, v := range h.headers {
req.Header.Add(k, v)
for k, v := range h.header {
for _, vv := range v {
Copy link
Member

Choose a reason for hiding this comment

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

Oh, noticed we don't seem to handle the multi-value when first parsing the headers. Should we?

Copy link
Member

Choose a reason for hiding this comment

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

We probably should, but feels like it's not super hard to add in the future anyhow.

req.Header.Add(k, vv)
}
}
return h.transport.RoundTrip(req)
}
Expand Down
8 changes: 4 additions & 4 deletions cli/scaletest.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ func scaletestCleanup() *cobra.Command {
client.HTTPClient = &http.Client{
Transport: &headerTransport{
transport: http.DefaultTransport,
headers: map[string]string{
codersdk.BypassRatelimitHeader: "true",
header: map[string][]string{
codersdk.BypassRatelimitHeader: {"true"},
},
},
}
Expand Down Expand Up @@ -515,8 +515,8 @@ It is recommended that all rate limits are disabled on the server before running
client.HTTPClient = &http.Client{
Transport: &headerTransport{
transport: http.DefaultTransport,
headers: map[string]string{
codersdk.BypassRatelimitHeader: "true",
header: map[string][]string{
codersdk.BypassRatelimitHeader: {"true"},
},
},
}
Expand Down
8 changes: 8 additions & 0 deletions codersdk/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,17 @@ func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, opti
}

ip := tailnet.IP()
var header http.Header
headerTransport, ok := c.HTTPClient.Transport.(interface {
Header() http.Header
})
if ok {
header = headerTransport.Header()
}
conn, err := tailnet.NewConn(&tailnet.Options{
Addresses: []netip.Prefix{netip.PrefixFrom(ip, 128)},
DERPMap: connInfo.DERPMap,
DERPHeader: &header,
Logger: options.Logger,
BlockEndpoints: options.BlockEndpoints,
})
Expand Down
9 changes: 7 additions & 2 deletions tailnet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net"
"net/http"
"net/netip"
"reflect"
"strconv"
Expand Down Expand Up @@ -50,8 +51,9 @@ func init() {
}

type Options struct {
Addresses []netip.Prefix
DERPMap *tailcfg.DERPMap
Addresses []netip.Prefix
DERPMap *tailcfg.DERPMap
DERPHeader *http.Header

// BlockEndpoints specifies whether P2P endpoints are blocked.
// If so, only DERPs can establish connections.
Expand Down Expand Up @@ -159,6 +161,9 @@ func NewConn(options *Options) (conn *Conn, err error) {
if !ok {
return nil, xerrors.New("get wireguard internals")
}
if options.DERPHeader != nil {
magicConn.SetDERPHeader(options.DERPHeader.Clone())
}

// Update the keys for the magic connection!
err = magicConn.SetPrivateKey(nodePrivateKey)
Expand Down