Skip to content

fix: Allow disabling built-in DERP server #3852

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 1 commit into from
Sep 2, 2022
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
8 changes: 6 additions & 2 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command {
validatedAutoImportTemplates[i] = v
}

derpMap, err := tailnet.NewDERPMap(ctx, &tailcfg.DERPRegion{
defaultRegion := &tailcfg.DERPRegion{
RegionID: derpServerRegionID,
RegionCode: derpServerRegionCode,
RegionName: derpServerRegionName,
Expand All @@ -341,7 +341,11 @@ func Server(newAPI func(*coderd.Options) *coderd.API) *cobra.Command {
STUNPort: -1,
ForceHTTP: accessURLParsed.Scheme == "http",
}},
}, derpServerSTUNAddrs, derpConfigURL)
}
if !derpServerEnabled {
defaultRegion = nil
}
derpMap, err := tailnet.NewDERPMap(ctx, defaultRegion, derpServerSTUNAddrs, derpConfigURL)
if err != nil {
return xerrors.Errorf("create derp map: %w", err)
}
Expand Down
42 changes: 23 additions & 19 deletions tailnet/derpmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ import (
// NewDERPMap constructs a DERPMap from a set of STUN addresses and optionally a remote
// URL to fetch a mapping from e.g. https://controlplane.tailscale.com/derpmap/default.
func NewDERPMap(ctx context.Context, region *tailcfg.DERPRegion, stunAddrs []string, remoteURL string) (*tailcfg.DERPMap, error) {
for index, stunAddr := range stunAddrs {
host, rawPort, err := net.SplitHostPort(stunAddr)
if err != nil {
return nil, xerrors.Errorf("split host port for %q: %w", stunAddr, err)
}
port, err := strconv.Atoi(rawPort)
if err != nil {
return nil, xerrors.Errorf("parse port for %q: %w", stunAddr, err)
if region != nil {
for index, stunAddr := range stunAddrs {
host, rawPort, err := net.SplitHostPort(stunAddr)
if err != nil {
return nil, xerrors.Errorf("split host port for %q: %w", stunAddr, err)
}
port, err := strconv.Atoi(rawPort)
if err != nil {
return nil, xerrors.Errorf("parse port for %q: %w", stunAddr, err)
}
region.Nodes = append([]*tailcfg.DERPNode{{
Name: fmt.Sprintf("%dstun%d", region.RegionID, index),
RegionID: region.RegionID,
HostName: host,
STUNOnly: true,
STUNPort: port,
}}, region.Nodes...)
}
region.Nodes = append([]*tailcfg.DERPNode{{
Name: fmt.Sprintf("%dstun%d", region.RegionID, index),
RegionID: region.RegionID,
HostName: host,
STUNOnly: true,
STUNPort: port,
}}, region.Nodes...)
}

derpMap := &tailcfg.DERPMap{
Expand All @@ -51,10 +53,12 @@ func NewDERPMap(ctx context.Context, region *tailcfg.DERPRegion, stunAddrs []str
return nil, xerrors.Errorf("fetch derpmap: %w", err)
}
}
_, conflicts := derpMap.Regions[region.RegionID]
if conflicts {
return nil, xerrors.Errorf("the default region ID conflicts with a remote region from %q", remoteURL)
if region != nil {
_, conflicts := derpMap.Regions[region.RegionID]
if conflicts {
return nil, xerrors.Errorf("the default region ID conflicts with a remote region from %q", remoteURL)
}
derpMap.Regions[region.RegionID] = region
}
derpMap.Regions[region.RegionID] = region
return derpMap, nil
}