Skip to content

Commit a24f26c

Browse files
authored
fix: Allow disabling built-in DERP server (#3852)
1 parent 4f4d470 commit a24f26c

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

cli/server.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command {
329329
validatedAutoImportTemplates[i] = v
330330
}
331331

332-
derpMap, err := tailnet.NewDERPMap(ctx, &tailcfg.DERPRegion{
332+
defaultRegion := &tailcfg.DERPRegion{
333333
RegionID: derpServerRegionID,
334334
RegionCode: derpServerRegionCode,
335335
RegionName: derpServerRegionName,
@@ -341,7 +341,11 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command {
341341
STUNPort: -1,
342342
ForceHTTP: accessURLParsed.Scheme == "http",
343343
}},
344-
}, derpServerSTUNAddrs, derpConfigURL)
344+
}
345+
if !derpServerEnabled {
346+
defaultRegion = nil
347+
}
348+
derpMap, err := tailnet.NewDERPMap(ctx, defaultRegion, derpServerSTUNAddrs, derpConfigURL)
345349
if err != nil {
346350
return xerrors.Errorf("create derp map: %w", err)
347351
}

tailnet/derpmap.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ import (
1515
// NewDERPMap constructs a DERPMap from a set of STUN addresses and optionally a remote
1616
// URL to fetch a mapping from e.g. https://controlplane.tailscale.com/derpmap/default.
1717
func NewDERPMap(ctx context.Context, region *tailcfg.DERPRegion, stunAddrs []string, remoteURL string) (*tailcfg.DERPMap, error) {
18-
for index, stunAddr := range stunAddrs {
19-
host, rawPort, err := net.SplitHostPort(stunAddr)
20-
if err != nil {
21-
return nil, xerrors.Errorf("split host port for %q: %w", stunAddr, err)
22-
}
23-
port, err := strconv.Atoi(rawPort)
24-
if err != nil {
25-
return nil, xerrors.Errorf("parse port for %q: %w", stunAddr, err)
18+
if region != nil {
19+
for index, stunAddr := range stunAddrs {
20+
host, rawPort, err := net.SplitHostPort(stunAddr)
21+
if err != nil {
22+
return nil, xerrors.Errorf("split host port for %q: %w", stunAddr, err)
23+
}
24+
port, err := strconv.Atoi(rawPort)
25+
if err != nil {
26+
return nil, xerrors.Errorf("parse port for %q: %w", stunAddr, err)
27+
}
28+
region.Nodes = append([]*tailcfg.DERPNode{{
29+
Name: fmt.Sprintf("%dstun%d", region.RegionID, index),
30+
RegionID: region.RegionID,
31+
HostName: host,
32+
STUNOnly: true,
33+
STUNPort: port,
34+
}}, region.Nodes...)
2635
}
27-
region.Nodes = append([]*tailcfg.DERPNode{{
28-
Name: fmt.Sprintf("%dstun%d", region.RegionID, index),
29-
RegionID: region.RegionID,
30-
HostName: host,
31-
STUNOnly: true,
32-
STUNPort: port,
33-
}}, region.Nodes...)
3436
}
3537

3638
derpMap := &tailcfg.DERPMap{
@@ -51,10 +53,12 @@ func NewDERPMap(ctx context.Context, region *tailcfg.DERPRegion, stunAddrs []str
5153
return nil, xerrors.Errorf("fetch derpmap: %w", err)
5254
}
5355
}
54-
_, conflicts := derpMap.Regions[region.RegionID]
55-
if conflicts {
56-
return nil, xerrors.Errorf("the default region ID conflicts with a remote region from %q", remoteURL)
56+
if region != nil {
57+
_, conflicts := derpMap.Regions[region.RegionID]
58+
if conflicts {
59+
return nil, xerrors.Errorf("the default region ID conflicts with a remote region from %q", remoteURL)
60+
}
61+
derpMap.Regions[region.RegionID] = region
5762
}
58-
derpMap.Regions[region.RegionID] = region
5963
return derpMap, nil
6064
}

0 commit comments

Comments
 (0)