mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: use client preferred URL for the default DERP (#18911)
The agentsdk currently does a remap of the DERP map to change the EmbeddedRelay node's URL to match the agent's access URL. This PR makes changes to the `workspacesdk` (used by clients like the CLI) and `vpn` (used by Coder Desktop) to match this behavior. This enables us the ability to try Coder clients in dogfood over a VPN without changing the global access URL.
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"cloud.google.com/go/compute/metadata"
|
||||
@@ -27,6 +26,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/coder/v2/codersdk/drpcsdk"
|
||||
"github.com/coder/coder/v2/tailnet"
|
||||
tailnetproto "github.com/coder/coder/v2/tailnet/proto"
|
||||
)
|
||||
|
||||
@@ -126,40 +126,13 @@ type Script struct {
|
||||
Script string `json:"script"`
|
||||
}
|
||||
|
||||
// RewriteDERPMap rewrites the DERP map to use the access URL of the SDK as the
|
||||
// "embedded relay" access URL. The passed derp map is modified in place.
|
||||
// RewriteDERPMap rewrites the DERP map to use the configured access URL of the
|
||||
// agent as the "embedded relay" access URL.
|
||||
//
|
||||
// Agents can provide an arbitrary access URL that may be different that the
|
||||
// globally configured one. This breaks the built-in DERP, which would continue
|
||||
// to reference the global access URL.
|
||||
// See tailnet.RewriteDERPMapDefaultRelay for more details on why this is
|
||||
// necessary.
|
||||
func (c *Client) RewriteDERPMap(derpMap *tailcfg.DERPMap) {
|
||||
accessingPort := c.SDK.URL.Port()
|
||||
if accessingPort == "" {
|
||||
accessingPort = "80"
|
||||
if c.SDK.URL.Scheme == "https" {
|
||||
accessingPort = "443"
|
||||
}
|
||||
}
|
||||
accessPort, err := strconv.Atoi(accessingPort)
|
||||
if err != nil {
|
||||
// this should never happen because URL.Port() returns the empty string if the port is not
|
||||
// valid.
|
||||
c.SDK.Logger().Critical(context.Background(), "failed to parse URL port", slog.F("port", accessingPort))
|
||||
}
|
||||
for _, region := range derpMap.Regions {
|
||||
if !region.EmbeddedRelay {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, node := range region.Nodes {
|
||||
if node.STUNOnly {
|
||||
continue
|
||||
}
|
||||
node.HostName = c.SDK.URL.Hostname()
|
||||
node.DERPPort = accessPort
|
||||
node.ForceHTTP = c.SDK.URL.Scheme == "http"
|
||||
}
|
||||
}
|
||||
tailnet.RewriteDERPMapDefaultRelay(context.Background(), c.SDK.Logger(), derpMap, c.SDK.URL)
|
||||
}
|
||||
|
||||
// ConnectRPC20 returns a dRPC client to the Agent API v2.0. Notably, it is missing
|
||||
|
||||
@@ -5,10 +5,12 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
"tailscale.com/tailcfg"
|
||||
|
||||
"cdr.dev/slog/sloggers/slogtest"
|
||||
"github.com/coder/coder/v2/codersdk/agentsdk"
|
||||
@@ -120,3 +122,31 @@ func TestStreamAgentReinitEvents(t *testing.T) {
|
||||
require.ErrorIs(t, receiveErr, context.Canceled)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRewriteDERPMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
// This test ensures that RewriteDERPMap mutates built-in DERPs with the
|
||||
// client access URL.
|
||||
dm := &tailcfg.DERPMap{
|
||||
Regions: map[int]*tailcfg.DERPRegion{
|
||||
1: {
|
||||
EmbeddedRelay: true,
|
||||
RegionID: 1,
|
||||
Nodes: []*tailcfg.DERPNode{{
|
||||
HostName: "bananas.org",
|
||||
DERPPort: 1,
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
parsed, err := url.Parse("https://coconuts.org:44558")
|
||||
require.NoError(t, err)
|
||||
client := agentsdk.New(parsed)
|
||||
client.RewriteDERPMap(dm)
|
||||
region := dm.Regions[1]
|
||||
require.True(t, region.EmbeddedRelay)
|
||||
require.Len(t, region.Nodes, 1)
|
||||
node := region.Nodes[0]
|
||||
require.Equal(t, "coconuts.org", node.HostName)
|
||||
require.Equal(t, 44558, node.DERPPort)
|
||||
}
|
||||
|
||||
@@ -193,6 +193,15 @@ type DialAgentOptions struct {
|
||||
EnableTelemetry bool
|
||||
}
|
||||
|
||||
// RewriteDERPMap rewrites the DERP map to use the configured access URL of the
|
||||
// client as the "embedded relay" access URL.
|
||||
//
|
||||
// See tailnet.RewriteDERPMapDefaultRelay for more details on why this is
|
||||
// necessary.
|
||||
func (c *Client) RewriteDERPMap(derpMap *tailcfg.DERPMap) {
|
||||
tailnet.RewriteDERPMapDefaultRelay(context.Background(), c.client.Logger(), derpMap, c.client.URL)
|
||||
}
|
||||
|
||||
func (c *Client) DialAgent(dialCtx context.Context, agentID uuid.UUID, options *DialAgentOptions) (agentConn *AgentConn, err error) {
|
||||
if options == nil {
|
||||
options = &DialAgentOptions{}
|
||||
@@ -248,6 +257,8 @@ func (c *Client) DialAgent(dialCtx context.Context, agentID uuid.UUID, options *
|
||||
telemetrySink = basicTel
|
||||
controller.TelemetryCtrl = basicTel
|
||||
}
|
||||
|
||||
c.RewriteDERPMap(connInfo.DERPMap)
|
||||
conn, err := tailnet.NewConn(&tailnet.Options{
|
||||
Addresses: []netip.Prefix{netip.PrefixFrom(ip, 128)},
|
||||
DERPMap: connInfo.DERPMap,
|
||||
@@ -270,7 +281,7 @@ func (c *Client) DialAgent(dialCtx context.Context, agentID uuid.UUID, options *
|
||||
coordCtrl := tailnet.NewTunnelSrcCoordController(options.Logger, conn)
|
||||
coordCtrl.AddDestination(agentID)
|
||||
controller.CoordCtrl = coordCtrl
|
||||
controller.DERPCtrl = tailnet.NewBasicDERPController(options.Logger, conn)
|
||||
controller.DERPCtrl = tailnet.NewBasicDERPController(options.Logger, c, conn)
|
||||
controller.Run(ctx)
|
||||
|
||||
options.Logger.Debug(ctx, "running tailnet API v2+ connector")
|
||||
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
"github.com/coder/coder/v2/codersdk/agentsdk"
|
||||
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
||||
"github.com/coder/coder/v2/tailnet"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
@@ -43,7 +42,7 @@ func TestWorkspaceRewriteDERPMap(t *testing.T) {
|
||||
}
|
||||
parsed, err := url.Parse("https://coconuts.org:44558")
|
||||
require.NoError(t, err)
|
||||
client := agentsdk.New(parsed)
|
||||
client := workspacesdk.New(codersdk.New(parsed))
|
||||
client.RewriteDERPMap(dm)
|
||||
region := dm.Regions[1]
|
||||
require.True(t, region.EmbeddedRelay)
|
||||
|
||||
Reference in New Issue
Block a user