mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: Allow disabling built-in DERP server (#3852)
This commit is contained in:
+23
-19
@@ -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)
|
||||
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...)
|
||||
}
|
||||
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...)
|
||||
}
|
||||
|
||||
derpMap := &tailcfg.DERPMap{
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user